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

Unified Diff: lib/shelf_io.dart

Issue 1037483002: Add support for controlling HttpResponse.bufferOutput to shelf_io. (Closed) Base URL: git@github.com:dart-lang/shelf@master
Patch Set: Code review changes Created 5 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/shelf_io.dart
diff --git a/lib/shelf_io.dart b/lib/shelf_io.dart
index 4ca48b6866481402cb7fd5ea118f19700a52934a..301050f28713f5a4668516c287d02168be74887f 100644
--- a/lib/shelf_io.dart
+++ b/lib/shelf_io.dart
@@ -7,7 +7,14 @@
/// One can provide an instance of [HttpServer] as the `requests` parameter in
/// [serveRequests].
///
-/// The `dart:io` adapter supports request hijacking; see [Request.hijack].
+/// This adapter supports request hijacking; see [Request.hijack]. It also
+/// supports the `"shelf.io.buffer_output"` `Response.context` property. If this
+/// property is `true` (the default), streamed responses will be buffered to
+/// improve performance; if it's `false`, all chunks will be pushed over the
+/// wire as they're received. See [`HttpResponse.bufferOutput`][bufferOutput]
+/// for more information.
+///
+/// [bufferOutput]: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:io.HttpResponse#id_bufferOutput
library shelf.io;
import 'dart:async';
@@ -78,17 +85,17 @@ Future handleRequest(HttpRequest request, Handler handler) {
}).then((response) {
if (response == null) {
response = _logError('null response from handler.');
- } else if (!shelfRequest.canHijack) {
- var message = new StringBuffer()
- ..writeln("Got a response for hijacked request "
- "${shelfRequest.method} ${shelfRequest.requestedUri}:")
- ..writeln(response.statusCode);
- response.headers
- .forEach((key, value) => message.writeln("${key}: ${value}"));
- throw new Exception(message.toString().trim());
+ } else if (shelfRequest.canHijack) {
+ return _writeResponse(response, request.response);
}
- return _writeResponse(response, request.response);
+ var message = new StringBuffer()
+ ..writeln("Got a response for hijacked request "
+ "${shelfRequest.method} ${shelfRequest.requestedUri}:")
+ ..writeln(response.statusCode);
+ response.headers
+ .forEach((key, value) => message.writeln("${key}: ${value}"));
+ throw new Exception(message.toString().trim());
}).catchError((error, stackTrace) {
// Ignore HijackExceptions.
if (error is! HijackException) throw error;
@@ -118,6 +125,10 @@ Request _fromHttpRequest(HttpRequest request) {
}
Future _writeResponse(Response response, HttpResponse httpResponse) {
+ if (response.context.containsKey("shelf.io.buffer_output")) {
+ httpResponse.bufferOutput = response.context["shelf.io.buffer_output"];
+ }
+
httpResponse.statusCode = response.statusCode;
response.headers.forEach((header, value) {
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698