@@ -33,6 +33,7 @@ static int ngx_stream_lua_socket_tcp_connect(lua_State *L);
33
33
static int ngx_stream_lua_socket_tcp_sslhandshake (lua_State * L );
34
34
#endif
35
35
static int ngx_stream_lua_socket_tcp_receive (lua_State * L );
36
+ static int ngx_stream_lua_socket_tcp_receiveany (lua_State * L );
36
37
static int ngx_stream_lua_socket_tcp_send (lua_State * L );
37
38
static int ngx_stream_lua_socket_tcp_close (lua_State * L );
38
39
static int ngx_stream_lua_socket_tcp_setoption (lua_State * L );
@@ -107,6 +108,7 @@ static int ngx_stream_lua_socket_write_error_retval_handler(
107
108
static ngx_int_t ngx_stream_lua_socket_read_all (void * data , ssize_t bytes );
108
109
static ngx_int_t ngx_stream_lua_socket_read_until (void * data , ssize_t bytes );
109
110
static ngx_int_t ngx_stream_lua_socket_read_chunk (void * data , ssize_t bytes );
111
+ static ngx_int_t ngx_stream_lua_socket_read_any (void * data , ssize_t bytes );
110
112
static int ngx_stream_lua_socket_tcp_receiveuntil (lua_State * L );
111
113
static int ngx_stream_lua_socket_receiveuntil_iterator (lua_State * L );
112
114
static ngx_int_t ngx_stream_lua_socket_compile_pattern (u_char * data , size_t len ,
@@ -316,7 +318,7 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
316
318
/* {{{tcp object metatable */
317
319
lua_pushlightuserdata (L , ngx_stream_lua_lightudata_mask (
318
320
tcp_socket_metatable_key ));
319
- lua_createtable (L , 0 /* narr */ , 13 /* nrec */ );
321
+ lua_createtable (L , 0 /* narr */ , 14 /* nrec */ );
320
322
321
323
lua_pushcfunction (L , ngx_stream_lua_socket_tcp_connect );
322
324
lua_setfield (L , -2 , "connect" );
@@ -334,6 +336,9 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
334
336
lua_pushcfunction (L , ngx_stream_lua_socket_tcp_receiveuntil );
335
337
lua_setfield (L , -2 , "receiveuntil" );
336
338
339
+ lua_pushcfunction (L , ngx_stream_lua_socket_tcp_receiveany );
340
+ lua_setfield (L , -2 , "receiveany" );
341
+
337
342
lua_pushcfunction (L , ngx_stream_lua_socket_tcp_send );
338
343
lua_setfield (L , -2 , "send" );
339
344
@@ -358,7 +363,6 @@ ngx_stream_lua_inject_socket_tcp_api(ngx_log_t *log, lua_State *L)
358
363
lua_pushcfunction (L , ngx_stream_lua_socket_tcp_shutdown );
359
364
lua_setfield (L , -2 , "shutdown" );
360
365
361
-
362
366
lua_pushvalue (L , -1 );
363
367
lua_setfield (L , -2 , "__index" );
364
368
lua_rawset (L , LUA_REGISTRYINDEX );
@@ -2366,6 +2370,75 @@ ngx_stream_lua_socket_tcp_receive_helper(ngx_stream_lua_request_t *r,
2366
2370
}
2367
2371
2368
2372
2373
+ static int
2374
+ ngx_stream_lua_socket_tcp_receiveany (lua_State * L )
2375
+ {
2376
+ int n ;
2377
+ lua_Integer bytes ;
2378
+ ngx_stream_lua_request_t * r ;
2379
+ ngx_stream_lua_loc_conf_t * llcf ;
2380
+
2381
+ ngx_stream_lua_socket_tcp_upstream_t * u ;
2382
+
2383
+ n = lua_gettop (L );
2384
+ if (n != 2 ) {
2385
+ return luaL_error (L , "expecting 2 arguments "
2386
+ "(including the object), but got %d" , n );
2387
+ }
2388
+
2389
+ r = ngx_stream_lua_get_req (L );
2390
+ if (r == NULL ) {
2391
+ return luaL_error (L , "no request found" );
2392
+ }
2393
+
2394
+ luaL_checktype (L , 1 , LUA_TTABLE );
2395
+
2396
+ lua_rawgeti (L , 1 , SOCKET_CTX_INDEX );
2397
+ u = lua_touserdata (L , -1 );
2398
+
2399
+ if (u == NULL || u -> peer .connection == NULL || u -> read_closed ) {
2400
+
2401
+ llcf = ngx_stream_lua_get_module_loc_conf (r , ngx_stream_lua_module );
2402
+
2403
+ if (llcf -> log_socket_errors ) {
2404
+ ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
2405
+ "attempt to receive data on a closed "
2406
+ "socket: u:%p, c:%p, ft:%d eof:%d" ,
2407
+ u , u ? u -> peer .connection : NULL ,
2408
+ u ? (int ) u -> ft_type : 0 , u ? (int ) u -> eof : 0 );
2409
+ }
2410
+
2411
+ lua_pushnil (L );
2412
+ lua_pushliteral (L , "closed" );
2413
+ return 2 ;
2414
+ }
2415
+
2416
+ if (u -> request != r ) {
2417
+ return luaL_error (L , "bad request" );
2418
+ }
2419
+
2420
+ ngx_stream_lua_socket_check_busy_connecting (r , u , L );
2421
+ ngx_stream_lua_socket_check_busy_reading (r , u , L );
2422
+
2423
+ if (!lua_isnumber (L , 2 )) {
2424
+ return luaL_argerror (L , 2 , "bad max argument" );
2425
+ }
2426
+
2427
+ bytes = lua_tointeger (L , 2 );
2428
+ if (bytes <= 0 ) {
2429
+ return luaL_argerror (L , 2 , "bad max argument" );
2430
+ }
2431
+
2432
+ u -> input_filter = ngx_stream_lua_socket_read_any ;
2433
+ u -> rest = (size_t ) bytes ;
2434
+ u -> length = u -> rest ;
2435
+
2436
+ ngx_log_debug1 (NGX_LOG_DEBUG_STREAM , r -> connection -> log , 0 ,
2437
+ "stream lua tcp socket calling receiveany() "
2438
+ "method to read at most %uz bytes" , u -> rest );
2439
+
2440
+ return ngx_stream_lua_socket_tcp_receive_helper (r , u , L );
2441
+ }
2369
2442
2370
2443
2371
2444
static int
@@ -2406,8 +2479,8 @@ ngx_stream_lua_socket_tcp_receive(lua_State *L)
2406
2479
2407
2480
if (llcf -> log_socket_errors ) {
2408
2481
ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
2409
- "attempt to receive data on a closed socket: u:%p, "
2410
- "c:%p, ft:%d eof:%d" ,
2482
+ "stream attempt to receive data on a closed "
2483
+ "socket: u:%p, c:%p, ft:%d eof:%d" ,
2411
2484
u , u ? u -> peer .connection : NULL ,
2412
2485
u ? (int ) u -> ft_type : 0 , u ? (int ) u -> eof : 0 );
2413
2486
}
@@ -2550,6 +2623,24 @@ ngx_stream_lua_socket_read_line(void *data, ssize_t bytes)
2550
2623
}
2551
2624
2552
2625
2626
+ static ngx_int_t
2627
+ ngx_stream_lua_socket_read_any (void * data , ssize_t bytes )
2628
+ {
2629
+ ngx_int_t rc ;
2630
+ ngx_stream_lua_socket_tcp_upstream_t * u = data ;
2631
+
2632
+ ngx_log_debug0 (NGX_LOG_DEBUG_STREAM , u -> request -> connection -> log , 0 ,
2633
+ "stream lua tcp socket read any" );
2634
+
2635
+ rc = ngx_stream_lua_read_any (& u -> buffer , u -> buf_in , & u -> rest , bytes ,
2636
+ u -> request -> connection -> log );
2637
+ if (rc == NGX_ERROR ) {
2638
+ u -> ft_type |= NGX_STREAM_LUA_SOCKET_FT_CLOSED ;
2639
+ return NGX_ERROR ;
2640
+ }
2641
+
2642
+ return rc ;
2643
+ }
2553
2644
2554
2645
2555
2646
static ngx_int_t
0 commit comments