From f1639153468485070271412ca761e080f59068b3 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Mon, 27 May 2013 14:46:40 -0700 Subject: [PATCH] Null-check request entity since it might not be present. Closes #198. --- .../okhttp/apache/OkApacheClient.java | 38 ++++++++++--------- .../okhttp/apache/OkApacheClientTest.java | 9 +++++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/okhttp-apache/src/main/java/com/squareup/okhttp/apache/OkApacheClient.java b/okhttp-apache/src/main/java/com/squareup/okhttp/apache/OkApacheClient.java index c381f6723..73e5f8857 100644 --- a/okhttp-apache/src/main/java/com/squareup/okhttp/apache/OkApacheClient.java +++ b/okhttp-apache/src/main/java/com/squareup/okhttp/apache/OkApacheClient.java @@ -125,25 +125,27 @@ public class OkApacheClient implements HttpClient { // Stream the request body. if (request instanceof HttpEntityEnclosingRequest) { HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); - connection.setDoOutput(true); - Header type = entity.getContentType(); - if (type != null) { - connection.addRequestProperty(type.getName(), type.getValue()); + if (entity != null) { + connection.setDoOutput(true); + Header type = entity.getContentType(); + if (type != null) { + connection.addRequestProperty(type.getName(), type.getValue()); + } + Header encoding = entity.getContentEncoding(); + if (encoding != null) { + connection.addRequestProperty(encoding.getName(), encoding.getValue()); + } + if (entity.isChunked() || entity.getContentLength() < 0) { + connection.setChunkedStreamingMode(0); + } else if (entity.getContentLength() <= 8192) { + // Buffer short, fixed-length request bodies. This costs memory, but permits the request + // to be transparently retried if there is a connection failure. + connection.addRequestProperty("Content-Length", Long.toString(entity.getContentLength())); + } else { + connection.setFixedLengthStreamingMode((int) entity.getContentLength()); + } + entity.writeTo(connection.getOutputStream()); } - Header encoding = entity.getContentEncoding(); - if (encoding != null) { - connection.addRequestProperty(encoding.getName(), encoding.getValue()); - } - if (entity.isChunked() || entity.getContentLength() < 0) { - connection.setChunkedStreamingMode(0); - } else if (entity.getContentLength() <= 8192) { - // Buffer short, fixed-length request bodies. This costs memory, but permits the request to - // be transparently retried if there is a connection failure. - connection.addRequestProperty("Content-Length", Long.toString(entity.getContentLength())); - } else { - connection.setFixedLengthStreamingMode((int) entity.getContentLength()); - } - entity.writeTo(connection.getOutputStream()); } // Read the response headers. diff --git a/okhttp-apache/src/test/java/com/squareup/okhttp/apache/OkApacheClientTest.java b/okhttp-apache/src/test/java/com/squareup/okhttp/apache/OkApacheClientTest.java index 4356d5eec..b68447ba4 100644 --- a/okhttp-apache/src/test/java/com/squareup/okhttp/apache/OkApacheClientTest.java +++ b/okhttp-apache/src/test/java/com/squareup/okhttp/apache/OkApacheClientTest.java @@ -6,6 +6,7 @@ import java.io.IOException; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.util.EntityUtils; import org.junit.After; import org.junit.Test; @@ -68,4 +69,12 @@ public class OkApacheClientTest { assertEquals("Bar", headers2[0].getValue()); assertEquals("Baz", headers2[1].getValue()); } + + @Test public void noEntity() throws Exception { + server.enqueue(new MockResponse()); + server.play(); + + HttpPost post = new HttpPost(server.getUrl("/").toURI()); + client.execute(post); + } }