ngx.socket in timeout with content_by_lua_file

405 views
Skip to first unread message

estebanSannin

unread,
Aug 30, 2015, 7:53:11 PM8/30/15
to openresty-en
Hi all,
I'm using openresty from short time... 
Why this code work well with "content_by_lua" and in external file with "content_by_lua_file" go every time in timeout?

----- START CODE -----

local sock = ngx.socket.tcp()
sock:settimeout(2000)  -- one second timeout
local ok, err = sock:connect("www.googleapis.com", 443)
if not ok then
    ngx.say("###failed to connect: ", err)
    return
end
ngx.say("###connected: ", ok)
local sess, err = sock:sslhandshake()
if not sess then
    ngx.say("###failed to do SSL handshake: ", err)
    return
end
ngx.say("###ssl handshake: ", type(sess))
local req = "GET /oauth2/v3/tokeninfo?access_token= HTTP/1.1\\r\\nHost: www.googleapis.com\\r\\nConnection: close\\r\\n\\r\\n"
local bytes, err = sock:send(req)
if not bytes then
    ngx.say("###failed to send http request: ", err)
    return
end

local line, err = sock:receive("*a")
if not line then
    ngx.say("###failed to recieve response status line: ", err)
    return
end
ngx.say(line)
local ok, err = sock:close()

----- END CODE -----

- Stefano

Yichun Zhang (agentzh)

unread,
Aug 31, 2015, 12:24:35 AM8/31/15
to openresty-en
Hello!

On Mon, Aug 31, 2015 at 7:53 AM, estebanSannin wrote:
> I'm using openresty from short time...
> Why this code work well with "content_by_lua" and in external file with
> "content_by_lua_file" go every time in timeout?
>
> ----- START CODE -----
>

Are you using content_by_lua or content_by_lua_file. Your code snippet
should be okay if the former is used.

I've run your example on my side and it is not timed out:

location = /t2 {
resolver 8.8.8.8;
content_by_lua '
...
';
}

Accessing /t gives

###connected: 1
###ssl handshake: userdata
HTTP/1.1 400 Bad Request
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
Date: Mon, 31 Aug 2015 04:23:45 GMT
Expires: Mon, 31 Aug 2015 04:23:45 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic,p=1
Alt-Svc: quic=":443"; p="1"; ma=604800
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Connection: close

{
"error_description": "Invalid Value"
}

Regards,
-agentzh

estebanSannin

unread,
Aug 31, 2015, 4:51:21 AM8/31/15
to openresty-en
content_by_lua works also for me.

In my case, with content_by_lua_file not work:

location /test {
            resolver 8.8.8.8;
            content_by_lua_file /home/esteban/server/test.lua;
        }

...

Yichun Zhang (agentzh)

unread,
Aug 31, 2015, 11:55:37 PM8/31/15
to openresty-en
Hello!

On Mon, Aug 31, 2015 at 4:51 PM, estebanSannin wrote:
> In my case, with content_by_lua_file not work:
>

If you're using content_by_lua_file, then you should replace \\r with
\r and \\n with \n. You no longer need to escape the \ character if
your Lua code is not embedded in some nginx config string literals.

See

https://github.com/openresty/lua-nginx-module#special-escaping-sequences

I've had plans to implement *_by_lua_block { ... } directives for
ngx_lua such that Lua code does not have to be put in nginx string
literals (which require crazy double escaping).

Regards,
-agentzh

Yichun Zhang (agentzh)

unread,
Aug 31, 2015, 11:57:31 PM8/31/15
to openresty-en
Hello!

On Mon, Aug 31, 2015 at 7:53 AM, estebanSannin wrote:
> local line, err = sock:receive("*a")

Oh, BTW, it's not a good practice to use "*a" to read HTTP 1.1
response bodies since some buggy or malicious http servers might not
close connections actively and it's always safer to honour the end of
response body according to the HTTP 1.1 protocol itself.

Regards,
-agentzh
Reply all
Reply to author
Forward
0 new messages