Skip to content

Commit

Permalink
Handle the case where content-length header is missing in the response
Browse files Browse the repository at this point in the history
Fix #592

How to reproduce the "missing content-length" case:
- Upload a relative big file to seafile server (larger than 300K is enough)
- Add 'gzip_types *' in nginx config

In such a setup, nginx would be forced to use chunked transfer-encoding
because when gzipped is enabled it can't know the exact response size
at the moment it sends the response header.
  • Loading branch information
lins05 committed Nov 24, 2016
1 parent f5993bd commit afae2da
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions app/src/main/java/com/seafile/seadroid2/SeafConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public String getAvatar(String email, int size) throws SeafException {
String apiPath = String.format("api2/avatars/user/%s/resized/%d", email, size);
HttpRequest req = prepareApiGetRequest(apiPath);
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);

String result = new String(req.bytes(), "UTF-8");
return result;
} catch (SeafException e) {
Expand Down Expand Up @@ -556,16 +556,27 @@ private File getFileFromLink(String dlink, String path, String localPath,
checkRequestResponseStatus(req, HttpURLConnection.HTTP_OK);

if (monitor != null) {
Long size;
Long size = -1L;
if (req.contentLength() > 0) {
size = Long.valueOf(req.contentLength());
size = Long.valueOf(req.contentLength());
} else {
/*if (req.header(HttpRequest.HEADER_CONTENT_LENGTH) == null) {
throw SeafException.illFormatException;
}*/
size = Long.parseLong(req.header(HttpRequest.HEADER_CONTENT_LENGTH));
// The req.contentLength() returns an int, which has a max value of
// 2GB. So if a file size exceeds 2GB, request.contentLength() would
// return -1. In such case, we parse the content length from the raw
// header string directly.
//
// See https://github.com/kevinsawicki/http-request/blob/http-request-5.6/lib/src/main/java/com/github/kevinsawicki/http/HttpRequest.java#L2519-L2521
String contentLengthheader = req.header(HttpRequest.HEADER_CONTENT_LENGTH);
// The server may not send us the "Content-Length" header in the
// response, e.g. when the server is using chunked transfer encoding.
if (contentLengthheader != null) {
size = Long.parseLong(contentLengthheader);
}
}

if (size > 0) {
monitor.onProgressNotify(size, false);
}
monitor.onProgressNotify(size, false);
}

File tmp = DataManager.createTempFile();
Expand Down

0 comments on commit afae2da

Please sign in to comment.