Java Code Examples for io.netty.handler.codec.http.HttpUtil#isTransferEncodingChunked()

The following examples show how to use io.netty.handler.codec.http.HttpUtil#isTransferEncodingChunked() . 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 vote down vote up
@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: HttpOperations.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
@Override
public final NettyOutbound sendFile(Path file, long position, long count) {
	Objects.requireNonNull(file);

	if (hasSentHeaders()) {
		return super.sendFile(file, position, count);
	}

	if (!HttpUtil.isTransferEncodingChunked(outboundHttpMessage()) && !HttpUtil.isContentLengthSet(
			outboundHttpMessage()) && count < Integer.MAX_VALUE) {
		outboundHttpMessage().headers()
		                     .setInt(HttpHeaderNames.CONTENT_LENGTH, (int) count);
	}
	else if (!HttpUtil.isContentLengthSet(outboundHttpMessage())) {
		outboundHttpMessage().headers()
		                     .remove(HttpHeaderNames.CONTENT_LENGTH)
		                     .remove(HttpHeaderNames.TRANSFER_ENCODING);
		HttpUtil.setTransferEncodingChunked(outboundHttpMessage(), true);
	}

	return super.sendFile(file, position, count);
}
 
Example 3
Source File: HttpUploadClientHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
        }

        if (response.status().code() == 200 && HttpUtil.isTransferEncodingChunked(response)) {
            readingChunks = true;
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent chunk = (HttpContent) msg;
        System.err.println(chunk.content().toString(CharsetUtil.UTF_8));

        if (chunk instanceof LastHttpContent) {
            if (readingChunks) {
                System.err.println("} END OF CHUNKED CONTENT");
            } else {
                System.err.println("} END OF CONTENT");
            }
            readingChunks = false;
        } else {
            System.err.println(chunk.content().toString(CharsetUtil.UTF_8));
        }
    }
}
 
Example 4
Source File: HttpSnoopClientHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();

        if (!response.headers().isEmpty()) {
            for (CharSequence name: response.headers().names()) {
                for (CharSequence value: response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
            System.err.println();
        }

        if (HttpUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            ctx.close();
        }
    }
}
 
Example 5
Source File: HttpResponseClientHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.out.println("STATUS: " + response.status());
        System.out.println("VERSION: " + response.protocolVersion());
        System.out.println();

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.out.println("HEADER: " + name + " = " + value);
                }
            }
            System.out.println();
        }

        if (HttpUtil.isTransferEncodingChunked(response)) {
            System.out.println("CHUNKED CONTENT {");
        } else {
            System.out.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.out.print(content.content().toString(CharsetUtil.UTF_8));
        System.out.flush();

        if (content instanceof LastHttpContent) {
            System.out.println("} END OF CONTENT");
            queue.add(ctx.channel().newSucceededFuture());
        }
    }
}
 
Example 6
Source File: HttpUploadClientHandler.java    From tools-journey with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
        }

        if (response.status().code() == 200 && HttpUtil.isTransferEncodingChunked(response)) {
            readingChunks = true;
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent chunk = (HttpContent) msg;
        System.err.println(chunk.content().toString(CharsetUtil.UTF_8));

        if (chunk instanceof LastHttpContent) {
            if (readingChunks) {
                System.err.println("} END OF CHUNKED CONTENT");
            } else {
                System.err.println("} END OF CONTENT");
            }
            readingChunks = false;
        } else {
            System.err.println(chunk.content().toString(CharsetUtil.UTF_8));
        }
    }
}
 
Example 7
Source File: HttpSnoopClientHandler.java    From tools-journey with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();

        if (!response.headers().isEmpty()) {
            for (CharSequence name: response.headers().names()) {
                for (CharSequence value: response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                }
            }
            System.err.println();
        }

        if (HttpUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;

        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            ctx.close();
        }
    }
}
 
