Skip to content

Commit

Permalink
Add HTTPClientResponse.upgradeConnection()
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
lniccoli committed Dec 29, 2014
1 parent ad5f912 commit b6be76f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions source/vibe/http/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b6be76f

Please sign in to comment.