Skip to content

Commit

Permalink
Returns TCPListener's from http listener. Solves vibe-d#1074.
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkimax committed May 2, 2015
1 parent d98046e commit fe99e3c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
6 changes: 4 additions & 2 deletions source/vibe/http/dist.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import std.process;
This function is usable as direct replacement of
*/
void listenHTTPDist(HTTPServerSettings settings, HTTPServerRequestDelegate handler, string balancer_address, ushort balancer_port = 11000)
TCPListener[] listenHTTPDist(HTTPServerSettings settings, HTTPServerRequestDelegate handler, string balancer_address, ushort balancer_port = 11000)
{
Json regmsg = Json.emptyObject;
regmsg.host_name = settings.hostName;
Expand All @@ -36,7 +36,7 @@ void listenHTTPDist(HTTPServerSettings settings, HTTPServerRequestDelegate handl
local_settings.bindAddresses = ["127.0.0.1"];
local_settings.port = 0;
local_settings.disableDistHost = true;
listenHTTP(local_settings, handler);
TCPListener[] listener = listenHTTP(local_settings, handler);

requestHTTP(URL("http://"~balancer_address~":"~to!string(balancer_port)~"/register"), (scope req){
logInfo("Listening for VibeDist connections on port %d", req.localAddress.port);
Expand All @@ -47,4 +47,6 @@ void listenHTTPDist(HTTPServerSettings settings, HTTPServerRequestDelegate handl
}, (scope res){
enforce(res.statusCode == HTTPStatus.ok, "Failed to register with load balancer.");
});

return listener;
}
38 changes: 21 additions & 17 deletions source/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import std.uri;
request_handler = This callback is invoked for each incoming request and is responsible
for generating the response.
*/
void listenHTTP(HTTPServerSettings settings, HTTPServerRequestDelegate request_handler)
TCPListener[] listenHTTP(HTTPServerSettings settings, HTTPServerRequestDelegate request_handler)
{
enforce(settings.bindAddresses.length, "Must provide at least one bind address for a HTTP server.");

Expand All @@ -86,35 +86,35 @@ void listenHTTP(HTTPServerSettings settings, HTTPServerRequestDelegate request_h
// if a VibeDist host was specified on the command line, register there instead of listening
// directly.
if (s_distHost.length && !settings.disableDistHost) {
listenHTTPDist(settings, request_handler, s_distHost, s_distPort);
return listenHTTPDist(settings, request_handler, s_distHost, s_distPort);
} else {
listenHTTPPlain(settings);
return listenHTTPPlain(settings);
}
}
/// ditto
void listenHTTP(HTTPServerSettings settings, HTTPServerRequestFunction request_handler)
TCPListener[] listenHTTP(HTTPServerSettings settings, HTTPServerRequestFunction request_handler)
{
listenHTTP(settings, toDelegate(request_handler));
return listenHTTP(settings, toDelegate(request_handler));
}
/// ditto
void listenHTTP(HTTPServerSettings settings, HTTPServerRequestHandler request_handler)
TCPListener[] listenHTTP(HTTPServerSettings settings, HTTPServerRequestHandler request_handler)
{
listenHTTP(settings, &request_handler.handleRequest);
return listenHTTP(settings, &request_handler.handleRequest);
}
/// ditto
void listenHTTP(HTTPServerSettings settings, HTTPServerRequestDelegateS request_handler)
TCPListener[] listenHTTP(HTTPServerSettings settings, HTTPServerRequestDelegateS request_handler)
{
listenHTTP(settings, cast(HTTPServerRequestDelegate)request_handler);
return listenHTTP(settings, cast(HTTPServerRequestDelegate)request_handler);
}
/// ditto
void listenHTTP(HTTPServerSettings settings, HTTPServerRequestFunctionS request_handler)
TCPListener[] listenHTTP(HTTPServerSettings settings, HTTPServerRequestFunctionS request_handler)
{
listenHTTP(settings, toDelegate(request_handler));
return listenHTTP(settings, toDelegate(request_handler));
}
/// ditto
void listenHTTP(HTTPServerSettings settings, HTTPServerRequestHandlerS request_handler)
TCPListener[] listenHTTP(HTTPServerSettings settings, HTTPServerRequestHandlerS request_handler)
{
listenHTTP(settings, &request_handler.handleRequest);
return listenHTTP(settings, &request_handler.handleRequest);
}


Expand Down Expand Up @@ -1251,15 +1251,17 @@ private {
This is the same as listenHTTP() except that it does not use a VibeDist host for
remote listening, even if specified on the command line.
*/
private void listenHTTPPlain(HTTPServerSettings settings)
private TCPListener[] listenHTTPPlain(HTTPServerSettings settings)
{
import std.algorithm : canFind;

static bool doListen(HTTPServerSettings settings, size_t listener_idx, string addr)
TCPListener[] listeners;

static bool doListen(HTTPServerSettings settings, size_t listener_idx, string addr, TCPListener[] listeners)
{
try {
bool dist = (settings.options & HTTPServerOption.distribute) != 0;
listenTCP(settings.port, (TCPConnection conn){ handleHTTPConnection(conn, g_listeners[listener_idx]); }, addr, dist ? TCPListenOptions.distribute : TCPListenOptions.defaults);
listeners ~= listenTCP(settings.port, (TCPConnection conn){ handleHTTPConnection(conn, g_listeners[listener_idx]); }, addr, dist ? TCPListenOptions.distribute : TCPListenOptions.defaults);
logInfo("Listening for HTTP%s requests on %s:%s", settings.sslContext ? "S" : "", addr, settings.port);
return true;
} catch( Exception e ) {
Expand Down Expand Up @@ -1318,7 +1320,7 @@ private void listenHTTPPlain(HTTPServerSettings settings)
}
if (!found_listener) {
auto listener = HTTPServerListener(addr, settings.port, settings.sslContext);
if (doListen(settings, g_listeners.length, addr)) // DMD BUG 2043
if (doListen(settings, g_listeners.length, addr, listeners)) // DMD BUG 2043
{
found_listener = true;
any_successful = true;
Expand All @@ -1328,6 +1330,8 @@ private void listenHTTPPlain(HTTPServerSettings settings)
}

enforce(any_successful, "Failed to listen for incoming HTTP connections on any of the supplied interfaces.");

return listeners;
}


Expand Down

0 comments on commit fe99e3c

Please sign in to comment.