Annotate

util/httpstream.lua @ 3495:bd7699a6d536

util.httpstream: Removed unused variables.
author Waqas Hussain <waqas20@gmail.com>
date Fri, 17 Sep 2010 03:52:11 +0500
parent 3494:0f185563a4e4
child 3496:9408d1e10e17
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
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10 local function parser(data, success_cb)
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 local function readline()
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 if not data then coroutine.yield("Unexpected EOF"); end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local pos, line = (data:find("\r\n", nil, true));
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 if not pos then
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local newdata = coroutine.yield();
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 if not newdata then data = nil; coroutine.yield("Unexpected EOF"); end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 data = data..newdata;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 return readline();
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 line, data = data:sub(1, pos-1), data:sub(pos+2);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 return line;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 local function readlength(n)
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 if not data then coroutine.yield("Unexpected EOF"); end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 while #data < n do
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 local newdata = coroutine.yield();
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 if not newdata then data = nil; coroutine.yield("Unexpected EOF"); end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 data = data..newdata;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 local r = data:sub(1, n);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 data = data:sub(n + 1);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 return r;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 while true do
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 -- read status line
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 local status_line = readline();
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38 local method, path, httpversion = status_line:match("^(%S+)%s+(%S+)%s+HTTP/(%S+)$");
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 if not method then coroutine.yield("invalid-status-line"); end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 -- TODO parse url
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 local headers = {}; -- read headers
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 while true do
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 local line = readline();
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 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
46 local key, val = line:match("^([^%s:]+): *(.*)$");
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 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
48 key = key:lower();
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 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
50 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 -- read body
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 local len = tonumber(headers["content-length"]);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 len = len or 0; -- TODO check for invalid len
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 local body = readlength(len);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 success_cb({
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 method = method;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 path = path;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 httpversion = httpversion;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 headers = headers;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 body = body;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 });
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 function new(success_cb, error_cb)
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 local co = coroutine.create(parser);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 return {
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 feed = function(self, data)
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 local success, result = coroutine.resume(co, data, success_cb);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 if result then
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 if result.method then
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 success_cb(result);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 else -- error
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 error_cb(result);
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 co = deadroutine;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 end;
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 };
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 end
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83
0f185563a4e4 util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 return _M;