Browse code

Fixed Android network mishandling null payload entities

When determining the payload strings from a successful HTTP request,
the previous implementation of AndroidNet would throw an
IllegalArgumentException and fail the request if the response entity is
null, such as in the case of a 204 status code. Updated implementation
handles null response entities by invoking success on the callback with
the appropriate status code and null for the payload methods.

Bryan Chacosky authored on 06/05/2014 20:43:55
Showing 1 changed files
... ...
@@ -81,30 +81,63 @@ class AndroidNet extends NetImpl {
81 81
           final HttpResponse response = httpclient.execute(hreq);
82 82
           int code = response.getStatusLine().getStatusCode();
83 83
           HttpEntity entity = response.getEntity();
84
-          byte[] data = EntityUtils.toByteArray(entity);
85
-          String encoding = EntityUtils.getContentCharSet(entity);
86
-          if (encoding == null) encoding = HTTP.UTF_8;
87
-          platform.notifySuccess(callback, new BinaryResponse(code, data, encoding) {
88
-            @Override
89
-            protected Map<String,List<String>> extractHeaders() {
90
-              Map<String,List<String>> hmap = new HashMap<String,List<String>>();
91
-              for (org.apache.http.Header header : response.getAllHeaders()) {
92
-                String name = header.getName();
93
-                List<String> values = hmap.get(name);
94
-                if (values == null) hmap.put(name, values = new ArrayList<String>());
95
-                values.add(header.getValue());
84
+
85
+          ResponseImpl impl;
86
+          if (entity == null) {
87
+              impl = new ResponseImpl(code) {
88
+                @Override
89
+                public String payloadString() {
90
+                    return null;
91
+                }
92
+
93
+                @Override
94
+                public byte[] payload() {
95
+                    return null;
96
+                }
97
+
98
+                @Override
99
+                protected Map<String, List<String>> extractHeaders() {
100
+                    return extractResponseHeaders(response);
101
+                }
102
+            };
103
+          } else {
104
+              byte[] data = EntityUtils.toByteArray(entity);
105
+              String encoding = EntityUtils.getContentCharSet(entity);
106
+              if (encoding == null) {
107
+                encoding = HTTP.UTF_8;
96 108
               }
97
-              return hmap;
98
-            }
99
-          });
109
+
110
+              impl = new BinaryResponse(code, data, encoding) {
111
+                  @Override
112
+                  protected Map<String,List<String>> extractHeaders() {
113
+                      return extractResponseHeaders(response);
114
+                  }
115
+                };
116
+          }
117
+
118
+          platform.notifySuccess(callback, impl);
100 119
         } catch (Exception e) {
101 120
           platform.notifyFailure(callback, e);
102 121
         }
103 122
       }
123
+
104 124
       @Override
105 125
       public String toString() {
106 126
         return "AndroidNet.exec(" + req.method() + ", " + req.url + ")";
107 127
       }
128
+
129
+      private Map<String,List<String>> extractResponseHeaders(HttpResponse response) {
130
+          Map<String,List<String>> hmap = new HashMap<String,List<String>>();
131
+          for (org.apache.http.Header header : response.getAllHeaders()) {
132
+            String name = header.getName();
133
+            List<String> values = hmap.get(name);
134
+            if (values == null) {
135
+                hmap.put(name, values = new ArrayList<String>());
136
+            }
137
+            values.add(header.getValue());
138
+          }
139
+          return hmap;
140
+      }
108 141
     });
109 142
   }
110 143
 }