Skip to content

Commit

Permalink
httpclient支持ssl
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Apr 12, 2024
1 parent 4e389a7 commit a8abc93
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 39 deletions.
3 changes: 2 additions & 1 deletion extend/lssl/src/ssl/lssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ namespace lssl {
m_packet_len = sz;
return lua_gettop(L) - top;
}
m_buf->clean();
bio_write(L, m_slice->head(), sz);
do {
uint8_t* outbuff = m_buf->peek_space(SSL_TLS_READ_SIZE);
Expand Down Expand Up @@ -271,7 +272,6 @@ namespace lssl {

protected:
void tls_handshake(lua_State* L) {
m_buf->clean();
is_handshake = SSL_is_init_finished(ssl);
if (is_handshake) return;
int ret = SSL_do_handshake(ssl);
Expand Down Expand Up @@ -305,6 +305,7 @@ namespace lssl {
}

void bio_read(lua_State* L) {
m_buf->clean();
int pending = BIO_ctrl_pending(out_bio);
while (pending > 0) {
uint8_t* outbuff = m_buf->peek_space(SSL_TLS_READ_SIZE);
Expand Down
8 changes: 0 additions & 8 deletions script/basic/environ.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local qgetenv = quanta.getenv
local saddr = qstring.addr
local ssplit = qstring.split
local usplit = qstring.usplit
local protoaddr = qstring.protoaddr

environ = {}

Expand Down Expand Up @@ -38,13 +37,6 @@ function environ.addr(key)
end
end

function environ.protoaddr(key)
local value = qgetenv(key)
if value then
return protoaddr(value)
end
end

function environ.split(key, val)
local value = qgetenv(key)
if value then
Expand Down
29 changes: 23 additions & 6 deletions script/basic/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ local pcall = pcall
local tostring = tostring
local tunpack = table.unpack
local ssub = string.sub
local sbyte = string.byte
local sfind = string.find
local sgsub = string.gsub
local supper = string.upper
local slower = string.lower
local sformat = string.format
local sbyte = string.byte
local sgmatch = string.gmatch

qstring = {}

Expand Down Expand Up @@ -109,12 +110,28 @@ function qstring.addr(value)
return ip, tonumber(port)
end

function qstring.protoaddr(value)
local addr, proto = tunpack(ssplit(value, "/"))
if addr then
local ip, port = tunpack(ssplit(addr, ":"))
return ip, tonumber(port), proto
local saddr = qstring.addr
function qstring.url(url)
if not url then
return
end
local proto, addr = sgmatch(url, "(.+)://(.+)")()
if not proto then
return
end
local host, port, path
local i, j = addr:find("/")
if not i then
path = "/"
host, port = saddr(addr)
else
path = addr:sub(j)
host, port = saddr(addr:sub(1, i - 1))
end
if not port then
port = proto == "https" and 443 or 80
end
return proto, host, port, path
end

function qstring.usplit(str, token)
Expand Down
50 changes: 26 additions & 24 deletions script/network/http_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ local busdns = luabus.dns
local log_err = logger.err
local log_debug = logger.debug
local tconcat = table.concat
local qsaddr = qstring.addr
local qsurl = qstring.url
local sformat = string.format
local sgmatch = string.gmatch
local jsoncodec = json.jsoncodec
local httpccodec = codec.httpccodec
local luencode = codec.url_encode
Expand Down Expand Up @@ -76,7 +75,10 @@ end

--构建请求
function HttpClient:send_request(url, timeout, querys, headers, method, datas)
local host, port, path, proto = self:parse_url_addr(url)
if not headers then
headers = {["Accept"] = "text/html" }
end
local host, port, path, proto = self:parse_url(headers, url)
if not host then
log_err("[HttpClient][send_request] failed : {}", port)
return false, port
Expand All @@ -85,9 +87,6 @@ function HttpClient:send_request(url, timeout, querys, headers, method, datas)
if not socket then
return false, err
end
if not headers then
headers = {["Content-Type"] = "text/plain" }
end
if type(datas) == "table" then
headers["Content-Type"] = "application/json"
end
Expand Down Expand Up @@ -159,32 +158,35 @@ function HttpClient:format_url(url, query)
return url
end

function HttpClient:parse_url_addr(url)
if not url then
return nil, "htpp url is empty"
end
if url:sub(-1) ~= "/" then
url = sformat("%s/", url)
end
local proto, addr, path = sgmatch(url, "(.+)://([^/]-)(/.*)")()
function HttpClient:parse_url(headers, url)
local proto, host, port, path = qsurl(url)
if not proto then
return nil, "Illegal htpp url"
end
local host, port = qsaddr(addr)
if not port then
port = proto == "https" and 443 or 80
end
local ip = self.domains[host]
if not ip then
local ipinfo = self.domains[host]
if not ipinfo then
if host:sub(1, 3) ~= "www" then
--尝试 + www
local nhost = sformat("www.%s", host)
local ips = busdns(nhost)
if ips then
local ip = ips[1]
headers["Host"] = nhost
self.domains[host] = { ip, nhost }
return ip, port, path, proto
end
end
local ips = busdns(host)
log_debug("[HttpClient][parse_url_addr] host:{} ips {}!", host, ips)
if not ips or #ips == 0 then
return nil, "ip addr parse failed!"
end
ip = ips[1]
self.domains[host] = ip
local ip = ips[1]
headers["Host"] = host
self.domains[host] = { ip, host }
return ip, port, path, proto
end
return ip, port, path, proto
headers["Host"] = ipinfo[2]
return ipinfo[1], port, path, proto
end

quanta.http_client = HttpClient()
Expand Down

0 comments on commit a8abc93

Please sign in to comment.