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()));
}
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()));
}
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));
}
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();
}
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;
}
Aggregations