summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2022-09-25 07:42:57 +0200
committerGravatar default2022-09-25 07:42:57 +0200
commitb070d2d8f88866bf83103c34c4d86352c6b74e8d (patch)
treef3d48f9a370b92560e25cc0d96dac252ee851b96
parentNew function srv_archive(). (diff)
downloadsnac2-b070d2d8f88866bf83103c34c4d86352c6b74e8d.tar.gz
snac2-b070d2d8f88866bf83103c34c4d86352c6b74e8d.tar.xz
snac2-b070d2d8f88866bf83103c34c4d86352c6b74e8d.zip
The HTTP request headers are stored in a plain dict.
-rw-r--r--activitypub.c6
-rw-r--r--httpd.c10
-rw-r--r--snac.c3
-rw-r--r--xs_httpd.h31
4 files changed, 21 insertions, 29 deletions
diff --git a/activitypub.c b/activitypub.c
index 976222a..ec1d820 100644
--- a/activitypub.c
+++ b/activitypub.c
@@ -258,8 +258,7 @@ int activitypub_get_handler(d_char *req, char *q_path,
258 char **body, int *b_size, char **ctype) 258 char **body, int *b_size, char **ctype)
259{ 259{
260 int status = 200; 260 int status = 200;
261 char *headers = xs_dict_get(req, "headers"); 261 char *accept = xs_dict_get(req, "accept");
262 char *accept = xs_dict_get(headers, "accept");
263 snac snac; 262 snac snac;
264 xs *msg = NULL; 263 xs *msg = NULL;
265 264
@@ -321,8 +320,7 @@ int activitypub_post_handler(d_char *req, char *q_path,
321/* processes an input message */ 320/* processes an input message */
322{ 321{
323 int status = 202; /* accepted */ 322 int status = 202; /* accepted */
324 char *headers = xs_dict_get(req, "headers"); 323 char *i_ctype = xs_dict_get(req, "content-type");
325 char *i_ctype = xs_dict_get(headers, "content-type");
326 snac snac; 324 snac snac;
327 325
328 if (i_ctype == NULL) 326 if (i_ctype == NULL)
diff --git a/httpd.c b/httpd.c
index bf72d5a..7728d04 100644
--- a/httpd.c
+++ b/httpd.c
@@ -24,8 +24,7 @@ int server_get_handler(d_char *req, char *q_path,
24/* basic server services */ 24/* basic server services */
25{ 25{
26 int status = 0; 26 int status = 0;
27 char *req_hdrs = xs_dict_get(req, "headers"); 27 char *acpt = xs_dict_get(req, "accept");
28 char *acpt = xs_dict_get(req_hdrs, "accept");
29 28
30 if (acpt == NULL) 29 if (acpt == NULL)
31 return 400; 30 return 400;
@@ -90,7 +89,6 @@ void httpd_connection(int rs)
90{ 89{
91 FILE *f; 90 FILE *f;
92 xs *req; 91 xs *req;
93 char *req_hdrs;
94 char *method; 92 char *method;
95 int status = 0; 93 int status = 0;
96 char *body = NULL; 94 char *body = NULL;
@@ -106,10 +104,8 @@ void httpd_connection(int rs)
106 104
107 req = xs_httpd_request(f, &payload, &p_size); 105 req = xs_httpd_request(f, &payload, &p_size);
108 106
109 req_hdrs = xs_dict_get(req, "headers"); 107 method = xs_dict_get(req, "method");
110 108 q_path = xs_dup(xs_dict_get(req, "path"));
111 method = xs_dict_get(req_hdrs, "method");
112 q_path = xs_dup(xs_dict_get(req_hdrs, "path"));
113 109
114 /* crop the q_path from leading / and the prefix */ 110 /* crop the q_path from leading / and the prefix */
115 if (xs_endswith(q_path, "/")) 111 if (xs_endswith(q_path, "/"))
diff --git a/snac.c b/snac.c
index ba50a79..f5e23bc 100644
--- a/snac.c
+++ b/snac.c
@@ -163,8 +163,7 @@ void srv_archive(char *direction, char *req, char *payload, int p_size,
163 163
164 if (p_size && payload) { 164 if (p_size && payload) {
165 xs *payload_fn; 165 xs *payload_fn;
166 char *h = xs_dict_get(req, "headers"); 166 char *v = xs_dict_get(req, "content-type");
167 char *v = xs_dict_get(h, "content-type");
168 167
169 if (v && xs_str_in(v, "json") != -1) { 168 if (v && xs_str_in(v, "json") != -1) {
170 payload_fn = xs_fmt("%s/payload.json", dir); 169 payload_fn = xs_fmt("%s/payload.json", dir);
diff --git a/xs_httpd.h b/xs_httpd.h
index d56226f..0842e15 100644
--- a/xs_httpd.h
+++ b/xs_httpd.h
@@ -72,10 +72,9 @@ d_char *xs_url_vars(char *str)
72d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) 72d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
73/* processes an httpd connection */ 73/* processes an httpd connection */
74{ 74{
75 xs *headers = NULL; 75 d_char *req = NULL;
76 xs *q_vars = NULL; 76 xs *q_vars = NULL;
77 xs *p_vars = NULL; 77 xs *p_vars = NULL;
78 d_char *req = NULL;
79 xs *l1, *l2; 78 xs *l1, *l2;
80 char *v; 79 char *v;
81 80
@@ -90,10 +89,10 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
90 return NULL; 89 return NULL;
91 } 90 }
92 91
93 headers = xs_dict_new(); 92 req = xs_dict_new();
94 93
95 headers = xs_dict_append(headers, "method", xs_list_get(l2, 0)); 94 req = xs_dict_append(req, "method", xs_list_get(l2, 0));
96 headers = xs_dict_append(headers, "proto", xs_list_get(l2, 2)); 95 req = xs_dict_append(req, "proto", xs_list_get(l2, 2));
97 96
98 { 97 {
99 /* split the path with its optional variables */ 98 /* split the path with its optional variables */
@@ -101,7 +100,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
101 xs *pnv = xs_split_n(udp, "?", 1); 100 xs *pnv = xs_split_n(udp, "?", 1);
102 101
103 /* store the path */ 102 /* store the path */
104 headers = xs_dict_append(headers, "path", xs_list_get(pnv, 0)); 103 req = xs_dict_append(req, "path", xs_list_get(pnv, 0));
105 104
106 /* get the variables */ 105 /* get the variables */
107 q_vars = xs_url_vars(xs_list_get(pnv, 1)); 106 q_vars = xs_url_vars(xs_list_get(pnv, 1));
@@ -121,20 +120,19 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
121 p = xs_split_n(l, ": ", 1); 120 p = xs_split_n(l, ": ", 1);
122 121
123 if (xs_list_len(p) == 2) 122 if (xs_list_len(p) == 2)
124 headers = xs_dict_append(headers, 123 req = xs_dict_append(req, xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1));
125 xs_tolower(xs_list_get(p, 0)), xs_list_get(p, 1));
126 } 124 }
127 125
128 xs_socket_timeout(fileno(f), 5.0, 0.0); 126 xs_socket_timeout(fileno(f), 5.0, 0.0);
129 127
130 if ((v = xs_dict_get(headers, "content-length")) != NULL) { 128 if ((v = xs_dict_get(req, "content-length")) != NULL) {
131 /* if it has a payload, load it */ 129 /* if it has a payload, load it */
132 *p_size = atoi(v); 130 *p_size = atoi(v);
133 *payload = xs_read(f, *p_size); 131 *payload = xs_read(f, *p_size);
134 } 132 }
135 133
136 /* does it have a payload with form urlencoded variables? */ 134 /* is the payload form urlencoded variables? */
137 v = xs_dict_get(headers, "content-type"); 135 v = xs_dict_get(req, "content-type");
138 136
139 if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) { 137 if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
140 xs *upl = xs_url_dec(*payload); 138 xs *upl = xs_url_dec(*payload);
@@ -143,11 +141,12 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
143 else 141 else
144 p_vars = xs_dict_new(); 142 p_vars = xs_dict_new();
145 143
146 if (errno == 0) { 144 req = xs_dict_append(req, "q_vars", q_vars);
147 req = xs_dict_new(); 145 req = xs_dict_append(req, "p_vars", p_vars);
148 req = xs_dict_append(req, "headers", headers); 146
149 req = xs_dict_append(req, "q_vars", q_vars); 147 if (errno) {
150 req = xs_dict_append(req, "p_vars", p_vars); 148 free(req);
149 req = NULL;
151 } 150 }
152 151
153 return req; 152 return req;