-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_stream_js_module.c
3622 lines (2783 loc) · 94.3 KB
/
ngx_stream_js_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) Roman Arutyunyan
* Copyright (C) Dmitry Volyntsev
* Copyright (C) NGINX, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
#include "ngx_js.h"
typedef struct ngx_stream_js_ctx_s ngx_stream_js_ctx_t;
typedef struct {
NGX_JS_COMMON_LOC_CONF;
ngx_str_t access;
ngx_str_t preread;
ngx_str_t filter;
} ngx_stream_js_srv_conf_t;
typedef struct {
njs_opaque_value_t function;
ngx_uint_t data_type;
} ngx_stream_js_ev_t;
typedef struct {
ngx_stream_conf_ctx_t *conf_ctx;
ngx_connection_t *connection;
uint8_t *worker_affinity;
/**
* fd is used for event debug and should be at the same position
* as in ngx_connection_t: after a 3rd pointer.
*/
ngx_socket_t fd;
ngx_str_t method;
ngx_msec_t interval;
ngx_msec_t jitter;
ngx_log_t log;
ngx_event_t event;
} ngx_js_periodic_t;
struct ngx_stream_js_ctx_s {
NGX_JS_COMMON_CTX;
ngx_buf_t *buf;
ngx_chain_t **last_out;
ngx_chain_t *free;
ngx_chain_t *upstream_busy;
ngx_chain_t *downstream_busy;
ngx_int_t status;
ngx_int_t (*run_event)(ngx_stream_session_t *s,
ngx_stream_js_ctx_t *ctx,
ngx_stream_js_ev_t *event,
ngx_uint_t from_upstream);
ngx_int_t (*body_filter)(ngx_stream_session_t *s,
ngx_stream_js_ctx_t *ctx,
ngx_chain_t *in,
ngx_uint_t from_upstream);
#define NGX_JS_EVENT_UPLOAD 0
#define NGX_JS_EVENT_DOWNLOAD 1
#define NGX_JS_EVENT_MAX 2
ngx_stream_js_ev_t events[NGX_JS_EVENT_MAX];
unsigned filter:1;
unsigned in_progress:1;
ngx_js_periodic_t *periodic;
};
#if (NJS_HAVE_QUICKJS)
typedef struct {
ngx_str_t name;
ngx_uint_t data_type;
ngx_uint_t id;
} ngx_stream_qjs_event_t;
typedef struct {
ngx_stream_session_t *session;
JSValue callbacks[NGX_JS_EVENT_MAX];
} ngx_stream_qjs_session_t;
#endif
#define ngx_stream_pending(ctx) \
(ngx_js_ctx_pending(ctx) || ngx_stream_js_pending_events(ctx))
static ngx_int_t ngx_stream_js_access_handler(ngx_stream_session_t *s);
static ngx_int_t ngx_stream_js_preread_handler(ngx_stream_session_t *s);
static ngx_int_t ngx_stream_js_phase_handler(ngx_stream_session_t *s,
ngx_str_t *name);
static ngx_int_t ngx_stream_js_body_filter(ngx_stream_session_t *s,
ngx_chain_t *in, ngx_uint_t from_upstream);
static ngx_int_t ngx_stream_njs_body_filter(ngx_stream_session_t *s,
ngx_stream_js_ctx_t *ctx, ngx_chain_t *in, ngx_uint_t from_upstream);
static ngx_int_t ngx_stream_js_next_filter(ngx_stream_session_t *s,
ngx_stream_js_ctx_t *ctx, ngx_chain_t *out, ngx_uint_t from_upstream);
static ngx_int_t ngx_stream_js_variable_set(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_stream_js_variable_var(ngx_stream_session_t *s,
ngx_stream_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_stream_js_init_vm(ngx_stream_session_t *s,
njs_int_t proto_id);
static ngx_int_t ngx_stream_js_pending_events(ngx_stream_js_ctx_t *ctx);
static void ngx_stream_js_drop_events(ngx_stream_js_ctx_t *ctx);
static void ngx_stream_js_cleanup(void *data);
static ngx_int_t ngx_stream_js_run_event(ngx_stream_session_t *s,
ngx_stream_js_ctx_t *ctx, ngx_stream_js_ev_t *event,
ngx_uint_t from_upstream);
static ngx_stream_js_ev_t *ngx_stream_js_event(ngx_stream_session_t *s,
njs_str_t *event);
static njs_int_t ngx_stream_js_ext_get_remote_address(njs_vm_t *vm,
njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
njs_value_t *retval);
static njs_int_t ngx_stream_js_ext_done(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
static njs_int_t ngx_stream_js_ext_on(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
static njs_int_t ngx_stream_js_ext_off(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
static njs_int_t ngx_stream_js_ext_send(njs_vm_t *vm, njs_value_t *args,
njs_uint_t nargs, njs_index_t from_upstream, njs_value_t *retval);
static njs_int_t ngx_stream_js_ext_set_return_value(njs_vm_t *vm,
njs_value_t *args, njs_uint_t nargs, njs_index_t unused,
njs_value_t *retval);
static njs_int_t ngx_stream_js_ext_variables(njs_vm_t *vm,
njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
njs_value_t *retval);
static njs_int_t ngx_stream_js_periodic_variables(njs_vm_t *vm,
njs_object_prop_t *prop, njs_value_t *value, njs_value_t *setval,
njs_value_t *retval);
#if (NJS_HAVE_QUICKJS)
static JSValue ngx_stream_qjs_ext_to_string_tag(JSContext *cx,
JSValueConst this_val);
static JSValue ngx_stream_qjs_ext_done(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic);
static JSValue ngx_stream_qjs_ext_log(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv, int level);
static JSValue ngx_stream_qjs_ext_on(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv);
static JSValue ngx_stream_qjs_ext_off(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv);
static JSValue ngx_stream_qjs_ext_periodic_to_string_tag(JSContext *cx,
JSValueConst this_val);
static JSValue ngx_stream_qjs_ext_periodic_variables(JSContext *cx,
JSValueConst this_val, int type);
static JSValue ngx_stream_qjs_ext_remote_address(JSContext *cx,
JSValueConst this_val);
static JSValue ngx_stream_qjs_ext_send(JSContext *cx, JSValueConst this_val,
int argc, JSValueConst *argv, int from_upstream);
static JSValue ngx_stream_qjs_ext_set_return_value(JSContext *cx,
JSValueConst this_val, int argc, JSValueConst *argv);
static JSValue ngx_stream_qjs_ext_variables(JSContext *cx,
JSValueConst this_val, int type);
static JSValue ngx_stream_qjs_ext_uint(JSContext *cx, JSValueConst this_val,
int offset);
static JSValue ngx_stream_qjs_ext_flag(JSContext *cx, JSValueConst this_val,
int mask);
static int ngx_stream_qjs_variables_own_property(JSContext *cx,
JSPropertyDescriptor *pdesc, JSValueConst obj, JSAtom prop);
static int ngx_stream_qjs_variables_set_property(JSContext *cx,
JSValueConst obj, JSAtom atom, JSValueConst value, JSValueConst receiver,
int flags);
static int ngx_stream_qjs_variables_define_own_property(JSContext *cx,
JSValueConst obj, JSAtom prop, JSValueConst value, JSValueConst getter,
JSValueConst setter, int flags);
static ngx_int_t ngx_stream_qjs_run_event(ngx_stream_session_t *s,
ngx_stream_js_ctx_t *ctx, ngx_stream_js_ev_t *event,
ngx_uint_t from_upstream);
static ngx_int_t ngx_stream_qjs_body_filter(ngx_stream_session_t *s,
ngx_stream_js_ctx_t *ctx, ngx_chain_t *in, ngx_uint_t from_upstream);
static ngx_stream_session_t *ngx_stream_qjs_session(JSValueConst val);
static JSValue ngx_stream_qjs_session_make(JSContext *cx, ngx_int_t proto_id,
ngx_stream_session_t *s);
static void ngx_stream_qjs_session_finalizer(JSRuntime *rt, JSValue val);
static void ngx_stream_qjs_periodic_finalizer(JSRuntime *rt, JSValue val);
#endif
static ngx_pool_t *ngx_stream_js_pool(ngx_stream_session_t *s);
static ngx_resolver_t *ngx_stream_js_resolver(ngx_stream_session_t *s);
static ngx_msec_t ngx_stream_js_resolver_timeout(ngx_stream_session_t *s);
static ngx_msec_t ngx_stream_js_fetch_timeout(ngx_stream_session_t *s);
static size_t ngx_stream_js_buffer_size(ngx_stream_session_t *s);
static size_t ngx_stream_js_max_response_buffer_size(ngx_stream_session_t *s);
static void ngx_stream_js_event_finalize(ngx_stream_session_t *s, ngx_int_t rc);
static ngx_js_ctx_t *ngx_stream_js_ctx(ngx_stream_session_t *s);
static void ngx_stream_js_periodic_handler(ngx_event_t *ev);
static void ngx_stream_js_periodic_event_handler(ngx_event_t *ev);
static void ngx_stream_js_periodic_finalize(ngx_stream_session_t *s,
ngx_int_t rc);
static void ngx_stream_js_periodic_destroy(ngx_stream_session_t *s,
ngx_js_periodic_t *periodic);
static njs_int_t ngx_js_stream_init(njs_vm_t *vm);
static ngx_int_t ngx_stream_js_init(ngx_conf_t *cf);
static ngx_int_t ngx_stream_js_init_worker(ngx_cycle_t *cycle);
static char *ngx_stream_js_periodic(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_stream_js_set(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_stream_js_var(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_int_t ngx_stream_js_init_conf_vm(ngx_conf_t *cf,
ngx_js_loc_conf_t *conf);
static void *ngx_stream_js_create_main_conf(ngx_conf_t *cf);
static void *ngx_stream_js_create_srv_conf(ngx_conf_t *cf);
static char *ngx_stream_js_merge_srv_conf(ngx_conf_t *cf, void *parent,
void *child);
static char *ngx_stream_js_shared_dict_zone(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_ssl_t *ngx_stream_js_ssl(ngx_stream_session_t *s);
static ngx_flag_t ngx_stream_js_ssl_verify(ngx_stream_session_t *s);
static ngx_conf_bitmask_t ngx_stream_js_engines[] = {
{ ngx_string("njs"), NGX_ENGINE_NJS },
#if (NJS_HAVE_QUICKJS)
{ ngx_string("qjs"), NGX_ENGINE_QJS },
#endif
{ ngx_null_string, 0 }
};
#if (NGX_STREAM_SSL)
static ngx_conf_bitmask_t ngx_stream_js_ssl_protocols[] = {
{ ngx_string("TLSv1"), NGX_SSL_TLSv1 },
{ ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
{ ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
{ ngx_string("TLSv1.3"), NGX_SSL_TLSv1_3 },
{ ngx_null_string, 0 }
};
#endif
static ngx_command_t ngx_stream_js_commands[] = {
{ ngx_string("js_engine"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_1MORE,
ngx_js_engine,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, type),
&ngx_stream_js_engines },
{ ngx_string("js_context_reuse"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, reuse),
NULL },
{ ngx_string("js_import"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE13,
ngx_js_import,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("js_periodic"),
NGX_STREAM_SRV_CONF|NGX_CONF_ANY,
ngx_stream_js_periodic,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("js_preload_object"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE13,
ngx_js_preload_object,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("js_path"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_array_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, paths),
NULL },
{ ngx_string("js_set"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE23,
ngx_stream_js_set,
0,
0,
NULL },
{ ngx_string("js_var"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE12,
ngx_stream_js_var,
0,
0,
NULL },
{ ngx_string("js_access"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, access),
NULL },
{ ngx_string("js_preread"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, preread),
NULL },
{ ngx_string("js_filter"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, filter),
NULL },
{ ngx_string("js_fetch_buffer_size"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, buffer_size),
NULL },
{ ngx_string("js_fetch_max_response_buffer_size"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, max_response_body_size),
NULL },
{ ngx_string("js_fetch_timeout"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, timeout),
NULL },
#if (NGX_STREAM_SSL)
{ ngx_string("js_fetch_ciphers"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, ssl_ciphers),
NULL },
{ ngx_string("js_fetch_protocols"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, ssl_protocols),
&ngx_stream_js_ssl_protocols },
{ ngx_string("js_fetch_verify"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, ssl_verify),
NULL },
{ ngx_string("js_fetch_verify_depth"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, ssl_verify_depth),
NULL },
{ ngx_string("js_fetch_trusted_certificate"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_js_srv_conf_t, ssl_trusted_certificate),
NULL },
#endif
{ ngx_string("js_shared_dict_zone"),
NGX_STREAM_MAIN_CONF|NGX_CONF_1MORE,
ngx_stream_js_shared_dict_zone,
0,
0,
NULL },
ngx_null_command
};
static ngx_stream_module_t ngx_stream_js_module_ctx = {
NULL, /* preconfiguration */
ngx_stream_js_init, /* postconfiguration */
ngx_stream_js_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
ngx_stream_js_create_srv_conf, /* create server configuration */
ngx_stream_js_merge_srv_conf, /* merge server configuration */
};
ngx_module_t ngx_stream_js_module = {
NGX_MODULE_V1,
&ngx_stream_js_module_ctx, /* module context */
ngx_stream_js_commands, /* module directives */
NGX_STREAM_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_stream_js_init_worker, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static njs_external_t ngx_stream_js_ext_session[] = {
{
.flags = NJS_EXTERN_PROPERTY | NJS_EXTERN_SYMBOL,
.name.symbol = NJS_SYMBOL_TO_STRING_TAG,
.u.property = {
.value = "Stream Session",
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("allow"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_done,
.magic8 = NGX_OK,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("decline"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_done,
.magic8 = -NGX_DECLINED,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("deny"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_done,
.magic8 = -NGX_DONE,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("done"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_done,
.magic8 = NGX_OK,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("error"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_js_ext_log,
.magic8 = NGX_LOG_ERR,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("log"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_js_ext_log,
.magic8 = NGX_LOG_INFO,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("off"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_off,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("on"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_on,
}
},
{
.flags = NJS_EXTERN_OBJECT,
.name.string = njs_str("rawVariables"),
.u.object = {
.writable = 1,
.prop_handler = ngx_stream_js_ext_variables,
.magic32 = NGX_JS_BUFFER,
}
},
{
.flags = NJS_EXTERN_PROPERTY,
.name.string = njs_str("remoteAddress"),
.enumerable = 1,
.u.property = {
.handler = ngx_stream_js_ext_get_remote_address,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("send"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_send,
.magic8 = NGX_JS_BOOL_UNSET,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("sendDownstream"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_send,
.magic8 = NGX_JS_BOOL_TRUE,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("sendUpstream"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_send,
.magic8 = NGX_JS_BOOL_FALSE,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("setReturnValue"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_stream_js_ext_set_return_value,
}
},
{
.flags = NJS_EXTERN_PROPERTY,
.name.string = njs_str("status"),
.enumerable = 1,
.u.property = {
.handler = ngx_js_ext_uint,
.magic32 = offsetof(ngx_stream_session_t, status),
}
},
{
.flags = NJS_EXTERN_OBJECT,
.name.string = njs_str("variables"),
.u.object = {
.writable = 1,
.prop_handler = ngx_stream_js_ext_variables,
.magic32 = NGX_JS_STRING,
}
},
{
.flags = NJS_EXTERN_METHOD,
.name.string = njs_str("warn"),
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.method = {
.native = ngx_js_ext_log,
.magic8 = NGX_LOG_WARN,
}
},
};
static njs_external_t ngx_stream_js_ext_periodic_session[] = {
{
.flags = NJS_EXTERN_PROPERTY | NJS_EXTERN_SYMBOL,
.name.symbol = NJS_SYMBOL_TO_STRING_TAG,
.u.property = {
.value = "PeriodicSession",
}
},
{
.flags = NJS_EXTERN_OBJECT,
.name.string = njs_str("rawVariables"),
.u.object = {
.writable = 1,
.prop_handler = ngx_stream_js_periodic_variables,
.magic32 = NGX_JS_BUFFER,
}
},
{
.flags = NJS_EXTERN_OBJECT,
.name.string = njs_str("variables"),
.u.object = {
.writable = 1,
.prop_handler = ngx_stream_js_periodic_variables,
.magic32 = NGX_JS_STRING,
}
},
};
static njs_external_t ngx_stream_js_ext_session_flags[] = {
{
.flags = NJS_EXTERN_PROPERTY | NJS_EXTERN_SYMBOL,
.name.symbol = NJS_SYMBOL_TO_STRING_TAG,
.u.property = {
.value = "Stream Flags",
}
},
{
.flags = NJS_EXTERN_PROPERTY,
.name.string = njs_str("from_upstream"),
.enumerable = 1,
.u.property = {
.handler = ngx_js_ext_flags,
.magic16 = NGX_JS_BOOLEAN,
.magic32 = 0x00000002,
}
},
{
.flags = NJS_EXTERN_PROPERTY,
.name.string = njs_str("last"),
.enumerable = 1,
.u.property = {
.handler = ngx_js_ext_flags,
.magic16 = NGX_JS_BOOLEAN,
.magic32 = 0x00000001,
}
},
};
static uintptr_t ngx_stream_js_uptr[] = {
offsetof(ngx_stream_session_t, connection),
(uintptr_t) ngx_stream_js_pool,
(uintptr_t) ngx_stream_js_resolver,
(uintptr_t) ngx_stream_js_resolver_timeout,
(uintptr_t) ngx_stream_js_event_finalize,
(uintptr_t) ngx_stream_js_ssl,
(uintptr_t) ngx_stream_js_ssl_verify,
(uintptr_t) ngx_stream_js_fetch_timeout,
(uintptr_t) ngx_stream_js_buffer_size,
(uintptr_t) ngx_stream_js_max_response_buffer_size,
(uintptr_t) 0 /* main_conf ptr */,
(uintptr_t) ngx_stream_js_ctx,
};
static njs_vm_meta_t ngx_stream_js_metas = {
.size = njs_nitems(ngx_stream_js_uptr),
.values = ngx_stream_js_uptr
};
static ngx_stream_filter_pt ngx_stream_next_filter;
static njs_int_t ngx_stream_js_session_proto_id = 1;
static njs_int_t ngx_stream_js_periodic_session_proto_id = 2;
static njs_int_t ngx_stream_js_session_flags_proto_id = 3;
njs_module_t ngx_js_stream_module = {
.name = njs_str("stream"),
.preinit = NULL,
.init = ngx_js_stream_init,
};
njs_module_t *njs_stream_js_addon_modules[] = {
/*
* Shared addons should be in the same order and the same positions
* in all nginx modules.
*/
&ngx_js_ngx_module,
&ngx_js_fetch_module,
&ngx_js_shared_dict_module,
#ifdef NJS_HAVE_OPENSSL
&njs_webcrypto_module,
#endif
#ifdef NJS_HAVE_XML
&njs_xml_module,
#endif
#ifdef NJS_HAVE_ZLIB
&njs_zlib_module,
#endif
&ngx_js_stream_module,
NULL,
};
#if (NJS_HAVE_QUICKJS)
static const JSCFunctionListEntry ngx_stream_qjs_ext_session[] = {
JS_CGETSET_DEF("[Symbol.toStringTag]", ngx_stream_qjs_ext_to_string_tag,
NULL),
JS_CFUNC_MAGIC_DEF("allow", 1, ngx_stream_qjs_ext_done, NGX_OK),
JS_CFUNC_MAGIC_DEF("decline", 1, ngx_stream_qjs_ext_done, -NGX_DECLINED),
JS_CFUNC_MAGIC_DEF("deny", 1, ngx_stream_qjs_ext_done, -NGX_DONE),
JS_CFUNC_MAGIC_DEF("done", 1, ngx_stream_qjs_ext_done, NGX_OK),
JS_CFUNC_MAGIC_DEF("error", 1, ngx_stream_qjs_ext_log, NGX_LOG_ERR),
JS_CFUNC_MAGIC_DEF("log", 1, ngx_stream_qjs_ext_log, NGX_LOG_INFO),
JS_CFUNC_DEF("on", 2, ngx_stream_qjs_ext_on),
JS_CFUNC_DEF("off", 1, ngx_stream_qjs_ext_off),
JS_CGETSET_MAGIC_DEF("rawVariables", ngx_stream_qjs_ext_variables,
NULL, NGX_JS_BUFFER),
JS_CGETSET_DEF("remoteAddress", ngx_stream_qjs_ext_remote_address, NULL),
JS_CFUNC_MAGIC_DEF("send", 2, ngx_stream_qjs_ext_send, NGX_JS_BOOL_UNSET),
JS_CFUNC_MAGIC_DEF("sendDownstream", 1, ngx_stream_qjs_ext_send,
NGX_JS_BOOL_TRUE),
JS_CFUNC_MAGIC_DEF("sendUpstream", 1, ngx_stream_qjs_ext_send,
NGX_JS_BOOL_FALSE),
JS_CFUNC_DEF("setReturnValue", 1, ngx_stream_qjs_ext_set_return_value),
JS_CGETSET_MAGIC_DEF("status", ngx_stream_qjs_ext_uint, NULL,
offsetof(ngx_stream_session_t, status)),
JS_CGETSET_MAGIC_DEF("variables", ngx_stream_qjs_ext_variables,
NULL, NGX_JS_STRING),
JS_CFUNC_MAGIC_DEF("warn", 1, ngx_stream_qjs_ext_log, NGX_LOG_WARN),
};
static const JSCFunctionListEntry ngx_stream_qjs_ext_periodic[] = {
JS_CGETSET_DEF("[Symbol.toStringTag]",
ngx_stream_qjs_ext_periodic_to_string_tag, NULL),
JS_CGETSET_MAGIC_DEF("rawVariables", ngx_stream_qjs_ext_periodic_variables,
NULL, NGX_JS_BUFFER),
JS_CGETSET_MAGIC_DEF("variables", ngx_stream_qjs_ext_periodic_variables,
NULL, NGX_JS_STRING),
};
static const JSCFunctionListEntry ngx_stream_qjs_ext_flags[] = {
JS_CGETSET_MAGIC_DEF("from_upstream", ngx_stream_qjs_ext_flag, NULL,
2),
JS_CGETSET_MAGIC_DEF("last", ngx_stream_qjs_ext_flag, NULL, 1),
};
static JSClassDef ngx_stream_qjs_session_class = {
"Session",
.finalizer = ngx_stream_qjs_session_finalizer,
};
static JSClassDef ngx_stream_qjs_periodic_class = {
"Periodic",
.finalizer = ngx_stream_qjs_periodic_finalizer,
};
static JSClassDef ngx_stream_qjs_flags_class = {
"Stream Flags",
.finalizer = NULL,
};
static JSClassDef ngx_stream_qjs_variables_class = {
"Variables",
.finalizer = NULL,
.exotic = & (JSClassExoticMethods) {
.get_own_property = ngx_stream_qjs_variables_own_property,
.set_property = ngx_stream_qjs_variables_set_property,
.define_own_property = ngx_stream_qjs_variables_define_own_property,
},
};
qjs_module_t *njs_stream_qjs_addon_modules[] = {
&ngx_qjs_ngx_module,
&ngx_qjs_ngx_shared_dict_module,
/*
* Shared addons should be in the same order and the same positions
* in all nginx modules.
*/
#ifdef NJS_HAVE_OPENSSL
&qjs_webcrypto_module,
#endif
#ifdef NJS_HAVE_ZLIB
&qjs_zlib_module,
#endif
NULL,
};
#endif
static ngx_int_t
ngx_stream_js_access_handler(ngx_stream_session_t *s)
{
ngx_stream_js_srv_conf_t *jscf;
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"js access handler");
jscf = ngx_stream_get_module_srv_conf(s, ngx_stream_js_module);
return ngx_stream_js_phase_handler(s, &jscf->access);
}
static ngx_int_t
ngx_stream_js_preread_handler(ngx_stream_session_t *s)
{
ngx_stream_js_srv_conf_t *jscf;
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"js preread handler");
jscf = ngx_stream_get_module_srv_conf(s, ngx_stream_js_module);
return ngx_stream_js_phase_handler(s, &jscf->preread);
}
static ngx_int_t
ngx_stream_js_phase_handler(ngx_stream_session_t *s, ngx_str_t *name)
{
ngx_int_t rc;
ngx_stream_js_ctx_t *ctx;
if (name->len == 0) {
return NGX_DECLINED;
}
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream js phase handler");
rc = ngx_stream_js_init_vm(s, ngx_stream_js_session_proto_id);
if (rc != NGX_OK) {
return rc;
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
if (!ctx->in_progress) {
/*
* status is expected to be overriden by allow(), deny(), decline() or
* done() methods.
*/
ctx->status = NGX_ERROR;
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, ctx->log, 0,
"stream js phase call \"%V\"", name);
rc = ctx->engine->call((ngx_js_ctx_t *) ctx, name, &ctx->args[0], 1);
if (rc == NGX_ERROR) {
return rc;
}
}
rc = ctx->run_event(s, ctx, &ctx->events[NGX_JS_EVENT_UPLOAD], 0);
if (rc != NGX_OK) {
return NGX_ERROR;
}
if (ngx_stream_pending(ctx)) {
ctx->in_progress = 1;
rc = (ctx->events[NGX_JS_EVENT_UPLOAD].data_type != NGX_JS_UNSET)
? NGX_AGAIN
: NGX_DONE;
} else {
ctx->in_progress = 0;
rc = ctx->status;
}
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, ctx->log, 0, "stream js phase rc: %i",
rc);
return rc;
}
#define ngx_stream_event(from_upstream) \
(from_upstream ? &ctx->events[NGX_JS_EVENT_DOWNLOAD] \
: &ctx->events[NGX_JS_EVENT_UPLOAD])
static ngx_int_t
ngx_stream_js_body_filter(ngx_stream_session_t *s, ngx_chain_t *in,
ngx_uint_t from_upstream)
{
ngx_int_t rc;
ngx_chain_t *out;
ngx_stream_js_ctx_t *ctx;
ngx_stream_js_srv_conf_t *jscf;
jscf = ngx_stream_get_module_srv_conf(s, ngx_stream_js_module);
if (jscf->filter.len == 0) {
return ngx_stream_next_filter(s, in, from_upstream);
}
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream js filter u:%ui", from_upstream);
rc = ngx_stream_js_init_vm(s, ngx_stream_js_session_proto_id);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
if (rc == NGX_DECLINED) {
return ngx_stream_next_filter(s, in, from_upstream);
}
ctx = ngx_stream_get_module_ctx(s, ngx_stream_js_module);
if (!ctx->filter) {
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
"stream js filter call \"%V\"" , &jscf->filter);
rc = ctx->engine->call((ngx_js_ctx_t *) ctx, &jscf->filter,
&ctx->args[0], 1);
if (rc == NGX_ERROR) {
return rc;
}
}
ctx->filter = 1;
ctx->last_out = &out;
rc = ctx->body_filter(s, ctx, in, from_upstream);
if (rc != NGX_OK) {
return NGX_ERROR;
}