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: large file content may fail #1176

Merged
merged 2 commits into from Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions kraken/lib/src/foundation/http_client_request.dart
Expand Up @@ -170,8 +170,10 @@ class ProxyHttpClientRequest extends HttpClientRequest {

request = await _createBackendClientRequest();
// Send the real data to backend client.
request.add(_data);
_data.clear();
if (_data.isNotEmpty) {
await request.addStream(Stream.value(_data));
_data.clear();
}

// Step 4: Lifecycle of shouldInterceptRequest
HttpClientResponse? response;
Expand Down Expand Up @@ -222,8 +224,11 @@ class ProxyHttpClientRequest extends HttpClientRequest {

} else {
request = await _createBackendClientRequest();
request.add(_data);
_data.clear();
// Not using request.add, because large data will cause core exception.
if (_data.isNotEmpty) {
await request.addStream(Stream.value(_data));
_data.clear();
}
}

return _requestQueue.add(request.close);
Expand Down
2 changes: 1 addition & 1 deletion kraken/test/local_http_server.dart
Expand Up @@ -43,7 +43,7 @@ class LocalHttpServer {
data.addAll(chunk);

if (data.length >= 4) {
var lastFour = data.sublist(chunk.length - 4, chunk.length);
var lastFour = data.sublist(data.length - 4, data.length);

// Ends with \r\n\r\n or
// @TODO: content-length.
Expand Down
14 changes: 14 additions & 0 deletions kraken/test/src/foundation/http_client.dart
Expand Up @@ -80,5 +80,19 @@ void main() {

assert(request.headers.value('referer') != null);
});

test('Large content', () async {
var request = await httpClient.openUrl('POST',
server.getUri('plain_text'));
KrakenHttpOverrides.setContextHeader(request.headers, contextId);
// Mocked 3M file.
var data = List<int>.generate(3034764, (i) => i);
request.headers.set(HttpHeaders.contentLengthHeader, data.length);
await request.addStream(Stream.value(data));
request.add([13, 10, 13, 10]); // End of file, double CRLF.
await request.close();

// No error is ok.
});
});
}