| 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 /// A Shelf adapter for handling [HttpRequest] objects from `dart:io`. | |
| 6 /// | |
| 7 /// One can provide an instance of [HttpServer] as the `requests` parameter in | |
| 8 /// [serveRequests]. | |
| 9 /// | |
| 10 /// The `dart:io` adapter supports request hijacking; see [Request.hijack]. | |
| 11 library shelf.io; | |
| 12 | |
| 13 import 'dart:async'; | |
| 14 import 'dart:io'; | |
| 15 | |
| 16 import 'package:stack_trace/stack_trace.dart'; | |
| 17 | |
| 18 import 'shelf.dart'; | |
| 19 import 'src/util.dart'; | |
| 20 | |
| 21 /// Starts an [HttpServer] that listens on the specified [address] and | |
| 22 /// [port] and sends requests to [handler]. | |
| 23 /// | |
| 24 /// See the documentation for [HttpServer.bind] for more details on [address], | |
| 25 /// [port], and [backlog]. | |
| 26 Future<HttpServer> serve(Handler handler, address, int port, | |
| 27 {int backlog}) { | |
| 28 if (backlog == null) backlog = 0; | |
| 29 return HttpServer.bind(address, port, backlog: backlog).then((server) { | |
| 30 serveRequests(server, handler); | |
| 31 return server; | |
| 32 }); | |
| 33 } | |
| 34 | |
| 35 /// Serve a [Stream] of [HttpRequest]s. | |
| 36 /// | |
| 37 /// [HttpServer] implements [Stream<HttpRequest>] so it can be passed directly | |
| 38 /// to [serveRequests]. | |
| 39 /// | |
| 40 /// Errors thrown by [handler] while serving a request will be printed to the | |
| 41 /// console and cause a 500 response with no body. Errors thrown asynchronously | |
| 42 /// by [handler] will be printed to the console or, if there's an active error | |
| 43 /// zone, passed to that zone. | |
| 44 void serveRequests(Stream<HttpRequest> requests, Handler handler) { | |
| 45 catchTopLevelErrors(() { | |
| 46 requests.listen((request) => handleRequest(request, handler)); | |
| 47 }, (error, stackTrace) { | |
| 48 _logError('Asynchronous error\n$error', stackTrace); | |
| 49 }); | |
| 50 } | |
| 51 | |
| 52 /// Uses [handler] to handle [request]. | |
| 53 /// | |
| 54 /// Returns a [Future] which completes when the request has been handled. | |
| 55 Future handleRequest(HttpRequest request, Handler handler) { | |
| 56 var shelfRequest; | |
| 57 try { | |
| 58 shelfRequest = _fromHttpRequest(request); | |
| 59 } catch (error, stackTrace) { | |
| 60 var response = _logError('Error parsing request.\n$error', stackTrace); | |
| 61 return _writeResponse(response, request.response); | |
| 62 } | |
| 63 | |
| 64 // TODO(nweiz): abstract out hijack handling to make it easier to implement an | |
| 65 // adapter. | |
| 66 return syncFuture(() => handler(shelfRequest)) | |
| 67 .catchError((error, stackTrace) { | |
| 68 if (error is HijackException) { | |
| 69 // A HijackException should bypass the response-writing logic entirely. | |
| 70 if (!shelfRequest.canHijack) throw error; | |
| 71 | |
| 72 // If the request wasn't hijacked, we shouldn't be seeing this exception. | |
| 73 return _logError( | |
| 74 "Caught HijackException, but the request wasn't hijacked.", | |
| 75 stackTrace); | |
| 76 } | |
| 77 | |
| 78 return _logError('Error thrown by handler.\n$error', stackTrace); | |
| 79 }).then((response) { | |
| 80 if (response == null) { | |
| 81 response = _logError('null response from handler.'); | |
| 82 } else if (!shelfRequest.canHijack) { | |
| 83 var message = new StringBuffer() | |
| 84 ..writeln("Got a response for hijacked request " | |
| 85 "${shelfRequest.method} ${shelfRequest.requestedUri}:") | |
| 86 ..writeln(response.statusCode); | |
| 87 response.headers.forEach((key, value) => | |
| 88 message.writeln("${key}: ${value}")); | |
| 89 throw new Exception(message.toString().trim()); | |
| 90 } | |
| 91 | |
| 92 return _writeResponse(response, request.response); | |
| 93 }).catchError((error, stackTrace) { | |
| 94 // Ignore HijackExceptions. | |
| 95 if (error is! HijackException) throw error; | |
| 96 }); | |
| 97 } | |
| 98 | |
| 99 /// Creates a new [Request] from the provided [HttpRequest]. | |
| 100 Request _fromHttpRequest(HttpRequest request) { | |
| 101 var headers = {}; | |
| 102 request.headers.forEach((k, v) { | |
| 103 // Multiple header values are joined with commas. | |
| 104 // See http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-21#page-22 | |
| 105 headers[k] = v.join(','); | |
| 106 }); | |
| 107 | |
| 108 onHijack(callback) { | |
| 109 return request.response.detachSocket(writeHeaders: false) | |
| 110 .then((socket) => callback(socket, socket)); | |
| 111 } | |
| 112 | |
| 113 return new Request(request.method, request.requestedUri, | |
| 114 protocolVersion: request.protocolVersion, headers: headers, | |
| 115 body: request, onHijack: onHijack); | |
| 116 } | |
| 117 | |
| 118 Future _writeResponse(Response response, HttpResponse httpResponse) { | |
| 119 httpResponse.statusCode = response.statusCode; | |
| 120 | |
| 121 response.headers.forEach((header, value) { | |
| 122 if (value == null) return; | |
| 123 httpResponse.headers.set(header, value); | |
| 124 }); | |
| 125 | |
| 126 if (!response.headers.containsKey(HttpHeaders.SERVER)) { | |
| 127 httpResponse.headers.set(HttpHeaders.SERVER, 'dart:io with Shelf'); | |
| 128 } | |
| 129 | |
| 130 if (!response.headers.containsKey(HttpHeaders.DATE)) { | |
| 131 httpResponse.headers.date = new DateTime.now().toUtc(); | |
| 132 } | |
| 133 | |
| 134 return httpResponse.addStream(response.read()) | |
| 135 .then((_) => httpResponse.close()); | |
| 136 } | |
| 137 | |
| 138 // TODO(kevmoo) A developer mode is needed to include error info in response | |
| 139 // TODO(kevmoo) Make error output plugable. stderr, logging, etc | |
| 140 Response _logError(String message, [StackTrace stackTrace]) { | |
| 141 var chain = new Chain.current(); | |
| 142 if (stackTrace != null) { | |
| 143 chain = new Chain.forTrace(stackTrace); | |
| 144 } | |
| 145 chain = chain | |
| 146 .foldFrames((frame) => frame.isCore || frame.package == 'shelf') | |
| 147 .terse; | |
| 148 | |
| 149 stderr.writeln('ERROR - ${new DateTime.now()}'); | |
| 150 stderr.writeln(message); | |
| 151 stderr.writeln(chain); | |
| 152 return new Response.internalServerError(); | |
| 153 } | |
| OLD | NEW |