Software /
code /
prosody
Annotate
net/http/server.lua @ 5222:61c47d26481d
net.http.parser: Fix syntax error introduced in c5edb08fc7cb.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 03 Dec 2012 10:01:06 +0500 |
parent | 4788:e14f5a156571 |
child | 5300:fcb1be0b4a5c |
rev | line source |
---|---|
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 local parser_new = require "net.http.parser".new; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 local events = require "util.events".new(); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 local addserver = require "net.server".addserver; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 local log = require "util.logger".init("http.server"); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 local os_date = os.date; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 local pairs = pairs; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 local s_upper = string.upper; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 local setmetatable = setmetatable; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local xpcall = xpcall; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 local debug = debug; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local tostring = tostring; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 local codes = require "net.http.codes"; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 local _M = {}; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 local sessions = {}; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 local listener = {}; |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
20 local hosts = {}; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
21 local default_host; |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 |
4668
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
23 local function is_wildcard_event(event) |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
24 return event:sub(-2, -1) == "/*"; |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
25 end |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
26 local function is_wildcard_match(wildcard_event, event) |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
27 return wildcard_event:sub(1, -2) == event:sub(1, #wildcard_event-1); |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
28 end |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
29 |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
30 local event_map = events._event_map; |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
31 setmetatable(events._handlers, { |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
32 __index = function (handlers, curr_event) |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
33 if is_wildcard_event(curr_event) then return; end -- Wildcard events cannot be fired |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
34 -- Find all handlers that could match this event, sort them |
4689
e8c357259993
net.http.server: Small fix to comment
Matthew Wild <mwild1@gmail.com>
parents:
4688
diff
changeset
|
35 -- and then put the array into handlers[curr_event] (and return it) |
4668
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
36 local matching_handlers_set = {}; |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
37 local handlers_array = {}; |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
38 for event, handlers_set in pairs(event_map) do |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
39 if event == curr_event or |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
40 is_wildcard_event(event) and is_wildcard_match(event, curr_event) then |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
41 for handler, priority in pairs(handlers_set) do |
4704
d6c4e58333cf
net.http.server: Lower score of wildcard handlers to ensure specific handlers beat them
Matthew Wild <mwild1@gmail.com>
parents:
4694
diff
changeset
|
42 matching_handlers_set[handler] = { (select(2, event:gsub("/", "%1"))), is_wildcard_event(event) and 0 or 1, priority }; |
4668
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
43 table.insert(handlers_array, handler); |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
44 end |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
45 end |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
46 end |
4726
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
47 if #handlers_array > 0 then |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
48 table.sort(handlers_array, function(b, a) |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
49 local a_score, b_score = matching_handlers_set[a], matching_handlers_set[b]; |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
50 for i = 1, #a_score do |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
51 if a_score[i] ~= b_score[i] then -- If equal, compare next score value |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
52 return a_score[i] < b_score[i]; |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
53 end |
4668
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
54 end |
4726
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
55 return false; |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
56 end); |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
57 else |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
58 handlers_array = false; |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
59 end |
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
60 rawset(handlers, curr_event, handlers_array); |
4668
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
61 return handlers_array; |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
62 end; |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
63 __newindex = function (handlers, curr_event, handlers_array) |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
64 if handlers_array == nil |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
65 and is_wildcard_event(curr_event) then |
4735
474166c08319
net.http.server: Improve comment
Matthew Wild <mwild1@gmail.com>
parents:
4727
diff
changeset
|
66 -- Invalidate the indexes of all matching events |
4668
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
67 for event in pairs(handlers) do |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
68 if is_wildcard_match(curr_event, event) then |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
69 handlers[event] = nil; |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
70 end |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
71 end |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
72 end |
4726
917a5ffb73f1
net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents:
4715
diff
changeset
|
73 rawset(handlers, curr_event, handlers_array); |
4668
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
74 end; |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
75 }); |
cce0c739b0d7
net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents:
4659
diff
changeset
|
76 |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 local handle_request; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 local _1, _2, _3; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 local function _handle_request() return handle_request(_1, _2, _3); end |
4710
54ca6511e699
net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents:
4709
diff
changeset
|
80 |
54ca6511e699
net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents:
4709
diff
changeset
|
81 local last_err; |
54ca6511e699
net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents:
4709
diff
changeset
|
82 local function _traceback_handler(err) last_err = err; log("error", "Traceback[http]: %s: %s", tostring(err), debug.traceback()); end |
54ca6511e699
net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents:
4709
diff
changeset
|
83 events.add_handler("http-error", function (error) |
54ca6511e699
net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents:
4709
diff
changeset
|
84 return "Error processing request: "..codes[error.code]..". Check your error log for more information."; |
54ca6511e699
net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents:
4709
diff
changeset
|
85 end, -1); |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 function listener.onconnect(conn) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 local secure = conn:ssl() and true or nil; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 local pending = {}; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 local waiting = false; |
4771
31a40da59308
net.http.server: Remove unused imports and variables (fixes traceback due to removed net.httpserver)
Matthew Wild <mwild1@gmail.com>
parents:
4740
diff
changeset
|
91 local function process_next() |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 --if waiting then log("debug", "can't process_next, waiting"); return; end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 if sessions[conn] and #pending > 0 then |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 local request = t_remove(pending); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 --log("debug", "process_next: %s", request.path); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 waiting = true; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 --handle_request(conn, request, process_next); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 _1, _2, _3 = conn, request, process_next; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 if not xpcall(_handle_request, _traceback_handler) then |
4710
54ca6511e699
net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents:
4709
diff
changeset
|
100 conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = last_err })); |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 conn:close(); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 else |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 --log("debug", "ready for more"); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 waiting = false; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 local function success_cb(request) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 --log("debug", "success_cb: %s", request.path); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 request.secure = secure; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 t_insert(pending, request); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 if not waiting then |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 process_next(); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 local function error_cb(err) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 log("debug", "error_cb: %s", err or "<nil>"); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 -- FIXME don't close immediately, wait until we process current stuff |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 -- FIXME if err, send off a bad-request response |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 sessions[conn] = nil; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 conn:close(); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 sessions[conn] = parser_new(success_cb, error_cb); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 function listener.ondisconnect(conn) |
4691
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
127 local open_response = conn._http_open_response; |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
128 if open_response and open_response.on_destroy then |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
129 open_response.finished = true; |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
130 open_response:on_destroy(); |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
131 end |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 sessions[conn] = nil; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 function listener.onincoming(conn, data) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 sessions[conn]:feed(data); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 local headerfix = setmetatable({}, { |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 __index = function(t, k) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 local v = "\r\n"..k:gsub("_", "-"):gsub("%f[%w].", s_upper)..": "; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 t[k] = v; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 return v; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 }); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 function _M.hijack_response(response, listener) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 error("TODO"); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 function handle_request(conn, request, finish_cb) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 --log("debug", "handler: %s", request.path); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 local headers = {}; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 for k,v in pairs(request.headers) do headers[k:gsub("-", "_")] = v; end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 request.headers = headers; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 request.conn = conn; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 local date_header = os_date('!%a, %d %b %Y %H:%M:%S GMT'); -- FIXME use |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 local conn_header = request.headers.connection; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 local keep_alive = conn_header == "Keep-Alive" or (request.httpversion == "1.1" and conn_header ~= "close"); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 local response = { |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 request = request; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 status_code = 200; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 headers = { date = date_header, connection = (keep_alive and "Keep-Alive" or "close") }; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 conn = conn; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 send = _M.send_response; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 finish_cb = finish_cb; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 }; |
4691
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
169 conn._http_open_response = response; |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
171 local host = (request.headers.host or ""):match("[^:]+"); |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
172 |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
173 -- Some sanity checking |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
174 local err_code, err; |
4740
bd9c8bc8036f
net.http.server: Try default_host if client sent no host anywhere, otherwise... fail. It's 2012.
Matthew Wild <mwild1@gmail.com>
parents:
4739
diff
changeset
|
175 if not request.path then |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
176 err_code, err = 400, "Invalid path"; |
4739
f1d32a0dc057
net.http.server: Fix traceback on missing host header (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents:
4736
diff
changeset
|
177 elseif not hosts[host] then |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
178 if hosts[default_host] then |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
179 host = default_host; |
4740
bd9c8bc8036f
net.http.server: Try default_host if client sent no host anywhere, otherwise... fail. It's 2012.
Matthew Wild <mwild1@gmail.com>
parents:
4739
diff
changeset
|
180 elseif host then |
bd9c8bc8036f
net.http.server: Try default_host if client sent no host anywhere, otherwise... fail. It's 2012.
Matthew Wild <mwild1@gmail.com>
parents:
4739
diff
changeset
|
181 err_code, err = 404, "Unknown host: "..host; |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
182 else |
4740
bd9c8bc8036f
net.http.server: Try default_host if client sent no host anywhere, otherwise... fail. It's 2012.
Matthew Wild <mwild1@gmail.com>
parents:
4739
diff
changeset
|
183 err_code, err = 400, "Missing or invalid 'Host' header"; |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
184 end |
4713
9c15fa5192d3
net.http.server: Fire http-error 400 if request fails sanity checks
Matthew Wild <mwild1@gmail.com>
parents:
4710
diff
changeset
|
185 end |
9c15fa5192d3
net.http.server: Fire http-error 400 if request fails sanity checks
Matthew Wild <mwild1@gmail.com>
parents:
4710
diff
changeset
|
186 |
9c15fa5192d3
net.http.server: Fire http-error 400 if request fails sanity checks
Matthew Wild <mwild1@gmail.com>
parents:
4710
diff
changeset
|
187 if err then |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
188 response.status_code = err_code; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
189 response:send(events.fire_event("http-error", { code = err_code, message = err })); |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
190 return; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
191 end |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
192 |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
193 local event = request.method.." "..host..request.path:match("[^?]*"); |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
194 local payload = { request = request, response = response }; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
195 --log("debug", "Firing event: %s", event); |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
196 local result = events.fire_event(event, payload); |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
197 if result ~= nil then |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
198 if result ~= true then |
4788
e14f5a156571
net.http.server: Code cleanup/adjustment now that send_response() accepts response.body
Matthew Wild <mwild1@gmail.com>
parents:
4787
diff
changeset
|
199 local body; |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
200 local result_type = type(result); |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
201 if result_type == "number" then |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
202 response.status_code = result; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
203 if result >= 400 then |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
204 body = events.fire_event("http-error", { code = result }); |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 end |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
206 elseif result_type == "string" then |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
207 body = result; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
208 elseif result_type == "table" then |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
209 for k, v in pairs(result) do |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
210 response[k] = v; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
211 end |
4683
c1374e083c97
net.http.server: Handle results returned by handlers, and send as a response. Also removes explicit firing of '*', which can now be done via wildcard events.
Matthew Wild <mwild1@gmail.com>
parents:
4682
diff
changeset
|
212 end |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
213 response:send(body); |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 end |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
215 return; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
216 end |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
218 -- if handler not called, return 404 |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
219 response.status_code = 404; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
220 response:send(events.fire_event("http-error", { code = 404 })); |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 function _M.send_response(response, body) |
4691
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
223 if response.finished then return; end |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
224 response.finished = true; |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
225 response.conn._http_open_response = nil; |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
226 |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 local headers = response.headers; |
4787
1d753fecf95b
net.http.server: Use response.body if it exists and body is not specified to send_response
Matthew Wild <mwild1@gmail.com>
parents:
4771
diff
changeset
|
229 body = body or response.body or ""; |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 headers.content_length = #body; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
231 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
232 local output = { status_line }; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
233 for k,v in pairs(headers) do |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
234 t_insert(output, headerfix[k]..v); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
235 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
236 t_insert(output, "\r\n\r\n"); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
237 t_insert(output, body); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
238 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
239 response.conn:write(t_concat(output)); |
4691
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
240 if response.on_destroy then |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
241 response:on_destroy(); |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
242 response.on_destroy = nil; |
a164fc7057ae
net.http.server: Support for on_destroy callback on response objects, and a 'finished' flag to say when they are destroyed (responded to or connection closed)
Matthew Wild <mwild1@gmail.com>
parents:
4689
diff
changeset
|
243 end |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
244 if headers.connection == "Keep-Alive" then |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
245 response:finish_cb(); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
246 else |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
247 response.conn:close(); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
248 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
249 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
250 function _M.add_handler(event, handler, priority) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
251 events.add_handler(event, handler, priority); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
252 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
253 function _M.remove_handler(event, handler) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
254 events.remove_handler(event, handler); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
255 end |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
256 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
257 function _M.listen_on(port, interface, ssl) |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
258 addserver(interface or "*", port, listener, "*a", ssl); |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
259 end |
4736
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
260 function _M.add_host(host) |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
261 hosts[host] = true; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
262 end |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
263 function _M.remove_host(host) |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
264 hosts[host] = nil; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
265 end |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
266 function _M.set_default_host(host) |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
267 default_host = host; |
3514338c59c3
net.http.server, mod_http: Support http_default_host config option to specify where to direct requests for unknown HTTP vhosts
Matthew Wild <mwild1@gmail.com>
parents:
4735
diff
changeset
|
268 end |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
269 |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
270 _M.listener = listener; |
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
271 _M.codes = codes; |
4706
845393c76d17
net.http.server: Expose events object (for debug purposes)
Matthew Wild <mwild1@gmail.com>
parents:
4704
diff
changeset
|
272 _M._events = events; |
4631
fc5d3b053454
net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
273 return _M; |