List of usage examples for io.netty.handler.codec.http2 Http2Headers getLong
long getLong(K name, long defaultValue);
From source file:com.linecorp.armeria.server.http.Http2RequestDecoder.java
License:Apache License
@Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) throws Http2Exception { final HttpHeaders convertedHeaders = ArmeriaHttpUtil.toArmeria(headers); DecodedHttpRequest req = requests.get(streamId); if (req == null) { // Validate the 'content-length' header if exists. if (headers.contains(HttpHeaderNames.CONTENT_LENGTH)) { final long contentLength = headers.getLong(HttpHeaderNames.CONTENT_LENGTH, -1L); if (contentLength < 0) { writeErrorResponse(ctx, streamId, HttpResponseStatus.BAD_REQUEST); return; }// ww w . j a v a2 s . c om } req = new DecodedHttpRequest(ctx.channel().eventLoop(), ++nextId, streamId, convertedHeaders, true, inboundTrafficController); requests.put(streamId, req); ctx.fireChannelRead(req); } else { try { req.write(convertedHeaders); } catch (Throwable t) { req.close(t); throw connectionError(INTERNAL_ERROR, t, "failed to consume a HEADERS frame"); } } if (endOfStream) { req.close(); } }
From source file:com.linecorp.armeria.server.Http2RequestDecoder.java
License:Apache License
@Override public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, boolean endOfStream) throws Http2Exception { DecodedHttpRequest req = requests.get(streamId); if (req == null) { // Validate the method. final CharSequence method = headers.method(); if (method == null) { writeErrorResponse(ctx, streamId, HttpResponseStatus.BAD_REQUEST); return; }/*from w w w. j a v a2 s .com*/ if (!HttpMethod.isSupported(method.toString())) { writeErrorResponse(ctx, streamId, HttpResponseStatus.METHOD_NOT_ALLOWED); return; } // Validate the 'content-length' header if exists. final boolean contentEmpty; if (headers.contains(HttpHeaderNames.CONTENT_LENGTH)) { final long contentLength = headers.getLong(HttpHeaderNames.CONTENT_LENGTH, -1L); if (contentLength < 0) { writeErrorResponse(ctx, streamId, HttpResponseStatus.BAD_REQUEST); return; } contentEmpty = contentLength == 0; } else { contentEmpty = true; } req = new DecodedHttpRequest(ctx.channel().eventLoop(), ++nextId, streamId, ArmeriaHttpUtil.toArmeria(headers), true, inboundTrafficController, cfg.defaultMaxRequestLength()); // Close the request early when it is sure that there will be // neither content nor trailing headers. if (contentEmpty && endOfStream) { req.close(); } requests.put(streamId, req); ctx.fireChannelRead(req); } else { try { req.write(ArmeriaHttpUtil.toArmeria(headers)); } catch (Throwable t) { req.close(t); throw connectionError(INTERNAL_ERROR, t, "failed to consume a HEADERS frame"); } } if (endOfStream) { req.close(); } }