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