tfail.c - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tfail.c (2850B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <bin.h>
            4 #include <httpd.h>
            5 
            6 typedef struct Error        Error;
            7 
            8 struct Error
            9 {
           10         char        *num;
           11         char        *concise;
           12         char        *verbose;
           13 };
           14 
           15 Error errormsg[] =
           16 {
           17         /* HInternal */        {"500 Internal Error", "Internal Error",
           18                 "This server could not process your request due to an internal error."},
           19         /* HTempFail */        {"500 Internal Error", "Temporary Failure",
           20                 "The object %s is currently inaccessible.<p>Please try again later."},
           21         /* HUnimp */        {"501 Not implemented", "Command not implemented",
           22                 "This server does not implement the %s command."},
           23         /* HBadReq */        {"400 Bad Request", "Strange Request",
           24                 "Your client sent a query that this server could not understand."},
           25         /* HBadSearch */        {"400 Bad Request", "Inapplicable Search",
           26                 "Your client sent a search that cannot be applied to %s."},
           27         /* HNotFound */        {"404 Not Found", "Object not found",
           28                 "The object %s does not exist on this server."},
           29         /* HUnauth */        {"403 Forbidden", "Forbidden",
           30                 "You are not allowed to see the object %s."},
           31         /* HSyntax */        {"400 Bad Request", "Garbled Syntax",
           32                 "Your client sent a query with incoherent syntax."},
           33         /* HNoSearch */        {"403 Forbidden", "Search not supported",
           34                 "The object %s does not support the search command."},
           35         /* HNoData */        {"403 Forbidden", "No data supplied",
           36                 "Search or forms data must be supplied to %s."},
           37         /* HExpectFail */        {"403 Expectation Failed", "Expectation Failed",
           38                 "This server does not support some of your request's expectations."},
           39         /* HUnkVers */        {"501 Not Implemented", "Unknown http version",
           40                 "This server does not know how to respond to http version %s."},
           41         /* HBadCont */        {"501 Not Implemented", "Impossible format",
           42                 "This server cannot produce %s in any of the formats your client accepts."},
           43         /* HOK */                {"200 OK", "everything is fine"},
           44 };
           45 
           46 /*
           47  * write a failure message to the net and exit
           48  */
           49 int
           50 hfail(HConnect *c, int reason, ...)
           51 {
           52         Hio *hout;
           53         char makeup[HBufSize];
           54         va_list arg;
           55         int n;
           56 
           57         hout = &c->hout;
           58         va_start(arg, reason);
           59         vseprint(makeup, makeup+HBufSize, errormsg[reason].verbose, arg);
           60         va_end(arg);
           61         n = snprint(c->xferbuf, HBufSize, "<head><title>%s</title></head>\n<body><h1>%s</h1>\n%s</body>\n",
           62                 errormsg[reason].concise, errormsg[reason].concise, makeup);
           63 
           64         hprint(hout, "%s %s\r\n", hversion, errormsg[reason].num);
           65         hprint(hout, "Date: %D\r\n", time(nil));
           66         hprint(hout, "Server: Plan9\r\n");
           67         hprint(hout, "Content-Type: text/html\r\n");
           68         hprint(hout, "Content-Length: %d\r\n", n);
           69         if(c->head.closeit)
           70                 hprint(hout, "Connection: close\r\n");
           71         else if(!http11(c))
           72                 hprint(hout, "Connection: Keep-Alive\r\n");
           73         hprint(hout, "\r\n");
           74 
           75         if(c->req.meth == nil || strcmp(c->req.meth, "HEAD") != 0)
           76                 hwrite(hout, c->xferbuf, n);
           77 
           78         if(c->replog)
           79                 c->replog(c, "Reply: %s\nReason: %s\n", errormsg[reason].num, errormsg[reason].concise);
           80         return hflush(hout);
           81 }