Skip to content

Commit

Permalink
Removed cookie store, added parser to extract cookies from cookie hea…
Browse files Browse the repository at this point in the history
…ders
  • Loading branch information
gomezd committed Aug 16, 2011
1 parent 11c5d68 commit 75b1d2f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/main/java/org/browsermob/proxy/http/BrowserMobHttpClient.java
Expand Up @@ -5,7 +5,6 @@
import cz.mallat.uasparser.UserAgentInfo;
import org.apache.http.*;
import org.apache.http.auth.*;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.*;
import org.apache.http.client.params.ClientPNames;
Expand All @@ -23,7 +22,6 @@
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.cookie.*;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
Expand Down Expand Up @@ -481,8 +479,6 @@ private BrowserMobHttpResponse execute(BrowserMobHttpRequest req, int depth) {
HttpResponse response = null;

BasicHttpContext ctx = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
ctx.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

ActiveRequest activeRequest = new ActiveRequest(method, ctx, entry.getStartedDateTime());
synchronized (activeRequests) {
Expand Down Expand Up @@ -657,6 +653,11 @@ public HeaderElement[] getElements() throws ParseException {
}
}
*/

//capture request cookies
CookieHeadersParser cookieParser = new CookieHeadersParser();
List<HarCookie> cookies = cookieParser.getCookies(method);
entry.getRequest().setCookies(cookies);

String contentType = null;

Expand All @@ -682,6 +683,10 @@ public HeaderElement[] getElements() throws ParseException {
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}

//capture response cookies
cookies = cookieParser.getCookies(response);
entry.getResponse().setCookies(cookies);
}

if (contentType != null) {
Expand Down Expand Up @@ -752,16 +757,6 @@ public HeaderElement[] getElements() throws ParseException {
}
}

List<Cookie> cookies = cookieStore.getCookies();
for (Cookie c : cookies) {
HarCookie hc = new HarCookie();
hc.setName(c.getName());
hc.setPath(c.getPath());
hc.setValue(c.getValue());
hc.setDomain(c.getDomain());
hc.setExpires(c.getExpiryDate());
entry.getRequest().getCookies().add(hc);
}

return new BrowserMobHttpResponse(entry, method, response, contentMatched, verificationText, errorMessage, responseBody, contentType, charSet);
}
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/org/browsermob/proxy/http/CookieHeadersParser.java
@@ -0,0 +1,106 @@
package org.browsermob.proxy.http;

import static com.google.common.collect.Lists.newLinkedList;
import static com.google.common.collect.Maps.newHashMap;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.browsermob.core.har.HarCookie;
import org.browsermob.core.har.HarNameValuePair;

import com.google.common.collect.Lists;

/**
* A very basic cookie parser
* @author dgomez
*
*/
public class CookieHeadersParser {

//rfc2616#section-3.3.1
public static final DateFormat RFC882_DATE_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
public static final DateFormat RFC850_DATE_FORMAT = new SimpleDateFormat("E, dd-MMM-yy HH:mm:ss z");
public static final DateFormat ASCII_DATE_FORMAT = new SimpleDateFormat("EEE, MM d HH:mm:ss yyyy");

public List<HarCookie> getCookies(HttpResponse response) {
List<HarCookie> cookies = Lists.newLinkedList();
for(Header hdr : response.getHeaders("Set-Cookie")) {
Map<String, String> cookieData = parseSetCookieHeader(hdr);

HarCookie cookie = new HarCookie();
cookie.setName(cookieData.get("name"));
cookie.setValue(cookieData.get("value"));
cookie.setPath(cookieData.get("path"));
cookie.setDomain(cookieData.get("domain"));

String expires = cookieData.get("expires");
if (expires != null) {
Date date = parseDate(expires);
cookie.setExpires(date);
}
cookies.add(cookie);
}
return cookies;
}

public List<HarCookie> getCookies(HttpRequest request) {
List<HarCookie> cookies = newLinkedList();
for(Header hdr : request.getHeaders("Cookie")) {
String[] pairs = hdr.getValue().split("; ");
for (String p : pairs) {
HarNameValuePair pair = nameValuePair(p);
HarCookie cookie = new HarCookie();
cookie.setName(pair.getName());
cookie.setValue(pair.getValue());
cookies.add(cookie);
}
}
return cookies;
}

private Map<String, String> parseSetCookieHeader(Header setCookieHdr) {
String[] pairs= setCookieHdr.getValue().split("; ");
HarNameValuePair pair = nameValuePair(pairs[0]);

Map<String, String> cookieData = newHashMap();
cookieData.put("name", pair.getName());
cookieData.put("value", pair.getValue());

for (int i = 1; i < pairs.length; i++) {
pair = nameValuePair(pairs[i]);
cookieData.put(pair.getName(), pair.getValue());
}
return cookieData;
}

private HarNameValuePair nameValuePair(String data) {
int eqIdx = data.indexOf("=");
if (eqIdx > 0) {
String name = data.substring(0, eqIdx);
String val = data.substring(eqIdx + 1);
return new HarNameValuePair(name, val);
}
else return new HarNameValuePair(data, "");
}

public Date parseDate(String dateString) {
//try different formats to comply with rfc2616#section-3.3.1
DateFormat[] formatters = {RFC882_DATE_FORMAT, RFC850_DATE_FORMAT, ASCII_DATE_FORMAT};
for (DateFormat df : formatters) {
try {
return df.parse(dateString);
} catch (ParseException e) {
//ignore
}
}
return null;
}
}

0 comments on commit 75b1d2f

Please sign in to comment.