Skip to content

Commit

Permalink
Set content-length request header
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Mar 25, 2024
1 parent 9f717d1 commit 671b781
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ void main() {
expect(profile.requestData.contentLength, 2);
expect(profile.requestData.endTime, isNotNull);
expect(profile.requestData.error, isNull);
// XXX Should 'content-length' also be in the headers? It is not for the
// request but it is for the response.
expect(
profile.requestData.headers, containsPair('Content-Length', ['2']));
expect(profile.requestData.headers,
containsPair('Content-Type', ['text/plain; charset=utf-8']));
expect(profile.requestData.persistentConnection, isNull);
Expand All @@ -66,8 +66,6 @@ void main() {
test('response attributes', () {
expect(profile.responseData.bodyBytes, 'Hello World'.codeUnits);
expect(profile.responseData.compressionState, isNull);
// XXX how are request and response connectionInfo supposed to be
// different?
expect(profile.responseData.connectionInfo, isNull);
expect(profile.responseData.contentLength, 11);
expect(profile.responseData.endTime, isNotNull);
Expand Down Expand Up @@ -128,6 +126,7 @@ void main() {
expect(profile.requestData.contentLength, isNull);
expect(profile.requestData.endTime, isNotNull);
expect(profile.requestData.startTime, isNotNull);
expect(profile.requestData.headers, isNot(contains('Content-Length')));
});
});

Expand Down Expand Up @@ -159,8 +158,8 @@ void main() {
expect(profile.requestData.contentLength, 2);
expect(profile.requestData.endTime, isNotNull);
expect(profile.requestData.error, startsWith('ClientException:'));
// XXX Should 'content-length' also be in the headers? It is not for the
// request but it is for the response.
expect(
profile.requestData.headers, containsPair('Content-Length', ['2']));
expect(profile.requestData.headers,
containsPair('Content-Type', ['text/plain; charset=utf-8']));
expect(profile.requestData.persistentConnection, isNull);
Expand Down Expand Up @@ -232,8 +231,8 @@ void main() {
expect(profile.requestData.contentLength, 2);
expect(profile.requestData.endTime, isNotNull);
expect(profile.requestData.error, isNull);
// XXX Should 'content-length' also be in the headers? It is not for the
// request but it is for the response.
expect(
profile.requestData.headers, containsPair('Content-Length', ['2']));
expect(profile.requestData.headers,
containsPair('Content-Type', ['text/plain; charset=utf-8']));
expect(profile.requestData.persistentConnection, isNull);
Expand Down
12 changes: 12 additions & 0 deletions pkgs/cupertino_http/lib/src/cupertino_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ class CupertinoClient extends BaseClient {
// `httpBodyStream` requires a lot of expensive setup and data passing.
urlRequest.httpBody = Data.fromList(request.bodyBytes);
profile?.requestData.bodySink.add(request.bodyBytes);
if (profile != null) {
final headers =
Map<String, List<String>>.from(profile.requestData.headers!);
headers['Content-Length'] = ['${request.contentLength}'];
profile.requestData.headersListValues = headers;
}
} else if (await _hasData(stream) case (true, final s)) {
// If the request is supposed to be bodyless (e.g. GET requests)
// then setting `httpBodyStream` will cause the request to fail -
Expand All @@ -325,6 +331,12 @@ class CupertinoClient extends BaseClient {
final splitter = StreamSplitter(s);
urlRequest.httpBodyStream = splitter.split();
unawaited(profile.requestData.bodySink.addStream(splitter.split()));
if (request.contentLength != null) {
final headers =
Map<String, List<String>>.from(profile.requestData.headers!);
headers['Content-Length'] = ['${request.contentLength}'];
profile.requestData.headersListValues = headers;
}
}
}

Expand Down

0 comments on commit 671b781

Please sign in to comment.