Skip to content

Commit 085db38

Browse files
committed
Handling content-types in BinaryHttpResponseHandler
1 parent e8c63f8 commit 085db38

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

library/src/com/loopj/android/http/AsyncHttpRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package com.loopj.android.http;
2020

21+
import android.util.Log;
22+
2123
import org.apache.http.HttpResponse;
2224
import org.apache.http.client.HttpRequestRetryHandler;
2325
import org.apache.http.client.methods.HttpUriRequest;
@@ -110,6 +112,7 @@ private void makeRequestWithRetries() throws IOException {
110112
}
111113
} catch (Exception e) {
112114
// catch anything else to ensure failure message is propagated
115+
Log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
113116
cause = new IOException("Unhandled exception: " + e.getMessage());
114117
}
115118

library/src/com/loopj/android/http/BinaryHttpResponseHandler.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818

1919
package com.loopj.android.http;
2020

21+
import android.util.Log;
22+
2123
import org.apache.http.Header;
2224
import org.apache.http.HttpResponse;
2325
import org.apache.http.StatusLine;
2426
import org.apache.http.client.HttpResponseException;
2527

2628
import java.io.IOException;
2729
import java.util.regex.Pattern;
30+
import java.util.regex.PatternSyntaxException;
2831

2932
/**
3033
* Used to intercept and handle the responses from requests made using
@@ -60,6 +63,8 @@ public class BinaryHttpResponseHandler extends AsyncHttpResponseHandler {
6063
/**
6164
* Method can be overriden to return allowed content types,
6265
* can be sometimes better than passing data in constructor
66+
*
67+
* @return array of content-types or Pattern string templates (eg. '.*' to match every response)
6368
*/
6469
public String[] getAllowedContentTypes() {
6570
return mAllowedContentTypes;
@@ -76,7 +81,7 @@ public BinaryHttpResponseHandler() {
7681
* Creates a new BinaryHttpResponseHandler, and overrides the default allowed
7782
* content types with passed String array (hopefully) of content types.
7883
*
79-
* @param allowedContentTypes content types array, eg. 'image/jpeg'
84+
* @param allowedContentTypes content types array, eg. 'image/jpeg' or pattern '.*'
8085
*/
8186
public BinaryHttpResponseHandler(String[] allowedContentTypes) {
8287
this();
@@ -150,8 +155,12 @@ protected void sendResponseMessage(HttpResponse response) throws IOException {
150155
Header contentTypeHeader = contentTypeHeaders[0];
151156
boolean foundAllowedContentType = false;
152157
for (String anAllowedContentType : getAllowedContentTypes()) {
153-
if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
154-
foundAllowedContentType = true;
158+
try {
159+
if (Pattern.matches(anAllowedContentType, contentTypeHeader.getValue())) {
160+
foundAllowedContentType = true;
161+
}
162+
} catch (PatternSyntaxException e) {
163+
Log.e("BinaryHttpResponseHandler", "Given pattern is not valid: " + anAllowedContentType, e);
155164
}
156165
}
157166
if (!foundAllowedContentType) {

0 commit comments

Comments
 (0)