From d40a0267b7a68e77e44513594fc779cd0d6a384f Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Wed, 13 Feb 2013 11:11:59 +0100 Subject: [PATCH 1/2] Allow setting of accepted content encodings and charset (issue 6). --- src/org/pixmob/httpclient/HttpClient.java | 38 +++++++++++++++++++ .../pixmob/httpclient/HttpRequestBuilder.java | 13 +++---- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/org/pixmob/httpclient/HttpClient.java b/src/org/pixmob/httpclient/HttpClient.java index 2149426a..39c77c74 100644 --- a/src/org/pixmob/httpclient/HttpClient.java +++ b/src/org/pixmob/httpclient/HttpClient.java @@ -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 inMemoryCookies = new HashMap(8); /** @@ -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 getInMemoryCookies() { return inMemoryCookies; } diff --git a/src/org/pixmob/httpclient/HttpRequestBuilder.java b/src/org/pixmob/httpclient/HttpRequestBuilder.java index 765a5f9b..9be3ff35 100644 --- a/src/org/pixmob/httpclient/HttpRequestBuilder.java +++ b/src/org/pixmob/httpclient/HttpRequestBuilder.java @@ -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> NO_HEADERS = new HashMap>(0); private static TrustManager[] trustManagers; private final byte[] buffer = new byte[1024]; @@ -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); @@ -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); @@ -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); } From 110276de98fc1f0035da2a9fe11b2a9892644064 Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Wed, 3 Apr 2013 14:35:57 +0200 Subject: [PATCH 2/2] If no cookies in a 'Set-Cookies' header are found, try a lowercase 'set-cookies' header, as workaround for the pre-Gingerbread lowercase header bug (issue #9). --- src/org/pixmob/httpclient/HttpRequestBuilder.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/org/pixmob/httpclient/HttpRequestBuilder.java b/src/org/pixmob/httpclient/HttpRequestBuilder.java index 9be3ff35..8e3354d2 100644 --- a/src/org/pixmob/httpclient/HttpRequestBuilder.java +++ b/src/org/pixmob/httpclient/HttpRequestBuilder.java @@ -315,7 +315,13 @@ public HttpResponse execute() throws HttpClientException { final Map> headerFields = conn.getHeaderFields(); final Map inMemoryCookies = hc.getInMemoryCookies(); if (headerFields != null) { - final List newCookies = headerFields.get("Set-Cookie"); + List 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];