Annotate

util/httpstream.lua @ 3564:90f4e6dc1c11

util.httpstream: Added support for HTTP response parsing.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 05 Nov 2010 03:07:36 +0500
parent 3563:544d9d2e3046
child 3565:e2cc09e83a3e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local coroutine = coroutine;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local tonumber = tonumber;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local deadroutine = coroutine.create(function() end);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 coroutine.resume(deadroutine);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 module("httpstream")
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9
3564
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
10 local function parser(data, success_cb, parser_type)
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local function readline()
3496
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
12 local pos = data:find("\r\n", nil, true);
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
13 while not pos do
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
14 data = data..coroutine.yield();
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
15 pos = data:find("\r\n", nil, true);
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 end
3496
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
17 local r = data:sub(1, pos-1);
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
18 data = data:sub(pos+2);
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
19 return r;
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 local function readlength(n)
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 while #data < n do
3496
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
23 data = data..coroutine.yield();
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 local r = data:sub(1, n);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 data = data:sub(n + 1);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 return r;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 end
3562
98f9dca3eb94 util.httpstream: Move HTTP header parsing into its own function.
Waqas Hussain <waqas20@gmail.com>
parents: 3496
diff changeset
29 local function readheaders()
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local headers = {}; -- read headers
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 while true do
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 local line = readline();
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 if line == "" then break; end -- headers done
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local key, val = line:match("^([^%s:]+): *(.*)$");
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 if not key then coroutine.yield("invalid-header-line"); end -- TODO handle multi-line and invalid headers
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 key = key:lower();
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 headers[key] = headers[key] and headers[key]..","..val or val;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 end
3563
544d9d2e3046 util.httpstream: Fixed a nil variable access introduced in the last commit.
Waqas Hussain <waqas20@gmail.com>
parents: 3562
diff changeset
39 return headers;
3562
98f9dca3eb94 util.httpstream: Move HTTP header parsing into its own function.
Waqas Hussain <waqas20@gmail.com>
parents: 3496
diff changeset
40 end
98f9dca3eb94 util.httpstream: Move HTTP header parsing into its own function.
Waqas Hussain <waqas20@gmail.com>
parents: 3496
diff changeset
41
3564
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
42 if not parser_type or parser_type == "server" then
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
43 while true do
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
44 -- read status line
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
45 local status_line = readline();
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
46 local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
47 if not method then coroutine.yield("invalid-status-line"); end
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
48 -- TODO parse url
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
49 local headers = readheaders();
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
50
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
51 -- read body
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
52 local len = tonumber(headers["content-length"]);
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
53 len = len or 0; -- TODO check for invalid len
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
54 local body = readlength(len);
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
55
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
56 success_cb({
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
57 method = method;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
58 path = path;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
59 httpversion = httpversion;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
60 headers = headers;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
61 body = body;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
62 });
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
63 end
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
64 elseif parser_type == "client" then
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
65 while true do
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
66 -- read status line
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
67 local status_line = readline();
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
68 local httpversion, status_code, reason_phrase = status_line:match("^HTTP/(%S+)%s+(%d%d%d)%s+(.*)$");
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
69 if not httpversion then coroutine.yield("invalid-status-line"); end
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
70 local headers = readheaders();
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
71
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
72 -- read body
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
73 local body;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
74 local len = tonumber(headers["content-length"]);
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
75 if len then -- TODO check for invalid len
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
76 body = readlength(len);
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
77 else -- read to end
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
78 repeat
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
79 local newdata = coroutine.yield();
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
80 data = data..newdata;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
81 until newdata == "";
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
82 body, data = data, "";
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
83 end
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
84
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
85 success_cb({
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
86 code = status_code;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
87 responseversion = httpversion;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
88 responseheaders = headers;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
89 body = body;
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
90 });
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
91 end
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
92 else coroutine.yield("unknown-parser-type"); end
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94
3564
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
95 function new(success_cb, error_cb, parser_type)
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 local co = coroutine.create(parser);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 return {
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 feed = function(self, data)
3496
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
99 if not data then
3564
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
100 if parser_type == "client" then coroutine.resume(co, "", success_cb, parser_type); end
3496
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
101 co = deadroutine;
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
102 return error_cb();
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
103 end
3564
90f4e6dc1c11 util.httpstream: Added support for HTTP response parsing.
Waqas Hussain <waqas20@gmail.com>
parents: 3563
diff changeset
104 local success, result = coroutine.resume(co, data, success_cb, parser_type);
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 if result then
3496
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
106 co = deadroutine;
9408d1e10e17 util.httpstream: Refactored and simplified code to improve readability.
Waqas Hussain <waqas20@gmail.com>
parents: 3495
diff changeset
107 return error_cb(result);
3494
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 end;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 };
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 return _M;