diff --git a/include/http.h b/include/http.h index bfe7a8c..435e2dd 100644 --- a/include/http.h +++ b/include/http.h @@ -10,6 +10,8 @@ extern const char http_404[]; extern const size_t http_404_len; +extern const char http_405[]; +extern const size_t http_405_len; extern const char http_500[]; extern const size_t http_500_len; @@ -24,4 +26,12 @@ http_parse_error http_parse_request(http_request *req, const char *path, void http_route(event_loop_conn *conn); +typedef enum http_response_type { + http_not_found = 404, + http_method_not_allowed = 405 +} http_response_type; + +void http_write_standard_response(event_loop_conn *conn, + http_response_type type); + #endif diff --git a/src/http/http_consts.c b/src/http/http_consts.c index b84fba5..b929c6d 100644 --- a/src/http/http_consts.c +++ b/src/http/http_consts.c @@ -5,6 +5,11 @@ const char http_404[] = "HTTP/1.1 404 Not Found\n" "Content-Length: 0\n\n"; const size_t http_404_len = sizeof(http_404) - 1; +const char http_405[] = "HTTP/1.1 405 Method Not Allowed\n" + "Connection: close\n" + "Content-Length: 0\n\n"; +const size_t http_405_len = sizeof(http_405) - 1; + const char http_500[] = "HTTP/1.1 500 Internal Server Error\n" "Connection: close\n" "Content-Length: 0\n\n"; diff --git a/src/http/http_route.c b/src/http/http_route.c index 38627a6..cbd5f49 100644 --- a/src/http/http_route.c +++ b/src/http/http_route.c @@ -9,9 +9,5 @@ void http_route(event_loop_conn *conn) { // TODO routing // Fallthrough is to return a 404 - memcpy(conn->wbuf, http_404, http_404_len); - - conn->state = event_loop_conn_state_res; - conn->wbuf_size = http_404_len; - conn->wbuf_sent = 0; + http_write_standard_response(conn, http_not_found); } diff --git a/src/http/http_util.c b/src/http/http_util.c new file mode 100644 index 0000000..4b916e7 --- /dev/null +++ b/src/http/http_util.c @@ -0,0 +1,23 @@ +#include "http.h" + +void http_write_standard_response(event_loop_conn *conn, + http_response_type type) { + const char *s; + size_t len; + + switch (type) { + case 404: + s = http_404; + len = http_404_len; + break; + case 405: + s = http_405; + len = http_405_len; + } + + memcpy(conn->wbuf, s, len); + + conn->state = event_loop_conn_state_res; + conn->wbuf_size = len; + conn->wbuf_sent = 0; +}