xref: /unit/src/nxt_http_request.c (revision 1473:e07d5b451423)
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)
40 {
41     nxt_int_t  ret;
42 
43     ret = nxt_h1p_init(task);
44 
45     if (ret != NXT_OK) {
46         return ret;
47     }
48 
49     return nxt_http_response_hash_init(task);
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     r->app_target = action->target;
345 
346     nxt_router_process_http_request(task, r, action->u.application);
347 
348     return NULL;
349 }
350 
351 
352 static void
353 nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r)
354 {
355     if (nxt_fast_path(r->proto.any != NULL)) {
356         nxt_http_proto[r->protocol].local_addr(task, r);
357     }
358 }
359 
360 
361 void
362 nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r)
363 {
364     if (nxt_fast_path(r->proto.any != NULL)) {
365         nxt_http_proto[r->protocol].body_read(task, r);
366     }
367 }
368 
369 
370 void
371 nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
372     nxt_work_handler_t body_handler, void *data)
373 {
374     u_char            *p, *end;
375     nxt_http_field_t  *server, *date, *content_length;
376 
377     /*
378      * TODO: "Server", "Date", and "Content-Length" processing should be moved
379      * to the last header filter.
380      */
381 
382     server = nxt_list_zero_add(r->resp.fields);
383     if (nxt_slow_path(server == NULL)) {
384         goto fail;
385     }
386 
387     nxt_http_field_set(server, "Server", NXT_SERVER);
388 
389     if (r->resp.date == NULL) {
390         date = nxt_list_zero_add(r->resp.fields);
391         if (nxt_slow_path(date == NULL)) {
392             goto fail;
393         }
394 
395         nxt_http_field_name_set(date, "Date");
396 
397         p = nxt_mp_nget(r->mem_pool, nxt_http_date_cache.size);
398         if (nxt_slow_path(p == NULL)) {
399             goto fail;
400         }
401 
402         (void) nxt_thread_time_string(task->thread, &nxt_http_date_cache, p);
403 
404         date->value = p;
405         date->value_length = nxt_http_date_cache.size;
406 
407         r->resp.date = date;
408     }
409 
410     if (r->resp.content_length_n != -1
411         && (r->resp.content_length == NULL || r->resp.content_length->skip))
412     {
413         content_length = nxt_list_zero_add(r->resp.fields);
414         if (nxt_slow_path(content_length == NULL)) {
415             goto fail;
416         }
417 
418         nxt_http_field_name_set(content_length, "Content-Length");
419 
420         p = nxt_mp_nget(r->mem_pool, NXT_OFF_T_LEN);
421         if (nxt_slow_path(p == NULL)) {
422             goto fail;
423         }
424 
425         content_length->value = p;
426         end = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%O", r->resp.content_length_n);
427         content_length->value_length = end - p;
428 
429         r->resp.content_length = content_length;
430     }
431 
432     if (nxt_fast_path(r->proto.any != NULL)) {
433         nxt_http_proto[r->protocol].header_send(task, r, body_handler, data);
434     }
435 
436     return;
437 
438 fail:
439 
440     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
441 }
442 
443 
444 void
445 nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r,
446     nxt_buf_t *ws_frame)
447 {
448     if (r->proto.any != NULL) {
449         nxt_http_proto[r->protocol].ws_frame_start(task, r, ws_frame);
450     }
451 }
452 
453 
454 void
455 nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out)
456 {
457     if (nxt_fast_path(r->proto.any != NULL)) {
458         nxt_http_proto[r->protocol].send(task, r, out);
459     }
460 }
461 
462 
463 nxt_buf_t *
464 nxt_http_buf_mem(nxt_task_t *task, nxt_http_request_t *r, size_t size)
465 {
466     nxt_buf_t  *b;
467 
468     b = nxt_buf_mem_alloc(r->mem_pool, size, 0);
469     if (nxt_fast_path(b != NULL)) {
470         b->completion_handler = nxt_http_request_mem_buf_completion;
471         b->parent = r;
472         nxt_mp_retain(r->mem_pool);
473 
474     } else {
475         nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
476     }
477 
478     return b;
479 }
480 
481 
482 static void
483 nxt_http_request_mem_buf_completion(nxt_task_t *task, void *obj, void *data)
484 {
485     nxt_buf_t           *b, *next;
486     nxt_http_request_t  *r;
487 
488     b = obj;
489     r = data;
490 
491     do {
492         next = b->next;
493 
494         nxt_mp_free(r->mem_pool, b);
495         nxt_mp_release(r->mem_pool);
496 
497         b = next;
498     } while (b != NULL);
499 }
500 
501 
502 nxt_buf_t *
503 nxt_http_buf_last(nxt_http_request_t *r)
504 {
505     nxt_buf_t  *last;
506 
507     last = r->last;
508     r->last = NULL;
509 
510     return last;
511 }
512 
513 
514 static void
515 nxt_http_request_done(nxt_task_t *task, void *obj, void *data)
516 {
517     nxt_http_request_t  *r;
518 
519     r = data;
520 
521     nxt_debug(task, "http request done");
522 
523     nxt_http_request_close_handler(task, r, r->proto.any);
524 }
525 
526 
527 void
528 nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
529 {
530     nxt_http_proto_t    proto;
531     nxt_http_request_t  *r;
532 
533     r = obj;
534     proto.any = data;
535 
536     nxt_debug(task, "http request error handler");
537 
538     r->error = 1;
539 
540     if (nxt_fast_path(proto.any != NULL)) {
541         nxt_http_proto[r->protocol].discard(task, r, nxt_http_buf_last(r));
542     }
543 }
544 
545 
546 void
547 nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
548 {
549     nxt_http_proto_t         proto;
550     nxt_http_request_t       *r;
551     nxt_http_protocol_t      protocol;
552     nxt_socket_conf_joint_t  *conf;
553     nxt_router_access_log_t  *access_log;
554 
555     r = obj;
556     proto.any = data;
557 
558     nxt_debug(task, "http request close handler");
559 
560     conf = r->conf;
561 
562     if (!r->logged) {
563         r->logged = 1;
564 
565         access_log = conf->socket_conf->router_conf->access_log;
566 
567         if (access_log != NULL) {
568             access_log->handler(task, r, access_log);
569         }
570     }
571 
572     r->proto.any = NULL;
573 
574     if (r->body != NULL && nxt_buf_is_file(r->body)
575         && r->body->file->fd != -1)
576     {
577         nxt_fd_close(r->body->file->fd);
578 
579         r->body->file->fd = -1;
580     }
581 
582     if (nxt_fast_path(proto.any != NULL)) {
583         protocol = r->protocol;
584 
585         nxt_http_proto[protocol].close(task, proto, conf);
586 
587         nxt_mp_release(r->mem_pool);
588     }
589 }
590 
591 
592 static u_char *
593 nxt_http_date_cache_handler(u_char *buf, nxt_realtime_t *now, struct tm *tm,
594     size_t size, const char *format)
595 {
596     return nxt_http_date(buf, tm);
597 }
598