Skip to content

Commit

Permalink
issue #341 handle = in cookie values and do not urlDecode by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Mar 7, 2020
1 parent 155d34b commit 7e8bd41
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
10 changes: 7 additions & 3 deletions unirest/src/main/java/kong/unirest/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ private Cookie(String[] split){
int pos = 0;
for(String s : split){
if(pos == 0){
String[] sub = s.split("=");
String[] sub = s.split("=",2);
name = sub[0];
if (sub.length == 2) {
value = getDecode(sub[1]);
value = stripQuoteWrapper(sub[1]);
} else {
value = "";
}
Expand All @@ -85,7 +85,7 @@ private Cookie(String[] split){

private String getDecode(String sub) {
try {
return URLDecoder.decode(stripQuoteWrapper(sub), "UTF-8");
return URLDecoder.decode(sub, "UTF-8");
} catch (UnsupportedEncodingException e) {
return sub;
}
Expand Down Expand Up @@ -183,6 +183,10 @@ public void setHttpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
}

public String getUrlDecodedValue() {
return getDecode(value);
}

private static class Pair {
final String key;
final String value;
Expand Down
4 changes: 2 additions & 2 deletions unirest/src/test/java/BehaviorTests/CookieTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void canGetUnicode() {

HttpResponse response = Unirest.get(MockServer.GET).asEmpty();

assertEquals("फनकी", response.getCookies().getNamed("nepali").getValue());
assertEquals("फनकी", response.getCookies().getNamed("nepali").getUrlDecodedValue());
}

@Test
Expand All @@ -95,7 +95,7 @@ public void canGetValuesWithBadCharacters() {

HttpResponse response = Unirest.get(MockServer.GET).asEmpty();

assertEquals("1=2;3=4", response.getCookies().getNamed("odd").getValue());
assertEquals("1=2;3=4", response.getCookies().getNamed("odd").getUrlDecodedValue());
}

@Test
Expand Down
21 changes: 21 additions & 0 deletions unirest/src/test/java/kong/unirest/CookieParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package kong.unirest;

import org.eclipse.jetty.server.CookieCutter;
import org.junit.Test;

import java.time.ZoneId;
Expand Down Expand Up @@ -78,6 +79,19 @@ public void secure() {
assertEquals("color=blue;Secure", c.toString());
}


@Test
public void matchJettyParsing() {
String s = "_id=A528A0D64DA61CB01241EF6E18E4D675170DDB56CB430000AF58625E920CF940~pl1ZYwDDdoAnfY3RtqZ2Ti1MYOf7Q8jRrFQLneSGK7qQoUX1GHW/xDcqweGyclm5rm/g/YFCV3ohuHoz2oad5M0MX9Ru9V7bFr2s08d1lHxbn39gw71AI+ZVejq5FpMHKBzyjoBGG6NY6xYVTwP9NHo14SY0CXs60k2UTpJsOTNzAHIZaedg7o6R/8qyAQ8GF25K2o773pFLrYtjgKHohkk5ukz/yEGQitq8NgC5hiqX0=; expires=Fri, 06 Mar 2020 16:05:35 GMT; max-age=7200; path=/; domain=xxx.com; HttpOnly";
CookieCutter cutter = new CookieCutter();
cutter.addCookieField(s);
javax.servlet.http.Cookie jetty = cutter.getCookies()[0];

Cookie unirest = new Cookie(s);

assertEquals(jetty.getValue(), unirest.getValue());
}

@Test
public void futurePropsDontBreak() {
String v = "color=blue;TheFuture";
Expand Down Expand Up @@ -108,6 +122,13 @@ public void justEmptyQuotes() {
assertEquals("", c.getValue());
}

@Test
public void valuesCanHaveEquals() {
String v = "SignOnDefault====;";
Cookie c = new Cookie(v);
assertEquals("===", c.getValue());
}

@Test
public void justOneQuote() {
String v = "SignOnDefault=\";";
Expand Down

0 comments on commit 7e8bd41

Please sign in to comment.