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

Add HTTPClientResponse.upgradeConnection() #929

Closed
wants to merge 1 commit into from
Closed
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
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