Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow listenHTTP to accept a convenience setting string #1816

Merged
merged 1 commit into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 59 additions & 21 deletions http/vibe/http/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ else version = HaveNoTLS;
use listenHTTPPlain() instead.

Params:
settings = Customizes the HTTP servers functionality.
settings = Customizes the HTTP servers functionality (host string or HTTPServerSettings object)
request_handler = This callback is invoked for each incoming request and is responsible
for generating the response.

Expand All @@ -83,8 +83,15 @@ else version = HaveNoTLS;
requests with the supplied settings. Another call to `listenHTTP` can be
used afterwards to start listening again.
*/
HTTPListener listenHTTP(HTTPServerSettings settings, HTTPServerRequestDelegate request_handler)
@safe {
HTTPListener listenHTTP(Settings)(Settings _settings, HTTPServerRequestDelegate request_handler)
@safe
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
// auto-construct HTTPServerSettings
static if (is(Settings == string))
auto settings = new HTTPServerSettings(_settings);
else
alias settings = _settings;

enforce(settings.bindAddresses.length, "Must provide at least one bind address for a HTTP server.");

HTTPServerContext ctx;
Expand All @@ -111,52 +118,83 @@ HTTPListener listenHTTP(HTTPServerSettings settings, HTTPServerRequestDelegate r
return HTTPListener(ctx.id);
}
/// ditto
HTTPListener listenHTTP(HTTPServerSettings settings, HTTPServerRequestFunction request_handler)
@safe {
HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestFunction request_handler)
@safe
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, () @trusted { return toDelegate(request_handler); } ());
}
/// ditto
HTTPListener listenHTTP(HTTPServerSettings settings, HTTPServerRequestHandler request_handler)
@safe {
HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestHandler request_handler)
@safe
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, &request_handler.handleRequest);
}
/// ditto
HTTPListener listenHTTP(HTTPServerSettings settings, HTTPServerRequestDelegateS request_handler)
@safe {
HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestDelegateS request_handler)
@safe
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, cast(HTTPServerRequestDelegate)request_handler);
}
/// ditto
HTTPListener listenHTTP(HTTPServerSettings settings, HTTPServerRequestFunctionS request_handler)
@safe {
HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestFunctionS request_handler)
@safe
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, () @trusted { return toDelegate(request_handler); } ());
}
/// ditto
HTTPListener listenHTTP(HTTPServerSettings settings, HTTPServerRequestHandlerS request_handler)
@safe {
HTTPListener listenHTTP(Settings)(Settings settings, HTTPServerRequestHandlerS request_handler)
@safe
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, &request_handler.handleRequest);
}

/// Scheduled for deprecation - use a `@safe` callback instead.
HTTPListener listenHTTP(HTTPServerSettings settings, void delegate(HTTPServerRequest, HTTPServerResponse) @system request_handler)
@system {
HTTPListener listenHTTP(Settings)(Settings settings, void delegate(HTTPServerRequest, HTTPServerResponse) @system request_handler)
@system
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, (req, res) @trusted => request_handler(req, res));
}
/// ditto
HTTPListener listenHTTP(HTTPServerSettings settings, void function(HTTPServerRequest, HTTPServerResponse) @system request_handler)
@system {
HTTPListener listenHTTP(Settings)(Settings settings, void function(HTTPServerRequest, HTTPServerResponse) @system request_handler)
@system
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, (req, res) @trusted => request_handler(req, res));
}
/// ditto
HTTPListener listenHTTP(HTTPServerSettings settings, void delegate(scope HTTPServerRequest, scope HTTPServerResponse) @system request_handler)
@system {
HTTPListener listenHTTP(Settings)(Settings settings, void delegate(scope HTTPServerRequest, scope HTTPServerResponse) @system request_handler)
@system
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, (scope req, scope res) @trusted => request_handler(req, res));
}
/// ditto
HTTPListener listenHTTP(HTTPServerSettings settings, void function(scope HTTPServerRequest, scope HTTPServerResponse) @system request_handler)
@system {
HTTPListener listenHTTP(Settings)(Settings settings, void function(scope HTTPServerRequest, scope HTTPServerResponse) @system request_handler)
@system
if (is(Settings == string) || is(Settings == HTTPServerSettings)) {
return listenHTTP(settings, (scope req, scope res) @trusted => request_handler(req, res));
}

unittest
{
void test()
{
static void testSafeFunction(HTTPServerRequest req, HTTPServerResponse res) @safe {}
listenHTTP("0.0.0.0:8080", &testSafeFunction);
listenHTTP(":8080", new class HTTPServerRequestHandler {
void handleRequest(HTTPServerRequest req, HTTPServerResponse res) @safe {}
});
listenHTTP(":8080", (req, res) {});

static void testSafeFunctionS(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe {}
listenHTTP(":8080", &testSafeFunctionS);
void testSafeDelegateS(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe {}
listenHTTP(":8080", &testSafeDelegateS);
listenHTTP(":8080", new class HTTPServerRequestHandler {
void handleRequest(scope HTTPServerRequest req, scope HTTPServerResponse res) @safe {}
});
listenHTTP(":8080", (scope req, scope res) {});
}
}

/**
Provides a HTTP request handler that responds with a static Diet template.
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/vibe.http.server.listenHTTP/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name "tests"
description "listenHTTP tests"
dependency "vibe-d:http" path="../../"
versions "VibeDefaultMain"
22 changes: 22 additions & 0 deletions tests/vibe.http.server.listenHTTP/source/app.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import vibe.core.core;
import vibe.core.core;
import vibe.core.log;
import vibe.http.client;
import vibe.http.server;
import vibe.stream.operations : readAllUTF8;

shared static this()
{
listenHTTP(":11721", (scope req, scope res) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the hard-coded port from other tests - how about adding something like getFreePort to Vibe.d?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or supporting ":0" and providing a way to retrieve the actual listen port.

res.writeBody("Hello world.");
});

runTask({
scope (exit) exitEventLoop();

auto res = requestHTTP("http://0.0.0.0:11721");
assert(res.statusCode == HTTPStatus.ok);
assert(res.bodyReader.readAllUTF8 == "Hello world.");
logInfo("All web tests succeeded.");
});
}