feat(lnm): support HEAD requests
ci/woodpecker/push/build Pipeline was successful Details
ci/woodpecker/push/docker Pipeline was successful Details

new-lnm-integration
Jef Roosens 2023-12-07 12:54:34 +01:00
parent cc9dc6aec5
commit 58a8645c6c
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 19 additions and 6 deletions

View File

@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Allow custom & an arbitrary number of response headers * Allow custom & an arbitrary number of response headers
* Better API for adding routes * Better API for adding routes
* State machine HTTP loop * State machine HTTP loop
* Auomatically support HEAD requests for all GET requests
## [0.2.0](https://git.rustybever.be/Chewing_Bever/lander/src/tag/0.2.0) ## [0.2.0](https://git.rustybever.be/Chewing_Bever/lander/src/tag/0.2.0)

View File

@ -11,7 +11,8 @@ typedef enum lnm_http_method {
lnm_http_method_post, lnm_http_method_post,
lnm_http_method_put, lnm_http_method_put,
lnm_http_method_patch, lnm_http_method_patch,
lnm_http_method_delete lnm_http_method_delete,
lnm_http_method_head,
} lnm_http_method; } lnm_http_method;
extern const char *lnm_http_status_names[][32]; extern const char *lnm_http_status_names[][32];

View File

@ -1,6 +1,7 @@
#include "lnm/http/consts.h" #include "lnm/http/consts.h"
const char *lnm_http_method_names[] = {"GET", "POST", "PUT", "PATCH", "DELETE"}; const char *lnm_http_method_names[] = {"GET", "POST", "PUT",
"PATCH", "DELETE", "HEAD"};
const size_t lnm_http_method_names_len = const size_t lnm_http_method_names_len =
sizeof(lnm_http_method_names) / sizeof(lnm_http_method_names[0]); sizeof(lnm_http_method_names) / sizeof(lnm_http_method_names[0]);

View File

@ -68,8 +68,14 @@ void lnm_http_loop_process_route(lnm_http_conn *conn) {
break; break;
} }
// Remember the previous match levels // GET routes also automatically route HEAD requests
int new_match_level = 2 * matched_path + (route->method == ctx->req.method); bool matched_method = route->method == ctx->req.method ||
(route->method == lnm_http_method_get &&
ctx->req.method == lnm_http_method_head);
int new_match_level = 2 * matched_path + matched_method;
// Remember the previous match levels so we can return the correct status
// message
match_level = match_level < new_match_level ? new_match_level : match_level; match_level = match_level < new_match_level ? new_match_level : match_level;
} }
@ -227,7 +233,11 @@ void lnm_http_loop_process_write_headers(lnm_http_conn *conn) {
conn->w.buf[conn->w.size] = '\n'; conn->w.buf[conn->w.size] = '\n';
conn->w.size++; conn->w.size++;
ctx->state = ctx->res.body.len > 0 ? lnm_http_loop_state_write_body // HEAD requests function exactly the same as GET requests, except that they
// skip the body writing part
ctx->state =
ctx->req.method != lnm_http_method_head && ctx->res.body.len > 0
? lnm_http_loop_state_write_body
: lnm_http_loop_state_finish; : lnm_http_loop_state_finish;
} }
} }