예제 #1
0
  @Override
  protected Object newContinueResponse(
      HttpMessage start, int maxContentLength, ChannelPipeline pipeline) {
    if (HttpUtil.is100ContinueExpected(start)) {
      if (getContentLength(start, -1) <= maxContentLength) {
        return CONTINUE.duplicate().retain();
      }

      pipeline.fireUserEventTriggered(HttpExpectationFailedEvent.INSTANCE);
      return EXPECTATION_FAILED.duplicate().retain();
    }
    return null;
  }
예제 #2
0
  @Override
  protected void handleOversizedMessage(final ChannelHandlerContext ctx, HttpMessage oversized)
      throws Exception {
    if (oversized instanceof HttpRequest) {
      // send back a 413 and close the connection
      ChannelFuture future =
          ctx.writeAndFlush(TOO_LARGE.duplicate().retain())
              .addListener(
                  new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                      if (!future.isSuccess()) {
                        logger.debug(
                            "Failed to send a 413 Request Entity Too Large.", future.cause());
                        ctx.close();
                      }
                    }
                  });

      // If the client started to send data already, close because it's impossible to recover.
      // If keep-alive is off and 'Expect: 100-continue' is missing, no need to leave the connection
      // open.
      if (oversized instanceof FullHttpMessage
          || !HttpUtil.is100ContinueExpected(oversized) && !HttpUtil.isKeepAlive(oversized)) {
        future.addListener(ChannelFutureListener.CLOSE);
      }

      // If an oversized request was handled properly and the connection is still alive
      // (i.e. rejected 100-continue). the decoder should prepare to handle a new message.
      HttpObjectDecoder decoder = ctx.pipeline().get(HttpObjectDecoder.class);
      if (decoder != null) {
        decoder.reset();
      }
    } else if (oversized instanceof HttpResponse) {
      ctx.close();
      throw new TooLongFrameException("Response entity too large: " + oversized);
    } else {
      throw new IllegalStateException();
    }
  }