forked from shevacjs/ngx_http_pbmsgpack_transfer_module
/
ngx_http_pb_msgpack_transfer_module.cpp
1218 lines (954 loc) · 34.1 KB
/
ngx_http_pb_msgpack_transfer_module.cpp
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) 2011 Baidu.com, Inc. All Rights Reserved
* $Id$
*
**************************************************************************/
/**
* @file ngx_http_pb_msgpack_transfer_module.c
* @author forum(chenjiansen@baidu.com)
* @date 2014/03/11 23:30
* @version $Revision$
* @brief
*
**/
// stub module to test header files' C++ compatibilty
extern "C"
{
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_event.h>
#include <ngx_event_connect.h>
#include <ngx_event_pipe.h>
#include <ngx_http.h>
}
#include "package_format.h"
static const int DEFAULT_STR_BUF_SIZE = 20;
static pkg_ctx_t* g_ctx = NULL;
// global buffer size
static int g_pbmsgpack_buf_size = 16 * 1024;
static const int g_max_content_type_size = 1024;
static ngx_str_t g_data_type_header = ngx_string("http_x_bd_data_type");
static ngx_str_t g_out_data_type_header = ngx_string("sent_http_x_bd_data_type");
static ngx_str_t g_protobuf_str = ngx_string("protobuf");
static ngx_str_t g_msgpack_str = ngx_string("msgpack");
static ngx_str_t g_msgpack_nginx_str = ngx_string("msgpack,nginx");
static ngx_str_t g_protobuf_nginx_str = ngx_string("protobuf,nginx");
static ngx_str_t g_proto_conf_path = ngx_string("./conf/protos");
static ngx_str_t g_content_type_str = ngx_string("http_content_type");
static const char* const g_bound_str = "boundary=";
static const char* const g_multipart_form = "multipart/form-data";
static const char* const g_tag_name = "\"data";
typedef struct _ngx_http_pb_msgpack_transfer_loc_conf_t
{
ngx_flag_t pbmsgpack_transfer_flag;
ngx_flag_t pbmsgpack_filter_flag;
}ngx_http_pb_msgpack_transfer_loc_conf_t;
typedef struct _ngx_http_pb_msgpack_transfer_main_conf_t
{
ngx_flag_t module_flag;
ngx_str_t proto_conf_path;
ngx_int_t pbmsgpack_buf_size;
}ngx_http_pb_msgpack_transfer_main_conf_t;
typedef struct _ngx_http_pbmsgpack_transfer_ctx_t
{
ngx_int_t is_waiting_body;
ngx_int_t is_header_illegal;
ngx_table_elt_t* data_type_header;
ngx_int_t has_filter_deal;
ngx_table_elt_t* sent_data_type_header;
ngx_table_elt_t* content_type_header;
u_char* pb_buf;
ngx_uint_t pb_buf_pos;
ngx_uint_t pb_buf_size;
u_char* msgpack_buf;
ngx_uint_t msgpack_pos;
ngx_uint_t msgpack_buf_size;
ngx_uint_t transfer_cost;
ngx_uint_t filter_cost;
}ngx_http_pbmsgpack_transfer_ctx_t;
enum PBMSGPACK_VAR_TYPE
{
PB_VAR_TYPE_TRANSFER_COST = 0,
PB_VAR_TYPE_FILTER_COST,
PB_VAR_TYPE_TRANSFER_SIZE ,
PB_VAR_TYPE_FILTER_SIZE,
};
static ngx_command_t ngx_http_pb_msgpack_transfer_commands[] =
{
{
ngx_string("pbmsgpack_flag"),
NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1 ,
ngx_conf_set_flag_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_pb_msgpack_transfer_main_conf_t,module_flag),
NULL,
},
{
ngx_string("pbmsgpack_conf_path"),
NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1 ,
ngx_conf_set_str_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_pb_msgpack_transfer_main_conf_t,proto_conf_path),
NULL,
},
{
ngx_string("pbmsgpack_buf_size"),
NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE1 ,
ngx_conf_set_size_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_pb_msgpack_transfer_main_conf_t,pbmsgpack_buf_size),
NULL,
},
{
ngx_string("pbmsgpack_transfer_flag"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE1 ,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_pb_msgpack_transfer_loc_conf_t,pbmsgpack_transfer_flag),
NULL,
},
{
ngx_string("pbmsgpack_filter_flag"),
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE1 ,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_pb_msgpack_transfer_loc_conf_t,pbmsgpack_filter_flag),
NULL,
},
ngx_null_command
};
static ngx_int_t ngx_http_pb_msgpack_transfer_init(ngx_conf_t* cf);
static void* ngx_http_pb_msgpack_transfer_create_loc_conf(ngx_conf_t* cf);
static void* ngx_http_pb_msgpack_transfer_create_main_conf(ngx_conf_t* cf);
static ngx_int_t ngx_http_pb_msgpack_transfer_handler(ngx_http_request_t* r);
static void ngx_http_transfer_body(ngx_http_request_t* r);
static void ngx_http_pb_msgpack_exit_worker(ngx_cycle_t* cycle);
static char* ngx_http_pb_msgpack_transfer_merge_loc_conf(ngx_conf_t* cf, void* parent, void * child);
static ngx_int_t ngx_http_pb_msgpack_body_filter(ngx_http_request_t* r , ngx_chain_t* in);
static ngx_int_t ngx_http_pb_msgpack_header_filter(ngx_http_request_t* r );
static ngx_table_elt_t* ngx_http_pbmsgpack_get_header(ngx_http_request_t* r, ngx_str_t* str_header);
static ngx_table_elt_t* ngx_http_pbmsgpack_get_res_header(ngx_http_request_t* r, ngx_str_t* str_header);
static int get_multipart_form_header_info(ngx_http_request_t* r, ngx_table_elt_t* elt, char* bound);
static int get_multipart_form_info(char* info, int size,const char* bound, const char* tag_name, int& start_pos, int& end_pos);
static ngx_int_t ngx_http_pbmsgpack_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_pbmsgpack_var(ngx_http_request_t *r,ngx_http_variable_value_t *v, uintptr_t data);
static unsigned long int ngx_http_pbmsgpack_get_time_us();
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 ngx_http_variable_t ngx_http_pbmsgpack_vars[] =
{
{ ngx_string("pbmsgpack_transfer_cost"),NULL,ngx_http_pbmsgpack_var,
PB_VAR_TYPE_TRANSFER_COST, NGX_HTTP_VAR_NOHASH, 0 },
{ ngx_string("pbmsgpack_filter_cost"),NULL,ngx_http_pbmsgpack_var,
PB_VAR_TYPE_FILTER_COST, NGX_HTTP_VAR_NOHASH, 0 },
{ ngx_string("pbmsgpack_transfer_size"),NULL,ngx_http_pbmsgpack_var,
PB_VAR_TYPE_TRANSFER_SIZE, NGX_HTTP_VAR_NOHASH, 0 },
{ ngx_string("pbmsgpack_filter_size"),NULL,ngx_http_pbmsgpack_var,
PB_VAR_TYPE_FILTER_SIZE, NGX_HTTP_VAR_NOHASH, 0 },
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
static ngx_http_module_t ngx_http_pb_msgpack_transfer_module_ctx =
{
ngx_http_pbmsgpack_add_variables, /* preconfiguration */
ngx_http_pb_msgpack_transfer_init, /* postconfiguration */
ngx_http_pb_msgpack_transfer_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_pb_msgpack_transfer_create_loc_conf, /* create location configration */
ngx_http_pb_msgpack_transfer_merge_loc_conf /* merge location configration */
};
ngx_module_t ngx_http_pb_msgpack_transfer_module =
{
NGX_MODULE_V1,
&ngx_http_pb_msgpack_transfer_module_ctx, /* module context */
ngx_http_pb_msgpack_transfer_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_pb_msgpack_exit_worker, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static void* ngx_http_pb_msgpack_transfer_create_main_conf(ngx_conf_t* cf)
{
ngx_http_pb_msgpack_transfer_main_conf_t* conf = NULL;
conf = (ngx_http_pb_msgpack_transfer_main_conf_t*) ngx_pcalloc(cf->pool, sizeof(ngx_http_pb_msgpack_transfer_main_conf_t));
if(NULL == conf)
{
return NULL;
}
conf->module_flag = NGX_CONF_UNSET;
conf->pbmsgpack_buf_size = NGX_CONF_UNSET;
conf->proto_conf_path.data = NULL;
conf->proto_conf_path.len = NULL;
return conf;
}
static void* ngx_http_pb_msgpack_transfer_create_loc_conf(ngx_conf_t* cf)
{
ngx_http_pb_msgpack_transfer_loc_conf_t* loc_conf = NULL;
loc_conf = (ngx_http_pb_msgpack_transfer_loc_conf_t*) ngx_pcalloc(cf->pool, sizeof(ngx_http_pb_msgpack_transfer_loc_conf_t));
// pcalloc make
// loc_conf->pbmsgpack_transfer_flag = 0
if(NULL == loc_conf)
{
return NULL;
}
loc_conf->pbmsgpack_transfer_flag = NGX_CONF_UNSET;
loc_conf->pbmsgpack_filter_flag = NGX_CONF_UNSET;
return loc_conf;
}
static ngx_int_t ngx_http_pb_msgpack_transfer_init(ngx_conf_t *cf)
{
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
ngx_http_pb_msgpack_transfer_main_conf_t* pmtmcf;
pmtmcf = (ngx_http_pb_msgpack_transfer_main_conf_t*) ngx_http_conf_get_module_main_conf(cf, ngx_http_pb_msgpack_transfer_module);
// default conf path
if(NULL == pmtmcf->proto_conf_path.data)
{
pmtmcf->proto_conf_path = g_proto_conf_path;
}
// default buffer size
if(NGX_CONF_UNSET == pmtmcf->pbmsgpack_buf_size)
{
pmtmcf->pbmsgpack_buf_size = g_pbmsgpack_buf_size;
}
else
{
g_pbmsgpack_buf_size = pmtmcf->pbmsgpack_buf_size;
}
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, cf->log, 0, "pbmsgpack main conf info! flag:%d, path:%V, size:%u ", pmtmcf->module_flag, & pmtmcf->proto_conf_path, pmtmcf->pbmsgpack_buf_size);
if(NGX_CONF_UNSET == pmtmcf->module_flag || 0 == pmtmcf->module_flag)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "ngx_http_pb_msgpack_transfer_main_conf_t flag is 0, donot add hook !");
return NGX_OK;
}
//设置钩子
cmcf = (ngx_http_core_main_conf_t *)ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = (ngx_http_handler_pt *)ngx_array_push(&cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers);
if(NULL == h)
{
return NGX_ERROR;
}
*h = ngx_http_pb_msgpack_transfer_handler;
// output filter
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_pb_msgpack_body_filter;
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_pb_msgpack_header_filter;
if(pmtmcf->proto_conf_path.len >= 1024)
{
ngx_log_error(NGX_LOG_ALERT, cf->log, 0 , "the conf path is too long![path:%V]", & pmtmcf->proto_conf_path);
return NGX_ERROR;
}
char pck_conf_path[1024];
memcpy(pck_conf_path, pmtmcf->proto_conf_path.data, pmtmcf->proto_conf_path.len);
pck_conf_path[pmtmcf->proto_conf_path.len] = '\0';
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "nginx try to init pkg context");
if(NULL != g_ctx)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "pbmsgpack g_ctx is not NULL, free it!");
pkg_ctx_free(g_ctx);
g_ctx = NULL;
}
if(NULL != g_pkg_ctx)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, "pbmsgpack g_pkg_ctx is not NULL, free it!");
global_pkg_ctx_free();
g_pkg_ctx = NULL;
}
// re-init protobuf-msgpack context
ngx_int_t ret = init_global_pkg_ctx(pck_conf_path);
if(0 != ret)
{
ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "init_global_pkg_ctx failed![ret:%d]",ret);
return NGX_ERROR;
}
if(NULL == g_ctx)
{
g_ctx = pkg_ctx_new(g_pkg_ctx);
if(NULL == g_ctx)
{
ngx_log_error(NGX_LOG_ALERT, cf->log, 0, "pkg_ctx_new failed!");
return NGX_ERROR;
}
}
return NGX_OK;
}
static ngx_int_t ngx_http_pb_msgpack_transfer_handler(ngx_http_request_t *r)
{
ngx_int_t rc = 0;
ngx_http_pb_msgpack_transfer_loc_conf_t* loc_conf = NULL;
ngx_http_pbmsgpack_transfer_ctx_t* ctx = NULL;
loc_conf = (ngx_http_pb_msgpack_transfer_loc_conf_t*) ngx_http_get_module_loc_conf(r, ngx_http_pb_msgpack_transfer_module);
if(NULL == loc_conf )
{
return NGX_DECLINED;
}
if(0 == loc_conf->pbmsgpack_transfer_flag || NGX_CONF_UNSET == loc_conf->pbmsgpack_transfer_flag )
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP,r->connection->log,0, "pbmsgpack transfer flag is off or unset");
return NGX_DECLINED;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pbmsgpack transfer try process body");
ctx = (ngx_http_pbmsgpack_transfer_ctx_t*) ngx_http_get_module_ctx(r, ngx_http_pb_msgpack_transfer_module);
if(NULL != ctx && 1 == ctx->is_header_illegal)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "head is illegal, skip it!");
return NGX_DECLINED;
}
if(NULL == ctx)
{
ctx = (ngx_http_pbmsgpack_transfer_ctx_t* )ngx_pcalloc(r->pool, sizeof(ngx_http_pbmsgpack_transfer_ctx_t) );
if(NULL == ctx)
{
return NGX_ERROR;
}
if(NULL == ctx->data_type_header)
{
//check whether transfer the protobuf to msgpack
ngx_table_elt_t* data_type_elt = ngx_http_pbmsgpack_get_header(r, & g_data_type_header);
ngx_table_elt_t* content_type_elt = ngx_http_pbmsgpack_get_header(r, & g_content_type_str);
if( (NULL == data_type_elt
|| data_type_elt->value.len != g_protobuf_str.len
|| 0 != memcmp(data_type_elt->value.data, g_protobuf_str.data, g_protobuf_str.len))
|| ( NULL == content_type_elt))
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "unexpected request header! skip it!");
if(NULL == data_type_elt)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pbmsgpack empty header unexpected request header! skip it!");
}
else
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pbmsgpack get http_x_bd_data_type result:%V", & data_type_elt->value);
}
ctx->is_header_illegal = 1;
}
else
{
ctx->is_header_illegal = 0;
ctx->data_type_header = data_type_elt;
ctx->content_type_header = content_type_elt;
}
}
ctx->is_waiting_body = 1;
ngx_http_set_ctx(r, ctx, ngx_http_pb_msgpack_transfer_module);
}
else
{
if(NULL != ctx && 1 == ctx->is_header_illegal)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "head is illegal, skip it!");
return NGX_DECLINED;
}
// if already read the whole request body, skip it
if(0 == ctx->is_waiting_body)
{
return NGX_DECLINED;
}
else
{
return NGX_DONE;
}
}
if(NULL != ctx && 1 == ctx->is_header_illegal)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "head is illegal, skip it!");
return NGX_DECLINED;
}
// try read request body
rc = ngx_http_read_client_request_body(r, ngx_http_transfer_body);
if( NGX_ERROR ==rc || rc >= NGX_HTTP_SPECIAL_RESPONSE)
{
return rc;
}
else if(NGX_AGAIN == rc)
{
return NGX_DONE;
}
return NGX_DECLINED;
}
static ngx_table_elt_t* ngx_http_pbmsgpack_get_res_header(ngx_http_request_t* r, ngx_str_t* var)
{
u_char ch;
ngx_uint_t i, n;
ngx_table_elt_t *header;
int prefix = sizeof("sent_http_") - 1;
ngx_list_part_t* part = (ngx_list_part_t*)& r->headers_out.headers.part;
header = (ngx_table_elt_t*)part->elts;
for (i = 0; /* void */ ; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = (ngx_table_elt_t*)part->elts;
i = 0;
}
if (header[i].hash == 0) {
continue;
}
for (n = 0; n + prefix < var->len && n < header[i].key.len; n++) {
ch = header[i].key.data[n];
if (ch >= 'A' && ch <= 'Z') {
ch |= 0x20;
} else if (ch == '-') {
ch = '_';
}
if (var->data[n + prefix] != ch) {
break;
}
}
if (n + prefix == var->len && n == header[i].key.len) {
/*
*v->len = header[i].value.len;
*v->valid = 1;
*v->no_cacheable = 0;
*v->not_found = 0;
*v->data = header[i].value.data;
*/
return & header[i];
}
}
return NULL;
}
static ngx_table_elt_t* ngx_http_pbmsgpack_get_header(ngx_http_request_t* r, ngx_str_t* var)
{
u_char ch;
ngx_uint_t i, n;
ngx_table_elt_t *header;
int prefix = sizeof("http_") - 1;
ngx_list_part_t* part = (ngx_list_part_t*)& r->headers_in.headers.part;
header = (ngx_table_elt_t*)part->elts;
for (i = 0; /* void */ ; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = (ngx_table_elt_t*)part->elts;
i = 0;
}
if (header[i].hash == 0) {
continue;
}
for (n = 0; n + prefix < var->len && n < header[i].key.len; n++) {
ch = header[i].key.data[n];
if (ch >= 'A' && ch <= 'Z') {
ch |= 0x20;
} else if (ch == '-') {
ch = '_';
}
if (var->data[n + prefix] != ch) {
break;
}
}
if (n + prefix == var->len && n == header[i].key.len) {
/*
*v->len = header[i].value.len;
*v->valid = 1;
*v->no_cacheable = 0;
*v->not_found = 0;
*v->data = header[i].value.data;
*/
return & header[i];
}
}
return NULL;
}
static int ngx_http_pbmsgpack_get_cmd ( ngx_http_request_t* r)
{
static ngx_str_t cmd_var_name = ngx_string("arg_cmd");
ngx_http_variable_value_t* res_value;
if(NULL == (res_value = ngx_http_get_variable(r,&cmd_var_name,0)))
{
return -1;
}
if(res_value->valid && 0 == res_value->not_found)
{
int cmd = ngx_atoi(res_value->data, res_value->len);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "[pagecache] get varaibale success![cmd:%d]" , cmd);
return cmd;
}
else
{
return -1;
}
return -1;
}
static void ngx_http_transfer_body(ngx_http_request_t* r)
{
// already get the whole request body
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "enter ngx_http_transfer_body");
unsigned long int start_time, end_time;
start_time = ngx_http_pbmsgpack_get_time_us();
// first, set ctx read request flag to 0
ngx_http_pbmsgpack_transfer_ctx_t* ctx = NULL;
ctx = (ngx_http_pbmsgpack_transfer_ctx_t*) ngx_http_get_module_ctx(r, ngx_http_pb_msgpack_transfer_module);
if(NULL == ctx)
{
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "[req_body_transfer] unexpected emmpty ctx!");
ngx_http_finalize_request(r,NGX_DONE);
return;
}
ctx->is_waiting_body = 0;
// second, process data
ngx_chain_t* bufs = r->request_body->bufs;
if(NULL == bufs)
{
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "request bufs is NULL!");
ngx_http_finalize_request(r,NGX_DONE);
return;
}
//try deal
//first , get boundary info from header;
char bound[g_max_content_type_size];
int ret = get_multipart_form_header_info(r, ctx->content_type_header, bound);
if(0 != ret)
{
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "pb msgpack get_multipart_form_header_info failed!");
ngx_http_finalize_request(r,NGX_DONE);
return ;
}
// check cmd
int cmd = ngx_http_pbmsgpack_get_cmd(r);
if(cmd <= 0)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "illegal cmd is: %d", cmd);
ngx_http_finalize_request(r,NGX_DONE);
return;
}
ngx_int_t total_size = 0;
ngx_int_t buf_num = 0;
ngx_buf_t* temp_buf = NULL;
ngx_chain_t* temp_chain = bufs;
ctx->pb_buf_size = ctx->msgpack_buf_size = g_pbmsgpack_buf_size;
ctx->pb_buf_pos = ctx->msgpack_pos = 0;
ctx->pb_buf = (u_char*)ngx_pcalloc(r->pool, g_pbmsgpack_buf_size);
// re-use pb buffer,
ctx->msgpack_buf = ctx->pb_buf;
while(NULL != temp_chain)
{
temp_buf = temp_chain->buf;
if(NULL != temp_buf)
{
total_size += (int)(temp_buf->last - temp_buf->pos);
++ buf_num;
}
temp_chain = temp_chain->next;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "[req_body_transfer]total requst body size:%d ,bufs:%d", total_size, buf_num);
if(total_size >= g_pbmsgpack_buf_size)
{
//TODO
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "illegal request body size![now:%d][limit:%d]", total_size, g_pbmsgpack_buf_size);
ngx_http_finalize_request(r,NGX_DONE);
return;
}
temp_chain = bufs;
while(NULL != temp_chain)
{
temp_buf = temp_chain->buf;
if(NULL != temp_buf)
{
total_size = (int)(temp_buf->last - temp_buf->pos);
memcpy(ctx->pb_buf + ctx->pb_buf_pos , temp_buf->pos, total_size);
ctx->pb_buf_pos += total_size;
}
temp_chain = temp_chain->next;
}
//get the whole request body
ctx->pb_buf[ ctx->pb_buf_pos ] = '\0';
int data_start_pos;
int data_end_pos;
ret = get_multipart_form_info( (char*)ctx->pb_buf, ctx->pb_buf_pos,bound, g_tag_name, data_start_pos, data_end_pos);
if(0 != ret)
{
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "pb msgpack get_multipart_form_info failed!");
ngx_http_finalize_request(r,NGX_DONE);
return ;
}
// 节省空间,拷贝两次
// 上面的pos是闭区间
int temp_copy_size = ctx->pb_buf_pos - data_end_pos + 1;
int temp_src_copy_pos = data_end_pos;
int temp_dst_copy_pos = g_pbmsgpack_buf_size - temp_copy_size ;
int raw_data_size = data_end_pos - data_start_pos ;
int old_data_size = raw_data_size;
memmove(ctx->pb_buf + temp_dst_copy_pos, ctx->pb_buf + temp_src_copy_pos, temp_copy_size );
ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pb msgpack post body start:%d,end:%d,copy:%d,new_copy_pos:%d,old_data:%d,total:%d", data_start_pos, data_end_pos, temp_copy_size, temp_dst_copy_pos, old_data_size, ctx->pb_buf_pos);
ret = pkg_pb2msgpack(g_ctx, ctx->pb_buf + data_start_pos, & raw_data_size, g_pbmsgpack_buf_size - temp_copy_size - data_start_pos, cmd, PKG_UPSTREAM);
if(0 != ret)
{
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "post body trans failed! pkg_pb2msgpack return failed!");
ngx_http_finalize_request(r,NGX_DONE);
return;
}
temp_src_copy_pos += raw_data_size - old_data_size;
memmove(ctx->pb_buf + temp_src_copy_pos, ctx->pb_buf + temp_dst_copy_pos, temp_copy_size);
ctx->pb_buf_pos += raw_data_size - old_data_size;
ngx_log_debug6(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pb msgpack post body start:%d,end:%d,copy:%d,new_copy_pos:%d,new_data:%d,total:%d", data_start_pos, data_end_pos, temp_copy_size, temp_dst_copy_pos, raw_data_size, ctx->pb_buf_pos);
// first , reset the request body info
ngx_chain_t* test_chain = ngx_alloc_chain_link(r->pool);
ngx_buf_t* test_buf = (ngx_buf_t*)ngx_calloc_buf(r->pool);
test_buf->pos = test_buf->start = ctx->pb_buf;
test_buf->last = test_buf->end = (u_char*) (ctx->pb_buf + ctx->pb_buf_pos);
test_buf->temporary = 1;
test_chain->buf = test_buf;
test_chain->next = NULL;
r->request_body->bufs = test_chain;
r->headers_in.content_length_n = ctx->pb_buf_pos;
// second , reset the requset header info , include content-length and x_bd_data_type
char* str_content_length = (char*)ngx_pcalloc(r->pool, DEFAULT_STR_BUF_SIZE);
int str_size = snprintf(str_content_length, DEFAULT_STR_BUF_SIZE, "%u", r->headers_in.content_length_n);
r->headers_in.content_length->value.data = (u_char*) str_content_length;
r->headers_in.content_length->value.len = str_size;
ctx->data_type_header->value = g_msgpack_str;
end_time = ngx_http_pbmsgpack_get_time_us();
ctx->transfer_cost = end_time - start_time;
ngx_http_finalize_request(r,NGX_DONE);
}
static char* ngx_http_pb_msgpack_transfer_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child)
{
ngx_http_pb_msgpack_transfer_loc_conf_t* prev = (ngx_http_pb_msgpack_transfer_loc_conf_t*) parent;
ngx_http_pb_msgpack_transfer_loc_conf_t* conf = (ngx_http_pb_msgpack_transfer_loc_conf_t*) child;
ngx_conf_merge_value(conf->pbmsgpack_transfer_flag, prev->pbmsgpack_transfer_flag, 0);
ngx_conf_merge_value(conf->pbmsgpack_filter_flag, prev->pbmsgpack_filter_flag, 0);
return NGX_CONF_OK;
}
static ngx_int_t ngx_http_pb_msgpack_header_filter(ngx_http_request_t* r )
{
if(r->headers_out.status != NGX_HTTP_OK)
{
return ngx_http_next_header_filter(r);
}
ngx_http_pb_msgpack_transfer_loc_conf_t* loc_conf = NULL;
loc_conf = (ngx_http_pb_msgpack_transfer_loc_conf_t*) ngx_http_get_module_loc_conf(r, ngx_http_pb_msgpack_transfer_module);
// flag is unset/0 , skip deal it
if(0 == loc_conf->pbmsgpack_filter_flag || NGX_CONF_UNSET == loc_conf->pbmsgpack_filter_flag )
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "output transfer flag is 0!");
return ngx_http_next_header_filter(r);
}
if( r == r->main)
{
ngx_http_clear_content_length(r);
}
return ngx_http_next_header_filter(r);
}
static ngx_int_t ngx_http_pb_msgpack_body_filter(ngx_http_request_t* r , ngx_chain_t* in)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "enter pb msgpack body filter!");
ngx_http_pb_msgpack_transfer_loc_conf_t* loc_conf = NULL;
ngx_http_pbmsgpack_transfer_ctx_t* ctx = NULL;
int req_cmd = -1;
int ret ;
loc_conf = (ngx_http_pb_msgpack_transfer_loc_conf_t*) ngx_http_get_module_loc_conf(r, ngx_http_pb_msgpack_transfer_module);
// flag is unset/0 , skip deal it
if(0 == loc_conf->pbmsgpack_filter_flag || NGX_CONF_UNSET == loc_conf->pbmsgpack_filter_flag )
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "output transfer flag is 0!");
return ngx_http_next_body_filter(r, in);
}
// response is failed, skip it
if(r->headers_out.status != NGX_HTTP_OK)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "Bypassed");
return ngx_http_next_body_filter(r, in);
}
ctx = (ngx_http_pbmsgpack_transfer_ctx_t*) ngx_http_get_module_ctx(r, ngx_http_pb_msgpack_transfer_module);
if(NULL == ctx)
{
ctx = (ngx_http_pbmsgpack_transfer_ctx_t* )ngx_pcalloc(r->pool, sizeof(ngx_http_pbmsgpack_transfer_ctx_t) );
if(NULL == ctx)
{
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "ngx_pcalloc ngx_http_pbmsgpack_transfer_ctx_t failed");
return ngx_http_next_body_filter(r,in);
}
ctx->msgpack_buf_size = ctx->pb_buf_size = g_pbmsgpack_buf_size;
ctx->msgpack_buf = (u_char*) ngx_pcalloc(r->pool, ctx->msgpack_buf_size);
ctx->msgpack_pos = 0;
if(NULL == ctx->msgpack_buf)
{
ngx_log_error(NGX_LOG_ALERT, r->connection->log,0, "ngx_pcalloc ngx_http_pbmsgpack_transfer_ctx_t failed");
return ngx_http_next_body_filter(r,in);
}
ngx_http_set_ctx(r, ctx, ngx_http_pb_msgpack_transfer_module);
}
else
{
if(NULL == ctx->msgpack_buf)
{
ctx->msgpack_buf_size = ctx->pb_buf_size = g_pbmsgpack_buf_size;
ctx->msgpack_buf = (u_char*) ngx_pcalloc(r->pool, ctx->msgpack_buf_size);
ctx->msgpack_pos = 0;
}
if(NULL == ctx->msgpack_buf)
{
ngx_log_error(NGX_LOG_ALERT, r->connection->log,0, "ngx_pcalloc ngx_http_pbmsgpack_transfer_ctx_t failed");
return ngx_http_next_body_filter(r,in);
}
}
if( 1 == ctx->has_filter_deal)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pbmsgpack filter transfer has already deal! skip it!");
return ngx_http_next_body_filter(r,in);
}
if(NULL == ctx->sent_data_type_header)
{
// check whether the res data type is msgpack
ngx_table_elt_t* res_data_type = ngx_http_pbmsgpack_get_res_header(r, & g_out_data_type_header);
ctx->sent_data_type_header = res_data_type;
if(NULL == res_data_type)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pbmsgpack sent_http_x_data_type is NULL, skip deal it!");
ctx->has_filter_deal = 1;
return ngx_http_next_body_filter(r, in);
}
else if( res_data_type->value.len != g_msgpack_nginx_str.len || 0 != memcmp(res_data_type->value.data, g_msgpack_nginx_str.data, g_msgpack_str.len))
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pbmsgpack sent_http_x_data_type is: %V, skip it", & res_data_type->value);
ctx->has_filter_deal = 1;
return ngx_http_next_body_filter(r, in);
}
}
req_cmd = ngx_http_pbmsgpack_get_cmd(r);
if( -1 == req_cmd)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0 ,"ngx_http_pbmsgpack_get_cmd failed, skip it");
return ngx_http_next_body_filter(r, in);
}
ngx_chain_t *it;
for(it = in; it; it = it->next)
{
if(it->buf->last == it->buf->pos)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pb msgpack filter skip empty buf");
}
else
{
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pb msgpack output filter %db\n", (int)(it->buf->last - it->buf->pos));
if(ctx->msgpack_pos + it->buf->last - it->buf->pos >= ctx->msgpack_buf_size)
{
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "pbmsgpack output body size is too large!");
return ngx_http_next_body_filter(r, in);
}
memcpy(ctx->msgpack_buf + ctx->msgpack_pos , it->buf->pos, it->buf->last - it->buf->pos);
ctx->msgpack_pos += it->buf->last - it->buf->pos;
}
// mark as sent to client so nginx is happy
it->buf->pos = it->buf->last;
if(it->buf->last_buf)
{
unsigned long int start_time, end_time;
start_time = ngx_http_pbmsgpack_get_time_us();
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pb msgpack output filter total size: %d ", ctx->msgpack_pos);
//it->buf->last_buf = 0;
ret = pkg_msgpack2pb(g_ctx, ctx->msgpack_buf, (int*) & ctx->msgpack_pos, ctx->msgpack_buf_size, req_cmd, PKG_DOWNSTREAM);
if(0 != ret)
{
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "pkg_msgpack2pb failed! [ret:%d]", ret);
// ctx->sent_data_type_header = g_protobuf_str;
// we do not skip to the next body filter
// because if failed, we will still send the msgpack info to client
ctx->has_filter_deal = 1;
return ngx_http_next_body_filter(r, in);
}
else
{
ctx->sent_data_type_header->value = g_protobuf_nginx_str;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "pb msgpack output filter new size: %d ", ctx->msgpack_pos);
// mark the filter deal flag
ctx->has_filter_deal = 1;
/*
* ngx_chain_t * empty_chain = ngx_alloc_chain_link(r->pool);
* if(NULL == empty_chain)
* {
* return NGX_ERROR;
* }
* empty_chain->next = NULL;
*
* ngx_buf_t* empty_buf = (ngx_buf_t*) ngx_calloc_buf(r->pool);
* empty_buf->memory = 1;
* empty_buf->last_buf = 1;
* empty_buf->pos = empty_buf->last = ctx->msgpack_buf;
* empty_chain->buf = empty_buf;
*/