forked from yzprofile/ngx_http_dyups_module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ngx_http_dyups_module.c
1600 lines (1213 loc) · 38.9 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_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#define NGX_DYUPS_DELETING 1
#define NGX_DYUPS_DELETED 2
typedef struct {
ngx_uint_t *count;
ngx_flag_t dynamic;
ngx_uint_t deleted;
ngx_pool_t *pool;
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_http_dyups_main_conf_t;
typedef struct {
ngx_uint_t count;
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;
} ngx_http_dyups_ctx_t;
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 ngx_array_t *ngx_dyups_parse_content(ngx_pool_t *pool, ngx_buf_t *buf);
static ngx_int_t ngx_dyups_conf_read_token(ngx_pool_t *pool, ngx_buf_t *body,
ngx_array_t *args);
static ngx_array_t *ngx_http_dyups_parse_path(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_int_t ngx_http_dyups_do_post(ngx_http_request_t *r,
ngx_array_t *resource, ngx_array_t *arglist, ngx_str_t *rv);
static ngx_int_t ngx_http_dyups_do_put(ngx_http_request_t *r,
ngx_array_t *resource, ngx_array_t *arglist, ngx_str_t *rv);
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_array_t *arglist);
static ngx_int_t ngx_dyups_init_upstream(ngx_http_dyups_srv_conf_t *duscf,
ngx_str_t *name, ngx_uint_t index);
static ngx_int_t ngx_dyups_delete_upstream(ngx_http_dyups_srv_conf_t *duscf);
static ngx_int_t ngx_http_dyups_check_commands(ngx_array_t *arglist);
static ngx_int_t ngx_http_dyups_init_peer(ngx_http_request_t *r,
ngx_http_upstream_srv_conf_t *us);
static void *ngx_http_dyups_create_srv_conf(ngx_conf_t *cf);
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 ngx_buf_t *ngx_http_dyups_show_list(ngx_http_request_t *r);
static ngx_buf_t *ngx_http_dyups_show_detail(ngx_http_request_t *r);
static ngx_buf_t *ngx_http_dyups_show_upstream(ngx_http_request_t *r,
ngx_http_dyups_srv_conf_t *duscf);
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_null_command
};
static ngx_http_module_t ngx_http_dyups_module_ctx = {
NULL, /* 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 */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
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, 4,
sizeof(ngx_http_dyups_srv_conf_t))
!= NGX_OK)
{
return NULL;
}
dmcf->enable = NGX_CONF_UNSET;
return dmcf;
}
static char *
ngx_http_dyups_init_main_conf(ngx_conf_t *cf, void *conf)
{
ngx_http_dyups_main_conf_t *dmcf = conf;
dmcf->enable = dmcf->enable == NGX_CONF_UNSET ? 0 : 1;
return NGX_CONF_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_uint_t i;
ngx_http_dyups_srv_conf_t *duscf;
ngx_http_dyups_main_conf_t *dmcf;
ngx_http_upstream_srv_conf_t **uscfp, *uscf;
ngx_http_upstream_main_conf_t *umcf;
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++) {
uscf = uscfp[i];
#if NGX_DEBUG
ngx_log_error(NGX_LOG_EMERG, cf->log, 0, "%ui: %V", i, &uscf->host);
#endif
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 = ngx_create_pool(128, cf->log);
if (duscf->pool == NULL) {
return NGX_ERROR;
}
duscf->upstream = uscfp[i];
duscf->dynamic = (uscfp[i]->port == 0
&& uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE);
duscf->deleted = 0;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_dyups_interface_handler(ngx_http_request_t *r)
{
ngx_array_t *res;
res = ngx_http_dyups_parse_path(r);
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);
}
static ngx_int_t
ngx_http_dyups_do_get(ngx_http_request_t *r, ngx_array_t *resource)
{
ngx_int_t rc, status, dumy;
ngx_buf_t *buf;
ngx_str_t *value;
ngx_chain_t out;
ngx_http_dyups_srv_conf_t *duscf;
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 (value[0].len == 6
&& ngx_strncasecmp(value[0].data, (u_char *) "detail", 6) == 0)
{
buf = ngx_http_dyups_show_detail(r);
if (buf == NULL) {
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
goto finish;
}
}
if (value[0].len == 8
&& ngx_strncasecmp(value[0].data, (u_char *) "upstream", 8) == 0)
{
if (resource->nelts != 2) {
status = NGX_HTTP_NOT_FOUND;
goto finish;
}
duscf = ngx_dyups_find_upstream(&value[1], &dumy);
if (duscf == NULL || duscf->deleted) {
status = NGX_HTTP_NOT_FOUND;
goto finish;
}
buf = ngx_http_dyups_show_upstream(r, duscf);
if (buf == NULL) {
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
goto finish;
}
}
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;
}
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_buf_t *
ngx_http_dyups_show_list(ngx_http_request_t *r)
{
ngx_uint_t i, len;
ngx_str_t host;
ngx_buf_t *buf;
ngx_http_dyups_srv_conf_t *duscfs, *duscf;
ngx_http_dyups_main_conf_t *dumcf;
dumcf = ngx_http_get_module_main_conf(r, ngx_http_dyups_module);
len = 0;
duscfs = dumcf->dy_upstreams.elts;
for (i = 0; i < dumcf->dy_upstreams.nelts; i++) {
duscf = &duscfs[i];
if (!duscf->dynamic) {
continue;
}
if (duscf->deleted) {
continue;
}
len += duscf->upstream->host.len + 1;
}
buf = ngx_create_temp_buf(r->pool, len);
if (buf == NULL) {
return NULL;
}
for (i = 0; i < dumcf->dy_upstreams.nelts; i++) {
duscf = &duscfs[i];
if (!duscf->dynamic) {
continue;
}
if (duscf->deleted) {
continue;
}
host = duscf->upstream->host;
buf->last = ngx_sprintf(buf->last, "%V\n", &host);
}
return buf;
}
static ngx_buf_t *
ngx_http_dyups_show_detail(ngx_http_request_t *r)
{
ngx_uint_t i, j, len;
ngx_str_t host;
ngx_buf_t *buf;
ngx_http_dyups_srv_conf_t *duscfs, *duscf;
ngx_http_dyups_main_conf_t *dumcf;
ngx_http_upstream_server_t *us;
dumcf = ngx_http_get_module_main_conf(r, ngx_http_dyups_module);
len = 0;
duscfs = dumcf->dy_upstreams.elts;
for (i = 0; i < dumcf->dy_upstreams.nelts; i++) {
duscf = &duscfs[i];
if (!duscf->dynamic) {
continue;
}
if (duscf->deleted) {
continue;
}
len += duscf->upstream->host.len + 1;
for (j = 0; j < duscf->upstream->servers->nelts; j++) {
len += sizeof("server ") + 81;
}
}
buf = ngx_create_temp_buf(r->pool, len);
if (buf == NULL) {
return NULL;
}
for (i = 0; i < dumcf->dy_upstreams.nelts; i++) {
duscf = &duscfs[i];
if (!duscf->dynamic) {
continue;
}
if (duscf->deleted) {
continue;
}
host = duscf->upstream->host;
buf->last = ngx_sprintf(buf->last, "%V\n", &host);
us = duscf->upstream->servers->elts;
for (j = 0; j < duscf->upstream->servers->nelts; j++) {
buf->last = ngx_sprintf(buf->last, "server %V\n",
&us[j].addrs->name);
}
buf->last = ngx_sprintf(buf->last, "\n");
}
return buf;
}
static ngx_buf_t *
ngx_http_dyups_show_upstream(ngx_http_request_t *r,
ngx_http_dyups_srv_conf_t *duscf)
{
ngx_uint_t i, len;
ngx_buf_t *buf;
ngx_http_upstream_server_t *us;
len = 0;
for (i = 0; i < duscf->upstream->servers->nelts; i++) {
len += sizeof("server ") + 81;
}
buf = ngx_create_temp_buf(r->pool, len);
if (buf == NULL) {
return NULL;
}
us = duscf->upstream->servers->elts;
for (i = 0; i < duscf->upstream->servers->nelts; i++) {
buf->last = ngx_sprintf(buf->last, "server %V\n",
&us[i].addrs->name);
}
return buf;
}
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 dumy, status, rc;
ngx_buf_t *b;
ngx_chain_t out;
ngx_http_dyups_srv_conf_t *duscf;
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];
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"delete upstream name: %V", &name);
duscf = ngx_dyups_find_upstream(&name, &dumy);
if (duscf == NULL || duscf->deleted) {
ngx_str_set(&rv, "not found uptream");
status = NGX_HTTP_NOT_FOUND;
goto finish;
}
rc = ngx_dyups_delete_upstream(duscf);
if (rc != NGX_OK) {
ngx_str_set(&rv, "failed");
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
goto finish;
}
ngx_str_set(&rv, "success");
status = NGX_HTTP_OK;
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 rv;
ngx_int_t status;
ngx_buf_t *body;
ngx_array_t *arglist, *res;
res = ngx_http_dyups_parse_path(r);
if (res == NULL) {
ngx_str_set(&rv, "out of memory");
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
goto failed;
}
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,
"interface no content");
ngx_str_set(&rv, "no content\n");
goto failed;
}
if (r->request_body->temp_file) {
body = ngx_http_dyups_read_body_from_file(r);
} else {
body = ngx_http_dyups_read_body(r);
}
ngx_str_set(&rv, "");
if (body == NULL) {
status = NGX_HTTP_INTERNAL_SERVER_ERROR;
ngx_str_set(&rv, "out of memory\n");
goto failed;
}
arglist = ngx_dyups_parse_content(r->pool, body);
if (arglist == NULL) {
status = NGX_HTTP_BAD_REQUEST;
ngx_str_set(&rv, "parse body error\n");
goto failed;
}
switch(r->method) {
case NGX_HTTP_POST:
status = ngx_http_dyups_do_post(r, res, arglist, &rv);
break;
case NGX_HTTP_PUT:
status = ngx_http_dyups_do_put(r, res, arglist, &rv);
break;
default:
status = NGX_HTTP_NOT_ALLOWED;
break;
}
failed:
ngx_http_dyups_send_response(r, status, &rv);
}
/*
url: /upstream
body: server ip:port weight
*/
static ngx_int_t
ngx_http_dyups_do_post(ngx_http_request_t *r, ngx_array_t *resource,
ngx_array_t *arglist, ngx_str_t *rv)
{
ngx_int_t rc, idx;
ngx_str_t *value, name;
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;
umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
dumcf = ngx_http_get_module_main_conf(r, ngx_http_dyups_module);
if (resource->nelts != 2) {
ngx_str_set(rv, "not support this interface");
return NGX_HTTP_NOT_FOUND;
}
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");
return NGX_HTTP_NOT_FOUND;
}
name = value[1];
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"upstream name: %V", &name);
rc = ngx_http_dyups_check_commands(arglist);
if (rc != NGX_OK) {
ngx_str_set(rv, "commands error");
return NGX_HTTP_NOT_ALLOWED;
}
duscf = ngx_dyups_find_upstream(&name, &idx);
if (idx == -1) {
/* need create a new upstream */
duscf = ngx_array_push(&dumcf->dy_upstreams);
if (duscf == NULL) {
ngx_str_set(rv, "out of memory");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
uscfp = ngx_array_push(&umcf->upstreams);
if (uscfp == NULL) {
ngx_str_set(rv, "out of memory");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_memzero(duscf, sizeof(ngx_http_dyups_srv_conf_t));
idx = umcf->upstreams.nelts - 1;
}
if (duscf->deleted) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"upstream reuse");
duscf->deleted = 0;
}
rc = ngx_dyups_init_upstream(duscf, &name, idx);
if (rc != NGX_OK) {
ngx_str_set(rv, "failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
/* init upstream */
rc = ngx_dyups_add_server(duscf, arglist);
if (rc != NGX_OK) {
ngx_str_set(rv, "failed");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
ngx_str_set(rv, "success");
return NGX_HTTP_OK;
}
static ngx_int_t
ngx_dyups_add_server(ngx_http_dyups_srv_conf_t *duscf, ngx_array_t *arglist)
{
ngx_url_t u;
ngx_str_t *value;
ngx_conf_t cf;
ngx_uint_t i;
ngx_array_t *line;
ngx_http_upstream_init_pt init;
ngx_http_upstream_server_t *us;
ngx_http_upstream_srv_conf_t *uscf;
ngx_http_dyups_upstream_srv_conf_t *dscf;
uscf = duscf->upstream;
if (uscf->servers == NULL) {
uscf->servers = ngx_array_create(duscf->pool, 4,
sizeof(ngx_http_upstream_server_t));
if (uscf->servers == NULL) {
return NGX_ERROR;
}
}
line = arglist->elts;
for (i = 0; i < arglist->nelts; i++) {
value = line[i].elts;
if (value[0].len == 6
&& ngx_strncasecmp(value[0].data, (u_char *) "server", 6) == 0)
{
us = ngx_array_push(uscf->servers);
if (us == NULL) {
return NGX_ERROR;
}
ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
u.url = value[1];
u.default_port = 80;
/* TODO: parse ip*/
if (ngx_parse_url(duscf->pool, &u) != NGX_OK) {
if (u.err) {
ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, 0,
"%s in upstream \"%V\"", u.err, &u.url);
}
return NGX_ERROR;
}
us->addrs = u.addrs;
us->naddrs = u.naddrs;
us->weight = 1;
us->max_fails = 1;
us->fail_timeout = 10;
}
}
cf.pool = duscf->pool;
cf.module_type = NGX_HTTP_MODULE;
cf.cmd_type = NGX_HTTP_MAIN_CONF;
cf.log = ngx_cycle->log;
init = uscf->peer.init_upstream ? uscf->peer.init_upstream:
ngx_http_upstream_init_round_robin;
if (init(&cf, uscf) != NGX_OK) {
return NGX_ERROR;
}
dscf = uscf->srv_conf[ngx_http_dyups_module.ctx_index];
dscf->init = uscf->peer.init;
uscf->peer.init = ngx_http_dyups_init_peer;
return NGX_OK;
}
static ngx_int_t
ngx_http_dyups_do_put(ngx_http_request_t *r, ngx_array_t *resource,
ngx_array_t *arglist, ngx_str_t *rv)
{
return NGX_HTTP_OK;
}
static ngx_http_dyups_srv_conf_t *
ngx_dyups_find_upstream(ngx_str_t *name, ngx_int_t *idx)
{
ngx_uint_t i;
ngx_http_dyups_srv_conf_t *duscfs, *duscf, *duscf_del;
ngx_http_dyups_main_conf_t *dumcf;
ngx_http_upstream_srv_conf_t *uscf;
ngx_http_upstream_main_conf_t *umcf;
umcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_upstream_module);
dumcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_dyups_module);
*idx = -1;
duscf_del = NULL;
duscfs = dumcf->dy_upstreams.elts;
for (i = 0; i < dumcf->dy_upstreams.nelts; i++) {
duscf = &duscfs[i];
if (!duscf->dynamic) {
continue;
}
if (duscf->deleted == NGX_DYUPS_DELETING && *(duscf->count) == 0) {
if (duscf->pool) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"free dynamic upstream");
ngx_destroy_pool(duscf->pool);
duscf->pool = NULL;
}
duscf->deleted = NGX_DYUPS_DELETED;
}
if (duscf->deleted == NGX_DYUPS_DELETED) {
*idx = i;
duscf_del = duscf;
continue;
}
uscf = duscf->upstream;
if (uscf->host.len != name->len
|| ngx_strncasecmp(uscf->host.data, name->data, uscf->host.len)
!= 0)
{
continue;
}
if (duscf->count != NULL && *(duscf->count) != 0) {
(void) ngx_dyups_delete_upstream(duscf);
continue;
}
*idx = i;
return duscf;
}
return duscf_del;
}
static ngx_int_t
ngx_dyups_init_upstream(ngx_http_dyups_srv_conf_t *duscf, ngx_str_t *name,
ngx_uint_t index)
{
void *mconf;
ngx_uint_t m;
ngx_conf_t cf;
ngx_http_module_t *module;
ngx_http_conf_ctx_t *ctx;
ngx_http_upstream_srv_conf_t *uscf, **uscfp;
ngx_http_upstream_main_conf_t *umcf;
ngx_http_dyups_upstream_srv_conf_t *dscf;
umcf = ngx_http_cycle_get_module_main_conf(ngx_cycle,
ngx_http_upstream_module);
uscfp = umcf->upstreams.elts;
if (duscf->pool) {
ngx_destroy_pool(duscf->pool);
}
uscf = duscf->upstream;
duscf->pool = ngx_create_pool(128, ngx_cycle->log);
if (duscf->pool == NULL) {
return NGX_ERROR;
}
uscf = ngx_pcalloc(duscf->pool, sizeof(ngx_http_upstream_srv_conf_t));
if (uscf == NULL) {
return NGX_ERROR;
}
uscf->flags = NGX_HTTP_UPSTREAM_CREATE
|NGX_HTTP_UPSTREAM_WEIGHT
|NGX_HTTP_UPSTREAM_MAX_FAILS
|NGX_HTTP_UPSTREAM_FAIL_TIMEOUT
|NGX_HTTP_UPSTREAM_DOWN
|NGX_HTTP_UPSTREAM_BACKUP;
uscf->host.data = ngx_pstrdup(duscf->pool, name);
uscf->host.len = name->len;
uscf->file_name = (u_char *) "dynamic_upstream";
uscf->line = 0;
uscf->port = 0;
uscf->default_port = 0;
uscfp[index] = uscf;
duscf->dynamic = 1;
duscf->upstream = uscf;
cf.module_type = NGX_HTTP_MODULE;
cf.cmd_type = NGX_HTTP_MAIN_CONF;
cf.pool = duscf->pool;
ctx = ngx_pcalloc(duscf->pool, sizeof(ngx_http_conf_ctx_t));
if (ctx == NULL) {
return NGX_ERROR;
}
ctx->main_conf = ((ngx_http_conf_ctx_t *)
ngx_cycle->conf_ctx[ngx_http_module.index])->main_conf;
ctx->srv_conf = ngx_pcalloc(cf.pool, sizeof(void *) * ngx_http_max_module);
if (ctx->srv_conf == NULL) {
return NGX_ERROR;
}
ctx->srv_conf[ngx_http_upstream_module.ctx_index] = uscf;
uscf->srv_conf = ctx->srv_conf;
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
continue;
}
if (ngx_modules[m]->index == ngx_http_core_module.index) {
continue;
}
module = ngx_modules[m]->ctx;
if (module->create_srv_conf) {
mconf = module->create_srv_conf(&cf);
if (mconf == NULL) {
return NGX_ERROR;
}
ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
}
}
dscf = uscf->srv_conf[ngx_http_dyups_module.ctx_index];
duscf->count = &dscf->count;
return NGX_OK;