xref: /unit/src/nxt_http_request.c (revision 1131:ec7d924d8dfb)
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_pass(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(u_char *buf, nxt_realtime_t *now, struct tm *tm,
21     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,
31     "%s, %02d %s %4d %02d:%02d:%02d GMT",
32     nxt_length("Wed, 31 Dec 1986 16:40:00 GMT"),
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;
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             return NXT_OK;
202         }
203     }
204 
205     return NXT_HTTP_BAD_REQUEST;
206 }
207 
208 
209 nxt_http_request_t *
210 nxt_http_request_create(nxt_task_t *task)
211 {
212     nxt_mp_t            *mp;
213     nxt_buf_t           *last;
214     nxt_http_request_t  *r;
215 
216     mp = nxt_mp_create(1024, 128, 256, 32);
217     if (nxt_slow_path(mp == NULL)) {
218         return NULL;
219     }
220 
221     r = nxt_mp_zget(mp, sizeof(nxt_http_request_t));
222     if (nxt_slow_path(r == NULL)) {
223         goto fail;
224     }
225 
226     r->resp.fields = nxt_list_create(mp, 8, sizeof(nxt_http_field_t));
227     if (nxt_slow_path(r->resp.fields == NULL)) {
228         goto fail;
229     }
230 
231     last = nxt_mp_zget(mp, NXT_BUF_SYNC_SIZE);
232     if (nxt_slow_path(last == NULL)) {
233         goto fail;
234     }
235 
236     nxt_buf_set_sync(last);
237     nxt_buf_set_last(last);
238     last->completion_handler = nxt_http_request_done;
239     last->parent = r;
240     r->last = last;
241 
242     r->mem_pool = mp;
243     r->content_length_n = -1;
244     r->resp.content_length_n = -1;
245     r->state = &nxt_http_request_init_state;
246 
247     return r;
248 
249 fail:
250 
251     nxt_mp_release(mp);
252 
253     return NULL;
254 }
255 
256 
257 static const nxt_http_request_state_t  nxt_http_request_init_state
258     nxt_aligned(64) =
259 {
260     .ready_handler = nxt_http_request_start,
261     .error_handler = nxt_http_request_close_handler,
262 };
263 
264 
265 static void
266 nxt_http_request_start(nxt_task_t *task, void *obj, void *data)
267 {
268     nxt_http_request_t  *r;
269 
270     r = obj;
271 
272     r->state = &nxt_http_request_body_state;
273 
274     nxt_http_request_read_body(task, r);
275 }
276 
277 
278 static const nxt_http_request_state_t  nxt_http_request_body_state
279     nxt_aligned(64) =
280 {
281     .ready_handler = nxt_http_request_pass,
282     .error_handler = nxt_http_request_close_handler,
283 };
284 
285 
286 static void
287 nxt_http_request_pass(nxt_task_t *task, void *obj, void *data)
288 {
289     nxt_http_pass_t     *pass;
290     nxt_http_request_t  *r;
291 
292     r = obj;
293 
294     pass = r->conf->socket_conf->pass;
295 
296     if (nxt_fast_path(pass != NULL)) {
297 
298         do {
299             nxt_debug(task, "http request route: %V", &pass->name);
300 
301             pass = pass->handler(task, r, pass);
302 
303             if (pass == NULL) {
304                 return;
305             }
306 
307             if (pass == NXT_HTTP_PASS_ERROR) {
308                 break;
309             }
310 
311         } while (r->pass_count++ < 255);
312     }
313 
314     nxt_http_request_error(task, r, NXT_HTTP_INTERNAL_SERVER_ERROR);
315 }
316 
317 
318 nxt_http_pass_t *
319 nxt_http_request_application(nxt_task_t *task, nxt_http_request_t *r,
320     nxt_http_pass_t *pass)
321 {
322     nxt_event_engine_t  *engine;
323 
324     nxt_debug(task, "http request application");
325 
326     nxt_mp_retain(r->mem_pool);
327 
328     engine = task->thread->engine;
329     r->timer.task = &engine->task;
330     r->timer.work_queue = &engine->fast_work_queue;
331     r->timer.log = engine->task.log;
332     r->timer.bias = NXT_TIMER_DEFAULT_BIAS;
333 
334     /*
335      * TODO: need an application flag to get local address
336      * required by "SERVER_ADDR" in Pyhton and PHP. Not used in Go.
337      */
338     nxt_http_request_proto_info(task, r);
339 
340     if (r->host.length != 0) {
341         r->server_name = r->host;
342 
343     } else {
344         nxt_str_set(&r->server_name, "localhost");
345     }
346 
347     nxt_router_process_http_request(task, r, pass->u.application);
348 
349     return NULL;
350 }
351 
352 
353 static void
354 nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r)
355 {
356     if (nxt_fast_path(r->proto.any != NULL)) {
357         nxt_http_proto[r->protocol].local_addr(task, r);
358     }
359 }
360 
361 
362 void
363 nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r)
364 {
365     if (nxt_fast_path(r->proto.any != NULL)) {
366         nxt_http_proto[r->protocol].body_read(task, r);
367     }
368 }
369 
370 
371 void
372 nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r)
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);
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;
486     nxt_http_request_t  *r;
487 
488     b = obj;
489     r = data;
490 
491     nxt_mp_free(r->mem_pool, b);
492 
493     nxt_mp_release(r->mem_pool);
494 }
495 
496 
497 nxt_buf_t *
498 nxt_http_buf_last(nxt_http_request_t *r)
499 {
500     nxt_buf_t  *last;
501 
502     last = r->last;
503     r->last = NULL;
504 
505     return last;
506 }
507 
508 
509 static void
510 nxt_http_request_done(nxt_task_t *task, void *obj, void *data)
511 {
512     nxt_http_request_t  *r;
513 
514     r = data;
515 
516     nxt_debug(task, "http request done");
517 
518     nxt_http_request_close_handler(task, r, r->proto.any);
519 }
520 
521 
522 void
523 nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
524 {
525     nxt_http_proto_t    proto;
526     nxt_http_request_t  *r;
527 
528     r = obj;
529     proto.any = data;
530 
531     nxt_debug(task, "http request error handler");
532 
533     r->error = 1;
534 
535     if (nxt_fast_path(proto.any != NULL)) {
536         nxt_http_proto[r->protocol].discard(task, r, nxt_http_buf_last(r));
537     }
538 }
539 
540 
541 void
542 nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
543 {
544     nxt_http_proto_t         proto;
545     nxt_http_request_t       *r;
546     nxt_http_protocol_t      protocol;
547     nxt_socket_conf_joint_t  *conf;
548     nxt_router_access_log_t  *access_log;
549 
550     r = obj;
551     proto.any = data;
552 
553     nxt_debug(task, "http request close handler");
554 
555     conf = r->conf;
556 
557     if (!r->logged) {
558         r->logged = 1;
559 
560         access_log = conf->socket_conf->router_conf->access_log;
561 
562         if (access_log != NULL) {
563             access_log->handler(task, r, access_log);
564         }
565     }
566 
567     r->proto.any = NULL;
568 
569     if (nxt_fast_path(proto.any != NULL)) {
570         protocol = r->protocol;
571 
572         nxt_mp_release(r->mem_pool);
573 
574         nxt_http_proto[protocol].close(task, proto, conf);
575     }
576 }
577 
578 
579 static u_char *
580 nxt_http_date(u_char *buf, nxt_realtime_t *now, struct tm *tm, size_t size,
581     const char *format)
582 {
583     static const char  *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri",
584                                    "Sat" };
585 
586     static const char  *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
587                                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
588 
589     return nxt_sprintf(buf, buf + size, format,
590                        week[tm->tm_wday], tm->tm_mday,
591                        month[tm->tm_mon], tm->tm_year + 1900,
592                        tm->tm_hour, tm->tm_min, tm->tm_sec);
593 }
594