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: Initial HTTPS Client Support #254

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
62 changes: 62 additions & 0 deletions lib/luvit/https.lua
@@ -0,0 +1,62 @@
--[[

Copyright 2012 The Luvit Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS-IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

--]]
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