use of io.netty.handler.codec.http.cookie.Cookie in project riposte by Nike-Inc.
the class ResponseSender method synchronizeAndSetupResponseInfoAndFirstChunk.
protected void synchronizeAndSetupResponseInfoAndFirstChunk(ResponseInfo<?> responseInfo, HttpResponse actualResponseObject, RequestInfo requestInfo, ChannelHandlerContext ctx) {
// Set the content type header.
// NOTE: This is ok even if the response doesn't have a body - in the case of chunked messages we don't
// know whether body content will be coming later or not so we have to be proactive here in case
// there *is* body content later.
// ALSO NOTE: The responseInfo may have already had a Content-Type header specified (e.g. reverse proxied
// response), but the way we build the header this will be ok. If responseInfo wanted to override it
// we allow that, and if not then the Content-Type in the headers will be honored, and if both of
// those are unspecified then the default mime type and charset are used.
responseInfo.getHeaders().set(CONTENT_TYPE, buildContentTypeHeader(responseInfo));
// Set the HTTP status code on the ResponseInfo object from the actualResponseObject if necessary.
if (responseInfo.getHttpStatusCode() == null)
responseInfo.setHttpStatusCode(actualResponseObject.getStatus().code());
// Make sure a trace ID is in the headers.
if (!responseInfo.getHeaders().contains(TraceHeaders.TRACE_ID)) {
// All responses must contain a trace ID. Try to get it from the request
// since it wasn't already in the response.
String traceId = extractDistributedTraceId(requestInfo, ctx);
if (traceId == null) {
// Couldn't find a valid trace ID anywhere, so just create a dummy one, and log what happened so if
// someone searches for that ID they'll find something explaining what happened.
traceId = TraceAndSpanIdGenerator.generateId();
String warningMsg = "Generating a dummy Trace ID for response header because a real Trace ID did not exist. This " + "probably happened because the request was not processed by the channel pipeline. dummy_trace_id=" + traceId;
runnableWithTracingAndMdc(() -> logger.warn(warningMsg), ctx).run();
}
responseInfo.getHeaders().set(TraceHeaders.TRACE_ID, traceId);
}
// Handle any keep-alive stuff
if (responseInfo.isForceConnectionCloseAfterResponseSent()) {
// We'll be closing the connection after this response is sent, so send the appropriate Connection header.
responseInfo.getHeaders().set(CONNECTION, HttpHeaders.Values.CLOSE);
} else if (requestInfo.isKeepAliveRequested()) {
// what the content length will be.
if (actualResponseObject instanceof LastHttpContent) {
responseInfo.getHeaders().set(CONTENT_LENGTH, ((LastHttpContent) actualResponseObject).content().readableBytes());
} else {
// If we have one of those responses, we mark it with content-length 0
if (isContentAlwaysEmpty(responseInfo)) {
responseInfo.getHeaders().remove(TRANSFER_ENCODING);
responseInfo.getHeaders().set(CONTENT_LENGTH, 0);
} else {
// Not a must-be-empty-payload status code. For these there might be a payload and we can't know the
// content length since it's being sent to us in chunks, so we have to set the
// Transfer-Encoding header to chunked in order for the response sending to be successful
// (otherwise the receiving client would just hang waiting for the connection to be closed).
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6
// and http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1 for the technical explanation.
// See http://en.wikipedia.org/wiki/Chunked_transfer_encoding for a more straightforward explanation
responseInfo.getHeaders().remove(CONTENT_LENGTH);
responseInfo.getHeaders().set(TRANSFER_ENCODING, CHUNKED);
}
}
// Add keep alive header as per
// http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
responseInfo.getHeaders().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
// Synchronize the ResponseInfo headers with the actualResponseObject
// (copy from responseInfo into actualResponseObject)
actualResponseObject.headers().add(responseInfo.getHeaders());
// Add cookies (if any)
if (responseInfo.getCookies() != null) {
for (Cookie cookie : responseInfo.getCookies()) {
actualResponseObject.headers().add(HttpHeaders.Names.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie.name(), cookie.value()));
}
}
}
use of io.netty.handler.codec.http.cookie.Cookie in project riposte by Nike-Inc.
the class FullResponseInfoTest method builder_sets_values_as_expected.
@Test
public void builder_sets_values_as_expected() {
// given
String content = UUID.randomUUID().toString();
int httpStatusCode = 200;
HttpHeaders headers = new DefaultHttpHeaders();
String mimeType = "text/text";
Charset contentCharset = CharsetUtil.ISO_8859_1;
Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
boolean preventCompressedOutput = true;
// when
FullResponseInfo<String> responseInfo = ResponseInfo.<String>newBuilder().withContentForFullResponse(content).withHttpStatusCode(httpStatusCode).withHeaders(headers).withDesiredContentWriterMimeType(mimeType).withDesiredContentWriterEncoding(contentCharset).withCookies(cookies).withPreventCompressedOutput(preventCompressedOutput).build();
// then
assertThat(responseInfo.getContentForFullResponse(), is(content));
assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode));
assertThat(responseInfo.getHeaders(), is(headers));
assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType));
assertThat(responseInfo.getDesiredContentWriterEncoding(), is(contentCharset));
assertThat(responseInfo.getCookies(), is(cookies));
assertThat(responseInfo.getUncompressedRawContentLength(), nullValue());
assertThat(responseInfo.getFinalContentLength(), nullValue());
assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedOutput));
assertThat(responseInfo.isChunkedResponse(), is(false));
assertThat(responseInfo.isResponseSendingStarted(), is(false));
assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false));
}
use of io.netty.handler.codec.http.cookie.Cookie in project riposte by Nike-Inc.
the class FullResponseInfoTest method uber_constructor_for_full_response_sets_fields_as_expected.
@Test
public void uber_constructor_for_full_response_sets_fields_as_expected() {
// given
String content = UUID.randomUUID().toString();
int httpStatusCode = 200;
HttpHeaders headers = new DefaultHttpHeaders();
String mimeType = "text/text";
Charset contentCharset = CharsetUtil.UTF_8;
Set<Cookie> cookies = Sets.newHashSet(new DefaultCookie("key1", "val1"), new DefaultCookie("key2", "val2"));
boolean preventCompressedResponse = true;
// when
FullResponseInfo<String> responseInfo = new FullResponseInfo<>(content, httpStatusCode, headers, mimeType, contentCharset, cookies, preventCompressedResponse);
// then
assertThat(responseInfo.getContentForFullResponse(), is(content));
assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode));
assertThat(responseInfo.getHeaders(), is(headers));
assertThat(responseInfo.getDesiredContentWriterMimeType(), is(mimeType));
assertThat(responseInfo.getDesiredContentWriterEncoding(), is(contentCharset));
assertThat(responseInfo.getCookies(), is(cookies));
assertThat(responseInfo.getUncompressedRawContentLength(), nullValue());
assertThat(responseInfo.getFinalContentLength(), nullValue());
assertThat(responseInfo.isPreventCompressedOutput(), is(preventCompressedResponse));
assertThat(responseInfo.isChunkedResponse(), is(false));
assertThat(responseInfo.isResponseSendingStarted(), is(false));
assertThat(responseInfo.isResponseSendingLastChunkSent(), is(false));
}
use of io.netty.handler.codec.http.cookie.Cookie in project riposte by Nike-Inc.
the class HttpUtilsTest method extractCookies_works_if_cookies_defined_in_trailing_headers.
@Test
public void extractCookies_works_if_cookies_defined_in_trailing_headers() {
// given
Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
HttpHeaders trailingHeaders = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2));
FullHttpRequest nettyRequestMock = mock(FullHttpRequest.class);
doReturn(new DefaultHttpHeaders()).when(nettyRequestMock).headers();
doReturn(trailingHeaders).when(nettyRequestMock).trailingHeaders();
// when
Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock);
// then
assertThat(extractedCookies.contains(cookie1), is(true));
assertThat(extractedCookies.contains(cookie2), is(true));
}
use of io.netty.handler.codec.http.cookie.Cookie in project riposte by Nike-Inc.
the class HttpUtilsTest method extractCookies_works_if_cookies_defined_in_headers.
@Test
public void extractCookies_works_if_cookies_defined_in_headers() {
// given
Cookie cookie1 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
Cookie cookie2 = new DefaultCookie(UUID.randomUUID().toString(), UUID.randomUUID().toString());
HttpHeaders headers = new DefaultHttpHeaders().add(HttpHeaders.Names.COOKIE, ClientCookieEncoder.LAX.encode(cookie1, cookie2));
HttpRequest nettyRequestMock = mock(HttpRequest.class);
doReturn(headers).when(nettyRequestMock).headers();
// when
Set<Cookie> extractedCookies = HttpUtils.extractCookies(nettyRequestMock);
// then
assertThat(extractedCookies.contains(cookie1), is(true));
assertThat(extractedCookies.contains(cookie2), is(true));
}
Aggregations