Software /
code /
prosody
Annotate
net/httpserver.lua @ 4446:d1d4d8c8a2a9
net.httpserver: Removed unused import.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 09 Dec 2011 11:38:35 +0500 |
parent | 4228:1c2daf61c348 |
child | 4658:de04897cb8fe |
rev | line source |
---|---|
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1435
diff
changeset
|
1 -- Prosody IM |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2877
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
2877
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
1522
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1435
diff
changeset
|
4 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1435
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1435
diff
changeset
|
6 -- COPYING file in the source package for more information. |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1435
diff
changeset
|
7 -- |
569d58d21612
Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents:
1435
diff
changeset
|
8 |
634
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 url_parse = require "socket.url".parse; |
3497
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
11 local httpstream_new = require "util.httpstream".new; |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 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
|
14 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
|
15 local listener; |
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 t_insert, t_concat = table.insert, table.concat; |
1536
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
18 local tonumber, tostring, pairs, ipairs, type = tonumber, tostring, pairs, ipairs, type; |
4161
c299726d2b4e
net.httpserver: Catch errors thrown in HTTP handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
4143
diff
changeset
|
19 local xpcall = xpcall; |
c299726d2b4e
net.httpserver: Catch errors thrown in HTTP handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
4143
diff
changeset
|
20 local debug_traceback = debug.traceback; |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
4005
420e57319f89
net.httpserver: Changed an unnecessary global access.
Waqas Hussain <waqas20@gmail.com>
parents:
4003
diff
changeset
|
22 local urlencode = function (s) return s and (s:gsub("%W", function (c) return ("%%%02x"):format(c:byte()); end)); end |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 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
|
25 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 local http_servers = {}; |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 module "httpserver" |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 local default_handler; |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 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
|
33 -- Write status line |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 local resp; |
2836
dfb5fa77d437
net.httpserver: Make it possible to return responses with no body
Matthew Wild <mwild1@gmail.com>
parents:
2835
diff
changeset
|
35 if response.body or response.headers then |
dfb5fa77d437
net.httpserver: Make it possible to return responses with no body
Matthew Wild <mwild1@gmail.com>
parents:
2835
diff
changeset
|
36 local body = response.body and tostring(response.body); |
1052
a3429542631d
net.httpserver: Don't log the response body (can be binary data...)
Matthew Wild <mwild1@gmail.com>
parents:
958
diff
changeset
|
37 log("debug", "Sending response to %s", request.id); |
2399
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
38 resp = { "HTTP/1.0 "..(response.status or "200 OK").."\r\n" }; |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 local h = response.headers; |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 if h then |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 for k, v in pairs(h) do |
2399
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
42 t_insert(resp, k..": "..v.."\r\n"); |
634
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 end |
2836
dfb5fa77d437
net.httpserver: Make it possible to return responses with no body
Matthew Wild <mwild1@gmail.com>
parents:
2835
diff
changeset
|
45 if body and not (h and h["Content-Length"]) then |
2399
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
46 t_insert(resp, "Content-Length: "..#body.."\r\n"); |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 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
|
49 |
2836
dfb5fa77d437
net.httpserver: Make it possible to return responses with no body
Matthew Wild <mwild1@gmail.com>
parents:
2835
diff
changeset
|
50 if body and request.method ~= "HEAD" then |
1632
4a70cbb0fad7
net.httpserver: Allow response.body to be a non-string
Matthew Wild <mwild1@gmail.com>
parents:
1608
diff
changeset
|
51 t_insert(resp, body); |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 end |
2399
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
53 request.write(t_concat(resp)); |
634
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) |
1661
33b1aee4b77f
net.httpserver: Don't log response bodies!
Matthew Wild <mwild1@gmail.com>
parents:
1632
diff
changeset
|
56 log("debug", "Sending 200 response to %s", request.id or "<none>"); |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 |
2399
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
58 local resp = "HTTP/1.0 200 OK\r\n" |
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
59 .. "Connection: close\r\n" |
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
60 .. "Content-Type: text/html\r\n" |
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
61 .. "Content-Length: "..#response.."\r\n" |
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
62 .. "\r\n" |
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
63 .. response; |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 |
2399
0325f241a26c
net.httpserver: Optimized response serialization.
Waqas Hussain <waqas20@gmail.com>
parents:
2371
diff
changeset
|
65 request.write(resp); |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 end |
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 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 if callback then |
4161
c299726d2b4e
net.httpserver: Catch errors thrown in HTTP handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
4143
diff
changeset
|
86 local _callback = callback; |
4164
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
87 function callback(method, body, request) |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
88 local ok, result = xpcall(function() return _callback(method, body, request) end, debug_traceback); |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
89 if ok then return result; end |
4161
c299726d2b4e
net.httpserver: Catch errors thrown in HTTP handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
4143
diff
changeset
|
90 log("error", "Error in HTTP server handler: %s", result); |
4164
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
91 -- TODO: When we support pipelining, request.destroyed |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
92 -- won't be the right flag - we just want to see if there |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
93 -- has been a response to this request yet. |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
94 if not request.destroyed then |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
95 return { |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
96 status = "500 Internal Server Error"; |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
97 headers = { ["Content-Type"] = "text/plain" }; |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
98 body = "There was an error processing your request. See the error log for more details."; |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
99 }; |
843240294be1
net.httpserver: Fix HTTP after commit c299726d2b4e and add a 500 error response if a request handler fails to make a response to the client
Matthew Wild <mwild1@gmail.com>
parents:
4161
diff
changeset
|
100 end |
4161
c299726d2b4e
net.httpserver: Catch errors thrown in HTTP handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
4143
diff
changeset
|
101 end |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 if err then |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 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
|
104 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
|
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 return; |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 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
|
111 if response then |
958
172fb9a73017
net.httpserver: Don't log that a request has been left open if it is destroyed
Matthew Wild <mwild1@gmail.com>
parents:
697
diff
changeset
|
112 if response == true and not request.destroyed then |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 -- Keep connection open, we will reply later |
1608
fb53fe17af36
net.httpserver: Reduce log level of 'request left open' message
Matthew Wild <mwild1@gmail.com>
parents:
1550
diff
changeset
|
114 log("debug", "Request %s left open, on_destroy is %s", request.id, tostring(request.on_destroy)); |
1053
c04b40a0740b
net.httpserver: Fix traceback when sending response to a destroyed request
Matthew Wild <mwild1@gmail.com>
parents:
1052
diff
changeset
|
115 elseif response ~= true then |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 -- Assume response |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 send_response(request, response); |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 destroy_request(request); |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 else |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 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
|
122 -- No response, close connection |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 destroy_request(request); |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 end |
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 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 local function request_reader(request, data, startpos) |
3497
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
129 if not request.parser then |
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
130 local function success_cb(r) |
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
131 for k,v in pairs(r) do request[k] = v; end |
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
132 request.url = url_parse(request.path); |
4003
b840c4c77b92
net.httpserver: Decode percent-encoded characters in URL path.
Waqas Hussain <waqas20@gmail.com>
parents:
3757
diff
changeset
|
133 request.url.path = request.url.path and request.url.path:gsub("%%(%x%x)", function(x) return x.char(tonumber(x, 16)) end); |
3497
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
134 request.body = { request.body }; |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 call_callback(request); |
2837
a17e73ab5f4c
net.httpserver: More robust handling of headers split across multiple packets
Matthew Wild <mwild1@gmail.com>
parents:
2836
diff
changeset
|
136 end |
3497
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
137 local function error_cb(r) |
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
138 call_callback(request, r or "connection-closed"); |
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
139 destroy_request(request); |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 end |
3497
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
141 request.parser = httpstream_new(success_cb, error_cb); |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 end |
3497
e9159b325e33
net.httpserver: Removed old HTTP parser, and updated to use util.httpstream.
Waqas Hussain <waqas20@gmail.com>
parents:
3473
diff
changeset
|
143 request.parser:feed(data); |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 -- 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
|
147 default_handler = function (method, body, request) |
2371
7ddb8ce0cd0b
net.httpserver: Fix usage of old connection API syntax
Matthew Wild <mwild1@gmail.com>
parents:
2360
diff
changeset
|
148 log("debug", method.." request for "..tostring(request.path) .. " on port "..request.handler:serverport()); |
2570
9e69aecad25e
net.httpserver: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2483
diff
changeset
|
149 return { status = "404 Not Found", |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 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
|
151 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
|
152 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 function new_request(handler) |
2571
8a16c5e92bcb
net.httpserver: Make request.conn be the server.lua conn instead of the underlying socket (more useful this way)
Matthew Wild <mwild1@gmail.com>
parents:
2570
diff
changeset
|
156 return { handler = handler, conn = handler, |
2570
9e69aecad25e
net.httpserver: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents:
2483
diff
changeset
|
157 write = function (...) return handler:write(...); end, state = "request", |
2275
dd344b94d088
net.httpserver: Fix to work with new server API
Matthew Wild <mwild1@gmail.com>
parents:
2274
diff
changeset
|
158 server = http_servers[handler:serverport()], |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 send = send_response, |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 destroy = destroy_request, |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 id = tostring{}:match("%x+$") |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 }; |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 function destroy_request(request) |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 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
|
167 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
|
168 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
|
169 request.destroyed = true; |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 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
|
171 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
|
172 request.on_destroy(request); |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 else |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 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
|
175 end |
2371
7ddb8ce0cd0b
net.httpserver: Fix usage of old connection API syntax
Matthew Wild <mwild1@gmail.com>
parents:
2360
diff
changeset
|
176 request.handler:close() |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 if request.conn then |
2571
8a16c5e92bcb
net.httpserver: Make request.conn be the server.lua conn instead of the underlying socket (more useful this way)
Matthew Wild <mwild1@gmail.com>
parents:
2570
diff
changeset
|
178 listener.ondisconnect(request.conn, "closed"); |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 function new(params) |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 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
|
185 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
|
186 http_server = { handlers = {} }; |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 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
|
188 -- 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
|
189 connlisteners_start("httpserver", params); |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 end |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 if params.base then |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 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
|
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 |
1666
d1243b321c45
net.httpserver: Allow overriding default request handler
Matthew Wild <mwild1@gmail.com>
parents:
1661
diff
changeset
|
196 function set_default_handler(handler) |
d1243b321c45
net.httpserver: Allow overriding default request handler
Matthew Wild <mwild1@gmail.com>
parents:
1661
diff
changeset
|
197 default_handler = handler; |
d1243b321c45
net.httpserver: Allow overriding default request handler
Matthew Wild <mwild1@gmail.com>
parents:
1661
diff
changeset
|
198 end |
d1243b321c45
net.httpserver: Allow overriding default request handler
Matthew Wild <mwild1@gmail.com>
parents:
1661
diff
changeset
|
199 |
1868
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
200 function new_from_config(ports, handle_request, default_options) |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
201 if type(handle_request) == "string" then -- COMPAT with old plugins |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
202 log("warn", "Old syntax of httpserver.new_from_config being used to register %s", handle_request); |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
203 handle_request, default_options = default_options, { base = handle_request }; |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
204 end |
3757
e8be634e217a
net.httpserver: Default 'ports' = {5280}
Matthew Wild <mwild1@gmail.com>
parents:
3540
diff
changeset
|
205 ports = ports or {5280}; |
1536
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
206 for _, options in ipairs(ports) do |
1868
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
207 local port = default_options.port or 5280; |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
208 local base = default_options.base; |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
209 local ssl = default_options.ssl or false; |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
210 local interface = default_options.interface; |
1536
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
211 if type(options) == "number" then |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
212 port = options; |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
213 elseif type(options) == "table" then |
1868
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
214 port = options.port or port; |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
215 base = options.path or base; |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
216 ssl = options.ssl or ssl; |
36e1238db2f2
net.httpserver: Allow modules registering to provide more than just a default path when using httpserver.new_from_config
Matthew Wild <mwild1@gmail.com>
parents:
1666
diff
changeset
|
217 interface = options.interface or interface; |
1536
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
218 elseif type(options) == "string" then |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
219 base = options; |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
220 end |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
221 |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
222 if ssl then |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
223 ssl.mode = "server"; |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
224 ssl.protocol = "sslv23"; |
2767
473627393d40
Disable SSLv2 by default, it's known to be insecure.
Paul Aurich <paul@darkrain42.org>
parents:
2761
diff
changeset
|
225 ssl.options = "no_sslv2"; |
1536
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
226 end |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
227 |
3540
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3497
diff
changeset
|
228 new{ port = port, interface = interface, |
bc139431830b
Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents:
3497
diff
changeset
|
229 base = base, handler = handle_request, |
2021
43b7c0980d23
net.httpserver: Pass on interface option from new_from_config() (thanks Chris)
Matthew Wild <mwild1@gmail.com>
parents:
1868
diff
changeset
|
230 ssl = ssl, type = (ssl and "ssl") or "tcp" }; |
1536
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
231 end |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
232 end |
8214458eacc8
net.httpserver: Add helper function to set up HTTP server according to given config options
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
233 |
634
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 _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
|
235 _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
|
236 _M.urlencode = urlencode; |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 |
1af93ea23f96
Adding initial net.httpserver (lots of work to do on it)
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 return _M; |