OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library builtin; | 5 library builtin; |
6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 import 'dart:typed_data'; |
7 | 8 |
8 // Corelib 'print' implementation. | 9 // Corelib 'print' implementation. |
9 void _print(arg) { | 10 void _print(arg) { |
10 _Logger._printString(arg.toString()); | 11 _Logger._printString(arg.toString()); |
11 } | 12 } |
12 | 13 |
13 | 14 |
14 class _Logger { | 15 class _Logger { |
15 static void _printString(String s) native "Logger_PrintString"; | 16 static void _printString(String s) native "Logger_PrintString"; |
16 } | 17 } |
(...skipping 11 matching lines...) Expand all Loading... |
28 | 29 |
29 | 30 |
30 var _httpRequestResponseCode = 0; | 31 var _httpRequestResponseCode = 0; |
31 var _httpRequestStatusString; | 32 var _httpRequestStatusString; |
32 var _httpRequestResponse; | 33 var _httpRequestResponse; |
33 | 34 |
34 _getHttpRequestResponseCode() => _httpRequestResponseCode; | 35 _getHttpRequestResponseCode() => _httpRequestResponseCode; |
35 _getHttpRequestStatusString() => _httpRequestStatusString; | 36 _getHttpRequestStatusString() => _httpRequestStatusString; |
36 _getHttpRequestResponse() => _httpRequestResponse; | 37 _getHttpRequestResponse() => _httpRequestResponse; |
37 | 38 |
38 void _requestCompleted(HttpClientResponseBody body) { | 39 void _requestCompleted(List<int> data, HttpClientResponse response) { |
39 _httpRequestResponseCode = body.statusCode; | 40 _httpRequestResponseCode = response.statusCode; |
40 _httpRequestStatusString = '${body.statusCode} ${body.reasonPhrase}'; | 41 _httpRequestStatusString = '${response.statusCode} ${response.reasonPhrase}'; |
41 _httpRequestResponse = null; | 42 _httpRequestResponse = null; |
42 if (body.statusCode != 200 || body.type == 'json') { | 43 if (response.statusCode != 200 || |
| 44 (response.headers.contentType != null && |
| 45 response.headers.contentType.mimeType == 'application/json')) { |
43 return; | 46 return; |
44 } | 47 } |
45 _httpRequestResponse = body.body; | 48 _httpRequestResponse = data; |
46 } | 49 } |
47 | 50 |
48 | 51 |
49 void _requestFailed(error) { | 52 void _requestFailed(error) { |
50 _httpRequestResponseCode = 0; | 53 _httpRequestResponseCode = 0; |
51 _httpRequestStatusString = error.toString(); | 54 _httpRequestStatusString = error.toString(); |
52 _httpRequestResponse = null; | 55 _httpRequestResponse = null; |
53 } | 56 } |
54 | 57 |
55 | 58 |
56 void _makeHttpRequest(String uri) { | 59 void _makeHttpRequest(String uri) { |
57 var _client = new HttpClient(); | 60 var _client = new HttpClient(); |
58 _httpRequestResponseCode = 0; | 61 _httpRequestResponseCode = 0; |
59 _httpRequestStatusString = null; | 62 _httpRequestStatusString = null; |
60 _httpRequestResponse = null; | 63 _httpRequestResponse = null; |
61 Uri requestUri = Uri.parse(uri); | 64 Uri requestUri = Uri.parse(uri); |
62 _client.getUrl(requestUri) | 65 _client.getUrl(requestUri) |
63 .then((HttpClientRequest request) => request.close()) | 66 .then((HttpClientRequest request) => request.close()) |
64 .then(HttpBodyHandler.processResponse) | 67 .then((HttpClientResponse response) { |
65 .then((HttpClientResponseBody body) { | 68 return response |
66 _requestCompleted(body); | 69 .fold(new _BufferList(), (b, d) => b..add(d)) |
| 70 .then((buffer) { |
| 71 _requestCompleted(buffer.readBytes(), response); |
| 72 }); |
67 }).catchError((error) { | 73 }).catchError((error) { |
68 _requestFailed(error); | 74 _requestFailed(error); |
69 }); | 75 }); |
70 } | 76 } |
71 | 77 |
72 | 78 |
73 // Are we running on Windows? | 79 // Are we running on Windows? |
74 var _isWindows = false; | 80 var _isWindows = false; |
75 // The current working directory | 81 // The current working directory |
76 var _workingDirectoryUri; | 82 var _workingDirectoryUri; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 } | 270 } |
265 _logResolution('# Package: $uri -> $path'); | 271 _logResolution('# Package: $uri -> $path'); |
266 return path; | 272 return path; |
267 } | 273 } |
268 | 274 |
269 | 275 |
270 String _filePathFromHttpUri(Uri uri) { | 276 String _filePathFromHttpUri(Uri uri) { |
271 _logResolution('# Path: $uri -> $uri'); | 277 _logResolution('# Path: $uri -> $uri'); |
272 return uri.toString(); | 278 return uri.toString(); |
273 } | 279 } |
| 280 |
| 281 /** |
| 282 * Utility class that can fast concatenate [List<int>]s of bytes. Use |
| 283 * [readBytes] to get the final buffer. |
| 284 */ |
| 285 // TODO(ajohnsen): Expose in dart:io? |
| 286 class _BufferList { |
| 287 const int _INIT_SIZE = 1 * 1024; |
| 288 |
| 289 _BufferList() { |
| 290 clear(); |
| 291 } |
| 292 |
| 293 int pow2roundup(int x) { |
| 294 --x; |
| 295 x |= x >> 1; |
| 296 x |= x >> 2; |
| 297 x |= x >> 4; |
| 298 x |= x >> 8; |
| 299 x |= x >> 16; |
| 300 return x + 1; |
| 301 } |
| 302 |
| 303 /** |
| 304 * Adds a new buffer to the list. |
| 305 */ |
| 306 void add(List<int> buffer) { |
| 307 int bufferLength = buffer.length; |
| 308 int required = _length + bufferLength; |
| 309 if (_buffer == null) { |
| 310 int size = pow2roundup(required); |
| 311 if (size < _INIT_SIZE) size = _INIT_SIZE; |
| 312 _buffer = new Uint8List(size); |
| 313 } else if (_buffer.length < required) { |
| 314 // This will give is a list in the range of 2-4 times larger than |
| 315 // required. |
| 316 int size = pow2roundup(required) * 2; |
| 317 Uint8List newBuffer = new Uint8List(size); |
| 318 newBuffer.setRange(0, _buffer.length, _buffer); |
| 319 _buffer = newBuffer; |
| 320 } |
| 321 assert(_buffer.length >= required); |
| 322 if (buffer is Uint8List) { |
| 323 _buffer.setRange(_length, required, buffer); |
| 324 } else { |
| 325 for (int i = 0; i < bufferLength; i++) { |
| 326 _buffer[_length + i] = buffer[i]; |
| 327 } |
| 328 } |
| 329 _length = required; |
| 330 } |
| 331 |
| 332 /** |
| 333 * Same as [add]. |
| 334 */ |
| 335 void write(List<int> buffer) { |
| 336 add(buffer); |
| 337 } |
| 338 |
| 339 /** |
| 340 * Read all the bytes from the buffer list. If it's empty, an empty list |
| 341 * is returned. A call to [readBytes] will clear the buffer. |
| 342 */ |
| 343 List<int> readBytes() { |
| 344 if (_buffer == null) return new Uint8List(0); |
| 345 var buffer = new Uint8List.view(_buffer.buffer, 0, _length); |
| 346 clear(); |
| 347 return buffer; |
| 348 } |
| 349 |
| 350 /** |
| 351 * Returns the total number of bytes in the buffer. |
| 352 */ |
| 353 int get length => _length; |
| 354 |
| 355 /** |
| 356 * Returns whether the buffer list is empty. |
| 357 */ |
| 358 bool get isEmpty => _length == 0; |
| 359 |
| 360 /** |
| 361 * Returns whether the buffer list is not empty. |
| 362 */ |
| 363 bool get isNotEmpty => !isEmpty; |
| 364 |
| 365 /** |
| 366 * Clears the content of the buffer list. |
| 367 */ |
| 368 void clear() { |
| 369 _length = 0; |
| 370 _buffer = null; |
| 371 } |
| 372 |
| 373 int _length; // Total number of bytes in the buffer. |
| 374 Uint8List _buffer; // Internal buffer. |
| 375 } |
OLD | NEW |