Created
February 28, 2014 14:04
-
-
Save hamishforbes/9271653 to your computer and use it in GitHub Desktop.
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
diff --git a/lib/ledge/ledge.lua b/lib/ledge/ledge.lua | |
index 4f7d1cd..4b0cdea 100644 | |
--- a/lib/ledge/ledge.lua | |
+++ b/lib/ledge/ledge.lua | |
@@ -145,6 +145,10 @@ function _M.new(self) | |
enable_esi = false, | |
enable_collapsed_forwarding = false, | |
collapsed_forwarding_window = 60 * 1000, -- Window for collapsed requests (ms) | |
+ | |
+ upstream = nil, | |
+ fastcgi = nil, | |
+ fastcgi_params = nil, | |
} | |
return setmetatable({ config = config }, mt) | |
@@ -1642,6 +1646,46 @@ function _M.read_from_cache(self) | |
end | |
+local function get_upstream_client(self) | |
+ local upstream = self:config_get("upstream") | |
+ if upstream then | |
+ return upstream, "upstream" | |
+ end | |
+ | |
+ -- Everything else will require upstream host / port | |
+ local upstream_host = self:config_get("upstream_host") | |
+ local upstream_port = self:config_get("upstream_port") | |
+ | |
+ local fastcgi = self:config_get("fastcgi") | |
+ if fastcgi then | |
+ local ok,err | |
+ if upstream_port then | |
+ ok,err = fastcgi:connect(upstream_host, upstream_port) | |
+ else | |
+ ok,err = fastcgi:connect(upstream_host) | |
+ end | |
+ | |
+ if not ok then | |
+ ngx_log(ngx_ERR, err) | |
+ return nil, err | |
+ end | |
+ return fastcgi, "fastcgi" | |
+ end | |
+ | |
+ local httpc = http.new() | |
+ local ok,err | |
+ if upstream_port then | |
+ ok,err = httpc:connect(upstream_host, upstream_port) | |
+ else | |
+ ok,err = httpc:connect(upstream_host) | |
+ end | |
+ if not ok then | |
+ ngx_log(ngx_ERR, err) | |
+ return nil, err | |
+ end | |
+ return httpc, "http" | |
+end | |
+ | |
-- Fetches a resource from the origin server. | |
function _M.fetch_from_origin(self) | |
local res = response.new() | |
@@ -1654,18 +1698,33 @@ function _M.fetch_from_origin(self) | |
return res | |
end | |
- ngx.req.read_body() -- Must read body into lua when passing options into location.capture | |
+ local client, client_type = get_upstream_client(self) | |
+ if not client then | |
+ return res | |
+ end | |
+ | |
+ local client_body_reader, err = client:get_client_body_reader() | |
+ if not client_body_reader then | |
+ if err == "chunked request bodies not supported yet" then | |
+ -- TODO: goto state machine here | |
+ ngx.status = 411 | |
+ ngx.say("411 Length Required") | |
+ ngx.exit(ngx.status) | |
+ end | |
+ end | |
- local httpc = http.new() | |
- httpc:connect(self:config_get("upstream_host"), self:config_get("upstream_port")) | |
- | |
- local origin, err = httpc:request{ | |
+ local req_params = { | |
method = ngx_req_get_method(), | |
path = self:relative_uri(), | |
- body = ngx.req.get_body_data(), -- TODO: stream this into httpc? | |
+ body = client_body_reader, | |
headers = ngx_req_get_headers(), | |
} | |
+ if client_type == "fastcgi" then | |
+ req_params.fastcgi = self:config_get("fastcgi_params") | |
+ end | |
+ | |
+ local origin, err = client:request(req_params) | |
if not origin then | |
ngx_log(ngx_ERR, err) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment