summaryrefslogtreecommitdiff
path: root/httpd.c
diff options
context:
space:
mode:
authorGravatar default2022-09-21 19:28:30 +0200
committerGravatar default2022-09-21 19:28:30 +0200
commit9270a0077ed0b78311db3267bdbc5fc2c0ac0ac4 (patch)
tree6a86bff32db37e4d93c5c78d0b68f5af02e31335 /httpd.c
parentMore httpd work. (diff)
downloadsnac2-9270a0077ed0b78311db3267bdbc5fc2c0ac0ac4.tar.gz
snac2-9270a0077ed0b78311db3267bdbc5fc2c0ac0ac4.tar.xz
snac2-9270a0077ed0b78311db3267bdbc5fc2c0ac0ac4.zip
Added a global server handler.
Diffstat (limited to 'httpd.c')
-rw-r--r--httpd.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/httpd.c b/httpd.c
index 5fd8bbf..3047b3e 100644
--- a/httpd.c
+++ b/httpd.c
@@ -2,6 +2,7 @@
2/* copyright (c) 2022 grunfink - MIT license */ 2/* copyright (c) 2022 grunfink - MIT license */
3 3
4#include "xs.h" 4#include "xs.h"
5#include "xs_io.h"
5#include "xs_encdec.h" 6#include "xs_encdec.h"
6#include "xs_json.h" 7#include "xs_json.h"
7#include "xs_socket.h" 8#include "xs_socket.h"
@@ -10,11 +11,80 @@
10#include "snac.h" 11#include "snac.h"
11 12
12 13
14void server_get_handler(d_char *req, int *status, char **body, int *b_size, char **ctype)
15/* basic server services */
16{
17 char *req_hdrs = xs_dict_get(req, "headers");
18 char *acpt = xs_dict_get(req_hdrs, "accept");
19 char *q_path = xs_dict_get(req_hdrs, "path");
20
21 if (acpt == NULL) {
22 *status = 400;
23 return;
24 }
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? */
32 if (strcmp(q_path, prefix) == 0) {
33 /* try to open greeting.html */
34 xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
35 FILE *f;
36
37 if ((f = fopen(fn, "r")) != NULL) {
38 d_char *s = xs_readall(f);
39 fclose(f);
40
41 *status = 200;
42
43 /* does it have a %userlist% mark? */
44 if (xs_str_in(s, "%userlist%") != -1) {
45 char *host = xs_dict_get(srv_config, "host");
46 xs *list = user_list();
47 char *p, *uid;
48 xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n");
49
50 p = list;
51 while (xs_list_iter(&p, &uid)) {
52 snac snac;
53
54 if (user_open(&snac, uid)) {
55 xs *u = xs_fmt(
56 "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n",
57 snac.actor, uid, host,
58 xs_dict_get(snac.config, "name"));
59
60 ul = xs_str_cat(ul, u);
61
62 user_free(&snac);
63 }
64 }
65
66 ul = xs_str_cat(ul, "</ul>\n");
67
68 s = xs_replace(s, "%userlist%", ul);
69 }
70
71 *body = s;
72 }
73 }
74}
75
13void httpd_connection(int rs) 76void httpd_connection(int rs)
14/* the connection loop */ 77/* the connection loop */
15{ 78{
16 FILE *f; 79 FILE *f;
17 xs *req; 80 xs *req;
81 char *req_hdrs;
82 char *method;
83 int status = 0;
84 char *body = NULL;
85 int b_size = 0;
86 char *ctype = NULL;
87 xs *headers = NULL;
18 88
19 f = xs_socket_accept(rs); 89 f = xs_socket_accept(rs);
20 90
@@ -25,7 +95,52 @@ void httpd_connection(int rs)
25 printf("%s\n", j); 95 printf("%s\n", j);
26 } 96 }
27 97
98 req_hdrs = xs_dict_get(req, "headers");
99
100 method = xs_dict_get(req_hdrs, "method");
101
102 if (strcmp(method, "GET") == 0) {
103 /* cascade through */
104 if (status == 0)
105 server_get_handler(req, &status, &body, &b_size, &ctype);
106 }
107 else
108 if (strcmp(method, "POST") == 0) {
109 }
110
111 /* let's go */
112 headers = xs_dict_new();
113
114 /* unattended? it's an error */
115 if (status == 0)
116 status = 404;
117
118 if (status == 404)
119 body = "<h1>404 Not Found</h1>";
120
121 if (status == 400)
122 body = "<h1>400 Bad Request</h1>";
123
124 if (status == 303)
125 headers = xs_dict_append(headers, "location", body);
126
127 if (status == 401)
128 headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
129
130 if (ctype == NULL)
131 ctype = "text/html; charset=utf-8";
132
133 headers = xs_dict_append(headers, "content-type", ctype);
134 headers = xs_dict_append(headers, "x-creator", "snac/2.x");
135
136 if (b_size == 0 && body != NULL)
137 b_size = strlen(body);
138
139 xs_httpd_response(f, status, headers, body, b_size);
140
28 fclose(f); 141 fclose(f);
142
143 free(body);
29} 144}
30 145
31 146