Skip to content

Commit

Permalink
wip - pkg:web migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed Nov 21, 2023
1 parent f7d5a9c commit 63a6fda
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
4 changes: 3 additions & 1 deletion pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## 1.1.1
## 1.1.1-wip

* `BrowserClient` throws `ClientException` when the `'Content-Length'` header
is invalid.
* `IOClient` trims trailing whitespace on header values.
* Require Dart 3.2
* Browser: support Wasm by using package:web.

## 1.1.0

Expand Down
63 changes: 49 additions & 14 deletions pkgs/http/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:html';
import 'dart:js_interop';
import 'dart:typed_data';

import 'package:web/helpers.dart';

import 'base_client.dart';
import 'base_request.dart';
import 'byte_stream.dart';
Expand Down Expand Up @@ -37,7 +39,7 @@ class BrowserClient extends BaseClient {
/// The currently active XHRs.
///
/// These are aborted if the client is closed.
final _xhrs = <HttpRequest>{};
final _xhrs = <XMLHttpRequest>{};

/// Whether to send credentials such as cookies or authorization headers for
/// cross-site requests.
Expand All @@ -55,18 +57,21 @@ class BrowserClient extends BaseClient {
'HTTP request failed. Client is already closed.', request.url);
}
var bytes = await request.finalize().toBytes();
var xhr = HttpRequest();

var xhr = await HttpRequest.request(
'${request.url}',
method: request.method,
responseType: 'arraybuffer',
requestHeaders: request.headers,
sendData: bytes.toJS,
);
_xhrs.add(xhr);
xhr
..open(request.method, '${request.url}', async: true)
..responseType = 'arraybuffer'
..withCredentials = withCredentials;
request.headers.forEach(xhr.setRequestHeader);

var completer = Completer<StreamedResponse>();

unawaited(xhr.onLoad.first.then((_) {
if (xhr.responseHeaders['content-length'] case final contentLengthHeader?
if (xhr.getResponseHeader('content-length')
case final contentLengthHeader?
when !_digitRegex.hasMatch(contentLengthHeader)) {
completer.completeError(ClientException(
'Invalid content-length header [$contentLengthHeader].',
Expand All @@ -75,12 +80,17 @@ class BrowserClient extends BaseClient {
return;
}
var body = (xhr.response as ByteBuffer).asUint8List();
completer.complete(StreamedResponse(
ByteStream.fromBytes(body), xhr.status!,

completer.complete(
StreamedResponse(
ByteStream.fromBytes(body),
xhr.status,
contentLength: body.length,
request: request,
headers: xhr.responseHeaders,
reasonPhrase: xhr.statusText));
reasonPhrase: xhr.statusText,
),
);
}));

unawaited(xhr.onError.first.then((_) {
Expand All @@ -91,8 +101,6 @@ class BrowserClient extends BaseClient {
StackTrace.current);
}));

xhr.send(bytes);

try {
return await completer.future;
} finally {
Expand All @@ -112,3 +120,30 @@ class BrowserClient extends BaseClient {
_xhrs.clear();
}
}

extension on XMLHttpRequest {
Map<String, String> get responseHeaders {
// from Closure's goog.net.Xhrio.getResponseHeaders.
var headers = <String, String>{};
var headersString = getAllResponseHeaders();
var headersList = headersString.split('\r\n');
for (var header in headersList) {
if (header.isEmpty) {
continue;
}

var splitIdx = header.indexOf(': ');
if (splitIdx == -1) {
continue;
}
var key = header.substring(0, splitIdx).toLowerCase();
var value = header.substring(splitIdx + 2);
if (headers.containsKey(key)) {
headers[key] = '${headers[key]}, $value';
} else {
headers[key] = value;
}
}
return headers;
}
}
3 changes: 2 additions & 1 deletion pkgs/http/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ description: A composable, multi-platform, Future-based API for HTTP requests.
repository: https://github.com/dart-lang/http/tree/master/pkgs/http

environment:
sdk: ^3.0.0
sdk: ^3.2.0

dependencies:
async: ^2.5.0
http_parser: ^4.0.0
meta: ^1.3.0
web: ^0.4.0

dev_dependencies:
dart_flutter_team_lints: ^2.0.0
Expand Down
2 changes: 1 addition & 1 deletion pkgs/http/test/html/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:html';
import 'package:web/helpers.dart';

export '../utils.dart';

Expand Down

0 comments on commit 63a6fda

Please sign in to comment.