Wang Yaofu | e432c6b | 2016-03-09 16:39:03 +0800 | [diff] [blame] | 1 | -- |
| 2 | -- Licensed to the Apache Software Foundation (ASF) under one |
| 3 | -- or more contributor license agreements. See the NOTICE file |
| 4 | -- distributed with this work for additional information |
| 5 | -- regarding copyright ownership. The ASF licenses this file |
| 6 | -- to you under the Apache License, Version 2.0 (the |
| 7 | -- "License"); you may not use this file except in compliance |
| 8 | -- with the License. You may obtain a copy of the License at |
| 9 | -- |
| 10 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | -- |
| 12 | -- Unless required by applicable law or agreed to in writing, |
| 13 | -- software distributed under the License is distributed on an |
| 14 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | -- KIND, either express or implied. See the License for the |
| 16 | -- specific language governing permissions and limitations |
| 17 | -- under the License. |
| 18 | -- |
| 19 | |
| 20 | require 'TTransport' |
| 21 | |
| 22 | THttpTransport = TTransportBase:new{ |
| 23 | __type = 'THttpTransport', |
| 24 | path = '/', |
| 25 | wBuf = '', |
| 26 | rBuf = '', |
| 27 | CRLF = '\r\n', |
James E. King III | c3375d9 | 2018-12-30 11:06:00 -0500 | [diff] [blame] | 28 | VERSION = version, |
Wang Yaofu | e432c6b | 2016-03-09 16:39:03 +0800 | [diff] [blame] | 29 | isServer = true |
| 30 | } |
| 31 | |
| 32 | function THttpTransport:new(obj) |
| 33 | if ttype(obj) ~= 'table' then |
| 34 | error(ttype(self) .. 'must be initialized with a table') |
| 35 | end |
| 36 | |
| 37 | -- Ensure a transport is provided |
| 38 | if not obj.trans then |
| 39 | error('You must provide ' .. ttype(self) .. ' with a trans') |
| 40 | end |
| 41 | |
| 42 | return TTransportBase.new(self, obj) |
| 43 | end |
| 44 | |
| 45 | function THttpTransport:isOpen() |
| 46 | return self.trans:isOpen() |
| 47 | end |
| 48 | |
| 49 | function THttpTransport:open() |
| 50 | return self.trans:open() |
| 51 | end |
| 52 | |
| 53 | function THttpTransport:close() |
| 54 | return self.trans:close() |
| 55 | end |
| 56 | |
| 57 | function THttpTransport:readAll(len) |
| 58 | return self:read(len) |
| 59 | end |
| 60 | |
| 61 | function THttpTransport:read(len) |
| 62 | if string.len(self.rBuf) == 0 then |
| 63 | self:_readMsg() |
| 64 | end |
| 65 | if len > string.len(self.rBuf) then |
| 66 | local val = self.rBuf |
| 67 | self.rBuf = '' |
| 68 | return val |
| 69 | end |
| 70 | |
| 71 | local val = string.sub(self.rBuf, 0, len) |
| 72 | self.rBuf = string.sub(self.rBuf, len+1) |
| 73 | return val |
| 74 | end |
| 75 | |
| 76 | function THttpTransport:_readMsg() |
| 77 | while true do |
| 78 | self.rBuf = self.rBuf .. self.trans:read(4) |
| 79 | if string.find(self.rBuf, self.CRLF .. self.CRLF) then |
| 80 | break |
| 81 | end |
| 82 | end |
| 83 | if not self.rBuf then |
| 84 | self.rBuf = "" |
| 85 | return |
| 86 | end |
| 87 | self:getLine() |
| 88 | local headers = self:_parseHeaders() |
| 89 | if not headers then |
| 90 | self.rBuf = "" |
| 91 | return |
| 92 | end |
| 93 | |
| 94 | local length = tonumber(headers["Content-Length"]) |
| 95 | if length then |
| 96 | length = length - string.len(self.rBuf) |
| 97 | self.rBuf = self.rBuf .. self.trans:readAll(length) |
| 98 | end |
| 99 | if self.rBuf == nil then |
| 100 | self.rBuf = "" |
| 101 | end |
| 102 | end |
| 103 | |
| 104 | function THttpTransport:getLine() |
| 105 | local a,b = string.find(self.rBuf, self.CRLF) |
| 106 | local line = "" |
| 107 | if a and b then |
| 108 | line = string.sub(self.rBuf, 0, a-1) |
| 109 | self.rBuf = string.sub(self.rBuf, b+1) |
| 110 | end |
| 111 | return line |
| 112 | end |
| 113 | |
| 114 | function THttpTransport:_parseHeaders() |
| 115 | local headers = {} |
| 116 | |
| 117 | repeat |
| 118 | local line = self:getLine() |
| 119 | for key, val in string.gmatch(line, "([%w%-]+)%s*:%s*(.+)") do |
| 120 | if headers[key] then |
| 121 | local delimiter = ", " |
| 122 | if key == "Set-Cookie" then |
| 123 | delimiter = "; " |
| 124 | end |
| 125 | headers[key] = headers[key] .. delimiter .. tostring(val) |
| 126 | else |
| 127 | headers[key] = tostring(val) |
| 128 | end |
| 129 | end |
| 130 | until string.find(line, "^%s*$") |
| 131 | |
| 132 | return headers |
| 133 | end |
| 134 | |
| 135 | function THttpTransport:write(buf, len) |
| 136 | if len and len < string.len(buf) then |
| 137 | buf = string.sub(buf, 0, len) |
| 138 | end |
| 139 | self.wBuf = self.wBuf .. buf |
| 140 | end |
| 141 | |
| 142 | function THttpTransport:writeHttpHeader(content_len) |
| 143 | if self.isServer then |
| 144 | local header = "HTTP/1.1 200 OK" .. self.CRLF |
| 145 | .. "Server: Thrift/" .. self.VERSION .. self.CRLF |
| 146 | .. "Access-Control-Allow-Origin: *" .. self.CRLF |
| 147 | .. "Content-Type: application/x-thrift" .. self.CRLF |
| 148 | .. "Content-Length: " .. content_len .. self.CRLF |
| 149 | .. "Connection: Keep-Alive" .. self.CRLF .. self.CRLF |
| 150 | self.trans:write(header) |
| 151 | else |
| 152 | local header = "POST " .. self.path .. " HTTP/1.1" .. self.CRLF |
| 153 | .. "Host: " .. self.trans.host .. self.CRLF |
| 154 | .. "Content-Type: application/x-thrift" .. self.CRLF |
| 155 | .. "Content-Length: " .. content_len .. self.CRLF |
| 156 | .. "Accept: application/x-thrift " .. self.CRLF |
| 157 | .. "User-Agent: Thrift/" .. self.VERSION .. " (Lua/THttpClient)" |
| 158 | .. self.CRLF .. self.CRLF |
| 159 | self.trans:write(header) |
| 160 | end |
| 161 | end |
| 162 | |
| 163 | function THttpTransport:flush() |
| 164 | -- If the write fails we still want wBuf to be clear |
| 165 | local tmp = self.wBuf |
| 166 | self.wBuf = '' |
| 167 | self:writeHttpHeader(string.len(tmp)) |
| 168 | self.trans:write(tmp) |
| 169 | self.trans:flush() |
| 170 | end |
| 171 | |
| 172 | THttpTransportFactory = TTransportFactoryBase:new{ |
| 173 | __type = 'THttpTransportFactory' |
| 174 | } |
| 175 | function THttpTransportFactory:getTransport(trans) |
| 176 | if not trans then |
| 177 | terror(TProtocolException:new{ |
| 178 | message = 'Must supply a transport to ' .. ttype(self) |
| 179 | }) |
| 180 | end |
| 181 | return THttpTransport:new{trans = trans} |
| 182 | end |