phroxy

Gopher to HTTP proxy
git clone git://git.z3bra.org/phroxy.git
Log | Files | Refs | LICENSE

commit 338b20f4ebd64a68962bedde167b480c3ceb3dee
parent bcdad97311b53db4557ec5bc06dd8ffce852610e
Author: Willy Goiffon <dev@z3bra.org>
Date:   Tue, 15 Sep 2020 12:50:21 +0200

Print HTTP error codes on gopher connection failures

Diffstat:
Mphroxy.c | 56++++++++++++++++++++------------------------------------
1 file changed, 20 insertions(+), 36 deletions(-)

diff --git a/phroxy.c b/phroxy.c @@ -141,37 +141,15 @@ getrawitem(int sock, size_t *sz) } void -print400(void) +printhttp(int code) { - printf("HTTP/1.1 400 That's Illegal\r\n"); - printf("\r\n"); -} - -void -print404(void) -{ - printf("HTTP/1.1 404 Google Broke The Web\r\n"); - printf("\r\n"); -} - -void -print405(void) -{ - printf("HTTP/1.1 405 Don't Do That\r\n"); - printf("\r\n"); -} - -void -print415(void) -{ - printf("HTTP/1.1 415 Gopher Type Not Handled\r\n"); - printf("\r\n"); -} - -void -print500(void) -{ - printf("HTTP/1.1 500 You Broke The Web\r\n"); + switch (code) { + case 400: printf("HTTP/1.1 400 That's Illegal\r\n"); break; + case 404: printf("HTTP/1.1 404 Google Broke The Web\r\n"); break; + case 405: printf("HTTP/1.1 405 Don't Do That\r\n"); break; + case 415: printf("HTTP/1.1 415 Gopher Type Not Handled\r\n"); break; + case 500: printf("HTTP/1.1 500 You Broke The Web\r\n"); break; + } printf("\r\n"); } @@ -341,7 +319,7 @@ serveitem(char item, char *data, size_t len) case '+': // mirror link default: /* IGNORE */ - print415(); + printhttp(415); break; } @@ -377,7 +355,7 @@ phroxy(char *url) url++; hole = strsep(&url, "/"); if (!hole) { - print404(); + printhttp(404); return 1; } @@ -393,20 +371,26 @@ phroxy(char *url) item = '1'; path = strsep(&url, "\0"); - if (!path) + if (!path || *path != '/') path = "/"; if((srch = strchr(path, '?'))) *srch = '\t'; - sock = connectto(host, port); + if ((sock = connectto(host, port)) < 0) { + printhttp(500); + return 1; + } + if (!sendselector(sock, path)) data = getrawitem(sock, &len); close(sock); - if (!data) + if (!data) { + printhttp(444); return 1; + } serveitem(item, data, len); @@ -426,7 +410,7 @@ main(void) request[rlen] = '\0'; if (strncmp(request, "GET ", 4)) { - print405(); + printhttp(405); return 1; }