OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 part of vmservice_io; | 5 part of vmservice_io; |
6 | 6 |
7 _log(msg) { | 7 _log(msg) { |
8 print("% $msg"); | 8 print("% $msg"); |
9 } | 9 } |
10 | 10 |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 } | 333 } |
334 sp.send("Could not resolve a package location for base at $base"); | 334 sp.send("Could not resolve a package location for base at $base"); |
335 } catch (e, s) { | 335 } catch (e, s) { |
336 if (traceLoading) { | 336 if (traceLoading) { |
337 _log("Error loading packages: $e\n$s"); | 337 _log("Error loading packages: $e\n$s"); |
338 } | 338 } |
339 sp.send("Uncaught error ($e) loading packages file."); | 339 sp.send("Uncaught error ($e) loading packages file."); |
340 } | 340 } |
341 } | 341 } |
342 | 342 |
343 | |
344 Future<bool> _loadHttpPackagesFile(SendPort sp, | |
345 bool traceLoading, | |
346 Uri resource) async { | |
347 try { | |
348 if (_httpClient == null) { | |
349 _httpClient = new HttpClient()..maxConnectionsPerHost = 6; | |
350 } | |
351 if (traceLoading) { | |
352 _log("Fetching packages file from '$resource'."); | |
353 } | |
354 var req = await _httpClient.getUrl(resource); | |
355 var rsp = await req.close(); | |
356 var builder = new BytesBuilder(copy: false); | |
357 await for (var bytes in rsp) { | |
358 builder.add(bytes); | |
359 } | |
360 if (rsp.statusCode != 200) { | |
Cutch
2015/08/05 14:52:37
Check status code before awaiting on the payload?
Ivan Posva
2015/08/05 23:58:10
This follows the non-async/await code above.
| |
361 if (traceLoading) { | |
362 _log("Got status ${rsp.statusCode} fetching '$resource'."); | |
363 } | |
364 return false; | |
365 } | |
366 var data = builder.takeBytes(); | |
367 if (traceLoading) { | |
368 _log("Loaded packages file from '$resource':\n" | |
369 "${new String.fromCharCodes(data)}"); | |
370 } | |
371 _parsePackagesFile(sp, traceLoading, resource, data); | |
372 } catch (e, s) { | |
373 if (traceLoading) { | |
374 _log("Error loading packages file from '$resource': $e\n$s"); | |
375 } | |
376 sp.send("Uncaught error ($e) loading packages file from '$resource'."); | |
377 } | |
378 return false; | |
379 } | |
380 | |
343 _handlePackagesRequest(SendPort sp, | 381 _handlePackagesRequest(SendPort sp, |
344 bool traceLoading, | 382 bool traceLoading, |
345 int id, | 383 int id, |
346 Uri resource) async { | 384 Uri resource) async { |
347 if (id == -1) { | 385 try { |
348 if (resource.scheme == 'file') { | 386 if (id == -1) { |
349 _findPackagesFile(sp, traceLoading, resource); | 387 if (resource.scheme == 'file') { |
350 } else if ((resource.scheme == 'http') || (resource.scheme == 'https')) { | 388 _findPackagesFile(sp, traceLoading, resource); |
351 // TODO(iposva): Check for the existence of a .packages file when loading | 389 } else if ((resource.scheme == 'http') || (resource.scheme == 'https')) { |
352 // from http or https. | 390 // Try to load the .packages file next to the resource. |
353 var packageRoot = resource.resolve('packages/'); | 391 var packagesUri = resource.resolve(".packages"); |
354 sp.send([packageRoot.toString()]); | 392 var exists = await _loadHttpPackagesFile(sp, traceLoading, packagesUri); |
393 if (!exists) { | |
394 // If the loading of the .packages file failed for http/https based | |
395 // scripts then setup the package root. | |
396 var packageRoot = resource.resolve('packages/'); | |
397 sp.send([packageRoot.toString()]); | |
398 } | |
399 } else { | |
400 sp.send("Unsupported base URI to identify .packages file: " | |
Cutch
2015/08/05 14:52:37
Unsupported URI scheme?
Ivan Posva
2015/08/05 23:58:11
Done.
| |
401 "'$resource'."); | |
402 } | |
403 } else if (id == -2) { | |
404 if (traceLoading) { | |
405 _log("Handling load of packages map: '$resource'."); | |
406 } | |
407 if (resource.scheme == 'file') { | |
408 var exists = await new File.fromUri(resource).exists(); | |
409 if (exists) { | |
410 _loadPackagesFile(sp, traceLoading, resource); | |
411 } else { | |
412 sp.send("Packages file '$resource' not found."); | |
413 } | |
414 } else if ((resource.scheme == 'http') || (resource.scheme == 'https')) { | |
415 var exists = await _loadHttpPackagesFile(sp, traceLoading, resource); | |
416 if (!exists) { | |
417 sp.send("Packages file '$resource' not found."); | |
418 } | |
419 } else { | |
420 sp.send("Unknown scheme (${resource.scheme}) for package file at " | |
421 "'$resource'."); | |
422 } | |
355 } else { | 423 } else { |
356 sp.send("Unsupported base URI to identify .packages file: '$resource'."); | 424 sp.send("Unknown packages request id: $id for '$resource'."); |
357 } | 425 } |
358 } else if (id == -2) { | 426 } catch (e, s) { |
359 if (traceLoading) { | 427 if (traceLoading) { |
360 _log("Handling load of packages map: '$resource'."); | 428 _log("Error handling packages request: $e\n$s"); |
361 } | 429 } |
362 var exists = await new File.fromUri(resource).exists(); | 430 sp.send("Uncaught error ($e) handling packages request."); |
363 if (exists) { | |
364 _loadPackagesFile(sp, traceLoading, resource); | |
365 } else { | |
366 sp.send("Packages file $resource not found."); | |
367 } | |
368 } else { | |
369 sp.send("Unknown packages request id: $id for '$resource'."); | |
370 } | 431 } |
371 } | 432 } |
372 | 433 |
373 | 434 |
374 // External entry point for loader requests. | 435 // External entry point for loader requests. |
375 _processLoadRequest(request) { | 436 _processLoadRequest(request) { |
376 SendPort sp = request[0]; | 437 SendPort sp = request[0]; |
377 assert(sp != null); | 438 assert(sp != null); |
378 bool traceLoading = request[1]; | 439 bool traceLoading = request[1]; |
379 assert(traceLoading != null); | 440 assert(traceLoading != null); |
380 int id = request[2]; | 441 int id = request[2]; |
381 assert(id != null); | 442 assert(id != null); |
382 String resource = request[3]; | 443 String resource = request[3]; |
383 assert(resource != null); | 444 assert(resource != null); |
384 var uri = Uri.parse(resource); | 445 var uri = Uri.parse(resource); |
385 if (id >= 0) { | 446 if (id >= 0) { |
386 _handleResourceRequest(sp, traceLoading, id, uri); | 447 _handleResourceRequest(sp, traceLoading, id, uri); |
387 } else { | 448 } else { |
388 _handlePackagesRequest(sp, traceLoading, id, uri); | 449 _handlePackagesRequest(sp, traceLoading, id, uri); |
389 } | 450 } |
390 } | 451 } |
OLD | NEW |