kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit a8052c7ac40a7be26a570d5db4a7ee7928cfebd6
parent ef9d37e54bc555d35f3f7e6b77cfe8afafd02d69
Author: Joris Vink <joris@coders.se>
Date:   Mon, 19 Aug 2013 09:11:31 +0200

Add reason phrases and http status constants (HTTP_STATUS_* see includes/http.h)
Based on work from mendor/ymv via github.

Diffstat:
includes/http.h | 43+++++++++++++++++++++++++++++++++++++++++++
src/http.c | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
2 files changed, 182 insertions(+), 2 deletions(-)

diff --git a/includes/http.h b/includes/http.h @@ -89,4 +89,47 @@ int http_argument_multiple_lookup(struct http_request *, void kore_accesslog(struct http_request *); +enum http_status_code { + HTTP_STATUS_CONTINUE = 100, + HTTP_STATUS_SWITCHING_PROTOCOLS = 101, + HTTP_STATUS_OK = 200, + HTTP_STATUS_CREATED = 201, + HTTP_STATUS_ACCEPTED = 202, + HTTP_STATUS_NON_AUTHORITATIVE = 203, + HTTP_STATUS_NO_CONTENT = 204, + HTTP_STATUS_RESET_CONTENT = 205, + HTTP_STATUS_PARTIAL_CONTENT = 206, + HTTP_STATUS_MULTIPLE_CHOICES = 300, + HTTP_STATUS_MOVED_PERMANENTLY = 301, + HTTP_STATUS_FOUND = 302, + HTTP_STATUS_SEE_OTHER = 303, + HTTP_STATUS_NOT_MODIFIED = 304, + HTTP_STATUS_USE_PROXY = 305, + HTTP_STATUS_TEMPORARY_REDIRECT = 307, + HTTP_STATUS_BAD_REQUEST = 400, + HTTP_STATUS_UNAUTHORIZED = 401, + HTTP_STATUS_PAYMENT_REQUIRED = 402, + HTTP_STATUS_FORBIDDEN = 403, + HTTP_STATUS_NOT_FOUND = 404, + HTTP_STATUS_METHOD_NOT_ALLOWED = 405, + HTTP_STATUS_NOT_ACCEPTABLE = 406, + HTTP_STATUS_PROXY_AUTH_REQUIRED = 407, + HTTP_STATUS_REQUEST_TIMEOUT = 408, + HTTP_STATUS_CONFLICT = 409, + HTTP_STATUS_GONE = 410, + HTTP_STATUS_LENGTH_REQUIRED = 411, + HTTP_STATUS_PRECONDITION_FAILED = 412, + HTTP_STATUS_REQUEST_ENTITY_TOO_LARGE = 413, + HTTP_STATUS_REQUEST_URI_TOO_LARGE = 414, + HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415, + HTTP_STATUS_REQUEST_RANGE_INVALID = 416, + HTTP_STATUS_EXPECTATION_FAILED = 417, + HTTP_STATUS_INTERNAL_ERROR = 500, + HTTP_STATUS_NOT_IMPLEMENTED = 501, + HTTP_STATUS_BAD_GATEWAY = 502, + HTTP_STATUS_SERVICE_UNAVAILABLE = 503, + HTTP_STATUS_GATEWAY_TIMEOUT = 504, + HTTP_STATUS_BAD_VERSION = 505 +}; + #endif /* !__H_HTTP_H */ diff --git a/src/http.c b/src/http.c @@ -22,6 +22,7 @@ #include "kore.h" #include "http.h" +static char *http_status_text(int); static int http_post_data_recv(struct netbuf *); static int http_send_done(struct netbuf *); @@ -226,7 +227,8 @@ http_response(struct http_request *req, int status, u_int8_t *d, u_int32_t len) req->status = status; if (req->owner->proto == CONN_PROTO_SPDY) { - snprintf(sbuf, sizeof(sbuf), "%d", status); + snprintf(sbuf, sizeof(sbuf), + "%d %s", status, http_status_text(status)); hblock = spdy_header_block_create(SPDY_HBLOCK_NORMAL); spdy_header_block_add(hblock, ":status", sbuf); @@ -260,7 +262,8 @@ http_response(struct http_request *req, int status, u_int8_t *d, u_int32_t len) } else { buf = kore_buf_create(KORE_BUF_INITIAL); - kore_buf_appendf(buf, "HTTP/1.1 %d\r\n", status); + kore_buf_appendf(buf, "HTTP/1.1 %d %s\r\n", + status, http_status_text(status)); kore_buf_appendf(buf, "Content-length: %d\r\n", len); kore_buf_appendf(buf, "Connection: keep-alive\r\n"); kore_buf_appendf(buf, "Keep-Alive: timeout=20\r\n"); @@ -616,3 +619,137 @@ http_send_done(struct netbuf *nb) return (KORE_RESULT_OK); } + +static char * +http_status_text(int status) +{ + char *r; + + switch (status) { + case HTTP_STATUS_CONTINUE: + r = "Continue"; + break; + case HTTP_STATUS_SWITCHING_PROTOCOLS: + r = "Switching Protocols"; + break; + case HTTP_STATUS_OK: + r = "OK"; + break; + case HTTP_STATUS_CREATED: + r = "Created"; + break; + case HTTP_STATUS_ACCEPTED: + r = "Accepted"; + break; + case HTTP_STATUS_NON_AUTHORITATIVE: + r = "Non-Authoritative Information"; + break; + case HTTP_STATUS_NO_CONTENT: + r = "No Content"; + break; + case HTTP_STATUS_RESET_CONTENT: + r = "Reset Content"; + break; + case HTTP_STATUS_PARTIAL_CONTENT: + r = "Partial Content"; + break; + case HTTP_STATUS_MULTIPLE_CHOICES: + r = "Multiple Choices"; + break; + case HTTP_STATUS_MOVED_PERMANENTLY: + r = "Moved Permanently"; + break; + case HTTP_STATUS_FOUND: + r = "Found"; + break; + case HTTP_STATUS_SEE_OTHER: + r = "See Other"; + break; + case HTTP_STATUS_NOT_MODIFIED: + r = "Not Modified"; + break; + case HTTP_STATUS_USE_PROXY: + r = "Use Proxy"; + break; + case HTTP_STATUS_TEMPORARY_REDIRECT: + r = "Temporary Redirect"; + break; + case HTTP_STATUS_BAD_REQUEST: + r = "Bad Request"; + break; + case HTTP_STATUS_UNAUTHORIZED: + r = "Unauthorized"; + break; + case HTTP_STATUS_PAYMENT_REQUIRED: + r = "Payment Required"; + break; + case HTTP_STATUS_FORBIDDEN: + r = "Forbidden"; + break; + case HTTP_STATUS_NOT_FOUND: + r = "Not Found"; + break; + case HTTP_STATUS_METHOD_NOT_ALLOWED: + r = "Method Not Allowed"; + break; + case HTTP_STATUS_NOT_ACCEPTABLE: + r = "Not Acceptable"; + break; + case HTTP_STATUS_PROXY_AUTH_REQUIRED: + r = "Proxy Authentication Required"; + break; + case HTTP_STATUS_REQUEST_TIMEOUT: + r = "Request Time-out"; + break; + case HTTP_STATUS_CONFLICT: + r = "Conflict"; + break; + case HTTP_STATUS_GONE: + r = "Gone"; + break; + case HTTP_STATUS_LENGTH_REQUIRED: + r = "Length Required"; + break; + case HTTP_STATUS_PRECONDITION_FAILED: + r = "Precondition Failed"; + break; + case HTTP_STATUS_REQUEST_ENTITY_TOO_LARGE: + r = "Request Entity Too Large"; + break; + case HTTP_STATUS_REQUEST_URI_TOO_LARGE: + r = "Request-URI Too Large"; + break; + case HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE: + r = "Unsupported Media Type"; + break; + case HTTP_STATUS_REQUEST_RANGE_INVALID: + r = "Requested range not satisfiable"; + break; + case HTTP_STATUS_EXPECTATION_FAILED: + r = "Expectation Failed"; + break; + case HTTP_STATUS_INTERNAL_ERROR: + r = "Internal Server Error"; + break; + case HTTP_STATUS_NOT_IMPLEMENTED: + r = "Not Implemented"; + break; + case HTTP_STATUS_BAD_GATEWAY: + r = "Bad Gateway"; + break; + case HTTP_STATUS_SERVICE_UNAVAILABLE: + r = "Service Unavailable"; + break; + case HTTP_STATUS_GATEWAY_TIMEOUT: + r = "Gateway Time-out"; + break; + case HTTP_STATUS_BAD_VERSION: + r = "HTTP Version not supported"; + break; + default: + r = ""; + break; + } + + return (r); +}