OLD | NEW |
| (Empty) |
1 library http_base.http_base_html; | |
2 | |
3 import 'dart:html'; | |
4 import 'dart:async'; | |
5 import 'dart:convert'; | |
6 import 'dart:typed_data'; | |
7 | |
8 import 'http_base.dart'; | |
9 export 'http_base.dart'; | |
10 | |
11 /// The following headers will be blocked by browsers. See: | |
12 /// http://www.w3.org/TR/XMLHttpRequest/ | |
13 const List<String> _BLOCKED_HEADERS = const [ | |
14 'accept-charset', 'accept-encoding', 'access-control-request-headers', | |
15 'access-control-request-method', 'connection', 'content-length', 'cookie', | |
16 'cookie2', 'date', 'dnt', 'expect', 'host', 'keep-alive', 'origin', | |
17 'referer', 'te', 'trailer', 'transfer-encoding', 'upgrade', 'user-agent', | |
18 'via']; | |
19 | |
20 /// An implementation for [RequestHandler]. It uses dart:html to make http | |
21 /// requests. | |
22 class Client { | |
23 Future<Response> call(Request request) { | |
24 return _bufferData(request.read()).then((Uint8List data) { | |
25 var url = request.url.toString(); | |
26 return _request(url, request.method, request.headers, data).then((xhr) { | |
27 var headers = HeadersImpl.Empty.replace(xhr.responseHeaders); | |
28 var body = _readResponse(xhr); | |
29 return new ResponseImpl(xhr.status, headers: headers, body: body); | |
30 }); | |
31 }); | |
32 } | |
33 | |
34 Future<Uint8List> _bufferData(Stream<List<int>> stream) { | |
35 int size = 0; | |
36 | |
37 return stream.fold([], (buffer, data) { | |
38 size += data.length; | |
39 return buffer..add(data); | |
40 }).then((List<List<int>> buffer) { | |
41 if (size > 0) { | |
42 var data; | |
43 if (buffer.length == 0 && buffer[0] is Uint8List) { | |
44 data = buffer[0]; | |
45 } else { | |
46 data = new Uint8List(size); | |
47 int offset = 0; | |
48 for (var bytes in buffer) { | |
49 var end = offset + bytes.length; | |
50 data.setRange(offset, end, bytes); | |
51 offset = end; | |
52 } | |
53 } | |
54 return data; | |
55 } | |
56 return null; | |
57 }); | |
58 } | |
59 | |
60 Future<HttpRequest> _request(String url, | |
61 String method, | |
62 Headers headers, | |
63 Uint8List sendData) { | |
64 var completer = new Completer<HttpRequest>(); | |
65 | |
66 var xhr = new HttpRequest(); | |
67 xhr.open(method, url, async: true); | |
68 | |
69 // Maybe we should use 'arraybuffer' instead? | |
70 xhr.responseType = 'blob'; | |
71 | |
72 // TODO: Special case Cookie/Set-Cookie here! | |
73 for (var name in headers.names) { | |
74 xhr.setRequestHeader(name, headers[name]); | |
75 } | |
76 | |
77 xhr.onLoad.first.then((_) => completer.complete(xhr)); | |
78 xhr.onError.first.then(completer.completeError); | |
79 xhr.send(sendData); | |
80 | |
81 return completer.future; | |
82 } | |
83 | |
84 Stream<List<int>> _readResponse(HttpRequest request) { | |
85 var controller = new StreamController<List<int>>(); | |
86 | |
87 var data = request.response; | |
88 assert (data is Blob); | |
89 | |
90 var reader = new FileReader(); | |
91 reader.onLoad.first.then((_) { | |
92 controller.add(reader.result); | |
93 controller.close(); | |
94 }); | |
95 reader.readAsArrayBuffer(data); | |
96 | |
97 return controller.stream; | |
98 } | |
99 } | |
OLD | NEW |