From: Fredrik Tolf Date: Tue, 24 Aug 2010 19:39:58 +0000 (+0200) Subject: Added a response function for standard redirection. X-Git-Tag: 0.1~85 X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=commitdiff_plain;h=46e6630278db9fb3b7aa7150d28fcd90993a5cc9 Added a response function for standard redirection. These things will always be ugly hacks, so why shouldn't this one be? :-/ --- diff --git a/lib/resp.c b/lib/resp.c index 63c8eca..c1b74b9 100644 --- a/lib/resp.c +++ b/lib/resp.c @@ -83,6 +83,47 @@ void simpleerror(int fd, int code, char *msg, char *fmt, ...) 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); + } 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(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. */ diff --git a/lib/resp.h b/lib/resp.h index 90771dc..00f1872 100644 --- a/lib/resp.h +++ b/lib/resp.h @@ -1,7 +1,10 @@ #ifndef _LIB_HTRESP_H #define _LIB_HTRESP_H +#include + void simpleerror(int fd, int code, char *msg, char *fmt, ...); +void stdredir(struct hthead *req, int fd, int code, char *dst); char *fmthttpdate(time_t time); time_t parsehttpdate(char *date); diff --git a/src/plaintcp.c b/src/plaintcp.c index 03da055..d1d5969 100644 --- a/src/plaintcp.c +++ b/src/plaintcp.c @@ -107,6 +107,7 @@ static int initreq(struct conn *conn, struct hthead *req) headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&tcp->name)->sin6_port))); } headappheader(req, "X-Ash-Server-Port", sprintf3("%i", tcp->port->sport)); + headappheader(req, "X-Ash-Protocol", "http"); return(0); }