OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 appengine.assets; | 5 library appengine.assets; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:http_server/http_server.dart' show VirtualDirectory; | 10 import 'package:http_server/http_server.dart' show VirtualDirectory; |
(...skipping 29 matching lines...) Expand all Loading... |
40 proxyResponse.headers.forEach((name, values) { | 40 proxyResponse.headers.forEach((name, values) { |
41 if (RESPONSE_HEADERS.contains(name)) { | 41 if (RESPONSE_HEADERS.contains(name)) { |
42 request.response.headers.set(name, values); | 42 request.response.headers.set(name, values); |
43 } | 43 } |
44 }); | 44 }); |
45 request.response.statusCode = proxyResponse.statusCode; | 45 request.response.statusCode = proxyResponse.statusCode; |
46 request.response.reasonPhrase = proxyResponse.reasonPhrase; | 46 request.response.reasonPhrase = proxyResponse.reasonPhrase; |
47 return proxyResponse.pipe(request.response); | 47 return proxyResponse.pipe(request.response); |
48 }) | 48 }) |
49 .catchError((e) { | 49 .catchError((e) { |
| 50 // TODO(kevmoo) Use logging here |
50 print("Unable to connect to 'pub serve' for '${request.uri}': $e"); | 51 print("Unable to connect to 'pub serve' for '${request.uri}': $e"); |
51 var error = new AssetError( | 52 throw new AssetError( |
52 "Unable to connect to 'pub serve' for '${request.uri}': $e"); | 53 "Unable to connect to 'pub serve' for '${request.uri}': $e"); |
53 return new Future.error(error); | |
54 }); | 54 }); |
55 } | 55 } |
56 | 56 |
57 Future _serveFromFile(HttpRequest request, String path) { | 57 Future _serveFromFile(HttpRequest request, String path) { |
58 // Check if the request path is pointing to a static resource. | 58 // Check if the request path is pointing to a static resource. |
59 path = normalize(path); | 59 path = normalize(path); |
60 return FileSystemEntity.isFile(root + path).then((exists) { | 60 return FileSystemEntity.isFile(root + path).then((exists) { |
61 if (exists) { | 61 if (exists) { |
62 return vd.serveFile(new File(root + path), request); | 62 return vd.serveFile(new File(root + path), request); |
63 } else { | 63 } else { |
64 return _serve404(request); | 64 return _serve404(request); |
65 } | 65 } |
66 }); | 66 }); |
67 } | 67 } |
68 | 68 |
69 Future<Stream<List<int>>> _readFromPub(String path) { | 69 Future<Stream<List<int>>> _readFromPub(String path) { |
70 var uri = pubServeUrl.resolve(path); | 70 var uri = pubServeUrl.resolve(path); |
71 return client.openUrl('GET', uri) | 71 return client.openUrl('GET', uri) |
72 .then((request) => request.close()) | 72 .then((request) => request.close()) |
73 .then((response) { | 73 .then((response) { |
74 if (response.statusCode == HttpStatus.OK) { | 74 if (response.statusCode == HttpStatus.OK) { |
75 return response; | 75 return response; |
76 } else { | 76 } else { |
77 var error = new AssetError( | 77 throw new AssetError( |
78 "Failed to fetch asset '$path' from pub: " | 78 "Failed to fetch asset '$path' from pub: " |
79 "${response.statusCode}."); | 79 "${response.statusCode}."); |
80 return new Future.error(error); | |
81 } | 80 } |
82 }) | 81 }) |
83 .catchError((error) { | 82 .catchError((error) { |
84 if (error is! AssetError) { | 83 if (error is! AssetError) { |
85 error = new AssetError( | 84 error = new AssetError( |
86 "Failed to fetch asset '$path' from pub: '${path}': $error"); | 85 "Failed to fetch asset '$path' from pub: '${path}': $error"); |
87 } | 86 } |
88 return new Future.error(error); | 87 throw error; |
89 }); | 88 }); |
90 } | 89 } |
91 | 90 |
92 Future<Stream<List<int>>> _readFromFile(String path) { | 91 Future<Stream<List<int>>> _readFromFile(String path) { |
93 path = normalize(path); | 92 path = normalize(path); |
94 return FileSystemEntity.isFile(root + path).then((exists) { | 93 return FileSystemEntity.isFile(root + path).then((exists) { |
95 if (exists) { | 94 if (exists) { |
96 return new File(root + path).openRead(); | 95 return new File(root + path).openRead(); |
97 } else { | 96 } else { |
98 var error = new AssetError("Asset '$path' not found"); | 97 throw new AssetError("Asset '$path' not found"); |
99 return new Future.error(error); | |
100 } | 98 } |
101 }); | 99 }); |
102 } | 100 } |
103 | 101 |
104 Future _serve404(HttpRequest request) { | 102 Future _serve404(HttpRequest request) { |
105 // Serve 404. | 103 // Serve 404. |
106 return request.drain().then((_) { | 104 return request.drain().then((_) { |
107 return request.response | 105 return request.response |
108 ..statusCode = HttpStatus.NOT_FOUND | 106 ..statusCode = HttpStatus.NOT_FOUND |
109 ..close(); | 107 ..close(); |
(...skipping 26 matching lines...) Expand all Loading... |
136 Future<Stream<List<int>>> read([String path]) { | 134 Future<Stream<List<int>>> read([String path]) { |
137 return appengineContext.assets.read( | 135 return appengineContext.assets.read( |
138 path == null ? request.uri.path : path); | 136 path == null ? request.uri.path : path); |
139 } | 137 } |
140 | 138 |
141 void serve([String path]) { | 139 void serve([String path]) { |
142 appengineContext.assets.serve(request, | 140 appengineContext.assets.serve(request, |
143 path == null ? request.uri.path : path); | 141 path == null ? request.uri.path : path); |
144 } | 142 } |
145 } | 143 } |
OLD | NEW |