| 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 application; | 5 library application; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert' show UTF8; | 8 import 'dart:convert' show UTF8; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| 11 import 'context_registry.dart'; | 11 import 'context_registry.dart'; |
| 12 import 'http_wrapper.dart'; | 12 import 'http_wrapper.dart'; |
| 13 | 13 |
| 14 void _info(String message) { | 14 void _info(String message) { |
| 15 var formattedMessage = "${new DateTime.now()}: " + message; | 15 var formattedMessage = "${new DateTime.now()}: " + message; |
| 16 print(formattedMessage); | 16 print(formattedMessage); |
| 17 } | 17 } |
| 18 | 18 |
| 19 String _getHostname() => | |
| 20 (Process.runSync('/bin/hostname', []).stdout as String).trim(); | |
| 21 | |
| 22 class AppEngineHttpServer { | 19 class AppEngineHttpServer { |
| 23 final ContextRegistry _contextRegistry; | 20 final ContextRegistry _contextRegistry; |
| 24 | 21 |
| 25 final String _hostname; | 22 final String _hostname; |
| 26 final int _port; | 23 final int _port; |
| 27 | 24 |
| 28 final String _localHostname; | |
| 29 final Completer _shutdownCompleter = new Completer(); | 25 final Completer _shutdownCompleter = new Completer(); |
| 30 int _pendingRequests = 0; | 26 int _pendingRequests = 0; |
| 31 | 27 |
| 32 HttpServer _httpServer; | 28 HttpServer _httpServer; |
| 33 | 29 |
| 34 AppEngineHttpServer(this._contextRegistry, | 30 AppEngineHttpServer(this._contextRegistry, |
| 35 {String hostname: '0.0.0.0', int port: 8080}) | 31 {String hostname: '0.0.0.0', int port: 8080}) |
| 36 : _localHostname = _getHostname(), _hostname = hostname, _port = port; | 32 : _hostname = hostname, _port = port; |
| 37 | 33 |
| 38 Future get done => _shutdownCompleter.future; | 34 Future get done => _shutdownCompleter.future; |
| 39 | 35 |
| 40 void run(applicationHandler(request, context)) { | 36 void run(applicationHandler(request, context)) { |
| 41 var serviceHandlers = { | 37 var serviceHandlers = { |
| 42 '/_ah/start' : _start, | 38 '/_ah/start' : _start, |
| 43 '/_ah/health' : _health, | 39 '/_ah/health' : _health, |
| 44 '/_ah/stop' : _stop | 40 '/_ah/stop' : _stop |
| 45 }; | 41 }; |
| 46 | 42 |
| 47 HttpServer.bind(_hostname, _port).then((HttpServer server) { | 43 HttpServer.bind(_hostname, _port).then((HttpServer server) { |
| 48 _httpServer = server; | 44 _httpServer = server; |
| 49 | 45 |
| 50 server.listen((HttpRequest request) { | 46 server.listen((HttpRequest request) { |
| 51 var appengineRequest = new AppengineHttpRequest(request); | 47 var appengineRequest = new AppengineHttpRequest(request); |
| 52 | 48 |
| 53 _info("Got request: ${appengineRequest.uri}"); | 49 _info("Got request: ${appengineRequest.uri}"); |
| 54 | 50 |
| 55 // Default handling is sending the request to the aplication. | 51 // Default handling is sending the request to the application. |
| 56 var handler = applicationHandler; | 52 var handler = applicationHandler; |
| 57 | 53 |
| 58 // Check if the request path is one of the service handlers. | 54 // Check if the request path is one of the service handlers. |
| 59 String path = appengineRequest.uri.path; | 55 String path = appengineRequest.uri.path; |
| 60 for (var pattern in serviceHandlers.keys) { | 56 for (var pattern in serviceHandlers.keys) { |
| 61 if (path.startsWith(pattern)) { | 57 if (path.startsWith(pattern)) { |
| 62 handler = serviceHandlers[pattern]; | 58 handler = serviceHandlers[pattern]; |
| 63 break; | 59 break; |
| 64 } | 60 } |
| 65 } | 61 } |
| 66 | 62 |
| 67 _pendingRequests++; | 63 _pendingRequests++; |
| 68 var context = _contextRegistry.add(appengineRequest); | 64 var context = _contextRegistry.add(appengineRequest); |
| 69 /* | |
| 70 * This sets the 'Server' header in the http response to the hostname | |
| 71 * of the machine the application is running on. | |
| 72 * It seems like the AppEngine VmRuntime stub (on the other end) will | |
| 73 * not accept the response if we use the default value. | |
| 74 */ | |
| 75 appengineRequest.response.headers.set('Server', _localHostname); | |
| 76 appengineRequest.response.registerHook( | 65 appengineRequest.response.registerHook( |
| 77 () => _contextRegistry.remove(appengineRequest)); | 66 () => _contextRegistry.remove(appengineRequest)); |
| 78 | 67 |
| 79 appengineRequest.response.done.catchError((error) { | 68 appengineRequest.response.done.catchError((error) { |
| 80 _info("Error while handling response: $error"); | 69 _info("Error while handling response: $error"); |
| 81 _pendingRequests--; | 70 _pendingRequests--; |
| 82 _checkShutdown(); | 71 _checkShutdown(); |
| 83 }); | 72 }); |
| 84 | 73 |
| 85 handler(appengineRequest, context); | 74 handler(appengineRequest, context); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 response.headers.contentType = | 112 response.headers.contentType = |
| 124 new ContentType('text', 'plain', charset: 'utf-8'); | 113 new ContentType('text', 'plain', charset: 'utf-8'); |
| 125 response.headers.set("Cache-Control", "no-cache"); | 114 response.headers.set("Cache-Control", "no-cache"); |
| 126 response.headers.set("Server", _hostname); | 115 response.headers.set("Server", _hostname); |
| 127 response.contentLength = data.length; | 116 response.contentLength = data.length; |
| 128 response.statusCode = statusCode; | 117 response.statusCode = statusCode; |
| 129 response.add(data); | 118 response.add(data); |
| 130 response.close(); | 119 response.close(); |
| 131 } | 120 } |
| 132 } | 121 } |
| OLD | NEW |