OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 http_server; | 5 library http_server; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 import 'dart:uri'; | 10 import 'dart:uri'; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 if (request.uri.path == "/echo") { | 135 if (request.uri.path == "/echo") { |
136 _handleEchoRequest(request, request.response); | 136 _handleEchoRequest(request, request.response); |
137 } else if (request.uri.path == '/ws') { | 137 } else if (request.uri.path == '/ws') { |
138 _handleWebSocketRequest(request); | 138 _handleWebSocketRequest(request); |
139 } else { | 139 } else { |
140 _handleFileOrDirectoryRequest( | 140 _handleFileOrDirectoryRequest( |
141 request, request.response, allowedPort); | 141 request, request.response, allowedPort); |
142 } | 142 } |
143 }, | 143 }, |
144 onError: (e) { | 144 onError: (e) { |
145 DebugLogger.error('HttpServer: an error occured: $e'); | 145 DebugLogger.error('HttpServer: an error occured', e); |
146 }); | 146 }); |
147 _serverList.add(httpServer); | 147 _serverList.add(httpServer); |
148 }); | 148 }); |
149 } | 149 } |
150 | 150 |
151 void _handleFileOrDirectoryRequest(HttpRequest request, | 151 void _handleFileOrDirectoryRequest(HttpRequest request, |
152 HttpResponse response, | 152 HttpResponse response, |
153 int allowedPort) { | 153 int allowedPort) { |
154 var path = _getFilePathFromRequestPath(request.uri.path); | 154 var path = _getFilePathFromRequestPath(request.uri.path); |
155 if (path != null) { | 155 if (path != null) { |
(...skipping 23 matching lines...) Expand all Loading... |
179 } else { | 179 } else { |
180 _sendNotFound(request, response); | 180 _sendNotFound(request, response); |
181 } | 181 } |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 void _handleEchoRequest(HttpRequest request, HttpResponse response) { | 185 void _handleEchoRequest(HttpRequest request, HttpResponse response) { |
186 response.headers.set("Access-Control-Allow-Origin", "*"); | 186 response.headers.set("Access-Control-Allow-Origin", "*"); |
187 request.pipe(response).catchError((e) { | 187 request.pipe(response).catchError((e) { |
188 DebugLogger.warning( | 188 DebugLogger.warning( |
189 'HttpServer: error while closing the response stream: $e'); | 189 'HttpServer: error while closing the response stream', e); |
190 }); | 190 }); |
191 } | 191 } |
192 | 192 |
193 void _handleWebSocketRequest(HttpRequest request) { | 193 void _handleWebSocketRequest(HttpRequest request) { |
194 WebSocketTransformer.upgrade(request).then((websocket) { | 194 WebSocketTransformer.upgrade(request).then((websocket) { |
195 websocket.listen((data) { | 195 websocket.listen((data) { |
196 websocket.send(data); | 196 websocket.send(data); |
197 websocket.close(); | 197 websocket.close(); |
198 }, onError: (e) { | 198 }, onError: (e) { |
199 DebugLogger.warning( | 199 DebugLogger.warning('HttpServer: error while echoing to WebSocket', e); |
200 'HttpServer: error while echoing to WebSocket: $e'); | |
201 }); | 200 }); |
202 }).catchError((e) { | 201 }).catchError((e) { |
203 DebugLogger.warning( | 202 DebugLogger.warning( |
204 'HttpServer: error while transforming to WebSocket: $e'); | 203 'HttpServer: error while transforming to WebSocket', e); |
205 }); | 204 }); |
206 } | 205 } |
207 | 206 |
208 Path _getFilePathFromRequestPath(String urlRequestPath) { | 207 Path _getFilePathFromRequestPath(String urlRequestPath) { |
209 // Go to the top of the file to see an explanation of the URL path scheme. | 208 // Go to the top of the file to see an explanation of the URL path scheme. |
210 var requestPath = new Path(urlRequestPath.substring(1)).canonicalize(); | 209 var requestPath = new Path(urlRequestPath.substring(1)).canonicalize(); |
211 var pathSegments = requestPath.segments(); | 210 var pathSegments = requestPath.segments(); |
212 if (pathSegments.length > 0) { | 211 if (pathSegments.length > 0) { |
213 var basePath; | 212 var basePath; |
214 var relativePath; | 213 var relativePath; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 response.write(header); | 278 response.write(header); |
280 for (var entry in entries) { | 279 for (var entry in entries) { |
281 response.write( | 280 response.write( |
282 '<li><a href="${new Path(request.uri.path).append(entry.name)}">' | 281 '<li><a href="${new Path(request.uri.path).append(entry.name)}">' |
283 '${entry.displayName}</a></li>'); | 282 '${entry.displayName}</a></li>'); |
284 } | 283 } |
285 response.write(footer); | 284 response.write(footer); |
286 response.close(); | 285 response.close(); |
287 response.done.catchError((e) { | 286 response.done.catchError((e) { |
288 DebugLogger.warning( | 287 DebugLogger.warning( |
289 'HttpServer: error while closing the response stream: $e'); | 288 'HttpServer: error while closing the response stream', e); |
290 }); | 289 }); |
291 } | 290 } |
292 | 291 |
293 void _sendFileContent(HttpRequest request, | 292 void _sendFileContent(HttpRequest request, |
294 HttpResponse response, | 293 HttpResponse response, |
295 int allowedPort, | 294 int allowedPort, |
296 Path path, | 295 Path path, |
297 File file) { | 296 File file) { |
298 if (allowedPort != -1) { | 297 if (allowedPort != -1) { |
299 var headerOrigin = request.headers.value('Origin'); | 298 var headerOrigin = request.headers.value('Origin'); |
(...skipping 29 matching lines...) Expand all Loading... |
329 } | 328 } |
330 if (path.filename.endsWith('.html')) { | 329 if (path.filename.endsWith('.html')) { |
331 response.headers.set('Content-Type', 'text/html'); | 330 response.headers.set('Content-Type', 'text/html'); |
332 } else if (path.filename.endsWith('.js')) { | 331 } else if (path.filename.endsWith('.js')) { |
333 response.headers.set('Content-Type', 'application/javascript'); | 332 response.headers.set('Content-Type', 'application/javascript'); |
334 } else if (path.filename.endsWith('.dart')) { | 333 } else if (path.filename.endsWith('.dart')) { |
335 response.headers.set('Content-Type', 'application/dart'); | 334 response.headers.set('Content-Type', 'application/dart'); |
336 } | 335 } |
337 file.openRead().pipe(response).catchError((e) { | 336 file.openRead().pipe(response).catchError((e) { |
338 DebugLogger.warning( | 337 DebugLogger.warning( |
339 'HttpServer: error while closing the response stream: $e'); | 338 'HttpServer: error while closing the response stream', e); |
340 }); | 339 }); |
341 } | 340 } |
342 | 341 |
343 void _sendNotFound(HttpRequest request, HttpResponse response) { | 342 void _sendNotFound(HttpRequest request, HttpResponse response) { |
344 // NOTE: Since some tests deliberately try to access non-existent files. | 343 // NOTE: Since some tests deliberately try to access non-existent files. |
345 // We might want to remove this warning (otherwise it will show | 344 // We might want to remove this warning (otherwise it will show |
346 // up in the debug.log every time). | 345 // up in the debug.log every time). |
347 if (request.uri.path != "/favicon.ico") { | 346 if (request.uri.path != "/favicon.ico") { |
348 DebugLogger.warning('HttpServer: could not find file for request path: ' | 347 DebugLogger.warning('HttpServer: could not find file for request path: ' |
349 '"${request.uri.path}"'); | 348 '"${request.uri.path}"'); |
350 } | 349 } |
351 response.statusCode = HttpStatus.NOT_FOUND; | 350 response.statusCode = HttpStatus.NOT_FOUND; |
352 response.close(); | 351 response.close(); |
353 response.done.catchError((e) { | 352 response.done.catchError((e) { |
354 DebugLogger.warning( | 353 DebugLogger.warning( |
355 'HttpServer: error while closing the response stream: $e'); | 354 'HttpServer: error while closing the response stream', e); |
356 }); | 355 }); |
357 } | 356 } |
358 } | 357 } |
359 | 358 |
360 // Helper class for displaying directory listings. | 359 // Helper class for displaying directory listings. |
361 class _Entry { | 360 class _Entry { |
362 final String name; | 361 final String name; |
363 final String displayName; | 362 final String displayName; |
364 | 363 |
365 _Entry(this.name, this.displayName); | 364 _Entry(this.name, this.displayName); |
366 | 365 |
367 int compareTo(_Entry other) { | 366 int compareTo(_Entry other) { |
368 return name.compareTo(other.name); | 367 return name.compareTo(other.name); |
369 } | 368 } |
370 } | 369 } |
OLD | NEW |