Skip to content
Navigation Menu
{{ message }}
forked from unbit/uwsgi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouting.c
More file actions
2277 lines (1967 loc) · 75 KB
/
Copy pathrouting.c
File metadata and controls
2277 lines (1967 loc) · 75 KB
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
#ifdef UWSGI_ROUTING
#include <uwsgi.h>
extern struct uwsgi_server uwsgi;
struct uwsgi_route_var *uwsgi_register_route_var(char *name, char *(*func)(struct wsgi_request *, char *, uint16_t, uint16_t *)) {
struct uwsgi_route_var *old_urv = NULL,*urv = uwsgi.route_vars;
while(urv) {
if (!strcmp(urv->name, name)) {
return urv;
}
old_urv = urv;
urv = urv->next;
}
urv = uwsgi_calloc(sizeof(struct uwsgi_route_var));
urv->name = name;
urv->name_len = strlen(name);
urv->func = func;
if (old_urv) {
old_urv->next = urv;
}
else {
uwsgi.route_vars = urv;
}
return urv;
}
struct uwsgi_route_var *uwsgi_get_route_var(char *name, uint16_t name_len) {
struct uwsgi_route_var *urv = uwsgi.route_vars;
while(urv) {
if (!uwsgi_strncmp(urv->name, urv->name_len, name, name_len)) {
return urv;
}
urv = urv->next;
}
return NULL;
}
struct uwsgi_buffer *uwsgi_routing_translate(struct wsgi_request *wsgi_req, struct uwsgi_route *ur, char *subject, uint16_t subject_len, char *data, size_t data_len) {
char *pass1 = data;
size_t pass1_len = data_len;
if (ur->condition_ub[wsgi_req->async_id] && ur->ovn[wsgi_req->async_id] > 0) {
pass1 = uwsgi_regexp_apply_ovec(ur->condition_ub[wsgi_req->async_id]->buf, ur->condition_ub[wsgi_req->async_id]->pos, data, data_len, ur->ovector[wsgi_req->async_id], ur->ovn[wsgi_req->async_id]);
pass1_len = strlen(pass1);
}
// cannot fail
else if (subject) {
pass1 = uwsgi_regexp_apply_ovec(subject, subject_len, data, data_len, ur->ovector[wsgi_req->async_id], ur->ovn[wsgi_req->async_id]);
pass1_len = strlen(pass1);
}
struct uwsgi_buffer *ub = uwsgi_buffer_new(pass1_len);
size_t i;
int status = 0;
char *key = NULL;
size_t keylen = 0;
for(i=0;i<pass1_len;i++) {
switch(status) {
case 0:
if (pass1[i] == '$') {
status = 1;
break;
}
if (uwsgi_buffer_append(ub, pass1 + i, 1)) goto error;
break;
case 1:
if (pass1[i] == '{') {
status = 2;
key = pass1+i+1;
keylen = 0;
break;
}
status = 0;
key = NULL;
keylen = 0;
if (uwsgi_buffer_append(ub, "$", 1)) goto error;
if (uwsgi_buffer_append(ub, pass1 + i, 1)) goto error;
break;
case 2:
if (pass1[i] == '}') {
uint16_t vallen = 0;
int need_free = 0;
char *value = NULL;
char *bracket = memchr(key, '[', keylen);
if (bracket && keylen > 0 && key[keylen-1] == ']') {
struct uwsgi_route_var *urv = uwsgi_get_route_var(key, bracket - key);
if (urv) {
need_free = urv->need_free;
value = urv->func(wsgi_req, bracket + 1, keylen - (urv->name_len+2), &vallen);
}
else {
value = uwsgi_get_var(wsgi_req, key, keylen, &vallen);
}
}
else {
value = uwsgi_get_var(wsgi_req, key, keylen, &vallen);
}
if (value) {
if (uwsgi_buffer_append(ub, value, vallen)) {
if (need_free) {
free(value);
}
goto error;
}
if (need_free) {
free(value);
}
}
status = 0;
key = NULL;
keylen = 0;
break;
}
keylen++;
break;
default:
break;
}
}
// fix the buffer
if (status == 1) {
if (uwsgi_buffer_append(ub, "$", 1)) goto error;
}
else if (status == 2) {
if (uwsgi_buffer_append(ub, "${", 2)) goto error;
if (keylen > 0) {
if (uwsgi_buffer_append(ub, key, keylen)) goto error;
}
}
// add the final NULL byte (to simplify plugin work)
if (uwsgi_buffer_append(ub, "\0", 1)) goto error;
// .. but came back of 1 position to avoid accounting it
ub->pos--;
if (pass1 != data) {
free(pass1);
}
return ub;
error:
uwsgi_buffer_destroy(ub);
return NULL;
}
static void uwsgi_routing_reset_memory(struct wsgi_request *wsgi_req, struct uwsgi_route *routes) {
// free dynamic memory structures
if (routes->if_func) {
routes->ovn[wsgi_req->async_id] = 0;
if (routes->ovector[wsgi_req->async_id]) {
free(routes->ovector[wsgi_req->async_id]);
routes->ovector[wsgi_req->async_id] = NULL;
}
if (routes->condition_ub[wsgi_req->async_id]) {
uwsgi_buffer_destroy(routes->condition_ub[wsgi_req->async_id]);
routes->condition_ub[wsgi_req->async_id] = NULL;
}
}
}
int uwsgi_apply_routes_do(struct uwsgi_route *routes, struct wsgi_request *wsgi_req, char *subject, uint16_t subject_len) {
int n = -1;
char *orig_subject = subject;
uint16_t orig_subject_len = subject_len;
uint32_t *r_goto = &wsgi_req->route_goto;
uint32_t *r_pc = &wsgi_req->route_pc;
if (routes == uwsgi.error_routes) {
r_goto = &wsgi_req->error_route_goto;
r_pc = &wsgi_req->error_route_pc;
}
else if (routes == uwsgi.response_routes) {
r_goto = &wsgi_req->response_route_goto;
r_pc = &wsgi_req->response_route_pc;
}
else if (routes == uwsgi.final_routes) {
r_goto = &wsgi_req->final_route_goto;
r_pc = &wsgi_req->final_route_pc;
}
while (routes) {
if (routes->label) goto next;
if (*r_goto > 0 && *r_pc < *r_goto) {
goto next;
}
*r_goto = 0;
if (!routes->if_func) {
// could be a "run"
if (!routes->subject) {
n = 0;
goto run;
}
if (!subject) {
char **subject2 = (char **) (((char *) (wsgi_req)) + routes->subject);
uint16_t *subject_len2 = (uint16_t *) (((char *) (wsgi_req)) + routes->subject_len);
subject = *subject2 ;
subject_len = *subject_len2;
}
n = uwsgi_regexp_match_ovec(routes->pattern, routes->pattern_extra, subject, subject_len, routes->ovector[wsgi_req->async_id], routes->ovn[wsgi_req->async_id]);
}
else {
int ret = routes->if_func(wsgi_req, routes);
// error
if (ret < 0) {
uwsgi_routing_reset_memory(wsgi_req, routes);
return UWSGI_ROUTE_BREAK;
}
// true
if (!routes->if_negate) {
if (ret == 0) {
uwsgi_routing_reset_memory(wsgi_req, routes);
goto next;
}
n = ret;
}
else {
if (ret > 0) {
uwsgi_routing_reset_memory(wsgi_req, routes);
goto next;
}
n = 1;
}
}
run:
if (n >= 0) {
wsgi_req->is_routing = 1;
int ret = routes->func(wsgi_req, routes);
uwsgi_routing_reset_memory(wsgi_req, routes);
wsgi_req->is_routing = 0;
if (ret == UWSGI_ROUTE_BREAK) {
uwsgi.workers[uwsgi.mywid].cores[wsgi_req->async_id].routed_requests++;
return ret;
}
if (ret == UWSGI_ROUTE_CONTINUE) {
return ret;
}
if (ret == -1) {
return UWSGI_ROUTE_BREAK;
}
}
next:
subject = orig_subject;
subject_len = orig_subject_len;
routes = routes->next;
if (routes) *r_pc = *r_pc+1;
}
return UWSGI_ROUTE_CONTINUE;
}
int uwsgi_apply_routes(struct wsgi_request *wsgi_req) {
if (!uwsgi.routes)
return UWSGI_ROUTE_CONTINUE;
// avoid loops
if (wsgi_req->is_routing)
return UWSGI_ROUTE_CONTINUE;
if (uwsgi_parse_vars(wsgi_req)) {
return UWSGI_ROUTE_BREAK;
}
// in case of static files serving previous rules could be applied
if (wsgi_req->routes_applied) {
return UWSGI_ROUTE_CONTINUE;
}
return uwsgi_apply_routes_do(uwsgi.routes, wsgi_req, NULL, 0);
}
void uwsgi_apply_final_routes(struct wsgi_request *wsgi_req) {
if (!uwsgi.final_routes) return;
// avoid loops
if (wsgi_req->is_routing) return;
wsgi_req->is_final_routing = 1;
uwsgi_apply_routes_do(uwsgi.final_routes, wsgi_req, NULL, 0);
}
int uwsgi_apply_error_routes(struct wsgi_request *wsgi_req) {
if (!uwsgi.error_routes) return 0;
// do not forget to check it !!!
if (wsgi_req->is_error_routing) return 0;
wsgi_req->is_error_routing = 1;
return uwsgi_apply_routes_do(uwsgi.error_routes, wsgi_req, NULL, 0);
}
int uwsgi_apply_response_routes(struct wsgi_request *wsgi_req) {
if (!uwsgi.response_routes) return 0;
if (wsgi_req->response_routes_applied) return 0;
// do not forget to check it !!!
if (wsgi_req->is_response_routing) return 0;
wsgi_req->is_response_routing = 1;
int ret = uwsgi_apply_routes_do(uwsgi.response_routes, wsgi_req, NULL, 0);
wsgi_req->response_routes_applied = 1;
return ret;
}
static void *uwsgi_route_get_condition_func(char *name) {
struct uwsgi_route_condition *urc = uwsgi.route_conditions;
while(urc) {
if (!strcmp(urc->name, name)) {
return urc->func;
}
urc = urc->next;
}
return NULL;
}
static int uwsgi_route_condition_status(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
if (wsgi_req->status == ur->if_status) {
return 1;
}
return 0;
}
void uwsgi_opt_add_route(char *opt, char *value, void *foobar) {
char *space = NULL;
char *command = NULL;
struct uwsgi_route *old_ur = NULL, *ur = uwsgi.routes;
if (!uwsgi_starts_with(opt, strlen(opt), "final", 5)) {
ur = uwsgi.final_routes;
}
else if (!uwsgi_starts_with(opt, strlen(opt), "error", 5)) {
ur = uwsgi.error_routes;
}
else if (!uwsgi_starts_with(opt, strlen(opt), "response", 8)) {
ur = uwsgi.response_routes;
}
uint64_t pos = 0;
while(ur) {
old_ur = ur;
ur = ur->next;
pos++;
}
ur = uwsgi_calloc(sizeof(struct uwsgi_route));
if (old_ur) {
old_ur->next = ur;
}
else {
if (!uwsgi_starts_with(opt, strlen(opt), "final", 5)) {
uwsgi.final_routes = ur;
}
else if (!uwsgi_starts_with(opt, strlen(opt), "error", 5)) {
uwsgi.error_routes = ur;
}
else if (!uwsgi_starts_with(opt, strlen(opt), "response", 8)) {
uwsgi.response_routes = ur;
}
else {
uwsgi.routes = ur;
}
}
ur->pos = pos;
// is it a label ?
if (foobar == NULL) {
ur->label = value;
ur->label_len = strlen(value);
return;
}
ur->orig_route = uwsgi_str(value);
if (!strcmp(foobar, "run")) {
command = ur->orig_route;
goto done;
}
space = strchr(ur->orig_route, ' ');
if (!space) {
uwsgi_log("invalid route syntax\n");
exit(1);
}
*space = 0;
if (!strcmp(foobar, "if") || !strcmp(foobar, "if-not")) {
char *colon = strchr(ur->orig_route, ':');
if (!colon) {
uwsgi_log("invalid route condition syntax\n");
exit(1);
}
*colon = 0;
if (!strcmp(foobar, "if-not")) {
ur->if_negate = 1;
}
foobar = colon+1;
ur->if_func = uwsgi_route_get_condition_func(ur->orig_route);
if (!ur->if_func) {
uwsgi_log("unable to find \"%s\" route condition\n", ur->orig_route);
exit(1);
}
}
else if (!strcmp(foobar, "status")) {
ur->if_status = atoi(ur->orig_route);
foobar = ur->orig_route;
ur->if_func = uwsgi_route_condition_status;
}
else if (!strcmp(foobar, "http_host")) {
ur->subject = offsetof(struct wsgi_request, host);
ur->subject_len = offsetof(struct wsgi_request, host_len);
}
else if (!strcmp(foobar, "request_uri")) {
ur->subject = offsetof(struct wsgi_request, uri);
ur->subject_len = offsetof(struct wsgi_request, uri_len);
}
else if (!strcmp(foobar, "query_string")) {
ur->subject = offsetof(struct wsgi_request, query_string);
ur->subject_len = offsetof(struct wsgi_request, query_string_len);
}
else if (!strcmp(foobar, "remote_addr")) {
ur->subject = offsetof(struct wsgi_request, remote_addr);
ur->subject_len = offsetof(struct wsgi_request, remote_addr_len);
}
else if (!strcmp(foobar, "user_agent")) {
ur->subject = offsetof(struct wsgi_request, user_agent);
ur->subject_len = offsetof(struct wsgi_request, user_agent_len);
}
else if (!strcmp(foobar, "referer")) {
ur->subject = offsetof(struct wsgi_request, referer);
ur->subject_len = offsetof(struct wsgi_request, referer_len);
}
else if (!strcmp(foobar, "remote_user")) {
ur->subject = offsetof(struct wsgi_request, remote_user);
ur->subject_len = offsetof(struct wsgi_request, remote_user_len);
}
else {
ur->subject = offsetof(struct wsgi_request, path_info);
ur->subject_len = offsetof(struct wsgi_request, path_info_len);
}
ur->subject_str = foobar;
ur->subject_str_len = strlen(ur->subject_str);
ur->regexp = ur->orig_route;
command = space + 1;
done:
ur->action = uwsgi_str(command);
char *colon = strchr(command, ':');
if (!colon) {
uwsgi_log("invalid route syntax\n");
exit(1);
}
*colon = 0;
struct uwsgi_router *r = uwsgi.routers;
while (r) {
if (!strcmp(r->name, command)) {
if (r->func(ur, colon + 1) == 0) {
return;
}
break;
}
r = r->next;
}
uwsgi_log("unable to register route \"%s\". Missing router for: \"%s\"\n", value, command);
exit(1);
}
void uwsgi_fixup_routes(struct uwsgi_route *ur) {
while(ur) {
// prepare the main pointers
ur->ovn = uwsgi_calloc(sizeof(int) * uwsgi.cores);
ur->ovector = uwsgi_calloc(sizeof(int *) * uwsgi.cores);
ur->condition_ub = uwsgi_calloc( sizeof(struct uwsgi_buffer *) * uwsgi.cores);
// fill them if needed... (this is an optimization for route with a static subject)
if (ur->subject && ur->subject_len) {
if (uwsgi_regexp_build(ur->orig_route, &ur->pattern, &ur->pattern_extra)) {
exit(1);
}
int i;
for(i=0;i<uwsgi.cores;i++) {
ur->ovn[i] = uwsgi_regexp_ovector(ur->pattern, ur->pattern_extra);
if (ur->ovn[i] > 0) {
ur->ovector[i] = uwsgi_calloc(sizeof(int) * (3 * (ur->ovn[i] + 1)));
}
}
}
ur = ur->next;
}
}
int uwsgi_route_api_func(struct wsgi_request *wsgi_req, char *router, char *args) {
struct uwsgi_route *ur = NULL;
struct uwsgi_router *r = uwsgi.routers;
while(r) {
if (!strcmp(router, r->name)) {
goto found;
}
r = r->next;
}
free(args);
return -1;
found:
ur = uwsgi_calloc(sizeof(struct uwsgi_route));
// initialize the virtual route
if (r->func(ur, args)) {
free(ur);
free(args);
return -1;
}
// call it
int ret = ur->func(wsgi_req, ur);
if (ur->free) {
ur->free(ur);
}
free(ur);
free(args);
return ret;
}
// continue/last route
static int uwsgi_router_continue_func(struct wsgi_request *wsgi_req, struct uwsgi_route *route) {
return UWSGI_ROUTE_CONTINUE;
}
static int uwsgi_router_continue(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_continue_func;
return 0;
}
// break route
static int uwsgi_router_break_func(struct wsgi_request *wsgi_req, struct uwsgi_route *route) {
if (route->data_len >= 3) {
if (uwsgi_response_prepare_headers(wsgi_req, route->data, route->data_len)) goto end;
if (uwsgi_response_add_connection_close(wsgi_req)) goto end;
if (uwsgi_response_add_content_type(wsgi_req, "text/plain", 10)) goto end;
// no need to check for return value
uwsgi_response_write_headers_do(wsgi_req);
}
end:
return UWSGI_ROUTE_BREAK;
}
static int uwsgi_router_break(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_break_func;
ur->data = arg;
ur->data_len = strlen(arg);
return 0;
}
static int uwsgi_router_return_func(struct wsgi_request *wsgi_req, struct uwsgi_route *route)
{
if (route->data_len < 3)
return UWSGI_ROUTE_BREAK;
uint16_t status_msg_len = 0;
const char *status_msg = uwsgi_http_status_msg(route->data, &status_msg_len);
if (!status_msg)
return UWSGI_ROUTE_BREAK;
char *buf = uwsgi_concat3n(route->data, route->data_len, " ", 1, (char *) status_msg, status_msg_len);
if (uwsgi_response_prepare_headers(wsgi_req, buf, route->data_len + 1 + status_msg_len))
goto end;
if (uwsgi_response_add_content_type(wsgi_req, "text/plain", 10))
goto end;
if (uwsgi_response_add_content_length(wsgi_req, status_msg_len))
goto end;
uwsgi_response_write_body_do(wsgi_req, (char *) status_msg, status_msg_len);
end:
free(buf);
return UWSGI_ROUTE_BREAK;
}
static int uwsgi_router_return(struct uwsgi_route *ur, char *arg)
{
ur->func = uwsgi_router_return_func;
ur->data = arg;
ur->data_len = strlen(arg);
return 0;
}
// simple math router
static int uwsgi_router_simple_math_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
uint16_t var_vallen = 0;
char *var_value = uwsgi_get_var(wsgi_req, ur->data, ur->data_len, &var_vallen);
if (!var_value) return UWSGI_ROUTE_BREAK;
int64_t base_value = uwsgi_str_num(var_value, var_vallen);
int64_t value = 1;
if (ur->data2_len) {
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data2, ur->data2_len);
if (!ub) return UWSGI_ROUTE_BREAK;
value = uwsgi_str_num(ub->buf, ub->pos);
uwsgi_buffer_destroy(ub);
}
char out[sizeof(UMAX64_STR)+1];
int64_t total = 0;
switch(ur->custom) {
// -
case 1:
total = base_value - value;
break;
// *
case 2:
total = base_value * value;
break;
// /
case 3:
if (value == 0) total = 0;
else {
total = base_value/value;
}
break;
default:
total = base_value + value;
break;
}
int ret = uwsgi_long2str2n(total, out, sizeof(UMAX64_STR)+1);
if (ret <= 0) return UWSGI_ROUTE_BREAK;
if (!uwsgi_req_append(wsgi_req, ur->data, ur->data_len, out, ret)) {
return UWSGI_ROUTE_BREAK;
}
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_simple_math_plus(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_simple_math_func;
char *comma = strchr(arg, ',');
if (comma) {
ur->data = arg;
ur->data_len = comma - arg;
ur->data2 = comma+1;
ur->data2_len = strlen(ur->data);
}
else {
ur->data = arg;
ur->data_len = strlen(arg);
}
return 0;
}
static int uwsgi_router_simple_math_minus(struct uwsgi_route *ur, char *arg) {
ur->custom = 1;
return uwsgi_router_simple_math_plus(ur, arg);
}
static int uwsgi_router_simple_math_multiply(struct uwsgi_route *ur, char *arg) {
ur->custom = 2;
return uwsgi_router_simple_math_plus(ur, arg);
}
static int uwsgi_router_simple_math_divide(struct uwsgi_route *ur, char *arg) {
ur->custom = 2;
return uwsgi_router_simple_math_plus(ur, arg);
}
// harakiri router
static int uwsgi_router_harakiri_func(struct wsgi_request *wsgi_req, struct uwsgi_route *route) {
if (route->custom > 0) {
set_user_harakiri(wsgi_req, route->custom);
}
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_harakiri(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_harakiri_func;
ur->custom = atoi(arg);
return 0;
}
// flush response
static int transform_flush(struct wsgi_request *wsgi_req, struct uwsgi_transformation *ut) {
// avoid loops !!!
if (ut->chunk->pos == 0) return 0;
wsgi_req->transformed_chunk = ut->chunk->buf;
wsgi_req->transformed_chunk_len = ut->chunk->pos;
int ret = uwsgi_response_write_body_do(wsgi_req, ut->chunk->buf, ut->chunk->pos);
wsgi_req->transformed_chunk = NULL;
wsgi_req->transformed_chunk_len = 0;
ut->flushed = 1;
return ret;
}
static int uwsgi_router_flush_func(struct wsgi_request *wsgi_req, struct uwsgi_route *route) {
struct uwsgi_transformation *ut = uwsgi_add_transformation(wsgi_req, transform_flush, NULL);
ut->can_stream = 1;
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_flush(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_flush_func;
return 0;
}
// fix content length
static int transform_fixcl(struct wsgi_request *wsgi_req, struct uwsgi_transformation *ut) {
char buf[sizeof(UMAX64_STR)+1];
int ret = snprintf(buf, sizeof(UMAX64_STR)+1, "%llu", (unsigned long long) ut->chunk->pos);
if (ret <= 0 || ret >= (int) (sizeof(UMAX64_STR)+1)) {
wsgi_req->write_errors++;
return -1;
}
// do not check for errors !!!
uwsgi_response_add_header(wsgi_req, "Content-Length", 14, buf, ret);
return 0;
}
static int uwsgi_router_fixcl_func(struct wsgi_request *wsgi_req, struct uwsgi_route *route) {
uwsgi_add_transformation(wsgi_req, transform_fixcl, NULL);
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_fixcl(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_fixcl_func;
return 0;
}
// force content length
static int transform_forcecl(struct wsgi_request *wsgi_req, struct uwsgi_transformation *ut) {
char buf[sizeof(UMAX64_STR)+1];
int ret = snprintf(buf, sizeof(UMAX64_STR)+1, "%llu", (unsigned long long) ut->chunk->pos);
if (ret <= 0 || ret >= (int) (sizeof(UMAX64_STR)+1)) {
wsgi_req->write_errors++;
return -1;
}
// do not check for errors !!!
uwsgi_response_add_header_force(wsgi_req, "Content-Length", 14, buf, ret);
return 0;
}
static int uwsgi_router_forcecl_func(struct wsgi_request *wsgi_req, struct uwsgi_route *route) {
uwsgi_add_transformation(wsgi_req, transform_forcecl, NULL);
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_forcecl(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_forcecl_func;
return 0;
}
// log route
static int uwsgi_router_log_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data, ur->data_len);
if (!ub) return UWSGI_ROUTE_BREAK;
uwsgi_log("%.*s\n", ub->pos, ub->buf);
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_log(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_log_func;
ur->data = arg;
ur->data_len = strlen(arg);
return 0;
}
// do not log !!!
static int uwsgi_router_donotlog_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
wsgi_req->do_not_log = 1;
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_donotlog(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_donotlog_func;
return 0;
}
// do not offload !!!
static int uwsgi_router_donotoffload_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
wsgi_req->socket->can_offload = 0;
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_donotoffload(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_donotoffload_func;
return 0;
}
// logvar route
static int uwsgi_router_logvar_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data2, ur->data2_len);
if (!ub) return UWSGI_ROUTE_BREAK;
uwsgi_logvar_add(wsgi_req, ur->data, ur->data_len, ub->buf, ub->pos);
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_logvar(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_logvar_func;
char *equal = strchr(arg, '=');
if (!equal) {
uwsgi_log("invalid logvar syntax, must be key=value\n");
exit(1);
}
ur->data = arg;
ur->data_len = equal-arg;
ur->data2 = equal+1;
ur->data2_len = strlen(ur->data2);
return 0;
}
// goto route
static int uwsgi_router_goto_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
// build the label (if needed)
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data, ur->data_len);
if (!ub) return UWSGI_ROUTE_BREAK;
uint32_t *r_goto = &wsgi_req->route_goto;
uint32_t *r_pc = &wsgi_req->route_pc;
// find the label
struct uwsgi_route *routes = uwsgi.routes;
if (wsgi_req->is_error_routing) {
routes = uwsgi.error_routes;
r_goto = &wsgi_req->error_route_goto;
r_pc = &wsgi_req->error_route_pc;
}
else if (wsgi_req->is_final_routing) {
routes = uwsgi.final_routes;
r_goto = &wsgi_req->final_route_goto;
r_pc = &wsgi_req->final_route_pc;
}
else if (wsgi_req->is_response_routing) {
routes = uwsgi.response_routes;
r_goto = &wsgi_req->response_route_goto;
r_pc = &wsgi_req->response_route_pc;
}
while(routes) {
if (!routes->label) goto next;
if (!uwsgi_strncmp(routes->label, routes->label_len, ub->buf, ub->pos)) {
*r_goto = routes->pos;
goto found;
}
next:
routes = routes->next;
}
*r_goto = ur->custom;
found:
uwsgi_buffer_destroy(ub);
if (*r_goto <= *r_pc) {
*r_goto = 0;
uwsgi_log("[uwsgi-route] ERROR \"goto\" instruction can only jump forward (check your label !!!)\n");
return UWSGI_ROUTE_BREAK;
}
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_goto(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_goto_func;
ur->data = arg;
ur->data_len = strlen(arg);
ur->custom = atoi(arg);
return 0;
}
// addvar route
static int uwsgi_router_addvar_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data2, ur->data2_len);
if (!ub) return UWSGI_ROUTE_BREAK;
if (!uwsgi_req_append(wsgi_req, ur->data, ur->data_len, ub->buf, ub->pos)) {
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_BREAK;
}
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_addvar(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_addvar_func;
char *equal = strchr(arg, '=');
if (!equal) {
uwsgi_log("[uwsgi-route] invalid addvar syntax, must be KEY=VAL\n");
exit(1);
}
ur->data = arg;
ur->data_len = equal-arg;
ur->data2 = equal+1;
ur->data2_len = strlen(ur->data2);
return 0;
}
// addheader route
static int uwsgi_router_addheader_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data, ur->data_len);
if (!ub) return UWSGI_ROUTE_BREAK;
uwsgi_additional_header_add(wsgi_req, ub->buf, ub->pos);
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_addheader(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_addheader_func;
ur->data = arg;
ur->data_len = strlen(arg);
return 0;
}
// remheader route
static int uwsgi_router_remheader_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data, ur->data_len);
if (!ub) return UWSGI_ROUTE_BREAK;
uwsgi_remove_header(wsgi_req, ub->buf, ub->pos);
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_NEXT;
}
static int uwsgi_router_remheader(struct uwsgi_route *ur, char *arg) {
ur->func = uwsgi_router_remheader_func;
ur->data = arg;
ur->data_len = strlen(arg);
return 0;
}
// clearheaders route
static int uwsgi_router_clearheaders_func(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) {
char **subject = (char **) (((char *)(wsgi_req))+ur->subject);
uint16_t *subject_len = (uint16_t *) (((char *)(wsgi_req))+ur->subject_len);
struct uwsgi_buffer *ub = uwsgi_routing_translate(wsgi_req, ur, *subject, *subject_len, ur->data, ur->data_len);
if (!ub) return UWSGI_ROUTE_BREAK;
if (uwsgi_response_prepare_headers(wsgi_req, ub->buf, ub->pos)) {
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_BREAK;
}
uwsgi_buffer_destroy(ub);
return UWSGI_ROUTE_NEXT;
}
You can’t perform that action at this time.