Example 8
Source File: HttpSender.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
 if (HttpUtil.isTransferEncodingChunked(res)){
  sendChunkedHttpResponse(ctx,req,res);
 } else {
  sendHttpResponseFull(ctx,req,res);
 }
}
 
Example 9
Source File: PublicAccessLogHandler.java    From ambry with Apache License 2.0 5 votes vote down vote up
/**
 * Appends specified headers to the log message if those headers are not null
 * @param tag pretty name for set of headers to append
 * @param message http message from which to log headers
 * @param headers array of headers to log
 */
private void logHeaders(String tag, HttpMessage message, String[] headers) {
  logMessage.append(tag).append(" (");
  for (String header : headers) {
    if (message.headers().get(header) != null) {
      logMessage.append("[").append(header).append("=").append(message.headers().get(header)).append("] ");
    }
  }
  boolean isChunked = HttpUtil.isTransferEncodingChunked(message);
  logMessage.append("[isChunked=").append(isChunked).append("]");
  logMessage.append(")");
}
 
Example 10
Source File: PublicAccessLogHandler.java    From ambry with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
  long startTimeInMs = System.currentTimeMillis();
  boolean shouldReset = msg instanceof LastHttpContent;
  if (request != null) {
    if (msg instanceof HttpResponse) {
      HttpResponse response = (HttpResponse) msg;
      logHeaders("Response", response, publicAccessLogger.getResponseHeaders());
      logMessage.append(", ");
      logMessage.append("status=").append(response.status().code());
      logMessage.append(", ");
      if (HttpUtil.isTransferEncodingChunked(response)) {
        responseFirstChunkStartTimeInMs = System.currentTimeMillis();
      } else {
        shouldReset = true;
      }
    } else if (!(msg instanceof HttpContent)) {
      logger.error(
          "Sending response that is not of type HttpResponse or HttpContent. Sending response to {}. Request is of type {}. No action being taken other than logging this unexpected state.",
          ctx.channel().remoteAddress(), msg.getClass());
    }
    if (shouldReset) {
      logDurations();
      publicAccessLogger.logInfo(logMessage.toString());
      reset();
    }
  }
  nettyMetrics.publicAccessLogResponseProcessingTimeInMs.update(System.currentTimeMillis() - startTimeInMs);
  super.write(ctx, msg, promise);
}
 
Example 11
Source File: HttpServerOperations.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public HttpServerOperations chunkedTransfer(boolean chunked) {
	if (!hasSentHeaders() && HttpUtil.isTransferEncodingChunked(nettyResponse) != chunked) {
		responseHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
		HttpUtil.setTransferEncodingChunked(nettyResponse, chunked);
	}
	return this;
}
 
Example 12
Source File: AccessLogHandler.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
	if (msg instanceof HttpResponse) {
		final HttpResponse response = (HttpResponse) msg;
		final HttpResponseStatus status = response.status();

		if (status.equals(HttpResponseStatus.CONTINUE)) {
			//"FutureReturnValueIgnored" this is deliberate
			ctx.write(msg, promise);
			return;
		}

		final boolean chunked = HttpUtil.isTransferEncodingChunked(response);
		accessLog.status(status.codeAsText())
		         .chunked(chunked);
		if (!chunked) {
			accessLog.contentLength(HttpUtil.getContentLength(response, -1));
		}
	}
	if (msg instanceof LastHttpContent) {
		accessLog.increaseContentLength(((LastHttpContent) msg).content().readableBytes());
		ctx.write(msg, promise.unvoid())
		   .addListener(future -> {
		       if (future.isSuccess()) {
		           accessLog.log();
		       }
		   });
		return;
	}
	if (msg instanceof ByteBuf) {
		accessLog.increaseContentLength(((ByteBuf) msg).readableBytes());
	}
	if (msg instanceof ByteBufHolder) {
		accessLog.increaseContentLength(((ByteBufHolder) msg).content().readableBytes());
	}
	//"FutureReturnValueIgnored" this is deliberate
	ctx.write(msg, promise);
}
 
