Skip to content

Instantly share code, notes, and snippets.

@danielkaldheim
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkaldheim/45f425fdfdd11913ab3b to your computer and use it in GitHub Desktop.
Save danielkaldheim/45f425fdfdd11913ab3b to your computer and use it in GitHub Desktop.
require 'Thrift'
require 'TTransport'
http = require("socket.http")
local https = require 'ssl.https'
local ltn12 = require("ltn12")
local table = table
THttpClient = TTransportBase:new{
__type = 'THttpClient',
wBuf = '',
rBuf = '',
rPos = 0,
scheme = '',
host = '',
port = '',
uri = '',
timeout = null,
headers = {
Host = '',
Accept = 'application/x-thrift',
["User-Agent"] = 'Lua/THttpClient',
["Content-Type"] = 'application/x-thrift',
["Content-Length"] = 0
}
}
function THttpClient:isOpen() end
function THttpClient:open() end
function THttpClient:close() end
function THttpClient:read(len)
local val = string.sub(self.rBuf, self.rPos, len)
self.rPos = self.rPos + len
return val
end
function THttpClient:write(buf)
self.wBuf = self.wBuf .. buf
end
function THttpClient:flush()
local host = self.host
if self.port == 80 then
else
host = self.host .. ":" .. self.port
end
self.headers.Host = host
self.headers['Content-Length'] = string.len(self.wBuf)
local t = {}
if self.scheme == "http" then
b,c,h = http.request {
url = self.scheme .. "://" .. host .. self.uri,
method = "POST",
headers = self.headers,
redirect = false,
source = ltn12.source.string(self.wBuf),
sink = ltn12.sink.table(t),
}
elseif self.scheme == "https" then
b,c,h = https.request {
url = self.scheme .. "://" .. host .. self.uri,
method = "POST",
headers = self.headers,
redirect = false,
source = ltn12.source.string(self.wBuf),
sink = ltn12.sink.table(t)
}
end
self.rBuf = table.concat(t)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment