tPrint HTTP error codes on gopher connection failures - phroxy - Gopher to HTTP proxy
 (HTM) git clone git://git.z3bra.org/phroxy.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit 338b20f4ebd64a68962bedde167b480c3ceb3dee
 (DIR) parent bcdad97311b53db4557ec5bc06dd8ffce852610e
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Tue, 15 Sep 2020 12:50:21 +0200
       
       Print HTTP error codes on gopher connection failures
       
       Diffstat:
         M phroxy.c                            |      56 +++++++++++--------------------
       
       1 file changed, 20 insertions(+), 36 deletions(-)
       ---
 (DIR) diff --git a/phroxy.c b/phroxy.c
       t@@ -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");
        }
        
       t@@ -341,7 +319,7 @@ serveitem(char item, char *data, size_t len)
                case '+': // mirror link
                default:
                        /* IGNORE */
       -                print415();
       +                printhttp(415);
                        break;
                }
        
       t@@ -377,7 +355,7 @@ phroxy(char *url)
                url++;
                hole = strsep(&url, "/");
                if (!hole) {
       -                print404();
       +                printhttp(404);
                        return 1;
                }
        
       t@@ -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);
        
       t@@ -426,7 +410,7 @@ main(void)
                request[rlen] = '\0';
        
                if (strncmp(request, "GET ", 4)) {
       -                print405();
       +                printhttp(405);
                        return 1;
                }