Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix response leaks & add basic InputStream support #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public interface Sardine
// InputStream not supported
// See https://github.com/square/okhttp/issues/2424

// void put(String url, InputStream dataStream) throws IOException;
void put(String url, InputStream dataStream) throws IOException;

/**
* Uses <code>PUT</code> to send data to a server with a specific content type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.thegrizzlylabs.sardineandroid.DavAce;
import com.thegrizzlylabs.sardineandroid.DavAcl;
import com.thegrizzlylabs.sardineandroid.DavPrincipal;
Expand Down Expand Up @@ -39,6 +41,7 @@
import com.thegrizzlylabs.sardineandroid.report.SardineReport;
import com.thegrizzlylabs.sardineandroid.util.SardineUtil;

import org.apache.commons.net.io.Util;
import org.w3c.dom.Element;

import java.io.File;
Expand All @@ -60,6 +63,9 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

/**
* Created by guillaume on 08/11/2017.
Expand Down Expand Up @@ -289,6 +295,22 @@ public void put(String url, byte[] data) throws IOException {
this.put(url, data, null);
}

@Override
public void put(String url, InputStream dataStream) throws IOException
{
RequestBody rb = create(MediaType.parse("binary/octet-stream"), dataStream);

Request request = new Request.Builder()
.url(url)
.put(rb)
.addHeader("Transfer-Encoding", "chunked")
.addHeader("Expect", "100-Continue")
.addHeader("Content-Length", "-1")
.build();

Response r = client.newCall(request).execute();
}

@Override
public void put(String url, byte[] data, String contentType) throws IOException {
MediaType mediaType = contentType == null ? null : MediaType.parse(contentType);
Expand Down Expand Up @@ -377,6 +399,7 @@ public void move(String sourceUrl, String destinationUrl, boolean overwrite, Str
}
builder.headers(headersBuilder.build());
Request request = builder.build();

execute(request);
}

Expand Down Expand Up @@ -456,7 +479,7 @@ public void unlock(String url, String token) throws IOException {
.header("Lock-Token", "<" + token + ">")
.build();

execute(request, new VoidResponseHandler());
execute(request);
}

@Override
Expand Down Expand Up @@ -527,7 +550,7 @@ public void setAcl(String url, List<DavAce> aces) throws IOException {
.method("ACL", requestBody)
.build();

this.execute(request, new VoidResponseHandler());
this.execute(request);
}

@Override
Expand Down Expand Up @@ -627,4 +650,36 @@ private <T> T execute(Request request, ResponseHandler<T> responseHandler) throw
return responseHandler.handleResponse(response);
}

private RequestBody create(final MediaType mediaType, final InputStream inputStream)
{
return new RequestBody()
{
@Override
public MediaType contentType()
{
return mediaType;
}

@Override
public long contentLength()
{
return -1;
}

@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException
{
Source source = null;
try
{
source = Okio.source(inputStream);
sink.writeAll(source);
}
finally
{
Util.closeQuietly(source);
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public class ExistsResponseHandler extends ValidatingResponseHandler<Boolean>
@Override
public Boolean handleResponse(Response response) throws SardineException {
if (!response.isSuccessful() && response.code() == 404) {
response.close();
return false;
}
validateResponse(response);
response.close();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public String handleResponse(Response response) throws IOException {
throw new SardineException("No entity found in response", response.code(), response.message());
}

return getToken(body.byteStream());
String token = getToken(body.byteStream());

body.close();

return token;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public Multistatus handleResponse(Response response) throws IOException {
throw new SardineException("No entity found in response", response.code(), response.message());
}

return getMultistatus(body.byteStream());
Multistatus status = getMultistatus(body.byteStream());
body.close();

return status;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public List<DavResource> handleResponse(Response response) throws IOException {
Log.w(TAG, String.format("Ignore resource with invalid URI %s", davResponse.getHref()/*.get(0)*/));
}
}
response.close();
return resources;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class VoidResponseHandler extends ValidatingResponseHandler<Void>
@Override
public Void handleResponse(Response response) throws IOException {
validateResponse(response);

response.close();

return null;
}
}