Search in sources :

Example 36 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project reactor-netty by reactor.

the class HttpClientWithTomcatTest method simpleClientPooling.

@Test
void simpleClientPooling() {
    ConnectionProvider p = ConnectionProvider.create("simpleClientPooling", 1);
    AtomicReference<Channel> ch1 = new AtomicReference<>();
    AtomicReference<Channel> ch2 = new AtomicReference<>();
    HttpResponseStatus r = HttpClient.create(p).doOnResponse((res, c) -> ch1.set(c.channel())).wiretap(true).get().uri(getURL() + "/status/404").responseSingle((res, buf) -> buf.thenReturn(res.status())).block(Duration.ofSeconds(30));
    HttpClient.create(p).doOnResponse((res, c) -> ch2.set(c.channel())).wiretap(true).get().uri(getURL() + "/status/404").responseSingle((res, buf) -> buf.thenReturn(res.status())).block(Duration.ofSeconds(30));
    AtomicBoolean same = new AtomicBoolean();
    same.set(ch1.get() == ch2.get());
    assertThat(same.get()).isTrue();
    assertThat(r).isEqualTo(HttpResponseStatus.NOT_FOUND);
    p.dispose();
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) Tuple2(reactor.util.function.Tuple2) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) ByteBufFlux(reactor.netty.ByteBufFlux) TomcatServer(reactor.netty.TomcatServer) Duration(java.time.Duration) Path(java.nio.file.Path) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Files(java.nio.file.Files) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Mono(reactor.core.publisher.Mono) Channel(io.netty.channel.Channel) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) Paths(java.nio.file.Paths) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) InputStream(java.io.InputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConnectionProvider(reactor.netty.resources.ConnectionProvider) Test(org.junit.jupiter.api.Test)

Example 37 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project reactor-netty by reactor.

the class HttpRedirectTest method redirect_issuesOnRequestForEachAttempt.

/**
 * This ensures functionality such as metrics and tracing can accurately count requests.
 */
@Test
void redirect_issuesOnRequestForEachAttempt() {
    disposableServer = createServer().host("localhost").route(r -> r.get("/1", (req, res) -> res.sendRedirect("/3")).get("/3", (req, res) -> res.status(200).sendString(Mono.just("OK")))).bindNow();
    AtomicInteger onRequestCount = new AtomicInteger();
    AtomicInteger onResponseCount = new AtomicInteger();
    AtomicInteger onRedirectCount = new AtomicInteger();
    AtomicInteger doOnResponseError = new AtomicInteger();
    Tuple2<String, HttpResponseStatus> response = createClient(disposableServer::address).followRedirect(true).doOnRequest((r, c) -> onRequestCount.incrementAndGet()).doOnResponse((r, c) -> onResponseCount.incrementAndGet()).doOnRedirect((r, c) -> onRedirectCount.incrementAndGet()).doOnResponseError((r, t) -> doOnResponseError.incrementAndGet()).get().uri("/1").responseSingle((res, bytes) -> bytes.asString().zipWith(Mono.just(res.status()))).block(Duration.ofSeconds(30));
    assertThat(response).isNotNull();
    assertThat(response.getT1()).isEqualTo("OK");
    assertThat(response.getT2()).isEqualTo(HttpResponseStatus.OK);
    assertThat(onRequestCount.get()).isEqualTo(2);
    assertThat(onResponseCount.get()).isEqualTo(1);
    assertThat(onRedirectCount.get()).isEqualTo(1);
    assertThat(doOnResponseError.get()).isEqualTo(0);
}
Also used : StepVerifier(reactor.test.StepVerifier) Http11SslContextSpec(reactor.netty.http.Http11SslContextSpec) SocketUtils(reactor.netty.SocketUtils) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) HttpProtocol(reactor.netty.http.HttpProtocol) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Http2SslContextSpec(reactor.netty.http.Http2SslContextSpec) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseHttpTest(reactor.netty.BaseHttpTest) Tuple2(reactor.util.function.Tuple2) AtomicReference(java.util.concurrent.atomic.AtomicReference) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) InsecureTrustManagerFactory(io.netty.handler.ssl.util.InsecureTrustManagerFactory) DomainSocketAddress(io.netty.channel.unix.DomainSocketAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) Duration(java.time.Duration) Connection(reactor.netty.Connection) LoopResources(reactor.netty.resources.LoopResources) HttpContent(io.netty.handler.codec.http.HttpContent) Assumptions.assumeThat(org.assertj.core.api.Assumptions.assumeThat) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BlockingQueue(java.util.concurrent.BlockingQueue) Mono(reactor.core.publisher.Mono) CertificateException(java.security.cert.CertificateException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) HttpServer(reactor.netty.http.server.HttpServer) SslHandler(io.netty.handler.ssl.SslHandler) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) DisposableServer(reactor.netty.DisposableServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 38 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project reactor-netty by reactor.

the class HttpClientOperationsTest method testStatus.

@Test
void testStatus() {
    doTestStatus(HttpResponseStatus.OK);
    doTestStatus(new HttpResponseStatus(200, "Some custom reason phrase for 200 status code"));
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.jupiter.api.Test)

Example 39 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project apollo by apollo-rsps.

the class HttpRequestWorker method service.

@Override
protected void service(ResourceProvider provider, Channel channel, HttpRequest request) throws IOException {
    String path = request.getUri();
    Optional<ByteBuffer> buf = provider.get(path);
    HttpResponseStatus status = HttpResponseStatus.OK;
    String mime = getMimeType(request.getUri());
    if (!buf.isPresent()) {
        status = HttpResponseStatus.NOT_FOUND;
        mime = "text/html";
    }
    ByteBuf wrapped = buf.isPresent() ? Unpooled.wrappedBuffer(buf.get()) : createErrorPage(status, "The page you requested could not be found.");
    HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), status);
    response.headers().set("Date", new Date());
    response.headers().set("Server", SERVER_IDENTIFIER);
    response.headers().set("Content-type", mime + ", charset=" + CHARACTER_SET.name());
    response.headers().set("Cache-control", "no-cache");
    response.headers().set("Pragma", "no-cache");
    response.headers().set("Expires", new Date(0));
    response.headers().set("Connection", "close");
    response.headers().set("Content-length", wrapped.readableBytes());
    channel.write(response);
    channel.writeAndFlush(wrapped).addListener(ChannelFutureListener.CLOSE);
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) Date(java.util.Date)

