diff options
-rw-r--r-- | cfws.c | 19 | ||||
-rw-r--r-- | http.c | 10 | ||||
-rw-r--r-- | http.h | 5 |
3 files changed, 25 insertions, 9 deletions
@@ -39,13 +39,21 @@ int main(int argc, char *argv[]) static void handle_request(const struct http_request *req, int sockfd) { - char *filepath; + char *filepath = NULL; enum http_res_code res_code; enum serve_method method; - /* Find the local path for the resource and decide how to serve it. */ - filepath = file_path_for_uri(req->uri); - method = file_method_for_path(filepath, &res_code); + if (req->method == HTTP_METHOD_UNKNOWN) { + method = SERVE_METHOD_ERROR; + res_code = HTTP_RESPONSE_NOTIMPLEMENTED; + } else if (req->uri == NULL) { + method = SERVE_METHOD_ERROR; + res_code = HTTP_RESPONSE_BADREQUEST; + } else { + /* Find the local path for the resource and decide how to serve it. */ + filepath = file_path_for_uri(req->uri); + method = file_method_for_path(filepath, &res_code); + } /* Write the status line and (TODO) extra headers */ http_response_statusline(res_code, sockfd); @@ -65,5 +73,6 @@ static void handle_request(const struct http_request *req, int sockfd) } } - free(filepath); + if (filepath) + free(filepath); } @@ -13,7 +13,7 @@ struct http_request http_parse_request(const char *reqstr) const char *counter; struct http_request req; - req.method = HTTP_METHOD_GET; + req.method = HTTP_METHOD_UNKNOWN; req.uri = NULL; req.query_str = NULL; @@ -23,6 +23,8 @@ struct http_request http_parse_request(const char *reqstr) return req; } + req.method = HTTP_METHOD_GET; + counter = reqstr + 4; while (*counter != ' ' && *counter != '?' && *counter != 0 && urilen < CFWS_MAXURI) { @@ -62,9 +64,11 @@ void http_free_request(struct http_request *req) free(req->query_str); } -static const char *response_msg[2] = { +static const char *response_msg[] = { "200 OK", - "404 Not Found" + "400 Bad Request", + "404 Not Found", + "501 Not Implemented" }; void http_response_statusline(enum http_res_code status_code, int sockfd) @@ -7,12 +7,15 @@ #define CFWS_MAX_RESPONSE 4096 enum http_req_method { + HTTP_METHOD_UNKNOWN, HTTP_METHOD_GET }; enum http_res_code { HTTP_RESPONSE_OK, - HTTP_RESPONSE_NOTFOUND + HTTP_RESPONSE_BADREQUEST, + HTTP_RESPONSE_NOTFOUND, + HTTP_RESPONSE_NOTIMPLEMENTED }; struct http_request { |