Commit aa7480cf authored by Ricki Hirner's avatar Ricki Hirner

Close response bodies when not needed

parent 324db079
......@@ -60,7 +60,7 @@ public class DavAddressBook extends DavResource {
.header("Depth", "1")
.build()).execute();
checkStatus(response);
checkStatus(response, false);
assertMultiStatus(response);
members.clear();
......@@ -106,7 +106,7 @@ public class DavAddressBook extends DavResource {
.header("Depth", "0") // "The request MUST include a Depth: 0 header [...]"
.build()).execute();
checkStatus(response);
checkStatus(response, false);
assertMultiStatus(response);
members.clear();
......
......@@ -85,7 +85,7 @@ public class DavCalendar extends DavResource {
.header("Depth", "1")
.build()).execute();
checkStatus(response);
checkStatus(response, false);
assertMultiStatus(response);
members.clear();
......@@ -125,7 +125,7 @@ public class DavCalendar extends DavResource {
.method("REPORT", RequestBody.create(MIME_XML, writer.toString()))
.build()).execute();
checkStatus(response);
checkStatus(response, false);
assertMultiStatus(response);
members.clear();
......
......@@ -105,13 +105,10 @@ public class DavResource {
.method("OPTIONS", null)
.url(location)
.build()).execute();
checkStatus(response);
checkStatus(response, true);
for (String capability : HttpUtils.listHeader(response, "DAV"))
capabilities.add(capability.trim());
if (response.body() != null)
response.body().close();
}
public void mkCol(String xmlBody) throws IOException, HttpException {
......@@ -128,10 +125,7 @@ public class DavResource {
else
break;
}
checkStatus(response);
if (response.body() != null)
response.body().close();
checkStatus(response, true);
}
/**
......@@ -154,7 +148,7 @@ public class DavResource {
else
break;
}
checkStatus(response);
checkStatus(response, false);
String eTag = response.header("ETag");
if (TextUtils.isEmpty(eTag))
......@@ -204,10 +198,7 @@ public class DavResource {
} else
break;
}
checkStatus(response);
if (response.body() != null)
response.body().close();
checkStatus(response, true);
String eTag = response.header("ETag");
if (TextUtils.isEmpty(eTag))
......@@ -230,17 +221,15 @@ public class DavResource {
if (ifMatchETag != null)
builder.header("If-Match", StringUtils.asQuotedString(ifMatchETag));
Response response = httpClient.newCall(builder.build()).execute();
checkStatus(response);
if (response.body() != null)
response.body().close();
checkStatus(response, false);
if (response.code() == 207) {
/* If an error occurs deleting a member resource (a resource other than
the resource identified in the Request-URI), then the response can be
a 207 (Multi-Status). […] (RFC 4918 9.6.1. DELETE for Collections) */
throw new HttpException(response);
}
} else
closeBody(response);
}
/**
......@@ -283,7 +272,7 @@ public class DavResource {
break;
}
checkStatus(response);
checkStatus(response, false);
assertMultiStatus(response);
if (depth > 0)
......@@ -297,6 +286,17 @@ public class DavResource {
// status handling
protected void checkStatus(Response response, boolean closeBody) throws HttpException {
checkStatus(response.code(), response.message(), response);
if (closeBody)
closeBody(response);
}
protected void checkStatus(StatusLine status) throws HttpException {
checkStatus(status.code, status.message, null);
}
protected void checkStatus(int code, String message, Response response) throws HttpException {
if (code/100 == 2)
// everything OK
......@@ -318,14 +318,6 @@ public class DavResource {
}
}
protected void checkStatus(Response response) throws HttpException {
checkStatus(response.code(), response.message(), response);
}
protected void checkStatus(StatusLine status) throws HttpException {
checkStatus(status.code, status.message, null);
}
protected void assertMultiStatus(Response response) throws DavException {
if (response.code() != 207)
throw new InvalidDavResponseException("Expected 207 Multi-Status, got " + response.code() + " " + response.message());
......@@ -349,6 +341,8 @@ public class DavResource {
if (href != null)
target = location.resolve(href);
closeBody(response);
if (target != null) {
log.fine("Redirected, new location=" + target);
location = target;
......@@ -356,6 +350,11 @@ public class DavResource {
throw new HttpException("Redirected without new Location");
}
protected void closeBody(Response response) {
if (response != null && response.body() != null)
response.body().close();
}
// multi-status handling
......
......@@ -8,12 +8,15 @@
package at.bitfire.dav4android.exception;
import java.io.Closeable;
import java.io.IOException;
import java.io.Serializable;
import at.bitfire.dav4android.Constants;
import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
public class HttpException extends Exception implements Serializable {
......@@ -53,11 +56,11 @@ public class HttpException extends Exception implements Serializable {
// format request
Request request = response.request();
StringBuilder formatted = new StringBuilder();
formatted.append(request.method() + " " + request.url().encodedPath() + "\n");
formatted.append(request.method()).append(" ").append(request.url().encodedPath()).append("\n");
Headers headers = request.headers();
for (String name : headers.names())
for (String value : headers.values(name))
formatted.append(name + ": " + value + "\n");
formatted.append(name).append(": ").append(value).append("\n");
if (request.body() != null)
try {
formatted.append("\n");
......@@ -66,23 +69,28 @@ public class HttpException extends Exception implements Serializable {
while (!buffer.exhausted())
appendByte(formatted, buffer.readByte());
} catch (IOException e) {
Constants.log.warning("Couldn't read request body");
}
this.request = formatted.toString();
// format response
formatted = new StringBuilder();
formatted.append(response.protocol() + " " + response.code() + " " + response.message() + "\n");
formatted.append(response.protocol()).append(" ").append(response.code()).append(" ").append(response.message()).append("\n");
headers = response.headers();
for (String name : headers.names())
for (String value : headers.values(name))
formatted.append(name + ": " + value + "\n");
if (response.body() != null)
formatted.append(name).append(": ").append(value).append("\n");
if (response.body() != null) {
ResponseBody body = response.body();
try {
formatted.append("\n");
for (byte b : response.body().bytes())
for (byte b : body.bytes())
appendByte(formatted, b);
} catch (IOException e) {
} catch(IOException e) {
Constants.log.warning("Couldn't read response body");
}
body.close();
}
this.response = formatted.toString();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment