Search in sources :

Example 6 with HttpResponseMetaData

use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.

the class AbstractHttpConnectionTest method requestShouldInsertLastPayloadChunkInRequestPayloadWhenMissing.

@SuppressWarnings("unchecked")
@Test
void requestShouldInsertLastPayloadChunkInRequestPayloadWhenMissing() throws Exception {
    Buffer chunk1 = allocator.fromAscii("test");
    Buffer chunk2 = allocator.fromAscii("payload");
    StreamingHttpRequest req = newRequest(GET, "/foo", HTTP_1_1, headersFactory.newHeaders(), allocator, headersFactory).payloadBody(// NO chunk3 here!
    from(chunk1, chunk2));
    HttpResponseMetaData respMeta = newResponseMetaData(HTTP_1_1, OK, headersFactory.newHeaders().add(CONTENT_TYPE, TEXT_PLAIN));
    Buffer chunk3 = allocator.fromAscii("payload");
    HttpHeaders trailers = headersFactory.newEmptyTrailers();
    Publisher<Object> respFlat = from(respMeta, chunk1, chunk2, chunk3, trailers);
    ArgumentCaptor<Publisher<Object>> reqFlatCaptor = ArgumentCaptor.forClass(Publisher.class);
    when(reqResp.apply(reqFlatCaptor.capture())).thenReturn(respFlat);
    Single<StreamingHttpResponse> responseSingle = http.request(req);
    StreamingHttpResponse resp = awaitIndefinitelyNonNull(responseSingle);
    List<Object> objects = awaitIndefinitelyNonNull(reqFlatCaptor.getValue());
    // User provided chunks
    assertThat(objects.subList(0, 3), contains(req, chunk1, chunk2));
    // Ensure new Last chunk inserted
    assertThat(objects.get(3), instanceOf(HttpHeaders.class));
    assertThat(resp.status(), equalTo(OK));
    assertThat(resp.version(), equalTo(HTTP_1_1));
    assertThat(resp.headers().get(CONTENT_TYPE), equalTo(TEXT_PLAIN));
    assertThat(resp.payloadBody().toFuture().get(), contains(chunk1, chunk2, chunk3));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Publisher(io.servicetalk.concurrent.api.Publisher) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 7 with HttpResponseMetaData

use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.

the class H2ToStH1ServerDuplexHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    if (msg instanceof HttpResponseMetaData) {
        HttpResponseMetaData metaData = (HttpResponseMetaData) msg;
        boolean realResponse = !isInterim(metaData.status());
        if (realResponse) {
            // Notify the CloseHandler only about "real" responses. We don't expose 1xx "interim responses" to the
            // user, and handle them internally.
            responseSent = true;
            closeHandler.protocolPayloadBeginOutbound(ctx);
        } else if (responseSent) {
            // Discard an "interim response" if it arrives after a "real response" is already sent.
            return;
        }
        Http2Headers h2Headers = h1HeadersToH2Headers(metaData.headers());
        h2Headers.status(metaData.status().codeAsCharSequence());
        writeMetaData(ctx, metaData, h2Headers, realResponse, promise);
    } else if (msg instanceof Buffer) {
        writeBuffer(ctx, (Buffer) msg, promise);
    } else if (msg instanceof HttpHeaders) {
        writeTrailers(ctx, msg, promise);
    } else {
        ctx.write(msg, promise);
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) Http2Headers(io.netty.handler.codec.http2.Http2Headers) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData)

Example 8 with HttpResponseMetaData

use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.

the class AbstractHttpConnectionTest method requestShouldWriteFlatStreamToConnectionAndReadFlatStreamSplicedIntoResponseAndPayload.

