Skip to content

Commit

Permalink
Eliminate NPE
Browse files Browse the repository at this point in the history
  • Loading branch information
larry-safran committed Jul 13, 2023
1 parent 4fa2814 commit 6c50205
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions netty/src/main/java/io/grpc/netty/NettyServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,15 @@ public void run() {
super.handlerAdded(ctx);
}

private void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers)
// @return true if completed without calling respondWithHttpError, false if called that method.
private boolean onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers)
throws Http2Exception {
try {
// Connection-specific header fields makes a request malformed. Ideally this would be handled
// by Netty. RFC 7540 section 8.1.2.2
if (!DISABLE_CONNECTION_HEADER_CHECK && headers.contains(CONNECTION)) {
resetStream(ctx, streamId, Http2Error.PROTOCOL_ERROR.code(), ctx.newPromise());
return;
return false;
}

if (headers.authority() == null) {
Expand All @@ -405,7 +406,7 @@ private void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers
// RFC 7230 section 5.4
respondWithHttpError(ctx, streamId, 400, Status.Code.INTERNAL,
"Multiple host headers");
return;
return false;
}
if (!hosts.isEmpty()) {
headers.add(AUTHORITY.value(), hosts.get(0));
Expand All @@ -419,13 +420,13 @@ private void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers
if (path == null) {
respondWithHttpError(ctx, streamId, 404, Status.Code.UNIMPLEMENTED,
"Expected path but is missing");
return;
return false;
}

if (path.charAt(0) != '/') {
respondWithHttpError(ctx, streamId, 404, Status.Code.UNIMPLEMENTED,
String.format("Expected path to start with /: %s", path));
return;
return false;
}

String method = path.subSequence(1, path.length()).toString();
Expand All @@ -435,19 +436,19 @@ private void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers
if (contentType == null) {
respondWithHttpError(
ctx, streamId, 415, Status.Code.INTERNAL, "Content-Type is missing from the request");
return;
return false;
}
String contentTypeString = contentType.toString();
if (!GrpcUtil.isGrpcContentType(contentTypeString)) {
respondWithHttpError(ctx, streamId, 415, Status.Code.INTERNAL,
String.format("Content-Type '%s' is not supported", contentTypeString));
return;
return false;
}

if (!HTTP_METHOD.contentEquals(headers.method())) {
respondWithHttpError(ctx, streamId, 405, Status.Code.INTERNAL,
String.format("Method '%s' is not supported", headers.method()));
return;
return false;
}

if (!teWarningLogged && !TE_TRAILERS.contentEquals(headers.get(TE_HEADER))) {
Expand Down Expand Up @@ -493,6 +494,8 @@ private void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers
// Throw an exception that will get handled by onStreamError.
throw newStreamException(streamId, e);
}

return true;
}

private String getOrUpdateAuthority(AsciiString authority) {
Expand Down Expand Up @@ -848,7 +851,9 @@ public void onHeadersRead(ChannelHandlerContext ctx,
if (keepAliveManager != null) {
keepAliveManager.onDataReceived();
}
NettyServerHandler.this.onHeadersRead(ctx, streamId, headers);
if (!NettyServerHandler.this.onHeadersRead(ctx, streamId, headers)) {
return;
}
if (endStream) {
NettyServerHandler.this.onDataRead(streamId, Unpooled.EMPTY_BUFFER, 0, endStream);
}
Expand Down

0 comments on commit 6c50205

Please sign in to comment.