Search in sources :

Example 26 with HttpHeaders

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

the class AbstractH2DuplexHandlerTest method responseWithContentLengthToHeadRequest.

private void responseWithContentLengthToHeadRequest(Variant variant, boolean endStream) {
    Assumptions.assumeTrue(variant == Variant.CLIENT_HANDLER, "Only relevant for the client-side");
    int contentLength = 1;
    // Send HEAD request
    channel.writeOutbound(newRequest(HEAD, "/", HTTP_2_0, HEADERS_FACTORY.newHeaders(), DEFAULT_ALLOCATOR, HEADERS_FACTORY), true);
    // Prepare server response with content-length header:
    Http2Headers headers = new DefaultHttp2Headers();
    headers.status(OK.codeAsText());
    headers.setInt(CONTENT_LENGTH, contentLength);
    channel.writeInbound(headersFrame(headers, endStream));
    HttpMetaData metaData = channel.readInbound();
    assertThat(metaData.headers().get(CONTENT_LENGTH), contentEqualTo(valueOf(contentLength)));
    if (endStream) {
        HttpHeaders trailers = channel.readInbound();
        if (trailers != null) {
            assertThat(trailers.isEmpty(), is(true));
        }
    } else {
        // No more items at this moment:
        assertThat(channel.inboundMessages(), is(empty()));
        channel.writeInbound(headersFrame(new DefaultHttp2Headers(), true));
        HttpHeaders trailers = channel.readInbound();
        assertThat(trailers.isEmpty(), is(true));
    }
    assertThat(channel.inboundMessages(), is(empty()));
}
Also used : HttpHeaders(io.servicetalk.http.api.HttpHeaders) EmptyHttpHeaders(io.servicetalk.http.api.EmptyHttpHeaders) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) HttpMetaData(io.servicetalk.http.api.HttpMetaData)

Example 27 with HttpHeaders

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

the class AbstractH2DuplexHandlerTest method withContentLength.

private void withContentLength(Variant variant, boolean addTrailers) {
    variant.writeOutbound(channel);
    String content = "hello";
    Http2Headers headers = variant.setHeaders(new DefaultHttp2Headers());
    headers.setInt(CONTENT_LENGTH, content.length());
    channel.writeInbound(headersFrame(headers, false));
    HttpMetaData metaData = channel.readInbound();
    assertThat(metaData.headers().get(CONTENT_LENGTH), contentEqualTo(valueOf(content.length())));
    channel.writeInbound(new DefaultHttp2DataFrame(writeAscii(UnpooledByteBufAllocator.DEFAULT, content), !addTrailers));
    Buffer buffer = channel.readInbound();
    assertThat(buffer, is(equalTo(DEFAULT_ALLOCATOR.fromAscii(content))));
    if (addTrailers) {
        channel.writeInbound(headersFrame(new DefaultHttp2Headers().set("trailer", "value"), true));
    }
    HttpHeaders trailers = channel.readInbound();
    if (trailers != null) {
        assertThat(trailers.isEmpty(), is(!addTrailers));
    }
    assertThat(channel.inboundMessages(), is(empty()));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) HttpHeaders(io.servicetalk.http.api.HttpHeaders) EmptyHttpHeaders(io.servicetalk.http.api.EmptyHttpHeaders) DefaultHttp2DataFrame(io.netty.handler.codec.http2.DefaultHttp2DataFrame) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) HttpMetaData(io.servicetalk.http.api.HttpMetaData)

Example 28 with HttpHeaders

use of io.servicetalk.http.api.HttpHeaders 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 29 with HttpHeaders

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

the class HttpObjectDecoder method readTrailingHeaders.

@Nullable
private HttpHeaders readTrailingHeaders(final ByteBuf buffer) {
    final long longLFIndex = findCRLF(buffer, maxHeaderFieldLength, allowLFWithoutCR);
    if (longLFIndex < 0) {
        return null;
    }
    if (crlfBeforeIndex(longLFIndex) > buffer.readerIndex()) {
        HttpHeaders trailer = this.trailer;
        if (trailer == null) {
            trailer = this.trailer = headersFactory.newTrailers();
        }
        return parseAllHeaders(buffer, trailer, longLFIndex) ? trailer : null;
    }
    consumeCRLF(buffer, crlfIndex(longLFIndex));
    // [1] https://tools.ietf.org/html/rfc7230.html#section-4.1
    return trailer != null ? trailer : headersFactory.newEmptyTrailers();
}
Also used : HttpHeaders(io.servicetalk.http.api.HttpHeaders) Nullable(javax.annotation.Nullable)

Example 30 with HttpHeaders

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

the class HttpObjectDecoder method getContentLength.

static long getContentLength(final HttpMetaData message) {
    final HttpHeaders headers = message.headers();
    final long contentLength = HeaderUtils.contentLength(headers.valuesIterator(CONTENT_LENGTH));
    if (contentLength >= 0) {
        return contentLength;
    }
    // We know the content length if it's a Web Socket message even if
    // Content-Length header is missing.
    long webSocketContentLength = getWebSocketContentLength(message);
    if (webSocketContentLength >= 0) {
        return webSocketContentLength;
    }
    // Otherwise we don't.
    return -1;
}
Also used : HttpHeaders(io.servicetalk.http.api.HttpHeaders)

Aggregations

HttpHeaders (io.servicetalk.http.api.HttpHeaders)43 Buffer (io.servicetalk.buffer.api.Buffer)25 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)13 Test (org.junit.jupiter.api.Test)13 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 HttpMetaData (io.servicetalk.http.api.HttpMetaData)11 Nullable (javax.annotation.Nullable)11 Http2Headers (io.netty.handler.codec.http2.Http2Headers)10 Single (io.servicetalk.concurrent.api.Single)9 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)9 Publisher (io.servicetalk.concurrent.api.Publisher)8 HttpResponseMetaData (io.servicetalk.http.api.HttpResponseMetaData)8 HttpServiceContext (io.servicetalk.http.api.HttpServiceContext)8 DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)7 EmptyHttpHeaders (io.servicetalk.http.api.EmptyHttpHeaders)7 CONTENT_LENGTH (io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH)7 CHUNKED (io.servicetalk.http.api.HttpHeaderValues.CHUNKED)7 DefaultHttp2DataFrame (io.netty.handler.codec.http2.DefaultHttp2DataFrame)5 HttpExecutionStrategy (io.servicetalk.http.api.HttpExecutionStrategy)5