aboutsummaryrefslogtreecommitdiff
path: root/wiki.c
diff options
context:
space:
mode:
authorthe little girl <yui@blekksprut.net>2014-01-10 16:34:42 +0100
committerthe little girl <yui@blekksprut.net>2014-01-10 16:34:42 +0100
commitd50bab21d036edcc1d7b54b913cb422c3c2143e9 (patch)
tree814d66b725d0c089c582ee9d345ae30d5475c15d /wiki.c
parentd6fcd7292c099422ba8c385f9f76836f8889ac1c (diff)
downloadukulele-d50bab21d036edcc1d7b54b913cb422c3c2143e9.tar.xz
ordentlig encoding
Diffstat (limited to 'wiki.c')
-rw-r--r--wiki.c54
1 files changed, 35 insertions, 19 deletions
diff --git a/wiki.c b/wiki.c
index 5dc3c55..4dea629 100644
--- a/wiki.c
+++ b/wiki.c
@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#include <glob.h>
#include <libgen.h>
#include <time.h>
@@ -103,11 +104,10 @@ int unauthorized(int status) {
int edit(char *path) {
if(!writable(path)) return unauthorized(403);
FILE *fp = fopen(path, "r");
- char *page = clean(path);
fp ? head(path, path, "vis") : head(path, NULL, NULL);
char buffer[sysconf(_SC_PAGESIZE)];
- printf("<form method='post' enctype='text/plain'>\n");
- printf("<p><textarea name=text rows=24 cols=72>");
+ printf("<form method='post'>\n");
+ printf("<p><textarea name=t rows=24 cols=72>");
if(fp)
while(fgets(buffer, sizeof(buffer), fp))
printf("%s", buffer);
@@ -119,38 +119,56 @@ int edit(char *path) {
return 0;
}
-char *loft = ".loft";
-int store(char *raw, char *data, int len) {
+int store(char *raw, int len) {
char *dir = dirname(strdup(raw));
char *id = basename(strdup(raw));
if(chdir(dir))
return problem(404, "Not found");
char path[strlen(id) + strlen(loft) + 256];
time_t epoch = time(NULL);
- snprintf(path, 512, "%s/%s.%d", loft, id, epoch);
+ snprintf(path, 512, "%s/%s.%d", loft, id, (int) epoch);
FILE *fp = fopen(path, "w");
- if(!fp) return redirect(id);
- fwrite(data + 5, len - 5, 1, fp);
+ if(!fp) return redirect("FFFF");
+
+ int pos = 0;
+ unsigned int decoded;
+ char buffer[3];
+ for(int i = 0; i < len; i++) {
+ buffer[pos] = getchar();
+ if(buffer[pos] == '+') buffer[pos] = ' ';
+ if(buffer[pos] == '&') { buffer[pos] = '\0'; break; }
+ if(pos == 2) {
+ if(buffer[0] == '%' && isxdigit(buffer[1]) && isxdigit(buffer[2])) {
+ sscanf(buffer, "%%%2x", &decoded);
+ fprintf(fp, "%c", decoded);
+ memset(buffer, 0, 3);
+ pos = 0;
+ } else {
+ fprintf(fp, "%c", buffer[0]);
+ memmove(buffer, &buffer[1], 2);
+ buffer[2] = 0;
+ }
+ }
+ else pos++;
+ }
+ fprintf(fp, "%.3s", buffer);
fputc('\0', fp);
fclose(fp);
unlink(id);
symlink(path, id);
+ return 0;
}
int post(char *path) {
- char *header, *data;
+ char *header;
int clen;
header = getenv("CONTENT_LENGTH");
if(!header) return redirect(path);
clen = atoi(header);
- data = calloc(1, clen + 1);
- fread(data, clen, 1, stdin);
- if(strncmp(data, "text=", 5))
- return redirect(path);
-
- if(store(path, data, clen)) {
-
- }
+ while(clen > 1)
+ if(clen--, getchar() == 't')
+ if(clen--, getchar() == '=')
+ store(path, clen);
return redirect(path);
}
@@ -169,7 +187,6 @@ int list(char *raw, int dir) {
foot(0);
return 0;
}
-
head("treff", NULL, NULL);
char *path;
printf("<ul class='glob'>");
@@ -202,7 +219,6 @@ int view(char *path) {
return list(path, 1);
FILE *fp = fopen(path, "r");
if(!fp) return redirect("?e");
- char *page = clean(path);
writable(path) ? head(path, "?e", "rediger") : head(path, NULL, NULL);
marxup(fp, stdout, NULL);
foot(1);