Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPS support ? #3

Closed
sebcante opened this issue Nov 24, 2012 · 8 comments
Closed

HTTPS support ? #3

sebcante opened this issue Nov 24, 2012 · 8 comments

Comments

@sebcante
Copy link

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
Copy link

chase commented Nov 24, 2012

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

@sebcante
Copy link
Author

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

@chase
Copy link

chase commented Nov 24, 2012

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
Copy link
Author

good news!, it looks like @agentzh is looking at adding support for SSL for lua nginx module's cosocket (openresty/lua-nginx-module#178)

@sebcante
Copy link
Author

i ll give that a try thanks chase!

@leafo
Copy link

leafo commented Nov 24, 2012

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
Copy link

ziontab commented Oct 21, 2014

Starting from lua-nginx-module 0.9.11 cosockets support SSL openresty/lua-nginx-module#290

@wendal
Copy link
Collaborator

wendal commented Feb 9, 2015

done

@wendal wendal closed this as completed Feb 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants