use of io.servicetalk.http.api.HttpRequestMetaData in project servicetalk by apple.
the class HttpRequestEncoderTest method contentLengthNoTrailersHeaderWhiteSpaceEncodedWithValidationOff.
@Test
void contentLengthNoTrailersHeaderWhiteSpaceEncodedWithValidationOff() {
EmbeddedChannel channel = newEmbeddedChannel();
byte[] content = new byte[128];
ThreadLocalRandom.current().nextBytes(content);
Buffer buffer = allocator.wrap(content);
HttpRequestMetaData request = newRequestMetaData(HTTP_1_1, GET, "/some/path?foo=bar&baz=yyy", new DefaultHttpHeadersFactory(false, false, false).newHeaders());
request.headers().add(" " + CONNECTION + " ", " " + KEEP_ALIVE).add(" " + USER_AGENT + " ", " unit-test ").add(CONTENT_LENGTH, valueOf(content.length));
channel.writeOutbound(request);
channel.writeOutbound(buffer.duplicate());
ByteBuf byteBuf = channel.readOutbound();
String actualMetaData = byteBuf.toString(US_ASCII);
byteBuf.release();
assertTrue(actualMetaData.contains("GET /some/path?foo=bar&baz=yyy HTTP/1.1" + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
assertTrue(actualMetaData.contains(" " + CONNECTION + " : " + KEEP_ALIVE + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
assertTrue(actualMetaData.contains(" " + USER_AGENT + " : unit-test " + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
assertTrue(actualMetaData.contains(CONTENT_LENGTH + ": " + buffer.readableBytes() + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
assertTrue(actualMetaData.endsWith("\r\n" + "\r\n"), () -> "unexpected metadata: " + actualMetaData);
byteBuf = channel.readOutbound();
assertEquals(buffer.toNioBuffer(), byteBuf.nioBuffer());
byteBuf.release();
assertFalse(channel.finishAndReleaseAll());
}
use of io.servicetalk.http.api.HttpRequestMetaData in project servicetalk by apple.
the class HttpRequestEncoderTest method variableNoTrailers.
@Test
void variableNoTrailers() {
EmbeddedChannel channel = newEmbeddedChannel();
byte[] content = new byte[128];
ThreadLocalRandom.current().nextBytes(content);
Buffer buffer = allocator.wrap(content);
HttpRequestMetaData request = newRequestMetaData(HTTP_1_1, GET, "/some/path?foo=bar&baz=yyy", INSTANCE.newHeaders());
request.headers().add(CONNECTION, KEEP_ALIVE).add(USER_AGENT, "unit-test");
channel.writeOutbound(request);
channel.writeOutbound(buffer.duplicate());
channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
verifyHttpRequest(channel, buffer, TransferEncoding.Variable, false);
assertFalse(channel.finishAndReleaseAll());
}
use of io.servicetalk.http.api.HttpRequestMetaData in project servicetalk by apple.
the class PartitionedHttpClientTest method testPartitionByTarget.
@Test
void testPartitionByTarget() throws Exception {
final Function<HttpRequestMetaData, PartitionAttributesBuilder> selector = req -> new DefaultPartitionAttributesBuilder(1).add(SRV_NAME, req.requestTarget().substring(1));
try (BlockingHttpClient clt = HttpClients.forPartitionedAddress(psd, "test-cluster", selector).initializer((pa, builder) -> builder.unresolvedAddressToHost(addr -> pa.get(SRV_NAME))).buildBlocking()) {
sdPublisher.onSubscribe(new TestSubscription());
sdPublisher.onNext(new TestPSDE(SRV_1, (InetSocketAddress) srv1.listenAddress()), new TestPSDE(SRV_2, (InetSocketAddress) srv2.listenAddress()));
final HttpResponse httpResponse1 = clt.request(clt.get("/" + SRV_2));
final HttpResponse httpResponse2 = clt.request(clt.get("/" + SRV_1));
MatcherAssert.assertThat(httpResponse1.headers().get(X_SERVER), hasToString(SRV_2));
MatcherAssert.assertThat(httpResponse2.headers().get(X_SERVER), hasToString(SRV_1));
}
}
use of io.servicetalk.http.api.HttpRequestMetaData in project servicetalk by apple.
the class H2ToStH1ClientDuplexHandler method write.
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
if (msg instanceof HttpRequestMetaData) {
closeHandler.protocolPayloadBeginOutbound(ctx);
HttpRequestMetaData metaData = (HttpRequestMetaData) msg;
HttpHeaders h1Headers = metaData.headers();
waitForContinuation = REQ_EXPECT_CONTINUE.test(metaData);
CharSequence host = h1Headers.getAndRemove(HOST);
Http2Headers h2Headers = h1HeadersToH2Headers(h1Headers);
if (host == null) {
host = metaData.host();
if (host != null) {
h2Headers.authority(host);
}
} else {
h2Headers.authority(host);
}
method = metaData.method();
h2Headers.method(method.name());
if (!CONNECT.equals(method)) {
// The ":scheme" and ":path" pseudo-header fields MUST be omitted for CONNECT.
// https://tools.ietf.org/html/rfc7540#section-8.3
h2Headers.scheme(scheme.name());
h2Headers.path(metaData.requestTarget());
}
try {
writeMetaData(ctx, metaData, h2Headers, true, promise);
} finally {
final Http2StreamChannel streamChannel = (Http2StreamChannel) ctx.channel();
final int streamId = streamChannel.stream().id();
if (streamId > 0) {
observer.streamIdAssigned(streamId);
}
}
} else if (msg instanceof Buffer) {
writeBuffer(ctx, (Buffer) msg, promise);
} else if (msg instanceof HttpHeaders) {
writeTrailers(ctx, msg, promise);
} else {
ctx.write(msg, promise);
}
}
use of io.servicetalk.http.api.HttpRequestMetaData in project servicetalk by apple.
the class HttpRequestEncoderTest method variableNoTrailersNoContent.
@Test
void variableNoTrailersNoContent() {
EmbeddedChannel channel = newEmbeddedChannel();
HttpRequestMetaData request = newRequestMetaData(HTTP_1_1, GET, "/some/path?foo=bar&baz=yyy", INSTANCE.newHeaders());
request.headers().add(CONNECTION, KEEP_ALIVE).add(USER_AGENT, "unit-test");
channel.writeOutbound(request);
channel.writeOutbound(EMPTY_BUFFER);
channel.writeOutbound(EmptyHttpHeaders.INSTANCE);
verifyHttpRequest(channel, EMPTY_BUFFER, TransferEncoding.Variable, false);
assertFalse(channel.finishAndReleaseAll());
}
Aggregations