@SuppressWarnings("unchecked")
@Test
void requestShouldWriteFlatStreamToConnectionAndReadFlatStreamSplicedIntoResponseAndPayload() throws Exception {
    Buffer chunk1 = allocator.fromAscii("test");
    Buffer chunk2 = allocator.fromAscii("payload");
    Buffer chunk3 = allocator.fromAscii("payload");
    HttpHeaders trailers = headersFactory.newEmptyTrailers();
    HttpHeaders headers = headersFactory.newHeaders();
    headers.add(TRANSFER_ENCODING, CHUNKED);
    StreamingHttpRequest req = newTransportRequest(GET, "/foo", HTTP_1_1, headers, allocator, from(chunk1, chunk2, chunk3, trailers), false, headersFactory);
    HttpResponseMetaData respMeta = newResponseMetaData(HTTP_1_1, OK, INSTANCE.newHeaders().add(CONTENT_TYPE, TEXT_PLAIN));
    Publisher<Object> respFlat = from(respMeta, chunk1, chunk2, chunk3, trailers);
    ArgumentCaptor<Publisher<Object>> reqFlatCaptor = ArgumentCaptor.forClass(Publisher.class);
    when(reqResp.apply(reqFlatCaptor.capture())).thenReturn(respFlat);
    Single<StreamingHttpResponse> responseSingle = http.request(req);
    StreamingHttpResponse resp = awaitIndefinitelyNonNull(responseSingle);
    assertThat(reqFlatCaptor.getValue().toFuture().get(), contains(req, chunk1, chunk2, chunk3, trailers));
    assertThat(resp.status(), equalTo(OK));
    assertThat(resp.version(), equalTo(HTTP_1_1));
    assertThat(resp.headers().get(CONTENT_TYPE), equalTo(TEXT_PLAIN));
    assertThat(resp.payloadBody().toFuture().get(), contains(chunk1, chunk2, chunk3));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Publisher(io.servicetalk.concurrent.api.Publisher) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Test(org.junit.jupiter.api.Test)

Example 9 with HttpResponseMetaData

use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.

the class HttpObjectDecoder method resetNow.

/**
 * Resets the state of the decoder to prepare it for parsing a new incoming message.
 * <p>
 * Subclasses that override this method have to invoke this implementation using {@code super} keyword.
 */
protected void resetNow() {
    T message = this.message;
    this.message = null;
    this.trailer = null;
    contentLength = Long.MIN_VALUE;
    parsingLine = 0;
    cumulationIndex = -1;
    if (!isDecodingRequest()) {
        HttpResponseMetaData res = (HttpResponseMetaData) message;
        if (res != null && isSwitchingToNonHttp1Protocol(res)) {
            currentState = State.UPGRADED;
            return;
        }
    }
    currentState = State.SKIP_CONTROL_CHARS;
    skippedControls = 0;
}
Also used : HT(io.netty.handler.codec.http.HttpConstants.HT) GET(io.servicetalk.http.api.HttpRequestMethod.GET) HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData)

Example 10 with HttpResponseMetaData

use of io.servicetalk.http.api.HttpResponseMetaData in project servicetalk by apple.

the class HttpResponseDecoderTest method assertResponseLine.

private static HttpResponseMetaData assertResponseLine(HttpProtocolVersion expectedVersion, HttpResponseStatus expectedStatus, EmbeddedChannel channel) {
    HttpResponseMetaData response = channel.readInbound();
    assertThat(response.version(), equalTo(expectedVersion));
    assertThat(response.status().code(), is(expectedStatus.code()));
    assertThat(response.status().reasonPhrase(), equalTo(expectedStatus.reasonPhrase()));
    return response;
}
Also used : HttpResponseMetaData(io.servicetalk.http.api.HttpResponseMetaData)

Aggregations

HttpResponseMetaData (io.servicetalk.http.api.HttpResponseMetaData)17 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)11 Buffer (io.servicetalk.buffer.api.Buffer)11 Test (org.junit.jupiter.api.Test)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 HttpHeaders (io.servicetalk.http.api.HttpHeaders)7 EmptyHttpHeaders (io.servicetalk.http.api.EmptyHttpHeaders)3 Publisher (io.servicetalk.concurrent.api.Publisher)2 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)2 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)2 Integer.toHexString (java.lang.Integer.toHexString)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2 ByteBuf (io.netty.buffer.ByteBuf)1 HT (io.netty.handler.codec.http.HttpConstants.HT)1 Http2Headers (io.netty.handler.codec.http2.Http2Headers)1 DefaultHttpHeadersFactory (io.servicetalk.http.api.DefaultHttpHeadersFactory)1 GET (io.servicetalk.http.api.HttpRequestMethod.GET)1 Benchmark (org.openjdk.jmh.annotations.Benchmark)1