-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathngx_hmux_module.c
3195 lines (2591 loc) · 94.3 KB
/
ngx_hmux_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
/*-----------------------------------------------------------------------------
* Description: implements resin's mod_caucho function for nginx
* Version: 1.3
* Author: bin wang
* Company: NetEase
* Mail: wangbin579@gmail.com
* Attension:
* 1) it is tested in linux only
* 2) we have already tested eight projects for this module:
* resin-doc
* jackrabbit
* hudson
* jetspeed
* geoserver
* VQWiki
* struts2-showcase
* spring-security-samples
* all are easily deployed in resin (web war)
* 3) this module references yaoweibin's nginx ajp module
* 4) if you use keepalive module,you should set accept_mutex
* off in multiprocess environment
* 5) if you have any problems or bugs, please contact me
*
* The following describes the hmux protocol implemented in this module
*
* hmux protocol
* A GET request:
* Frontend Backend
* CSE_METHOD
* ...
* CSE_HEADER/CSE_VALUE
* CSE_END
* CSE_DATA
* CSE_DATA
* CSE_END
*
* Short POST:
* Frontend Backend
* CSE_METHOD
* ...
* CSE_HEADER/CSE_VALUE
* CSE_DATA
* CSE_END
* CSE_DATA
* CSE_DATA
* CSE_END
*
* Long POST:
* Frontend Backend
* CSE_METHOD
* ...
* CSE_HEADER/CSE_VALUE
* CSE_DATA
* CSE_DATA (optional) #here we buffer resp data
* CSE_DATA
* CSE_ACK
* CSE_DATA (optional) #here we buffer resp data
* CSE_DATA
* CSE_ACK
* CSE_END
* CSE_DATA
* CSE_END
*
*
*---------------------------------------------------------------------------*/
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#define HMUX_CHANNEL 'C'
#define HMUX_ACK 'A'
#define HMUX_ERROR 'E'
#define HMUX_YIELD 'Y'
#define HMUX_QUIT 'Q'
#define HMUX_EXIT 'X'
#define HMUX_DATA 'D'
#define HMUX_URL 'U'
#define HMUX_STRING 'S'
#define HMUX_HEADER 'H'
#define HMUX_META_HEADER 'M'
#define HMUX_PROTOCOL 'P'
#define CSE_NULL '?'
#define CSE_PATH_INFO 'b'
#define CSE_PROTOCOL 'c'
#define CSE_REMOTE_USER 'd'
#define CSE_QUERY_STRING 'e'
#define CSE_SERVER_PORT 'g'
#define CSE_REMOTE_HOST 'h'
#define CSE_REMOTE_ADDR 'i'
#define CSE_REMOTE_PORT 'j'
#define CSE_REAL_PATH 'k'
#define CSE_AUTH_TYPE 'n'
#define CSE_URI 'o'
#define CSE_CONTENT_LENGTH 'p'
#define CSE_CONTENT_TYPE 'q'
#define CSE_IS_SECURE 'r'
#define CSE_SESSION_GROUP 's'
#define CSE_CLIENT_CERT 't'
#define CSE_SERVER_TYPE 'u'
#define HMUX_METHOD 'm'
#define HMUX_FLUSH 'f'
#define HMUX_SERVER_NAME 'v'
#define HMUX_STATUS 's'
#define HMUX_CLUSTER 'c'
#define HMUX_SRUN 's'
#define HMUX_SRUN_BACKUP 'b'
#define HMUX_SRUN_SSL 'e'
#define HMUX_UNAVAILABLE 'u'
#define HMUX_WEB_APP_UNAVAILABLE 'U'
#define CSE_HEADER 'H'
#define CSE_VALUE 'V'
#define CSE_STATUS 'S'
#define CSE_SEND_HEADER 'G'
#define CSE_PING 'P'
#define CSE_QUERY 'Q'
#define CSE_ACK 'A'
#define CSE_DATA 'D'
#define CSE_FLUSH 'F'
#define CSE_KEEPALIVE 'K'
#define CSE_END 'Z'
#define CSE_CLOSE 'X'
#define HMUX_CMD_SZ 8
#define HMUX_DATA_SEG_SZ 8
#define HMUX_META_DATA_LEN 3
#define HMUX_MSG_BUFFER_SZ 8192
#define HMUX_MAX_BUFFER_SZ 65536
#define HMUX_DISPATCH_PROTOCOL 0x102
#define HMUX_QUERY 0x102
#define HMUX_EOVERFLOW 1001
#define NGX_HMUX_END 1
ngx_module_t ngx_hmux_module;
typedef struct hmux_msg_s hmux_msg_t;
struct hmux_msg_s
{
ngx_buf_t *buf;
};
typedef struct {
ngx_http_upstream_conf_t upstream;
size_t hmux_header_packet_buffer_size_conf;
size_t max_hmux_data_packet_size_conf;
ngx_flag_t hmux_set_header_x_forwarded_for;
ngx_array_t *hmux_lengths;
ngx_array_t *hmux_values;
#if (NGX_HTTP_CACHE)
ngx_http_complex_value_t cache_key;
#endif
} ngx_hmux_loc_conf_t;
typedef enum {
ngx_hmux_st_init_state = 0,
ngx_hmux_st_forward_request_sent,
ngx_hmux_st_request_body_data_sending,
ngx_hmux_st_request_send_all_done,
ngx_hmux_st_response_recv_headers,
ngx_hmux_st_response_parse_headers_done,
ngx_hmux_st_response_headers_sent,
ngx_hmux_st_response_body_data_sending,
ngx_hmux_st_response_end
} ngx_hmux_state_e;
typedef struct {
hmux_msg_t msg;
ngx_hmux_state_e state;
/*
* this is for fixing the problem
* when request content length is not equal to content-length
*/
off_t req_body_send_len;
off_t req_body_len;
/* request body which has not been sent to the backend */
ngx_chain_t *req_body;
/* buffer for Long POST disposure */
ngx_chain_t *resp_body;
/* for input filter disposure */
void *undisposed;
size_t undisposed_size;
/* the response body chunk packet's length */
int resp_chunk_len;
int buf_next_read_offset;
unsigned int req_body_sent_over:1;
unsigned int head_send_flag:1;
unsigned int long_post_flag:1;
unsigned int flush_flag:1;
unsigned int restore_flag:1;
unsigned int code:8;
unsigned int mend_flag:8;
} ngx_hmux_ctx_t;
#if (NGX_HTTP_CACHE)
static ngx_int_t ngx_hmux_create_key(ngx_http_request_t *r);
#endif
static ngx_int_t ngx_hmux_eval(ngx_http_request_t *r,
ngx_hmux_loc_conf_t *hlcf);
static ngx_int_t ngx_hmux_create_request(ngx_http_request_t *r);
static ngx_int_t ngx_hmux_reinit_request(ngx_http_request_t *r);
static ngx_int_t ngx_hmux_process_header(ngx_http_request_t *r);
static ngx_int_t ngx_hmux_input_filter_init(void *data);
static ngx_int_t ngx_hmux_input_filter(ngx_event_pipe_t *p,
ngx_buf_t *buf);
static void ngx_hmux_abort_request(ngx_http_request_t *r);
static void ngx_hmux_finalize_request(ngx_http_request_t *r,
ngx_int_t rc);
static ngx_int_t ngx_http_upstream_send_request_body(ngx_http_request_t *r,
ngx_http_upstream_t *u);
static ngx_chain_t *hmux_data_msg_send_body(ngx_http_request_t *r,
size_t max_size, ngx_chain_t **body);
static void ngx_http_upstream_send_request_body_handler(ngx_http_request_t *r,
ngx_http_upstream_t *u);
static void ngx_http_upstream_dummy_handler(ngx_http_request_t *r,
ngx_http_upstream_t *u);
#if (NGX_HTTP_CACHE)
static char *ngx_hmux_cache(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_hmux_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#endif
static char *ngx_hmux_pass(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_hmux_store(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_hmux_lowat_check(ngx_conf_t *cf, void *post,
void *data);
static char *ngx_hmux_upstream_max_fails_unsupported(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_hmux_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_hmux_get_x_forwarded_for_value(ngx_http_request_t *r,
ngx_str_t *v, uintptr_t data);
static void *ngx_hmux_create_loc_conf(ngx_conf_t *cf);
static char *ngx_hmux_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static int hmux_log_overflow(ngx_uint_t level, hmux_msg_t *msg,
const char *context);
/*
* protocol functions
*/
static ngx_int_t hmux_start_channel(hmux_msg_t *msg,
unsigned short channel);
static ngx_int_t hmux_write_string(hmux_msg_t *msg,
char code, ngx_str_t *value);
static ngx_int_t hmux_read_len(hmux_msg_t *msg, int *rlen);
static ngx_int_t hmux_read_byte(hmux_msg_t *s, u_char *rvalue);
static ngx_int_t hmux_read_string(hmux_msg_t *msg, ngx_str_t *rvalue);
static hmux_msg_t *hmux_msg_reuse(hmux_msg_t *msg);
static ngx_int_t hmux_data_msg_begin(hmux_msg_t *msg, size_t size);
static ngx_chain_t *hmux_cmd_msg(ngx_hmux_ctx_t *ctx, ngx_http_request_t *r,
u_char code);
static ngx_int_t hmux_msg_create_buffer(ngx_pool_t *pool, size_t size,
hmux_msg_t *msg);
static ngx_int_t hmux_marshal_into_msg(hmux_msg_t *msg,
ngx_http_request_t *r, ngx_hmux_loc_conf_t *hlcf);
static ngx_int_t hmux_unmarshal_response(hmux_msg_t *msg,
ngx_http_request_t *r, ngx_hmux_loc_conf_t *hlcf);
static ngx_conf_post_t ngx_hmux_lowat_post = { ngx_hmux_lowat_check };
static ngx_conf_bitmask_t ngx_hmux_next_upstream_masks[] = {
{ ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
{ ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
{ ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
#if defined(nginx_version) && nginx_version >= 1009013
{ ngx_string("non_idempotent"), NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT },
#endif
{ ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
{ ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 },
{ ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
{ ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 },
{ ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
{ ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
{ ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
{ ngx_null_string, 0 }
};
static ngx_path_init_t ngx_hmux_temp_path = {
ngx_string("hmux_temp"), { 1, 2, 0 }
};
static ngx_str_t ngx_hmux_hide_headers[] = {
ngx_string("Status"),
ngx_string("X-Accel-Expires"),
ngx_string("X-Accel-Redirect"),
ngx_string("X-Accel-Limit-Rate"),
ngx_string("X-Accel-Buffering"),
ngx_string("X-Accel-Charset"),
ngx_null_string
};
#if (NGX_HTTP_CACHE)
static ngx_str_t ngx_hmux_hide_cache_headers[] = {
ngx_string("Status"),
ngx_string("X-Accel-Expires"),
ngx_string("X-Accel-Redirect"),
ngx_string("X-Accel-Limit-Rate"),
ngx_string("X-Accel-Buffering"),
ngx_string("X-Accel-Charset"),
ngx_string("Set-Cookie"),
ngx_string("P3P"),
ngx_null_string
};
#endif
static ngx_command_t ngx_hmux_commands[] = {
{ ngx_string("hmux_pass"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
ngx_hmux_pass,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("hmux_header_packet_buffer_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, hmux_header_packet_buffer_size_conf),
NULL },
{ ngx_string("hmux_max_data_packet_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, max_hmux_data_packet_size_conf),
NULL },
{ ngx_string("hmux_store"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_hmux_store,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("hmux_store_access"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
ngx_conf_set_access_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.store_access),
NULL },
{ ngx_string("hmux_ignore_client_abort"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.ignore_client_abort),
NULL },
{ ngx_string("hmux_connect_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.connect_timeout),
NULL },
{ ngx_string("hmux_send_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.send_timeout),
NULL },
{ ngx_string("hmux_send_lowat"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.send_lowat),
&ngx_hmux_lowat_post },
{ ngx_string("hmux_buffer_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.buffer_size),
NULL },
{ ngx_string("hmux_pass_request_headers"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.pass_request_headers),
NULL },
{ ngx_string("hmux_pass_request_body"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.pass_request_body),
NULL },
{ ngx_string("hmux_intercept_errors"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.intercept_errors),
NULL },
{ ngx_string("hmux_read_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.read_timeout),
NULL },
#if defined(nginx_version) && nginx_version >= 1007005
{ ngx_string("hmux_next_upstream_tries"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.next_upstream_tries),
NULL },
{ ngx_string("hmux_next_upstream_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.next_upstream_timeout),
NULL },
#endif
{ ngx_string("hmux_buffers"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
ngx_conf_set_bufs_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.bufs),
NULL },
{ ngx_string("hmux_busy_buffers_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.busy_buffers_size_conf),
NULL },
#if (NGX_HTTP_CACHE)
{ ngx_string("hmux_cache"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_hmux_cache,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("hmux_cache_key"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_hmux_cache_key,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("hmux_cache_path"),
NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
ngx_http_file_cache_set_slot,
0,
0,
&ngx_hmux_module },
{ ngx_string("hmux_cache_valid"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_http_file_cache_valid_set_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.cache_valid),
NULL },
{ ngx_string("hmux_cache_min_uses"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.cache_min_uses),
NULL },
{ ngx_string("hmux_cache_use_stale"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.cache_use_stale),
&ngx_hmux_next_upstream_masks },
{ ngx_string("hmux_cache_methods"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.cache_methods),
&ngx_http_upstream_cache_method_mask },
#endif
{ ngx_string("hmux_temp_path"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
ngx_conf_set_path_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.temp_path),
NULL },
{ ngx_string("hmux_max_temp_file_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.max_temp_file_size_conf),
NULL },
{ ngx_string("hmux_temp_file_write_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.temp_file_write_size_conf),
NULL },
{ ngx_string("hmux_next_upstream"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.next_upstream),
&ngx_hmux_next_upstream_masks },
{ ngx_string("hmux_upstream_max_fails"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_hmux_upstream_max_fails_unsupported,
0,
0,
NULL },
{ ngx_string("hmux_upstream_fail_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_hmux_upstream_fail_timeout_unsupported,
0,
0,
NULL },
{ ngx_string("hmux_pass_header"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_str_array_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.pass_headers),
NULL },
{ ngx_string("hmux_hide_header"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_str_array_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.hide_headers),
NULL },
{ ngx_string("hmux_ignore_headers"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, upstream.ignore_headers),
&ngx_http_upstream_ignore_headers_masks},
{ ngx_string("hmux_x_forwarded_for"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_hmux_loc_conf_t, hmux_set_header_x_forwarded_for),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_hmux_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_hmux_create_loc_conf, /* create location configuration */
ngx_hmux_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_hmux_module = {
NGX_MODULE_V1,
&ngx_hmux_module_ctx, /* module context */
ngx_hmux_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
};
ngx_int_t
ngx_hmux_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_upstream_t *u;
ngx_hmux_ctx_t *ctx;
ngx_hmux_loc_conf_t *hlcf;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"enter hmux handler:%V", &r->uri);
if (r->subrequest_in_memory) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"ngx_hmux_module does not support "
"subrequest in memory");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_http_set_content_type(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (ngx_http_upstream_create(r) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ctx = ngx_pcalloc(r->pool, sizeof(ngx_hmux_ctx_t));
if (ctx == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (r->headers_in.content_length_n > 0) {
ctx->req_body_len = r->headers_in.content_length_n;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"set req_body_len:%d", ctx->req_body_len);
ctx->state = ngx_hmux_st_init_state;
ngx_http_set_ctx(r, ctx, ngx_hmux_module);
hlcf = ngx_http_get_module_loc_conf(r, ngx_hmux_module);
if (hlcf->hmux_lengths) {
if (ngx_hmux_eval(r, hlcf) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
u = r->upstream;
u->schema.len = sizeof("hmux://") - 1;
u->schema.data = (u_char *) "hmux://";
u->output.tag = (ngx_buf_tag_t) &ngx_hmux_module;
u->conf = &hlcf->upstream;
#if (NGX_HTTP_CACHE)
u->create_key = ngx_hmux_create_key;
#endif
u->create_request = ngx_hmux_create_request;
u->reinit_request = ngx_hmux_reinit_request;
u->process_header = ngx_hmux_process_header;
u->abort_request = ngx_hmux_abort_request;
u->finalize_request = ngx_hmux_finalize_request;
u->buffering = 1;
u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
if (u->pipe == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
u->pipe->input_filter = ngx_hmux_input_filter;
u->pipe->input_ctx = r;
u->input_filter_init = ngx_hmux_input_filter_init;
rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_DONE;
}
static ngx_int_t
ngx_hmux_eval(ngx_http_request_t *r, ngx_hmux_loc_conf_t *hlcf)
{
ngx_url_t u;
ngx_memzero(&u, sizeof(ngx_url_t));
if (ngx_http_script_run(r, &u.url, hlcf->hmux_lengths->elts, 0,
hlcf->hmux_values->elts) == NULL)
{
return NGX_ERROR;
}
u.no_resolve = 1;
if (ngx_parse_url(r->pool, &u) != NGX_OK) {
if (u.err) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"%s in upstream \"%V\"", u.err, &u.url);
}
return NGX_ERROR;
}
r->upstream->resolved = ngx_pcalloc(r->pool,
sizeof(ngx_http_upstream_resolved_t));
if (r->upstream->resolved == NULL) {
return NGX_ERROR;
}
#if defined(nginx_version) && nginx_version >= 1011006
if (u.addrs) {
r->upstream->resolved->sockaddr = u.addrs[0].sockaddr;
r->upstream->resolved->socklen = u.addrs[0].socklen;
r->upstream->resolved->naddrs = 1;
r->upstream->resolved->name = u.addrs[0].name;
}
r->upstream->resolved->host = u.host;
#else
if (u.addrs && u.addrs[0].sockaddr) {
r->upstream->resolved->sockaddr = u.addrs[0].sockaddr;
r->upstream->resolved->socklen = u.addrs[0].socklen;
r->upstream->resolved->naddrs = 1;
r->upstream->resolved->host = u.addrs[0].name;
} else {
r->upstream->resolved->host = u.host;
}
#endif
r->upstream->resolved->port = u.port;
r->upstream->resolved->no_port = u.no_port;
return NGX_OK;
}
#if (NGX_HTTP_CACHE)
static ngx_int_t
ngx_hmux_create_key(ngx_http_request_t *r)
{
ngx_str_t *key;
ngx_hmux_loc_conf_t *hlcf;
key = ngx_array_push(&r->cache->keys);
if (key == NULL) {
return NGX_ERROR;
}
hlcf = ngx_http_get_module_loc_conf(r, ngx_hmux_module);
if (ngx_http_complex_value(r, &hlcf->cache_key, key) != NGX_OK) {
return NGX_ERROR;
}
return NGX_OK;
}
#endif
static ngx_int_t
ngx_hmux_create_request(ngx_http_request_t *r)
{
ngx_int_t rc, need_send_body = 0;
hmux_msg_t *msg;
ngx_chain_t *cl, *last;
ngx_hmux_ctx_t *ctx;
ngx_hmux_loc_conf_t *hlcf;
ctx = ngx_http_get_module_ctx(r, ngx_hmux_module);
hlcf = ngx_http_get_module_loc_conf(r, ngx_hmux_module);
if (ctx == NULL || hlcf == NULL) {
return NGX_ERROR;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"create req,req_body_len:%d", ctx->req_body_len);
msg = hmux_msg_reuse(&ctx->msg);
/* creates buffer for header */
if (NGX_OK != hmux_msg_create_buffer(r->pool,
hlcf->hmux_header_packet_buffer_size_conf, msg)) {
return NGX_ERROR;
}
rc = hmux_marshal_into_msg(msg, r, hlcf);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
"hmux_header_packet_buffer_size may be too small:%uz",
hlcf->hmux_header_packet_buffer_size_conf);
return rc;
}
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = msg->buf;
cl->buf->flush = 1;
ctx->state = ngx_hmux_st_forward_request_sent;
if (hlcf->upstream.pass_request_body) {
ctx->req_body = r->upstream->request_bufs;
if (ctx->req_body != NULL && ctx->req_body->buf != NULL) {
if (ctx->req_body->buf->in_file) {
if (ctx->req_body->buf->file_pos !=
ctx->req_body->buf->file_last)
{
need_send_body = 1;
}
} else {
if (ctx->req_body->buf->pos != ctx->req_body->buf->last) {
need_send_body = 1;
}
}
}
if (!need_send_body) {
if (ctx->req_body != NULL) {
if (ctx->req_body->buf != NULL) {
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
"It has body but need_send_body is false");
} else {
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
"strange here, ctx->req_body->buf is null");
}
}
}
}
r->upstream->request_bufs = cl;
if (need_send_body) {
cl->next = hmux_data_msg_send_body(r,
hlcf->max_hmux_data_packet_size_conf, &ctx->req_body);
last = cl;
while (last->next != NULL) {
last = last->next;
}
if (ctx->req_body != NULL && !ctx->req_body_sent_over) {
/* it has body data left for sending */
ctx->state = ngx_hmux_st_request_body_data_sending;
last->next = hmux_cmd_msg(ctx, r, HMUX_YIELD);
} else {
ctx->state = ngx_hmux_st_request_send_all_done;
last->next = hmux_cmd_msg(ctx, r, HMUX_QUIT);
ctx->req_body_sent_over = 1;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"req body sent done:%V", &r->uri);
}
} else {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"no need to send req_body:%V", &r->uri);
ctx->req_body_sent_over = 1;
ctx->state = ngx_hmux_st_request_send_all_done;
cl->next = hmux_cmd_msg(ctx, r, HMUX_QUIT);
}
return NGX_OK;
}
static ngx_int_t
ngx_hmux_reinit_request(ngx_http_request_t *r)
{
ngx_hmux_ctx_t *ctx;
ngx_hmux_loc_conf_t *hlcf;
ctx = ngx_http_get_module_ctx(r, ngx_hmux_module);
hlcf = ngx_http_get_module_loc_conf(r, ngx_hmux_module);
if (ctx == NULL || hlcf == NULL) {
return NGX_ERROR;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"reinit req,req_body_len:%d", ctx->req_body_len);
memset(ctx, 0, sizeof(ngx_hmux_ctx_t));
ctx->state = ngx_hmux_st_init_state;
hmux_msg_reuse(&ctx->msg);
return NGX_OK;
}
static ngx_int_t
ngx_hmux_process_header(ngx_http_request_t *r)
{
ngx_hmux_ctx_t *ctx;
ngx_hmux_loc_conf_t *hlcf;
ctx = ngx_http_get_module_ctx(r, ngx_hmux_module);
hlcf = ngx_http_get_module_loc_conf(r, ngx_hmux_module);
if (ctx == NULL || hlcf == NULL) {
return NGX_ERROR;
}
ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
"ngx_hmux_process_header: state(%d)", ctx->state);
return hmux_unmarshal_response(&ctx->msg, r, hlcf);
}
static ngx_int_t
ngx_http_upstream_send_request_body(ngx_http_request_t *r,
ngx_http_upstream_t *u)
{
ngx_int_t rc;
ngx_chain_t *cl, *last;
hmux_msg_t *msg;
ngx_connection_t *c;
ngx_hmux_ctx_t *ctx;
ngx_hmux_loc_conf_t *hlcf;
c = u->peer.connection;
ctx = ngx_http_get_module_ctx(r, ngx_hmux_module);
hlcf = ngx_http_get_module_loc_conf(r, ngx_hmux_module);
if (ctx == NULL || hlcf == NULL)
{
return NGX_ERROR;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"send req body,req_body_len:%d", ctx->req_body_len);
cl = hmux_data_msg_send_body(r, hlcf->max_hmux_data_packet_size_conf,
&ctx->req_body);
if (u->output.in == NULL && u->output.busy == NULL) {
if (cl == NULL) {
msg = hmux_msg_reuse(&ctx->msg);
hmux_data_msg_begin(msg, 0);
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL ) {
return NGX_ERROR;
}
cl->buf = msg->buf;
cl->next = NULL;
}
}
last = cl;
while (last->next != NULL) {
last = last->next;
}
if (ctx->req_body != NULL && !ctx->req_body_sent_over) {
ctx->state = ngx_hmux_st_request_body_data_sending;
last->next = hmux_cmd_msg(ctx, r, HMUX_YIELD);
} else {
last->next = hmux_cmd_msg(ctx, r, HMUX_QUIT);
ctx->state = ngx_hmux_st_request_send_all_done;
ctx->req_body_sent_over = 1;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"req body sent over:%V", &r->uri);