Skip to content

Commit

Permalink
fix(nextcloud): Make WebDAV get operations work with chunked responses
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Apr 28, 2024
1 parent 9cb12d7 commit 6af8ddb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
26 changes: 12 additions & 14 deletions packages/nextcloud/lib/src/webdav/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -294,25 +294,23 @@ class WebDavClient {

unawaited(
response.then(
(response) async {
(response) {
final contentLength = response.contentLength;
if (contentLength == null || contentLength <= 0) {
onProgress?.call(1);
} else {
final completer = Completer<void>();
var downloaded = 0;
var downloaded = 0;

response.stream.listen((chunk) async {
response.stream.listen(
(chunk) async {
controller.add(chunk);
downloaded += chunk.length;
onProgress?.call(downloaded / contentLength);
if (downloaded >= contentLength) {
completer.complete();
if (contentLength != null) {
onProgress?.call(downloaded / contentLength);
}
});
await completer.future;
}
await controller.close();
},
onDone: () {
onProgress?.call(1);
controller.close();
},
);
},
// ignore: avoid_types_on_closure_parameters
onError: (Object error) {
Expand Down
49 changes: 49 additions & 0 deletions packages/nextcloud/test/webdav_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,55 @@ void main() {
});
});

test('Chunked responses', () async {
await HttpServer.bind('127.0.0.1', 0).then((server) async {
server.listen((request) {
request.listen(
(_) {},
onDone: () async {
if (request.uri.path == '/index.php') {
final response = request.response..write('data-requesttoken="token"');
await response.close();

return;
}

final response = request.response
..headers.chunkedTransferEncoding = true
..write('1')
..write('2')
..write('3');

await response.close();

await server.close();
},
);
});

final client = NextcloudClient(
Uri(
scheme: 'http',
host: server.address.host,
port: server.port,
),
);

final progress = <double>[];

final buffer = BytesBuilder(copy: false);
await client.webdav
.getStream(
PathUri.cwd(),
onProgress: progress.add,
)
.forEach(buffer.add);

expect(buffer.toBytes(), Uint8List.fromList(utf8.encode('123')));
expect(progress, [1]);
});
});

presets(
'server',
'webdav',
Expand Down

0 comments on commit 6af8ddb

Please sign in to comment.