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