简体   繁体   English

Netty解码器结果-针对错误的请求发送正确的响应代码

[英]Netty decoder result - send proper response codes for bad request

I'm looking at the HttpStaticFileServerHandler example at Netty Github . 我正在看Netty Github上的HttpStaticFileServerHandler示例。 On messageRecieved, if the decoder result is not success, bad request is thrown. 在messageRecived上,如果解码器结果不成功,则会引发错误请求。 So the response code goes as 400. I would like to send 414 when the request uri is too long. 因此响应代码为400。当请求uri太长时,我想发送414。

I've tried, 我试过了,

Throwable cause = requestMessage.getDecoderResult().cause();
String message = cause != null ? cause.getMessage() : "";
HttpResponseStatus responseStatus = cause instanceof TooLongFrameException?
      HttpResponseStatus.REQUEST_URI_TOO_LONG : HttpResponseStatus.BAD_REQUEST;

Is there a better way? 有没有更好的办法? I believe TooLongFrameException is thrown for other cases as well. 我相信在其他情况下也会抛出TooLongFrameException。 So in those cases it may not be right. 因此,在这些情况下可能不正确。 Parsing the error message also doesn't look good. 解析错误消息也不是一件好事。 Wondering if this can be done in a better/elegant way. 想知道这是否可以更好/优雅地完成。

A bit further down in that handler is the code block: 该处理程序中稍稍下方的是代码块:

    final String uri = request.uri();
    final String path = sanitizeUri(uri);
    if (path == null) {
        sendError(ctx, FORBIDDEN);
        return;
    }

Could you use this block with a uri.length() check and relevant sendError? 您可以将此块与uri.length()检查和相关的sendError一起使用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM