@Test void testOnHeadersRead_headersStreamContinuing() throws Exception { testHandler = new Http2Handler(mockEncoder, MAX_PAYLOAD, NO_CORS); headers.method("GET").path(PARAM_PATH_PREFIX + "/group"); testHandler.onHeadersRead(mockContext, STREAM_ID, headers, 1, false); assertEquals(1L, requestMeter.getCount()); verifyNoResponse(); XrpcRequest xrpcRequest = testHandler.requests.get(STREAM_ID); assertNotNull(xrpcRequest); assertEquals(headers, xrpcRequest.h2Headers()); assertEquals("group", xrpcRequest.variable(PARAM_NAME)); Handler handler = testHandler.handlers.get(STREAM_ID); assertNotNull(handler); assertEquals(HttpResponseStatus.OK, handler.handle(xrpcRequest).status()); }
@Override public void onHeadersRead( ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { Channel channel = ctx.channel(); ServerContext xctx = channel.attr(ServerContext.ATTRIBUTE_KEY).get(); XrpcRequest request = requests.get(streamId); if (request == null) { xctx.requestMeter().mark(); } if (channel.hasAttr(XrpcConstants.XRPC_SOFT_RATE_LIMITED)) { writeResponse( ctx, streamId, HttpResponseStatus.TOO_MANY_REQUESTS, Unpooled.wrappedBuffer(XrpcConstants.RATE_LIMIT_RESPONSE)); return; } long contentLength = 0; try { contentLength = headers.getLong(CONTENT_LENGTH, 0); } catch (NumberFormatException nfe) { } if (contentLength > maxPayloadBytes) { writeResponse( ctx, streamId, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, Unpooled.wrappedBuffer(XrpcConstants.PAYLOAD_EXCEEDED_RESPONSE)); ctx.flush(); ctx.close(); return; } Handler handler; if (request == null) { String path = getPathFromHeaders(headers); CompiledRoutes.Match match = xctx.routes().match(path, headers.method().toString()); request = new XrpcRequest(headers, xctx, match.getGroups(), channel); handler = match.getHandler(); } else { request.h2Headers().add(headers); handler = handlers.get(streamId); } if (endOfStream) { Optional<HttpResponse> corsResponse = corsHandler.inbound(headers, streamId); if (corsResponse.isPresent()) { writeResponse(ctx, streamId, corsResponse.get()); return; } try { HttpResponse response = handler.handle(request); writeResponse(ctx, streamId, response); } catch (IOException e) { log.error("Error in handling Route", e); ByteBuf buf = channel.alloc().directBuffer(); buf.writeBytes("Error executing endpoint".getBytes(XrpcConstants.DEFAULT_CHARSET)); writeResponse(ctx, streamId, HttpResponseStatus.INTERNAL_SERVER_ERROR, buf); } } else { requests.put(streamId, request); handlers.put(streamId, handler); } }
Http2Handler extends Http2EventAdapter { @Override public void onHeadersRead( ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { Channel channel = ctx.channel(); ServerContext xctx = channel.attr(ServerContext.ATTRIBUTE_KEY).get(); XrpcRequest request = requests.get(streamId); if (request == null) { xctx.requestMeter().mark(); } if (channel.hasAttr(XrpcConstants.XRPC_SOFT_RATE_LIMITED)) { writeResponse( ctx, streamId, HttpResponseStatus.TOO_MANY_REQUESTS, Unpooled.wrappedBuffer(XrpcConstants.RATE_LIMIT_RESPONSE)); return; } long contentLength = 0; try { contentLength = headers.getLong(CONTENT_LENGTH, 0); } catch (NumberFormatException nfe) { } if (contentLength > maxPayloadBytes) { writeResponse( ctx, streamId, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, Unpooled.wrappedBuffer(XrpcConstants.PAYLOAD_EXCEEDED_RESPONSE)); ctx.flush(); ctx.close(); return; } Handler handler; if (request == null) { String path = getPathFromHeaders(headers); CompiledRoutes.Match match = xctx.routes().match(path, headers.method().toString()); request = new XrpcRequest(headers, xctx, match.getGroups(), channel); handler = match.getHandler(); } else { request.h2Headers().add(headers); handler = handlers.get(streamId); } if (endOfStream) { Optional<HttpResponse> corsResponse = corsHandler.inbound(headers, streamId); if (corsResponse.isPresent()) { writeResponse(ctx, streamId, corsResponse.get()); return; } try { HttpResponse response = handler.handle(request); writeResponse(ctx, streamId, response); } catch (IOException e) { log.error("Error in handling Route", e); ByteBuf buf = channel.alloc().directBuffer(); buf.writeBytes("Error executing endpoint".getBytes(XrpcConstants.DEFAULT_CHARSET)); writeResponse(ctx, streamId, HttpResponseStatus.INTERNAL_SERVER_ERROR, buf); } } else { requests.put(streamId, request); handlers.put(streamId, handler); } } }
Http2Handler extends Http2EventAdapter { @Override public void onHeadersRead( ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { Channel channel = ctx.channel(); ServerContext xctx = channel.attr(ServerContext.ATTRIBUTE_KEY).get(); XrpcRequest request = requests.get(streamId); if (request == null) { xctx.requestMeter().mark(); } if (channel.hasAttr(XrpcConstants.XRPC_SOFT_RATE_LIMITED)) { writeResponse( ctx, streamId, HttpResponseStatus.TOO_MANY_REQUESTS, Unpooled.wrappedBuffer(XrpcConstants.RATE_LIMIT_RESPONSE)); return; } long contentLength = 0; try { contentLength = headers.getLong(CONTENT_LENGTH, 0); } catch (NumberFormatException nfe) { } if (contentLength > maxPayloadBytes) { writeResponse( ctx, streamId, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, Unpooled.wrappedBuffer(XrpcConstants.PAYLOAD_EXCEEDED_RESPONSE)); ctx.flush(); ctx.close(); return; } Handler handler; if (request == null) { String path = getPathFromHeaders(headers); CompiledRoutes.Match match = xctx.routes().match(path, headers.method().toString()); request = new XrpcRequest(headers, xctx, match.getGroups(), channel); handler = match.getHandler(); } else { request.h2Headers().add(headers); handler = handlers.get(streamId); } if (endOfStream) { Optional<HttpResponse> corsResponse = corsHandler.inbound(headers, streamId); if (corsResponse.isPresent()) { writeResponse(ctx, streamId, corsResponse.get()); return; } try { HttpResponse response = handler.handle(request); writeResponse(ctx, streamId, response); } catch (IOException e) { log.error("Error in handling Route", e); ByteBuf buf = channel.alloc().directBuffer(); buf.writeBytes("Error executing endpoint".getBytes(XrpcConstants.DEFAULT_CHARSET)); writeResponse(ctx, streamId, HttpResponseStatus.INTERNAL_SERVER_ERROR, buf); } } else { requests.put(streamId, request); handlers.put(streamId, handler); } } Http2Handler(Http2ConnectionEncoder encoder, int maxPayloadBytes, Http2CorsHandler corsHandler); }
Http2Handler extends Http2EventAdapter { @Override public void onHeadersRead( ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { Channel channel = ctx.channel(); ServerContext xctx = channel.attr(ServerContext.ATTRIBUTE_KEY).get(); XrpcRequest request = requests.get(streamId); if (request == null) { xctx.requestMeter().mark(); } if (channel.hasAttr(XrpcConstants.XRPC_SOFT_RATE_LIMITED)) { writeResponse( ctx, streamId, HttpResponseStatus.TOO_MANY_REQUESTS, Unpooled.wrappedBuffer(XrpcConstants.RATE_LIMIT_RESPONSE)); return; } long contentLength = 0; try { contentLength = headers.getLong(CONTENT_LENGTH, 0); } catch (NumberFormatException nfe) { } if (contentLength > maxPayloadBytes) { writeResponse( ctx, streamId, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, Unpooled.wrappedBuffer(XrpcConstants.PAYLOAD_EXCEEDED_RESPONSE)); ctx.flush(); ctx.close(); return; } Handler handler; if (request == null) { String path = getPathFromHeaders(headers); CompiledRoutes.Match match = xctx.routes().match(path, headers.method().toString()); request = new XrpcRequest(headers, xctx, match.getGroups(), channel); handler = match.getHandler(); } else { request.h2Headers().add(headers); handler = handlers.get(streamId); } if (endOfStream) { Optional<HttpResponse> corsResponse = corsHandler.inbound(headers, streamId); if (corsResponse.isPresent()) { writeResponse(ctx, streamId, corsResponse.get()); return; } try { HttpResponse response = handler.handle(request); writeResponse(ctx, streamId, response); } catch (IOException e) { log.error("Error in handling Route", e); ByteBuf buf = channel.alloc().directBuffer(); buf.writeBytes("Error executing endpoint".getBytes(XrpcConstants.DEFAULT_CHARSET)); writeResponse(ctx, streamId, HttpResponseStatus.INTERNAL_SERVER_ERROR, buf); } } else { requests.put(streamId, request); handlers.put(streamId, handler); } } Http2Handler(Http2ConnectionEncoder encoder, int maxPayloadBytes, Http2CorsHandler corsHandler); @Override int onDataRead(
      ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream); @Override void onHeadersRead(
      ChannelHandlerContext ctx,
      int streamId,
      Http2Headers headers,
      int padding,
      boolean endOfStream); @Override void onHeadersRead(
      ChannelHandlerContext ctx,
      int streamId,
      Http2Headers headers,
      int streamDependency,
      short weight,
      boolean exclusive,
      int padding,
      boolean endOfStream); @Override void onStreamRemoved(Http2Stream stream); }
Http2Handler extends Http2EventAdapter { @Override public void onHeadersRead( ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) { Channel channel = ctx.channel(); ServerContext xctx = channel.attr(ServerContext.ATTRIBUTE_KEY).get(); XrpcRequest request = requests.get(streamId); if (request == null) { xctx.requestMeter().mark(); } if (channel.hasAttr(XrpcConstants.XRPC_SOFT_RATE_LIMITED)) { writeResponse( ctx, streamId, HttpResponseStatus.TOO_MANY_REQUESTS, Unpooled.wrappedBuffer(XrpcConstants.RATE_LIMIT_RESPONSE)); return; } long contentLength = 0; try { contentLength = headers.getLong(CONTENT_LENGTH, 0); } catch (NumberFormatException nfe) { } if (contentLength > maxPayloadBytes) { writeResponse( ctx, streamId, HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE, Unpooled.wrappedBuffer(XrpcConstants.PAYLOAD_EXCEEDED_RESPONSE)); ctx.flush(); ctx.close(); return; } Handler handler; if (request == null) { String path = getPathFromHeaders(headers); CompiledRoutes.Match match = xctx.routes().match(path, headers.method().toString()); request = new XrpcRequest(headers, xctx, match.getGroups(), channel); handler = match.getHandler(); } else { request.h2Headers().add(headers); handler = handlers.get(streamId); } if (endOfStream) { Optional<HttpResponse> corsResponse = corsHandler.inbound(headers, streamId); if (corsResponse.isPresent()) { writeResponse(ctx, streamId, corsResponse.get()); return; } try { HttpResponse response = handler.handle(request); writeResponse(ctx, streamId, response); } catch (IOException e) { log.error("Error in handling Route", e); ByteBuf buf = channel.alloc().directBuffer(); buf.writeBytes("Error executing endpoint".getBytes(XrpcConstants.DEFAULT_CHARSET)); writeResponse(ctx, streamId, HttpResponseStatus.INTERNAL_SERVER_ERROR, buf); } } else { requests.put(streamId, request); handlers.put(streamId, handler); } } Http2Handler(Http2ConnectionEncoder encoder, int maxPayloadBytes, Http2CorsHandler corsHandler); @Override int onDataRead(
      ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream); @Override void onHeadersRead(
      ChannelHandlerContext ctx,
      int streamId,
      Http2Headers headers,
      int padding,
      boolean endOfStream); @Override void onHeadersRead(
      ChannelHandlerContext ctx,
      int streamId,
      Http2Headers headers,
      int streamDependency,
      short weight,
      boolean exclusive,
      int padding,
      boolean endOfStream); @Override void onStreamRemoved(Http2Stream stream);  }