feat: small steps

c-web-server
Jef Roosens 2023-05-26 22:41:01 +02:00
parent dfd27b579d
commit f3fced908f
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 39 additions and 5 deletions

View File

@ -10,6 +10,8 @@
extern const char http_404[]; extern const char http_404[];
extern const size_t http_404_len; 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 char http_500[];
extern const size_t http_500_len; 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); 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 #endif

View File

@ -5,6 +5,11 @@ const char http_404[] = "HTTP/1.1 404 Not Found\n"
"Content-Length: 0\n\n"; "Content-Length: 0\n\n";
const size_t http_404_len = sizeof(http_404) - 1; 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" const char http_500[] = "HTTP/1.1 500 Internal Server Error\n"
"Connection: close\n" "Connection: close\n"
"Content-Length: 0\n\n"; "Content-Length: 0\n\n";

View File

@ -9,9 +9,5 @@ void http_route(event_loop_conn *conn) {
// TODO routing // TODO routing
// Fallthrough is to return a 404 // Fallthrough is to return a 404
memcpy(conn->wbuf, http_404, http_404_len); http_write_standard_response(conn, http_not_found);
conn->state = event_loop_conn_state_res;
conn->wbuf_size = http_404_len;
conn->wbuf_sent = 0;
} }

View File

@ -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;
}