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