From b6be76f01003a1f81362569037ae79342a32235e Mon Sep 17 00:00:00 2001 From: Luca Niccoli Date: Tue, 16 Dec 2014 17:20:18 +0100 Subject: [PATCH] Add HTTPClientResponse.upgradeConnection() Since the response does not have access to the request headers, the expected protocol must be supplied as a parameter. Signed-off-by: Luca Niccoli --- source/vibe/http/client.d | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/source/vibe/http/client.d b/source/vibe/http/client.d index a18c4b249b..34abeecb1f 100644 --- a/source/vibe/http/client.d +++ b/source/vibe/http/client.d @@ -20,6 +20,7 @@ import vibe.inet.url; import vibe.stream.counting; import vibe.stream.ssl; import vibe.stream.operations; +import vibe.stream.wrapper : ConnectionProxyStream; import vibe.stream.zlib; import vibe.utils.array; import vibe.utils.memory; @@ -883,6 +884,26 @@ final class HTTPClientResponse : HTTPResponse { finalize(true); } + /** + Upgrades the connection to and returns the resulting ConnectionStream. + + The caller caller gets ownership of the ConnectionStream and is responsible + for closing it. + Params: + newProtocol = The protocol to which the connection is expected to upgrade. Should match the Upgrade header of the request. + */ + ConnectionStream upgradeConnection(in string newProtocol) + { + enforce(statusCode == HTTPStatus.switchingProtocols, "Server did not send a 101 - Switching Protocols response"); + string *resNewProto = "Upgrade" in headers; + enforce(resNewProto, "Server did not send an Upgrade header"); + enforce(*resNewProto == newProtocol, "Expected Upgrade: " ~ newProtocol ~", received Upgrade: " ~ *resNewProto); + auto stream = new ConnectionProxyStream(m_client.m_stream, m_client.m_conn); + m_client.m_responding = false; + m_client = null; + return stream; + } + private void finalize() { finalize(m_closeConn);