Annotate

net/http/server.lua @ 7489:d32406f27efd

net.http.server: Add response method for reading response body from a file handle
author Kim Alvefur <zash@zash.se>
date Mon, 11 Jul 2016 11:52:43 +0200
parent 7084:fdbe9ccac17d
child 7541:1d3f9da189b5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
5505
0b6a99e6c1b1 mod_c2s, mod_s2s, net.http, net.http.server: Improve tracebacks (omit traceback function), to make it clearer where an error occured
Matthew Wild <mwild1@gmail.com>
parents: 5504
diff changeset
12 local traceback = debug.traceback;
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local tostring = tostring;
6963
e3be91ccca05 net.http.server: Import util.cache
Matthew Wild <mwild1@gmail.com>
parents: 6950
diff changeset
14 local cache = require "util.cache";
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 local codes = require "net.http.codes";
7489
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
16 local blocksize = require "socket".BLOCKSIZE or 2048;
4631
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 _M = {};
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 local sessions = {};
7489
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
21 local incomplete = {};
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 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
23 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
24 local default_host;
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25
4668
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_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 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
28 end
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
29 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
30 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
31 end
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
32
6950
8ab809358922 net.http.server: Use new util.cache to remember wildcard event handlers
Matthew Wild <mwild1@gmail.com>
parents: 6608
diff changeset
33 local _handlers = events._handlers;
8ab809358922 net.http.server: Use new util.cache to remember wildcard event handlers
Matthew Wild <mwild1@gmail.com>
parents: 6608
diff changeset
34 local recent_wildcard_events = cache.new(10000, function (key, value)
8ab809358922 net.http.server: Use new util.cache to remember wildcard event handlers
Matthew Wild <mwild1@gmail.com>
parents: 6608
diff changeset
35 rawset(_handlers, key, nil);
8ab809358922 net.http.server: Use new util.cache to remember wildcard event handlers
Matthew Wild <mwild1@gmail.com>
parents: 6608
diff changeset
36 end);
5503
91052e59375c net.server.http: Ensure that event map cannot grow forever (limit to 10K wildcard-only entries)
Matthew Wild <mwild1@gmail.com>
parents: 5487
diff changeset
37
4668
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
38 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
39 setmetatable(events._handlers, {
5504
b760b5f0c2b0 net.server.http: Add a comment
Matthew Wild <mwild1@gmail.com>
parents: 5503
diff changeset
40 -- Called when firing an event that doesn't exist (but may match a wildcard handler)
4668
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
41 __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
42 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
43 -- 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
44 -- 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
45 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
46 local handlers_array = {};
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
47 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
48 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
49 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
50 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
51 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
52 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
53 end
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
54 end
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 end
4668
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
63 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
64 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
65 end);
917a5ffb73f1 net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents: 4715
diff changeset
66 else
917a5ffb73f1 net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents: 4715
diff changeset
67 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
68 end
917a5ffb73f1 net.http.server: Correctly cache results of handler indexing, and also cache failures
Matthew Wild <mwild1@gmail.com>
parents: 4715
diff changeset
69 rawset(handlers, curr_event, handlers_array);
5503
91052e59375c net.server.http: Ensure that event map cannot grow forever (limit to 10K wildcard-only entries)
Matthew Wild <mwild1@gmail.com>
parents: 5487
diff changeset
70 if not event_map[curr_event] then -- Only wildcard handlers match, if any
6950
8ab809358922 net.http.server: Use new util.cache to remember wildcard event handlers
Matthew Wild <mwild1@gmail.com>
parents: 6608
diff changeset
71 recent_wildcard_events:set(curr_event, true);
5503
91052e59375c net.server.http: Ensure that event map cannot grow forever (limit to 10K wildcard-only entries)
Matthew Wild <mwild1@gmail.com>
parents: 5487
diff changeset
72 end
4668
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
73 return handlers_array;
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 __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
76 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
77 and is_wildcard_event(curr_event) then
4735
474166c08319 net.http.server: Improve comment
Matthew Wild <mwild1@gmail.com>
parents: 4727
diff changeset
78 -- 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
79 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
80 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
81 handlers[event] = nil;
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
82 end
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
83 end
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
84 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
85 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
86 end;
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
87 });
cce0c739b0d7 net.http.server: Support for wildcard events (events that end with '/*')
Matthew Wild <mwild1@gmail.com>
parents: 4659
diff changeset
88
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 local handle_request;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 local _1, _2, _3;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 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
92
54ca6511e699 net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents: 4709
diff changeset
93 local last_err;
5505
0b6a99e6c1b1 mod_c2s, mod_s2s, net.http, net.http.server: Improve tracebacks (omit traceback function), to make it clearer where an error occured
Matthew Wild <mwild1@gmail.com>
parents: 5504
diff changeset
94 local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end
4710
54ca6511e699 net.http.server: Make error handling overrideable via 'http-error' event
Matthew Wild <mwild1@gmail.com>
parents: 4709
diff changeset
95 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
96 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
97 end, -1);
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 function listener.onconnect(conn)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 local secure = conn:ssl() and true or nil;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 local pending = {};
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 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
103 local function process_next()
6360
ffcd3992a40c net.http.server: Comment out a log message
Kim Alvefur <zash@zash.se>
parents: 5754
diff changeset
104 if waiting then return; end -- log("debug", "can't process_next, waiting");
5333
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
105 waiting = true;
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
106 while sessions[conn] and #pending > 0 do
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 local request = t_remove(pending);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 --log("debug", "process_next: %s", request.path);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 --handle_request(conn, request, process_next);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 _1, _2, _3 = conn, request, process_next;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 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
112 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
113 conn:close();
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
5333
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
116 --log("debug", "ready for more");
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
117 waiting = false;
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 local function success_cb(request)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 --log("debug", "success_cb: %s", request.path);
5333
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
121 if waiting then
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
122 log("error", "http connection handler is not reentrant: %s", request.path);
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
123 assert(false, "http connection handler is not reentrant");
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
124 end
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 request.secure = secure;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 t_insert(pending, request);
5333
760c345dc7a1 net.http.server: Ensure HTTP callbacks are never called recursively for pipelined requests.
Waqas Hussain <waqas20@gmail.com>
parents: 5300
diff changeset
127 process_next();
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 local function error_cb(err)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 log("debug", "error_cb: %s", err or "<nil>");
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 -- 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
132 -- 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
133 sessions[conn] = nil;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 conn:close();
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 sessions[conn] = parser_new(success_cb, error_cb);
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 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
140 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
141 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
142 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
143 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
144 end
7489
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
145 incomplete[conn] = nil;
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 sessions[conn] = nil;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148
6380
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 6360
diff changeset
149 function listener.ondetach(conn)
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 6360
diff changeset
150 sessions[conn] = nil;
7489
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
151 incomplete[conn] = nil;
6380
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 6360
diff changeset
152 end
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 6360
diff changeset
153
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
154 function listener.onincoming(conn, data)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
155 sessions[conn]:feed(data);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
156 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
157
7489
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
158 function listener.ondrain(conn)
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
159 local response = incomplete[conn];
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
160 if response and response._send_more then
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
161 response._send_more();
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
162 end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
163 end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
164
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
165 local headerfix = setmetatable({}, {
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
166 __index = function(t, k)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
167 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
168 t[k] = v;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
169 return v;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
170 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
171 });
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
172
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
173 function _M.hijack_response(response, listener)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
174 error("TODO");
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
175 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
176 function handle_request(conn, request, finish_cb)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
177 --log("debug", "handler: %s", request.path);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
178 local headers = {};
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
179 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
180 request.headers = headers;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
181 request.conn = conn;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
182
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
183 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
184 local conn_header = request.headers.connection;
5300
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
185 conn_header = conn_header and ","..conn_header:gsub("[ \t]", ""):lower().."," or ""
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
186 local httpversion = request.httpversion
5754
dee0f05ccf70 net.http.server: Fix Keep-Alive requests with HTTP 1.0
Kim Alvefur <zash@zash.se>
parents: 5505
diff changeset
187 local persistent = conn_header:find(",keep-alive,", 1, true)
5300
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
188 or (httpversion == "1.1" and not conn_header:find(",close,", 1, true));
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
189
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
190 local response_conn_header;
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
191 if persistent then
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
192 response_conn_header = "Keep-Alive";
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
193 else
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
194 response_conn_header = httpversion == "1.1" and "close" or nil
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
195 end
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
196
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
197 local response = {
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
198 request = request;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
199 status_code = 200;
5300
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
200 headers = { date = date_header, connection = response_conn_header };
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
201 persistent = persistent;
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
202 conn = conn;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
203 send = _M.send_response;
7489
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
204 send_file = _M.send_file;
6071
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
205 done = _M.finish_response;
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
206 finish_cb = finish_cb;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
207 };
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
208 conn._http_open_response = response;
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
209
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
210 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
211
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
212 -- 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
213 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
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222 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
223 end
4713
9c15fa5192d3 net.http.server: Fire http-error 400 if request fails sanity checks
Matthew Wild <mwild1@gmail.com>
parents: 4710
diff changeset
224 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5754
diff changeset
225
4713
9c15fa5192d3 net.http.server: Fire http-error 400 if request fails sanity checks
Matthew Wild <mwild1@gmail.com>
parents: 4710
diff changeset
226 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
227 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
228 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
229 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
230 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
231
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
232 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
233 local payload = { request = request, response = response };
6589
015f2b4e2f9a net.http.server: Log event name when firing a request event
Matthew Wild <mwild1@gmail.com>
parents: 6380
diff changeset
234 log("debug", "Firing event: %s", event);
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
235 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
236 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
237 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
238 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
239 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
240 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
241 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
242 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
243 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
244 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
245 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
246 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
247 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
248 for k, v in pairs(result) do
5404
ae9a47e579d7 net.http.server: Don't overwrite existing response.headers when returning a response object from a HTTP handler (waqas says it's wrong)
Matthew Wild <mwild1@gmail.com>
parents: 5333
diff changeset
249 if k ~= "headers" then
ae9a47e579d7 net.http.server: Don't overwrite existing response.headers when returning a response object from a HTTP handler (waqas says it's wrong)
Matthew Wild <mwild1@gmail.com>
parents: 5333
diff changeset
250 response[k] = v;
ae9a47e579d7 net.http.server: Don't overwrite existing response.headers when returning a response object from a HTTP handler (waqas says it's wrong)
Matthew Wild <mwild1@gmail.com>
parents: 5333
diff changeset
251 else
ae9a47e579d7 net.http.server: Don't overwrite existing response.headers when returning a response object from a HTTP handler (waqas says it's wrong)
Matthew Wild <mwild1@gmail.com>
parents: 5333
diff changeset
252 for header_name, header_value in pairs(v) do
ae9a47e579d7 net.http.server: Don't overwrite existing response.headers when returning a response object from a HTTP handler (waqas says it's wrong)
Matthew Wild <mwild1@gmail.com>
parents: 5333
diff changeset
253 response.headers[header_name] = header_value;
ae9a47e579d7 net.http.server: Don't overwrite existing response.headers when returning a response object from a HTTP handler (waqas says it's wrong)
Matthew Wild <mwild1@gmail.com>
parents: 5333
diff changeset
254 end
ae9a47e579d7 net.http.server: Don't overwrite existing response.headers when returning a response object from a HTTP handler (waqas says it's wrong)
Matthew Wild <mwild1@gmail.com>
parents: 5333
diff changeset
255 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
256 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
257 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
258 response:send(body);
4631
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 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
261 end
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
262
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
263 -- 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
264 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
265 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
266 end
6071
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
267 local function prepare_header(response)
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
268 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
269 local headers = response.headers;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
270 local output = { status_line };
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
271 for k,v in pairs(headers) do
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
272 t_insert(output, headerfix[k]..v);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
273 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
274 t_insert(output, "\r\n\r\n");
6071
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
275 return output;
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
276 end
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
277 _M.prepare_header = prepare_header;
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
278 function _M.send_response(response, body)
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
279 if response.finished then return; end
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
280 body = body or response.body or "";
6082
d0e824a21861 net.http.server: Fix some typos introduced in 420c0d3b8583.
Daurnimator <quae@daurnimator.com>
parents: 6071
diff changeset
281 response.headers.content_length = #body;
d0e824a21861 net.http.server: Fix some typos introduced in 420c0d3b8583.
Daurnimator <quae@daurnimator.com>
parents: 6071
diff changeset
282 local output = prepare_header(response);
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
283 t_insert(output, body);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
284 response.conn:write(t_concat(output));
6082
d0e824a21861 net.http.server: Fix some typos introduced in 420c0d3b8583.
Daurnimator <quae@daurnimator.com>
parents: 6071
diff changeset
285 response:done();
6071
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
286 end
7489
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
287 function _M.send_file(response, f)
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
288 if response.finished then return; end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
289 local chunked = not response.headers.content_length;
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
290 if chunked then response.headers.transfer_encoding = "chunked"; end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
291 incomplete[response.conn] = response;
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
292 response._send_more = function ()
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
293 if response.finished then
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
294 incomplete[response.conn] = nil;
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
295 return;
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
296 end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
297 local chunk = f:read(blocksize);
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
298 if chunk then
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
299 if chunked then
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
300 chunk = ("%x\r\n%s\r\n"):format(#chunk, chunk);
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
301 end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
302 -- io.write("."); io.flush();
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
303 response.conn:write(chunk);
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
304 else
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
305 if chunked then
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
306 response.conn:write("0\r\n\r\n");
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
307 end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
308 -- io.write("\n");
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
309 if f.close then f:close(); end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
310 incomplete[response.conn] = nil;
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
311 return response:done();
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
312 end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
313 end
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
314 response.conn:write(t_concat(prepare_header(response)));
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
315 return true;
d32406f27efd net.http.server: Add response method for reading response body from a file handle
Kim Alvefur <zash@zash.se>
parents: 7084
diff changeset
316 end
6071
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
317 function _M.finish_response(response)
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
318 if response.finished then return; end
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
319 response.finished = true;
420c0d3b8583 net.http.server: Add prepare_header() and finish_response() to allow sending chunked responses via the API
Daurnimator <quae@daurnimator.com>
parents: 5776
diff changeset
320 response.conn._http_open_response = nil;
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
321 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
322 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
323 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
324 end
5300
fcb1be0b4a5c net.http.server: Properly handle persistent connections
Florian Zeitz <florob@babelmonkeys.de>
parents: 4788
diff changeset
325 if response.persistent then
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
326 response:finish_cb();
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
327 else
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
328 response.conn:close();
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
329 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
330 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
331 function _M.add_handler(event, handler, priority)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
332 events.add_handler(event, handler, priority);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
333 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
334 function _M.remove_handler(event, handler)
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
335 events.remove_handler(event, handler);
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
336 end
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
337
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
338 function _M.listen_on(port, interface, ssl)
7084
fdbe9ccac17d net.http.server: Return from listen_on() whatever net.server.addserver() returns
Kim Alvefur <zash@zash.se>
parents: 6963
diff changeset
339 return addserver(interface or "*", port, listener, "*a", ssl);
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
340 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
341 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
342 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
343 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
344 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
345 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
346 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
347 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
348 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
349 end
5439
bd7b314c2301 net.http.server: add API to allow firing events directly on the server.
Marco Cirillo <maranda@lightwitch.org>
parents: 5404
diff changeset
350 function _M.fire_event(event, ...)
bd7b314c2301 net.http.server: add API to allow firing events directly on the server.
Marco Cirillo <maranda@lightwitch.org>
parents: 5404
diff changeset
351 return events.fire_event(event, ...);
bd7b314c2301 net.http.server: add API to allow firing events directly on the server.
Marco Cirillo <maranda@lightwitch.org>
parents: 5404
diff changeset
352 end
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
353
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
354 _M.listener = listener;
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
355 _M.codes = codes;
4706
845393c76d17 net.http.server: Expose events object (for debug purposes)
Matthew Wild <mwild1@gmail.com>
parents: 4704
diff changeset
356 _M._events = events;
4631
fc5d3b053454 net.http.{server|codes|parser}: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
357 return _M;