Comparison

util/httpstream.lua @ 3562:98f9dca3eb94

util.httpstream: Move HTTP header parsing into its own function.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 05 Nov 2010 02:09:56 +0500
parent 3496:9408d1e10e17
child 3563:544d9d2e3046
comparison
equal deleted inserted replaced
3561:579c087059fc 3562:98f9dca3eb94
24 end 24 end
25 local r = data:sub(1, n); 25 local r = data:sub(1, n);
26 data = data:sub(n + 1); 26 data = data:sub(n + 1);
27 return r; 27 return r;
28 end 28 end
29 29 local function readheaders()
30 while true do
31 -- read status line
32 local status_line = readline();
33 local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
34 if not method then coroutine.yield("invalid-status-line"); end
35 -- TODO parse url
36
37 local headers = {}; -- read headers 30 local headers = {}; -- read headers
38 while true do 31 while true do
39 local line = readline(); 32 local line = readline();
40 if line == "" then break; end -- headers done 33 if line == "" then break; end -- headers done
41 local key, val = line:match("^([^%s:]+): *(.*)$"); 34 local key, val = line:match("^([^%s:]+): *(.*)$");
42 if not key then coroutine.yield("invalid-header-line"); end -- TODO handle multi-line and invalid headers 35 if not key then coroutine.yield("invalid-header-line"); end -- TODO handle multi-line and invalid headers
43 key = key:lower(); 36 key = key:lower();
44 headers[key] = headers[key] and headers[key]..","..val or val; 37 headers[key] = headers[key] and headers[key]..","..val or val;
45 end 38 end
39 end
40
41 while true do
42 -- read status line
43 local status_line = readline();
44 local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
45 if not method then coroutine.yield("invalid-status-line"); end
46 -- TODO parse url
47 local headers = readheaders();
46 48
47 -- read body 49 -- read body
48 local len = tonumber(headers["content-length"]); 50 local len = tonumber(headers["content-length"]);
49 len = len or 0; -- TODO check for invalid len 51 len = len or 0; -- TODO check for invalid len
50 local body = readlength(len); 52 local body = readlength(len);