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 print("Unable to connect to 'pub serve' for '${request.uri}': $e"); | 50 print("Unable to connect to 'pub serve' for '${request.uri}': $e"); |
kevmoo
2014/11/10 21:57:56
Should we get rid of this print statement, too? Or
kustermann
2014/11/11 09:53:13
"stdout" is currently our log for locally testing.
kevmoo
2014/11/11 15:15:52
Done.
| |
51 var error = new AssetError( | 51 throw new AssetError( |
52 "Unable to connect to 'pub serve' for '${request.uri}': $e"); | 52 "Unable to connect to 'pub serve' for '${request.uri}': $e"); |
53 return new Future.error(error); | |
54 }); | 53 }); |
55 } | 54 } |
56 | 55 |
57 Future _serveFromFile(HttpRequest request, String path) { | 56 Future _serveFromFile(HttpRequest request, String path) { |
58 // Check if the request path is pointing to a static resource. | 57 // Check if the request path is pointing to a static resource. |
59 path = normalize(path); | 58 path = normalize(path); |
60 return FileSystemEntity.isFile(root + path).then((exists) { | 59 return FileSystemEntity.isFile(root + path).then((exists) { |
61 if (exists) { | 60 if (exists) { |
62 return vd.serveFile(new File(root + path), request); | 61 return vd.serveFile(new File(root + path), request); |
63 } else { | 62 } else { |
64 return _serve404(request); | 63 return _serve404(request); |
65 } | 64 } |
66 }); | 65 }); |
67 } | 66 } |
68 | 67 |
69 Future<Stream<List<int>>> _readFromPub(String path) { | 68 Future<Stream<List<int>>> _readFromPub(String path) { |
70 var uri = pubServeUrl.resolve(path); | 69 var uri = pubServeUrl.resolve(path); |
71 return client.openUrl('GET', uri) | 70 return client.openUrl('GET', uri) |
72 .then((request) => request.close()) | 71 .then((request) => request.close()) |
73 .then((response) { | 72 .then((response) { |
74 if (response.statusCode == HttpStatus.OK) { | 73 if (response.statusCode == HttpStatus.OK) { |
75 return response; | 74 return response; |
76 } else { | 75 } else { |
77 var error = new AssetError( | 76 throw new AssetError( |
78 "Failed to fetch asset '$path' from pub: " | 77 "Failed to fetch asset '$path' from pub: " |
79 "${response.statusCode}."); | 78 "${response.statusCode}."); |
80 return new Future.error(error); | |
81 } | 79 } |
82 }) | 80 }) |
83 .catchError((error) { | 81 .catchError((error) { |
84 if (error is! AssetError) { | 82 if (error is! AssetError) { |
85 error = new AssetError( | 83 error = new AssetError( |
86 "Failed to fetch asset '$path' from pub: '${path}': $error"); | 84 "Failed to fetch asset '$path' from pub: '${path}': $error"); |
87 } | 85 } |
88 return new Future.error(error); | 86 throw error; |
89 }); | 87 }); |
90 } | 88 } |
91 | 89 |
92 Future<Stream<List<int>>> _readFromFile(String path) { | 90 Future<Stream<List<int>>> _readFromFile(String path) { |
93 path = normalize(path); | 91 path = normalize(path); |
94 return FileSystemEntity.isFile(root + path).then((exists) { | 92 return FileSystemEntity.isFile(root + path).then((exists) { |
95 if (exists) { | 93 if (exists) { |
96 return new File(root + path).openRead(); | 94 return new File(root + path).openRead(); |
97 } else { | 95 } else { |
98 var error = new AssetError("Asset '$path' not found"); | 96 throw new AssetError("Asset '$path' not found"); |
99 return new Future.error(error); | |
100 } | 97 } |
101 }); | 98 }); |
102 } | 99 } |
103 | 100 |
104 Future _serve404(HttpRequest request) { | 101 Future _serve404(HttpRequest request) { |
105 // Serve 404. | 102 // Serve 404. |
106 return request.drain().then((_) { | 103 return request.drain().then((_) { |
107 return request.response | 104 return request.response |
108 ..statusCode = HttpStatus.NOT_FOUND | 105 ..statusCode = HttpStatus.NOT_FOUND |
109 ..close(); | 106 ..close(); |
(...skipping 26 matching lines...) Expand all Loading... | |
136 Future<Stream<List<int>>> read([String path]) { | 133 Future<Stream<List<int>>> read([String path]) { |
137 return appengineContext.assets.read( | 134 return appengineContext.assets.read( |
138 path == null ? request.uri.path : path); | 135 path == null ? request.uri.path : path); |
139 } | 136 } |
140 | 137 |
141 void serve([String path]) { | 138 void serve([String path]) { |
142 appengineContext.assets.serve(request, | 139 appengineContext.assets.serve(request, |
143 path == null ? request.uri.path : path); | 140 path == null ? request.uri.path : path); |
144 } | 141 } |
145 } | 142 } |
OLD | NEW |