2 local tonumber = tonumber;
4 local url_parse = require "socket.url".parse;
5 local urldecode = require "net.http".urldecode;
7 local function preprocess_path(path)
8 path = urldecode((path:gsub("//+", "/")));
9 if path:sub(1,1) ~= "/" then
13 for component in path:gmatch("([^/]+)/") do
14 if component == ".." then
16 elseif component ~= "." then
26 local httpstream = {};
28 function httpstream.new(success_cb, error_cb, parser_type, options_cb)
30 if not parser_type or parser_type == "server" then client = false; else assert(parser_type == "client", "Invalid parser type"); end
39 feed = function(self, data)
40 if error then return nil, "parse has failed"; end
41 if not data then -- EOF
42 if state and client and not len then -- reading client body until EOF
45 elseif buf ~= "" then -- unexpected EOF
46 error = true; return error_cb();
52 if state == nil then -- read request
53 local index = buf:find("\r\n\r\n", nil, true);
54 if not index then return; end -- not enough data
55 local method, path, httpversion, status_code, reason_phrase;
58 for line in buf:sub(1,index+1):gmatch("([^\r\n]+)\r\n") do -- parse request
60 local key, val = line:match("^([^%s:]+): *(.*)$");
61 if not key then error = true; return error_cb("invalid-header-line"); end -- TODO handle multi-line and invalid headers
63 headers[key] = headers[key] and headers[key]..","..val or val;
67 httpversion, status_code, reason_phrase = line:match("^HTTP/(1%.[01]) (%d%d%d) (.*)$");
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) );
73 chunked = have_body and headers["transfer-encoding"] == "chunked";
75 method, path, httpversion = line:match("^(%w+) (%S+) HTTP/(1%.[01])$");
76 if not method then error = true; return error_cb("invalid-status-line"); end
80 if not first_line then error = true; return error_cb("invalid-status-line"); end
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 local index = buf:find("\r\n", nil, true);
124 if not index then return; end -- not enough data
125 local chunk_size = buf:match("^%x+");
126 if not chunk_size then error = true; return error_cb("invalid-chunk-size"); end
127 chunk_size = tonumber(chunk_size, 16);
129 if chunk_size == 0 then
130 state = nil; success_cb(packet);
131 elseif #buf - index + 1 >= chunk_size then -- we have a chunk
132 packet.body = packet.body..buf:sub(index, index + chunk_size - 1);
133 buf = buf:sub(index + chunk_size);
135 error("trailers"); -- FIXME MUST read trailers
136 elseif len and #buf >= len then
137 packet.body, buf = buf:sub(1, len), buf:sub(len + 1);
138 state = nil; success_cb(packet);
140 elseif #buf >= len then
141 packet.body, buf = buf:sub(1, len), buf:sub(len + 1);
142 state = nil; success_cb(packet);