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

Added set-cookie header parsing into in-memory cookies #10

Open
wants to merge 2 commits 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
38 changes: 38 additions & 0 deletions src/org/pixmob/httpclient/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ public final class HttpClient {
}

private static final String DEFAULT_USER_AGENT = getDefaultUserAgent();
private static final String DEFAULT_CONTENT_CHARSET = "UTF-8";
private static final String DEFAULT_ACCEPTED_ENCODINGS = "gzip,deflate";
private final Context context;
private int connectTimeout;
private int readTimeout;
private String userAgent;
private String contentCharset;
private String acceptedEncodings;
private final Map<String, String> inMemoryCookies = new HashMap<String, String>(8);

/**
Expand Down Expand Up @@ -162,6 +166,40 @@ public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}

/**
* Get the accepted character set to encode content in with every request.
*/
public String getContentCharset() {
if (contentCharset == null) {
return DEFAULT_CONTENT_CHARSET;
}
return contentCharset;
}

/**
* Set the accepted character set to encode content in with every request.
*/
public void setContentCharset(String contentCharset) {
this.contentCharset = contentCharset;
}

/**
* Get the accepted encodings for every request.
*/
public String getAcceptedEncodings() {
if (acceptedEncodings == null) {
return DEFAULT_ACCEPTED_ENCODINGS;
}
return acceptedEncodings;
}

/**
* Set the accepted encodings for every request.
*/
public void setAcceptedEncodings(String acceptedEncodings) {
this.acceptedEncodings = acceptedEncodings;
}

Map<String, String> getInMemoryCookies() {
return inMemoryCookies;
}
Expand Down
21 changes: 13 additions & 8 deletions src/org/pixmob/httpclient/HttpRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
*/
public final class HttpRequestBuilder {
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
private static final String CONTENT_CHARSET = "UTF-8";
private static final Map<String, List<String>> NO_HEADERS = new HashMap<String, List<String>>(0);
private static TrustManager[] trustManagers;
private final byte[] buffer = new byte[1024];
Expand Down Expand Up @@ -209,15 +208,15 @@ public HttpResponse execute() throws HttpClientException {
}
final String name = e.getKey();
final String value = e.getValue();
buf.append(URLEncoder.encode(name, CONTENT_CHARSET)).append("=")
.append(URLEncoder.encode(value, CONTENT_CHARSET));
buf.append(URLEncoder.encode(name, hc.getContentCharset())).append("=")
.append(URLEncoder.encode(value, hc.getContentCharset()));
++paramIdx;
}

if (!contentSet
&& (HTTP_POST.equals(method) || HTTP_DELETE.equals(method) || HTTP_PUT.equals(method))) {
try {
content = buf.toString().getBytes(CONTENT_CHARSET);
content = buf.toString().getBytes(hc.getContentCharset());
} catch (UnsupportedEncodingException e) {
// Unlikely to happen.
throw new HttpClientException("Encoding error", e);
Expand Down Expand Up @@ -264,8 +263,8 @@ public HttpResponse execute() throws HttpClientException {
conn.setRequestProperty("Connection", "close");
conn.setRequestProperty("Location", uri);
conn.setRequestProperty("Referrer", uri);
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
conn.setRequestProperty("Accept-Charset", CONTENT_CHARSET);
conn.setRequestProperty("Accept-Encoding", hc.getAcceptedEncodings());
conn.setRequestProperty("Accept-Charset", hc.getContentCharset());

if (conn instanceof HttpsURLConnection) {
setupSecureConnection(hc.getContext(), (HttpsURLConnection) conn);
Expand All @@ -276,7 +275,7 @@ public HttpResponse execute() throws HttpClientException {
conn.setDoOutput(true);
if (!contentSet) {
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset="
+ CONTENT_CHARSET);
+ hc.getContentCharset());
} else if (contentType != null) {
conn.setRequestProperty("Content-Type", contentType);
}
Expand Down Expand Up @@ -316,7 +315,13 @@ public HttpResponse execute() throws HttpClientException {
final Map<String, List<String>> headerFields = conn.getHeaderFields();
final Map<String, String> inMemoryCookies = hc.getInMemoryCookies();
if (headerFields != null) {
final List<String> newCookies = headerFields.get("Set-Cookie");
List<String> newCookies = headerFields.get("Set-Cookie");
if (newCookies == null) {
// Malicious web servers may return lower case header fields
// and before Gingerbread HttpURLConnection does not correct
// these properly; instead parse "set-cookie" headers too.
newCookies = headerFields.get("set-cookie");
}
if (newCookies != null) {
for (final String newCookie : newCookies) {
final String rawCookie = newCookie.split(";", 2)[0];
Expand Down