util.x509: Nameprep commonName once
[prosody.git] / net / server.lua
blobabbb421d4f8fd2eb2589e8bef1205bed20cc160b
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
9 if not (prosody and prosody.config_loaded) then
10 -- This module only supports loading inside Prosody, outside Prosody
11 -- you should directly require net.server_select or server_event, etc.
12 error(debug.traceback("Loading outside Prosody or Prosody not yet initialized"), 0);
13 end
15 local log = require "util.logger".init("net.server");
16 local server_type = require "core.configmanager".get("*", "network_backend") or "select";
18 if require "core.configmanager".get("*", "use_libevent") then
19 server_type = "event";
20 end
22 if server_type == "event" then
23 if not pcall(require, "luaevent.core") then
24 log("error", "libevent not found, falling back to select()");
25 server_type = "select"
26 end
27 end
29 local server;
30 local set_config;
31 if server_type == "event" then
32 server = require "net.server_event";
34 local defaults = {};
35 for k,v in pairs(server.cfg) do
36 defaults[k] = v;
37 end
38 function set_config(settings)
39 local event_settings = {
40 ACCEPT_DELAY = settings.accept_retry_interval;
41 ACCEPT_QUEUE = settings.tcp_backlog;
42 CLEAR_DELAY = settings.event_clear_interval;
43 CONNECT_TIMEOUT = settings.connect_timeout;
44 DEBUG = settings.debug;
45 HANDSHAKE_TIMEOUT = settings.ssl_handshake_timeout;
46 MAX_CONNECTIONS = settings.max_connections;
47 MAX_HANDSHAKE_ATTEMPTS = settings.max_ssl_handshake_roundtrips;
48 MAX_READ_LENGTH = settings.max_receive_buffer_size;
49 MAX_SEND_LENGTH = settings.max_send_buffer_size;
50 READ_TIMEOUT = settings.read_timeout;
51 WRITE_TIMEOUT = settings.send_timeout;
54 for k,default in pairs(defaults) do
55 server.cfg[k] = event_settings[k] or default;
56 end
57 end
58 elseif server_type == "select" then
59 server = require "net.server_select";
61 local defaults = {};
62 for k,v in pairs(server.getsettings()) do
63 defaults[k] = v;
64 end
65 function set_config(settings)
66 local select_settings = {};
67 for k,default in pairs(defaults) do
68 select_settings[k] = settings[k] or default;
69 end
70 server.changesettings(select_settings);
71 end
72 else
73 server = require("net.server_"..server_type);
74 set_config = server.set_config;
75 if not server.get_backend then
76 function server.get_backend()
77 return server_type;
78 end
79 end
80 end
82 -- If server.hook_signal exists, replace signal.signal()
83 local has_signal, signal = pcall(require, "util.signal");
84 if has_signal then
85 if server.hook_signal then
86 function signal.signal(signal_id, handler)
87 if type(signal_id) == "string" then
88 signal_id = signal[signal_id:upper()];
89 end
90 if type(signal_id) ~= "number" then
91 return false, "invalid-signal";
92 end
93 return server.hook_signal(signal_id, handler);
94 end
95 else
96 server.hook_signal = signal.signal;
97 end
98 else
99 if not server.hook_signal then
100 server.hook_signal = function()
101 return false, "signal hooking not supported"
106 if prosody and set_config then
107 local config_get = require "core.configmanager".get;
108 local function load_config()
109 local settings = config_get("*", "network_settings") or {};
110 return set_config(settings);
112 load_config();
113 prosody.events.add_handler("config-reloaded", load_config);
116 -- require "net.server" shall now forever return this,
117 -- ie. server_select or server_event as chosen above.
118 return server;