X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Fresp.c;h=7de5c90c76150cb3238731a652e9f46a3cc2619c;hb=dd55bfef25d9301b797b24080fee710b05d12849;hp=c1b74b9920802c27744933e28e8e4a1c53580e84;hpb=46e6630278db9fb3b7aa7150d28fcd90993a5cc9;p=ashd.git diff --git a/lib/resp.c b/lib/resp.c index c1b74b9..7de5c90 100644 --- a/lib/resp.c +++ b/lib/resp.c @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -29,10 +30,45 @@ #include #include +static char safechars[128] = { + /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, +}; + +char *urlquote(char *text) +{ + static char *ret = NULL; + struct charbuf buf; + unsigned char c; + + if(ret != NULL) + free(ret); + bufinit(buf); + for(; *text; text++) { + c = *text; + if(!c < 128 && safechars[(int)c]) + bufadd(buf, *text); + else + bprintf(&buf, "%%%02X", (int)c); + } + bufadd(buf, 0); + return(ret = buf.b); +} + char *htmlquote(char *text) { + static char *ret = NULL; struct charbuf buf; + if(ret != NULL) + free(ret); bufinit(buf); for(; *text; text++) { if(*text == '<') @@ -41,25 +77,25 @@ char *htmlquote(char *text) bufcatstr(buf, ">"); else if(*text == '&') bufcatstr(buf, "&"); + else if(*text == '\"') + bufcatstr(buf, """); else bufadd(buf, *text); } bufadd(buf, 0); - return(buf.b); + return(ret = buf.b); } void simpleerror(int fd, int code, char *msg, char *fmt, ...) { struct charbuf buf; - char *tmp1, *tmp2; + char *tmp; va_list args; FILE *out; va_start(args, fmt); - tmp1 = vsprintf2(fmt, args); + tmp = vsprintf2(fmt, args); va_end(args); - tmp2 = htmlquote(tmp1); - free(tmp1); bufinit(buf); bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); @@ -69,11 +105,10 @@ void simpleerror(int fd, int code, char *msg, char *fmt, ...) bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); bprintf(&buf, "

%s

\r\n", msg); - bprintf(&buf, "

%s

\r\n", tmp2); + bprintf(&buf, "

%s

\r\n", htmlquote(tmp)); bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); - free(tmp2); - out = fdopen(fd, "w"); + out = fdopen(dup(fd), "w"); fprintf(out, "HTTP/1.1 %i %s\n", code, msg); fprintf(out, "Content-Type: text/html\n"); fprintf(out, "Content-Length: %zi\n", buf.d); @@ -86,7 +121,7 @@ void simpleerror(int fd, int code, char *msg, char *fmt, ...) void stdredir(struct hthead *req, int fd, int code, char *dst) { FILE *out; - char *sp, *cp, *ep, *path, *url, *adst, *proto, *host; + char *sp, *cp, *ep, *qs, *path, *url, *adst, *proto, *host; sp = strchr(dst, '/'); cp = strchr(dst, ':'); @@ -101,21 +136,24 @@ void stdredir(struct hthead *req, int fd, int code, char *dst) adst = sstrdup(dst); } else { if(*dst == '/') { - path = sstrdup(dst); + path = sstrdup(dst + 1); } else { if((*(url = req->url)) == '/') url++; - if((ep = strrchr(url, '/')) != NULL) - ep++; - else - ep = url; - path = sprintf2("%.*s%s", ep - url, url, dst); + if((ep = strchr(url, '?')) == NULL) { + ep = url + strlen(url); + qs = ""; + } else { + qs = ep; + } + for(; (ep > url) && (ep[-1] != '/'); ep--); + path = sprintf2("%.*s%s%s", ep - url, url, dst, qs); } adst = sprintf2("%s://%s/%s", proto, host, path); free(path); } } - out = fdopen(fd, "w"); + out = fdopen(dup(fd), "w"); fprintf(out, "HTTP/1.1 %i Redirection\n", code); fprintf(out, "Content-Length: 0\n"); fprintf(out, "Location: %s\n", adst);