Java源码示例:io.netty.handler.codec.http.HttpObjectDecoder

示例1
protected static int determineHttpClientCodecInboundState(HttpClientCodec currentCodec) {
    try {
        HttpObjectDecoder decoder = (HttpObjectDecoder) httpClientCodecInboundHandlerField.get(currentCodec);
        return ((Enum) httpObjectDecoderCurrentStateField.get(decoder)).ordinal();
    }
    catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
示例2
@Test
public void httpServerRequestConfigInjectAttributes() {
	AtomicReference<Channel> channelRef = new AtomicReference<>();
	AtomicReference<Boolean> validate = new AtomicReference<>();
	AtomicReference<Integer> chunkSize = new AtomicReference<>();
	HttpServer server =
			HttpServer.create()
			          .httpRequestDecoder(opt -> opt.maxInitialLineLength(123)
			                                        .maxHeaderSize(456)
			                                        .maxChunkSize(789)
			                                        .validateHeaders(false)
			                                        .initialBufferSize(10))
			          .handle((req, resp) -> req.receive().then(resp.sendNotFound()))
			          .doOnConnection(c -> {
			                      channelRef.set(c.channel());
			                      HttpServerCodec codec = c.channel()
			                                               .pipeline()
			                                               .get(HttpServerCodec.class);
			                      HttpObjectDecoder decoder = (HttpObjectDecoder) getValueReflection(codec, "inboundHandler", 1);
			                      chunkSize.set((Integer) getValueReflection(decoder, "maxChunkSize", 2));
			                      validate.set((Boolean) getValueReflection(decoder, "validateHeaders", 2));
			                  })
			          .wiretap(true);

	disposableServer = server.bindNow();

	HttpClient.create()
	          .remoteAddress(disposableServer::address)
	          .post()
	          .uri("/")
	          .send(ByteBufFlux.fromString(Mono.just("bodysample")))
	          .responseContent()
	          .aggregate()
	          .asString()
	          .block();

	assertThat(channelRef.get()).isNotNull();
	assertThat(chunkSize.get()).as("line length").isEqualTo(789);
	assertThat(validate.get()).as("validate headers").isFalse();
}
 
示例3
@Test
public void httpClientResponseConfigInjectAttributes() {
	AtomicReference<Channel> channelRef = new AtomicReference<>();
	AtomicReference<Boolean> validate = new AtomicReference<>();
	AtomicReference<Integer> chunkSize = new AtomicReference<>();

	disposableServer =
			HttpServer.create()
			          .handle((req, resp) -> req.receive()
			                                    .then(resp.sendNotFound()))
			          .wiretap(true)
			          .bindNow();

	createHttpClientForContextWithAddress()
	        .httpResponseDecoder(opt -> opt.maxInitialLineLength(123)
	                                       .maxHeaderSize(456)
	                                       .maxChunkSize(789)
	                                       .validateHeaders(false)
	                                       .initialBufferSize(10)
	                                       .failOnMissingResponse(true)
	                                       .parseHttpAfterConnectRequest(true))
	        .doOnConnected(c -> {
	                    channelRef.set(c.channel());
	                    HttpClientCodec codec = c.channel()
	                                             .pipeline()
	                                             .get(HttpClientCodec.class);
	                    HttpObjectDecoder decoder = (HttpObjectDecoder) getValueReflection(codec, "inboundHandler", 1);
	                    chunkSize.set((Integer) getValueReflection(decoder, "maxChunkSize", 2));
	                    validate.set((Boolean) getValueReflection(decoder, "validateHeaders", 2));
	                })
	        .post()
	        .uri("/")
	        .send(ByteBufFlux.fromString(Mono.just("bodysample")))
	        .responseContent()
	        .aggregate()
	        .asString()
	        .block(Duration.ofSeconds(30));

	assertThat(channelRef.get()).isNotNull();

	assertThat(chunkSize.get()).as("line length").isEqualTo(789);
	assertThat(validate.get()).as("validate headers").isFalse();
}