liseen / lua-resty-http

Lua http client driver for the ngx_lua based on the cosocket API
188 stars 70 forks source link

HTTPS support ? #3

Closed sebcante closed 9 years ago

sebcante commented 12 years ago

Hi,

First let me congrats you with great lib. I was just wondering if HTTPS was supported ? i do not think to have any luck with it. i managed to get http working ok.

thanks

-seb

local hc = http:new()
local ok, code, headers, status, body  = hc:request {
    url = "https://secure.com/auth",
    method = "POST", 
    scheme = "https",
    port = "443",
    headers = { ["Content-Length"] = string.len(post_args_escaped), ["Content-Type"] = "application/x-www-form-urlencoded" },
    body = post_args_escaped,
}
chase commented 12 years ago

HTTPS is not supported as the Lua Nginx module's cosockets do not support SSL.

sebcante commented 12 years ago

thanks chase for the quick answer. would you know any other alternative to HTTP post via SSL from lua nginx?

chase commented 12 years ago

I'm fairly certain you can make HTTPS requests using nginx's proxy_pass and a subrequest from Lua Nginx

@leafo created an elagent solution use the aforementioned method. The language you see in the example is MoonScript, a language like CoffeeScript but for Lua.

The code for proxy_http.moon roughly compiles to:

-- proxy_http.lua
-- This implements LuaSocket's http.request on top of a proxy_pass within
-- nginx.
--
-- Add the following location to your server:
--
-- location /proxy {
--     internal;   
--     rewrite_by_lua "
--       local req = ngx.req
--       req.clear_header'Cookie'
--       req.clear_header'Accept-Encoding'
--       req.clear_header'User-Agent'
--       if ngx.ctx.headers then
--         for k,v in pairs(ngx.ctx.headers) do
--           req.set_header(k, v)
--         end
--       end
--     ";
-- 
--     resolver 8.8.8.8;
--     proxy_pass $_url;
-- }

local ltn12 = require("ltn12")
local proxy_location = "/proxy"
local methods = {
  ["GET"] = ngx.HTTP_GET,
  ["HEAD"] = ngx.HTTP_HEAD,
  ["PUT"] = ngx.HTTP_PUT,
  ["POST"] = ngx.HTTP_POST,
  ["DELETE"] = ngx.HTTP_DELETE,
  ["OPTIONS"] = ngx.HTTP_OPTIONS
}
local set_proxy_location
set_proxy_location = function(loc)
  proxy_location = loc
end
local request
request = function(url, str_body)
  local return_res_body
  local req
  if type(url) == "table" then
    req = url
  else
    return_res_body = true
    req = {
      url = url,
      source = str_body and ltn12.source.string(str_body)
    }
  end
  req.method = req.method or (req.source and "POST" or "GET")
  local body
  if req.source then
    local buff = { }
    local sink = ltn12.sink.table(buff)
    ltn12.pump.all(req.source, sink)
    body = table.concat(buff)
  end
  local res = ngx.location.capture(proxy_location, {
    method = methods[req.method],
    body = body,
    ctx = {
      headers = req.headers
    },
    vars = {
      _url = req.url
    }
  })
  local out
  if return_res_body then
    out = res.body
  else
    if req.sink then
      ltn12.pump.all(ltn12.source.string(res.body), req.sink)
    end
    out = 1
  end
  return out, res.status, res.header
end
return {
  request = request,
  set_proxy_location = set_proxy_location
}
sebcante commented 12 years ago

good news!, it looks like @agentzh is looking at adding support for SSL for lua nginx module's cosocket (https://github.com/chaoslawful/lua-nginx-module/issues/178)

sebcante commented 12 years ago

i ll give that a try thanks chase!

leafo commented 12 years ago

There were some issues with headers in the snippet above. I've fixed it. You can find the updated version here: https://github.com/leafo/heroku-openresty/wiki/Making-HTTP-Requests

Also it's worth noting that with this method you can't use any hosts that are local to your system, like localhost. It has to be a url that be resolved by 8.8.8.8, or an IP address. Just so you know why it won't work when you try to fetch localhost :).

ziontab commented 10 years ago

Starting from lua-nginx-module 0.9.11 cosockets support SSL https://github.com/openresty/lua-nginx-module/pull/290

wendal commented 9 years ago

done