Annotate

net/httpserver.lua @ 847:2d424936723c

core.componentmanager: Refactor a little to make XEP-0114 plugin a little simpler
author Matthew Wild <mwild1@gmail.com>
date Sat, 28 Feb 2009 02:05:37 +0000
parent 697:8ddc85fa7602
child 958:172fb9a73017
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local socket = require "socket"
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local server = require "net.server"
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local url_parse = require "socket.url".parse;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local connlisteners_start = require "net.connlisteners".start;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local connlisteners_get = require "net.connlisteners".get;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 local listener;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local t_insert, t_concat = table.insert, table.concat;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local s_match, s_gmatch = string.match, string.gmatch;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local tonumber, tostring, pairs = tonumber, tostring, pairs;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local urlcodes = setmetatable({}, { __index = function (t, k) t[k] = char(tonumber("0x"..k)); return t[k]; end });
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local urlencode = function (s) return s and (s:gsub("%W", function (c) return string.format("%%%x", c:byte()); end)); end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local log = require "util.logger".init("httpserver");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local http_servers = {};
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 module "httpserver"
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local default_handler;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 local function expectbody(reqt)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 return reqt.method == "POST";
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 local function send_response(request, response)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- Write status line
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local resp;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if response.body then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 log("debug", "Sending response to %s: %s", request.id, response.body);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 resp = { "HTTP/1.0 ", response.status or "200 OK", "\r\n"};
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 local h = response.headers;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 if h then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 for k, v in pairs(h) do
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 t_insert(resp, k);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 t_insert(resp, ": ");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 t_insert(resp, v);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 t_insert(resp, "\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 if response.body and not (h and h["Content-Length"]) then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 t_insert(resp, "Content-Length: ");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 t_insert(resp, #response.body);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 t_insert(resp, "\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 t_insert(resp, "\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 if response.body and request.method ~= "HEAD" then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 t_insert(resp, response.body);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 -- Response we have is just a string (the body)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 log("debug", "Sending response to %s: %s", request.id, response);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 resp = { "HTTP/1.0 200 OK\r\n" };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 t_insert(resp, "Connection: close\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 t_insert(resp, "Content-Length: ");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 t_insert(resp, #response);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 t_insert(resp, "\r\n\r\n");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 t_insert(resp, response);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 request.write(t_concat(resp));
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 if not request.stayopen then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 request:destroy();
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 local function call_callback(request, err)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 if request.handled then return; end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 request.handled = true;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 local callback = request.callback;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 if not callback and request.path then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 local path = request.url.path;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 local base = path:match("^/([^/?]+)");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 if not base then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 base = path:match("^http://[^/?]+/([^/?]+)");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 callback = (request.server and request.server.handlers[base]) or default_handler;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 if callback == default_handler then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 log("debug", "Default callback for this request (base: "..tostring(base)..")")
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if callback then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 if err then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 log("debug", "Request error: "..err);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 if not callback(nil, err, request) then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 destroy_request(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 return;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 local response = callback(request.method, request.body and t_concat(request.body), request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 if response then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 if response == true then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 -- Keep connection open, we will reply later
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 log("warn", "Request %s left open, on_destroy is %s", request.id, tostring(request.on_destroy));
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 -- Assume response
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 send_response(request, response);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 destroy_request(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 log("debug", "Request handler provided no response, destroying request...");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109 -- No response, close connection
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 destroy_request(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 local function request_reader(request, data, startpos)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 if not data then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 if request.body then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 call_callback(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 -- Error.. connection was closed prematurely
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 call_callback(request, "connection-closed");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 -- Here we force a destroy... the connection is gone, so we can't reply later
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 destroy_request(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 return;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 if request.state == "body" then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 log("debug", "Reading body...")
697
8ddc85fa7602 core.httpserver: Rename request.responseheaders to the more logical request.headers
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
129 if not request.body then request.body = {}; request.havebodylength, request.bodylength = 0, tonumber(request.headers["content-length"]); end
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 if startpos then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 data = data:sub(startpos, -1)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 t_insert(request.body, data);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 if request.bodylength then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 request.havebodylength = request.havebodylength + #data;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 if request.havebodylength >= request.bodylength then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 -- We have the body
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 call_callback(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 elseif request.state == "headers" then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142 log("debug", "Reading headers...")
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 local pos = startpos;
697
8ddc85fa7602 core.httpserver: Rename request.responseheaders to the more logical request.headers
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
144 local headers = request.headers or {};
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 for line in data:gmatch("(.-)\r\n") do
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 startpos = (startpos or 1) + #line + 2;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 local k, v = line:match("(%S+): (.+)");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 if k and v then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 headers[k:lower()] = v;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 -- log("debug", "Header: "..k:lower().." = "..v);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 elseif #line == 0 then
697
8ddc85fa7602 core.httpserver: Rename request.responseheaders to the more logical request.headers
Matthew Wild <mwild1@gmail.com>
parents: 634
diff changeset
152 request.headers = headers;
634
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 break;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155 log("debug", "Unhandled header line: "..line);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 if not expectbody(request) then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 call_callback(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 return;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 -- Reached the end of the headers
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165 request.state = "body";
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 if #data > startpos then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 return request_reader(request, data:sub(startpos, -1));
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 elseif request.state == "request" then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 log("debug", "Reading request line...")
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 local method, path, http, linelen = data:match("^(%S+) (%S+) HTTP/(%S+)\r\n()", startpos);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 if not method then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 return call_callback(request, "invalid-status-line");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 request.method, request.path, request.httpversion = method, path, http;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 request.url = url_parse(request.path);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler.serverport());
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 if request.onlystatus then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 if not call_callback(request) then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 return;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 request.state = "headers";
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 if #data > linelen then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 return request_reader(request, data:sub(linelen, -1));
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 -- The default handler for requests
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 default_handler = function (method, body, request)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler.serverport());
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 return { status = "404 Not Found",
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 headers = { ["Content-Type"] = "text/html" },
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 body = "<html><head><title>Page Not Found</title></head><body>Not here :(</body></html>" };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 function new_request(handler)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 return { handler = handler, conn = handler.socket,
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 write = handler.write, state = "request",
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 server = http_servers[handler.serverport()],
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 send = send_response,
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 destroy = destroy_request,
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 id = tostring{}:match("%x+$")
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 function destroy_request(request)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216 log("debug", "Destroying request %s", request.id);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 listener = listener or connlisteners_get("httpserver");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 if not request.destroyed then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 request.destroyed = true;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 if request.on_destroy then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221 log("debug", "Request has destroy callback");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 request.on_destroy(request);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 else
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 log("debug", "Request has no destroy callback");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 request.handler.close()
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 if request.conn then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 listener.disconnect(request.conn, "closed");
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233 function new(params)
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 local http_server = http_servers[params.port];
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 if not http_server then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 http_server = { handlers = {} };
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 http_servers[params.port] = http_server;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 -- We weren't already listening on this port, so start now
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 connlisteners_start("httpserver", params);
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 if params.base then
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 http_server.handlers[params.base] = params.handler;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 end
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 _M.request_reader = request_reader;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 _M.send_response = send_response;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 _M.urlencode = urlencode;
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249
1af93ea23f96 Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 return _M;