On Wednesday, September 17, 2014 11:12:27 AM UTC+3, James Hurst wrote:
I'm using resty-http (
https://github.com/pintsized/lua-resty-http) to proxy requests to a backend server. I want to mirror all headers from the proxied response to the client. Using the example code from the documentation, I can access *most* of the headers via the res.headers table but some headers, like Set-Cookie are not there for some reason. How can I forward all proxied response headers including cookies?
Hi,
You shouldn't have any problem accessing all headers using lua-resty-http. Can you post example code and your server response in a gist? You may need to pay attention to the case of the header key, since HTTP header fields are technically case insensitive?
Hi James,
The code calls the following function which proxies the request. As shown below, at first I tried using 'ngx.header.set_cookie = res.headers["Set-Cookie"]' to set the cookie. This worked for setting the content type and length but not the cookie set in the server response. When that didn't work I added a debugging loop to log all of the response headers and saw that the Set-Cookie response header is not there, despite the fact that the back end server sets it (as shown via a tcpdump). Having said that, the response body is proxied correctly. Any ideas how I can proxy the Set-Cookie header? An example response header is show below as well.
function ProxyRequest(Host, URI, Params)
local http = require "resty.http"
local httpc = http.new()
local UserAgent = "Openresty"
httpc:set_timeout(2000)
httpc:connect(Host, 80)
local res, err = httpc:request{
method = "POST",
path = URI,
body = Params,
headers = {
["Host"] = Host,
["User-Agent"] = UserAgent,
["Content-Type"] = "application/x-www-form-urlencoded"
},
}
if not res then
ngx.say("failed to request: ", err)
return
end
for k,v in pairs(res.headers) do
-- Log all headers here - note the Set-Cookie header is missing for some reason
ngx.log(ngx.ERR, "Header: ", k, " ", v)
end
ngx.header.content_type = res.headers["Content-Type"]
ngx.header.content_length = res.headers["Content-Length"]
ngx.header.set_cookie = res.headers["Set-Cookie"]
local reader = res.body_reader
repeat
local chunk, err = reader(8192)
if err then
ngx.log(ngx.ERR, err)
break
end
if chunk then
ngx.say(chunk)
end
until not chunk
local ok, err = httpc:set_keepalive()
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
end
-------------------------------------server response headers--------------------------------------
HTTP/1.1 200 OK
Server: nginx/1.4.1
Date: Wed, 17 Sep 2014 07:38:30 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.10-1~lucid+2uwsgi2
Set-Cookie: login=test%2Ftest
Content-Length: 5117
--------------------------------------------------------------------------------------------------------------
thanks,
- G