Skip to content

Commit

Permalink
Issue #4603 - avoid NPE during websocket HTTP/2 upgrade
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Mar 24, 2020
1 parent b7a4fb7 commit 035c6d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,16 @@ boolean prepareUpgrade()
Request request = channel.getRequest();
if (request.getHttpInput().hasContent())
return channel.sendErrorOrAbort("Unexpected content in CONNECT request");

Connection connection = (Connection)request.getAttribute(UPGRADE_CONNECTION_ATTRIBUTE);
if (connection == null)
return channel.sendErrorOrAbort("No UPGRADE_CONNECTION_ATTRIBUTE available");

EndPoint endPoint = connection.getEndPoint();
endPoint.upgrade(connection);
stream.setAttachment(endPoint);
// Only now that we have switched the attachment,
// we can demand DATA frames to process them.

// Only now that we have switched the attachment, we can demand DATA frames to process them.
stream.demand(1);

if (LOG.isDebugEnabled())
Expand All @@ -340,21 +344,6 @@ public void onCompleted()
Object attachment = stream.getAttachment();
if (attachment instanceof HttpChannelOverHTTP2)
{
// TODO: we used to "fake" a 101 response to upgrade the endpoint
// but we don't anymore, so this code should be deleted.
HttpChannelOverHTTP2 channel = (HttpChannelOverHTTP2)attachment;
if (channel.getResponse().getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101)
{
Connection connection = (Connection)channel.getRequest().getAttribute(UPGRADE_CONNECTION_ATTRIBUTE);
EndPoint endPoint = connection.getEndPoint();
// TODO: check that endPoint implements HTTP2Channel.
if (LOG.isDebugEnabled())
LOG.debug("Tunnelling DATA frames through {}", endPoint);
endPoint.upgrade(connection);
stream.setAttachment(endPoint);
return;
}

// If the stream is not closed, it is still reading the request content.
// Send a reset to the other end so that it stops sending data.
if (!stream.isClosed())
Expand All @@ -366,6 +355,7 @@ public void onCompleted()

// Consume the existing queued data frames to
// avoid stalling the session flow control.
HttpChannelOverHTTP2 channel = (HttpChannelOverHTTP2)attachment;
channel.consumeInput();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ public boolean handle()
break;
}

// Check if an update is done (if so, do not close)
// If an upgrade is attempted and failed with sendError call, then do not close here.
if (checkAndPrepareUpgrade())
break;

Expand Down

0 comments on commit 035c6d2

Please sign in to comment.