summaryrefslogtreecommitdiff
path: root/cfws.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-06-03 11:52:47 -0600
committercflip <cflip@cflip.net>2023-06-03 11:52:47 -0600
commit9239ee0dbee152f8ebbd12726d8bdc6d5720e2f4 (patch)
tree96b3e274a4ce1631a72d6490d4a7585118d1f49c /cfws.c
parentff0c20e0828a204222b6c535605cf00d796a1e1b (diff)
Respond with 501 Not Implemented for unknown request methods
Diffstat (limited to 'cfws.c')
-rw-r--r--cfws.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/cfws.c b/cfws.c
index afe2a1d..19a8d0b 100644
--- a/cfws.c
+++ b/cfws.c
@@ -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);
}