phroxy

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/phroxy.git
Log | Files | Refs | LICENSE

commit fe4f5657743a30daa28fe32376c1158b0b0240d3
parent e133739f4bb2117d8e6389ab5a7ce83291131410
Author: Willy Goiffon <dev@z3bra.org>
Date:   Mon, 14 Sep 2020 15:50:32 +0200

Wrap text files in HTML too

Diffstat:
Mconfig.def.h | 4++--
Mphroxy.c | 74++++++++++++++++++++++++++++++++++++++++++++++++++------------------------
2 files changed, 52 insertions(+), 26 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -7,7 +7,7 @@ const char *htmlfmt = "<style>body { white-space: pre; }</style>" "<title>gopher proxy</title>\n" "</head>\n" - "<body><pre>\n" + "<body>\n" "%s\n" - "</pre></body></html>\n"; + "</body></html>\n"; diff --git a/phroxy.c b/phroxy.c @@ -180,14 +180,24 @@ contenttype(char i) { switch(i) { case '0': - return "text/plain; charset=utf-8"; - break; /* NOTREACHED */ case '1': + case '7': return "text/html; charset=utf-8"; break; /* NOTREACHED */ + case '6': + return "text/x-uuencode"; + break; /* NOTREACHED */ + case '4': + case '5': case '9': return "application/octet-stream"; break; /* NOTREACHED */ + case 'I': + return "image/youguess"; + break; /* NOTREACHED */ + case 'g': + return "image/gif"; + break; /* NOTREACHED */ } return NULL; @@ -209,11 +219,11 @@ printheaders(char *ctype) } char * -htmlize(char *data, size_t *htmlsize) +htmlize(char *data, size_t *bodysize) { - size_t alen, blen, hlen; - char *html = NULL; - char i, *p, *body, a[LINE_MAX], *f[4]; + size_t alen, blen; + char *body = NULL; + char i, *p, a[LINE_MAX], *f[4]; char *fmt = "<a href='http://%s/%s:%s/%c%s'>%s</a>\n"; blen = 0; @@ -240,18 +250,31 @@ htmlize(char *data, size_t *htmlsize) default: snprintf(a, sizeof(a), fmt, http_host, f[2], f[3], i, f[1], f[0]); } + alen = strnlen(a, sizeof(a)); body = realloc(body, blen + alen); memcpy(body + blen, a, alen); blen += alen; } - hlen = strlen(htmlfmt) + blen; + if (bodysize) + *bodysize = blen; + + return body; +} + +char * +htmlwrap(char *data, size_t len, size_t *htmlsize) +{ + size_t hlen; + char *html = NULL; + + hlen = strlen(htmlfmt) + len; html = malloc(hlen); if (!html) return NULL; - snprintf(html, hlen, htmlfmt, body); + snprintf(html, hlen, htmlfmt, data); if (htmlsize) *htmlsize = hlen; @@ -266,27 +289,31 @@ serveitem(char item, char *data, size_t len) int sent; switch(item) { - case '1': + case '7': // search + case '1': // menu html = htmlize(data, &len); free(data); data = html; - break; + /* FALLTHROUGH */ - case '0': - case '6': - case '9': - case 'g': - case 'I': + case '0': // text + html = htmlwrap(data, len, &len); + free(data); + data = html; + break; + case '4': // BinHexed Macintosh file + case '5': // DOS binary archive of some sort + case '6': // uuencoded + case '9': // binary + case 'g': // gif + case 'I': // image break; - case '2': // Item is a CSO phone-book server - case '3': // Error - case '4': // Item is a BinHexed Macintosh file. - case '5': // Item is DOS binary archive of some sort. - case '7': // Item is an Index-Search server. - case '8': // Item points to a text-based telnet session. - case 'T': // Item points to a text-based tn3270 session. - case '+': + case '2': // CSO phone-book server + case '3': // Error + case '8': // telnet session. + case 'T': // tn3270 session. + case '+': // mirror link default: /* IGNORE */ print415(); @@ -296,7 +323,6 @@ serveitem(char item, char *data, size_t len) printf("HTTP/1.1 200 OK\r\n"); printheaders(contenttype(item)); - printf("Content-Length: %ld\r\n", len); printf("\r\n"); fflush(stdout);