Skip to content

Commit

Permalink
Cookie names should be treatead case-sensitively (ExpediaGroup#186)
Browse files Browse the repository at this point in the history
* Cookie names should be considered case sensitive. Fixes ExpediaGroup#185
  • Loading branch information
dvlato authored Jun 13, 2018
1 parent 8e8058b commit 0f9ea5a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import static com.hotels.styx.api.HttpHeaderNames.HOST;
import static com.hotels.styx.api.HttpHeaderValues.KEEP_ALIVE;
import static com.hotels.styx.api.support.CookiesSupport.findCookie;
import static io.netty.handler.codec.http.HttpMethod.CONNECT;
import static io.netty.handler.codec.http.HttpMethod.DELETE;
import static io.netty.handler.codec.http.HttpMethod.GET;
Expand Down Expand Up @@ -334,9 +335,7 @@ public Iterable<String> queryParamNames() {
* @return returns an optional cookie object from the header
*/
public Optional<HttpCookie> cookie(String name) {
return cookies().stream()
.filter(cookie -> name.equalsIgnoreCase(cookie.name()))
.findFirst();
return findCookie(cookies, name);
}

/**
Expand Down Expand Up @@ -648,9 +647,7 @@ public Builder addCookie(String name, String value) {
* @return {@code this}
*/
public Builder removeCookie(String name) {
cookies.stream()
.filter(cookie -> cookie.name().equals(name))
.findFirst()
findCookie(cookies, name)
.ifPresent(cookies::remove);

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.HttpMessageBody.NO_BODY;
import static com.hotels.styx.api.support.CookiesSupport.findCookie;
import static io.netty.buffer.Unpooled.copiedBuffer;
import static io.netty.handler.codec.http.HttpResponseStatus.FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.MOVED_PERMANENTLY;
Expand Down Expand Up @@ -151,9 +152,7 @@ public ImmutableList<HttpCookie> cookies() {
* @return the cookie if present
*/
public Optional<HttpCookie> cookie(String name) {
return cookies().stream()
.filter(cookie -> name.equalsIgnoreCase(cookie.name()))
.findFirst();
return findCookie(cookies, name);
}

/**
Expand Down Expand Up @@ -515,11 +514,8 @@ public Builder addCookie(String name, String value) {
* @return {@code this}
*/
public Builder removeCookie(String name) {
cookies.stream()
.filter(cookie -> cookie.name().equalsIgnoreCase(name))
.findFirst()
findCookie(cookies, name)
.ifPresent(cookie -> cookies.remove(cookie));

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.support.CookiesSupport.findCookie;

/**
* All behaviour common to both full requests and full responses.
Expand Down Expand Up @@ -97,9 +98,7 @@ default List<String> headers(CharSequence name) {
* @return the cookie if present
*/
default Optional<HttpCookie> cookie(String name) {
return cookies().stream()
.filter(cookie -> name.equalsIgnoreCase(cookie.name()))
.findFirst();
return findCookie(cookies(), name);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static com.hotels.styx.api.messages.HttpMethod.httpMethod;
import static com.hotels.styx.api.messages.HttpVersion.HTTP_1_1;
import static com.hotels.styx.api.messages.HttpVersion.httpVersion;
import static com.hotels.styx.api.support.CookiesSupport.findCookie;
import static com.hotels.styx.api.support.CookiesSupport.isCookieHeader;
import static java.lang.Integer.parseInt;
import static java.net.InetSocketAddress.createUnresolved;
Expand Down Expand Up @@ -592,9 +593,7 @@ public Builder addCookie(String name, String value) {
* @return {@code this}
*/
public Builder removeCookie(String name) {
cookies.stream()
.filter(cookie -> cookie.name().equalsIgnoreCase(name))
.findFirst()
findCookie(cookies, name)
.ifPresent(cookies::remove);

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static com.hotels.styx.api.messages.HttpResponseStatus.statusWithCode;
import static com.hotels.styx.api.messages.HttpVersion.HTTP_1_1;
import static com.hotels.styx.api.messages.HttpVersion.httpVersion;
import static com.hotels.styx.api.support.CookiesSupport.findCookie;
import static java.lang.Integer.parseInt;
import static java.util.Objects.requireNonNull;
import static rx.Observable.just;
Expand Down Expand Up @@ -361,9 +362,7 @@ public Builder addCookie(String name, String value) {
* @return {@code this}
*/
public Builder removeCookie(String name) {
cookies.stream()
.filter(cookie -> cookie.name().equalsIgnoreCase(name))
.findFirst()
findCookie(cookies, name)
.ifPresent(cookies::remove);

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static com.hotels.styx.api.HttpHeaderNames.CONTENT_LENGTH;
import static com.hotels.styx.api.HttpHeaderNames.CONTENT_TYPE;
import static com.hotels.styx.api.support.CookiesSupport.findCookie;

/**
* All behaviour common to both streaming requests and streaming responses.
Expand Down Expand Up @@ -86,9 +87,7 @@ default List<String> headers(CharSequence name) {
* @return the cookie if present
*/
default Optional<HttpCookie> cookie(String name) {
return cookies().stream()
.filter(cookie -> name.equalsIgnoreCase(cookie.name()))
.findFirst();
return findCookie(cookies(), name);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import static com.hotels.styx.api.messages.HttpMethod.httpMethod;
import static com.hotels.styx.api.messages.HttpVersion.HTTP_1_1;
import static com.hotels.styx.api.messages.HttpVersion.httpVersion;
import static com.hotels.styx.api.support.CookiesSupport.findCookie;
import static com.hotels.styx.api.support.CookiesSupport.isCookieHeader;
import static io.netty.buffer.ByteBufUtil.getBytes;
import static io.netty.buffer.Unpooled.compositeBuffer;
Expand Down Expand Up @@ -577,9 +578,7 @@ public Builder addCookie(String name, String value) {
* @return {@code this}
*/
public Builder removeCookie(String name) {
cookies.stream()
.filter(cookie -> cookie.name().equalsIgnoreCase(name))
.findFirst()
findCookie(cookies, name)
.ifPresent(cookies::remove);

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static com.hotels.styx.api.messages.HttpResponseStatus.statusWithCode;
import static com.hotels.styx.api.messages.HttpVersion.HTTP_1_1;
import static com.hotels.styx.api.messages.HttpVersion.httpVersion;
import static com.hotels.styx.api.support.CookiesSupport.findCookie;
import static io.netty.buffer.ByteBufUtil.getBytes;
import static io.netty.buffer.Unpooled.compositeBuffer;
import static io.netty.util.ReferenceCountUtil.release;
Expand Down Expand Up @@ -320,9 +321,7 @@ public Builder addCookie(String name, String value) {
* @return {@code this}
*/
public Builder removeCookie(String name) {
cookies.stream()
.filter(cookie -> cookie.name().equalsIgnoreCase(name))
.findFirst()
findCookie(cookies, name)
.ifPresent(cookies::remove);

return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
*/
package com.hotels.styx.api.support;

import com.hotels.styx.api.HttpCookie;

import java.util.Collection;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
* Does some validation for cookie header names.
*/
Expand All @@ -31,4 +38,20 @@ private CookiesSupport() {
public static boolean isCookieHeader(String header) {
return "Set-Cookie".equalsIgnoreCase(header) || "Cookie".equalsIgnoreCase(header);
}

/**
* Find a cookie with the specified {@code name}.
*
* @param cookies list of cookies
* @param name cookie name
* @return the cookie if present
*/
public static Optional<HttpCookie> findCookie(Collection<HttpCookie> cookies, String name){
requireNonNull(cookies);
requireNonNull(name);
return cookies
.stream()
.filter(cookie -> name.equals(cookie.name()))
.findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
*/
package com.hotels.styx.api.support;

import com.google.common.collect.ImmutableList;
import com.hotels.styx.api.HttpCookie;
import com.hotels.styx.api.HttpCookieAttribute;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Optional;

import static com.hotels.styx.api.support.CookiesSupport.findCookie;
import static com.hotels.styx.api.support.CookiesSupport.isCookieHeader;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
Expand All @@ -35,4 +42,17 @@ public void onlyRequestAndResponseCookieHeaderIsAllowed() {
assertThat(isCookieHeader("cookiex"), is(false));
}

@Test
public void canFindCookieName(){
HttpCookie expectedCookie = HttpCookie.cookie("cookie1", "value1", HttpCookieAttribute.httpOnly());
List<HttpCookie> cookies = ImmutableList.of(expectedCookie);
assertThat(findCookie(cookies, expectedCookie.name()),is(Optional.of(expectedCookie)));
}

@Test
public void cookieNamesAreCaseSensitive(){
HttpCookie expectedCookie = HttpCookie.cookie("cookie1", "value1", HttpCookieAttribute.httpOnly());
List<HttpCookie> cookies = ImmutableList.of(expectedCookie);
assertThat(findCookie(cookies, expectedCookie.name().toUpperCase()),is(Optional.empty()));
}
}

0 comments on commit 0f9ea5a

Please sign in to comment.