-
Notifications
You must be signed in to change notification settings - Fork 31
/
luaembed.c
2034 lines (1794 loc) · 59.6 KB
/
luaembed.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
/*
* solbox could storage accelerator
*
* KEEP IN MIND!
* (1) 성능을 생각할 것!
* - switch 문은 되도록 사용말 것
* - memcpy, strcpy등은 되도록 사용하지 말것
* (2) byte 단위 operation을 쓸 때, 4byte, 8byte operation으로
* 대치할 수 있는지 생각할것
* (3) lock은 되도록, 최선을 다해서 피할 것
* (4) strcmp, strcasestr, strcpy대신 strNxxx함수를 사용할 것
* -- written by [email protected]
*/
//#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <microhttpd.h>
#include <unistd.h>
#include <search.h>
#include <errno.h>
//#include <ncapi.h>
//#include <trace.h>
#include <getopt.h>
/* Include the Lua API header files. */
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "common.h"
#include "reqpool.h"
#include "vstring.h"
#include "site.h"
#include "luaembed.h"
#include "setup_vars.h"
#include "luapool.h"
#include "scx_util.h"
/******************************************************
* 여기부터 LUA 테스트 코드임
******************************************************/
//lua 콜백 function에서 request를 참조하기 위해서는 따로 선언을 해주어야 한다.
//lua에서 호출되는 api는 return값을 성공일때 1 실패 일때 0을 해주어야 정상 동작한다.
static void scx_lua_set_req_p( lua_State* L, lua_main_st_t *lua_main);
static lua_main_st_t * scx_lua_get_req_p( lua_State* L );
static int scx_lua_header_set(lua_State *L);
static int scx_lua_header_get(lua_State *L);
static int scx_lua_request_set(lua_State *L);
static int scx_lua_request_get(lua_State *L);
static int scx_lua_conn_argument_set(lua_State *L);
static int scx_lua_conn_argument_get(lua_State *L);
static int scx_lua_conn_header_set(lua_State *L);
static int scx_lua_conn_header_get(lua_State *L);
static int scx_lua_response_set(lua_State *L);
static int scx_lua_response_get(lua_State *L);
static int scx_lua_server_set(lua_State *L);
static int scx_lua_server_get(lua_State *L);
static int scx_lua_request_geoip(void *p, nc_request_t *req, lua_req_element_t *res);
static int scx_lua_util_nextaction(lua_State *L);
static int scx_lua_util_set_pttl(lua_State *L);
static int scx_lua_util_set_nttl(lua_State *L);
static int scx_lua_solproxy_get(lua_State *L);
static int scx_lua_solproxy_set(lua_State *L);
static int scx_hyphen_to_underbar(char * str);
static int scx_underbar_to_hyphen(char * str);
static int scx_lua_req_comparei(const void *a, const void *b);
static int scx_lua_svr_comparei(const void *a, const void *b);
static int scx_lua_bit_comparei(const void *a, const void *b);
static int scx_lua_print(lua_State* L);
static int scx_lua_origin_header_set(lua_State *L);
static int scx_lua_origin_header_get(lua_State *L);
static int scx_lua_origin_argument_set(lua_State *L);
static int scx_lua_origin_argument_get(lua_State *L);
LUALIB_API int luaopen_bit(lua_State *L);
extern lua_pool_t *gscx__lua_pool;
#define LUA_REQ_GLOBAL_NAME "global_lua_request"
#define REQ_OFFSET( m) ((size_t)(&((nc_request_t *)0)->m))
lua_req_element_t __lua_req_tags[] = {
// {name, type, offset, readonly, proc},
{"zmethod", LUA_TYPE_VSTRING, REQ_OFFSET(zmethod), 1, NULL},
{"host", LUA_TYPE_VSTRING, REQ_OFFSET(host), 0, NULL},
{"version", LUA_TYPE_VSTRING, REQ_OFFSET(version), 1, NULL},
{"path", LUA_TYPE_VSTRING, REQ_OFFSET(path), 1, NULL},
{"url", LUA_TYPE_VSTRING, REQ_OFFSET(url), 1, NULL},
{"ori_url", LUA_TYPE_VSTRING, REQ_OFFSET(ori_url), 0, NULL},
{"client_ip", LUA_TYPE_VSTRING, REQ_OFFSET(client_ip), 1, NULL},
{"object_key", LUA_TYPE_VSTRING, REQ_OFFSET(object_key), 0, NULL},
{"property_key", LUA_TYPE_VSTRING, REQ_OFFSET(property_key), 0, NULL},
{"nocache", LUA_TYPE_BIT, (size_t)0, 0, NULL},
{"norandom", LUA_TYPE_BIT, (size_t)0, 0, NULL},
{"private", LUA_TYPE_BIT, (size_t)0, 0, NULL},
{"skip_auth", LUA_TYPE_BIT, (size_t)0, 0, NULL},
{"connect_type", LUA_TYPE_INT, REQ_OFFSET(connect_type), 1, NULL},
{"mode", LUA_TYPE_INT, REQ_OFFSET(mode), 0, NULL},
{"limit_rate", LUA_TYPE_INT, REQ_OFFSET(limit_rate), 0, NULL},
{"limit_rate_after", LUA_TYPE_INT, REQ_OFFSET(limit_rate_after), 0, NULL},
{"limit_traffic_rate", LUA_TYPE_INT, REQ_OFFSET(limit_traffic_rate), 0, NULL},
//여기부터는 geo ip handler function이다
{"geoip_city_country_code", LUA_TYPE_GEOIP, (size_t)0, 1,&gip_lookup_country2},
{"geoip_city_country_code3", LUA_TYPE_GEOIP, (size_t)0, 1,&gip_lookup_country3},
{"geoip_city_country_name", LUA_TYPE_GEOIP, (size_t)0, 1,&gip_lookup_country_name},
/* {"geoip_region", LUA_TYPE_GEOIP, (size_t)0, 1,&gip_lookup_region},
{"geoip_postal_code", LUA_TYPE_GEOIP, (size_t)0, 1,&scx_lua_request_postal_code},
{"geoip_city_continent_code", LUA_TYPE_GEOIP, (size_t)0, 1,&scx_lua_request_continent_code},
{"geoip_latitude", LUA_TYPE_GEOIP, (size_t)0, 1,&scx_lua_request_latitude},
{"geoip_longitude", LUA_TYPE_GEOIP, (size_t)0, 1,&scx_lua_request_longitude},
{"geoip_dma_code", LUA_TYPE_GEOIP, (size_t)0, 1,&scx_lua_request_dma_code},
{"geoip_area_code", LUA_TYPE_GEOIP, (size_t)0, 1,&scx_lua_request_area_code}
{"geoip_city", LUA_TYPE_GEOIP, (size_t)0, 1,&gip_lookup_city}
*/
{"geoip_isp", LUA_TYPE_GEOIP, (size_t)0, 1,&gip_lookup_isp}
};
#define LUA_REQ_DICT_COUNT howmany(sizeof(__lua_req_tags), sizeof(lua_req_element_t))
#define GET_REQ_FIELD(req,offset) ((char*)req + offset)
lua_svr_element_t __lua_svr_tags[] = {
{SV_ORIGIN, LUA_TYPE_VSTRING},
{SV_DOMAIN, LUA_TYPE_VSTRING},
{SV_PROXY, LUA_TYPE_VSTRING},
{SV_FOLLOW_REDIR, LUA_TYPE_INT},
{SV_NEGATIVE_TTL, LUA_TYPE_INT},
{SV_POSITIVE_TTL, LUA_TYPE_INT},
{SV_READAHEAD_MB, LUA_TYPE_INT},
{SV_CERTIFICATE_CRT, LUA_TYPE_VSTRING},
{SV_INODE_SIZE, LUA_TYPE_INT},
{SV_CHUNK_SIZE, LUA_TYPE_INT},
{SV_WRITEBACK_CHUNKS, LUA_TYPE_INT},
{SV_DRIVER_CLASS, LUA_TYPE_VSTRING},
{SV_CACHE_PATH, LUA_TYPE_VSTRING},
{SV_WORKERS, LUA_TYPE_INT},
{SV_POOL_SIZE, LUA_TYPE_INT},
{SV_CLUSTER_IP, LUA_TYPE_VSTRING},
{SV_CLUSTER_TTL, LUA_TYPE_INT},
{SV_LOG_DIR, LUA_TYPE_VSTRING},
{SV_HOST_NAME, LUA_TYPE_VSTRING},
{SV_HTTPS_PORT, LUA_TYPE_INT},
{SV_HTTP_PORT, LUA_TYPE_INT},
{SV_USE_HEAD, LUA_TYPE_INT},
{SV_LOG_FORMAT, LUA_TYPE_INT},
{SV_GEOIP_COUNTRY, LUA_TYPE_INT},
{SV_GEOIP_CITY, LUA_TYPE_INT},
{SV_GEOIP_ISP, LUA_TYPE_INT}
};
#define LUA_SVR_DICT_COUNT howmany(sizeof(__lua_svr_tags), sizeof(lua_svr_element_t))
lua_bit_element_t __lua_bit_tags[] = {
{"NCX_NOCACHE", O_NCX_NOCACHE},
{"DONTCHECK_ORIGIN", O_NCX_DONTCHECK_ORIGIN},
{"SEQUENTIAL", O_NCX_NORANDOM},
{"NORANDOM", O_NCX_NORANDOM},
{"REFRESH_STAT", O_NCX_REFRESH_STAT},
{"MUST_REVAL", O_NCX_MUST_REVAL}
};
#define LUA_BIT_DICT_COUNT howmany(sizeof(__lua_bit_tags), sizeof(lua_bit_element_t))
const struct luaL_Reg __request_symbols[] = {
{"__index", scx_lua_request_get},
{"__newindex", scx_lua_request_set},
{NULL, NULL}
};
const struct luaL_Reg __response_symbols[] = {
{"__index", scx_lua_response_get},
{"__newindex", scx_lua_response_set},
{NULL, NULL}
};
const struct luaL_Reg __solproxylib[] = {
{"nextaction", scx_lua_util_nextaction},
{"set_pttl", scx_lua_util_set_pttl},
{"set_nttl", scx_lua_util_set_nttl},
{NULL, NULL} /* end of array */
};
const struct luaL_Reg __printlib[] = {
{"print", scx_lua_print},
{NULL, NULL} /* end of array */
};
int scx_lua_client_request_phase(void *cls, const char *script)
{
lua_main_st_t main_st = {cls, LUA_PHASE_CLIENT_REQUEST, NULL} ;
nc_request_t *req = (nc_request_t *)cls;
lua_main_st_t * lua_main = &main_st;
lua_State *L = NULL;
lua_elment_t * element = NULL;
int ret = 0;
const struct luaL_Reg __header_symbols[] = {
{"__index", scx_lua_header_get},
{"__newindex", scx_lua_header_set},
{NULL, NULL}
};
const struct luaL_Reg __argument_symbols[] = {
{"__index", scx_lua_conn_argument_get},
{"__newindex", scx_lua_conn_argument_set},
{NULL, NULL}
};
const struct luaL_Reg __server_symbols[] = {
{"__index", scx_lua_server_get},
{"__newindex", scx_lua_server_set},
{NULL, NULL}
};
const struct luaL_Reg __solproxyattr[] = {
{"__index", scx_lua_solproxy_get},
{"__newindex", scx_lua_solproxy_set},
{NULL, NULL} /* end of array */
};
TRC_BEGIN((__func__));
// TRACE((T_DEBUG, "[%llu] lua script = %s\n", req->id, script));
if(gscx__lua_pool == NULL)
{
L = luaL_newstate();
// load the libs
luaL_openlibs(L);
}
else
{
element = lua_pool_get_element(gscx__lua_pool);
if(element == NULL)
{
TRACE((T_ERROR|T_TRIGGER, "[%llu] failed to get free lua state\n", req->id));
//printf("error : %s\n",lua_tostring(L, -1));
ret = -1;
goto close_state;
}
L = (lua_State * )element->data;
}
// lua_createtable(L, 0 /* narr */, 20 /* nrec */);
lua_settop(L,0);
scx_lua_set_req_p(L, lua_main); //lua_main 포인터를 global 변수 할당한다.
//아래 부분은 순서를 바꾸면 동작 하지 않는다. -_-
lua_settop(L,0);
luaL_newmetatable(L, "BIT");
luaopen_bit(L);
#if LUA_VERSION_NUM < 502
luaL_register(L, "BIT", __solproxyattr);
#else
luaL_setfuncs(L, __solproxyattr, 0);
#endif
lua_setmetatable(L, -2);
lua_setglobal(L, "BIT");
//여기까지 순서를 바꾸면 안됨
lua_settop(L,0);
luaL_newmetatable(L, "HEADER");
#if LUA_VERSION_NUM < 502
luaL_register(L, "HEADER", __header_symbols);
#else
luaL_setfuncs(L, __header_symbols, 0);
#endif
lua_setmetatable(L, -2);
lua_setglobal(L, "HEADER");
/* URI 에 포함된 GET 파라미터 */
lua_settop(L,0);
luaL_newmetatable(L, "ARGUMENT");
#if LUA_VERSION_NUM < 502
luaL_register(L, "ARGUMENT", __argument_symbols);
#else
luaL_setfuncs(L, __argument_symbols, 0);
#endif
lua_setmetatable(L, -2);
lua_setglobal(L, "ARGUMENT");
lua_settop(L,0);
luaL_newmetatable(L, "REQUEST");
#if LUA_VERSION_NUM < 502
luaL_register(L, "REQUEST", __request_symbols);
#else
luaL_setfuncs(L, __request_symbols, 0);
#endif
lua_setmetatable(L, -2);
lua_setglobal(L, "REQUEST");
lua_settop(L,0);
luaL_newmetatable(L, "RESPONSE");
#if LUA_VERSION_NUM < 502
luaL_register(L, "RESPONSE", __response_symbols);
#else
luaL_setfuncs(L, __response_symbols, 0);
#endif
lua_setmetatable(L, -2);
lua_setglobal(L, "RESPONSE");
lua_settop(L,0);
luaL_newmetatable(L, "SERVER");
#if LUA_VERSION_NUM < 502
luaL_register(L, "SERVER", __server_symbols);
#else
luaL_setfuncs(L, __server_symbols, 0);
#endif
lua_setmetatable(L, -2);
lua_setglobal(L, "SERVER");
lua_settop(L,0);
#if LUA_VERSION_NUM < 502
luaL_register(L, "UTIL", __solproxylib);
#else
luaL_setfuncs(L, __solproxylib, 0);
#endif
lua_settop(L,0);
#if LUA_VERSION_NUM < 502
luaL_register(L, "_G", __printlib);
#else
luaL_setfuncs(L, __printlib, 0);
#endif
lua_settop(L,0);
// ret = luaL_dostring(L, script); //luaL_loadstring()과 lua_pcall()를 합친 function
ret = luaL_loadstring(L, script); //lua script를 메모리에 load하고 문법을 검사한다.
if(ret > 0 ) //return이 0보다 큰 경우 script의 문법이 잘못됐거나 파일을 읽을수 없는 경우이다.
{
TRACE((T_ERROR|T_TRIGGER, "[%llu] error in lua script loading (%s)\n", req->id, lua_tostring(L, -1)));
//printf("error : %s\n",lua_tostring(L, -1));
ret = -1;
goto close_state;
}
ret = lua_pcall(L, 0, LUA_MULTRET, 0); //lua를 실행한다.
if(ret > 0 )
{
TRACE((T_ERROR|T_TRIGGER, "[%llu] error in lua script run (%s)\nscript = %s", req->id, lua_tostring(L, -1), script));
ret = -1;
goto close_state;
}
if(req->scx_res.trigger == LUA_TRIGGER_RETURN &&
(req->scx_res.code < 100 || req->scx_res.code > 600 )) {
//LUA_TRIGGER_RETURN이고 error code가 정상적으로 셋팅 되지 않은 경우는
// 400에러를 리턴한다..
TRACE((T_TRIGGER, "[%llu] invalid http return code(%d)\n", req->id, req->scx_res.code));
req->scx_res.code = MHD_HTTP_BAD_REQUEST;
}
ret = 0;
close_state :
if(element == NULL)
{
//lua state pool을 사용하지 않는 경우에는 직접 close 한다.
if(L != NULL) lua_close(L);
}
else
{
lua_pool_release_element(element);
}
TRC_END;
return ret;
}
//request struct pointer를 lua에서 call 되는 function들에 넘겨주기 위한 function임
static void scx_lua_set_req_p( lua_State* L, lua_main_st_t *lua_main)
{
lua_pushlightuserdata(L, (void *)lua_main); /* push address */
lua_setglobal( L, LUA_REQ_GLOBAL_NAME );
}
static lua_main_st_t * scx_lua_get_req_p( lua_State* L )
{
lua_getglobal( L, LUA_REQ_GLOBAL_NAME );
lua_main_st_t * lua_main = (lua_main_st_t *)lua_touserdata(L, -1);
lua_pop( L, 1 );
return lua_main;
}
static int
scx_lua_header_set(lua_State *L)
{
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get request data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
char *key = (char *)luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다. const char * 형이어야 하지만 scx_underbar_to_hyphen를 사용하기 위해 const를 사용하지 않는다.
scx_underbar_to_hyphen(key); // '_'를 찾아서 '-'로 바꾼다. lua에서 변수로 '-'를 허용하지 않기 때문에 '_' 대신 사용한다.
const char *val = NULL;
//항상 nil인지 확인해야 한다.
if (lua_isnil(L, 3)) { //변수에 nil을 직접 할당 하거나 변수만 선언한 경우는 모두 nil이다
//값이 할당 되지 않는 경우 해당 key를 hash에서 지운다.
kv_remove(req->options, key, 0);
TRC_END;
return 1;
}
else {
val = luaL_checkstring(L, 3); //lua로 부터 value 값을 읽어 온다.
}
kv_replace(req->options, key, val, 0);
TRC_END;
return 1;
}
static int
scx_lua_header_get(lua_State *L)
{
//request 정보를 가져 온다
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get request data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
char *key = (char *)luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다. const char * 형이어야 하지만 scx_underbar_to_hyphen를 사용하기 위해 const를 사용하지 않는다.
scx_underbar_to_hyphen(key); // '_'를 찾아서 '-'로 바꾼다.
const char *val = NULL;
val = kv_find_val_extened(req->options, key, 0, 1);
if(val == NULL){
TRACE((T_TRIGGER, "[%llu] failed to find header tag '%s'\n", req->id, key));
lua_pushnil(L);
//lua_pushstring(L, "");
}
else {
TRACE((T_TRIGGER, "[%llu] find header tag '%s' is '%s'\n", req->id, key, val));
lua_pushstring(L, val);
}
TRC_END;
return 1;
}
static int
scx_lua_conn_argument_set(lua_State *L)
{
/*
* request URL에 포함된 GET 파라미터(argument)는 개별적으로 수정을 할수가 없다.
* 파라미터의 수정이 필요한 경우 REQUEST.ori_url을 새로 만드는 방식을 사용해야 한다.
* 사용 예 :
* if (ARGUMENT.token ~= nil) and (ARGUMENT.expr ~= nil) then
* REQUEST.ori_url=REQUEST.path.."?token="..ARGUMENT.token.."&expr="..ARGUMENT.expr.."&ARG=1234"
* end
*
*/
return 1;
}
/* MHD의 connection으로 부터 직접 url에 포함된 argument를 읽는다. */
static int
scx_lua_conn_argument_get(lua_State *L)
{
//request 정보를 가져 온다
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get argument data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
char *key = (char *)luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다. const char * 형이어야 하지만 scx_underbar_to_hyphen를 사용하기 위해 const를 사용하지 않는다.
scx_underbar_to_hyphen(key); // '_'를 찾아서 '-'로 바꾼다.
const char *val = NULL;
val = MHD_lookup_connection_value(req->connection, MHD_GET_ARGUMENT_KIND, key);
if(val == NULL){
TRACE((T_TRIGGER, "[%llu] failed to find argument '%s'\n", req->id, key));
lua_pushnil(L);
//lua_pushstring(L, "");
}
else {
TRACE((T_TRIGGER, "[%llu] find argument '%s' is '%s'\n", req->id, key, val));
lua_pushstring(L, val);
}
TRC_END;
return 1;
}
/* MHD의 connection에 헤더를 직접 변경한다. */
static int
scx_lua_conn_header_set(lua_State *L)
{
/*
* MHD_set_connection_value()를 사용해서 값이 변경가능.
* 변경은 MHD AccessHandlerCallback 단계에서만 유효하다.
* 변경시 넘겨주는 key와 value는 connection이 종료 될때까지 유효한 문자열이어야 한다.
* 현재는 설정이 불가능 하도록 한다.
*/
return 1;
}
/* MHD의 connection으로 부터 직접 헤더를 읽는다. */
static int
scx_lua_conn_header_get(lua_State *L)
{
//request 정보를 가져 온다
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get argument data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
char *key = (char *)luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다. const char * 형이어야 하지만 scx_underbar_to_hyphen를 사용하기 위해 const를 사용하지 않는다.
scx_underbar_to_hyphen(key); // '_'를 찾아서 '-'로 바꾼다.
const char *val = NULL;
val = MHD_lookup_connection_value(req->connection, MHD_HEADER_KIND, key);
if(val == NULL){
TRACE((T_TRIGGER, "[%llu] failed to find header '%s'\n", req->id, key));
lua_pushnil(L);
//lua_pushstring(L, "");
}
else {
TRACE((T_TRIGGER, "[%llu] find header '%s' is '%s'\n", req->id, key, val));
lua_pushstring(L, val);
}
TRC_END;
return 1;
}
static int
scx_lua_request_set(lua_State *L)
{
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get request data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
const char *key = luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다.
lua_req_element_t *res = NULL;
lua_req_element_t qry ;
strncpy(qry.name, key, MAX_LUA_ELEMENT_NAME_LEN);
//res = bsearch(&qry, lua_main->elm, lua_main->elmcnt, sizeof(lua_req_element_t), scx_lua_req_comparei);
res = bsearch(&qry, __lua_req_tags, LUA_REQ_DICT_COUNT, sizeof(lua_req_element_t), scx_lua_req_comparei);
if (res == NULL) {
TRACE((T_TRIGGER, "[%llu] failed to find request tag '%s'\n", req->id, key));
TRC_END;
return 0;
}
const char *val = NULL;
int num;
if(res->readonly == 0) { /* 읽기 전용인 경우에는 아래의 과정을 skip한다. */
//TRACE((T_TRIGGER, "REQUEST.%s being changed\n", key));
switch (res->type) {
case LUA_TYPE_VSTRING:
//항상 nil인지 확인해야 한다.
if (lua_isnil(L, 3)) { //변수에 nil을 직접 할당 하거나 변수만 선언한 경우는 모두 nil이다
*((vstring_t **)GET_REQ_FIELD(req, res->offset)) = vs_strdup_lg(req->pool, "");
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is deleted.\n", req->id, key));
}
else {
val = luaL_checkstring(L, 3); //lua로 부터 value 값을 읽어 온다.
*((vstring_t **)GET_REQ_FIELD(req, res->offset)) = vs_strdup_lg(req->pool, val);
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is set to '%s'\n", req->id, key, val));
}
break;
case LUA_TYPE_INT:
if (lua_isnil(L, 3)) {
*((mode_t **)GET_REQ_FIELD(req, res->offset)) = 0;
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is deleted.\n", req->id, key));
}
else {
num = luaL_checkint(L, 3);
//*((mode_t **)GET_REQ_FIELD(lua_main->cls, res->offset)) = num;
*((mode_t **)((char*)req + res->offset)) = num;
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is set to '%d'\n", req->id, key, num));
}
break;
case LUA_TYPE_BIT:
//bit형 변수인 경우 변수 포인터를 사용할수 없다.
if(strcasecmp(key, "nocache") == 0) {
if (lua_isnil(L, 3)) {
req->nocache = 0;
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is deleted.\n", req->id, key));
}
else {
num = luaL_checkint(L, 3);
req->nocache = (num == 0)? 0 : 1; //값이 0이외에는 모두 1로 변환한다.
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is set to '%d' (num=%d)\n", req->id, key, req->nocache, num));
}
}
else if (strcasecmp(key, "norandom") == 0) {
if (lua_isnil(L, 3)) {
req->norandom = 0;
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is deleted.\n", req->id, key));
}
else {
num = luaL_checkint(L, 3);
if(num < 0 || num > 2) num = 0;
req->norandom = num;
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is set to '%d' (num=%d)\n", req->id, key, req->norandom, num));
}
}
else if (strcasecmp(key, "private") == 0) {
if (lua_isnil(L, 3)) {
req->private = 0;
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is deleted.\n", req->id, key));
}
else {
num = luaL_checkint(L, 3);
req->private = (num == 0)? 0 : 1; //값이 0이외에는 모두 1로 변환한다.
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is set to '%d' (num=%d)\n", req->id, key, req->private, num));
}
}
else if (strcasecmp(key, "skip_auth") == 0) {
if (lua_isnil(L, 3)) {
req->skip_auth = 0;
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is deleted.\n", req->id, key));
}
else {
num = luaL_checkint(L, 3);
req->skip_auth = (num == 0)? 0 : 1; //값이 0이외에는 모두 1로 변환한다.
TRACE((T_DEBUG|T_TRIGGER, "[%llu] request tag '%s' is set to '%d' (num=%d)\n", req->id, key, req->skip_auth, num));
}
}
break;
default:
TRACE((T_TRIGGER, "[%llu] failed to set request tag '%s'\n", req->id, key));
break;
}
}
TRC_END;
return 1;
}
static int
scx_lua_request_get(lua_State *L)
{
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get request data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
const char *key = luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다.
lua_req_element_t *res = NULL;
lua_req_element_t qry ;
strncpy(qry.name, key, MAX_LUA_ELEMENT_NAME_LEN);
res = bsearch(&qry, __lua_req_tags, LUA_REQ_DICT_COUNT, sizeof(lua_req_element_t), scx_lua_req_comparei);
if (res == NULL) {
TRACE((T_TRIGGER, "[%llu] failed to find request tag '%s'\n", req->id, key));
TRC_END;
return 0;
}
const char *val = NULL;
vstring_t * tmpstr = NULL;
int num = 0;
switch (res->type) {
case LUA_TYPE_VSTRING:
tmpstr = *((vstring_t **)GET_REQ_FIELD(req, res->offset));
if(tmpstr == NULL){
lua_pushnil(L); //nil을 script에서 출력하려고 할 경우 script가 멈춘다.
//lua_pushstring(L, "");
TRACE((T_TRIGGER, "[%llu] failed to find request tag '%s'\n", req->id, key));
break;
}
val = vs_data(tmpstr);
if(val == NULL){
lua_pushnil(L);
//lua_pushstring(L, "");
TRACE((T_TRIGGER, "[%llu] failed to find request tag '%s'\n", req->id, key));
}
else {
// TRACE((T_INFO, "======== get key= REQUEST.'%s' => value(%s)\n", key, val));
TRACE((T_TRIGGER, "[%llu] find request tag '%s' is '%s'\n", req->id, key, val));
lua_pushstring(L, val);
}
break;
case LUA_TYPE_INT:
num = *(mode_t **)GET_REQ_FIELD(req, res->offset);
lua_pushinteger(L, num);
TRACE((T_TRIGGER, "[%llu] find request tag '%s' is '%d'\n", req->id, key, num));
break;
case LUA_TYPE_BIT:
//bit형 변수인 경우 변수 포인터를 사용할수 없다.
if(strcasecmp(key, "nocache") == 0) {
num = req->nocache;
}
else if(strcasecmp(key, "norandom") == 0) {
num = req->norandom;
}
else if(strcasecmp(key, "private") == 0) {
num = req->private;
}
else if(strcasecmp(key, "skip_auth") == 0) {
num = req->skip_auth;
}
else {
break;
}
lua_pushinteger(L, num);
TRACE((T_TRIGGER, "[%llu] find request tag '%s' is '%d'\n", req->id, key, num));
break;
case LUA_TYPE_GEOIP:
if (res->proc) {
//(*res->proc)((void *)L, req);
scx_lua_request_geoip((void *)L, req, res);
}
else {
TRACE((T_TRIGGER, "[%llu] geoip tag '%s' handler not defined\n", req->id, key));
}
break;
default :
TRACE((T_TRIGGER, "[%llu] undefined key(%s) type(%d)\n", req->id, key, res->type));
break;
}
TRC_END;
return 1;
}
static int scx_lua_request_geoip(void *p, nc_request_t *req, lua_req_element_t *res)
{
lua_State *L = (lua_State *)p;
TRC_BEGIN((__func__));
char buf[512] = {'\0'};
char * retbuf = NULL;
TRACE((T_TRIGGER, "client ip = '%s' \n", vs_data(req->client_ip) ));
retbuf = (*res->proc)(vs_data(req->client_ip), buf);
if(retbuf == NULL) {
// TRACE((T_ERROR, "-------------- scx_lua_request_geoip is null\n"));
TRACE((T_TRIGGER, "failed to find geoip tag '%s' \n", res->name));
lua_pushnil(L);
//lua_pushstring(L, "");
}
else {
// TRACE((T_ERROR, "-------------- scx_lua_request_geoip = %s\n", retbuf));
TRACE((T_TRIGGER, "find geoip tag '%s' is '%s'\n", res->name, retbuf));
lua_pushstring(L, retbuf);
}
TRC_END;
return 1;
}
/* RESPONSE에서 변경 가능한 header
RESPONSE.location = "http://xxxx.yyy.zzz.com/give_special_gift.html"
RESPONSE.body("축하합니다. 이벤트에 당첨되셨습니다")
RESPONSE.code=301
*/
static int
scx_lua_response_set(lua_State *L)
{
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get request data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
//TRACE((T_TRIGGER, "%s, gettop = %d\n", __FUNCTION__,lua_gettop(L)));
char *key = (char *)luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다.
const char *val = NULL;
if (lua_isnil(L, 3)) { // nil을 할당한 경우는 response에서 삭제 해야 할까?
TRACE((T_TRIGGER, "[%llu] %s, %s is NULL\n", req->id, __FUNCTION__, key));
}
else {
val = luaL_checkstring(L, 3); //lua로 부터 value 값을 읽어 온다.
}
if(val == NULL) {
TRC_END;
return 0;
}
if(strcasecmp(key, "code") == 0) {
req->scx_res.code = atoi(val);
}
else if(strcasecmp(key, "body") == 0) {
if (req->scx_res.body) { /* body가 이전에 기록되어 있는 경우 해당 메모리의 해제가 먼저 이루어 진다. */
SCX_FREE(req->scx_res.body);
req->scx_res.body = NULL;
req->objstat.st_size = req->scx_res.body_len = 0;
}
req->objstat.st_size = req->scx_res.body_len = strlen(val);
req->scx_res.body = SCX_CALLOC(1, req->scx_res.body_len + 1);
strncpy(req->scx_res.body, val, req->scx_res.body_len);
TRACE((T_TRIGGER, "[%llu] %s, scx_res.body = %s\n", req->id, __FUNCTION__, req->scx_res.body));
}
else {
scx_underbar_to_hyphen(key); // '_'를 찾아서 '-'로 바꾼다. lua에서 변수로 '-'를 허용하지 않기 때문에 '_' 대신 사용한다.
kv_replace(req->scx_res.headers, key, val, 0);
}
TRC_END;
return 0;
}
static int
scx_lua_response_get(lua_State *L)
{
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get request data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
char *key = (char *)luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다.
scx_underbar_to_hyphen(key); // '_'를 찾아서 '-'로 바꾼다.
const char *val = NULL;
if(strcasecmp(key, "code") == 0) {
lua_pushinteger (L, req->scx_res.code);
}
else if(strcasecmp(key, "body") == 0) {
if(req->scx_res.body == NULL)
{
lua_pushnil(L);
}
else {
lua_pushstring(L, req->scx_res.body);
}
}
else {
val = kv_find_val_extened(req->scx_res.headers, key, 0, 1);
if(val == NULL){
TRACE((T_TRIGGER, "[%llu] failed to find header tag '%s'\n", req->id, key));
lua_pushnil(L);
//lua_pushstring(L, "");
}
else {
TRACE((T_TRIGGER, "[%llu] find header tag '%s' is '%s'\n", req->id, key, val));
lua_pushstring(L, val);
}
}
TRC_END;
return 1;
}
static int
scx_lua_server_set(lua_State *L)
{
TRC_BEGIN((__func__));
TRACE((T_TRIGGER, "SERVER set is disabled '%s'\n"));
TRC_END;
return 1;
}
static int
scx_lua_server_get(lua_State *L)
{
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get request data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
const char *key = luaL_checkstring(L, 2); //lua로 부터 key 값을 읽어 온다.
lua_svr_element_t *res = NULL;
lua_svr_element_t qry ;
strncpy(qry.name, key, MAX_LUA_ELEMENT_NAME_LEN);
res = bsearch(&qry, __lua_svr_tags, LUA_SVR_DICT_COUNT, sizeof(lua_svr_element_t), scx_lua_svr_comparei);
if (res == NULL) {
TRACE((T_TRIGGER, "[%llu] failed to find SERVER tag '%s'\n", req->id, key));
TRC_END;
return 0;
}
char *val = NULL;
int num = 0;
val = (char *)st_get(req->service->site->st_setup, key);
switch (res->type) {
case LUA_TYPE_VSTRING:
if(val == NULL){
lua_pushnil(L);
TRACE((T_TRIGGER, "[%llu] failed to find server tag '%s'\n", req->id, key));
}
else {
lua_pushstring(L, val);
TRACE((T_TRIGGER, "[%llu] find server tag '%s' is '%s'\n", req->id, key, val));
}
break;
case LUA_TYPE_INT:
if(val == NULL){
lua_pushinteger(L, 0);
TRACE((T_TRIGGER, "[%llu] failed to find server tag '%s'\n", req->id, key));
}
else {
num = atoi(val);
lua_pushinteger(L, num);
TRACE((T_TRIGGER, "[%llu] find server tag '%s' is '%d'\n", req->id, key, num));
}
break;
default:
TRACE((T_TRIGGER, "[%llu] undefined key(%s) type(%d)\n", req->id, key, res->type));
break;
}
TRC_END;
return 1;
}
static int
scx_lua_util_nextaction(lua_State *L)
{
TRC_BEGIN((__func__));
lua_main_st_t * lua_main = (lua_main_st_t *)scx_lua_get_req_p(L);
if (lua_main == NULL) {
TRACE((T_ERROR|T_TRIGGER, "failed to get request data\n"));
TRC_END;
return 0;
}
nc_request_t *req = (nc_request_t *)lua_main->cls;
if(lua_gettop(L) != 1) {
TRACE((T_TRIGGER, "[%llu] nextaction must require parameter\n", req->id));
TRC_END;
return 0;
}
if (lua_isnil(L, 1)) {
TRACE((T_TRIGGER, "[%llu] nextaction not permit nil value\n", req->id));
TRC_END;
return 0;
}
const char *val = luaL_checkstring(L, 1); //lua로 부터 key 값을 읽어 온다.
if(val == NULL) {
TRACE((T_TRIGGER, "[%llu] nextaction not defined.\n", req->id));
TRC_END;
return 0;
}
if(strcasecmp(val, "continue") == 0) {
req->scx_res.trigger = LUA_TRIGGER_CONTINUE;
}