This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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