下面列出了 io.netty.handler.codec.json.JsonObjectDecoder #io.netty.handler.codec.http.cookie.ClientCookieDecoder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void addDecoderReplaysLastHttp() {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(),
ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT)
.addHandler(new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
MatcherAssert.assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor"));
Object content = channel.readInbound();
MatcherAssert.assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
MatcherAssert.assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
MatcherAssert.assertThat(channel.readInbound(), nullValue());
}
@Test
public void addNamedDecoderReplaysLastHttp() {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(),
ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT)
.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
MatcherAssert.assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
MatcherAssert.assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
MatcherAssert.assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
MatcherAssert.assertThat(channel.readInbound(), nullValue());
}
@Test
public void addEncoderReplaysLastHttp() {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(),
ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT)
.addHandler(new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
MatcherAssert.assertThat(channel.pipeline().names().iterator().next(), is("JsonObjectDecoder$extractor"));
Object content = channel.readInbound();
MatcherAssert.assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
MatcherAssert.assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
MatcherAssert.assertThat(channel.readInbound(), nullValue());
}
@Test
public void addNamedEncoderReplaysLastHttp() {
ByteBuf buf = Unpooled.copiedBuffer("{\"foo\":1}", CharsetUtil.UTF_8);
EmbeddedChannel channel = new EmbeddedChannel();
new HttpClientOperations(() -> channel, ConnectionObserver.emptyListener(),
ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT)
.addHandler("json", new JsonObjectDecoder());
channel.writeInbound(new DefaultLastHttpContent(buf));
MatcherAssert.assertThat(channel.pipeline().names().iterator().next(), is("json$extractor"));
Object content = channel.readInbound();
MatcherAssert.assertThat(content, instanceOf(ByteBuf.class));
((ByteBuf) content).release();
content = channel.readInbound();
MatcherAssert.assertThat(content, instanceOf(LastHttpContent.class));
((LastHttpContent) content).release();
MatcherAssert.assertThat(channel.readInbound(), nullValue());
}
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
FullHttpResponse response = (FullHttpResponse) msg;
if (httpRequiresSessionId && !active) {
final List<String> setCookieHeaderValues = response.headers().getAll(HttpHeaderNames.SET_COOKIE);
for (String setCookieHeaderValue : setCookieHeaderValues) {
final Cookie cookie = ClientCookieDecoder.LAX.decode(setCookieHeaderValue);
if ("JSESSIONID".equals(cookie.name())) {
this.cookie = setCookieHeaderValue;
break;
}
}
active = true;
handShakeFuture.run();
}
waitingGet = false;
ctx.fireChannelRead(response.content());
}
private static Future<HttpContext> getContextAsync() {
logger.debug("Getting CSRF token...");
Request request =
new RequestBuilder().setUri(ROOT_URI).setMethod("GET").build();
return GetClient.getClient()
.executeRequest(request)
.toCompletableFuture()
.handleAsync((resp, throwable) -> {
if (resp == null) {
logger.error(throwable.getMessage());
return null;
}
List<Cookie> cookies =
resp.getHeaders()
.getAll("Set-Cookie")
.stream()
.map(cookie -> ClientCookieDecoder.STRICT.decode(cookie))
.collect(Collectors.toList());
Cookie csrfCookie =
cookies.stream()
.filter(cookie -> cookie.name().equals("CSRFCookie"))
.findAny()
.orElse(null);
if (csrfCookie == null) {
logger.error("Couldn't find cookie with name=CSRFCookie");
return null;
}
logger.debug("Retrieved CSRF token `{}`", csrfCookie.value());
return new HttpContext(csrfCookie.value(), cookies);
});
}
public static Future<HttpContext> getContextAsync(Term term) {
Request request = new RequestBuilder()
.setUri(Uri.create(ROOT_URI + term.getId()))
.setMethod("GET")
.build();
return GetClient.getClient()
.executeRequest(request)
.toCompletableFuture()
.handleAsync((resp, throwable) -> {
if (resp == null) {
return null;
}
List<Cookie> cookies =
resp.getHeaders()
.getAll("Set-Cookie")
.stream()
.map(cookie -> ClientCookieDecoder.STRICT.decode(cookie))
.collect(Collectors.toList());
Cookie csrfCookie =
cookies.stream()
.filter(cookie -> cookie.name().equals("CSRFCookie"))
.findAny()
.orElse(null);
if (csrfCookie == null) {
return null;
}
return new HttpContext(csrfCookie.value(), cookies);
});
}
protected void captureResponseCookies(HttpResponse httpResponse) {
List<String> setCookieHeaders = httpResponse.headers().getAll(HttpHeaderNames.SET_COOKIE);
if (setCookieHeaders == null) {
return;
}
for (String setCookieHeader : setCookieHeaders) {
Cookie cookie = ClientCookieDecoder.LAX.decode(setCookieHeader);
if (cookie == null) {
return;
}
HarCookie harCookie = new HarCookie();
harCookie.setName(cookie.name());
harCookie.setValue(cookie.value());
// comment is no longer supported in the netty ClientCookieDecoder
harCookie.setDomain(cookie.domain());
harCookie.setHttpOnly(cookie.isHttpOnly());
harCookie.setPath(cookie.path());
harCookie.setSecure(cookie.isSecure());
if (cookie.maxAge() > 0) {
// use a Calendar with the current timestamp + maxAge seconds. the locale of the calendar is irrelevant,
// since we are dealing with timestamps.
Calendar expires = Calendar.getInstance();
// zero out the milliseconds, since maxAge is in seconds
expires.set(Calendar.MILLISECOND, 0);
// we can't use Calendar.add, since that only takes ints. TimeUnit.convert handles second->millisecond
// overflow reasonably well by returning the result as Long.MAX_VALUE.
expires.setTimeInMillis(expires.getTimeInMillis() + MILLISECONDS.convert(cookie.maxAge(), SECONDS));
harCookie.setExpires(expires.getTime());
}
harEntry.getResponse().getCookies().add(harCookie);
}
}
protected void captureResponseCookies(HttpResponse httpResponse) {
List<String> setCookieHeaders = httpResponse.headers().getAll(HttpHeaders.Names.SET_COOKIE);
if (setCookieHeaders == null) {
return;
}
for (String setCookieHeader : setCookieHeaders) {
Cookie cookie = ClientCookieDecoder.LAX.decode(setCookieHeader);
if (cookie == null) {
return;
}
HarCookie harCookie = new HarCookie();
harCookie.setName(cookie.name());
harCookie.setValue(cookie.value());
// comment is no longer supported in the netty ClientCookieDecoder
harCookie.setDomain(cookie.domain());
harCookie.setHttpOnly(cookie.isHttpOnly());
harCookie.setPath(cookie.path());
harCookie.setSecure(cookie.isSecure());
if (cookie.maxAge() > 0) {
// use a Calendar with the current timestamp + maxAge seconds. the locale of the calendar is irrelevant,
// since we are dealing with timestamps.
Calendar expires = Calendar.getInstance();
// zero out the milliseconds, since maxAge is in seconds
expires.set(Calendar.MILLISECOND, 0);
// we can't use Calendar.add, since that only takes ints. TimeUnit.convert handles second->millisecond
// overflow reasonably well by returning the result as Long.MAX_VALUE.
expires.setTimeInMillis(expires.getTimeInMillis() + TimeUnit.MILLISECONDS.convert(cookie.maxAge(), TimeUnit.SECONDS));
harCookie.setExpires(expires.getTime());
}
harEntry.getResponse().getCookies().add(harCookie);
}
}
static Cookie fromVertxCookie(io.vertx.core.http.Cookie cookie) {
io.netty.handler.codec.http.cookie.Cookie decoded = ClientCookieDecoder.STRICT.decode(cookie.encode());
Cookie out = new Cookie(decoded.name(), decoded.value());
Optional.ofNullable(decoded.domain()).ifPresent(out::setDomain);
out.setPath(decoded.path());
out.setHttpOnly(decoded.isHttpOnly());
out.setSecure(decoded.isSecure());
if (decoded.maxAge() != Long.MIN_VALUE) {
out.setMaxAge((int) decoded.maxAge());
}
// TODO extract other values
return out;
}
static Cookie fromVertxCookie(io.vertx.ext.web.Cookie cookie) {
io.netty.handler.codec.http.cookie.Cookie decoded = ClientCookieDecoder.STRICT.decode(cookie.encode());
Cookie out = new Cookie(decoded.name(), decoded.value());
Optional.ofNullable(decoded.domain()).ifPresent(out::setDomain);
out.setPath(decoded.path());
out.setHttpOnly(decoded.isHttpOnly());
out.setSecure(decoded.isSecure());
if (decoded.maxAge() != Long.MIN_VALUE) {
out.setMaxAge((int) decoded.maxAge());
}
// TODO extract other values
return out;
}
@Test
public void shouldCreateNettyResponseWithCookieWithAttributes() {
ResponseCookie cookie = responseCookie("cookie-test", "cookie-value")
.domain("cookie-domain")
.path("cookie-path")
.maxAge(1234)
.httpOnly(true)
.secure(true)
.build();
LiveHttpResponse styxResponse = new LiveHttpResponse.Builder(OK)
.cookies(cookie)
.build();
io.netty.handler.codec.http.HttpResponse nettyResponse = translator.toNettyResponse(styxResponse);
String setCookie = nettyResponse.headers().get(SET_COOKIE);
Cookie nettyCookie = ClientCookieDecoder.LAX.decode(setCookie);
assertThat(nettyCookie.name(), is("cookie-test"));
assertThat(nettyCookie.value(), is("cookie-value"));
assertThat(nettyCookie.domain(), is("cookie-domain"));
assertThat(nettyCookie.maxAge(), is(1234L));
assertThat(nettyCookie.isHttpOnly(), is(true));
assertThat(nettyCookie.isSecure(), is(true));
}
@Test
public void testBasicAuthentication() throws Exception {
Configuration config = TestConfiguration.createMinimalConfigurationForTest();
BasicAuthLogin auth = new BasicAuthLogin();
auth.setUsername("test");
auth.setPassword("test1");
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/login");
request.content().writeBytes(JsonSerializer.getObjectMapper().writeValueAsBytes(auth));
TestHttpQueryDecoder decoder = new TestHttpQueryDecoder(config);
decoder.decode(null, request, results);
Assert.assertEquals(1, results.size());
Object result = results.iterator().next();
Assert.assertEquals(BasicAuthLoginRequest.class, result.getClass());
BasicAuthLoginRequestHandler handler = new BasicAuthLoginRequestHandler(config);
CaptureChannelHandlerContext ctx = new CaptureChannelHandlerContext();
handler.channelRead(ctx, result);
Assert.assertNotNull(ctx.msg);
Assert.assertTrue(ctx.msg instanceof DefaultFullHttpResponse);
DefaultFullHttpResponse response = (DefaultFullHttpResponse) ctx.msg;
Assert.assertEquals(HttpResponseStatus.OK, response.status());
Assert.assertTrue(response.headers().contains(HttpHeaderNames.CONTENT_TYPE));
Assert.assertEquals(Constants.JSON_TYPE, response.headers().get(HttpHeaderNames.CONTENT_TYPE));
Assert.assertTrue(response.headers().contains(HttpHeaderNames.SET_COOKIE));
Cookie c = ClientCookieDecoder.STRICT.decode(response.headers().get(HttpHeaderNames.SET_COOKIE));
Assert.assertEquals(TestConfiguration.HTTP_ADDRESS_DEFAULT, c.domain());
Assert.assertEquals(86400, c.maxAge());
Assert.assertTrue(c.isHttpOnly());
Assert.assertTrue(c.isSecure());
Assert.assertEquals(Constants.COOKIE_NAME, c.name());
UUID.fromString(c.value());
}
@Test
public void testBasicAuthentication() throws Exception {
Configuration config = TestConfiguration.createMinimalConfigurationForTest();
// @formatter:off
String form =
"{\n" +
" \"username\": \"test\",\n" +
" \"password\": \"test1\"\n" +
"}";
// @formatter:on
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/login");
request.content().writeBytes(form.getBytes());
TestHttpQueryDecoder decoder = new TestHttpQueryDecoder(config);
decoder.decode(null, request, results);
Assert.assertEquals(1, results.size());
Object result = results.iterator().next();
Assert.assertEquals(BasicAuthLoginRequest.class, result.getClass());
BasicAuthLoginRequestHandler handler = new BasicAuthLoginRequestHandler(config.getSecurity(), config.getHttp());
CaptureChannelHandlerContext ctx = new CaptureChannelHandlerContext();
handler.channelRead(ctx, result);
Assert.assertNotNull(ctx.msg);
Assert.assertTrue(ctx.msg instanceof DefaultFullHttpResponse);
DefaultFullHttpResponse response = (DefaultFullHttpResponse) ctx.msg;
Assert.assertEquals(HttpResponseStatus.OK, response.status());
Assert.assertTrue(response.headers().contains(HttpHeaderNames.CONTENT_TYPE));
Assert.assertEquals(Constants.JSON_TYPE, response.headers().get(HttpHeaderNames.CONTENT_TYPE));
Assert.assertTrue(response.headers().contains(HttpHeaderNames.SET_COOKIE));
Cookie c = ClientCookieDecoder.STRICT.decode(response.headers().get(HttpHeaderNames.SET_COOKIE));
Assert.assertEquals(TestConfiguration.TIMELY_HTTP_ADDRESS_DEFAULT, c.domain());
Assert.assertEquals(86400, c.maxAge());
Assert.assertTrue(c.isHttpOnly());
Assert.assertTrue(c.isSecure());
Assert.assertEquals(Constants.COOKIE_NAME, c.name());
UUID.fromString(c.value());
}
protected void captureResponseCookies(HttpResponse httpResponse) {
Log.e("InnerHandle", "captureResponseCookies " + harEntry.getId());
List<String> setCookieHeaders = httpResponse.headers().getAll(HttpHeaders.Names.SET_COOKIE);
if (setCookieHeaders == null) {
return;
}
for (String setCookieHeader : setCookieHeaders) {
Cookie cookie = ClientCookieDecoder.LAX.decode(setCookieHeader);
if (cookie == null) {
return;
}
HarCookie harCookie = new HarCookie();
harCookie.setName(cookie.name());
harCookie.setValue(cookie.value());
// comment is no longer supported in the netty ClientCookieDecoder
harCookie.setDomain(cookie.domain());
harCookie.setHttpOnly(cookie.isHttpOnly());
harCookie.setPath(cookie.path());
harCookie.setSecure(cookie.isSecure());
if (cookie.maxAge() > 0) {
// use a Calendar with the current timestamp + maxAge seconds. the locale of the calendar is irrelevant,
// since we are dealing with timestamps.
Calendar expires = Calendar.getInstance();
// zero out the milliseconds, since maxAge is in seconds
expires.set(Calendar.MILLISECOND, 0);
// we can't use Calendar.add, since that only takes ints. TimeUnit.convert handles second->millisecond
// overflow reasonably well by returning the result as Long.MAX_VALUE.
expires.setTimeInMillis(expires.getTimeInMillis() + TimeUnit.MILLISECONDS.convert(cookie.maxAge(), TimeUnit.SECONDS));
harCookie.setExpires(expires.getTime());
}
harResponse.getResponse().getCookies().add(harCookie);
harResponse.addHeader(harCookie.getName(), harCookie.getValue());
}
}
HttpClientConfig(ConnectionProvider connectionProvider, Map<ChannelOption<?>, ?> options,
Supplier<? extends SocketAddress> remoteAddress) {
super(connectionProvider, options, remoteAddress);
this.acceptGzip = false;
this.cookieDecoder = ClientCookieDecoder.STRICT;
this.cookieEncoder = ClientCookieEncoder.STRICT;
this.decoder = new HttpResponseDecoderSpec();
this.headers = new DefaultHttpHeaders();
this.method = HttpMethod.GET;
this.protocols = new HttpProtocol[]{HttpProtocol.HTTP11};
this._protocols = h11;
this.retryDisabled = false;
}
HttpClientOperations(Connection c, ConnectionObserver listener, ClientCookieEncoder encoder, ClientCookieDecoder decoder) {
super(c, listener);
this.isSecure = c.channel()
.pipeline()
.get(NettyPipeline.SslHandler) != null;
this.nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
this.requestHeaders = nettyRequest.headers();
this.cookieDecoder = decoder;
this.cookieEncoder = encoder;
}
@Test
public void testConstructorWithProvidedReplacement() {
EmbeddedChannel channel = new EmbeddedChannel();
channel.pipeline().addFirst(NettyPipeline.SslHandler, new ChannelHandlerAdapter() {
});
HttpClientOperations ops1 = new HttpClientOperations(() -> channel,
ConnectionObserver.emptyListener(),
ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT);
ops1.followRedirectPredicate((req, res) -> true);
ops1.started = true;
ops1.retrying = true;
ops1.redirecting = new RedirectClientException(new DefaultHttpHeaders().add(HttpHeaderNames.LOCATION, "/"));
HttpClientOperations ops2 = new HttpClientOperations(ops1);
assertSame(ops1.channel(), ops2.channel());
assertSame(ops1.started, ops2.started);
assertSame(ops1.retrying, ops2.retrying);
assertSame(ops1.redirecting, ops2.redirecting);
assertSame(ops1.redirectedFrom, ops2.redirectedFrom);
assertSame(ops1.isSecure, ops2.isSecure);
assertSame(ops1.nettyRequest, ops2.nettyRequest);
assertSame(ops1.responseState, ops2.responseState);
assertSame(ops1.followRedirectPredicate, ops2.followRedirectPredicate);
assertSame(ops1.requestHeaders, ops2.requestHeaders);
}
private void doTestStatus(HttpResponseStatus status) {
EmbeddedChannel channel = new EmbeddedChannel();
HttpClientOperations ops = new HttpClientOperations(() -> channel,
ConnectionObserver.emptyListener(),
ClientCookieEncoder.STRICT, ClientCookieDecoder.STRICT);
ops.setNettyResponse(new DefaultFullHttpResponse(HTTP_1_1, status, Unpooled.EMPTY_BUFFER));
assertEquals(status.reasonPhrase(), ops.status().reasonPhrase());
}
protected void captureResponseCookies(HttpResponse httpResponse) {
List<String> setCookieHeaders = httpResponse.headers().getAll(HttpHeaders.Names.SET_COOKIE);
if (setCookieHeaders == null) {
return;
}
for (String setCookieHeader : setCookieHeaders) {
Cookie cookie = ClientCookieDecoder.LAX.decode(setCookieHeader);
if (cookie == null) {
return;
}
HarCookie harCookie = new HarCookie();
harCookie.setName(cookie.name());
harCookie.setValue(cookie.value());
// comment is no longer supported in the netty ClientCookieDecoder
harCookie.setDomain(cookie.domain());
harCookie.setHttpOnly(cookie.isHttpOnly());
harCookie.setPath(cookie.path());
harCookie.setSecure(cookie.isSecure());
if (cookie.maxAge() > 0) {
// use a Calendar with the current timestamp + maxAge seconds. the locale of the calendar is irrelevant,
// since we are dealing with timestamps.
Calendar expires = Calendar.getInstance();
// zero out the milliseconds, since maxAge is in seconds
expires.set(Calendar.MILLISECOND, 0);
// we can't use Calendar.add, since that only takes ints. TimeUnit.convert handles second->millisecond
// overflow reasonably well by returning the result as Long.MAX_VALUE.
expires.setTimeInMillis(expires.getTimeInMillis() + TimeUnit.MILLISECONDS.convert(cookie.maxAge(), TimeUnit.SECONDS));
harCookie.setExpires(expires.getTime());
}
harEntry.getResponse().getCookies().add(harCookie);
}
}
private MultiValueMap<String, ResponseCookie> initCookies() {
final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
headers.getAll(HttpHeaderNames.SET_COOKIE)
.stream()
.map(ClientCookieDecoder.LAX::decode)
.forEach(c -> cookies.add(c.name(), ResponseCookie.from(c.name(), c.value())
.maxAge(c.maxAge())
.domain(c.domain())
.path(c.path())
.secure(c.isSecure())
.httpOnly(c.isHttpOnly())
.build()));
return cookies;
}
private void processRedirectResponse(HttpContext<?> context) {
// Now the context contains the redirect request in clientRequest() and the original request in request()
List<String> cookieHeaders = context.clientResponse().cookies();
if (cookieHeaders == null) {
return;
}
WebClientSessionAware webclient = (WebClientSessionAware) ((HttpRequestImpl)context.request()).client;
HttpRequestImpl<?> originalRequest = (HttpRequestImpl<?>) context.request();
CookieStore cookieStore = webclient.cookieStore();
String domain = URI.create(context.clientResponse().request().absoluteURI()).getHost();
if (domain.equals(originalRequest.host()) && originalRequest.virtualHost != null) {
domain = originalRequest.virtualHost;
}
final String finalDomain = domain;
cookieHeaders.forEach(header -> {
Cookie cookie = ClientCookieDecoder.STRICT.decode(header);
if (cookie != null) {
if (cookie.domain() == null) {
// Set the domain if missing, because we need to send cookies
// only to the domains we received them from.
cookie.setDomain(finalDomain);
}
cookieStore.put(cookie);
}
});
}
protected void assertCookieSet(FullHttpResponse response) {
Cookie cookie = ClientCookieDecoder.STRICT.decode(response.headers().get("Set-Cookie"));
assertEquals("session-id", cookie.value());
}
protected void assertCookieCleared(FullHttpResponse response) {
Cookie cookie = ClientCookieDecoder.STRICT.decode(response.headers().get("Set-Cookie"));
assertEquals("", cookie.value());
}
protected void assertCookieSet(FullHttpResponse response) {
Cookie cookie = ClientCookieDecoder.STRICT.decode(response.headers().get("Set-Cookie"));
assertEquals("session-id", cookie.value());
}
protected void assertCookieCleared(FullHttpResponse response) {
Cookie cookie = ClientCookieDecoder.STRICT.decode(response.headers().get("Set-Cookie"));
assertEquals("", cookie.value());
}
@Benchmark
public Cookie decodeCookieWithRfc1123ExpiresField() {
return ClientCookieDecoder.STRICT.decode(COOKIE_STRING);
}
ResponseState(HttpResponse response, HttpHeaders headers, ClientCookieDecoder decoder) {
this.response = response;
this.headers = headers;
this.cookieHolder = Cookies.newClientResponseHolder(headers, decoder);
}
/**
* Configure the
* {@link ClientCookieEncoder}, {@link ClientCookieDecoder} will be
* chosen based on the encoder
*
* @param encoder the preferred ClientCookieEncoder
*
* @return a new {@link HttpClient}
*/
public final HttpClient cookieCodec(ClientCookieEncoder encoder) {
Objects.requireNonNull(encoder, "encoder");
ClientCookieDecoder decoder = encoder == ClientCookieEncoder.LAX ?
ClientCookieDecoder.LAX : ClientCookieDecoder.STRICT;
return cookieCodec(encoder, decoder);
}
/**
* Configure the
* {@link ClientCookieEncoder} and {@link ClientCookieDecoder}
*
* @param encoder the preferred ClientCookieEncoder
* @param decoder the preferred ClientCookieDecoder
*
* @return a new {@link HttpClient}
*/
public final HttpClient cookieCodec(ClientCookieEncoder encoder, ClientCookieDecoder decoder) {
Objects.requireNonNull(encoder, "encoder");
Objects.requireNonNull(decoder, "decoder");
HttpClient dup = duplicate();
dup.configuration().cookieEncoder = encoder;
dup.configuration().cookieDecoder = decoder;
return dup;
}