Java Code Examples for io.netty.handler.codec.http.HttpUtil#getContentLength()
The following examples show how to use
io.netty.handler.codec.http.HttpUtil#getContentLength() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpStreamsClientHandler.java From aws-sdk-java-v2 with Apache License 2.0 | 6 votes |
@Override protected boolean hasBody(HttpResponse response) { if (response.status().code() >= 100 && response.status().code() < 200) { return false; } if (response.status().equals(HttpResponseStatus.NO_CONTENT) || response.status().equals(HttpResponseStatus.NOT_MODIFIED)) { return false; } if (HttpUtil.isTransferEncodingChunked(response)) { return true; } if (HttpUtil.isContentLengthSet(response)) { return HttpUtil.getContentLength(response) > 0; } return true; }
Example 2
Source File: HttpServerOperations.java From reactor-netty with Apache License 2.0 | 6 votes |
@Override protected HttpMessage newFullBodyMessage(ByteBuf body) { HttpResponse res = new DefaultFullHttpResponse(version(), status(), body); responseHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING); if (!HttpResponseStatus.NOT_MODIFIED.equals(status())) { if (HttpUtil.getContentLength(nettyResponse, -1) == -1) { responseHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, body.readableBytes()); } } res.headers().set(responseHeaders); return res; }
Example 3
Source File: DFHttpSyncHandler.java From dfactor with MIT License | 5 votes |
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { try{ if(msg instanceof FullHttpResponse){ FullHttpResponse rsp = (FullHttpResponse) msg; HttpHeaders headers = rsp.headers(); long contentLen = HttpUtil.getContentLength(rsp); String contentType = headers.get(HttpHeaderNames.CONTENT_TYPE); // DFHttpCliRsp dfRsp = null; ByteBuf buf = rsp.content(); //parse msg boolean isString = contentIsString(contentType); if(isString){ //String String str = null; if(buf != null){ str = (String) buf.readCharSequence(buf.readableBytes(), CharsetUtil.UTF_8); } dfRsp = new DFHttpCliRspWrap(rsp.status().code(), headers, contentType, (int) contentLen, null, str); }else{ //binary dfRsp = new DFHttpCliRspWrap(rsp.status().code(), headers, contentType, (int) contentLen, buf, null); } // _recvData = dfRsp; if(!isString && buf != null){ buf.retain(); } } }finally{ if(_recvPromise != null){ _recvPromise.setSuccess(); } ReferenceCountUtil.release(msg); } }
Example 4
Source File: RoutingHandler.java From riposte with Apache License 2.0 | 5 votes |
private void throwExceptionIfContentLengthHeaderIsLargerThanConfiguredMaxRequestSize(HttpRequest msg, Endpoint<?> endpoint) { int configuredMaxRequestSize = getConfiguredMaxRequestSize(endpoint, globalConfiguredMaxRequestSizeInBytes); if (!isMaxRequestSizeValidationDisabled(configuredMaxRequestSize) && HttpUtil.isContentLengthSet(msg) && HttpUtil.getContentLength(msg) > configuredMaxRequestSize ) { throw new RequestTooBigException( "Content-Length header value exceeded configured max request size of " + configuredMaxRequestSize ); } }
Example 5
Source File: ServerHttp1ObjectEncoder.java From armeria with Apache License 2.0 | 5 votes |
private static void maybeSetTransferEncoding(HttpMessage out) { final io.netty.handler.codec.http.HttpHeaders outHeaders = out.headers(); final long contentLength = HttpUtil.getContentLength(out, -1L); if (contentLength < 0) { // Use chunked encoding. outHeaders.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); outHeaders.remove(HttpHeaderNames.CONTENT_LENGTH); } }
Example 6
Source File: NettyResponseChannel.java From ambry with Apache License 2.0 | 5 votes |
/** * Writes response metadata to the channel if not already written previously and channel is active. * @param responseMetadata the {@link HttpResponse} that needs to be written. * @param listener the {@link GenericFutureListener} that needs to be attached to the write. * @return {@code true} if response metadata was written to the channel in this call. {@code false} otherwise. */ private boolean maybeWriteResponseMetadata(HttpResponse responseMetadata, GenericFutureListener<ChannelFuture> listener) { long writeProcessingStartTime = System.currentTimeMillis(); boolean writtenThisTime = false; if (ctx.channel().isActive() && responseMetadataWriteInitiated.compareAndSet(false, true)) { // we do some manipulation here for chunking. According to the HTTP spec, we can have either a Content-Length // or Transfer-Encoding:chunked, never both. So we check for Content-Length - if it is not there, we add // Transfer-Encoding:chunked on 200 response. Note that sending HttpContent chunks data anyway - we are just // explicitly specifying this in the header. if (!HttpUtil.isContentLengthSet(responseMetadata) && (responseMetadata.status().equals(HttpResponseStatus.OK) || responseMetadata.status().equals(HttpResponseStatus.PARTIAL_CONTENT))) { // This makes sure that we don't stomp on any existing transfer-encoding. HttpUtil.setTransferEncodingChunked(responseMetadata, true); } else if (HttpUtil.isContentLengthSet(responseMetadata) && HttpUtil.getContentLength(responseMetadata) == 0 && !(responseMetadata instanceof FullHttpResponse)) { // if the Content-Length is 0, we can send a FullHttpResponse since there is no content expected. FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseMetadata.status()); fullHttpResponse.headers().set(responseMetadata.headers()); responseMetadata = fullHttpResponse; } logger.trace("Sending response with status {} on channel {}", responseMetadata.status(), ctx.channel()); finalResponseMetadata = responseMetadata; ChannelPromise writePromise = ctx.newPromise().addListener(listener); ctx.writeAndFlush(responseMetadata, writePromise); writtenThisTime = true; long writeProcessingTime = System.currentTimeMillis() - writeProcessingStartTime; nettyMetrics.responseMetadataProcessingTimeInMs.update(writeProcessingTime); if (request != null) { request.getMetricsTracker().nioMetricsTracker.markFirstByteSent(); } } return writtenThisTime; }
Example 7
Source File: NettyResponseChannel.java From ambry with Apache License 2.0 | 5 votes |
/** * Creates a chunk. * @param buffer the {@link ByteBuf} that forms the data of this chunk. * @param callback the {@link Callback} to invoke when {@link #writeCompleteThreshold} is reached. */ Chunk(ByteBuf buffer, Callback<Long> callback) { this.buffer = buffer; bytesToBeWritten = buffer.readableBytes(); this.callback = callback; writeCompleteThreshold = totalBytesReceived.addAndGet(bytesToBeWritten); chunksToWriteCount.incrementAndGet(); // if channel becomes inactive, no one set finalResponseMetadata. It's possible finalResponseMetadata is null. long contentLength = finalResponseMetadata == null ? -1 : HttpUtil.getContentLength(finalResponseMetadata, -1); isLast = contentLength != -1 && writeCompleteThreshold >= contentLength; }
Example 8
Source File: DFHttpCliHandler.java From dfactor with MIT License | 4 votes |
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { hasRsp = true; try{ if(msg instanceof FullHttpResponse){ int actorId = 0; FullHttpResponse rsp = (FullHttpResponse) msg; HttpHeaders headers = rsp.headers(); long contentLen = HttpUtil.getContentLength(rsp); String contentType = headers.get(HttpHeaderNames.CONTENT_TYPE); // DFHttpCliRsp dfRsp = null; ByteBuf buf = rsp.content(); //parse msg boolean isString = contentIsString(contentType); if(isString){ //String String str = null; if(buf != null){ str = (String) buf.readCharSequence(buf.readableBytes(), CharsetUtil.UTF_8); } dfRsp = new DFHttpCliRspWrap(rsp.status().code(), headers, contentType, (int) contentLen, null, str); }else{ //binary dfRsp = new DFHttpCliRspWrap(rsp.status().code(), headers, contentType, (int) contentLen, buf, null); } // Object msgWrap = null; //decode if(decoder != null){ Object tmp = decoder.onDecode(dfRsp); if(tmp != null){ msgWrap = tmp; } } if(msgWrap == null){ //没有解码 msgWrap = dfRsp; if(!isString && buf != null){ buf.retain(); } } //检测分发 if(dispatcher != null){ actorId = dispatcher.onQueryMsgActorId(addrRemote.getPort(), addrRemote, msgWrap); } // if(actorId == 0){ actorId = actorIdDef; } // if(actorId != 0 && msgWrap != null){ //可以后续处理 DFActorManager.get().send(requestId, actorId, 2, DFActorDefine.SUBJECT_NET, DFActorDefine.NET_TCP_MESSAGE, msgWrap, true, session, actorId==actorIdDef?userHandler:null, false); } } }finally{ ReferenceCountUtil.release(msg); ctx.close(); } }
Example 9
Source File: ClientHttp1ObjectEncoder.java From armeria with Apache License 2.0 | 4 votes |
private HttpObject convertHeaders(RequestHeaders headers, boolean endStream) { final String method = headers.method().name(); final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(method), headers.path(), false); final io.netty.handler.codec.http.HttpHeaders nettyHeaders = req.headers(); ArmeriaHttpUtil.toNettyHttp1ClientHeader(headers, nettyHeaders); if (!nettyHeaders.contains(HttpHeaderNames.USER_AGENT)) { nettyHeaders.add(HttpHeaderNames.USER_AGENT, HttpHeaderUtil.USER_AGENT.toString()); } if (!nettyHeaders.contains(HttpHeaderNames.HOST)) { final InetSocketAddress remoteAddress = (InetSocketAddress) channel().remoteAddress(); nettyHeaders.add(HttpHeaderNames.HOST, ArmeriaHttpUtil.authorityHeader(remoteAddress.getHostName(), remoteAddress.getPort(), protocol().defaultPort())); } if (endStream) { nettyHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING); // Set or remove the 'content-length' header depending on request method. // See: https://tools.ietf.org/html/rfc7230#section-3.3.2 // // > A user agent SHOULD send a Content-Length in a request message when // > no Transfer-Encoding is sent and the request method defines a meaning // > for an enclosed payload body. For example, a Content-Length header // > field is normally sent in a POST request even when the value is 0 // > (indicating an empty payload body). A user agent SHOULD NOT send a // > Content-Length header field when the request message does not contain // > a payload body and the method semantics do not anticipate such a // > body. switch (method) { case "POST": case "PUT": case "PATCH": nettyHeaders.set(HttpHeaderNames.CONTENT_LENGTH, "0"); break; default: nettyHeaders.remove(HttpHeaderNames.CONTENT_LENGTH); } } else if (HttpUtil.getContentLength(req, -1L) >= 0) { // Avoid the case where both 'content-length' and 'transfer-encoding' are set. nettyHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING); } else { nettyHeaders.set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); } return req; }
Example 10
Source File: NettyHttpServletRequest.java From cxf with Apache License 2.0 | 4 votes |
@Override public int getContentLength() { return HttpUtil.getContentLength(this.originalRequest, -1); }
Example 11
Source File: NettyResponseChannel.java From ambry with Apache License 2.0 | 4 votes |
@Override public long length() { return HttpUtil.getContentLength(finalResponseMetadata, -1); }
Example 12
Source File: NettyResponseChannelTest.java From ambry with Apache License 2.0 | 4 votes |
/** * Tests the common workflow of the {@link NettyResponseChannel} i.e., add some content to response body via * {@link NettyResponseChannel#write(ByteBuffer, Callback)} and then complete the response. * <p/> * These responses have the header Content-Length set. * @throws Exception */ @Test public void responsesWithContentLengthTest() throws Exception { EmbeddedChannel channel = createEmbeddedChannel(); MockNettyMessageProcessor processor = channel.pipeline().get(MockNettyMessageProcessor.class); final int ITERATIONS = 10; for (int i = 0; i < ITERATIONS; i++) { boolean isKeepAlive = i != (ITERATIONS - 1); HttpHeaders httpHeaders = new DefaultHttpHeaders(); httpHeaders.set(MockNettyMessageProcessor.CHUNK_COUNT_HEADER_NAME, i); HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, TestingUri.ResponseWithContentLength.toString(), httpHeaders); HttpUtil.setKeepAlive(httpRequest, isKeepAlive); channel.writeInbound(httpRequest); verifyCallbacks(processor); // first outbound has to be response. HttpResponse response = channel.readOutbound(); assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status()); long contentLength = HttpUtil.getContentLength(response, -1); assertEquals("Unexpected Content-Length", MockNettyMessageProcessor.CHUNK.length * i, contentLength); if (contentLength == 0) { // special case. Since Content-Length is set, the response should be an instance of FullHttpResponse. assertTrue("Response not instance of FullHttpResponse", response instanceof FullHttpResponse); } else { HttpContent httpContent = null; for (int j = 0; j < i; j++) { httpContent = channel.readOutbound(); byte[] returnedContent = new byte[httpContent.content().readableBytes()]; httpContent.content().readBytes(returnedContent); httpContent.release(); assertArrayEquals("Content does not match with expected content", MockNettyMessageProcessor.CHUNK, returnedContent); } // When we know the content-length, the last httpContent would be an instance of LastHttpContent and there is no // empty last http content following it. // the last HttpContent should also be an instance of LastHttpContent assertTrue("The last part of the content is not LastHttpContent", httpContent instanceof LastHttpContent); } assertEquals("Unexpected channel state on the server", isKeepAlive, channel.isActive()); } }