Search in sources :

Example 36 with LiveHttpRequest

use of com.hotels.styx.api.LiveHttpRequest in project styx by ExpediaGroup.

the class BackendServicesRouterTest method doesNotMatchRequestIfFinalSlashIsMissing.

@Test
public void doesNotMatchRequestIfFinalSlashIsMissing() {
    BackendServicesRouter router = new BackendServicesRouter(serviceClientFactory, environment, executor);
    router.onChange(added(appB().newCopy().path("/appB/hotel/details.html").build()));
    LiveHttpRequest request = get("/ba/").build();
    Optional<HttpHandler> route = router.route(request, context);
    assertThat(route, is(Optional.empty()));
}
Also used : HttpHandler(com.hotels.styx.api.HttpHandler) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Test(org.junit.jupiter.api.Test)

Example 37 with LiveHttpRequest

use of com.hotels.styx.api.LiveHttpRequest in project styx by ExpediaGroup.

the class HttpErrorStatusCauseLoggerTest method logsInternalServerErrorWithRequest.

@Test
public void logsInternalServerErrorWithRequest() {
    LiveHttpRequest request = LiveHttpRequest.get("/foo").build();
    Exception exception = new Exception("This is just a test");
    httpErrorStatusCauseLogger.proxyErrorOccurred(request, InetSocketAddress.createUnresolved("localhost", 80), INTERNAL_SERVER_ERROR, exception);
    assertThat(loggingTestSupport.log(), hasItem(loggingEvent(ERROR, "Failure status=\"500 Internal Server Error\" during request=" + FORMATTED_REQUEST + ", clientAddress=localhost:80", "java.lang.Exception", "This is just a test")));
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Test(org.junit.jupiter.api.Test)

Example 38 with LiveHttpRequest

use of com.hotels.styx.api.LiveHttpRequest in project styx by ExpediaGroup.

the class PathPrefixRouterTest method testRequestRoute.

private void testRequestRoute(PathPrefixRouter router, String path, RoutingObject handler) {
    LiveHttpRequest request = LiveHttpRequest.get(path).build();
    router.handle(request, context);
    verify(handler).handle(request, context);
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest)

Example 39 with LiveHttpRequest

use of com.hotels.styx.api.LiveHttpRequest in project styx by ExpediaGroup.

the class NettyToStyxRequestDecoder method decode.

@Override
protected void decode(ChannelHandlerContext ctx, HttpObject httpObject, List<Object> out) throws Exception {
    if (httpObject.getDecoderResult().isFailure()) {
        String formattedHttpObject = httpMessageFormatter.formatNettyMessage(httpObject);
        throw new BadRequestException("Error while decoding request: " + formattedHttpObject, httpMessageFormatter.wrap(httpObject.getDecoderResult().cause()));
    }
    try {
        if (httpObject instanceof HttpRequest) {
            this.producer = new FlowControllingHttpContentProducer(ctx, this.flowControlEnabled);
            Observable<ByteBuf> contentObservable = Observable.create(contentSubscriber -> {
                contentSubscriber.setProducer(this.producer);
                this.producer.subscriptionStart(contentSubscriber);
            });
            HttpRequest request = (HttpRequest) httpObject;
            LiveHttpRequest styxRequest = toStyxRequest(request, contentObservable);
            out.add(styxRequest);
        } else if (httpObject instanceof HttpContent && this.producer != null) {
            this.producer.onNext(content(httpObject));
            if (httpObject instanceof LastHttpContent) {
                this.producer.onCompleted();
            }
        }
    } catch (BadRequestException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new BadRequestException(ex.getMessage() + " in " + httpObject, ex);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) BadRequestException(com.hotels.styx.server.BadRequestException) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) MalformedURLException(java.net.MalformedURLException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) BadRequestException(com.hotels.styx.server.BadRequestException)

Example 40 with LiveHttpRequest

use of com.hotels.styx.api.LiveHttpRequest in project styx by ExpediaGroup.

the class HttpPipelineHandler method onLegitimateRequest.

private State onLegitimateRequest(LiveHttpRequest request, ChannelHandlerContext ctx) {
    statsSink.onRequest(request.id());
    LiveHttpRequest v11Request = request.newBuilder().version(HTTP_1_1).build();
    tracker.trackRequest(request, () -> this.state().toString());
    ongoingRequest = request;
    // generates a response.
    try {
        Eventual<LiveHttpResponse> responseEventual = httpPipeline.handle(v11Request, new HttpInterceptorContext(this.secure, remoteAddress(ctx), ctx.executor()));
        responseEventual.subscribe(new BaseSubscriber<LiveHttpResponse>() {

            @Override
            public void hookOnSubscribe(Subscription s) {
                subscription = s;
                s.request(1);
            }

            @Override
            public void hookOnComplete() {
                eventProcessor.submit(new ResponseObservableCompletedEvent(ctx, request.id()));
            }

            @Override
            public void hookOnError(Throwable cause) {
                eventProcessor.submit(new ResponseObservableErrorEvent(ctx, cause, request.id()));
            }

            @Override
            public void hookOnNext(LiveHttpResponse response) {
                eventProcessor.submit(new ResponseReceivedEvent(response, ctx));
            }
        });
        return WAITING_FOR_RESPONSE;
    } catch (Throwable cause) {
        LiveHttpResponse response = exceptionToResponse(cause, request, originsHeaderName);
        httpErrorStatusListener.proxyErrorOccurred(request, remoteAddress(ctx), response.status(), cause);
        statsSink.onTerminate(request.id());
        tracker.endTrack(ongoingRequest);
        if (ctx.channel().isActive()) {
            respondAndClose(ctx, response);
        }
        return TERMINATED;
    }
}
Also used : LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) HttpInterceptorContext(com.hotels.styx.server.HttpInterceptorContext) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Subscription(org.reactivestreams.Subscription)

Aggregations

LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)82 Test (org.junit.jupiter.api.Test)76 Condition (com.hotels.styx.server.routing.Condition)19 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)14 HttpHandler (com.hotels.styx.api.HttpHandler)10 MicrometerRegistry (com.hotels.styx.api.MicrometerRegistry)9 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)9 MeterRegistry (com.hotels.styx.api.MeterRegistry)8 BackendService (com.hotels.styx.api.extension.service.BackendService)7 Registry (com.hotels.styx.api.extension.service.spi.Registry)7 Eventual (com.hotels.styx.api.Eventual)6 HttpInterceptor (com.hotels.styx.api.HttpInterceptor)6 HttpResponse (com.hotels.styx.api.HttpResponse)5 Origin.newOriginBuilder (com.hotels.styx.api.extension.Origin.newOriginBuilder)5 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)5 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Mono (reactor.core.publisher.Mono)5 HttpRequestMessageLogger (com.hotels.styx.common.logging.HttpRequestMessageLogger)4 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)4