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

Last change on this file since 58 was 46, checked in by Nishi, on Sep 18, 2024 at 6:58:45 PM

fixing close_socket

  • Property svn:keywords set to Id
File size: 6.1 KB
Line 
1/* $Id: http.c 46 2024-09-18 09:58:45Z 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 cm_log("HTTP", "Timeout, disconncting");
70 free(header);
71 tw_free_request(req);
72 return -1;
73 }
74#ifndef NO_SSL
75 }
76#endif
77 int len = tw_read(ssl, sock, buffer, 512);
78 if(len <= 0) break;
79 int i;
80 for(i = 0; i < len; i++) {
81 char c = buffer[i];
82 if(phase == 0) {
83 if(c == ' ') {
84 if(req->method == NULL) {
85 tw_free_request(req);
86 bad = true;
87 goto getout;
88 } else {
89 phase++;
90 }
91 } else {
92 if(req->method == NULL) {
93 req->method = malloc(1);
94 req->method[0] = 0;
95 }
96 cbuf[0] = c;
97 char* tmp = req->method;
98 req->method = cm_strcat(tmp, cbuf);
99 free(tmp);
100 }
101 } else if(phase == 1) {
102 if(c == ' ') {
103 if(req->path == NULL) {
104 tw_free_request(req);
105 bad = true;
106 goto getout;
107 } else {
108 phase++;
109 }
110 } else {
111 if(req->path == NULL) {
112 req->path = malloc(1);
113 req->path[0] = 0;
114 }
115 cbuf[0] = c;
116 char* tmp = req->path;
117 req->path = cm_strcat(tmp, cbuf);
118 free(tmp);
119 }
120 } else if(phase == 2) {
121 if(c == '\n') {
122 if(req->version == NULL) {
123 tw_free_request(req);
124 bad = true;
125 goto getout;
126 } else {
127 /* We have Method, Path, Version now */
128
129 if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) {
130 cm_log("HTTP", "Bad HTTP Version");
131 bad = true;
132 goto getout;
133 }
134
135 int j;
136 char* p = malloc(1);
137 p[0] = 0;
138 for(j = 0; req->path[j] != 0; j++) {
139 if(req->path[j] == '/') {
140 cbuf[0] = '/';
141 for(; req->path[j] != 0 && req->path[j] == '/'; j++)
142 ;
143 j--;
144 } else {
145 cbuf[0] = req->path[j];
146 }
147 char* tmp = p;
148 p = cm_strcat(tmp, cbuf);
149 free(tmp);
150 }
151 free(req->path);
152 req->path = p;
153
154 int incr = 0;
155 p = malloc(1);
156 p[0] = 0;
157 for(j = 0;; j++) {
158 if(req->path[j] == '/' || req->path[j] == 0) {
159 char oldc = req->path[j];
160 cbuf[0] = oldc;
161 req->path[j] = 0;
162
163 char* pth = req->path + incr;
164
165 if(strcmp(pth, "..") == 0) {
166 int k;
167 if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
168 for(k = strlen(p) - 1; k >= 0; k--) {
169 if(p[k] == '/') {
170 p[k + 1] = 0;
171 break;
172 }
173 }
174 if(strlen(p) == 0) {
175 free(p);
176 p = cm_strdup("/");
177 }
178 } else if(strcmp(pth, ".") == 0) {
179 } else {
180 char* tmp = p;
181 p = cm_strcat3(tmp, pth, cbuf);
182 free(tmp);
183 }
184
185 incr = j + 1;
186 if(oldc == 0) break;
187 }
188 }
189 free(req->path);
190 req->path = p;
191
192 cm_log("HTTP", "Request: %s %s %s", req->method, req->path, req->version);
193
194 phase++;
195 }
196 } else if(c != '\r') {
197 if(req->version == NULL) {
198 req->version = malloc(1);
199 req->version[0] = 0;
200 }
201 cbuf[0] = c;
202 char* tmp = req->version;
203 req->version = cm_strcat(tmp, cbuf);
204 free(tmp);
205 }
206 } else if(phase == 3) {
207 if(c == '\n') {
208 nl++;
209 if(nl == 2) {
210 phase++;
211 goto getout;
212 } else {
213 if(req->headers == NULL) {
214 req->headers = malloc(sizeof(*req->headers));
215 req->headers[0] = NULL;
216 }
217 int j;
218 for(j = 0; header[j] != 0; j++) {
219 if(header[j] == ':') {
220 header[j] = 0;
221 j++;
222 for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
223 ;
224 char* kv = header;
225 char* vv = header + j;
226
227 char** old = req->headers;
228 int k;
229 for(k = 0; old[k] != NULL; k++)
230 ;
231 req->headers = malloc(sizeof(*req->headers) * (k + 3));
232 for(k = 0; old[k] != NULL; k++) req->headers[k] = old[k];
233 req->headers[k] = cm_strdup(kv);
234 req->headers[k + 1] = cm_strdup(vv);
235 req->headers[k + 2] = NULL;
236 free(old);
237
238 cm_log("HTTP", "Header: %s: %s", kv, vv);
239
240 break;
241 }
242 }
243 free(header);
244 header = malloc(1);
245 header[0] = 0;
246 }
247 } else if(c != '\r') {
248 nl = 0;
249 cbuf[0] = c;
250 char* tmp = header;
251 header = cm_strcat(tmp, cbuf);
252 free(tmp);
253 }
254 }
255 }
256 }
257getout:
258 free(header);
259 if(bad) {
260 tw_free_request(req);
261 return 1;
262 }
263 char* result = malloc(1);
264 result[0] = 0;
265 int i;
266 for(i = 0; req->path[i] != 0; i++) {
267 if(req->path[i] == '?') {
268 req->path[i] = 0;
269 req->query = cm_strdup(req->path + i + 1);
270 break;
271 }
272 }
273 for(i = 0; req->path[i] != 0; i++) {
274 if(req->path[i] == '%') {
275 if(req->path[i + 1] == 0) continue;
276 cbuf[0] = cm_hex(req->path + i + 1, 2);
277 char* tmp = result;
278 result = cm_strcat(tmp, cbuf);
279 free(tmp);
280 i += 2;
281 } else {
282 cbuf[0] = req->path[i];
283 char* tmp = result;
284 result = cm_strcat(tmp, cbuf);
285 free(tmp);
286 }
287 }
288 free(req->path);
289 req->path = result;
290 return 0;
291}
Note: See TracBrowser for help on using the repository browser.