source: Main/trunk/Server/http.c@ 32

Last change on this file since 32 was 23, checked in by Nishi, on Sep 14, 2024 at 10:31:16 PM

icon works

  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1/* $Id: http.c 23 2024-09-14 13:31:16Z nishi $ */
2
3#define SOURCE
4
5#include "tw_http.h"
6
7#include "tw_server.h"
8
9#include <cm_log.h>
10#include <cm_string.h>
11
12#include <stdbool.h>
13#include <stdlib.h>
14#include <string.h>
15
16#ifdef __MINGW32__
17#include <winsock2.h>
18#else
19#include <sys/select.h>
20#endif
21
22void tw_free_request(struct tw_http_request* req) {
23 if(req->method != NULL) free(req->method);
24 if(req->path != NULL) free(req->path);
25 if(req->query != NULL) free(req->query);
26 if(req->headers != NULL) {
27 int i;
28 for(i = 0; req->headers[i] != NULL; i++) free(req->headers[i]);
29 free(req->headers);
30 }
31 if(req->body != NULL) free(req->body);
32 if(req->version != NULL) free(req->version);
33}
34
35int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) {
36 char buffer[512];
37 char cbuf[2];
38 int phase = 0;
39 fd_set fds;
40
41 bool bad = false;
42
43 cbuf[1] = 0;
44
45 req->method = NULL;
46 req->path = NULL;
47 req->query = NULL;
48 req->headers = NULL;
49 req->body = NULL;
50 req->version = NULL;
51
52 char* header = malloc(1);
53 header[0] = 0;
54 int nl = 0;
55
56 while(1) {
57 FD_ZERO(&fds);
58 FD_SET(sock, &fds);
59 struct timeval tv;
60 tv.tv_sec = 5;
61 tv.tv_usec = 0;
62 if(ssl == NULL || !SSL_has_pending(ssl)) {
63 int n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
64 if(n <= 0) {
65 free(header);
66 tw_free_request(req);
67 return -1;
68 }
69 }
70 int len = tw_read(ssl, sock, buffer, 512);
71 if(len <= 0) break;
72 int i;
73 for(i = 0; i < len; i++) {
74 char c = buffer[i];
75 if(phase == 0) {
76 if(c == ' ') {
77 if(req->method == NULL) {
78 tw_free_request(req);
79 bad = true;
80 goto getout;
81 } else {
82 phase++;
83 }
84 } else {
85 if(req->method == NULL) {
86 req->method = malloc(1);
87 req->method[0] = 0;
88 }
89 cbuf[0] = c;
90 char* tmp = req->method;
91 req->method = cm_strcat(tmp, cbuf);
92 free(tmp);
93 }
94 } else if(phase == 1) {
95 if(c == ' ') {
96 if(req->path == NULL) {
97 tw_free_request(req);
98 bad = true;
99 goto getout;
100 } else {
101 phase++;
102 }
103 } else {
104 if(req->path == NULL) {
105 req->path = malloc(1);
106 req->path[0] = 0;
107 }
108 cbuf[0] = c;
109 char* tmp = req->path;
110 req->path = cm_strcat(tmp, cbuf);
111 free(tmp);
112 }
113 } else if(phase == 2) {
114 if(c == '\n') {
115 if(req->version == NULL) {
116 tw_free_request(req);
117 bad = true;
118 goto getout;
119 } else {
120 /* We have Method, Path, Version now */
121
122 if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) {
123 cm_log("HTTP", "Bad HTTP Version");
124 bad = true;
125 goto getout;
126 }
127
128 int j;
129 char* p = malloc(1);
130 p[0] = 0;
131 for(j = 0; req->path[j] != 0; j++) {
132 if(req->path[j] == '/') {
133 cbuf[0] = '/';
134 for(; req->path[j] != 0 && req->path[j] == '/'; j++)
135 ;
136 j--;
137 } else {
138 cbuf[0] = req->path[j];
139 }
140 char* tmp = p;
141 p = cm_strcat(tmp, cbuf);
142 free(tmp);
143 }
144 free(req->path);
145 req->path = p;
146
147 int incr = 0;
148 p = malloc(1);
149 p[0] = 0;
150 for(j = 0;; j++) {
151 if(req->path[j] == '/' || req->path[j] == 0) {
152 char oldc = req->path[j];
153 cbuf[0] = oldc;
154 req->path[j] = 0;
155
156 char* pth = req->path + incr;
157
158 if(strcmp(pth, "..") == 0) {
159 int k;
160 if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
161 for(k = strlen(p) - 1; k >= 0; k--) {
162 if(p[k] == '/') {
163 p[k + 1] = 0;
164 break;
165 }
166 }
167 if(strlen(p) == 0) {
168 free(p);
169 p = cm_strdup("/");
170 }
171 } else if(strcmp(pth, ".") == 0) {
172 } else {
173 char* tmp = p;
174 p = cm_strcat3(tmp, pth, cbuf);
175 free(tmp);
176 }
177
178 incr = j + 1;
179 if(oldc == 0) break;
180 }
181 }
182 free(req->path);
183 req->path = p;
184
185 cm_log("HTTP", "Request: %s %s %s", req->method, req->path, req->version);
186
187 phase++;
188 }
189 } else if(c != '\r') {
190 if(req->version == NULL) {
191 req->version = malloc(1);
192 req->version[0] = 0;
193 }
194 cbuf[0] = c;
195 char* tmp = req->version;
196 req->version = cm_strcat(tmp, cbuf);
197 free(tmp);
198 }
199 } else if(phase == 3) {
200 if(c == '\n') {
201 nl++;
202 if(nl == 2) {
203 phase++;
204 goto getout;
205 } else {
206 if(req->headers == NULL) {
207 req->headers = malloc(sizeof(*req->headers));
208 req->headers[0] = NULL;
209 }
210 int j;
211 for(j = 0; header[j] != 0; j++) {
212 if(header[j] == ':') {
213 header[j] = 0;
214 j++;
215 for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
216 ;
217 char* kv = header;
218 char* vv = header + j;
219
220 char** old = req->headers;
221 int k;
222 for(k = 0; old[k] != NULL; k++)
223 ;
224 req->headers = malloc(sizeof(*req->headers) * (k + 3));
225 for(k = 0; old[k] != NULL; k++) req->headers[k] = old[k];
226 req->headers[k] = cm_strdup(kv);
227 req->headers[k + 1] = cm_strdup(vv);
228 req->headers[k + 2] = NULL;
229 free(old);
230
231 cm_log("HTTP", "Header: %s: %s", kv, vv);
232
233 break;
234 }
235 }
236 free(header);
237 header = malloc(1);
238 header[0] = 0;
239 }
240 } else if(c != '\r') {
241 nl = 0;
242 cbuf[0] = c;
243 char* tmp = header;
244 header = cm_strcat(tmp, cbuf);
245 free(tmp);
246 }
247 }
248 }
249 }
250getout:
251 free(header);
252 if(bad) {
253 tw_free_request(req);
254 return 1;
255 }
256 char* result = malloc(1);
257 result[0] = 0;
258 int i;
259 for(i = 0; req->path[i] != 0; i++) {
260 if(req->path[i] == '?') {
261 req->path[i] = 0;
262 req->query = cm_strdup(req->path + i + 1);
263 break;
264 }
265 }
266 for(i = 0; req->path[i] != 0; i++) {
267 if(req->path[i] == '%') {
268 if(req->path[i + 1] == 0) continue;
269 cbuf[0] = cm_hex(req->path + i + 1, 2);
270 char* tmp = result;
271 result = cm_strcat(tmp, cbuf);
272 free(tmp);
273 i += 2;
274 } else {
275 cbuf[0] = req->path[i];
276 char* tmp = result;
277 result = cm_strcat(tmp, cbuf);
278 free(tmp);
279 }
280 }
281 free(req->path);
282 req->path = result;
283 return 0;
284}
Note: See TracBrowser for help on using the repository browser.