 Chromium Code Reviews
 Chromium Code Reviews Issue 11641005:
  Add cross-origin test with credentials.  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
    
  
    Issue 11641005:
  Add cross-origin test with credentials.  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart| 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 http_server; | 5 library http_server; | 
| 6 | 6 | 
| 7 import 'dart:io'; | 7 import 'dart:io'; | 
| 8 import 'dart:isolate'; | 8 import 'dart:isolate'; | 
| 9 import 'test_suite.dart'; // For TestUtils. | 9 import 'test_suite.dart'; // For TestUtils. | 
| 10 | 10 | 
| 11 HttpServer _httpServer; | 11 HttpServer startHttpServer(String host, [int allowedPort = -1]) { | 
| 12 | |
| 13 void startHttpServer(String host, int port) { | |
| 14 var basePath = TestUtils.dartDir(); | 12 var basePath = TestUtils.dartDir(); | 
| 15 _httpServer = new HttpServer(); | 13 var httpServer = new HttpServer(); | 
| 16 _httpServer.onError = (e) { | 14 httpServer.onError = (e) { | 
| 17 // Consider errors in the builtin http server fatal. | 15 // Consider errors in the builtin http server fatal. | 
| 18 // Intead of just throwing the exception we print | 16 // Intead of just throwing the exception we print | 
| 19 // a message that makes it clearer what happened. | 17 // a message that makes it clearer what happened. | 
| 20 print('Test http server error: $e'); | 18 print('Test http server error: $e'); | 
| 21 exit(1); | 19 exit(1); | 
| 22 }; | 20 }; | 
| 23 _httpServer.defaultRequestHandler = (request, resp) { | 21 httpServer.defaultRequestHandler = (request, resp) { | 
| 24 var requestPath = new Path(request.path).canonicalize(); | 22 var requestPath = new Path(request.path).canonicalize(); | 
| 25 if (!requestPath.isAbsolute) { | 23 if (!requestPath.isAbsolute) { | 
| 26 resp.statusCode = HttpStatus.NOT_FOUND; | 24 resp.statusCode = HttpStatus.NOT_FOUND; | 
| 27 resp.outputStream.close(); | 25 resp.outputStream.close(); | 
| 28 } else { | 26 } else { | 
| 29 var path = basePath; | 27 var path = basePath; | 
| 30 requestPath.segments().forEach((s) => path = path.append(s)); | 28 requestPath.segments().forEach((s) => path = path.append(s)); | 
| 31 var file = new File(path.toNativePath()); | 29 var file = new File(path.toNativePath()); | 
| 32 file.exists().then((exists) { | 30 file.exists().then((exists) { | 
| 33 if (exists) { | 31 if (exists) { | 
| 34 // Allow loading from localhost in browsers. | 32 if (allowedPort != -1) { | 
| 35 resp.headers.set("Access-Control-Allow-Origin", "*"); | 33 // Allow loading from localhost:$allowedPort in browsers. | 
| 34 resp.headers.set("Access-Control-Allow-Origin", | |
| 35 "http://127.0.0.1:$allowedPort"); | |
| 36 resp.headers.set('Access-Control-Allow-Credentials', 'true'); | |
| 37 } else { | |
| 38 // No allowedPort specified. Allow from anywhere (but cross-origin | |
| 39 // requests *with credentials* will fail because you can't use "*"). | |
| 40 resp.headers.set("Access-Control-Allow-Origin", "*"); | |
| 41 } | |
| 42 if (path.toNativePath().endsWith('.html')) { | |
| 
Mads Ager (google)
2013/01/07 07:15:52
path.extension == 'html'
 | |
| 43 resp.headers.set('Content-Type', 'text/html'); | |
| 44 } else if (path.toNativePath().endsWith('.js')) { | |
| 
Mads Ager (google)
2013/01/07 07:15:52
ditto
 | |
| 45 resp.headers.set('Content-Type', 'application/javascript'); | |
| 46 } else if (path.toNativePath().endsWith('.dart')) { | |
| 
Mads Ager (google)
2013/01/07 07:15:52
ditto
 | |
| 47 resp.headers.set('Content-Type', 'application/dart'); | |
| 48 } | |
| 36 file.openInputStream().pipe(resp.outputStream); | 49 file.openInputStream().pipe(resp.outputStream); | 
| 37 } else { | 50 } else { | 
| 38 resp.statusCode = HttpStatus.NOT_FOUND; | 51 resp.statusCode = HttpStatus.NOT_FOUND; | 
| 39 resp.outputStream.close(); | 52 resp.outputStream.close(); | 
| 40 } | 53 } | 
| 41 }); | 54 }); | 
| 42 } | 55 } | 
| 43 }; | 56 }; | 
| 44 | 57 | 
| 45 // Echos back the contents of the request as the response data. | 58 // Echos back the contents of the request as the response data. | 
| 46 _httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { | 59 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { | 
| 47 resp.headers.set("Access-Control-Allow-Origin", "*"); | 60 resp.headers.set("Access-Control-Allow-Origin", "*"); | 
| 48 | 61 | 
| 49 request.inputStream.pipe(resp.outputStream); | 62 request.inputStream.pipe(resp.outputStream); | 
| 50 }); | 63 }); | 
| 51 | 64 | 
| 52 _httpServer.listen(host, port); | 65 httpServer.listen(host, 0); | 
| 66 return httpServer; | |
| 53 } | 67 } | 
| 54 | 68 | 
| 55 terminateHttpServer() { | 69 terminateHttpServers(List<HttpServer> servers) { | 
| 56 if (_httpServer != null) _httpServer.close(); | 70 for (var server in servers) server.close(); | 
| 57 } | 71 } | 
| OLD | NEW |