forked from kaltura/nginx-vod-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_child_http_request.c
936 lines (772 loc) · 24.1 KB
/
ngx_child_http_request.c
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
#include <ngx_event.h>
#include "ngx_child_http_request.h"
#include "ngx_http_vod_module.h"
// macros
#define is_dump_request(r) (r == r->main) // regular requests use subrequests
// typedefs
typedef struct {
ngx_buf_t* request_buffer;
ngx_http_status_t status;
} ngx_child_request_base_context_t; // fields common to dump requests and child requests
typedef struct {
ngx_child_request_base_context_t base; // must be first
ngx_child_request_callback_t callback;
void* callback_context;
u_char* headers_buffer;
size_t headers_buffer_size;
off_t read_body_size;
u_char* response_buffer;
off_t response_buffer_size;
ngx_http_upstream_conf_t* upstream_conf;
} ngx_child_request_context_t;
// globals
static ngx_str_t child_http_hide_headers[] = {
ngx_string("Date"),
ngx_string("Server"),
ngx_null_string
};
// constants
static const char content_length_header[] = "content-length";
static ngx_int_t
ngx_http_vod_create_request(ngx_http_request_t *r)
{
ngx_chain_t *cl;
ngx_child_request_base_context_t* ctx;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_vod_create_request: started");
// allocate a chain and associate the previously created request buffer
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_create_request: ngx_alloc_chain_link failed");
return NGX_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
cl->buf = ctx->request_buffer;
cl->next = NULL;
r->upstream->request_bufs = cl;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_vod_create_request: done %s", ctx->request_buffer->pos);
return NGX_OK;
}
static void
ngx_http_vod_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_vod_finalize_request: called rc=%i", rc);
}
static ngx_int_t
ngx_http_vod_reinit_request(ngx_http_request_t *r)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_vod_reinit_request: called");
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_process_header(ngx_http_request_t *r)
{
ngx_child_request_context_t* ctx;
ngx_http_upstream_t *u;
ngx_table_elt_t *h;
ngx_int_t rc;
u = r->upstream;
// Note: ctx must not be used in case of a dump request
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
for (;;)
{
rc = ngx_http_parse_header_line(r, &u->buffer, 1);
if (rc == NGX_OK) // a header line has been parsed successfully
{
if (is_dump_request(r))
{
h = ngx_list_push(&r->upstream->headers_in.headers);
if (h == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_process_header: ngx_list_push failed");
return NGX_ERROR;
}
h->hash = r->header_hash;
h->key.len = r->header_name_end - r->header_name_start;
h->value.len = r->header_end - r->header_start;
h->key.data = ngx_pnalloc(r->pool, h->key.len + 1 + h->value.len + 1 + h->key.len);
if (h->key.data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_process_header: ngx_pnalloc failed (1)");
return NGX_ERROR;
}
h->value.data = h->key.data + h->key.len + 1;
h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
h->key.data[h->key.len] = '\0';
ngx_memcpy(h->value.data, r->header_start, h->value.len);
h->value.data[h->value.len] = '\0';
if (h->key.len == r->lowcase_index)
{
ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
}
else
{
ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
}
}
// parse the content length
if (r->header_name_end - r->header_name_start == sizeof(content_length_header) - 1 &&
ngx_memcmp(r->lowcase_header, content_length_header, sizeof(content_length_header) - 1) == 0)
{
u->headers_in.content_length_n = ngx_atoof(r->header_start, r->header_end - r->header_start);
if (u->headers_in.content_length_n < 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_process_header: failed to parse content length");
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
}
// Note: not validating the content length in case of dump, since we don't load the whole response into memory
if (!is_dump_request(r) && u->headers_in.content_length_n > ctx->response_buffer_size)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_process_header: content length %O exceeds the limit %O", u->headers_in.content_length_n, ctx->response_buffer_size);
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_process_header: parsed content length %O", u->headers_in.content_length_n);
}
continue;
}
if (rc == NGX_HTTP_PARSE_HEADER_DONE) // finished reading all headers
{
// in case of ngx_dump_request, no need to do anything
if (is_dump_request(r))
{
return NGX_OK;
}
if (u->state && u->state->status != NGX_HTTP_OK && u->state->status != NGX_HTTP_PARTIAL_CONTENT)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_process_header: upstream returned a bad status %ui", u->state->status);
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
}
// make sure we got some content length
if (u->headers_in.content_length_n < 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_process_header: got no content-length header");
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
}
// allocate a response buffer if needed
if (ctx->response_buffer == NULL)
{
ctx->response_buffer_size = u->headers_in.content_length_n;
ctx->response_buffer = ngx_palloc(r->pool, ctx->response_buffer_size + 1);
if (ctx->response_buffer == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_process_header: ngx_pnalloc failed (2)");
return NGX_ERROR;
}
}
// in case we already got some of the response body, copy it to the response buffer
ctx->read_body_size = u->buffer.last - u->buffer.pos;
if (ctx->read_body_size > ctx->response_buffer_size)
{
ctx->read_body_size = ctx->response_buffer_size;
}
ngx_memcpy(ctx->response_buffer, u->buffer.pos, ctx->read_body_size);
// set the upstream buffer to our response buffer
u->buffer.start = ctx->response_buffer;
u->buffer.pos = u->buffer.start;
u->buffer.last = u->buffer.start + ctx->read_body_size;
u->buffer.end = u->buffer.start + ctx->response_buffer_size + 1;
u->buffer.temporary = 1;
return NGX_OK;
}
if (rc == NGX_AGAIN) // need more data
{
return NGX_AGAIN;
}
// there was error while a header line parsing
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_process_header: failed to parse header line rc=%i", rc);
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
}
}
static ngx_int_t
ngx_http_vod_process_status_line(ngx_http_request_t *r)
{
ngx_child_request_base_context_t* ctx;
ngx_http_upstream_t *u;
size_t len;
ngx_int_t rc;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_vod_process_status_line: started");
u = r->upstream;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
rc = ngx_http_parse_status_line(r, &u->buffer, &ctx->status);
if (rc == NGX_AGAIN)
{
return rc;
}
if (rc == NGX_ERROR)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_process_status_line: failed to parse status line");
return NGX_ERROR;
}
if (u->state && u->state->status == 0)
{
u->state->status = ctx->status.code;
}
u->headers_in.status_n = ctx->status.code;
len = ctx->status.end - ctx->status.start;
u->headers_in.status_line.len = len;
u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
if (u->headers_in.status_line.data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_http_vod_process_status_line: ngx_pnalloc failed");
return NGX_ERROR;
}
ngx_memcpy(u->headers_in.status_line.data, ctx->status.start, len);
if (ctx->status.http_version < NGX_HTTP_VERSION_11)
{
u->headers_in.connection_close = 1;
}
// change state to processing the rest of the headers
u->process_header = ngx_http_vod_process_header;
return ngx_http_vod_process_header(r);
}
static void
ngx_http_vod_abort_request(ngx_http_request_t *r)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ngx_http_vod_abort_request: called");
return;
}
static ngx_int_t
ngx_http_vod_filter_init(void *data)
{
ngx_child_request_context_t* ctx;
ngx_http_request_t *r = data;
ngx_http_upstream_t *u;
u = r->upstream;
if (u->headers_in.content_length_n + 1 > u->buffer.end - u->buffer.start)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_http_vod_filter_init: content length %O exceeds buffer size %O", u->headers_in.content_length_n, (off_t)(u->buffer.end - u->buffer.start));
return NGX_ERROR;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
u->buffer.last = u->buffer.start + ctx->read_body_size;
u->length = u->headers_in.content_length_n;
return NGX_OK;
}
static ngx_int_t
ngx_http_vod_filter(void *data, ssize_t bytes)
{
ngx_http_request_t *r = data;
ngx_http_upstream_t *u;
ngx_buf_t *b;
u = r->upstream;
b = &u->buffer;
// only need to update the length and buffer position
b->last += bytes;
u->length -= bytes;
return NGX_OK;
}
static ngx_int_t
ngx_create_upstream(
ngx_http_request_t *r,
ngx_http_upstream_conf_t* conf)
{
ngx_http_upstream_t *u;
ngx_int_t rc;
// create the upstream
rc = ngx_http_upstream_create(r);
if (rc != NGX_OK)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_create_upstream: failed to create upstream rc=%i", rc);
return NGX_ERROR;
}
// initialize the upstream
u = r->upstream;
ngx_str_set(&u->schema, "kalapi://");
u->output.tag = (ngx_buf_tag_t)&ngx_http_vod_module;
u->buffer.tag = u->output.tag;
u->conf = conf;
u->create_request = ngx_http_vod_create_request;
u->reinit_request = ngx_http_vod_reinit_request;
u->process_header = ngx_http_vod_process_status_line;
u->abort_request = ngx_http_vod_abort_request;
u->finalize_request = ngx_http_vod_finalize_request;
return NGX_OK;
}
static ngx_flag_t
ngx_should_proxy_header(ngx_table_elt_t* header, ngx_child_request_params_t* params)
{
if (header->key.len == sizeof("host") - 1 &&
ngx_memcmp(header->lowcase_key, "host", sizeof("host") - 1) == 0)
{
return 0;
}
if (!params->proxy_range &&
header->key.len == sizeof("range") - 1 &&
ngx_memcmp(header->lowcase_key, "range", sizeof("range") - 1) == 0)
{
return 0;
}
if (!params->proxy_accept_encoding &&
header->key.len == sizeof("accept-encoding") - 1 &&
ngx_memcmp(header->lowcase_key, "accept-encoding", sizeof("accept-encoding") - 1) == 0)
{
return 0;
}
return 1;
}
static ngx_buf_t*
ngx_init_request_buffer(
ngx_http_request_t *r,
ngx_buf_t* request_buffer,
ngx_child_request_params_t* params)
{
ngx_http_core_srv_conf_t *cscf;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_uint_t i;
ngx_flag_t range_request = params->range_start < params->range_end;
ngx_buf_t *b;
uintptr_t escape_chars;
size_t len;
u_char* p;
// if the host name is empty, use by default the host name of the incoming request
if (params->host_name.len == 0)
{
if (r->headers_in.host != NULL)
{
params->host_name = r->headers_in.host->value;
}
else
{
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
params->host_name = cscf->server_name;
}
}
// calculate the request size
if (params->escape_uri)
{
escape_chars = ngx_escape_uri(NULL, params->base_uri.data, params->base_uri.len, NGX_ESCAPE_URI);
}
else
{
escape_chars = 0;
}
len =
sizeof("HEAD ") - 1 + params->base_uri.len + 2 * escape_chars + sizeof("?") - 1 + params->extra_args.len + sizeof(" HTTP/1.1" CRLF) - 1 +
sizeof("Host: ") - 1 + params->host_name.len + sizeof(CRLF) - 1 +
params->extra_headers.len +
sizeof(CRLF);
if (range_request)
{
len += sizeof("Range: bytes=") - 1 + NGX_INT64_LEN + sizeof("-") - 1 + NGX_INT64_LEN + sizeof(CRLF) - 1;
}
// add all input headers except host and possibly range
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
if (!ngx_should_proxy_header(&header[i], params))
{
continue;
}
len += header[i].key.len + sizeof(": ") - 1
+ header[i].value.len + sizeof(CRLF) - 1;
}
// get/allocate the request buffer
if (request_buffer != NULL && len <= (size_t)(request_buffer->end - request_buffer->start))
{
b = request_buffer;
b->pos = b->start;
p = b->start;
}
else
{
b = ngx_create_temp_buf(r->pool, len);
if (b == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_init_request_buffer: ngx_create_temp_buf failed");
return NULL;
}
p = b->last;
}
// first header line
if (params->method == NGX_HTTP_HEAD)
{
*p++ = 'H'; *p++ = 'E'; *p++ = 'A'; *p++ = 'D';
}
else
{
*p++ = 'G'; *p++ = 'E'; *p++ = 'T';
}
*p++ = ' ';
if (escape_chars > 0)
{
p = (u_char *)ngx_escape_uri(p, params->base_uri.data, params->base_uri.len, NGX_ESCAPE_URI);
}
else
{
p = ngx_copy(p, params->base_uri.data, params->base_uri.len);
}
if (params->extra_args.len > 0)
{
if (ngx_strchr(params->base_uri.data, '?'))
{
*p++ = '&';
}
else
{
*p++ = '?';
}
p = ngx_copy(p, params->extra_args.data, params->extra_args.len);
}
p = ngx_copy(p, " HTTP/1.1" CRLF, sizeof(" HTTP/1.1" CRLF) - 1);
// host line
p = ngx_copy(p, "Host: ", sizeof("Host: ") - 1);
p = ngx_copy(p, params->host_name.data, params->host_name.len);
*p++ = CR; *p++ = LF;
// range request
if (range_request)
{
p = ngx_sprintf(p, "Range: bytes=%O-%O" CRLF, params->range_start, params->range_end - 1);
}
// additional headers
p = ngx_copy(p, params->extra_headers.data, params->extra_headers.len);
// input headers
part = &r->headers_in.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
if (!ngx_should_proxy_header(&header[i], params))
{
continue;
}
p = ngx_copy(p, header[i].key.data, header[i].key.len);
*p++ = ':'; *p++ = ' ';
p = ngx_copy(p, header[i].value.data, header[i].value.len);
*p++ = CR; *p++ = LF;
}
// headers end
*p++ = CR; *p++ = LF;
*p = '\0';
b->last = p;
return b;
}
ngx_int_t
ngx_dump_request(
ngx_http_request_t *r,
ngx_http_upstream_conf_t* upstream_conf,
ngx_child_request_params_t* params)
{
ngx_child_request_base_context_t* ctx;
ngx_int_t rc;
// replace the module context - after calling ngx_dump_request the caller is not allowed to use the context
ctx = ngx_pcalloc(r->pool, sizeof(*ctx));
if (ctx == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_dump_request: ngx_pcalloc failed");
return NGX_ERROR;
}
ngx_http_set_ctx(r, ctx, ngx_http_vod_module);
// create an upstream
rc = ngx_create_upstream(r, upstream_conf);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_dump_request: ngx_create_upstream failed %i", rc);
return rc;
}
// build the request
ctx->request_buffer = ngx_init_request_buffer(r, NULL, params);
if (ctx->request_buffer == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_dump_request: ngx_init_request_buffer failed");
return NGX_ERROR;
}
// start the request
ngx_http_upstream_init(r);
return NGX_AGAIN;
}
ngx_int_t
ngx_child_request_internal_handler(ngx_http_request_t *r)
{
ngx_child_request_context_t* ctx;
ngx_http_upstream_t *u;
ngx_int_t rc;
// create the upstream
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
rc = ngx_create_upstream(r, ctx->upstream_conf);
if (rc != NGX_OK)
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_internal_handler: ngx_create_upstream failed %i", rc);
return rc;
}
u = r->upstream;
// initialize the upstream buffer
u->buffer.start = ctx->headers_buffer;
u->buffer.pos = u->buffer.start;
u->buffer.last = u->buffer.start;
u->buffer.end = u->buffer.start + ctx->headers_buffer_size;
u->buffer.temporary = 1;
// create the headers list
if (ngx_list_init(&u->headers_in.headers, r->pool, 8, sizeof(ngx_table_elt_t)) != NGX_OK)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_internal_handler: ngx_list_init failed");
return NGX_ERROR;
}
// initialize the input filter
u->input_filter_init = ngx_http_vod_filter_init;
u->input_filter = ngx_http_vod_filter;
u->input_filter_ctx = r;
#if defined nginx_version && nginx_version >= 8011
r->main->count++;
#endif
// start the request
ngx_http_upstream_init(r);
return NGX_DONE;
}
static ngx_int_t
ngx_child_request_finished_handler(ngx_http_request_t *r, void *data, ngx_int_t rc)
{
ngx_child_request_context_t* ctx;
ngx_http_upstream_t *u;
ctx = ngx_http_get_module_ctx(r, ngx_http_vod_module);
if (ctx->callback == NULL)
{
return NGX_OK; // already called the callback
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_finished_handler: called rc=%i, r=%p", rc, r);
u = r->upstream;
if (rc == NGX_OK)
{
if (u->length != 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_child_request_finished_handler: upstream connection was closed with %O bytes left to read", u->length);
rc = NGX_HTTP_BAD_GATEWAY;
}
}
ctx->callback(ctx->callback_context, rc, &u->buffer);
ctx->callback = NULL;
return NGX_OK;
}
// Note: must not return positive error codes (like NGX_HTTP_INTERNAL_SERVER_ERROR)
ngx_int_t
ngx_child_request_start(
ngx_http_request_t *r,
ngx_child_request_buffers_t* buffers,
ngx_child_request_callback_t callback,
void* callback_context,
ngx_http_upstream_conf_t* upstream_conf,
ngx_str_t* internal_location,
ngx_child_request_params_t* params,
off_t max_response_length,
u_char* response_buffer)
{
ngx_child_request_context_t* child_ctx;
ngx_http_post_subrequest_t *psr;
ngx_http_request_t *sr;
ngx_str_t args = ngx_null_string;
ngx_str_t uri;
ngx_int_t rc;
u_char* p;
// initialize the headers buffer
if (buffers->headers_buffer == NULL || buffers->headers_buffer_size < upstream_conf->buffer_size)
{
if (buffers->headers_buffer != NULL)
{
ngx_pfree(r->pool, buffers->headers_buffer);
}
buffers->headers_buffer_size = upstream_conf->buffer_size;
buffers->headers_buffer = ngx_palloc(r->pool, buffers->headers_buffer_size);
if (buffers->headers_buffer == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_start: ngx_palloc failed (1)");
return NGX_ERROR;
}
}
// initialize the request buffer
buffers->request_buffer = ngx_init_request_buffer(r, buffers->request_buffer, params);
if (buffers->request_buffer == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_start: ngx_init_request_buffer failed");
return NGX_ERROR;
}
// create the child context
child_ctx = ngx_pcalloc(r->pool, sizeof(*child_ctx));
if (child_ctx == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_start: ngx_pcalloc failed");
return NGX_ERROR;
}
child_ctx->callback = callback;
child_ctx->callback_context = callback_context;
child_ctx->response_buffer_size = max_response_length;
child_ctx->response_buffer = response_buffer;
child_ctx->upstream_conf = upstream_conf;
child_ctx->headers_buffer_size = buffers->headers_buffer_size;
child_ctx->headers_buffer = buffers->headers_buffer;
child_ctx->base.request_buffer = buffers->request_buffer;
// build the subrequest uri
// Note: this uri is not important, we could have just used internal_location as is
// but adding the child request uri makes the logs more readable
uri.data = ngx_palloc(r->pool, internal_location->len + params->base_uri.len + 1);
if (uri.data == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_start: ngx_palloc failed (2)");
return NGX_ERROR;
}
p = ngx_copy(uri.data, internal_location->data, internal_location->len);
p = ngx_copy(p, params->base_uri.data, params->base_uri.len);
*p = '\0';
uri.len = p - uri.data;
// create the subrequest
psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
if (psr == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_start: ngx_palloc failed (3)");
return NGX_ERROR;
}
psr->handler = ngx_child_request_finished_handler;
psr->data = r;
rc = ngx_http_subrequest(r, &uri, &args, &sr, psr, NGX_HTTP_SUBREQUEST_WAITED | NGX_HTTP_SUBREQUEST_IN_MEMORY);
if (rc == NGX_ERROR)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_child_request_start: ngx_http_subrequest failed %i", rc);
return rc;
}
// set the context of the subrequest
ngx_http_set_ctx(sr, child_ctx, ngx_http_vod_module);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"ngx_child_request_start: completed successfully sr=%p", sr);
return NGX_AGAIN;
}
void
ngx_child_request_free_buffers(ngx_pool_t* pool, ngx_child_request_buffers_t* buffers)
{
ngx_pfree(pool, buffers->headers_buffer);
buffers->headers_buffer = NULL;
ngx_pfree(pool, buffers->request_buffer);
buffers->request_buffer = NULL;
}
void
ngx_init_upstream_conf(ngx_http_upstream_conf_t* upstream)
{
upstream->connect_timeout = NGX_CONF_UNSET_MSEC;
upstream->send_timeout = NGX_CONF_UNSET_MSEC;
upstream->read_timeout = NGX_CONF_UNSET_MSEC;
upstream->buffer_size = NGX_CONF_UNSET_SIZE;
upstream->hide_headers = NGX_CONF_UNSET_PTR;
upstream->pass_headers = NGX_CONF_UNSET_PTR;
// hardcoded values
upstream->cyclic_temp_file = 0;
upstream->buffering = 0;
upstream->ignore_client_abort = 0;
upstream->send_lowat = 0;
upstream->bufs.num = 0;
upstream->busy_buffers_size = 0;
upstream->max_temp_file_size = 0;
upstream->temp_file_write_size = 0;
upstream->intercept_errors = 1;
upstream->intercept_404 = 1;
upstream->pass_request_headers = 0;
upstream->pass_request_body = 0;
}
char *
ngx_merge_upstream_conf(
ngx_conf_t *cf,
ngx_http_upstream_conf_t* conf_upstream,
ngx_http_upstream_conf_t* prev_upstream)
{
ngx_hash_init_t hash;
ngx_conf_merge_msec_value(conf_upstream->connect_timeout,
prev_upstream->connect_timeout, 60000);
ngx_conf_merge_msec_value(conf_upstream->send_timeout,
prev_upstream->send_timeout, 60000);
ngx_conf_merge_msec_value(conf_upstream->read_timeout,
prev_upstream->read_timeout, 60000);
ngx_conf_merge_size_value(conf_upstream->buffer_size,
prev_upstream->buffer_size,
(size_t)ngx_pagesize);
ngx_conf_merge_bitmask_value(conf_upstream->next_upstream,
prev_upstream->next_upstream,
(NGX_CONF_BITMASK_SET
| NGX_HTTP_UPSTREAM_FT_ERROR
| NGX_HTTP_UPSTREAM_FT_TIMEOUT));
if (conf_upstream->next_upstream & NGX_HTTP_UPSTREAM_FT_OFF)
{
conf_upstream->next_upstream = NGX_CONF_BITMASK_SET | NGX_HTTP_UPSTREAM_FT_OFF;
}
if (conf_upstream->upstream == NULL)
{
conf_upstream->upstream = prev_upstream->upstream;
}
hash.max_size = 512;
hash.bucket_size = 64;
hash.name = "child_request_headers_hash";
if (ngx_http_upstream_hide_headers_hash(cf, conf_upstream,
prev_upstream, child_http_hide_headers, &hash)
!= NGX_OK)
{
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
char *
ngx_http_upstream_command(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_upstream_conf_t *upstream_conf = (ngx_http_upstream_conf_t*)((u_char*)conf + cmd->offset);
ngx_str_t *value;
ngx_url_t u;
if (upstream_conf->upstream)
{
return "is duplicate";
}
value = cf->args->elts;
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
u.no_resolve = 1;
u.default_port = 80;
upstream_conf->upstream = ngx_http_upstream_add(cf, &u, 0);
if (upstream_conf->upstream == NULL)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
"ngx_http_upstream_command: ngx_http_upstream_add failed");
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}