diff --git a/quark.c b/quark.c index 9d22df1..b42bba6 100644 --- a/quark.c +++ b/quark.c @@ -30,18 +30,32 @@ char *argv0; #include "config.h" -static char *status[] = { - [200] = "OK", - [206] = "Partial Content", - [301] = "Moved Permanently", - [400] = "Bad Request", - [403] = "Forbidden", - [404] = "Not Found", - [405] = "Method Not Allowed", - [408] = "Request Time-out", - [431] = "Request Header Fields Too Large", - [500] = "Internal Server Error", - [505] = "HTTP Version not supported", +enum stati { + S_OK = 200, + S_PARTIAL_CONTENT = 206, + S_MOVED_PERMANENTLY = 301, + S_BAD_REQUEST = 400, + S_FORBIDDEN = 403, + S_NOT_FOUND = 404, + S_METHOD_NOT_ALLOWED = 405, + S_REQUEST_TIMEOUT = 408, + S_REQUEST_TOO_LARGE = 431, + S_INTERNAL_SERVER_ERROR = 500, + S_VERSION_NOT_SUPPORTED = 505, +}; + +static char *statistr[] = { + [S_OK] = "OK", + [S_PARTIAL_CONTENT] = "Partial Content", + [S_MOVED_PERMANENTLY] = "Moved Permanently", + [S_BAD_REQUEST] = "Bad Request", + [S_FORBIDDEN] = "Forbidden", + [S_NOT_FOUND] = "Not Found", + [S_METHOD_NOT_ALLOWED] = "Method Not Allowed", + [S_REQUEST_TIMEOUT] = "Request Time-out", + [S_REQUEST_TOO_LARGE] = "Request Header Fields Too Large", + [S_INTERNAL_SERVER_ERROR] = "Internal Server Error", + [S_VERSION_NOT_SUPPORTED] = "HTTP Version not supported", }; @@ -61,7 +75,7 @@ timestamp(time_t t) } static int -sendstatus(int code, int fd, ...) +sendstatus(enum stati code, int fd, ...) { va_list ap; char buf[4096]; @@ -70,13 +84,13 @@ sendstatus(int code, int fd, ...) long lower, upper, size; buflen = snprintf(buf, 4096, "HTTP/1.1 %d %s\r\n", code, - status[code]); + statistr[code]); buflen += snprintf(buf + buflen, 4096 - buflen, "Date: %s\r\n", timestamp(0)); va_start(ap, fd); switch (code) { - case 200: /* arg-list: mime, size */ + case S_OK: /* arg-list: mime, size */ buflen += snprintf(buf + buflen, 4096 - buflen, "Content-Type: %s\r\n", va_arg(ap, char *)); @@ -86,7 +100,7 @@ sendstatus(int code, int fd, ...) size); } break; - case 206: /* arg-list: mime, lower, upper, size */ + case S_PARTIAL_CONTENT: /* arg-list: mime, lower, upper, size */ buflen += snprintf(buf + buflen, 4096 - buflen, "Content-Type: %s\r\n", va_arg(ap, char *)); @@ -100,7 +114,7 @@ sendstatus(int code, int fd, ...) "Content-Length: %ld\r\n", (upper - lower) + 1); break; - case 301: /* arg-list: host, url */ + case S_MOVED_PERMANENTLY: /* arg-list: host, url */ if (!strcmp(port, "80")) { buflen += snprintf(buf + buflen, 4096 - buflen, "Location: http://%s%s\r\n", @@ -113,32 +127,34 @@ sendstatus(int code, int fd, ...) va_arg(ap, char *)); } break; - case 405: /* arg-list: none */ + case S_METHOD_NOT_ALLOWED: /* arg-list: none */ buflen += snprintf(buf + buflen, 4096 - buflen, "Allow: GET\r\n"); break; + default: + break; } va_end(ap); buflen += snprintf(buf + buflen, 4096 - buflen, "Connection: close\r\n"); - if (code != 200 && code != 206) { + if (code != S_OK && code != S_PARTIAL_CONTENT) { buflen += snprintf(buf + buflen, 4096 - buflen, "Content-Type: text/html\r\n"); buflen += snprintf(buf + buflen, 4096 - buflen, "\r\n\r\n\r\n" "\t