Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

86: Add cookie support to HttpLoader #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions java/utils/src/main/java/fr/eolya/utils/http/CookieHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package fr.eolya.utils.http;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;

/**
* This class is used to add cookie support to {@link HttpLoader} without changing its API.
*
* The idea behind it is to work around {@link HttpLoader} storing cookie info in a single String value (persisting the value of the cookie, but not
* domain and path). As a workaround, the three attributes of the cookie (Value, Domain and Path) are serialized into a single String and later parsed
* into a Cookie object.
*/
class CookieHelper {
private static final String UTF_8 = StandardCharsets.UTF_8.name();
private static final char SEPARATOR_CHAR = '$';
private static final String SERIALIZED_COOKIE_MARKER = "$SERIALIZED_COOKIE$";

static boolean isSerializedCookie(String value) {
return value.startsWith(SERIALIZED_COOKIE_MARKER);
}

static Cookie deserialize(String name, String value) {
String serializedCookie = StringUtils.substringAfter(value, SERIALIZED_COOKIE_MARKER);
String[] tokens = StringUtils.split(serializedCookie, SEPARATOR_CHAR);

try {
BasicClientCookie cookie = new BasicClientCookie(name, URLDecoder.decode(tokens[0], UTF_8));
cookie.setDomain(URLDecoder.decode(tokens[1], UTF_8));
cookie.setPath(URLDecoder.decode(tokens[2], UTF_8));
return cookie;
} catch (UnsupportedEncodingException impossible) {
throw new RuntimeException(impossible);
}
}

static String serialize(Cookie cookie) {
try {
StringBuilder sb = new StringBuilder();
sb.append(SERIALIZED_COOKIE_MARKER);
sb.append(URLEncoder.encode(cookie.getValue(), UTF_8)).append(SEPARATOR_CHAR);
sb.append(URLEncoder.encode(cookie.getDomain(), UTF_8)).append(SEPARATOR_CHAR);
sb.append(URLEncoder.encode(cookie.getPath(), UTF_8)).append(SEPARATOR_CHAR);
return sb.toString();

} catch (UnsupportedEncodingException impossible) {
throw new RuntimeException(impossible);
}
}
}
23 changes: 14 additions & 9 deletions java/utils/src/main/java/fr/eolya/utils/http/HttpLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,16 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
CookieStore cookieStore = new BasicCookieStore();
Iterator<Entry<String, String>> it = cookies.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>)it.next();
BasicClientCookie cookie = new BasicClientCookie(pairs.getKey(), pairs.getValue());
//cookie.setDomain("your domain");
cookie.setPath("/");
cookieStore.addCookie(cookie);
Map.Entry<String, String> pairs = it.next();
String cookieValue = pairs.getValue();
if (cookieValue != null && CookieHelper.isSerializedCookie(cookieValue)) {
cookieStore.addCookie(CookieHelper.deserialize(pairs.getKey(), cookieValue));
} else {
BasicClientCookie cookie = new BasicClientCookie(pairs.getKey(), pairs.getValue());
//cookie.setDomain("your domain");
cookie.setPath("/");
cookieStore.addCookie(cookie);
}
}
httpClient.setCookieStore(cookieStore);
}
Expand Down Expand Up @@ -676,6 +681,7 @@ public static Map<String, String> getAuthCookies(int authMode, String authLogin,
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
httpClient.setCookieStore(cookieStore);

try
{
Expand All @@ -691,15 +697,14 @@ public static Map<String, String> getAuthCookies(int authMode, String authLogin,
HttpResponse response = httpClient.execute(httpPost, localContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
entity.consumeContent();
EntityUtils.consume(entity);
}

List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (!cookies.isEmpty()) {
authCookies = new HashMap<String, String>();
authCookies = new HashMap<>();
for (Cookie c : cookies) {
// TODO: What about the path, the domain ???
authCookies.put(c.getName(), c.getValue());
authCookies.put(c.getName(), CookieHelper.serialize(c));
}
}
httpPost.abort();
Expand Down
39 changes: 39 additions & 0 deletions java/utils/src/test/java/fr/eolya/utils/http/CookieHelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fr.eolya.utils.http;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.SetCookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.junit.Test;

public class CookieHelperTest {
private static final String COOKIE_NAME = "cookieName";
private static final String COOKIE_VALUE = "fancy$value%22xx";
private static final String COOKIE_PATH = "/bla$action";
private static final String COOKIE_DOMAIN = "www.domain.com";

@Test
public void testSerialization() throws Exception {
SetCookie cookie = new BasicClientCookie(COOKIE_NAME, COOKIE_VALUE);
cookie.setPath(COOKIE_PATH);
cookie.setDomain(COOKIE_DOMAIN);

String serializedCookie = CookieHelper.serialize(cookie);

assertTrue(CookieHelper.isSerializedCookie(serializedCookie));
assertEquals("$SERIALIZED_COOKIE$fancy%24value%2522xx$www.domain.com$%2Fbla%24action$", serializedCookie);
}

@Test
public void testDeserialization() throws Exception {
String serializedCookie = "$SERIALIZED_COOKIE$fancy%24value%2522xx$www.domain.com$%2Fbla%24action$";

Cookie cookie = CookieHelper.deserialize(COOKIE_NAME, serializedCookie);
assertEquals(COOKIE_NAME, cookie.getName());
assertEquals(COOKIE_VALUE, cookie.getValue());
assertEquals(COOKIE_PATH, cookie.getPath());
assertEquals(COOKIE_DOMAIN, cookie.getDomain());
}
}