1
0
mirror of https://github.com/square/okhttp.git synced 2026-01-25 16:01:38 +03:00

Merge pull request #199 from square/jw/null-chick

Null-check request entity since it might not be present.
This commit is contained in:
Jake Wharton
2013-05-27 14:53:44 -07:00
2 changed files with 29 additions and 18 deletions

View File

@@ -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.

View File

@@ -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);
}
}