From 8df3d4ab5d9292575d0dd13a329c450dc362cb28 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 21 Sep 2022 19:46:02 +0200 Subject: [PATCH] Fix q_path. --- httpd.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/httpd.c b/httpd.c index 3047b3e..7cd73cc 100644 --- a/httpd.c +++ b/httpd.c @@ -11,25 +11,20 @@ #include "snac.h" -void server_get_handler(d_char *req, int *status, char **body, int *b_size, char **ctype) +void server_get_handler(d_char *req, char *q_path, int *status, + char **body, int *b_size, char **ctype) /* basic server services */ { char *req_hdrs = xs_dict_get(req, "headers"); char *acpt = xs_dict_get(req_hdrs, "accept"); - char *q_path = xs_dict_get(req_hdrs, "path"); if (acpt == NULL) { *status = 400; return; } - /* get the server prefix */ - char *prefix = xs_dict_get(srv_config, "prefix"); - if (*prefix == '\0') - prefix = "/"; - /* is it the server root? */ - if (strcmp(q_path, prefix) == 0) { + if (*q_path == '\0') { /* try to open greeting.html */ xs *fn = xs_fmt("%s/greeting.html", srv_basedir); FILE *f; @@ -85,6 +80,8 @@ void httpd_connection(int rs) int b_size = 0; char *ctype = NULL; xs *headers = NULL; + xs *q_path = NULL; + char *p; f = xs_socket_accept(rs); @@ -98,11 +95,20 @@ void httpd_connection(int rs) req_hdrs = xs_dict_get(req, "headers"); method = xs_dict_get(req_hdrs, "method"); + q_path = xs_dup(xs_dict_get(req_hdrs, "path")); + + /* crop the q_path from leading / and the prefix */ + if (xs_endswith(q_path, "/")) + q_path = xs_crop(q_path, 0, -1); + + p = xs_dict_get(srv_config, "prefix"); + if (xs_startswith(q_path, p)) + q_path = xs_crop(q_path, strlen(p), 0); if (strcmp(method, "GET") == 0) { /* cascade through */ if (status == 0) - server_get_handler(req, &status, &body, &b_size, &ctype); + server_get_handler(req, q_path, &status, &body, &b_size, &ctype); } else if (strcmp(method, "POST") == 0) { @@ -116,10 +122,10 @@ void httpd_connection(int rs) status = 404; if (status == 404) - body = "

404 Not Found

"; + body = xs_str_new("

404 Not Found

"); if (status == 400) - body = "

400 Bad Request

"; + body = xs_str_new("

400 Bad Request

"); if (status == 303) headers = xs_dict_append(headers, "location", body);