1 2 /* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) NGINX, Inc. 5 */ 6 7 #include <nxt_router.h> 8 #include <nxt_http.h> 9 10 11 static nxt_int_t nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp); 12 static void nxt_http_request_start(nxt_task_t *task, void *obj, void *data); 13 static void nxt_http_request_action(nxt_task_t *task, void *obj, void *data); 14 static void nxt_http_request_proto_info(nxt_task_t *task, 15 nxt_http_request_t *r); 16 static void nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, 17 void *data); 18 static void nxt_http_request_done(nxt_task_t *task, void *obj, void *data); 19 20 static u_char *nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, 21 struct tm *tm, size_t size, const char *format); 22 23 24 static const nxt_http_request_state_t nxt_http_request_init_state; 25 static const nxt_http_request_state_t nxt_http_request_body_state; 26 27 28 nxt_time_string_t nxt_http_date_cache = { 29 (nxt_atomic_uint_t) -1, 30 nxt_http_date_cache_handler, 31 NULL, 32 NXT_HTTP_DATE_LEN, 33 NXT_THREAD_TIME_GMT, 34 NXT_THREAD_TIME_SEC, 35 }; 36 37 38 nxt_int_t 39 nxt_http_init(nxt_task_t *task, nxt_runtime_t *rt) 40 { 41 nxt_int_t ret; 42 43 ret = nxt_h1p_init(task, rt); 44 45 if (ret != NXT_OK) { 46 return ret; 47 } 48 49 return nxt_http_response_hash_init(task, rt); 50 } 51 52 53 nxt_int_t 54 nxt_http_request_host(void *ctx, nxt_http_field_t *field, uintptr_t data) 55 { 56 nxt_int_t ret; 57 nxt_str_t host; 58 nxt_http_request_t *r; 59 60 r = ctx; 61 62 if (nxt_slow_path(r->host.start != NULL)) { 63 return NXT_HTTP_BAD_REQUEST; 64 } 65 66 host.length = field->value_length; 67 host.start = field->value; 68 69 ret = nxt_http_validate_host(&host, r->mem_pool); 70 71 if (nxt_fast_path(ret == NXT_OK)) { 72 r->host = host; 73 } 74 75 return ret; 76 } 77 78 79 static nxt_int_t 80 nxt_http_validate_host(nxt_str_t *host, nxt_mp_t *mp) 81 { 82 u_char *h, ch; 83 size_t i, dot_pos, host_length; 84 nxt_bool_t lowcase; 85 86 enum { 87 sw_usual, 88 sw_literal, 89 sw_rest 90 } state; 91 92 dot_pos = host->length; 93 host_length = host->length; 94 95 h = host->start; 96 97 lowcase = 0; 98 state = sw_usual; 99 100 for (i = 0; i < host->length; i++) { 101 ch = h[i]; 102 103 if (ch > ']') { 104 /* Short path. */ 105 continue; 106 } 107 108 switch (ch) { 109 110 case '.': 111 if (dot_pos == i - 1) { 112 return NXT_HTTP_BAD_REQUEST; 113 } 114 115 dot_pos = i; 116 break; 117 118 case ':': 119 if (state == sw_usual) { 120 host_length = i; 121 state = sw_rest; 122 } 123 124 break; 125 126 case '[': 127 if (i == 0) { 128 state = sw_literal; 129 } 130 131 break; 132 133 case ']': 134 if (state == sw_literal) { 135 host_length = i + 1; 136 state = sw_rest; 137 } 138 139 break; 140 141 case '/': 142 return NXT_HTTP_BAD_REQUEST; 143 144 default: 145 if (ch >= 'A' && ch <= 'Z') { 146 lowcase = 1; 147 } 148 149 break; 150 } 151 } 152 153 if (dot_pos == host_length - 1) { 154 host_length--; 155 } 156 157 host->length = host_length; 158 159 if (lowcase) { 160 host->start = nxt_mp_nget(mp, host_length); 161 if (nxt_slow_path(host->start == NULL)) { 162 return NXT_HTTP_INTERNAL_SERVER_ERROR; 163 } 164 165 nxt_memcpy_lowcase(host->start, h, host_length); 166 } 167 168 return NXT_OK; 169 } 170 171 172 nxt_int_t 173 nxt_http_request_field(void *ctx, nxt_http_field_t *field, uintptr_t offset) 174 { 175 nxt_http_request_t *r; 176 177 r = ctx; 178 179 nxt_value_at(nxt_http_field_t *, r, offset) = field; 180 181 return NXT_OK; 182 } 183 184 185 nxt_int_t 186 nxt_http_request_content_length(void *ctx, nxt_http_field_t *field, 187 uintptr_t data) 188 { 189 nxt_off_t n, max_body_size; 190 nxt_http_request_t *r; 191 192 r = ctx; 193 194 if (nxt_fast_path(r->content_length == NULL)) { 195 r->content_length = field; 196 197 n = nxt_off_t_parse(field->value, field->value_length); 198 199 if (nxt_fast_path(n >= 0)) { 200 r->content_length_n = n; 201 202 max_body_size = r->conf->socket_conf->max_body_size; 203 204 if (nxt_slow_path(n > max_body_size)) { 205 return NXT_HTTP_PAYLOAD_TOO_LARGE; 206 } 207 208 return NXT_OK; 209 } 210 } 211 212 return NXT_HTTP_BAD_REQUEST; 213 } 214 215 216 nxt_http_request_t * 217 nxt_http_request_create(nxt_task_t *task) 218 { 219 nxt_mp_t *mp; 220 nxt_buf_t *last; 221 nxt_http_request_t *r; 222 223 mp = nxt_mp_create(1024, 128, 256, 32); 224 if (nxt_slow_path(mp == NULL)) { 225 return NULL; 226 } 227 228 r = nxt_mp_zget(mp, sizeof(nxt_http_request_t)); 229 if (nxt_slow_path(r == NULL)) { 230 goto fail; 231 } 232 233 r->resp.fields = nxt_list_create(mp, 8, sizeof(nxt_http_field_t)); 234 if (nxt_slow_path(r->resp.fields == NULL)) { 235 goto fail; 236 } 237 238 last = nxt_mp_zget(mp, NXT_BUF_SYNC_SIZE); 239 if (nxt_slow_path(last == NULL)) { 240 goto fail; 241 } 242 243 nxt_buf_set_sync(last); 244 nxt_buf_set_last(last); 245 last->completion_handler = nxt_http_request_done; 246 last->parent = r; 247 r->last = last; 248 249 r->mem_pool = mp; 250 r->content_length_n = -1; 251 r->resp.content_length_n = -1; 252 r->state = &nxt_http_request_init_state; 253 254 return r; 255 256 fail: 257 258 nxt_mp_release(mp); 259 260 return NULL; 261 } 262 263 264 static const nxt_http_request_state_t nxt_http_request_init_state 265 nxt_aligned(64) = 266 { 267 .ready_handler = nxt_http_request_start, 268 .error_handler = nxt_http_request_close_handler, 269 }; 270 271 272 static void 273 nxt_http_request_start(nxt_task_t *task, void *obj, void *data) 274 { 275 nxt_http_request_t *r; 276 277 r = obj; 278 279 r->state = &nxt_http_request_body_state; 280 281 nxt_http_request_read_body(task, r); 282 } 283 284 285 static const nxt_http_request_state_t nxt_http_request_body_state 286 nxt_aligned(64) = 287 { 288 .ready_handler = nxt_http_request_action, 289 .error_handler = nxt_http_request_close_handler, 290 }; 291 292 293 static void 294 nxt_http_request_action(nxt_task_t *task, void *obj, void *data) 295 { 296 nxt_http_action_t *action; 297 nxt_http_request_t *r; 298 299 r = obj; 300 301 action = r->conf->socket_conf->action; 302 303 if (nxt_fast_path(action != NULL)) { 304 305 do { 306 nxt_debug(task, "http request route: %V", &action->name); 307 308 action = action->handler(task, r, action); 309 310 if (action == NULL) { 311 return; 312 } 313 314 if (action == NXT_HTTP_ACTION_ERROR) { 315 break; 316 } 317 318 } while (r->pass_count++ < 255); 319 } 320 321 nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); 322 } 323 324 325 nxt_http_action_t * 326 nxt_http_application_handler(nxt_task_t *task, nxt_http_request_t *r, 327 nxt_http_action_t *action) 328 { 329 nxt_debug(task, "http application handler"); 330 331 /* 332 * TODO: need an application flag to get local address 333 * required by "SERVER_ADDR" in Pyhton and PHP. Not used in Go. 334 */ 335 nxt_http_request_proto_info(task, r); 336 337 if (r->host.length != 0) { 338 r->server_name = r->host; 339 340 } else { 341 nxt_str_set(&r->server_name, "localhost"); 342 } 343 344 nxt_router_process_http_request(task, r, action->u.application); 345 346 return NULL; 347 } 348 349 350 static void 351 nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r) 352 { 353 if (nxt_fast_path(r->proto.any != NULL)) { 354 nxt_http_proto[r->protocol].local_addr(task, r); 355 } 356 } 357 358 359 void 360 nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r) 361 { 362 if (nxt_fast_path(r->proto.any != NULL)) { 363 nxt_http_proto[r->protocol].body_read(task, r); 364 } 365 } 366 367 368 void 369 nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r, 370 nxt_work_handler_t body_handler, void *data) 371 { 372 u_char *p, *end; 373 nxt_http_field_t *server, *date, *content_length; 374 375 /* 376 * TODO: "Server", "Date", and "Content-Length" processing should be moved 377 * to the last header filter. 378 */ 379 380 server = nxt_list_zero_add(r->resp.fields); 381 if (nxt_slow_path(server == NULL)) { 382 goto fail; 383 } 384 385 nxt_http_field_set(server, "Server", NXT_SERVER); 386 387 if (r->resp.date == NULL) { 388 date = nxt_list_zero_add(r->resp.fields); 389 if (nxt_slow_path(date == NULL)) { 390 goto fail; 391 } 392 393 nxt_http_field_name_set(date, "Date"); 394 395 p = nxt_mp_nget(r->mem_pool, nxt_http_date_cache.size); 396 if (nxt_slow_path(p == NULL)) { 397 goto fail; 398 } 399 400 (void) nxt_thread_time_string(task->thread, &nxt_http_date_cache, p); 401 402 date->value = p; 403 date->value_length = nxt_http_date_cache.size; 404 405 r->resp.date = date; 406 } 407 408 if (r->resp.content_length_n != -1 409 && (r->resp.content_length == NULL || r->resp.content_length->skip)) 410 { 411 content_length = nxt_list_zero_add(r->resp.fields); 412 if (nxt_slow_path(content_length == NULL)) { 413 goto fail; 414 } 415 416 nxt_http_field_name_set(content_length, "Content-Length"); 417 418 p = nxt_mp_nget(r->mem_pool, NXT_OFF_T_LEN); 419 if (nxt_slow_path(p == NULL)) { 420 goto fail; 421 } 422 423 content_length->value = p; 424 end = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%O", r->resp.content_length_n); 425 content_length->value_length = end - p; 426 427 r->resp.content_length = content_length; 428 } 429 430 if (nxt_fast_path(r->proto.any != NULL)) { 431 nxt_http_proto[r->protocol].header_send(task, r, body_handler, data); 432 } 433 434 return; 435 436 fail: 437 438 nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); 439 } 440 441 442 void 443 nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r, 444 nxt_buf_t *ws_frame) 445 { 446 if (r->proto.any != NULL) { 447 nxt_http_proto[r->protocol].ws_frame_start(task, r, ws_frame); 448 } 449 } 450 451 452 void 453 nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) 454 { 455 if (nxt_fast_path(r->proto.any != NULL)) { 456 nxt_http_proto[r->protocol].send(task, r, out); 457 } 458 } 459 460 461 nxt_buf_t * 462 nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size) 463 { 464 nxt_buf_t *b; 465 466 b = nxt_buf_mem_alloc(r->mem_pool, size, 0); 467 if (nxt_fast_path(b != NULL)) { 468 b->completion_handler = nxt_http_request_mem_buf_completion; 469 b->parent = r; 470 nxt_mp_retain(r->mem_pool); 471 472 } else { 473 nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR); 474 } 475 476 return b; 477 } 478 479 480 static void 481 nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data) 482 { 483 nxt_buf_t *b, *next; 484 nxt_http_request_t *r; 485 486 b = obj; 487 r = data; 488 489 do { 490 next = b->next; 491 492 nxt_mp_free(r->mem_pool, b); 493 nxt_mp_release(r->mem_pool); 494 495 b = next; 496 } while (b != NULL); 497 } 498 499 500 nxt_buf_t * 501 nxt_http_buf_last(nxt_http_request_t *r) 502 { 503 nxt_buf_t *last; 504 505 last = r->last; 506 r->last = NULL; 507 508 return last; 509 } 510 511 512 static void 513 nxt_http_request_done(nxt_task_t *task, void *obj, void *data) 514 { 515 nxt_http_request_t *r; 516 517 r = data; 518 519 nxt_debug(task, "http request done"); 520 521 nxt_http_request_close_handler(task, r, r->proto.any); 522 } 523 524 525 void 526 nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data) 527 { 528 nxt_http_proto_t proto; 529 nxt_http_request_t *r; 530 531 r = obj; 532 proto.any = data; 533 534 nxt_debug(task, "http request error handler"); 535 536 r->error = 1; 537 538 if (nxt_fast_path(proto.any != NULL)) { 539 nxt_http_proto[r->protocol].discard(task, r, nxt_http_buf_last(r)); 540 } 541 } 542 543 544 void 545 nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data) 546 { 547 nxt_http_proto_t proto; 548 nxt_http_request_t *r; 549 nxt_http_protocol_t protocol; 550 nxt_socket_conf_joint_t *conf; 551 nxt_router_access_log_t *access_log; 552 553 r = obj; 554 proto.any = data; 555 556 nxt_debug(task, "http request close handler"); 557 558 conf = r->conf; 559 560 if (!r->logged) { 561 r->logged = 1; 562 563 access_log = conf->socket_conf->router_conf->access_log; 564 565 if (access_log != NULL) { 566 access_log->handler(task, r, access_log); 567 } 568 } 569 570 r->proto.any = NULL; 571 572 if (nxt_fast_path(proto.any != NULL)) { 573 protocol = r->protocol; 574 575 nxt_http_proto[protocol].close(task, proto, conf); 576 577 nxt_mp_release(r->mem_pool); 578 } 579 } 580 581 582 static u_char * 583 nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm, 584 size_t size, const char *format) 585 { 586 return nxt_http_date(buf, tm); 587 } 588