X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Fresp.c;h=9d7325bcd0199ba9f01f8653e97ab8d4b3b8b010;hb=c3b910928f34306f34ee6a9c3c13debbf8ff67f4;hp=da354f99e40f3dcb98d91863a406550d60bf0504;hpb=f812ea037b5c693977e83ace9ed7ac2515a4a6d0;p=ashd.git diff --git a/lib/resp.c b/lib/resp.c index da354f9..9d7325b 100644 --- a/lib/resp.c +++ b/lib/resp.c @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -73,16 +74,57 @@ void simpleerror(int fd, int code, char *msg, char *fmt, ...) bufcatstr(buf, "\r\n"); bufcatstr(buf, "\r\n"); free(tmp2); - out = fdopen(fd, "w"); - fprintf(out, "HTTP/1.1 %i %s\r\n", code, msg); - fprintf(out, "Content-Type: text/html\r\n"); - fprintf(out, "Content-Length: %zi\r\n", buf.d); - fprintf(out, "\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); } +void stdredir(struct hthead *req, int fd, int code, char *dst) +{ + FILE *out; + char *sp, *cp, *ep, *path, *url, *adst, *proto, *host; + + sp = strchr(dst, '/'); + cp = strchr(dst, ':'); + if(cp && (!sp || (cp < sp))) { + adst = sstrdup(dst); + } else { + proto = getheader(req, "X-Ash-Protocol"); + host = getheader(req, "Host"); + if((proto == NULL) || (host == NULL)) { + /* Not compliant, but there isn't a whole lot to be done + * about it. */ + adst = sstrdup(dst); + } else { + if(*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); + } + adst = sprintf2("%s://%s/%s", proto, host, path); + free(path); + } + } + 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); + fprintf(out, "\n"); + fclose(out); + free(adst); +} + char *fmthttpdate(time_t time) { /* I avoid using strftime, since it depends on locale settings. */