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 issues with HTTP redirects and file upload #514

Open
wants to merge 3 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
9 changes: 7 additions & 2 deletions src/main/java/com/nextcloud/common/NextcloudClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ class NextcloudClient(var baseUri: Uri,
while (redirectionsCount < OwnCloudClient.MAX_REDIRECTIONS_COUNT &&
(status == HttpStatus.SC_MOVED_PERMANENTLY ||
status == HttpStatus.SC_MOVED_TEMPORARILY ||
status == HttpStatus.SC_TEMPORARY_REDIRECT)) {
status == HttpStatus.SC_TEMPORARY_REDIRECT ||
status == /* Permanent Redirect */ 308)) {
var location = method.getResponseHeader("Location")
if (location == null) {
location = method.getResponseHeader("location")
Expand All @@ -132,7 +133,11 @@ class NextcloudClient(var baseUri: Uri,
}

if (destination != null) {
val suffixIndex = location.lastIndexOf(AccountUtils.WEBDAV_PATH_4_0)
var suffixIndex = location.lastIndexOf(AccountUtils.WEBDAV_PATH_4_0)
if (suffixIndex == -1) {
suffixIndex = location.lastIndexOf(AccountUtils.WEBDAV_PATH_9_0)
}
check (suffixIndex != -1) { "Failed to find webdav substring in $location" }
val redirectionBase = location.substring(0, suffixIndex)
val destinationStr = destination
val destinationPath = destinationStr.substring(baseUri.toString().length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ public RedirectionPath followRedirection(HttpMethod method) throws IOException {
while (redirectionsCount < MAX_REDIRECTIONS_COUNT &&
( status == HttpStatus.SC_MOVED_PERMANENTLY ||
status == HttpStatus.SC_MOVED_TEMPORARILY ||
status == HttpStatus.SC_TEMPORARY_REDIRECT)
status == HttpStatus.SC_TEMPORARY_REDIRECT ||
status == /* Permanent Redirect */ 308)
) {

Header location = method.getResponseHeader("Location");
Expand All @@ -268,6 +269,12 @@ public RedirectionPath followRedirection(HttpMethod method) throws IOException {
}
if (destination != null) {
int suffixIndex = locationStr.lastIndexOf(AccountUtils.WEBDAV_PATH_4_0);
if (suffixIndex == -1) {
suffixIndex = locationStr.lastIndexOf(AccountUtils.WEBDAV_PATH_9_0);
}
if (suffixIndex == -1) {
throw new IllegalStateException("Failed to find webdav substring in " + locationStr);
}
String redirectionBase = locationStr.substring(0, suffixIndex);

String destinationStr = destination.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ private RemoteOperationResult uploadChunk(OwnCloudClient client, String uploadFo
private PutMethod createPutMethod(String uriPrefix) {
putMethod = new PutMethod(uriPrefix);
putMethod.setRequestEntity(entity);
putMethod.setUseExpectHeader(true);
if (cancellationRequested.get()) {
putMethod.abort(); // next method will throw an exception
}
Expand Down