From 8668fd0e03d45f15a136eaa63f54d2335a863407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 12 Aug 2017 22:35:57 +0200 Subject: [PATCH] Add nothrow WebSocket handler overloads and deprecate the rest. Fixes #1420. --- http/vibe/http/websockets.d | 76 ++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/http/vibe/http/websockets.d b/http/vibe/http/websockets.d index bc65ad48ae..cd1792ca53 100644 --- a/http/vibe/http/websockets.d +++ b/http/vibe/http/websockets.d @@ -56,7 +56,7 @@ import vibe.crypto.cryptorand; @safe: -alias WebSocketHandshakeDelegate = void delegate(scope WebSocket); +alias WebSocketHandshakeDelegate = void delegate(scope WebSocket) nothrow; /// Exception thrown by $(D vibe.http.websockets). @@ -167,10 +167,26 @@ void connectWebSocket(URL url, scope WebSocketHandshakeDelegate del, const(HTTPC ); } /// Scheduled for deprecation - use a `@safe` callback instead. -void connectWebSocket(URL url, scope void delegate(scope WebSocket) @system del, const(HTTPClientSettings) settings = defaultSettings) +void connectWebSocket(URL url, scope void delegate(scope WebSocket) @system nothrow del, const(HTTPClientSettings) settings = defaultSettings) @system { connectWebSocket(url, (scope ws) @trusted => del(ws), settings); } +/// Scheduled for deprecation - use a `nothrow` callback instead. +void connectWebSocket(URL url, scope void delegate(scope WebSocket) @safe del, const(HTTPClientSettings) settings = defaultSettings) +@safe { + connectWebSocket(url, (scope ws) nothrow { + try del(ws); + catch (Exception e) logWarn("WebSocket handler failed: %s", e.msg); + }, settings); +} +/// ditto +void connectWebSocket(URL url, scope void delegate(scope WebSocket) @system del, const(HTTPClientSettings) settings = defaultSettings) +@system { + connectWebSocket(url, (scope ws) nothrow { + try del(ws); + catch (Exception e) logWarn("WebSocket handler failed: %s", e.msg); + }, settings); +} /** @@ -223,16 +239,32 @@ void handleWebSocket(scope WebSocketHandshakeDelegate on_handshake, scope HTTPSe socket.close(); } /// Scheduled for deprecation - use a `@safe` callback instead. -void handleWebSocket(scope void delegate(scope WebSocket) @system on_handshake, scope HTTPServerRequest req, scope HTTPServerResponse res) +void handleWebSocket(scope void delegate(scope WebSocket) @system nothrow on_handshake, scope HTTPServerRequest req, scope HTTPServerResponse res) @system { handleWebSocket((scope ws) @trusted => on_handshake(ws), req, res); } +/// Scheduled for deprecation - use a `nothrow` callback instead. +void handleWebSocket(scope void delegate(scope WebSocket) @safe on_handshake, scope HTTPServerRequest req, scope HTTPServerResponse res) +{ + handleWebSocket((scope ws) nothrow { + try on_handshake(ws); + catch (Exception e) logWarn("WebSocket handler failed: %s", e.msg); + }, req, res); +} +/// ditto +void handleWebSocket(scope void delegate(scope WebSocket) @system on_handshake, scope HTTPServerRequest req, scope HTTPServerResponse res) +@system { + handleWebSocket((scope ws) nothrow { + try on_handshake(ws); + catch (Exception e) logWarn("WebSocket handler failed: %s", e.msg); + }, req, res); +} /** Returns a HTTP request handler that establishes web socket conections. */ -HTTPServerRequestDelegateS handleWebSockets(void function(scope WebSocket) @safe on_handshake) +HTTPServerRequestDelegateS handleWebSockets(void function(scope WebSocket) @safe nothrow on_handshake) @safe { return handleWebSockets(() @trusted { return toDelegate(on_handshake); } ()); } @@ -285,15 +317,47 @@ HTTPServerRequestDelegateS handleWebSockets(WebSocketHandshakeDelegate on_handsh return &callback; } /// Scheduled for deprecation - use a `@safe` callback instead. -HTTPServerRequestDelegateS handleWebSockets(void delegate(scope WebSocket) @system on_handshake) +HTTPServerRequestDelegateS handleWebSockets(void delegate(scope WebSocket) @system nothrow on_handshake) @system { return handleWebSockets(delegate (scope ws) @trusted => on_handshake(ws)); } /// Scheduled for deprecation - use a `@safe` callback instead. -HTTPServerRequestDelegateS handleWebSockets(void function(scope WebSocket) @system on_handshake) +HTTPServerRequestDelegateS handleWebSockets(void function(scope WebSocket) @system nothrow on_handshake) @system { return handleWebSockets(delegate (scope ws) @trusted => on_handshake(ws)); } +/// Scheduled for deprecation - use a `nothrow` callback instead. +HTTPServerRequestDelegateS handleWebSockets(void delegate(scope WebSocket) @safe on_handshake) +{ + return handleWebSockets(delegate (scope ws) nothrow { + try on_handshake(ws); + catch (Exception e) logWarn("WebSocket handler failed: %s", e.msg); + }); +} +/// ditto +HTTPServerRequestDelegateS handleWebSockets(void function(scope WebSocket) @safe on_handshake) +{ + return handleWebSockets(delegate (scope ws) nothrow { + try on_handshake(ws); + catch (Exception e) logWarn("WebSocket handler failed: %s", e.msg); + }); +} +/// ditto +HTTPServerRequestDelegateS handleWebSockets(void delegate(scope WebSocket) @system on_handshake) +@system { + return handleWebSockets(delegate (scope ws) nothrow { + try on_handshake(ws); + catch (Exception e) logWarn("WebSocket handler failed: %s", e.msg); + }); +} +/// ditto +HTTPServerRequestDelegateS handleWebSockets(void function(scope WebSocket) @system on_handshake) +@system { + return handleWebSockets(delegate (scope ws) nothrow { + try on_handshake(ws); + catch (Exception e) logWarn("WebSocket handler failed: %s", e.msg); + }); +} /**