This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 198
/
ngx_http_dyups_module.c
2012 lines (1514 loc) · 51 KB
/
ngx_http_dyups_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
#include <ngx_http.h>
#include <ngx_http_dyups.h>
#ifdef NGX_DYUPS_LUA
#include <ngx_http_dyups_lua.h>
#endif
#define NGX_DYUPS_DELETING 1
#define NGX_DYUPS_DELETED 2
#define NGX_DYUPS_SHM_NAME_LEN 32
#define NGX_DYUPS_DELETE 1
#define NGX_DYUPS_ADD 2
#if (NGX_DEBUG)
#define NGX_DYUPS_INIT_SIZE 1
#else
#define NGX_DYUPS_INIT_SIZE 1024
#endif
typedef struct {
ngx_uint_t idx;
ngx_uint_t *ref;
ngx_uint_t deleted;
ngx_flag_t dynamic;
ngx_pool_t *pool;
ngx_http_conf_ctx_t *ctx;
ngx_http_upstream_srv_conf_t *upstream;
} ngx_http_dyups_srv_conf_t;
typedef struct {
ngx_flag_t enable;
ngx_array_t dy_upstreams;/* ngx_http_dyups_srv_conf_t */
ngx_str_t shm_name;
ngx_uint_t shm_size;
} ngx_http_dyups_main_conf_t;
typedef struct {
ngx_uint_t ref;
ngx_http_upstream_init_peer_pt init;
} ngx_http_dyups_upstream_srv_conf_t;
typedef struct {
void *data;
ngx_http_dyups_upstream_srv_conf_t *scf;
ngx_event_get_peer_pt get;
ngx_event_free_peer_pt free;
#if (NGX_HTTP_SSL)
ngx_ssl_session_t *ssl_session;
#endif
} ngx_http_dyups_ctx_t;
typedef struct ngx_dyups_upstream_s {
ngx_rbtree_node_t node;
ngx_str_t name;
ngx_str_t content;
ngx_uint_t version;
} ngx_dyups_upstream_t;
typedef struct ngx_dyups_shctx_s {
ngx_uint_t version;
ngx_rbtree_t rbtree;
ngx_rbtree_node_t sentinel;
} ngx_dyups_shctx_t;
typedef struct ngx_dyups_global_ctx_s {
ngx_slab_pool_t *shpool;
ngx_dyups_shctx_t *sh;
ngx_uint_t version;
} ngx_dyups_global_ctx_t;
static ngx_int_t ngx_http_dyups_pre_conf(ngx_conf_t *cf);
static ngx_int_t ngx_http_dyups_init(ngx_conf_t *cf);
static void *ngx_http_dyups_create_main_conf(ngx_conf_t *cf);
static char *ngx_http_dyups_init_main_conf(ngx_conf_t *cf, void *conf);
static char *ngx_http_dyups_interface(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static ngx_int_t ngx_http_dyups_interface_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_dyups_interface_read_body(ngx_http_request_t *r);
static ngx_buf_t *ngx_http_dyups_read_body(ngx_http_request_t *r);
static ngx_buf_t *ngx_http_dyups_read_body_from_file(ngx_http_request_t *r);
static void ngx_http_dyups_body_handler(ngx_http_request_t *r);
static void ngx_http_dyups_send_response(ngx_http_request_t *r,
ngx_int_t status, ngx_str_t *content);
static ngx_int_t ngx_http_dyups_do_get(ngx_http_request_t *r,
ngx_array_t *resource);
static ngx_int_t ngx_http_dyups_do_delete(ngx_http_request_t *r,
ngx_array_t *resource);
static ngx_http_dyups_srv_conf_t *ngx_dyups_find_upstream(ngx_str_t *name,
ngx_int_t *idx);
static ngx_int_t ngx_dyups_add_server(ngx_http_dyups_srv_conf_t *duscf,
ngx_buf_t *buf);
static ngx_int_t ngx_dyups_init_upstream(ngx_http_dyups_srv_conf_t *duscf,
ngx_str_t *name, ngx_uint_t index);
static void ngx_dyups_mark_upstream_delete(ngx_http_dyups_srv_conf_t *duscf);
static ngx_int_t ngx_http_dyups_init_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *us);
static ngx_int_t ngx_http_dyups_get_peer(ngx_peer_connection_t *pc, void *data);
static void ngx_http_dyups_free_peer(ngx_peer_connection_t *pc, void *data,
ngx_uint_t state);
static void *ngx_http_dyups_create_srv_conf(ngx_conf_t *cf);
static ngx_buf_t *ngx_http_dyups_show_list(ngx_http_request_t *r);
static ngx_int_t ngx_http_dyups_init_shm_zone(ngx_shm_zone_t *shm_zone,
void *data);
static char *ngx_http_dyups_init_shm(ngx_conf_t *cf, void *conf);
static ngx_int_t ngx_http_dyups_get_shm_name(ngx_str_t *shm_name,
ngx_pool_t *pool);
static ngx_int_t ngx_http_dyups_init_process(ngx_cycle_t *cycle);
static void ngx_http_dyups_exit_process(ngx_cycle_t *cycle);
static ngx_int_t ngx_dyups_shm_update_ups(ngx_str_t *name, ngx_buf_t *body);
static ngx_int_t ngx_dyups_shm_delete_ups(ngx_str_t *name);
static void ngx_dyups_shm_free_ups(ngx_slab_pool_t *shpool,
ngx_dyups_upstream_t *ups);
static ngx_array_t *ngx_dyups_parse_path(ngx_pool_t *pool, ngx_str_t *path);
static ngx_int_t ngx_dyups_do_delete(ngx_str_t *name, ngx_str_t *rv);
static ngx_int_t ngx_dyups_do_update(ngx_str_t *name, ngx_buf_t *buf,
ngx_str_t *rv);
static ngx_int_t ngx_dyups_sandbox_update(ngx_buf_t *buf, ngx_str_t *rv);
static void ngx_http_dyups_clean_request(void *data);
static ngx_int_t ngx_http_variable_dyups(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_dyups_add_vars(ngx_conf_t *cf);
static ngx_int_t ngx_http_dyups_reload();
#if (NGX_HTTP_SSL)
static ngx_int_t ngx_http_dyups_set_peer_session(ngx_peer_connection_t *pc,
void *data);
static void ngx_http_dyups_save_peer_session(ngx_peer_connection_t *pc,
void *data);
#endif
static ngx_int_t ngx_dyups_add_upstream_filter(
ngx_http_upstream_main_conf_t *umcf, ngx_http_upstream_srv_conf_t *uscf);
static ngx_int_t ngx_dyups_del_upstream_filter(
ngx_http_upstream_main_conf_t *umcf, ngx_http_upstream_srv_conf_t *uscf);
ngx_int_t (*ngx_dyups_add_upstream_top_filter)
(ngx_http_upstream_main_conf_t *umcf, ngx_http_upstream_srv_conf_t *uscf);
ngx_int_t (*ngx_dyups_del_upstream_top_filter)
(ngx_http_upstream_main_conf_t *umcf, ngx_http_upstream_srv_conf_t *uscf);
static ngx_http_variable_t ngx_http_dyups_variables[] = {
{ ngx_string("dyups_"), NULL, ngx_http_variable_dyups,
0, NGX_HTTP_VAR_PREFIX, 0 },
ngx_http_null_variable
};
static ngx_command_t ngx_http_dyups_commands[] = {
{ ngx_string("dyups_interface"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_dyups_interface,
0,
0,
NULL },
{ ngx_string("dyups_shm_zone_size"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_dyups_main_conf_t, shm_size),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_dyups_module_ctx = {
ngx_http_dyups_pre_conf, /* preconfiguration */
ngx_http_dyups_init, /* postconfiguration */
ngx_http_dyups_create_main_conf, /* create main configuration */
ngx_http_dyups_init_main_conf, /* init main configuration */
ngx_http_dyups_create_srv_conf, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_dyups_module = {
NGX_MODULE_V1,
&ngx_http_dyups_module_ctx, /* module context */
ngx_http_dyups_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_dyups_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_dyups_exit_process, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
ngx_flag_t ngx_http_dyups_api_enable = 0;
static ngx_http_upstream_srv_conf_t ngx_http_dyups_deleted_upstream;
static ngx_dyups_global_ctx_t ngx_dyups_global_ctx;
static ngx_int_t
ngx_http_dyups_pre_conf(ngx_conf_t *cf)
{
ngx_dyups_add_upstream_top_filter = ngx_dyups_add_upstream_filter;
ngx_dyups_del_upstream_top_filter = ngx_dyups_del_upstream_filter;
return ngx_http_dyups_add_vars(cf);
}
static ngx_int_t
ngx_http_dyups_add_vars(ngx_conf_t *cf)
{
ngx_http_variable_t *cv, *v;
for (cv = ngx_http_dyups_variables; cv->name.len; cv++) {
v = ngx_http_add_variable(cf, &cv->name, cv->flags);
if (v == NULL) {
return NGX_ERROR;
}
*v = *cv;
}
return NGX_OK;
}
static char *
ngx_http_dyups_interface(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf;
ngx_http_dyups_main_conf_t *dmcf;
dmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_dyups_module);
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_dyups_interface_handler;
dmcf->enable = 1;
return NGX_CONF_OK;
}
static void *
ngx_http_dyups_create_main_conf(ngx_conf_t *cf)
{
ngx_http_dyups_main_conf_t *dmcf;
dmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dyups_main_conf_t));
if (dmcf == NULL) {
return NULL;
}
if (ngx_array_init(&dmcf->dy_upstreams, cf->pool, NGX_DYUPS_INIT_SIZE,
sizeof(ngx_http_dyups_srv_conf_t))
!= NGX_OK)
{
return NULL;
}
dmcf->enable = NGX_CONF_UNSET;
dmcf->shm_size = NGX_CONF_UNSET_UINT;
return dmcf;
}
static char *
ngx_http_dyups_init_main_conf(ngx_conf_t *cf, void *conf)
{
ngx_http_dyups_main_conf_t *dmcf = conf;
if (dmcf->enable == NGX_CONF_UNSET) {
dmcf->enable = 0;
}
dmcf->enable = dmcf->enable || ngx_http_dyups_api_enable;
if (!dmcf->enable) {
return NGX_CONF_OK;
}
if (dmcf->shm_size == NGX_CONF_UNSET_UINT) {
dmcf->shm_size = 2 * 1024 * 1024;
}
return ngx_http_dyups_init_shm(cf, conf);
}
static char *
ngx_http_dyups_init_shm(ngx_conf_t *cf, void *conf)
{
ngx_http_dyups_main_conf_t *dmcf = conf;
ngx_shm_zone_t *shm_zone;
if (ngx_http_dyups_get_shm_name(&dmcf->shm_name, cf->pool)
!= NGX_OK)
{
return NGX_CONF_ERROR;
}
shm_zone = ngx_shared_memory_add(cf, &dmcf->shm_name, dmcf->shm_size,
&ngx_http_dyups_module);
if (shm_zone == NULL) {
return NGX_CONF_ERROR;
}
ngx_log_error(NGX_LOG_DEBUG, cf->log, 0,
"[dyups] init shm:%V, size:%ui", &dmcf->shm_name,
dmcf->shm_size);
shm_zone->data = &ngx_dyups_global_ctx;
shm_zone->init = ngx_http_dyups_init_shm_zone;
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_dyups_get_shm_name(ngx_str_t *shm_name, ngx_pool_t *pool)
{
u_char *last;
shm_name->data = ngx_palloc(pool, NGX_DYUPS_SHM_NAME_LEN);
if (shm_name->data == NULL) {
return NGX_ERROR;
}
last = ngx_snprintf(shm_name->data, NGX_DYUPS_SHM_NAME_LEN, "%s#shm",
"ngx_http_dyups_module");
shm_name->len = last - shm_name->data;
return NGX_OK;
}
static ngx_int_t
ngx_http_dyups_init_shm_zone(ngx_shm_zone_t *shm_zone, void *data)
{
ngx_dyups_global_ctx_t *octx = data;
ngx_slab_pool_t *shpool;
ngx_dyups_shctx_t *sh;
if (octx != NULL) {
ngx_dyups_global_ctx.sh = octx->sh;
ngx_dyups_global_ctx.shpool = octx->shpool;
return NGX_OK;
}
shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
sh = ngx_slab_alloc(shpool, sizeof(ngx_dyups_shctx_t));
if (sh == NULL) {
return NGX_ERROR;
}
ngx_dyups_global_ctx.sh = sh;
ngx_dyups_global_ctx.shpool = shpool;
sh->version = 0;
ngx_rbtree_init(&sh->rbtree, &sh->sentinel, ngx_str_rbtree_insert_value);
return NGX_OK;
}
static void *
ngx_http_dyups_create_srv_conf(ngx_conf_t *cf)
{
ngx_http_dyups_upstream_srv_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_dyups_upstream_srv_conf_t));
if (conf == NULL) {
return NULL;
}
/*
conf->init = NULL;
*/
return conf;
}
static ngx_int_t
ngx_http_dyups_init(ngx_conf_t *cf)
{
ngx_url_t u;
ngx_uint_t i;
ngx_http_dyups_srv_conf_t *duscf;
ngx_http_upstream_server_t *us;
ngx_http_dyups_main_conf_t *dmcf;
ngx_http_upstream_srv_conf_t **uscfp;
ngx_http_upstream_main_conf_t *umcf;
ngx_http_dyups_upstream_srv_conf_t *dscf;
dmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_dyups_module);
umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);
if (!dmcf->enable) {
return NGX_OK;
}
uscfp = umcf->upstreams.elts;
for (i = 0; i < umcf->upstreams.nelts; i++) {
duscf = ngx_array_push(&dmcf->dy_upstreams);
if (duscf == NULL) {
return NGX_ERROR;
}
ngx_memzero(duscf, sizeof(ngx_http_dyups_srv_conf_t));
duscf->pool = NULL;
duscf->upstream = uscfp[i];
duscf->dynamic = (uscfp[i]->port == 0
&& uscfp[i]->srv_conf && uscfp[i]->servers
&& uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE);
duscf->deleted = 0;
duscf->idx = i;
if (duscf->dynamic) {
dscf = duscf->upstream->srv_conf[ngx_http_dyups_module.ctx_index];
duscf->ref = &dscf->ref;
}
}
/* alloc a dummy upstream */
ngx_memzero(&ngx_http_dyups_deleted_upstream,
sizeof(ngx_http_upstream_srv_conf_t));
ngx_http_dyups_deleted_upstream.srv_conf = ((ngx_http_conf_ctx_t *)
(cf->ctx))->srv_conf;
ngx_http_dyups_deleted_upstream.servers = ngx_array_create(cf->pool, 1,
sizeof(ngx_http_upstream_server_t));
us = ngx_array_push(ngx_http_dyups_deleted_upstream.servers);
if (us == NULL) {
return NGX_ERROR;
}
ngx_memzero(&u, sizeof(ngx_url_t));
ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
u.default_port = 80;
ngx_str_set(&u.url, "0.0.0.0");
if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
if (u.err) {
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
"[dyups] %s in init", u.err);
}
return NGX_ERROR;
}
us->addrs = u.addrs;
us->naddrs = u.naddrs;
us->down = 1;
ngx_str_set(&ngx_http_dyups_deleted_upstream.host,
"_dyups_upstream_down_host_");
ngx_http_dyups_deleted_upstream.file_name = (u_char *) "dyups_upstream";
#ifdef NGX_DYUPS_LUA
return ngx_http_dyups_lua_preload(cf);
#else
return NGX_OK;
#endif
}
static ngx_int_t
ngx_http_dyups_init_process(ngx_cycle_t *cycle)
{
ngx_core_conf_t *ccf;
ngx_slab_pool_t *shpool;
ngx_dyups_shctx_t *sh;
ngx_http_dyups_main_conf_t *dmcf;
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
dmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_dyups_module);
if (!dmcf || !dmcf->enable || ngx_process == NGX_PROCESS_HELPER) {
ngx_http_dyups_api_enable = 0;
return NGX_OK;
}
ngx_http_dyups_api_enable = 1;
shpool = ngx_dyups_global_ctx.shpool;
sh = ngx_dyups_global_ctx.sh;
ngx_shmtx_lock(&shpool->mutex);
ngx_http_dyups_reload();
ngx_shmtx_unlock(&shpool->mutex);
return NGX_OK;
}
static void
ngx_http_dyups_exit_process(ngx_cycle_t *cycle)
{
ngx_uint_t i;
ngx_http_dyups_srv_conf_t *duscfs, *duscf;
ngx_http_dyups_main_conf_t *dumcf;
dumcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_dyups_module);
if (!dumcf) {
return;
}
duscfs = dumcf->dy_upstreams.elts;
for (i = 0; i < dumcf->dy_upstreams.nelts; i++) {
duscf = &duscfs[i];
if (duscf->pool) {
ngx_destroy_pool(duscf->pool);
duscf->pool = NULL;
}
}
}
static ngx_int_t
ngx_http_dyups_interface_handler(ngx_http_request_t *r)
{
ngx_array_t *res;
res = ngx_dyups_parse_path(r->pool, &r->uri);
if (res == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
if (r->method == NGX_HTTP_GET) {
return ngx_http_dyups_do_get(r, res);
}
if (r->method == NGX_HTTP_DELETE) {
return ngx_http_dyups_do_delete(r, res);
}
return ngx_http_dyups_interface_read_body(r);
}
ngx_int_t
ngx_dyups_delete_upstream(ngx_str_t *name, ngx_str_t *rv)
{
ngx_int_t status, rc;
ngx_slab_pool_t *shpool;
ngx_http_dyups_main_conf_t *dmcf;
dmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_dyups_module);
shpool = ngx_dyups_global_ctx.shpool;
if (!ngx_http_dyups_api_enable) {
ngx_str_set(rv, "API disabled\n");
return NGX_HTTP_NOT_ALLOWED;
}
ngx_shmtx_lock(&shpool->mutex);
status = ngx_dyups_do_delete(name, rv);
if (status != NGX_HTTP_OK) {
goto finish;
}
rc = ngx_dyups_shm_delete_ups(name);
if (rc != NGX_OK) {
ngx_str_set(rv, "shm delete failed");
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, "[dyups] %V", &rv);
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
}
finish:
ngx_shmtx_unlock(&shpool->mutex);
return status;
}
static ngx_int_t
ngx_http_dyups_do_get(ngx_http_request_t *r, ngx_array_t *resource)
{
ngx_int_t rc, status;
ngx_buf_t *buf;
ngx_str_t *value;
ngx_chain_t out;
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
if (resource->nelts == 0) {
return NGX_HTTP_NOT_FOUND;
}
buf = NULL;
value = resource->elts;
if (value[0].len == 4
&& ngx_strncasecmp(value[0].data, (u_char *) "list", 4) == 0)
{
buf = ngx_http_dyups_show_list(r);
if (buf == NULL) {
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
goto finish;
}
}
if (buf != NULL && ngx_buf_size(buf) == 0) {
status = NGX_HTTP_NO_CONTENT;
} else {
status = buf ? NGX_HTTP_OK : NGX_HTTP_NOT_FOUND;
}
finish:
r->headers_out.status = status;
if (status != NGX_HTTP_OK) {
r->headers_out.content_length_n = 0;
} else {
r->headers_out.content_length_n = ngx_buf_size(buf);
}
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK) {
return rc;
}
if (status != NGX_HTTP_OK) {
return ngx_http_send_special(r, NGX_HTTP_FLUSH);
}
buf->last_buf = 1;
out.buf = buf;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
static ngx_int_t
ngx_http_dyups_reload()
{
ngx_buf_t body;
ngx_int_t rc;
ngx_str_t rv;
ngx_slab_pool_t *shpool;
ngx_dyups_shctx_t *sh;
ngx_rbtree_node_t *node, *root, *sentinel;
ngx_dyups_upstream_t *ups;
sh = ngx_dyups_global_ctx.sh;
shpool = ngx_dyups_global_ctx.shpool;
sentinel = sh->rbtree.sentinel;
root = sh->rbtree.root;
if (root == sentinel) {
return NGX_OK;
}
for (node = ngx_rbtree_min(root, sentinel);
node;
node = ngx_rbtree_next(&sh->rbtree, node))
{
ups = (ngx_dyups_upstream_t*)
((char*) node - offsetof(ngx_dyups_upstream_t, node));
if (ups->version > ngx_dyups_global_ctx.version) {
continue;
}
body.start = body.pos = ups->content.data;
body.end = body.last = ups->content.data + ups->content.len;
rc = ngx_dyups_do_update(&ups->name, &body, &rv);
ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0,
"[dyups] sync add: %V rv: %V rc: %i",
&ups->name, &rv, rc);
}
ngx_dyups_global_ctx.version = sh->version;
return NGX_OK;
}
static ngx_buf_t *
ngx_http_dyups_show_list(ngx_http_request_t *r)
{
ngx_uint_t len;
ngx_buf_t *buf;
ngx_slab_pool_t *shpool;
ngx_dyups_shctx_t *sh;
ngx_rbtree_node_t *node, *root, *sentinel;
ngx_dyups_upstream_t *ups;
sh = ngx_dyups_global_ctx.sh;
shpool = ngx_dyups_global_ctx.shpool;
sentinel = sh->rbtree.sentinel;
root = sh->rbtree.root;
if (root == sentinel) {
return ngx_create_temp_buf(r->pool, 0);
}
ngx_shmtx_lock(&shpool->mutex);
len = 0;
for (node = ngx_rbtree_min(root, sentinel);
node;
node = ngx_rbtree_next(&sh->rbtree, node))
{
ups = (ngx_dyups_upstream_t*)
((char*) node - offsetof(ngx_dyups_upstream_t, node));
len += + ups->name.len + ups->content.len + sizeof("upstream {\n\n}\n");
}
buf = ngx_create_temp_buf(r->pool, len);
if (buf == NULL) {
goto done;
}
for (node = ngx_rbtree_min(root, sentinel);
node;
node = ngx_rbtree_next(&sh->rbtree, node))
{
ups = (ngx_dyups_upstream_t*)
((char*) node - offsetof(ngx_dyups_upstream_t, node));
buf->last = ngx_sprintf(buf->last, "upstream %V {\n", &ups->name);
buf->last = ngx_sprintf(buf->last, "%V\n}\n", &ups->content);
}
done:
ngx_shmtx_unlock(&shpool->mutex);
return buf;
}
static ngx_int_t
ngx_dyups_do_delete(ngx_str_t *name, ngx_str_t *rv)
{
ngx_int_t dumy;
ngx_http_dyups_srv_conf_t *duscf;
duscf = ngx_dyups_find_upstream(name, &dumy);
if (duscf == NULL || duscf->deleted) {
ngx_log_error(NGX_LOG_DEBUG, ngx_cycle->log, 0,
"[dyups] not find upstream %V %p", name, duscf);
ngx_str_set(rv, "not found uptream");
return NGX_HTTP_NOT_FOUND;
}
ngx_dyups_mark_upstream_delete(duscf);
ngx_str_set(rv, "success");
return NGX_HTTP_OK;
}
static ngx_int_t
ngx_http_dyups_do_delete(ngx_http_request_t *r, ngx_array_t *resource)
{
ngx_str_t *value, name, rv;
ngx_int_t status, rc;
ngx_buf_t *b;
ngx_chain_t out;
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
if (resource->nelts != 2) {
ngx_str_set(&rv, "not support this interface");
status = NGX_HTTP_NOT_ALLOWED;
goto finish;
}
value = resource->elts;
if (value[0].len != 8
|| ngx_strncasecmp(value[0].data, (u_char *) "upstream", 8) != 0)
{
ngx_str_set(&rv, "not support this api");
status = NGX_HTTP_NOT_ALLOWED;
goto finish;
}
name = value[1];
status = ngx_dyups_delete_upstream(&name, &rv);
finish:
r->headers_out.status = status;
r->headers_out.content_length_n = rv.len;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK) {
return rc;
}
if (rv.len == 0) {
return ngx_http_send_special(r, NGX_HTTP_FLUSH);
}
b = ngx_create_temp_buf(r->pool, rv.len);
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
b->pos = rv.data;
b->last = rv.data + rv.len;
b->last_buf = 1;
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
static ngx_int_t
ngx_http_dyups_interface_read_body(ngx_http_request_t *r)
{
ngx_int_t rc;
rc = ngx_http_read_client_request_body(r, ngx_http_dyups_body_handler);
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
return rc;
}
return NGX_DONE;
}
static void
ngx_http_dyups_body_handler(ngx_http_request_t *r)
{
ngx_str_t *value, rv, name;
ngx_int_t status;
ngx_buf_t *body;
ngx_array_t *res;
ngx_str_set(&rv, "");
if (r->method != NGX_HTTP_POST) {
status = NGX_HTTP_NOT_ALLOWED;
goto finish;
}
res = ngx_dyups_parse_path(r->pool, &r->uri);
if (res == NULL) {
ngx_str_set(&rv, "out of memory");
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
goto finish;
}
if (r->request_body == NULL || r->request_body->bufs == NULL) {
status = NGX_HTTP_NO_CONTENT;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"[dyups] interface no content");
ngx_str_set(&rv, "no content\n");
goto finish;
}
if (r->request_body->temp_file) {
body = ngx_http_dyups_read_body_from_file(r);
} else {
body = ngx_http_dyups_read_body(r);
}
if (body == NULL) {
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
ngx_str_set(&rv, "out of memory\n");
goto finish;
}
if (res->nelts != 2) {
ngx_str_set(&rv, "not support this interface");
status = NGX_HTTP_NOT_FOUND;
goto finish;
}
/*
url: /upstream
body: server ip:port weight
*/
value = res->elts;
if (value[0].len != 8
|| ngx_strncasecmp(value[0].data, (u_char *) "upstream", 8) != 0)
{
ngx_str_set(&rv, "not support this api");
status = NGX_HTTP_NOT_FOUND;
goto finish;
}
name = value[1];
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"[dyups] post upstream name: %V", &name);
status = ngx_dyups_update_upstream(&name, body, &rv);
finish:
ngx_http_dyups_send_response(r, status, &rv);
}
ngx_int_t
ngx_dyups_update_upstream(ngx_str_t *name, ngx_buf_t *buf, ngx_str_t *rv)
{
ngx_int_t status;
ngx_slab_pool_t *shpool;
ngx_http_dyups_main_conf_t *dmcf;
dmcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_dyups_module);
shpool = ngx_dyups_global_ctx.shpool;
if (!ngx_http_dyups_api_enable) {
ngx_str_set(rv, "API disabled\n");
return NGX_HTTP_NOT_ALLOWED;
}
ngx_shmtx_lock(&shpool->mutex);
status = ngx_dyups_sandbox_update(buf, rv);
if (status != NGX_HTTP_OK) {
goto finish;
}
status = ngx_dyups_do_update(name, buf, rv);
if (status == NGX_HTTP_OK) {
if (ngx_dyups_shm_update_ups(name, buf)) {
ngx_str_set(rv, "alert: update success but save to shm failed\n");
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
}
}
finish:
ngx_shmtx_unlock(&shpool->mutex);
return status;
}
static ngx_int_t
ngx_dyups_do_update(ngx_str_t *name, ngx_buf_t *buf, ngx_str_t *rv)
{
ngx_int_t rc, idx;
ngx_http_dyups_srv_conf_t *duscf;
ngx_http_dyups_main_conf_t *dumcf;
ngx_http_upstream_srv_conf_t **uscfp;
ngx_http_upstream_main_conf_t *umcf;