Example 13
Source File: ClientResponseWriter.java    From zuul with Apache License 2.0 4 votes vote down vote up
private HttpResponse buildHttpResponse(final HttpResponseMessage zuulResp) {
    final HttpRequestInfo zuulRequest = zuulResp.getInboundRequest();
    HttpVersion responseHttpVersion;
    final String inboundProtocol = zuulRequest.getProtocol();
    if (inboundProtocol.startsWith("HTTP/1")) {
        responseHttpVersion = HttpVersion.valueOf(inboundProtocol);
    }
    else {
        // Default to 1.1. We do this to cope with HTTP/2 inbound requests.
        responseHttpVersion = HttpVersion.HTTP_1_1;
    }

    // Create the main http response to send, with body.
    final DefaultHttpResponse nativeResponse = new DefaultHttpResponse(responseHttpVersion,
            HttpResponseStatus.valueOf(zuulResp.getStatus()), false, false);

    // Now set all of the response headers - note this is a multi-set in keeping with HTTP semantics
    final HttpHeaders nativeHeaders = nativeResponse.headers();
    for (Header entry : zuulResp.getHeaders().entries()) {
        nativeHeaders.add(entry.getKey(), entry.getValue());
    }

    // Netty does not automatically add Content-Length or Transfer-Encoding: chunked. So we add here if missing.
    if (! HttpUtil.isContentLengthSet(nativeResponse) && ! HttpUtil.isTransferEncodingChunked(nativeResponse)) {
        nativeResponse.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    }

    final HttpRequest nativeReq = (HttpRequest) zuulResp.getContext().get(CommonContextKeys.NETTY_HTTP_REQUEST);
    if (!closeConnection && HttpUtil.isKeepAlive(nativeReq)) {
        HttpUtil.setKeepAlive(nativeResponse, true);
    } else {
        // Send a Connection: close response header (only needed for HTTP/1.0 but no harm in doing for 1.1 too).
        nativeResponse.headers().set("Connection", "close");
    }

    // TODO - temp hack for http/2 handling.
    if (nativeReq.headers().contains(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text())) {
        String streamId = nativeReq.headers().get(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
        nativeResponse.headers().set(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), streamId);
    }

    return nativeResponse;
}
 
Example 14
Source File: NettyHttpClientHandler.java    From jus with Apache License 2.0 4 votes vote down vote up
public void messageReceived(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();

        builder.setStatusCode(response.status().code());

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                    if (name.toString().equalsIgnoreCase("content-length")) {
                        len = Integer.parseInt(value.toString());
                        System.err.println("LEN: " + len);
                    }
                    builder.setHeader(name.toString(), value.toString());
                }
            }
            System.err.println();
        }

        if (HttpUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }

    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        if (!content.content().hasArray()) {
            try {
                builder.setBody(AbstractHttpStack.getContentBytes(
                        new ByteBufInputStream(content.content(), len), byteArrayPool,
                        len));
            } catch (IOException e) {
                e.printStackTrace();
                exceptionCaught(ctx, e);
            }
        } else {
            builder.setBody(content.content().array());
        }

        content.content().resetReaderIndex();
        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            result = builder.build();
            resultReceived = true;
            ctx.close();
            synchronized (this) {
                this.notifyAll();
            }
        }
    }
}
 
