Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: tools/testing/dart/http_server.dart

Issue 11641005: Add cross-origin test with credentials. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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]) {
Mads Ager (google) 2013/01/04 09:39:20 Could you add spaces: [int allowedPort = -1]?
Emily Fortuna 2013/01/04 23:26:54 Done.
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 }
36 file.openInputStream().pipe(resp.outputStream); 42 file.openInputStream().pipe(resp.outputStream);
37 } else { 43 } else {
38 resp.statusCode = HttpStatus.NOT_FOUND; 44 resp.statusCode = HttpStatus.NOT_FOUND;
39 resp.outputStream.close(); 45 resp.outputStream.close();
40 } 46 }
41 }); 47 });
42 } 48 }
43 }; 49 };
44 50
45 // Echos back the contents of the request as the response data. 51 // Echos back the contents of the request as the response data.
46 _httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) { 52 httpServer.addRequestHandler((req) => req.path == "/echo", (request, resp) {
47 resp.headers.set("Access-Control-Allow-Origin", "*"); 53 resp.headers.set("Access-Control-Allow-Origin", "*");
48 54
49 request.inputStream.pipe(resp.outputStream); 55 request.inputStream.pipe(resp.outputStream);
50 }); 56 });
51 57
52 _httpServer.listen(host, port); 58 httpServer.listen(host, 0);
59 return httpServer;
53 } 60 }
54 61
55 terminateHttpServer() { 62 terminateHttpServers([List<HttpServer> servers = const []]) {
Mads Ager (google) 2013/01/04 09:39:20 Can we make the servers argument mandatory?
Emily Fortuna 2013/01/04 23:26:54 Done.
56 if (_httpServer != null) _httpServer.close(); 63 for (var server in servers) server.close();
57 } 64 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698