Skip to content

Commit

Permalink
Merge pull request #5312 from eclipse/jetty-9.4.x-5170-WebSocketUpgrade
Browse files Browse the repository at this point in the history
Issue #5170 - backport fixes for WebSocket upgrade to jetty-9.4.x
  • Loading branch information
lachlan-roberts committed Sep 25, 2020
2 parents 8fe32ce + 5d951e8 commit 6186d71
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 7 deletions.
Expand Up @@ -47,6 +47,7 @@ public class HttpReceiverOverHTTP extends HttpReceiver implements HttpParser.Res
private boolean shutdown;
private boolean complete;
private boolean unsolicited;
private int status;

public HttpReceiverOverHTTP(HttpChannelOverHTTP channel)
{
Expand Down Expand Up @@ -118,15 +119,17 @@ private void releaseNetworkBuffer()

protected ByteBuffer onUpgradeFrom()
{
ByteBuffer upgradeBuffer = null;
if (networkBuffer.hasRemaining())
{
ByteBuffer upgradeBuffer = BufferUtil.allocate(networkBuffer.remaining());
upgradeBuffer = BufferUtil.allocate(networkBuffer.remaining());
BufferUtil.clearToFill(upgradeBuffer);
BufferUtil.put(networkBuffer.getBuffer(), upgradeBuffer);
BufferUtil.flipToFlush(upgradeBuffer, 0);
return upgradeBuffer;
}
return null;

releaseNetworkBuffer();
return upgradeBuffer;
}

private void process()
Expand All @@ -145,12 +148,11 @@ private void process()
return;
}

// Connection may be closed or upgraded in a parser callback.
boolean upgraded = connection != endPoint.getConnection();
if (connection.isClosed() || upgraded)
// Connection may be closed in a parser callback.
if (connection.isClosed())
{
if (LOG.isDebugEnabled())
LOG.debug("{} {}", upgraded ? "Upgraded" : "Closed", connection);
LOG.debug("Closed {}", connection);
releaseNetworkBuffer();
return;
}
Expand Down Expand Up @@ -215,6 +217,14 @@ private boolean parse()
if (LOG.isDebugEnabled())
LOG.debug("Parse complete={}, remaining {} {}", complete, networkBuffer.remaining(), parser);

if (complete)
{
int status = this.status;
this.status = 0;
if (status == HttpStatus.SWITCHING_PROTOCOLS_101)
return true;
}

if (networkBuffer.isEmpty())
return false;

Expand Down Expand Up @@ -269,6 +279,7 @@ public boolean startResponse(HttpVersion version, int status, String reason)
if (exchange == null)
return false;

this.status = status;
String method = exchange.getRequest().getMethod();
parser.setHeadResponse(HttpMethod.HEAD.is(method) ||
(HttpMethod.CONNECT.is(method) && status == HttpStatus.OK_200));
Expand Down

0 comments on commit 6186d71

Please sign in to comment.