summaryrefslogtreecommitdiff
path: root/httpd.c
diff options
context:
space:
mode:
authorGravatar default2022-09-21 19:46:02 +0200
committerGravatar default2022-09-21 19:46:02 +0200
commit8df3d4ab5d9292575d0dd13a329c450dc362cb28 (patch)
tree772f71415ad342aeb8fdeb9fbb07f26099aac5ea /httpd.c
parentAdded a global server handler. (diff)
downloadpenes-snac2-8df3d4ab5d9292575d0dd13a329c450dc362cb28.tar.gz
penes-snac2-8df3d4ab5d9292575d0dd13a329c450dc362cb28.tar.xz
penes-snac2-8df3d4ab5d9292575d0dd13a329c450dc362cb28.zip
Fix q_path.
Diffstat (limited to 'httpd.c')
-rw-r--r--httpd.c28
1 files 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 @@
11#include "snac.h" 11#include "snac.h"
12 12
13 13
14void server_get_handler(d_char *req, int *status, char **body, int *b_size, char **ctype) 14void server_get_handler(d_char *req, char *q_path, int *status,
15 char **body, int *b_size, char **ctype)
15/* basic server services */ 16/* basic server services */
16{ 17{
17 char *req_hdrs = xs_dict_get(req, "headers"); 18 char *req_hdrs = xs_dict_get(req, "headers");
18 char *acpt = xs_dict_get(req_hdrs, "accept"); 19 char *acpt = xs_dict_get(req_hdrs, "accept");
19 char *q_path = xs_dict_get(req_hdrs, "path");
20 20
21 if (acpt == NULL) { 21 if (acpt == NULL) {
22 *status = 400; 22 *status = 400;
23 return; 23 return;
24 } 24 }
25 25
26 /* get the server prefix */
27 char *prefix = xs_dict_get(srv_config, "prefix");
28 if (*prefix == '\0')
29 prefix = "/";
30
31 /* is it the server root? */ 26 /* is it the server root? */
32 if (strcmp(q_path, prefix) == 0) { 27 if (*q_path == '\0') {
33 /* try to open greeting.html */ 28 /* try to open greeting.html */
34 xs *fn = xs_fmt("%s/greeting.html", srv_basedir); 29 xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
35 FILE *f; 30 FILE *f;
@@ -85,6 +80,8 @@ void httpd_connection(int rs)
85 int b_size = 0; 80 int b_size = 0;
86 char *ctype = NULL; 81 char *ctype = NULL;
87 xs *headers = NULL; 82 xs *headers = NULL;
83 xs *q_path = NULL;
84 char *p;
88 85
89 f = xs_socket_accept(rs); 86 f = xs_socket_accept(rs);
90 87
@@ -98,11 +95,20 @@ void httpd_connection(int rs)
98 req_hdrs = xs_dict_get(req, "headers"); 95 req_hdrs = xs_dict_get(req, "headers");
99 96
100 method = xs_dict_get(req_hdrs, "method"); 97 method = xs_dict_get(req_hdrs, "method");
98 q_path = xs_dup(xs_dict_get(req_hdrs, "path"));
99
100 /* crop the q_path from leading / and the prefix */
101 if (xs_endswith(q_path, "/"))
102 q_path = xs_crop(q_path, 0, -1);
103
104 p = xs_dict_get(srv_config, "prefix");
105 if (xs_startswith(q_path, p))
106 q_path = xs_crop(q_path, strlen(p), 0);
101 107
102 if (strcmp(method, "GET") == 0) { 108 if (strcmp(method, "GET") == 0) {
103 /* cascade through */ 109 /* cascade through */
104 if (status == 0) 110 if (status == 0)
105 server_get_handler(req, &status, &body, &b_size, &ctype); 111 server_get_handler(req, q_path, &status, &body, &b_size, &ctype);
106 } 112 }
107 else 113 else
108 if (strcmp(method, "POST") == 0) { 114 if (strcmp(method, "POST") == 0) {
@@ -116,10 +122,10 @@ void httpd_connection(int rs)
116 status = 404; 122 status = 404;
117 123
118 if (status == 404) 124 if (status == 404)
119 body = "<h1>404 Not Found</h1>"; 125 body = xs_str_new("<h1>404 Not Found</h1>");
120 126
121 if (status == 400) 127 if (status == 400)
122 body = "<h1>400 Bad Request</h1>"; 128 body = xs_str_new("<h1>400 Bad Request</h1>");
123 129
124 if (status == 303) 130 if (status == 303)
125 headers = xs_dict_append(headers, "location", body); 131 headers = xs_dict_append(headers, "location", body);