Example 15
Source File: HttpClientOperations.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
void _subscribe(CoreSubscriber<? super Void> s) {
	HttpDataFactory df = DEFAULT_FACTORY;

	try {
		HttpClientFormEncoder encoder = new HttpClientFormEncoder(df,
				parent.nettyRequest,
				false,
				HttpConstants.DEFAULT_CHARSET,
				HttpPostRequestEncoder.EncoderMode.RFC1738);

		formCallback.accept(parent, encoder);

		encoder = encoder.applyChanges(parent.nettyRequest);
		df = encoder.newFactory;

		if (!encoder.isMultipart()) {
			parent.requestHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
		}

		// Returned value is deliberately ignored
		parent.addHandlerFirst(NettyPipeline.ChunkedWriter, new ChunkedWriteHandler());

		boolean chunked = HttpUtil.isTransferEncodingChunked(parent.nettyRequest);

		HttpRequest r = encoder.finalizeRequest();

		if (!chunked) {
			HttpUtil.setTransferEncodingChunked(r, false);
			HttpUtil.setContentLength(r, encoder.length());
		}

		ChannelFuture f = parent.channel()
		                        .writeAndFlush(r);

		Flux<Long> tail = encoder.progressFlux.onBackpressureLatest();

		if (encoder.cleanOnTerminate) {
			tail = tail.doOnCancel(encoder)
			           .doAfterTerminate(encoder);
		}

		if (encoder.isChunked()) {
			if (progressCallback != null) {
				progressCallback.accept(tail);
			}
			//"FutureReturnValueIgnored" this is deliberate
			parent.channel()
			      .writeAndFlush(encoder);
		}
		else {
			if (progressCallback != null) {
				progressCallback.accept(FutureMono.from(f)
				                                  .cast(Long.class)
				                                  .switchIfEmpty(Mono.just(encoder.length()))
				                                  .flux());
			}
		}
		s.onComplete();


	}
	catch (Throwable e) {
		Exceptions.throwIfJvmFatal(e);
		df.cleanRequestHttpData(parent.nettyRequest);
		s.onError(Exceptions.unwrap(e));
	}
}
 
Example 16
Source File: HttpClientOperations.java    From reactor-netty with Apache License 2.0 4 votes vote down vote up
public void chunkedTransfer(boolean chunked) {
	if (!hasSentHeaders() && HttpUtil.isTransferEncodingChunked(nettyRequest) != chunked) {
		requestHeaders.remove(HttpHeaderNames.TRANSFER_ENCODING);
		HttpUtil.setTransferEncodingChunked(nettyRequest, chunked);
	}
}
 
Example 17
Source File: HttpClientHandler.java    From protools with Apache License 2.0 4 votes vote down vote up
@Override
public void channelRead0(final ChannelHandlerContext ctx, final HttpObject msg) {
    if (msg instanceof FullHttpResponse) {
        final FullHttpResponse response = (FullHttpResponse) msg;

        httpReceive.setStatusCode(response.status().code())
                .setStatusText(response.status().codeAsText().toString());

        HttpHeaders headers = response.headers();
        if (httpSend.getNeedReceiveHeaders() && !headers.isEmpty()) {

            final Map<String, String> responseHeaderMap = Maps.newHashMapWithExpectedSize(headers.size());

            headers.forEach(one -> {
                responseHeaderMap.put(one.getKey(), one.getValue());
            });

            httpReceive.setResponseHeader(responseHeaderMap);
        }

        if (HttpUtil.isTransferEncodingChunked(response)) {
            if (log.isDebugEnabled()) {
                log.debug("#HTTP 内容开始{");
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("#HTTP 内容开始{");
            }
        }

        final String responseBody = response.content().toString(httpSend.getCharset());

        httpReceive.setResponseBody(responseBody);

        if (log.isDebugEnabled()) {
            log.debug(responseBody);
        }

        if (log.isDebugEnabled()) {
            log.debug("}EOF#");
        }

        final DecoderResult decoderResult = response.decoderResult();
        if (decoderResult.isFailure()) {
            Throwable cause = decoderResult.cause();
            if (log.isErrorEnabled()) {
                log.error(ToolFormat.toException(cause), cause);
            }
            httpReceive.setHaveError(true)
                    .setErrMsg(cause.getMessage())
                    .setThrowable(cause);
        } else if (response.status().code() != 200) {
            httpReceive.setHaveError(true)
                    .setErrMsg("本次请求响应码不是200,是" + response.status().code());
        }

        httpReceive.setIsDone(true);
        ctx.close();
    }
}