1 local tonumber = tonumber;
3 local url_parse = require "socket.url".parse;
4 local urldecode = require "util.http".urldecode;
6 local function preprocess_path(path)
7 path = urldecode((path:gsub("//+", "/")));
8 if path:sub(1,1) ~= "/" then
12 for component in path:gmatch("([^/]+)/") do
13 if component == ".." then
15 elseif component ~= "." then
25 local httpstream = {};
27 function httpstream.new(success_cb, error_cb, parser_type, options_cb)
29 if not parser_type or parser_type == "server" then client = false; else assert(parser_type == "client", "Invalid parser type"); end
31 local chunked, chunk_size, chunk_start;
38 feed = function(self, data)
39 if error then return nil, "parse has failed"; end
40 if not data then -- EOF
41 if state and client and not len then -- reading client body until EOF
44 elseif buf ~= "" then -- unexpected EOF
45 error = true; return error_cb();
51 if state == nil then -- read request
52 local index = buf:find("\r\n\r\n", nil, true);
53 if not index then return; end -- not enough data
54 local method, path, httpversion, status_code, reason_phrase;
57 for line in buf:sub(1,index+1):gmatch("([^\r\n]+)\r\n") do -- parse request
59 local key, val = line:match("^([^%s:]+): *(.*)$");
60 if not key then error = true; return error_cb("invalid-header-line"); end -- TODO handle multi-line and invalid headers
62 headers[key] = headers[key] and headers[key]..","..val or val;
66 httpversion, status_code, reason_phrase = line:match("^HTTP/(1%.[01]) (%d%d%d) (.*)$");
67 status_code = tonumber(status_code);
68 if not status_code then error = true; return error_cb("invalid-status-line"); end
70 ( (options_cb and options_cb().method == "HEAD")
71 or (status_code == 204 or status_code == 304 or status_code == 301)
72 or (status_code >= 100 and status_code < 200) );
74 method, path, httpversion = line:match("^(%w+) (%S+) HTTP/(1%.[01])$");
75 if not method then error = true; return error_cb("invalid-status-line"); end
79 if not first_line then error = true; return error_cb("invalid-status-line"); end
80 chunked = have_body and headers["transfer-encoding"] == "chunked";
81 len = tonumber(headers["content-length"]); -- TODO check for invalid len
83 -- FIXME handle '100 Continue' response (by skipping it)
84 if not have_body then len = 0; end
87 httpversion = httpversion;
89 body = have_body and "" or nil;
90 -- COMPAT the properties below are deprecated
91 responseversion = httpversion;
92 responseheaders = headers;
96 if path:byte() == 47 then -- starts with /
97 local _path, _query = path:match("([^?]*).?(.*)");
98 if _query == "" then _query = nil; end
99 parsed_url = { path = _path, query = _query };
101 parsed_url = url_parse(path);
102 if not(parsed_url and parsed_url.path) then error = true; return error_cb("invalid-url"); end
104 path = preprocess_path(parsed_url.path);
105 headers.host = parsed_url.host or headers.host;
112 httpversion = httpversion;
117 buf = buf:sub(index + 4);
120 if state then -- read body
123 if not buf:find("\r\n", nil, true) then
125 end -- not enough data
126 if not chunk_size then
127 chunk_size, chunk_start = buf:match("^(%x+)[^\r\n]*\r\n()");
128 chunk_size = chunk_size and tonumber(chunk_size, 16);
129 if not chunk_size then error = true; return error_cb("invalid-chunk-size"); end
131 if chunk_size == 0 and buf:find("\r\n\r\n", chunk_start-2, true) then
132 state, chunk_size = nil, nil;
133 buf = buf:gsub("^.-\r\n\r\n", ""); -- This ensure extensions and trailers are stripped
135 elseif #buf - chunk_start + 2 >= chunk_size then -- we have a chunk
136 packet.body = packet.body..buf:sub(chunk_start, chunk_start + (chunk_size-1));
137 buf = buf:sub(chunk_start + chunk_size + 2);
138 chunk_size, chunk_start = nil, nil;
139 else -- Partial chunk remaining
142 elseif len and #buf >= len then
143 if packet.code == 101 then
144 packet.body, buf = buf, ""
146 packet.body, buf = buf:sub(1, len), buf:sub(len + 1);
148 state = nil; success_cb(packet);
152 elseif #buf >= len then
153 packet.body, buf = buf:sub(1, len), buf:sub(len + 1);
154 state = nil; success_cb(packet);