Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Unified Diff: dart/pkg/http_base/lib/http_base_html.dart

Issue 445933004: Add implementations for Headers, Request, Response and dart:io/dart:html clients to http_base (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « dart/pkg/http_base/lib/http_base.dart ('k') | dart/pkg/http_base/lib/http_base_io.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/pkg/http_base/lib/http_base_html.dart
diff --git a/dart/pkg/http_base/lib/http_base_html.dart b/dart/pkg/http_base/lib/http_base_html.dart
new file mode 100644
index 0000000000000000000000000000000000000000..14d57e4528a0352d6213955a156090e61bd77908
--- /dev/null
+++ b/dart/pkg/http_base/lib/http_base_html.dart
@@ -0,0 +1,99 @@
+library http_base.http_base_html;
+
+import 'dart:html';
+import 'dart:async';
+import 'dart:convert';
+import 'dart:typed_data';
+
+import 'http_base.dart';
+export 'http_base.dart';
+
+/// The following headers will be blocked by browsers. See:
+/// http://www.w3.org/TR/XMLHttpRequest/
+const List<String> _BLOCKED_HEADERS = const [
+ 'accept-charset', 'accept-encoding', 'access-control-request-headers',
+ 'access-control-request-method', 'connection', 'content-length', 'cookie',
+ 'cookie2', 'date', 'dnt', 'expect', 'host', 'keep-alive', 'origin',
+ 'referer', 'te', 'trailer', 'transfer-encoding', 'upgrade', 'user-agent',
+ 'via'];
+
+/// An implementation for [RequestHandler]. It uses dart:html to make http
+/// requests.
+class Client {
+ Future<Response> call(Request request) {
+ return _bufferData(request.read()).then((Uint8List data) {
+ var url = request.url.toString();
+ return _request(url, request.method, request.headers, data).then((xhr) {
+ var headers = HeadersImpl.Empty.replace(xhr.responseHeaders);
+ var body = _readResponse(xhr);
+ return new ResponseImpl(xhr.status, headers: headers, body: body);
+ });
+ });
+ }
+
+ Future<Uint8List> _bufferData(Stream<List<int>> stream) {
+ int size = 0;
+
+ return stream.fold([], (buffer, data) {
+ size += data.length;
+ return buffer..add(data);
+ }).then((List<List<int>> buffer) {
+ if (size > 0) {
+ var data;
+ if (buffer.length == 0 && buffer[0] is Uint8List) {
+ data = buffer[0];
+ } else {
+ data = new Uint8List(size);
+ int offset = 0;
+ for (var bytes in buffer) {
+ var end = offset + bytes.length;
+ data.setRange(offset, end, bytes);
+ offset = end;
+ }
+ }
+ return data;
+ }
+ return null;
+ });
+ }
+
+ Future<HttpRequest> _request(String url,
+ String method,
+ Headers headers,
+ Uint8List sendData) {
+ var completer = new Completer<HttpRequest>();
+
+ var xhr = new HttpRequest();
+ xhr.open(method, url, async: true);
+
+ // Maybe we should use 'arraybuffer' instead?
+ xhr.responseType = 'blob';
+
+ // TODO: Special case Cookie/Set-Cookie here!
+ for (var name in headers.names) {
+ xhr.setRequestHeader(name, headers[name]);
+ }
+
+ xhr.onLoad.first.then((_) => completer.complete(xhr));
+ xhr.onError.first.then(completer.completeError);
+ xhr.send(sendData);
+
+ return completer.future;
+ }
+
+ Stream<List<int>> _readResponse(HttpRequest request) {
+ var controller = new StreamController<List<int>>();
+
+ var data = request.response;
+ assert (data is Blob);
+
+ var reader = new FileReader();
+ reader.onLoad.first.then((_) {
+ controller.add(reader.result);
+ controller.close();
+ });
+ reader.readAsArrayBuffer(data);
+
+ return controller.stream;
+ }
+}
« no previous file with comments | « dart/pkg/http_base/lib/http_base.dart ('k') | dart/pkg/http_base/lib/http_base_io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698