summaryrefslogtreecommitdiffstats
path: root/lem/http.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lem/http.lua')
-rw-r--r--lem/http.lua85
1 files changed, 6 insertions, 79 deletions
diff --git a/lem/http.lua b/lem/http.lua
index 1f972a9..c758430 100644
--- a/lem/http.lua
+++ b/lem/http.lua
@@ -16,86 +16,13 @@
-- License along with LEM. If not, see <http://www.gnu.org/licenses/>.
--
-local io = require 'lem.io'
-local http = require 'lem.http.core'
+local http = require 'lem.http.core'
+local parsers = require 'lem.parsers'
-do
- local parsers = require 'lem.parsers'
-
- parsers.lookup['HTTPRequest'] = http.HTTPRequest
- http.HTTPRequest = nil
- parsers.lookup['HTTPResponse'] = http.HTTPResponse
- http.HTTPResponse = nil
-end
-
-local tonumber = tonumber
-local concat = table.concat
-
-function http.Request:body()
- local len, body = self.headers['content-length'], ''
- if not len then return body end
-
- len = tonumber(len)
- if len <= 0 then return body end
-
- if self.headers['expect'] == '100-continue' then
- local ok, err = self.client:write('HTTP/1.1 100 Continue\r\n\r\n')
- if not ok then return nil, err end
- end
-
- local err
- body, err = self.client:read(len)
- if not body then return nil, err end
-
- return body
-end
-
-function http.Response:body_chunked()
- local client = self.client
- local t, n = {}, 0
- local line, err
- while true do
- line, err = client:read('*l')
- if not line then return nil, err end
-
- local num = tonumber(line, 16)
- if not num then return nil, 'expectation failed' end
- if num == 0 then break end
-
- local data, err = client:read(num)
- if not data then return nil, err end
-
- n = n + 1
- t[n] = data
-
- line, err = client:read('*l')
- if not line then return nil, err end
- end
-
- line, err = client:read('*l')
- if not line then return nil, err end
-
- return concat(t)
-end
-
-function http.Response:body()
- if self.headers['transfer-encoding'] == 'chunked' then
- return self:body_chunked()
- end
-
- local num = self.headers['content-length']
- if not num then
- if self.headers['connection'] == 'close' then
- return self.client:read('*a')
- end
- return nil, 'no content length specified'
- end
-
- num = tonumber(num)
- if not num then return nil, 'invalid content length' end
-
- return self.client:read(num)
-end
+parsers.lookup['HTTPRequest'] = http.HTTPRequest
+http.HTTPRequest = nil
+parsers.lookup['HTTPResponse'] = http.HTTPResponse
+http.HTTPResponse = nil
return http