Skip to content

Commit

Permalink
https: Initial HTTPS Client Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Phillips committed Jun 20, 2012
1 parent 5a552fb commit 05f2b24
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/luvit/http.lua
Expand Up @@ -322,6 +322,10 @@ function Response:destroy(...)
return self.socket:destroy(...)
end
function Response:close()
self.socket:close()
end
--------------------------------------------------------------------------------
function http.request(options, callback)
Expand Down Expand Up @@ -350,9 +354,14 @@ function http.request(options, callback)
-- placeholders for original client methods.
-- we restore them upon connection callback fired
local original_write
local createConnection = net.create
if options.createConnection then
createConnection = options.createConnection
end
local client
client = net.create(port, host, function (err)
client = createConnection(port, host, function(err)
if err then
client:emit("error", err)
Expand Down
45 changes: 45 additions & 0 deletions lib/luvit/https.lua
@@ -0,0 +1,45 @@
local fmt = require('string').fmt
local http = require('http')
local tls = require('tls')

function createConnection(...)
local args = {...}
local options = {}
local callback
if type(args[1]) == 'table' then
options = args[1]
elseif type(args[2]) == 'table' then
options = args[2]
options.port = args[1]
elseif type(args[3]) == 'table' then
options = args[3]
options.port = args[2]
options.host = args[1]
else
if type(args[1]) == 'number' then
options.port = args[1]
end
if type(args[2]) == 'string' then
options.host = args[2]
end
end

if type(args[#args]) == 'function' then
callback = args[#args]
end

return tls.connect(options, callback)
end

function request(options, callback)
if options.protocol and options.protocol ~= 'https:' then
error(fmt('Protocol %s not supported', options.protocol))
end
options.createConnection = createConnection
options.port = options.port or 443
return http.request(options, callback)
end

local https = {}
https.request = request
return https
12 changes: 12 additions & 0 deletions lib/luvit/tls.lua
Expand Up @@ -438,6 +438,18 @@ function CleartextStream:address()
return self.socket and self.socket:address()
end
function CleartextStream:shutdown()
if self.socket then
self.socket:shutdown()
end
end
function CleartextStream:close()
if self.socket then
self.socket:destroy()
end
end
--[[ EncryptedStream ]]--
local EncryptedStream = CryptoStream:extend()
Expand Down
1 change: 1 addition & 0 deletions luvit.gyp
Expand Up @@ -70,6 +70,7 @@
'lib/luvit/fiber.lua',
'lib/luvit/fs.lua',
'lib/luvit/http.lua',
'lib/luvit/https.lua',
'lib/luvit/json.lua',
'lib/luvit/luvit.lua',
'lib/luvit/mime.lua',
Expand Down
2 changes: 2 additions & 0 deletions src/luvit_exports.c
Expand Up @@ -11,6 +11,7 @@ extern const char *luaJIT_BC_dns[];
extern const char *luaJIT_BC_fiber[];
extern const char *luaJIT_BC_fs[];
extern const char *luaJIT_BC_http[];
extern const char *luaJIT_BC_https[];
extern const char *luaJIT_BC_json[];
extern const char *luaJIT_BC_luvit[];
extern const char *luaJIT_BC_mime[];
Expand Down Expand Up @@ -41,6 +42,7 @@ const void *luvit__suck_in_symbols(void)
(size_t)(const char *)luaJIT_BC_fiber +
(size_t)(const char *)luaJIT_BC_fs +
(size_t)(const char *)luaJIT_BC_http +
(size_t)(const char *)luaJIT_BC_https +
(size_t)(const char *)luaJIT_BC_json +
(size_t)(const char *)luaJIT_BC_luvit +
(size_t)(const char *)luaJIT_BC_mime +
Expand Down

0 comments on commit 05f2b24

Please sign in to comment.