[16] | 1 | /* $Id: http.c 337 2024-10-14 18:08:05Z nishi $ */
|
---|
| 2 |
|
---|
| 3 | #define SOURCE
|
---|
| 4 |
|
---|
[43] | 5 | #include "../config.h"
|
---|
| 6 |
|
---|
[16] | 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>
|
---|
[17] | 16 | #include <string.h>
|
---|
[16] | 17 |
|
---|
[312] | 18 | #ifdef __OS2__
|
---|
| 19 | #include <sys/time.h>
|
---|
| 20 | #include <types.h>
|
---|
| 21 | #include <tcpustd.h>
|
---|
| 22 | #endif
|
---|
| 23 |
|
---|
[315] | 24 | #if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__NETWARE__))
|
---|
[240] | 25 | #ifdef USE_WINSOCK1
|
---|
| 26 | #include <winsock.h>
|
---|
| 27 | #else
|
---|
[17] | 28 | #include <winsock2.h>
|
---|
[240] | 29 | #endif
|
---|
[315] | 30 | #elif defined(__NETWARE__)
|
---|
| 31 | #include <sys/socket.h>
|
---|
[17] | 32 | #else
|
---|
[118] | 33 | #ifdef USE_POLL
|
---|
[187] | 34 | #ifdef __PPU__
|
---|
| 35 | #include <net/poll.h>
|
---|
| 36 | #else
|
---|
[118] | 37 | #include <poll.h>
|
---|
[187] | 38 | #endif
|
---|
[118] | 39 | #else
|
---|
[336] | 40 | #ifdef __NeXT__
|
---|
| 41 | #include <sys/socket.h>
|
---|
[337] | 42 | #include <sys/time.h>
|
---|
[336] | 43 | #else
|
---|
[17] | 44 | #include <sys/select.h>
|
---|
| 45 | #endif
|
---|
[118] | 46 | #endif
|
---|
[335] | 47 | #endif
|
---|
[17] | 48 |
|
---|
[16] | 49 | void tw_free_request(struct tw_http_request* req) {
|
---|
| 50 | if(req->method != NULL) free(req->method);
|
---|
| 51 | if(req->path != NULL) free(req->path);
|
---|
[20] | 52 | if(req->query != NULL) free(req->query);
|
---|
[16] | 53 | if(req->headers != NULL) {
|
---|
| 54 | int i;
|
---|
| 55 | for(i = 0; req->headers[i] != NULL; i++) free(req->headers[i]);
|
---|
| 56 | free(req->headers);
|
---|
| 57 | }
|
---|
| 58 | if(req->body != NULL) free(req->body);
|
---|
| 59 | if(req->version != NULL) free(req->version);
|
---|
[64] | 60 |
|
---|
| 61 | req->method = NULL;
|
---|
| 62 | req->path = NULL;
|
---|
| 63 | req->query = NULL;
|
---|
| 64 | req->headers = NULL;
|
---|
| 65 | req->body = NULL;
|
---|
| 66 | req->version = NULL;
|
---|
[16] | 67 | }
|
---|
| 68 |
|
---|
| 69 | int tw_http_parse(SSL* ssl, int sock, struct tw_http_request* req) {
|
---|
| 70 | char buffer[512];
|
---|
| 71 | char cbuf[2];
|
---|
| 72 | int phase = 0;
|
---|
[212] | 73 | char* header;
|
---|
| 74 | int nl;
|
---|
[294] | 75 | bool bad;
|
---|
[118] | 76 |
|
---|
| 77 | #ifdef USE_POLL
|
---|
| 78 | struct pollfd pollfds[1];
|
---|
| 79 | pollfds[0].fd = sock;
|
---|
| 80 | pollfds[0].events = POLLIN | POLLPRI;
|
---|
| 81 | #else
|
---|
[16] | 82 | fd_set fds;
|
---|
[118] | 83 | #endif
|
---|
[16] | 84 |
|
---|
[294] | 85 | bad = false;
|
---|
[16] | 86 |
|
---|
| 87 | cbuf[1] = 0;
|
---|
| 88 |
|
---|
| 89 | req->method = NULL;
|
---|
| 90 | req->path = NULL;
|
---|
[20] | 91 | req->query = NULL;
|
---|
[16] | 92 | req->headers = NULL;
|
---|
| 93 | req->body = NULL;
|
---|
| 94 | req->version = NULL;
|
---|
| 95 |
|
---|
[212] | 96 | header = malloc(1);
|
---|
[16] | 97 | header[0] = 0;
|
---|
[212] | 98 | nl = 0;
|
---|
[16] | 99 |
|
---|
| 100 | while(1) {
|
---|
[212] | 101 | int i;
|
---|
| 102 | int len;
|
---|
[213] | 103 | int n;
|
---|
[118] | 104 | #ifndef USE_POLL
|
---|
[212] | 105 | struct timeval tv;
|
---|
[16] | 106 | FD_ZERO(&fds);
|
---|
| 107 | FD_SET(sock, &fds);
|
---|
| 108 | tv.tv_sec = 5;
|
---|
| 109 | tv.tv_usec = 0;
|
---|
[118] | 110 | #endif
|
---|
[43] | 111 | #ifndef NO_SSL
|
---|
[23] | 112 | if(ssl == NULL || !SSL_has_pending(ssl)) {
|
---|
[43] | 113 | #endif
|
---|
[118] | 114 | #ifdef USE_POLL
|
---|
[212] | 115 | n = poll(pollfds, 1, 5000);
|
---|
[118] | 116 | #else
|
---|
[88] | 117 | #ifdef __HAIKU__
|
---|
[212] | 118 | n = select(32, &fds, NULL, NULL, &tv);
|
---|
[88] | 119 | #else
|
---|
[212] | 120 | n = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
|
---|
[88] | 121 | #endif
|
---|
[118] | 122 | #endif
|
---|
[22] | 123 | if(n <= 0) {
|
---|
[147] | 124 | cm_log("HTTP", "Timeout, disconnecting");
|
---|
[22] | 125 | free(header);
|
---|
| 126 | tw_free_request(req);
|
---|
| 127 | return -1;
|
---|
| 128 | }
|
---|
[43] | 129 | #ifndef NO_SSL
|
---|
[22] | 130 | }
|
---|
[43] | 131 | #endif
|
---|
[212] | 132 | len = tw_read(ssl, sock, buffer, 512);
|
---|
[70] | 133 | if(len <= 0) {
|
---|
[64] | 134 | bad = true;
|
---|
| 135 | break;
|
---|
| 136 | }
|
---|
[16] | 137 | for(i = 0; i < len; i++) {
|
---|
| 138 | char c = buffer[i];
|
---|
| 139 | if(phase == 0) {
|
---|
| 140 | if(c == ' ') {
|
---|
| 141 | if(req->method == NULL) {
|
---|
| 142 | tw_free_request(req);
|
---|
| 143 | bad = true;
|
---|
| 144 | goto getout;
|
---|
| 145 | } else {
|
---|
| 146 | phase++;
|
---|
| 147 | }
|
---|
| 148 | } else {
|
---|
[212] | 149 | char* tmp;
|
---|
[16] | 150 | if(req->method == NULL) {
|
---|
| 151 | req->method = malloc(1);
|
---|
| 152 | req->method[0] = 0;
|
---|
| 153 | }
|
---|
| 154 | cbuf[0] = c;
|
---|
[212] | 155 | tmp = req->method;
|
---|
[16] | 156 | req->method = cm_strcat(tmp, cbuf);
|
---|
| 157 | free(tmp);
|
---|
| 158 | }
|
---|
| 159 | } else if(phase == 1) {
|
---|
| 160 | if(c == ' ') {
|
---|
| 161 | if(req->path == NULL) {
|
---|
| 162 | tw_free_request(req);
|
---|
| 163 | bad = true;
|
---|
| 164 | goto getout;
|
---|
| 165 | } else {
|
---|
| 166 | phase++;
|
---|
| 167 | }
|
---|
| 168 | } else {
|
---|
[212] | 169 | char* tmp;
|
---|
[16] | 170 | if(req->path == NULL) {
|
---|
| 171 | req->path = malloc(1);
|
---|
| 172 | req->path[0] = 0;
|
---|
| 173 | }
|
---|
| 174 | cbuf[0] = c;
|
---|
[212] | 175 | tmp = req->path;
|
---|
[16] | 176 | req->path = cm_strcat(tmp, cbuf);
|
---|
| 177 | free(tmp);
|
---|
| 178 | }
|
---|
| 179 | } else if(phase == 2) {
|
---|
| 180 | if(c == '\n') {
|
---|
| 181 | if(req->version == NULL) {
|
---|
| 182 | tw_free_request(req);
|
---|
| 183 | bad = true;
|
---|
| 184 | goto getout;
|
---|
| 185 | } else {
|
---|
| 186 | /* We have Method, Path, Version now */
|
---|
[212] | 187 | int j;
|
---|
| 188 | char* p;
|
---|
| 189 | int incr;
|
---|
[16] | 190 | if(strcmp(req->version, "HTTP/1.1") != 0 && strcmp(req->version, "HTTP/1.0") != 0) {
|
---|
| 191 | cm_log("HTTP", "Bad HTTP Version");
|
---|
| 192 | bad = true;
|
---|
| 193 | goto getout;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[212] | 196 | p = malloc(1);
|
---|
[16] | 197 | p[0] = 0;
|
---|
| 198 | for(j = 0; req->path[j] != 0; j++) {
|
---|
[212] | 199 | char* tmp;
|
---|
[16] | 200 | if(req->path[j] == '/') {
|
---|
| 201 | cbuf[0] = '/';
|
---|
| 202 | for(; req->path[j] != 0 && req->path[j] == '/'; j++)
|
---|
| 203 | ;
|
---|
| 204 | j--;
|
---|
| 205 | } else {
|
---|
| 206 | cbuf[0] = req->path[j];
|
---|
| 207 | }
|
---|
[212] | 208 | tmp = p;
|
---|
[16] | 209 | p = cm_strcat(tmp, cbuf);
|
---|
| 210 | free(tmp);
|
---|
| 211 | }
|
---|
| 212 | free(req->path);
|
---|
| 213 | req->path = p;
|
---|
| 214 |
|
---|
[212] | 215 | incr = 0;
|
---|
[16] | 216 | p = malloc(1);
|
---|
| 217 | p[0] = 0;
|
---|
| 218 | for(j = 0;; j++) {
|
---|
| 219 | if(req->path[j] == '/' || req->path[j] == 0) {
|
---|
| 220 | char oldc = req->path[j];
|
---|
[212] | 221 | char* pth;
|
---|
[16] | 222 | cbuf[0] = oldc;
|
---|
| 223 | req->path[j] = 0;
|
---|
| 224 |
|
---|
[212] | 225 | pth = req->path + incr;
|
---|
[16] | 226 |
|
---|
| 227 | if(strcmp(pth, "..") == 0) {
|
---|
| 228 | int k;
|
---|
| 229 | if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
|
---|
| 230 | for(k = strlen(p) - 1; k >= 0; k--) {
|
---|
| 231 | if(p[k] == '/') {
|
---|
| 232 | p[k + 1] = 0;
|
---|
| 233 | break;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 | if(strlen(p) == 0) {
|
---|
| 237 | free(p);
|
---|
| 238 | p = cm_strdup("/");
|
---|
| 239 | }
|
---|
| 240 | } else if(strcmp(pth, ".") == 0) {
|
---|
| 241 | } else {
|
---|
| 242 | char* tmp = p;
|
---|
| 243 | p = cm_strcat3(tmp, pth, cbuf);
|
---|
| 244 | free(tmp);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | incr = j + 1;
|
---|
| 248 | if(oldc == 0) break;
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 | free(req->path);
|
---|
| 252 | req->path = p;
|
---|
| 253 |
|
---|
| 254 | cm_log("HTTP", "Request: %s %s %s", req->method, req->path, req->version);
|
---|
| 255 |
|
---|
| 256 | phase++;
|
---|
| 257 | }
|
---|
| 258 | } else if(c != '\r') {
|
---|
[212] | 259 | char* tmp;
|
---|
[16] | 260 | if(req->version == NULL) {
|
---|
| 261 | req->version = malloc(1);
|
---|
| 262 | req->version[0] = 0;
|
---|
| 263 | }
|
---|
| 264 | cbuf[0] = c;
|
---|
[212] | 265 | tmp = req->version;
|
---|
[16] | 266 | req->version = cm_strcat(tmp, cbuf);
|
---|
| 267 | free(tmp);
|
---|
| 268 | }
|
---|
| 269 | } else if(phase == 3) {
|
---|
| 270 | if(c == '\n') {
|
---|
| 271 | nl++;
|
---|
| 272 | if(nl == 2) {
|
---|
| 273 | phase++;
|
---|
| 274 | goto getout;
|
---|
| 275 | } else {
|
---|
[212] | 276 | int j;
|
---|
[16] | 277 | if(req->headers == NULL) {
|
---|
| 278 | req->headers = malloc(sizeof(*req->headers));
|
---|
| 279 | req->headers[0] = NULL;
|
---|
| 280 | }
|
---|
| 281 | for(j = 0; header[j] != 0; j++) {
|
---|
| 282 | if(header[j] == ':') {
|
---|
[212] | 283 | char* kv;
|
---|
| 284 | char* vv;
|
---|
| 285 | char** old;
|
---|
| 286 | int k;
|
---|
[16] | 287 | header[j] = 0;
|
---|
| 288 | j++;
|
---|
| 289 | for(; header[j] != 0 && (header[j] == ' ' || header[j] == '\t'); j++)
|
---|
| 290 | ;
|
---|
[212] | 291 | kv = header;
|
---|
| 292 | vv = header + j;
|
---|
[16] | 293 |
|
---|
[212] | 294 | old = req->headers;
|
---|
[16] | 295 | for(k = 0; old[k] != NULL; k++)
|
---|
| 296 | ;
|
---|
| 297 | req->headers = malloc(sizeof(*req->headers) * (k + 3));
|
---|
| 298 | for(k = 0; old[k] != NULL; k++) req->headers[k] = old[k];
|
---|
| 299 | req->headers[k] = cm_strdup(kv);
|
---|
| 300 | req->headers[k + 1] = cm_strdup(vv);
|
---|
| 301 | req->headers[k + 2] = NULL;
|
---|
| 302 | free(old);
|
---|
| 303 |
|
---|
| 304 | cm_log("HTTP", "Header: %s: %s", kv, vv);
|
---|
| 305 |
|
---|
| 306 | break;
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | free(header);
|
---|
| 310 | header = malloc(1);
|
---|
| 311 | header[0] = 0;
|
---|
| 312 | }
|
---|
| 313 | } else if(c != '\r') {
|
---|
[212] | 314 | char* tmp;
|
---|
[16] | 315 | nl = 0;
|
---|
| 316 | cbuf[0] = c;
|
---|
[212] | 317 | tmp = header;
|
---|
[16] | 318 | header = cm_strcat(tmp, cbuf);
|
---|
| 319 | free(tmp);
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 | getout:
|
---|
| 325 | free(header);
|
---|
| 326 | if(bad) {
|
---|
| 327 | tw_free_request(req);
|
---|
| 328 | return 1;
|
---|
| 329 | }
|
---|
[212] | 330 | {
|
---|
| 331 | char* result = malloc(1);
|
---|
| 332 | int i;
|
---|
| 333 | int j;
|
---|
| 334 | int incr;
|
---|
| 335 | char* p;
|
---|
| 336 | result[0] = 0;
|
---|
| 337 | for(i = 0; req->path[i] != 0; i++) {
|
---|
| 338 | if(req->path[i] == '?') {
|
---|
| 339 | req->path[i] = 0;
|
---|
| 340 | req->query = cm_strdup(req->path + i + 1);
|
---|
| 341 | break;
|
---|
| 342 | }
|
---|
[20] | 343 | }
|
---|
[212] | 344 | for(i = 0; req->path[i] != 0; i++) {
|
---|
| 345 | if(req->path[i] == '%') {
|
---|
| 346 | if(req->path[i + 1] == 0) continue;
|
---|
| 347 | cbuf[0] = cm_hex(req->path + i + 1, 2);
|
---|
| 348 | if(cbuf[0] != '\\') {
|
---|
| 349 | char* tmp = result;
|
---|
| 350 | result = cm_strcat(tmp, cbuf);
|
---|
| 351 | free(tmp);
|
---|
| 352 | }
|
---|
| 353 | i += 2;
|
---|
| 354 | } else if(req->path[i] != '\\') {
|
---|
| 355 | char* tmp;
|
---|
| 356 | cbuf[0] = req->path[i];
|
---|
| 357 | tmp = result;
|
---|
[70] | 358 | result = cm_strcat(tmp, cbuf);
|
---|
| 359 | free(tmp);
|
---|
| 360 | }
|
---|
[20] | 361 | }
|
---|
[212] | 362 | free(req->path);
|
---|
| 363 | req->path = result;
|
---|
[213] | 364 |
|
---|
[212] | 365 | incr = 0;
|
---|
| 366 | p = malloc(1);
|
---|
| 367 | p[0] = 0;
|
---|
| 368 | for(j = 0;; j++) {
|
---|
| 369 | if(req->path[j] == '/' || req->path[j] == 0) {
|
---|
| 370 | char oldc = req->path[j];
|
---|
| 371 | char* pth;
|
---|
| 372 | cbuf[0] = oldc;
|
---|
| 373 | req->path[j] = 0;
|
---|
[213] | 374 |
|
---|
[212] | 375 | pth = req->path + incr;
|
---|
[213] | 376 |
|
---|
[212] | 377 | if(strcmp(pth, "..") == 0) {
|
---|
| 378 | int k;
|
---|
| 379 | if(p[strlen(p) - 1] == '/') p[strlen(p) - 1] = 0;
|
---|
| 380 | for(k = strlen(p) - 1; k >= 0; k--) {
|
---|
| 381 | if(p[k] == '/') {
|
---|
| 382 | p[k + 1] = 0;
|
---|
| 383 | break;
|
---|
| 384 | }
|
---|
[69] | 385 | }
|
---|
[212] | 386 | if(strlen(p) == 0) {
|
---|
| 387 | free(p);
|
---|
| 388 | p = cm_strdup("/");
|
---|
| 389 | }
|
---|
| 390 | } else if(strcmp(pth, ".") == 0) {
|
---|
| 391 | } else {
|
---|
| 392 | char* tmp = p;
|
---|
| 393 | p = cm_strcat3(tmp, pth, cbuf);
|
---|
| 394 | free(tmp);
|
---|
[69] | 395 | }
|
---|
[213] | 396 |
|
---|
[212] | 397 | incr = j + 1;
|
---|
| 398 | if(oldc == 0) break;
|
---|
[69] | 399 | }
|
---|
| 400 | }
|
---|
[212] | 401 | free(req->path);
|
---|
| 402 | req->path = p;
|
---|
| 403 | return 0;
|
---|
[69] | 404 | }
|
---|
[16] | 405 | }
|
---|