use async dns resolve in httpc, and dns resolve cache by ttl. see about issue #253

This commit is contained in:
Cloud Wu
2015-11-25 13:09:41 +08:00
parent d35388581f
commit c180036bed
3 changed files with 89 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
local socket = require "http.sockethelper"
local url = require "http.url"
local internal = require "http.internal"
local dns = require "dns"
local string = string
local table = table
@@ -78,6 +79,13 @@ local function request(fd, method, host, url, recvheader, header, content)
return code, body
end
local async_dns
function httpc.dns(server,port)
async_dns = true
dns.server(server,port)
end
function httpc.request(method, host, url, recvheader, header, content)
local hostname, port = host:match"([^:]+):?(%d*)$"
if port == "" then
@@ -85,6 +93,9 @@ function httpc.request(method, host, url, recvheader, header, content)
else
port = tonumber(port)
end
if async_dns and not hostname:match(".*%d+$") then
hostname = dns.resolve(hostname)
end
local fd = socket.connect(hostname, port)
local ok , statuscode, body = pcall(request, fd,method, host, url, recvheader, header, content)
socket.close(fd)