Skip to content

Commit

Permalink
Response must use 4xx status code when sent because of decoding error (
Browse files Browse the repository at this point in the history
…#12)


Motivation:

A 4xx status code must be used when sending a response caused because of an decoding error:
https://www.ietf.org/archive/id/draft-ietf-ohai-ohttp-10.html#section-5.2

Modifications:

Use a 4xx status code when exception is caused because of decoding

Result:

Follow the spec
  • Loading branch information
normanmaurer authored Dec 14, 2023
1 parent fd8c1c8 commit 8a043e9
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,21 @@ protected final void decode(ChannelHandlerContext ctx, HttpObject msg, List<Obje
} else {
out.add(ReferenceCountUtil.retain(msg));
}
} catch (CryptoException e) {
throw new DecoderException("failed to decrypt bytes", e);
} catch (Exception e) {
throw new OHttpServerDecoderException("failed to decode bytes", e);
}
}

@Override
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (!sentResponse && request != null) {
sentResponse = true;
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);

// Respond with 4xx status code in case of unable to decode message:
// See https://www.ietf.org/archive/id/draft-ietf-ohai-ohttp-10.html#section-5.2
HttpResponseStatus status = cause instanceof OHttpServerDecoderException ?
HttpResponseStatus.BAD_REQUEST : HttpResponseStatus.INTERNAL_SERVER_ERROR;
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status);
HttpUtil.setKeepAlive(response, false);
onResponse(request, response);

Expand Down Expand Up @@ -234,6 +239,12 @@ private void destroyContext() {
}
}

private static final class OHttpServerDecoderException extends DecoderException {
OHttpServerDecoderException(String msg, Throwable cause) {
super(msg, cause);
}
}

private static final class OHttpServerRequestResponseContext extends OHttpRequestResponseContext {

private final HybridPublicKeyEncryption encryption;
Expand Down

0 comments on commit 8a043e9

Please sign in to comment.