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

Last change on this file since 43 was 43, checked in by Nishi, on Sep 18, 2024 at 6:19:03 PM

add NO_SSL

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