Example 40 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project turbo-rpc by hank-whu.

the class RestHttResponseEncoder method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (!(msg instanceof RestHttpResponse)) {
        ctx.write(msg, promise);
        return;
    }
    RestHttpResponse restHttpResponse = (RestHttpResponse) msg;
    HttpResponseStatus status = restHttpResponse.getStatus();
    if (status == null) {
        status = INTERNAL_SERVER_ERROR;
    }
    if (restHttpResponse.getResult() == null) {
        status = INTERNAL_SERVER_ERROR;
        restHttpResponse.setStatus(status);
        restHttpResponse.setResult("UNKNOWN");
    }
    doResponse(ctx, promise, restHttpResponse);
}
Also used : RestHttpResponse(rpc.turbo.transport.server.rest.protocol.RestHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus)

Aggregations

HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)73 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)16 ByteBuf (io.netty.buffer.ByteBuf)15 HttpMethod (io.netty.handler.codec.http.HttpMethod)11 IOException (java.io.IOException)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)10 HttpResponse (io.netty.handler.codec.http.HttpResponse)10 HttpVersion (io.netty.handler.codec.http.HttpVersion)9 Map (java.util.Map)8 URI (java.net.URI)7 Test (org.junit.Test)7 URISyntaxException (java.net.URISyntaxException)6 Test (org.junit.jupiter.api.Test)6 Channel (io.netty.channel.Channel)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)5 Duration (java.time.Duration)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4