Created
September 10, 2014 02:20
-
-
Save MayamaTakeshi/f2e467780f2dce7dfde2 to your computer and use it in GitHub Desktop.
lua-resty-http behavior on redirection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# nginx.conf snippet: | |
location /test_redirection { | |
content_by_lua ' | |
local http = require "http"; | |
function get_req(httpc, base_uri, path) | |
local uri = base_uri .. path | |
ngx.log(ngx.DEBUG, "Requesting ", uri) | |
local res, err = httpc:request_uri(uri, {method = "GET", ssl_verify=false}) | |
if not res then | |
error("Failed to request ", uri, " err: ", err) | |
end | |
if res.status == 302 then | |
local new_uri = res.headers["Location"] | |
ngx.log(ngx.DEBUG, "Redirecting to ", new_uri) | |
local slash_pos = string.find(new_uri, "/", 10) | |
local new_base_uri = string.sub(new_uri, 1, slash_pos) | |
local new_path = string.sub(new_uri, slash_pos+1) | |
return get_req(httpc, new_base_uri, new_path) | |
end | |
if res.status ~= 200 then | |
error("Error when requesting ", url) | |
end | |
ngx.say(res.body) | |
return base_uri | |
end | |
local base_uri = "http://192.168.2.143/" | |
local httpc = http.new() | |
for i,v in ipairs({"one", "two", "three", "four", "five", "six", "seven"}) do | |
base_uri = get_req(httpc, base_uri, v) | |
end | |
'; | |
} | |
# node.js server: | |
# cat my_http_server_with_close.js | |
var http = require('http'); | |
var s1 = http.createServer(function (req, res) { | |
console.log("Server(80) request arrived for ", req.url); | |
if(req.url == "/four") { | |
res.writeHead(302, {'Location': 'http://192.168.2.143:8080/FOUR', 'Connection': 'close'}); | |
res.end(); | |
} else { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end(req.url + "\r\n"); | |
} | |
}).listen(80); | |
s1.on('connection', function(socket) { | |
console.log("New connection" + socket); | |
}); | |
var s2 = http.createServer(function (req, res) { | |
console.log("Server(8080) request arrived for ", req.url); | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end(req.url + "\r\n"); | |
}).listen(8080); | |
s2.on('connection', function(socket) { | |
console.log("New connection" + socket); | |
}); | |
# making the request: | |
$ curl -x "" http://192.168.2.142/test_redirection | |
# ... i get: | |
$ node my_http_server_with_close.js | |
New connection[object Object] | |
Server(80) request arrived for /one | |
Server(80) request arrived for /two | |
Server(80) request arrived for /three | |
Server(80) request arrived for /four | |
New connection[object Object] | |
Server(8080) request arrived for /FOUR | |
New connection[object Object] | |
Server(8080) request arrived for /five | |
New connection[object Object] | |
Server(8080) request arrived for /six | |
New connection[object Object] | |
Server(8080) request arrived for /seven | |
# ngrep output | |
interface: any | |
match: HTTP | |
T 192.168.2.142:38472 -> 192.168.2.142:80 [AP] | |
GET /test_redirection HTTP/1.1. | |
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2. | |
Host: 192.168.2.142. | |
Accept: */*. | |
. | |
T 192.168.2.142:50675 -> 192.168.2.143:80 [AP] | |
GET /one HTTP/1.1. | |
Host: 192.168.2.143. | |
User-Agent: Resty/HTTP 0.03 (Lua). | |
. | |
T 192.168.2.143:80 -> 192.168.2.142:50675 [AP] | |
HTTP/1.1 200 OK. | |
Content-Type: text/plain. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Connection: keep-alive. | |
Transfer-Encoding: chunked. | |
. | |
6. | |
/one. | |
. | |
0. | |
. | |
T 192.168.2.142:50675 -> 192.168.2.143:80 [AP] | |
GET /two HTTP/1.1. | |
Host: 192.168.2.143. | |
User-Agent: Resty/HTTP 0.03 (Lua). | |
. | |
T 192.168.2.143:80 -> 192.168.2.142:50675 [AP] | |
HTTP/1.1 200 OK. | |
Content-Type: text/plain. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Connection: keep-alive. | |
Transfer-Encoding: chunked. | |
. | |
6. | |
/two. | |
. | |
0. | |
. | |
T 192.168.2.142:50675 -> 192.168.2.143:80 [AP] | |
GET /three HTTP/1.1. | |
Host: 192.168.2.143. | |
User-Agent: Resty/HTTP 0.03 (Lua). | |
. | |
T 192.168.2.143:80 -> 192.168.2.142:50675 [AP] | |
HTTP/1.1 200 OK. | |
Content-Type: text/plain. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Connection: keep-alive. | |
Transfer-Encoding: chunked. | |
. | |
8. | |
/three. | |
. | |
0. | |
. | |
T 192.168.2.142:50675 -> 192.168.2.143:80 [AP] | |
GET /four HTTP/1.1. | |
Host: 192.168.2.143. | |
User-Agent: Resty/HTTP 0.03 (Lua). | |
. | |
T 192.168.2.143:80 -> 192.168.2.142:50675 [AP] | |
HTTP/1.1 302 Moved Temporarily. | |
Location: http://192.168.2.143:8080/FOUR. | |
Connection: close. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Transfer-Encoding: chunked. | |
. | |
0. | |
. | |
T 192.168.2.142:37510 -> 192.168.2.143:8080 [AP] | |
GET /FOUR HTTP/1.1. | |
Host: 192.168.2.143. | |
User-Agent: Resty/HTTP 0.03 (Lua). | |
. | |
T 192.168.2.143:8080 -> 192.168.2.142:37510 [AP] | |
HTTP/1.1 200 OK. | |
Content-Type: text/plain. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Connection: keep-alive. | |
Transfer-Encoding: chunked. | |
. | |
7. | |
/FOUR. | |
. | |
0. | |
. | |
T 192.168.2.142:37511 -> 192.168.2.143:8080 [AP] | |
GET /five HTTP/1.1. | |
Host: 192.168.2.143. | |
User-Agent: Resty/HTTP 0.03 (Lua). | |
. | |
T 192.168.2.143:8080 -> 192.168.2.142:37511 [AP] | |
HTTP/1.1 200 OK. | |
Content-Type: text/plain. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Connection: keep-alive. | |
Transfer-Encoding: chunked. | |
. | |
7. | |
/five. | |
. | |
0. | |
. | |
T 192.168.2.142:37512 -> 192.168.2.143:8080 [AP] | |
GET /six HTTP/1.1. | |
Host: 192.168.2.143. | |
User-Agent: Resty/HTTP 0.03 (Lua). | |
. | |
T 192.168.2.143:8080 -> 192.168.2.142:37512 [AP] | |
HTTP/1.1 200 OK. | |
Content-Type: text/plain. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Connection: keep-alive. | |
Transfer-Encoding: chunked. | |
. | |
6. | |
/six. | |
. | |
0. | |
. | |
T 192.168.2.142:37513 -> 192.168.2.143:8080 [AP] | |
GET /seven HTTP/1.1. | |
Host: 192.168.2.143. | |
User-Agent: Resty/HTTP 0.03 (Lua). | |
. | |
T 192.168.2.143:8080 -> 192.168.2.142:37513 [AP] | |
HTTP/1.1 200 OK. | |
Content-Type: text/plain. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Connection: keep-alive. | |
Transfer-Encoding: chunked. | |
. | |
8. | |
/seven. | |
. | |
0. | |
. | |
T 192.168.2.142:80 -> 192.168.2.142:38472 [AP] | |
HTTP/1.1 200 OK. | |
Server: openresty/1.7.4.1rc1.3. | |
Date: Wed, 10 Sep 2014 02:14:08 GMT. | |
Content-Type: text/plain. | |
Transfer-Encoding: chunked. | |
Connection: keep-alive. | |
. | |
7. | |
/one. | |
. | |
7. | |
/two. | |
. | |
9. | |
/three. | |
. | |
8. | |
/FOUR. | |
. | |
8. | |
/five. | |
. | |
7. | |
/six. | |
. | |
9. | |
/seven. | |
. | |
0. | |
. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment