Software /
code /
prosody
Annotate
util/httpstream.lua @ 3494:0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sat, 04 Sep 2010 17:44:13 +0500 |
child | 3495:bd7699a6d536 |
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 setmetatable = setmetatable; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 local coroutine = coroutine; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 local tonumber = tonumber; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 local print = print; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 local error = error; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local ser = require "util.serialization".serialize; |
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 deadroutine = coroutine.create(function() end); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 coroutine.resume(deadroutine); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 module("httpstream") |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 local function parser(data, success_cb) |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 local function readline() |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 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
|
18 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
|
19 if not pos then |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 local newdata = coroutine.yield(); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 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
|
22 data = data..newdata; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 return readline(); |
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 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
|
26 return line; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 end |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 local function readlength(n) |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 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
|
30 while #data < n do |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 local newdata = coroutine.yield(); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 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
|
33 data = data..newdata; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 end |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 local r = data:sub(1, n); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 data = data:sub(n + 1); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 return r; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 end |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 while true do |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 -- read status line |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 local status_line = readline(); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 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
|
44 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
|
45 -- TODO parse url |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 local headers = {}; -- read headers |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 while true do |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 local line = readline(); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 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
|
51 local key, val = line:match("^([^%s:]+): *(.*)$"); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 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
|
53 key = key:lower(); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 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
|
55 end |
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 -- read body |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 local len = tonumber(headers["content-length"]); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 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
|
60 local body = readlength(len); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 success_cb({ |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 method = method; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 path = path; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 httpversion = httpversion; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 headers = headers; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 body = body; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 }); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 end |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 end |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 function new(success_cb, error_cb) |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 local co = coroutine.create(parser); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 return { |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 feed = function(self, data) |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 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
|
77 if result then |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 if result.method then |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 success_cb(result); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 else -- error |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 error_cb(result); |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 co = deadroutine; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 end |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 end |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 end; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 }; |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 end |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 |
0f185563a4e4
util.httpstream: Initial commit of the new HTTP parser.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 return _M; |