Mon, 04 Jan 2016 17:03:19 +0000
clients: Include request in the new-client event
0
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 | local parser_new = require "net.http.parser".new; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | local events = require "util.events".new(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 | local addserver = require "net.server".addserver; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 | local log = require "util.logger".init("http.server"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 | local os_date = os.date; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 | local pairs = pairs; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 | local s_upper = string.upper; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | local setmetatable = setmetatable; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 | local xpcall = xpcall; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 | local traceback = debug.traceback; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 | local tostring = tostring; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 | local codes = require "net.http.codes"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 | local _M = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 | local sessions = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 | local listener = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 | local hosts = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 | local default_host; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 | local function is_wildcard_event(event) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 | return event:sub(-2, -1) == "/*"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 | local function is_wildcard_match(wildcard_event, event) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 | return wildcard_event:sub(1, -2) == event:sub(1, #wildcard_event-1); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 | local recent_wildcard_events, max_cached_wildcard_events = {}, 10000; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 | local event_map = events._event_map; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 | setmetatable(events._handlers, { |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 | -- Called when firing an event that doesn't exist (but may match a wildcard handler) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 | __index = function (handlers, curr_event) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 | if is_wildcard_event(curr_event) then return; end -- Wildcard events cannot be fired |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 | -- Find all handlers that could match this event, sort them |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 | -- and then put the array into handlers[curr_event] (and return it) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 | local matching_handlers_set = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 | local handlers_array = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 | for event, handlers_set in pairs(event_map) do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | if event == curr_event or |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | is_wildcard_event(event) and is_wildcard_match(event, curr_event) then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 | for handler, priority in pairs(handlers_set) do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 | matching_handlers_set[handler] = { (select(2, event:gsub("/", "%1"))), is_wildcard_event(event) and 0 or 1, priority }; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | table.insert(handlers_array, handler); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 | if #handlers_array > 0 then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 | table.sort(handlers_array, function(b, a) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 | local a_score, b_score = matching_handlers_set[a], matching_handlers_set[b]; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 | for i = 1, #a_score do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 | if a_score[i] ~= b_score[i] then -- If equal, compare next score value |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 | return a_score[i] < b_score[i]; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
56 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
57 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
58 | return false; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
59 | end); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
60 | else |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
61 | handlers_array = false; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
62 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
63 | rawset(handlers, curr_event, handlers_array); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
64 | if not event_map[curr_event] then -- Only wildcard handlers match, if any |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
65 | table.insert(recent_wildcard_events, curr_event); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
66 | if #recent_wildcard_events > max_cached_wildcard_events then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
67 | rawset(handlers, table.remove(recent_wildcard_events, 1), nil); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
68 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
69 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
70 | return handlers_array; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
71 | end; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
72 | __newindex = function (handlers, curr_event, handlers_array) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
73 | if handlers_array == nil |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
74 | and is_wildcard_event(curr_event) then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
75 | -- Invalidate the indexes of all matching events |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
76 | for event in pairs(handlers) do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
77 | if is_wildcard_match(curr_event, event) then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
78 | handlers[event] = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
79 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
80 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
81 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
82 | rawset(handlers, curr_event, handlers_array); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
83 | end; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
84 | }); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
85 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
86 | local handle_request; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
87 | local _1, _2, _3; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
88 | local function _handle_request() return handle_request(_1, _2, _3); end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
89 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
90 | local last_err; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
91 | local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
92 | events.add_handler("http-error", function (error) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
93 | return "Error processing request: "..codes[error.code]..". Check your error log for more information."; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
94 | end, -1); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
95 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
96 | function listener.onconnect(conn) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
97 | local secure = conn:ssl() and true or nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
98 | local pending = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
99 | local waiting = false; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
100 | local function process_next() |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
101 | if waiting then return; end -- log("debug", "can't process_next, waiting"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
102 | waiting = true; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
103 | while sessions[conn] and #pending > 0 do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
104 | local request = t_remove(pending); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
105 | --log("debug", "process_next: %s", request.path); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
106 | --handle_request(conn, request, process_next); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
107 | _1, _2, _3 = conn, request, process_next; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
108 | if not xpcall(_handle_request, _traceback_handler) then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
109 | conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = last_err })); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
110 | conn:close(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
111 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
112 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
113 | --log("debug", "ready for more"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
114 | waiting = false; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
115 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
116 | local function success_cb(request) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
117 | --log("debug", "success_cb: %s", request.path); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
118 | if waiting then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
119 | log("error", "http connection handler is not reentrant: %s", request.path); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
120 | assert(false, "http connection handler is not reentrant"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
121 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
122 | request.secure = secure; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
123 | t_insert(pending, request); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
124 | process_next(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
125 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
126 | local function error_cb(err) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
127 | log("debug", "error_cb: %s", err or "<nil>"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
128 | -- FIXME don't close immediately, wait until we process current stuff |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
129 | -- FIXME if err, send off a bad-request response |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
130 | sessions[conn] = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
131 | conn:close(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
132 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
133 | sessions[conn] = parser_new(success_cb, error_cb); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
134 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
135 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
136 | function listener.ondisconnect(conn) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
137 | local open_response = conn._http_open_response; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
138 | if open_response and open_response.on_destroy then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
139 | open_response.finished = true; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
140 | open_response:on_destroy(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
141 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
142 | sessions[conn] = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
143 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
144 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
145 | function listener.ondetach(conn) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
146 | sessions[conn] = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
147 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
148 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
149 | function listener.onincoming(conn, data) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
150 | sessions[conn]:feed(data); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
151 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
152 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
153 | local headerfix = setmetatable({}, { |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
154 | __index = function(t, k) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
155 | local v = "\r\n"..k:gsub("_", "-"):gsub("%f[%w].", s_upper)..": "; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
156 | t[k] = v; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
157 | return v; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
158 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
159 | }); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
160 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
161 | function _M.hijack_response(response, listener) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
162 | error("TODO"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
163 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
164 | function handle_request(conn, request, finish_cb) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
165 | --log("debug", "handler: %s", request.path); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
166 | local headers = {}; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
167 | for k,v in pairs(request.headers) do headers[k:gsub("-", "_")] = v; end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
168 | request.headers = headers; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
169 | request.conn = conn; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
170 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
171 | local date_header = os_date('!%a, %d %b %Y %H:%M:%S GMT'); -- FIXME use |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
172 | local conn_header = request.headers.connection; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
173 | conn_header = conn_header and ","..conn_header:gsub("[ \t]", ""):lower().."," or "" |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
174 | local httpversion = request.httpversion |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
175 | local persistent = conn_header:find(",keep-alive,", 1, true) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
176 | or (httpversion == "1.1" and not conn_header:find(",close,", 1, true)); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
177 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
178 | local response_conn_header; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
179 | if persistent then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
180 | response_conn_header = "Keep-Alive"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
181 | else |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
182 | response_conn_header = httpversion == "1.1" and "close" or nil |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
183 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
184 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
185 | local response = { |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
186 | request = request; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
187 | status_code = 200; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
188 | headers = { date = date_header, connection = response_conn_header }; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
189 | persistent = persistent; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
190 | conn = conn; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
191 | send = _M.send_response; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
192 | done = _M.finish_response; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
193 | finish_cb = finish_cb; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
194 | }; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
195 | conn._http_open_response = response; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
196 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
197 | local host = (request.headers.host or ""):match("[^:]+"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
198 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
199 | -- Some sanity checking |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
200 | local err_code, err; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
201 | if not request.path then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
202 | err_code, err = 400, "Invalid path"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
203 | elseif not hosts[host] then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
204 | if hosts[default_host] then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
205 | host = default_host; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
206 | elseif host then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
207 | err_code, err = 404, "Unknown host: "..host; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
208 | else |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
209 | err_code, err = 400, "Missing or invalid 'Host' header"; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
210 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
211 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
212 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
213 | if err then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
214 | response.status_code = err_code; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
215 | response:send(events.fire_event("http-error", { code = err_code, message = err })); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
216 | return; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
217 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
218 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
219 | local event = request.method.." "..host..request.path:match("[^?]*"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
220 | local payload = { request = request, response = response }; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
221 | --log("debug", "Firing event: %s", event); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
222 | local result = events.fire_event(event, payload); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
223 | if result ~= nil then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
224 | if result ~= true then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
225 | local body; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
226 | local result_type = type(result); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
227 | if result_type == "number" then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
228 | response.status_code = result; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
229 | if result >= 400 then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
230 | body = events.fire_event("http-error", { code = result }); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
231 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
232 | elseif result_type == "string" then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
233 | body = result; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
234 | elseif result_type == "table" then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
235 | for k, v in pairs(result) do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
236 | if k ~= "headers" then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
237 | response[k] = v; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
238 | else |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
239 | for header_name, header_value in pairs(v) do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
240 | response.headers[header_name] = header_value; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
241 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
242 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
243 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
244 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
245 | response:send(body); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
246 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
247 | return; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
248 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
249 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
250 | -- if handler not called, return 404 |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
251 | response.status_code = 404; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
252 | response:send(events.fire_event("http-error", { code = 404 })); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
253 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
254 | local function prepare_header(response) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
255 | local status_line = "HTTP/"..response.request.httpversion.." "..(response.status or codes[response.status_code]); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
256 | local headers = response.headers; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
257 | local output = { status_line }; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
258 | for k,v in pairs(headers) do |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
259 | t_insert(output, headerfix[k]..v); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
260 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
261 | t_insert(output, "\r\n\r\n"); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
262 | return output; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
263 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
264 | _M.prepare_header = prepare_header; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
265 | function _M.send_response(response, body) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
266 | if response.finished then return; end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
267 | body = body or response.body or ""; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
268 | response.headers.content_length = #body; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
269 | local output = prepare_header(response); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
270 | t_insert(output, body); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
271 | response.conn:write(t_concat(output)); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
272 | response:done(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
273 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
274 | function _M.finish_response(response) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
275 | if response.finished then return; end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
276 | response.finished = true; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
277 | response.conn._http_open_response = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
278 | if response.on_destroy then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
279 | response:on_destroy(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
280 | response.on_destroy = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
281 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
282 | if response.persistent then |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
283 | response:finish_cb(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
284 | else |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
285 | response.conn:close(); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
286 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
287 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
288 | function _M.add_handler(event, handler, priority) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
289 | events.add_handler(event, handler, priority); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
290 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
291 | function _M.remove_handler(event, handler) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
292 | events.remove_handler(event, handler); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
293 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
294 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
295 | function _M.listen_on(port, interface, ssl) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
296 | addserver(interface or "*", port, listener, "*a", ssl); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
297 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
298 | function _M.add_host(host) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
299 | hosts[host] = true; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
300 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
301 | function _M.remove_host(host) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
302 | hosts[host] = nil; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
303 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
304 | function _M.set_default_host(host) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
305 | default_host = host; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
306 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
307 | function _M.fire_event(event, ...) |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
308 | return events.fire_event(event, ...); |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
309 | end |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
310 | |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
311 | _M.listener = listener; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
312 | _M.codes = codes; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
313 | _M._events = events; |
d363a6692a10
Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
314 | return _M; |