Skip to content

Commit 6944d74

Browse files
committed
resty http client handles errors, is used by default in the nginx context
1 parent 855fe18 commit 6944d74

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

lapis/http.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
if ngx then
2-
return require("lapis.nginx.http")
2+
if ngx.config and ngx.config.is_console then
3+
return require("lapis.nginx.resty_http")
4+
else
5+
return require("lapis.nginx.http")
6+
end
37
elseif package.loaded["lapis.running_server"] == "cqueues" then
48
return require("http.compat.socket")
59
else

lapis/http.moon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
if ngx
2-
require "lapis.nginx.http"
2+
if ngx.config and ngx.config.is_console
3+
-- the "location capture" client is not compatible with resty
4+
require "lapis.nginx.resty_http"
5+
else
6+
require "lapis.nginx.http"
37
elseif package.loaded["lapis.running_server"] == "cqueues"
48
require "http.compat.socket"
59
else

lapis/nginx/resty_http.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
local lapis_config = require("lapis.config")
22
local increment_perf
33
increment_perf = require("lapis.nginx.context").increment_perf
4+
local wrap_source
5+
wrap_source = function(source)
6+
return function()
7+
local chunk, err = source()
8+
if err then
9+
ngx.log(ngx.ERR, "source error: ", err)
10+
return nil
11+
end
12+
return chunk
13+
end
14+
end
415
local request
516
request = function(url, str_body)
617
local http = require("resty.http")
@@ -30,7 +41,7 @@ request = function(url, str_body)
3041
local res, err = httpc:request_uri(req.url, {
3142
method = req.method,
3243
headers = req.headers,
33-
body = req.source,
44+
body = req.source and wrap_source(req.source),
3445
ssl_verify = true
3546
})
3647
if not (res) then

lapis/nginx/resty_http.moon

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11

22
-- This implements a basic LuaSocket's http.request interface using lua-resty-http.
3+
-- Dependencies:
4+
-- luarocks install lua-resty-http
5+
-- luarocks install lua-resty-openssl
36
--
47
-- Usage:
58
-- http = require "lapis.nginx.resty_http"
69
-- body, status, headers = http.request("http://example.com")
10+
--
11+
-- From resty:
12+
-- resty --http-conf "lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; lua_ssl_verify_depth 2;" -e 'print(require("lapis.http").request("https://leafo.net"))'
713

814
lapis_config = require "lapis.config"
915

1016
import increment_perf from require "lapis.nginx.context"
1117

18+
-- convert ltn12 source to a function that resty.http can use without swalling up any error messages
19+
wrap_source = (source) ->
20+
->
21+
chunk, err = source!
22+
if err
23+
ngx.log ngx.ERR, "source error: ", err
24+
return nil
25+
chunk
26+
1227
---Make an HTTP request using lua-resty-http
1328
---@param url string|table URL string or request table with url, method, source, sink, headers fields
1429
---@param str_body string? Request body for simple POST requests
@@ -44,7 +59,7 @@ request = (url, str_body) ->
4459
res, err = httpc\request_uri req.url, {
4560
method: req.method
4661
headers: req.headers
47-
body: req.source -- ltn12 source is compatible as body iterator; caller provides Content-Length header if needed
62+
body: req.source and wrap_source req.source
4863
ssl_verify: true
4964
}
5065

0 commit comments

Comments
 (0)