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

WebSockets over HTTP/2 #1978

Merged
merged 20 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from 19 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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageIcon></PackageIcon>
<PackageIconFullPath></PackageIconFullPath>
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<StrongNameKeyId>Microsoft</StrongNameKeyId>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand Down
14 changes: 9 additions & 5 deletions docs/docfx/articles/websockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

YARP enables proxying WebSocket and SPDY connections by default. This support works with both the [direct forwarding](direct-forwarding.md) and [full pipeline](getting-started.md) approaches.

[WebSockets](https://www.rfc-editor.org/rfc/rfc6455.html) is a bidirectional streaming protocol built on HTTP/1.1.
[WebSockets](https://www.rfc-editor.org/rfc/rfc6455.html) is a bidirectional streaming protocol built on HTTP/1.1 and later adapted to [HTTP/2](https://datatracker.ietf.org/doc/html/rfc8441).

[SPDY](https://www.chromium.org/spdy/spdy-protocol/) is the precursor to HTTP/2 and is commonly used in Kubernetes environments.

## Upgrades
## HTTP/1.1 Upgrades

WebSockets and SPDY are built on top of HTTP/1.1 using a feature called [connection upgrades](https://datatracker.ietf.org/doc/html/rfc7230#section-6.7). YARP proxies the initial request, and if the destination server responds with `101 Switching Protocols`, upgrades the connection to an opaque, bidirectional stream using the new protocol. YARP does not allow upgrading to other protocols like HTTP/2 this way.
WebSockets and SPDY are built on top of HTTP/1.1 using a feature called [connection upgrades](https://datatracker.ietf.org/doc/html/rfc7230#section-6.7). YARP proxies the initial request, and if the destination server responds with `101 Switching Protocols`, upgrades the connection to an opaque, bidirectional stream using the new protocol. YARP does not support upgrading to other protocols like HTTP/2 this way.

## HTTP/2

YARP and its underlying components (ASP.NET Core and HttpClient) do not support [WebSockets over HTTP/2](https://datatracker.ietf.org/doc/html/rfc8441). Even if YARP is configured to proxy requests to the destination using HTTP/2, it will detect WebSocket and SPDY requests and use HTTP/1.1 to proxy them.
YARP supports [WebSockets over HTTP/2](https://datatracker.ietf.org/doc/html/rfc8441) starting in .NET 7 and YARP 2.0. Kestrel is the only available AspNetCore server that will accept incoming HTTP/2 WebSocket requests and that support is automatically enabled. Browsers can detect this support advertised by the server and automatically switch to HTTP/2.

See https://github.com/microsoft/reverse-proxy/issues/1375 for future work here.
The incoming and outgoing protocol versions do not need to match. The incoming WebSocket request can be HTTP/1.1 or 2. There is no configuration specific to WebSockets for outgoing requests, YARP will use the [ForwarderRequestConfig](xref:Yarp.ReverseProxy.Forwarder.ForwarderRequestConfig)'s Version and VersionPolicy to determine the outbound version to use. These default to HTTP/2 and RequestVersionOrLower.

WebSockets require different HTTP headers for HTTP/2 so YARP will add and remove these headers as needed when adapting between the different versions.

After the initial handshake WebSockets function the same way over both HTTP versions.
88 changes: 0 additions & 88 deletions src/ReverseProxy/Forwarder/AutoFlushingStream.cs

This file was deleted.

Loading