-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngx_http_nphase_module.c
1177 lines (933 loc) · 35.1 KB
/
ngx_http_nphase_module.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
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Simon Lee@Huawei Tech.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
off_t start;
off_t end;
off_t length;
ngx_int_t flag; /* -1: no range; 0: xxx-xxx ; 1: xxx-; 2: -xxx; */
ngx_str_t range;
} ngx_http_nphase_range_t;
typedef struct {
ngx_str_t uri;
ngx_int_t uri_var_index;
ngx_int_t range_var_index;
} ngx_http_nphase_conf_t;
typedef struct {
ngx_uint_t pr_status;
ngx_uint_t sr_count;
ngx_uint_t sr_count_e;
ngx_str_t uri_var_value;
off_t wfsz;
ngx_array_t range_in;
ngx_http_nphase_range_t range_sent;
ngx_str_t loc_body_c;
unsigned sr_done:1;
unsigned sr_error:1;
unsigned header_sent:1;
unsigned body_ready:1;
unsigned loc_ready:1;
unsigned loc_body:1;
} ngx_http_nphase_ctx_t;
typedef struct {
ngx_http_nphase_range_t range_sent;
} ngx_http_nphase_sub_ctx_t;
typedef struct {
ngx_int_t index;
ngx_http_complex_value_t value;
ngx_http_set_variable_pt set_handler;
} ngx_http_nphase_variable_t;
#define NGX_HTTP_NPHASE_MAX_RETRY 3
static void * ngx_http_nphase_create_conf(ngx_conf_t *cf);
static char * ngx_http_nphase_merge_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_nphase_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_nphase_access_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_nphase_content_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_nphase_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc);
ngx_int_t ngx_http_nphase_filter_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_nphase_header_filter(ngx_http_request_t *r);
static ngx_int_t ngx_http_nphase_body_filter(ngx_http_request_t *r, ngx_chain_t *in);
void ngx_http_nphase_discard_bufs(ngx_pool_t *pool, ngx_chain_t *in);
static char *ngx_http_nphase_uri(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_nphase_set_uri_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_nphase_set_range_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
ngx_int_t ngx_http_nphase_range_update(ngx_http_request_t *r, ngx_int_t index, off_t start, off_t end, ngx_int_t flag);
ngx_int_t ngx_http_nphase_range_parse(ngx_http_request_t *r, ngx_http_nphase_ctx_t *ctx);
ngx_int_t ngx_http_nphase_content_range_parse(u_char *p, ngx_http_nphase_range_t *range);
ngx_int_t ngx_http_nphase_copy_header_value(ngx_list_t *headers, ngx_str_t *k, ngx_str_t *v);
ngx_int_t ngx_http_nphase_run_subrequest(ngx_http_request_t *r, ngx_http_nphase_ctx_t *ctx,
ngx_str_t *uri, ngx_str_t *args);
ngx_int_t ngx_http_nphase_process_header(ngx_http_request_t *r, ngx_http_nphase_ctx_t *ctx);
static ngx_int_t ngx_http_nphase_add_range_singlepart_header(ngx_http_request_t *r, ngx_http_nphase_ctx_t *ctx);
static ngx_command_t ngx_http_nphase_commands[] = {
{ ngx_string("nphase_uri"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_nphase_uri,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("nphase_set_uri_var"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_nphase_set_uri_var,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("nphase_set_range_var"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_nphase_set_range_var,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_nphase_module_ctx = {
NULL, /* preconfiguration */
ngx_http_nphase_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_nphase_create_conf, /* create location configuration */
ngx_http_nphase_merge_conf /* merge location configuration */
};
ngx_module_t ngx_http_nphase_module = {
NGX_MODULE_V1,
&ngx_http_nphase_module_ctx, /* module context */
ngx_http_nphase_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static void *
ngx_http_nphase_create_conf(ngx_conf_t *cf)
{
ngx_http_nphase_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_nphase_conf_t));
if (conf == NULL) {
return NULL;
}
conf->uri_var_index = NGX_CONF_UNSET_UINT;
conf->range_var_index = NGX_CONF_UNSET_UINT;
return conf;
}
static char *
ngx_http_nphase_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_nphase_conf_t *prev = parent;
ngx_http_nphase_conf_t *conf = child;
ngx_conf_merge_str_value(conf->uri, prev->uri, "");
ngx_conf_merge_value(conf->uri_var_index, prev->uri_var_index, -1);
ngx_conf_merge_value(conf->range_var_index, prev->range_var_index, -1);
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_nphase_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
if (cmcf == NULL) {
return NGX_ERROR;
}
h = ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_nphase_access_handler;
ngx_http_nphase_filter_init(cf);
return NGX_OK;
}
static ngx_int_t
ngx_http_nphase_access_handler(ngx_http_request_t *r)
{
ngx_http_nphase_ctx_t *ctx;
ngx_http_nphase_conf_t *npcf;
ngx_http_variable_value_t *var;
ngx_int_t rc;
ngx_http_nphase_range_t *rin;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase access handler");
npcf = ngx_http_get_module_loc_conf(r, ngx_http_nphase_module);
if (npcf->uri.len == 0) {
return NGX_DECLINED;
}
ctx = ngx_http_get_module_ctx(r, ngx_http_nphase_module);
if (ctx != NULL) {
if (ctx->sr_count_e >= NGX_HTTP_NPHASE_MAX_RETRY) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"nphase subrequest max retry num(%d) reached",
NGX_HTTP_NPHASE_MAX_RETRY);
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* phase 2 process */
if (ctx->body_ready == 1) {
if (ctx->sr_done == 0) {
return NGX_AGAIN;
}
if (ctx->range_in.nelts > 1) {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
rin = ctx->range_in.elts;
ngx_log_debug8(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase rin s:%O e:%O f:%d, sent s:%O e:%O, w:%O, c:%d, ce:%d",
rin->start, rin->end, rin->flag,
ctx->range_sent.start, ctx->range_sent.end,
ctx->wfsz, ctx->sr_count, ctx->sr_count_e);
/* todo: compare ctx->wfsz and range_sent to find out range need send */
if ((rin->flag == -1 && ctx->range_sent.end < ctx->wfsz)
|| (ctx->range_sent.end < rin->end - rin->start + 1))
{
ctx->loc_ready = 0;
ctx->body_ready = 0;
if (ngx_http_nphase_range_update(r, npcf->range_var_index,
rin->start + ctx->range_sent.end,
rin->end ? rin->end : ctx->wfsz, 0)
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* restore next phase subrequest uri to phase 1 uri */
var = ngx_http_get_indexed_variable(r, npcf->uri_var_index);
if (var == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
var->data = ctx->uri_var_value.data;
var->len = ctx->uri_var_value.len;
if (ngx_http_nphase_run_subrequest(r, ctx, &npcf->uri, NULL)
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
return NGX_AGAIN;
}
return NGX_OK;
}
/* phase 1 process */
if (ctx->loc_ready == 1) {
ctx->loc_ready = 0;
/* run subrequest by loc_body_c */
if (ctx->loc_body_c.len == 0) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ctx->loc_body_c.data[0] == '/') {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
var = ngx_http_get_indexed_variable(r, npcf->uri_var_index);
if (var == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
var->len = ctx->loc_body_c.len;
var->data = ctx->loc_body_c.data;
rin = ctx->range_in.elts;
/*
if (ngx_http_nphase_range_update(r, npcf->range_var_index,
rin->start, rin->end, rin->flag)
!= NGX_OK)
*/
if (ngx_http_nphase_range_update(r, npcf->range_var_index,
rin->start + ctx->range_sent.end,
rin->end ? rin->end : ctx->wfsz, 0)
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_http_nphase_run_subrequest(r, ctx, &npcf->uri, NULL)
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
return NGX_AGAIN;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"nphase location uri and body both not ready");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* initial module ctx */
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_nphase_ctx_t));
if (ctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* parse headers_in range to ctx->range_in */
if (r->headers_in.range != NULL) {
if (r->headers_in.range->value.len >= 7
&& ngx_strncasecmp(r->headers_in.range->value.data,
(u_char *) "bytes=", 6)
== 0)
{
if (ngx_array_init(&ctx->range_in, r->pool, 1,
sizeof(ngx_http_nphase_range_t))
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
rc = ngx_http_nphase_range_parse(r, ctx);
if (rc == NGX_OK) {
if (ctx->range_in.nelts > 1) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"multipart range request not supported");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
r->allow_ranges = 1;
} else {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
} else {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
} else {
if (ngx_array_init(&ctx->range_in, r->pool, 1,
sizeof(ngx_http_nphase_range_t))
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_http_nphase_range_t *range;
range = ngx_array_push(&ctx->range_in);
if (range == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
range->start = 0;
range->end = 0;
range->flag = -1;
}
rin = ctx->range_in.elts;
if (ngx_http_nphase_range_update(r, npcf->range_var_index,
rin->start, rin->end, rin->flag)
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* run a subrequest to nphase_uri */
if (ngx_http_nphase_run_subrequest(r, ctx, &npcf->uri, NULL)
!= NGX_OK)
{
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_http_set_ctx(r, ctx, ngx_http_nphase_module);
/* save phase 1 uri to ctx->uri_var_value */
var = ngx_http_get_indexed_variable(r, npcf->uri_var_index);
if (var == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx->uri_var_value.data = var->data;
ctx->uri_var_value.len = var->len;
return NGX_AGAIN;
}
static ngx_int_t
ngx_http_nphase_content_handler(ngx_http_request_t *r)
{
ngx_http_nphase_ctx_t *ctx;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase content handler");
ctx = ngx_http_get_module_ctx(r, ngx_http_nphase_module);
/* Entering content phase means valid response has been
received by subrequest. */
if (ctx == NULL) {
return NGX_DECLINED;
}
if (! ctx->header_sent) {
if (ngx_http_send_header(r) == NGX_ERROR) {
return NGX_ERROR;
}
}
/* send out buf in case of not sending by subrequest */
if (r->out && r->out->buf && r->out->buf->pos) {
return ngx_http_output_filter(r, NULL);
}
return NGX_OK;
}
static ngx_int_t
ngx_http_nphase_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc)
{
off_t size = 0;
ngx_chain_t *ln;
ngx_http_nphase_ctx_t *ctx = data; /* parent ctx */
ngx_http_nphase_sub_ctx_t *sr_ctx;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase subrequest done s:%d", r->headers_out.status);
sr_ctx = ngx_http_get_module_ctx(r, ngx_http_nphase_module);
if (!sr_ctx) {
return rc;
}
/* todo: update ctx->range_sent */
for (ln = r->parent->out; ln; ln = ln->next) {
size += ngx_buf_size(ln->buf);
}
ctx->range_sent.end =
r->parent->connection->sent + size - r->parent->header_size;
if (ctx->range_sent.end < 0) {
ctx->range_sent.end = 0;
}
ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase range sent offset:%O", ctx->range_sent.end);
if (r->headers_out.status >= NGX_HTTP_SPECIAL_RESPONSE
&& r->headers_out.status != NGX_HTTP_MOVED_TEMPORARILY )
{
ctx->sr_error = 1;
ctx->sr_count_e++;
}
// todo: .....
if ((r->headers_out.status == NGX_HTTP_OK
|| r->headers_out.status == NGX_HTTP_PARTIAL_CONTENT)
&& ctx->range_sent.end == 0)
{
/* backend return 200 or 206 without body */
ctx->sr_error = 1;
ctx->sr_count_e++;
}
ctx->sr_done = 1;
return rc;
}
ngx_int_t
ngx_http_nphase_filter_init(ngx_conf_t *cf)
{
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_nphase_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_nphase_body_filter;
return NGX_OK;
}
static ngx_int_t
ngx_http_nphase_header_filter(ngx_http_request_t *r)
{
ngx_http_nphase_ctx_t *pr_ctx;
ngx_http_nphase_sub_ctx_t *sr_ctx;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase header filter status:%d",
r->headers_out.status);
if (r == r->main) {
/* parent request */
pr_ctx = ngx_http_get_module_ctx(r, ngx_http_nphase_module);
if (! pr_ctx) {
return ngx_http_next_header_filter(r);
}
if (pr_ctx->header_sent) {
return NGX_OK;
}
pr_ctx->header_sent = 1;
if (pr_ctx->pr_status != 0) {
r->headers_out.status = pr_ctx->pr_status;
}
if (r->headers_out.status == NGX_HTTP_OK) {
r->headers_out.content_length_n = pr_ctx->wfsz;
}
if (r->headers_out.status == NGX_HTTP_PARTIAL_CONTENT) {
r->headers_out.content_length_n = pr_ctx->wfsz;
ngx_http_nphase_add_range_singlepart_header(r, pr_ctx);
}
return ngx_http_next_header_filter(r);
}else{
/* sub request */
sr_ctx = ngx_http_get_module_ctx(r, ngx_http_nphase_module);
if (! sr_ctx) {
return ngx_http_next_header_filter(r);
}
if (! r->parent) {
return ngx_http_next_header_filter(r);
}
pr_ctx = ngx_http_get_module_ctx(r->parent, ngx_http_nphase_module);
if (! pr_ctx) {
return ngx_http_next_header_filter(r);
}
if (pr_ctx->body_ready) {
return NGX_OK;
}
if (r->headers_out.status >= NGX_HTTP_SPECIAL_RESPONSE
&& r->headers_out.status != NGX_HTTP_MOVED_TEMPORARILY ) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (r->headers_out.status == NGX_HTTP_MOVED_TEMPORARILY ) {
if (ngx_http_nphase_process_header(r, pr_ctx) == NGX_ERROR
&& pr_ctx->wfsz == 0) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (! r->headers_out.location) {
u_char *p;
size_t len = 0;
ngx_str_t val;
ngx_str_t key = ngx_string("Location");
if (ngx_http_nphase_copy_header_value(
&r->headers_out.headers, &key, &val) == NGX_OK)
{
if ( val.data[0] == '/' ) {
len += r->upstream->schema.len;
len += r->upstream->resolved->host.len;
if (len == 0) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
len += val.len;
p = ngx_palloc(r->pool, len);
if (p == NULL) {
return NGX_ERROR;
}
pr_ctx->loc_body_c.data = p;
pr_ctx->loc_body_c.len = len;
if ( val.data[0] == '/' ) {
p = ngx_copy(p, r->upstream->schema.data,
r->upstream->schema.len);
p = ngx_copy(p, r->upstream->resolved->host.data,
r->upstream->resolved->host.len);
}
p = ngx_copy(p, val.data, val.len);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase get next phase loc: %V",
&pr_ctx->loc_body_c);
pr_ctx->loc_ready = 1;
return NGX_OK;
}
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"nphase get 302 from upstream without location string");
return NGX_ERROR;
}
pr_ctx->loc_body_c.data = ngx_palloc(r->pool,
r->headers_out.location->value.len);
if (pr_ctx->loc_body_c.data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(pr_ctx->loc_body_c.data,
r->headers_out.location->value.data,
r->headers_out.location->value.len);
pr_ctx->loc_body_c.len = r->headers_out.location->value.len;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase get next phase loc: %V",
&pr_ctx->loc_body_c);
pr_ctx->loc_ready = 1;
return NGX_OK;
}
/* upstream return 20x */
pr_ctx->body_ready = 1;
return NGX_OK;
}
return ngx_http_next_header_filter(r);
}
static ngx_int_t
ngx_http_nphase_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
ngx_http_nphase_sub_ctx_t *sr_ctx;
ngx_http_nphase_ctx_t *pr_ctx;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"nphase body filter status:%d",
r->headers_out.status);
if (r == r->main) {
/* parent request */
pr_ctx = ngx_http_get_module_ctx(r, ngx_http_nphase_module);
if (! pr_ctx) {
return ngx_http_next_body_filter(r, in);
}
if (pr_ctx->sr_error
&& r->headers_out.status < NGX_HTTP_SPECIAL_RESPONSE)
{
return ngx_http_next_body_filter(r, NULL);
}
return ngx_http_next_body_filter(r, in);
}else{
sr_ctx = ngx_http_get_module_ctx(r, ngx_http_nphase_module);
if (!sr_ctx) {
return ngx_http_next_body_filter(r, in);
}
if (! r->parent) {
return ngx_http_next_body_filter(r, in);
}
pr_ctx = ngx_http_get_module_ctx(r->parent, ngx_http_nphase_module);
if (! pr_ctx) {
return ngx_http_next_body_filter(r, in);
}
if (! pr_ctx->body_ready) {
return NGX_OK;
}
if (! pr_ctx->header_sent){
pr_ctx->pr_status = r->headers_out.status;
if (r->parent->headers_out.content_length_n == -1) {
r->parent->headers_out.content_length_n =
r->headers_out.content_length_n;
}
if (ngx_http_send_header(r->parent) == NGX_ERROR) {
return NGX_ERROR;
}
}
return ngx_http_output_filter(r->parent, in);
}
}
void
ngx_http_nphase_discard_bufs(ngx_pool_t *pool, ngx_chain_t *in)
{
ngx_chain_t *cl;
for (cl = in; cl; cl = cl->next) {
cl->buf->pos = cl->buf->last;
cl->buf->file_pos = cl->buf->file_last;
}
}
static char *
ngx_http_nphase_uri(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_nphase_conf_t *npcf = conf;
ngx_http_core_loc_conf_t *clcf;
ngx_str_t *value;
if (npcf->uri.data != NULL) {
return "is duplicate";
}
value = cf->args->elts;
if (ngx_strcmp(value[1].data, "off") == 0) {
npcf->uri.len = 0;
npcf->uri.data = (u_char *) "";
return NGX_CONF_OK;
}
npcf->uri = value[1];
/* register content phase handler */
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
if (clcf == NULL) {
return NGX_CONF_ERROR;
}
clcf->handler = ngx_http_nphase_content_handler;
return NGX_CONF_OK;
}
static char *
ngx_http_nphase_set_uri_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_nphase_conf_t *npcf = conf;
ngx_str_t *value;
value = cf->args->elts;
if (value[1].data[0] != '$') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"%V\"", &value[1]);
return NGX_CONF_ERROR;
}
value[1].len--;
value[1].data++;
npcf->uri_var_index = ngx_http_get_variable_index(cf, &value[1]);
if (npcf->uri_var_index == NGX_ERROR) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static char *
ngx_http_nphase_set_range_var(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_nphase_conf_t *npcf = conf;
ngx_str_t *value;
value = cf->args->elts;
if (value[1].data[0] != '$') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"%V\"", &value[1]);
return NGX_CONF_ERROR;
}
value[1].len--;
value[1].data++;
npcf->range_var_index = ngx_http_get_variable_index(cf, &value[1]);
if (npcf->range_var_index == NGX_ERROR) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
ngx_int_t
ngx_http_nphase_range_update(ngx_http_request_t *r, ngx_int_t index,
off_t start, off_t end, ngx_int_t flag)
{
ngx_http_variable_value_t *var;
var = ngx_http_get_indexed_variable(r, index);
if (var == NULL) {
return NGX_ERROR;
}
var->data = ngx_pnalloc(r->pool,
sizeof("bytes=-") + 2 * NGX_OFF_T_LEN);
if (var->data == NULL) {
return NGX_ERROR;
}
if (flag == 0) {
var->len = ngx_sprintf(var->data, "bytes=%O-%O", start, end)
- var->data;
} else if (flag == 1) {
var->len = ngx_sprintf(var->data, "bytes=%O-", start)
- var->data;
} else if (flag == 2) {
var->len = ngx_sprintf(var->data, "bytes=-%O", end)
- var->data;
} else if (flag == -1) {
var->len = ngx_sprintf(var->data, "bytes=0-")
- var->data;
} else {
return NGX_ERROR;
}
return NGX_OK;
}
ngx_int_t
ngx_http_nphase_range_parse(ngx_http_request_t *r, ngx_http_nphase_ctx_t *ctx)
{
u_char *p;
off_t start, end;
ngx_int_t flag;
ngx_uint_t suffix;
ngx_http_nphase_range_t *range;
p = r->headers_in.range->value.data + 6;
for ( ;; ) {
start = 0;
end = 0;
suffix = 0;
flag = 0;
while (*p == ' ') { p++; }
if (*p != '-') {
if (*p < '0' || *p > '9') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p >= '0' && *p <= '9') {
start = start * 10 + *p++ - '0';
}
while (*p == ' ') { p++; }
if (*p++ != '-') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p == ' ') { p++; }
if (*p == ',' || *p == '\0') {
range = ngx_array_push(&ctx->range_in);
if (range == NULL) {
return NGX_ERROR;
}
range->start = start;
range->end = end;
range->flag = 1;
if (*p++ != ',') {
return NGX_OK;
}
continue;
}
} else {
suffix = 1;
p++;
}
if (*p < '0' || *p > '9') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p >= '0' && *p <= '9') {
end = end * 10 + *p++ - '0';
}
while (*p == ' ') { p++; }
if (*p != ',' && *p != '\0') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
if (suffix) {
flag = 2;
}
if (start > end && flag != 1) {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
range = ngx_array_push(&ctx->range_in);
if (range == NULL) {
return NGX_ERROR;
}
range->start = start;
range->end = end;
range->flag = flag;
if (*p++ != ',') {
return NGX_OK;
}
}
}
ngx_int_t
ngx_http_nphase_content_range_parse(u_char *p, ngx_http_nphase_range_t *range)
{
off_t start, end, length;
ngx_uint_t suffix;
start = 0;
end = 0;
length = 0;
suffix = 0;
while (*p == ' ') { p++; }
if (*p != '-') {
if (*p < '0' || *p > '9') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p >= '0' && *p <= '9') {
start = start * 10 + *p++ - '0';
}
while (*p == ' ') { p++; }
if (*p++ != '-') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p == ' ') { p++; }
if (*p == ',' || *p == '\0') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
} else {
suffix = 1;
p++;
}
if (*p < '0' || *p > '9') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p >= '0' && *p <= '9') {
end = end * 10 + *p++ - '0';
}
if (start > end) {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
if (!suffix) {
range->start = start;
}
range->end = end;
while (*p == ' ') { p++; }
if (*p != '/') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
p++;
if (*p < '0' || *p > '9') {
return NGX_HTTP_RANGE_NOT_SATISFIABLE;
}
while (*p >= '0' && *p <= '9') {
length = length * 10 + *p++ - '0';
}