X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Fresp.c;h=25fb2cb40b3962132e93f00872d5d5b26f349445;hb=62e76c423b0621726c5da1cf9e1bd88005a863eb;hp=1b08b00c55cf1a8eb94382d25c9514e84162cb69;hpb=3095582d21be4087f774f5a68a2a9abfa37dd1ff;p=ashd.git diff --git a/lib/resp.c b/lib/resp.c index 1b08b00..25fb2cb 100644 --- a/lib/resp.c +++ b/lib/resp.c @@ -86,16 +86,12 @@ char *htmlquote(char *text) return(ret = buf.b); } -void simpleerror(int fd, int code, char *msg, char *fmt, ...) +static void simpleerror2v(FILE *out, int code, char *msg, char *fmt, va_list args) { struct charbuf buf; char *tmp; - va_list args; - FILE *out; - va_start(args, fmt); tmp = vsprintf2(fmt, args); - va_end(args); bufinit(buf); bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); @@ -108,20 +104,40 @@ void simpleerror(int fd, int code, char *msg, char *fmt, ...) bprintf(&buf, "

%s

\r\n", htmlquote(tmp)); bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); - 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); fprintf(out, "\n"); fwrite(buf.b, 1, buf.d, out); - fclose(out); buffree(buf); + free(tmp); +} + +void simpleerror2(FILE *out, int code, char *msg, char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + simpleerror2v(out, code, msg, fmt, args); + va_end(args); +} + +void simpleerror(int fd, int code, char *msg, char *fmt, ...) +{ + va_list args; + FILE *out; + + va_start(args, fmt); + out = fdopen(dup(fd), "w"); + simpleerror2v(out, code, msg, fmt, args); + fclose(out); + va_end(args); } 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, ':'); @@ -140,11 +156,14 @@ void stdredir(struct hthead *req, int fd, int code, char *dst) } 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); @@ -237,3 +256,45 @@ time_t parsehttpdate(char *date) return(timegm(&tm) - tz); } + +char *httpdefstatus(int code) +{ + switch(code) { + case 200: + return("OK"); + case 201: + return("Created"); + case 202: + return("Accepted"); + case 204: + return("No Content"); + case 300: + return("Multiple Choices"); + case 301: + return("Moved Permanently"); + case 302: + return("Found"); + case 303: + return("See Other"); + case 304: + return("Not Modified"); + case 307: + return("Moved Temporarily"); + case 400: + return("Bad Request"); + case 401: + return("Unauthorized"); + case 403: + return("Forbidden"); + case 404: + return("Not Found"); + case 500: + return("Internal Server Error"); + case 501: + return("Not Implemented"); + case 503: + return("Service Unavailable"); + default: + return("Unknown status"); + } +}