forked from openresty/lua-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngx_http_lua_socket_tcp.c
More file actions
4040 lines (2985 loc) · 105 KB
/
Copy pathngx_http_lua_socket_tcp.c
File metadata and controls
4040 lines (2985 loc) · 105 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
/*
* Copyright (C) Yichun Zhang (agentzh)
*/
#ifndef DDEBUG
#define DDEBUG 0
#endif
#include "ddebug.h"
#include "ngx_http_lua_socket_tcp.h"
#include "ngx_http_lua_util.h"
#include "ngx_http_lua_output.h"
#include "ngx_http_lua_contentby.h"
#include "ngx_http_lua_probe.h"
#if 1
#undef ngx_http_lua_probe_info
#define ngx_http_lua_probe_info(msg)
#endif
static int ngx_http_lua_socket_tcp(lua_State *L);
static int ngx_http_lua_socket_tcp_connect(lua_State *L);
static int ngx_http_lua_socket_tcp_receive(lua_State *L);
static int ngx_http_lua_socket_tcp_send(lua_State *L);
static int ngx_http_lua_socket_tcp_close(lua_State *L);
static int ngx_http_lua_socket_tcp_setoption(lua_State *L);
static int ngx_http_lua_socket_tcp_settimeout(lua_State *L);
static void ngx_http_lua_socket_tcp_handler(ngx_event_t *ev);
static ngx_int_t ngx_http_lua_socket_tcp_get_peer(ngx_peer_connection_t *pc,
void *data);
static void ngx_http_lua_socket_read_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static void ngx_http_lua_socket_send_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static void ngx_http_lua_socket_connected_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static void ngx_http_lua_socket_tcp_cleanup(void *data);
static void ngx_http_lua_req_socket_cleanup(void *data);
static void ngx_http_lua_socket_tcp_finalize(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static ngx_int_t ngx_http_lua_socket_send(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static ngx_int_t ngx_http_lua_socket_test_connect(ngx_connection_t *c);
static void ngx_http_lua_socket_handle_error(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, ngx_uint_t ft_type);
static void ngx_http_lua_socket_handle_success(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static int ngx_http_lua_socket_tcp_send_retval_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L);
static int ngx_http_lua_socket_tcp_connect_retval_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L);
static void ngx_http_lua_socket_dummy_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static ngx_int_t ngx_http_lua_socket_tcp_read(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static void ngx_http_lua_socket_read_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static int ngx_http_lua_socket_tcp_receive_retval_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L);
static ngx_int_t ngx_http_lua_socket_read_line(void *data, ssize_t bytes);
static void ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx);
static int ngx_http_lua_socket_resolve_retval_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L);
static int ngx_http_lua_socket_error_retval_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L);
static ngx_int_t ngx_http_lua_socket_read_all(void *data, ssize_t bytes);
static ngx_int_t ngx_http_lua_socket_read_until(void *data, ssize_t bytes);
static ngx_int_t ngx_http_lua_socket_read_chunk(void *data, ssize_t bytes);
static int ngx_http_lua_socket_tcp_receiveuntil(lua_State *L);
static int ngx_http_lua_socket_receiveuntil_iterator(lua_State *L);
static ngx_int_t ngx_http_lua_socket_compile_pattern(u_char *data, size_t len,
ngx_http_lua_socket_compiled_pattern_t *cp, ngx_log_t *log);
static int ngx_http_lua_socket_cleanup_compiled_pattern(lua_State *L);
static int ngx_http_lua_req_socket(lua_State *L);
static void ngx_http_lua_req_socket_rev_handler(ngx_http_request_t *r);
static int ngx_http_lua_socket_tcp_getreusedtimes(lua_State *L);
static int ngx_http_lua_socket_tcp_setkeepalive(lua_State *L);
static ngx_int_t ngx_http_lua_get_keepalive_peer(ngx_http_request_t *r,
lua_State *L, int key_index,
ngx_http_lua_socket_tcp_upstream_t *u);
static void ngx_http_lua_socket_keepalive_dummy_handler(ngx_event_t *ev);
static ngx_int_t ngx_http_lua_socket_keepalive_close_handler(ngx_event_t *ev);
static void ngx_http_lua_socket_keepalive_rev_handler(ngx_event_t *ev);
static void ngx_http_lua_socket_free_pool(ngx_log_t *log,
ngx_http_lua_socket_pool_t *spool);
static int ngx_http_lua_socket_tcp_upstream_destroy(lua_State *L);
static int ngx_http_lua_socket_downstream_destroy(lua_State *L);
static ngx_int_t ngx_http_lua_socket_push_input_data(ngx_http_request_t *r,
ngx_http_lua_ctx_t *ctx, ngx_http_lua_socket_tcp_upstream_t *u,
lua_State *L);
static ngx_int_t ngx_http_lua_socket_add_pending_data(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, u_char *pos, size_t len, u_char *pat,
int prefix, int old_state);
static ngx_int_t ngx_http_lua_socket_add_input_buffer(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u);
static ngx_int_t ngx_http_lua_socket_insert_buffer(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, u_char *pat, size_t prefix);
static ngx_int_t ngx_http_lua_socket_tcp_resume(ngx_http_request_t *r);
static void ngx_http_lua_tcp_resolve_cleanup(void *data);
static void ngx_http_lua_tcp_socket_cleanup(void *data);
enum {
SOCKET_CTX_INDEX = 1,
SOCKET_TIMEOUT_INDEX = 2,
SOCKET_KEY_INDEX = 3
};
static char ngx_http_lua_req_socket_metatable_key;
static char ngx_http_lua_tcp_socket_metatable_key;
void
ngx_http_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
{
ngx_int_t rc;
lua_createtable(L, 0, 3 /* nrec */); /* ngx.socket */
lua_pushcfunction(L, ngx_http_lua_socket_tcp);
lua_setfield(L, -2, "tcp");
{
const char buf[] = "local sock = ngx.socket.tcp()"
" local ok, err = sock:connect(...)"
" if ok then return sock else return nil, err end";
rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "ngx.socket.connect");
}
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_CRIT, log, 0,
"failed to load Lua code for ngx.socket.connect(): %i",
rc);
} else {
lua_setfield(L, -2, "connect");
}
lua_setfield(L, -2, "socket");
/* {{{req socket object metatable */
lua_pushlightuserdata(L, &ngx_http_lua_req_socket_metatable_key);
lua_createtable(L, 0 /* narr */, 4 /* nrec */);
lua_pushcfunction(L, ngx_http_lua_socket_tcp_receive);
lua_setfield(L, -2, "receive");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_receiveuntil);
lua_setfield(L, -2, "receiveuntil");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_settimeout);
lua_setfield(L, -2, "settimeout"); /* ngx socket mt */
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
/* {{{tcp object metatable */
lua_pushlightuserdata(L, &ngx_http_lua_tcp_socket_metatable_key);
lua_createtable(L, 0 /* narr */, 10 /* nrec */);
lua_pushcfunction(L, ngx_http_lua_socket_tcp_connect);
lua_setfield(L, -2, "connect");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_receive);
lua_setfield(L, -2, "receive");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_receiveuntil);
lua_setfield(L, -2, "receiveuntil");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_send);
lua_setfield(L, -2, "send");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_close);
lua_setfield(L, -2, "close");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_setoption);
lua_setfield(L, -2, "setoption");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_settimeout);
lua_setfield(L, -2, "settimeout"); /* ngx socket mt */
lua_pushcfunction(L, ngx_http_lua_socket_tcp_getreusedtimes);
lua_setfield(L, -2, "getreusedtimes");
lua_pushcfunction(L, ngx_http_lua_socket_tcp_setkeepalive);
lua_setfield(L, -2, "setkeepalive");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
lua_rawset(L, LUA_REGISTRYINDEX);
/* }}} */
}
void
ngx_http_lua_inject_req_socket_api(lua_State *L)
{
lua_pushcfunction(L, ngx_http_lua_req_socket);
lua_setfield(L, -2, "socket");
}
static int
ngx_http_lua_socket_tcp(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
if (lua_gettop(L) != 0) {
return luaL_error(L, "expecting zero arguments, but got %d",
lua_gettop(L));
}
lua_pushlightuserdata(L, &ngx_http_lua_request_key);
lua_rawget(L, LUA_GLOBALSINDEX);
r = lua_touserdata(L, -1);
lua_pop(L, 1);
if (r == NULL) {
return luaL_error(L, "no request found");
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
| NGX_HTTP_LUA_CONTEXT_ACCESS
| NGX_HTTP_LUA_CONTEXT_CONTENT);
lua_createtable(L, 3 /* narr */, 1 /* nrec */);
lua_pushlightuserdata(L, &ngx_http_lua_tcp_socket_metatable_key);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
dd("top: %d", lua_gettop(L));
return 1;
}
static int
ngx_http_lua_socket_tcp_connect(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_ctx_t *ctx;
ngx_str_t host;
int port;
ngx_resolver_ctx_t *rctx, temp;
ngx_http_core_loc_conf_t *clcf;
int saved_top;
int n;
u_char *p;
size_t len;
ngx_url_t url;
ngx_int_t rc;
ngx_http_lua_loc_conf_t *llcf;
ngx_peer_connection_t *pc;
int timeout;
unsigned custom_pool;
int key_index;
const char *msg;
ngx_http_lua_co_ctx_t *coctx;
ngx_http_lua_socket_tcp_upstream_t *u;
n = lua_gettop(L);
if (n != 2 && n != 3 && n != 4) {
return luaL_error(L, "ngx.socket connect: expecting 2, 3, or 4 "
"arguments (including the object), but seen %d", n);
}
lua_pushlightuserdata(L, &ngx_http_lua_request_key);
lua_rawget(L, LUA_GLOBALSINDEX);
r = lua_touserdata(L, -1);
lua_pop(L, 1);
if (r == NULL) {
return luaL_error(L, "no request found");
}
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (ctx == NULL) {
return luaL_error(L, "no ctx found");
}
ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
| NGX_HTTP_LUA_CONTEXT_ACCESS
| NGX_HTTP_LUA_CONTEXT_CONTENT);
luaL_checktype(L, 1, LUA_TTABLE);
p = (u_char *) luaL_checklstring(L, 2, &len);
host.data = ngx_palloc(r->pool, len + 1);
if (host.data == NULL) {
return luaL_error(L, "out of memory");
}
host.len = len;
ngx_memcpy(host.data, p, len);
host.data[len] = '\0';
key_index = 2;
custom_pool = 0;
if (lua_type(L, n) == LUA_TTABLE) {
/* found the last optional option table */
lua_getfield(L, n, "pool");
switch (lua_type(L, -1)) {
case LUA_TNUMBER:
lua_tostring(L, -1);
case LUA_TSTRING:
custom_pool = 1;
lua_pushvalue(L, -1);
lua_rawseti(L, 1, SOCKET_KEY_INDEX);
key_index = n + 1;
break;
default:
msg = lua_pushfstring(L, "bad \"pool\" option type: %s",
luaL_typename(L, -1));
luaL_argerror(L, n, msg);
break;
}
n--;
}
if (n == 3) {
port = luaL_checkinteger(L, 3);
if (port < 0 || port > 65536) {
lua_pushnil(L);
lua_pushfstring(L, "bad port number: %d", port);
return 2;
}
if (!custom_pool) {
lua_pushliteral(L, ":");
lua_insert(L, 3);
lua_concat(L, 3);
}
dd("socket key: %s", lua_tostring(L, -1));
} else { /* n == 2 */
port = 0;
}
if (!custom_pool) {
/* the key's index is 2 */
lua_pushvalue(L, 2);
lua_rawseti(L, 1, SOCKET_KEY_INDEX);
}
lua_rawgeti(L, 1, SOCKET_CTX_INDEX);
u = lua_touserdata(L, -1);
lua_pop(L, 1);
if (u) {
if (u->waiting) {
lua_pushnil(L);
lua_pushliteral(L, "socket busy");
return 2;
}
if (u->is_downstream) {
return luaL_error(L, "attempt to re-connect a request socket");
}
if (u->peer.connection) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket reconnect without shutting down");
ngx_http_lua_socket_tcp_finalize(r, u);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua reuse socket upstream ctx");
} else {
u = lua_newuserdata(L, sizeof(ngx_http_lua_socket_tcp_upstream_t));
if (u == NULL) {
return luaL_error(L, "out of memory");
}
#if 1
lua_createtable(L, 0 /* narr */, 1 /* nrec */); /* metatable */
lua_pushcfunction(L, ngx_http_lua_socket_tcp_upstream_destroy);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
#endif
lua_rawseti(L, 1, SOCKET_CTX_INDEX);
}
ngx_memzero(u, sizeof(ngx_http_lua_socket_tcp_upstream_t));
coctx = ctx->cur_co_ctx;
u->request = r; /* set the controlling request */
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
u->conf = llcf;
pc = &u->peer;
pc->log = r->connection->log;
pc->log_error = NGX_ERROR_ERR;
dd("lua peer connection log: %p", pc->log);
lua_rawgeti(L, 1, SOCKET_TIMEOUT_INDEX);
timeout = (ngx_int_t) lua_tointeger(L, -1);
lua_pop(L, 1);
if (timeout > 0) {
u->send_timeout = (ngx_msec_t) timeout;
u->read_timeout = (ngx_msec_t) timeout;
u->connect_timeout = (ngx_msec_t) timeout;
} else {
u->read_timeout = u->conf->read_timeout;
u->send_timeout = u->conf->send_timeout;
u->connect_timeout = u->conf->connect_timeout;
}
rc = ngx_http_lua_get_keepalive_peer(r, L, key_index, u);
if (rc == NGX_OK) {
lua_pushinteger(L, 1);
return 1;
}
if (rc == NGX_ERROR) {
lua_pushnil(L);
lua_pushliteral(L, "error in get keepalive peer");
return 2;
}
/* rc == NGX_DECLINED */
ngx_memzero(&url, sizeof(ngx_url_t));
url.url.len = host.len;
url.url.data = host.data;
url.default_port = port;
url.no_resolve = 1;
if (ngx_parse_url(r->pool, &url) != NGX_OK) {
lua_pushnil(L);
if (url.err) {
lua_pushfstring(L, "failed to parse host name \"%s\": %s",
host.data, url.err);
} else {
lua_pushfstring(L, "failed to parse host name \"%s\"", host.data);
}
return 2;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket connect timeout: %M", u->connect_timeout);
u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
if (u->resolved == NULL) {
return luaL_error(L, "out of memory");
}
if (url.addrs && url.addrs[0].sockaddr) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket network address given directly");
u->resolved->sockaddr = url.addrs[0].sockaddr;
u->resolved->socklen = url.addrs[0].socklen;
u->resolved->naddrs = 1;
u->resolved->host = url.addrs[0].name;
} else {
u->resolved->host = host;
u->resolved->port = (in_port_t) port;
}
if (u->resolved->sockaddr) {
rc = ngx_http_lua_socket_resolve_retval_handler(r, u, L);
if (rc == NGX_AGAIN) {
return lua_yield(L, 0);
}
return rc;
}
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
temp.name = host;
rctx = ngx_resolve_start(clcf->resolver, &temp);
if (rctx == NULL) {
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
lua_pushnil(L);
lua_pushliteral(L, "failed to start the resolver");
return 2;
}
if (rctx == NGX_NO_RESOLVER) {
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
lua_pushnil(L);
lua_pushfstring(L, "no resolver defined to resolve \"%s\"", host.data);
return 2;
}
rctx->name = host;
rctx->type = NGX_RESOLVE_A;
rctx->handler = ngx_http_lua_socket_resolve_handler;
rctx->data = u;
rctx->timeout = clcf->resolver_timeout;
u->resolved->ctx = rctx;
u->co_ctx = ctx->cur_co_ctx;
coctx->data = u;
saved_top = lua_gettop(L);
coctx->cleanup = ngx_http_lua_tcp_resolve_cleanup;
if (ngx_resolve_name(rctx) != NGX_OK) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket fail to run resolver immediately");
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
u->resolved->ctx = NULL;
lua_pushnil(L);
lua_pushfstring(L, "%s could not be resolved", host.data);
return 2;
}
if (u->waiting == 1) {
/* resolved and already connecting */
return lua_yield(L, 0);
}
n = lua_gettop(L) - saved_top;
if (n) {
/* errors occurred during resolving or connecting
* or already connected */
return n;
}
/* still resolving */
u->waiting = 1;
u->prepare_retvals = ngx_http_lua_socket_resolve_retval_handler;
dd("setting data to %p", u);
if (ctx->entered_content_phase) {
r->write_event_handler = ngx_http_lua_content_wev_handler;
} else {
r->write_event_handler = ngx_http_core_run_phases;
}
return lua_yield(L, 0);
}
static void
ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
{
ngx_http_request_t *r;
ngx_http_upstream_resolved_t *ur;
ngx_http_lua_ctx_t *lctx;
lua_State *L;
ngx_http_lua_socket_tcp_upstream_t *u;
u_char *p;
size_t len;
struct sockaddr_in *sin;
ngx_uint_t i;
unsigned waiting;
u = ctx->data;
r = u->request;
ur = u->resolved;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket resolve handler");
lctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
if (lctx == NULL) {
return;
}
lctx->cur_co_ctx = u->co_ctx;
u->co_ctx->cleanup = NULL;
L = lctx->cur_co_ctx->co;
waiting = u->waiting;
if (ctx->state) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket resolver error: %s (waiting: %d)",
ngx_resolver_strerror(ctx->state), (int) u->waiting);
lua_pushnil(L);
lua_pushlstring(L, (char *) ctx->name.data, ctx->name.len);
lua_pushfstring(L, " could not be resolved (%d: %s)",
(int) ctx->state,
ngx_resolver_strerror(ctx->state));
lua_concat(L, 2);
u->prepare_retvals = ngx_http_lua_socket_error_retval_handler;
ngx_http_lua_socket_handle_error(r, u,
NGX_HTTP_LUA_SOCKET_FT_RESOLVER);
return;
}
ur->naddrs = ctx->naddrs;
ur->addrs = ctx->addrs;
#if (NGX_DEBUG)
{
in_addr_t addr;
ngx_uint_t i;
for (i = 0; i < ctx->naddrs; i++) {
dd("addr i: %d %p", (int) i, &ctx->addrs[i]);
addr = ntohl(ctx->addrs[i]);
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"name was resolved to %ud.%ud.%ud.%ud",
(addr >> 24) & 0xff, (addr >> 16) & 0xff,
(addr >> 8) & 0xff, addr & 0xff);
}
}
#endif
if (ur->naddrs == 0) {
ngx_resolve_name_done(ctx);
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
lua_pushnil(L);
lua_pushliteral(L, "name cannot be resolved to a address");
return;
}
if (ur->naddrs == 1) {
i = 0;
} else {
i = ngx_random() % ur->naddrs;
}
dd("selected addr index: %d", (int) i);
len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
p = ngx_pnalloc(r->pool, len + sizeof(struct sockaddr_in));
if (p == NULL) {
ngx_resolve_name_done(ctx);
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_RESOLVER;
lua_pushnil(L);
lua_pushliteral(L, "out of memory");
return;
}
sin = (struct sockaddr_in *) &p[len];
ngx_memzero(sin, sizeof(struct sockaddr_in));
len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN);
len = ngx_sprintf(&p[len], ":%d", ur->port) - p;
sin->sin_family = AF_INET;
sin->sin_port = htons(ur->port);
sin->sin_addr.s_addr = ur->addrs[i];
ur->sockaddr = (struct sockaddr *) sin;
ur->socklen = sizeof(struct sockaddr_in);
ur->host.data = p;
ur->host.len = len;
ur->naddrs = 1;
ur->ctx = NULL;
ngx_resolve_name_done(ctx);
u->waiting = 0;
if (waiting) {
lctx->resume_handler = ngx_http_lua_socket_tcp_resume;
r->write_event_handler(r);
} else {
(void) ngx_http_lua_socket_resolve_retval_handler(r, u, L);
}
}
static int
ngx_http_lua_socket_resolve_retval_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L)
{
ngx_http_lua_ctx_t *ctx;
ngx_peer_connection_t *pc;
ngx_connection_t *c;
ngx_http_cleanup_t *cln;
ngx_http_upstream_resolved_t *ur;
ngx_int_t rc;
ngx_http_lua_co_ctx_t *coctx;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket resolve retval handler");
if (u->ft_type & NGX_HTTP_LUA_SOCKET_FT_RESOLVER) {
return 2;
}
pc = &u->peer;
ur = u->resolved;
if (ur->sockaddr) {
pc->sockaddr = ur->sockaddr;
pc->socklen = ur->socklen;
pc->name = &ur->host;
} else {
lua_pushnil(L);
lua_pushliteral(L, "resolver not working");
return 2;
}
pc->get = ngx_http_lua_socket_tcp_get_peer;
rc = ngx_event_connect_peer(pc);
if (rc == NGX_ERROR) {
u->socket_errno = ngx_socket_errno;
}
if (u->cleanup == NULL) {
cln = ngx_http_cleanup_add(r, 0);
if (cln == NULL) {
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_ERROR;
lua_pushnil(L);
lua_pushliteral(L, "out of memory");
return 2;
}
cln->handler = ngx_http_lua_socket_tcp_cleanup;
cln->data = u;
u->cleanup = &cln->handler;
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket connect: %i", rc);
if (rc == NGX_ERROR) {
return ngx_http_lua_socket_error_retval_handler(r, u, L);
}
if (rc == NGX_BUSY) {
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_ERROR;
lua_pushnil(L);
lua_pushliteral(L, "no live connection");
return 2;
}
if (rc == NGX_DECLINED) {
dd("socket errno: %d", (int) ngx_socket_errno);
u->ft_type |= NGX_HTTP_LUA_SOCKET_FT_ERROR;
u->socket_errno = ngx_socket_errno;
return ngx_http_lua_socket_error_retval_handler(r, u, L);
}
/* rc == NGX_OK || rc == NGX_AGAIN */
c = pc->connection;
c->data = u;
c->write->handler = ngx_http_lua_socket_tcp_handler;
c->read->handler = ngx_http_lua_socket_tcp_handler;
u->write_event_handler = ngx_http_lua_socket_connected_handler;
u->read_event_handler = ngx_http_lua_socket_connected_handler;
c->sendfile &= r->connection->sendfile;
c->pool = r->pool;
c->log = r->connection->log;
c->read->log = c->log;
c->write->log = c->log;
/* init or reinit the ngx_output_chain() and ngx_chain_writer() contexts */
#if 0
u->writer.out = NULL;
u->writer.last = &u->writer.out;
#endif
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
coctx = ctx->cur_co_ctx;
dd("setting data to %p", u);
coctx->data = u;
if (rc == NGX_OK) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket connected: fd:%d", (int) c->fd);
/* We should delete the current write/read event
* here because the socket object may not be used immediately
* on the Lua land, thus causing hot spin around level triggered
* event poll and wasting CPU cycles. */
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
ngx_http_lua_socket_handle_error(r, u,
NGX_HTTP_LUA_SOCKET_FT_ERROR);
lua_pushnil(L);
lua_pushliteral(L, "failed to handle write event");
return 2;
}
if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
ngx_http_lua_socket_handle_error(r, u,
NGX_HTTP_LUA_SOCKET_FT_ERROR);
lua_pushnil(L);
lua_pushliteral(L, "failed to handle write event");
return 2;
}
u->read_event_handler = ngx_http_lua_socket_dummy_handler;
u->write_event_handler = ngx_http_lua_socket_dummy_handler;
lua_pushinteger(L, 1);
return 1;
}
/* rc == NGX_AGAIN */
coctx->cleanup = ngx_http_lua_tcp_socket_cleanup;
ngx_add_timer(c->write, u->connect_timeout);
if (ctx->entered_content_phase) {
r->write_event_handler = ngx_http_lua_content_wev_handler;
} else {
r->write_event_handler = ngx_http_core_run_phases;
}
u->co_ctx = ctx->cur_co_ctx;
u->waiting = 1;
u->prepare_retvals = ngx_http_lua_socket_tcp_connect_retval_handler;
dd("setting data to %p", u);
if (ctx->entered_content_phase) {
r->write_event_handler = ngx_http_lua_content_wev_handler;
} else {
r->write_event_handler = ngx_http_core_run_phases;
}
return NGX_AGAIN;
}
static int
ngx_http_lua_socket_error_retval_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L)
{
u_char errstr[NGX_MAX_ERROR_STR];
u_char *p;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket error retval handler");
if (u->co_ctx) {
u->co_ctx->cleanup = NULL;
}
ngx_http_lua_socket_tcp_finalize(r, u);
if (u->ft_type & NGX_HTTP_LUA_SOCKET_FT_RESOLVER) {
return 2;
}
lua_pushnil(L);
if (u->ft_type & NGX_HTTP_LUA_SOCKET_FT_TIMEOUT) {
lua_pushliteral(L, "timeout");
} else if (u->ft_type & NGX_HTTP_LUA_SOCKET_FT_CLOSED) {
lua_pushliteral(L, "closed");
} else if (u->ft_type & NGX_HTTP_LUA_SOCKET_FT_BUFTOOSMALL) {
lua_pushliteral(L, "buffer too small");
} else if (u->ft_type & NGX_HTTP_LUA_SOCKET_FT_NOMEM) {
lua_pushliteral(L, "out of memory");
} else if (u->ft_type & NGX_HTTP_LUA_SOCKET_FT_CLIENTABORT) {
lua_pushliteral(L, "client aborted");
} else {
if (u->socket_errno) {
#if (nginx_version >= 1000000)
p = ngx_strerror(u->socket_errno, errstr, sizeof(errstr));
#else
p = ngx_strerror_r(u->socket_errno, errstr, sizeof(errstr));
#endif
/* for compatibility with LuaSocket */
ngx_strlow(errstr, errstr, p - errstr);
lua_pushlstring(L, (char *) errstr, p - errstr);
} else {
lua_pushliteral(L, "error");
}
}
return 2;
}
static int
ngx_http_lua_socket_tcp_connect_retval_handler(ngx_http_request_t *r,
ngx_http_lua_socket_tcp_upstream_t *u, lua_State *L)
{
if (u->ft_type) {
return ngx_http_lua_socket_error_retval_handler(r, u, L);
}
lua_pushinteger(L, 1);
return 1;
}
static int
ngx_http_lua_socket_tcp_receive(lua_State *L)
{
ngx_http_request_t *r;
ngx_http_lua_socket_tcp_upstream_t *u;
ngx_int_t rc;
ngx_http_lua_ctx_t *ctx;
int n;
ngx_str_t pat;
lua_Integer bytes;
char *p;
int typ;
ngx_http_lua_loc_conf_t *llcf;
ngx_http_lua_co_ctx_t *coctx;
n = lua_gettop(L);
if (n != 1 && n != 2) {
return luaL_error(L, "expecting 1 or 2 arguments "
"(including the object), but got %d", n);
}
lua_pushlightuserdata(L, &ngx_http_lua_request_key);
lua_rawget(L, LUA_GLOBALSINDEX);
r = lua_touserdata(L, -1);
lua_pop(L, 1);
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua tcp socket calling receive() method");