| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of http_server; | 5 part of http_server; |
| 6 | 6 |
| 7 class _HttpBodyHandlerTransformer | 7 class _HttpBodyHandlerTransformer |
| 8 implements StreamTransformer<HttpRequest, HttpRequestBody> { | 8 implements StreamTransformer<HttpRequest, HttpRequestBody> { |
| 9 final Encoding _defaultEncoding; | 9 final Encoding _defaultEncoding; |
| 10 | 10 |
| 11 const _HttpBodyHandlerTransformer(this._defaultEncoding); | 11 const _HttpBodyHandlerTransformer(this._defaultEncoding); |
| 12 | 12 |
| 13 Stream<HttpRequestBody> bind(Stream<HttpRequest> stream) { | 13 Stream<HttpRequestBody> bind(Stream<HttpRequest> stream) { |
| 14 return new Stream<HttpRequestBody>.eventTransformed( | 14 return new Stream<HttpRequestBody>.eventTransformed( |
| 15 stream, | 15 stream, |
| 16 (EventSink<HttpRequestBody> sink) => | 16 (EventSink<HttpRequestBody> sink) => |
| 17 new _HttpBodyHandlerTransformerSink(_defaultEncoding, sink)); | 17 new _HttpBodyHandlerTransformerSink(_defaultEncoding, sink)); |
| 18 } | 18 } |
| 19 } | 19 } |
| 20 | 20 |
| 21 class _HttpBodyHandlerTransformerSink implements EventSink<HttpRequest> { | 21 class _HttpBodyHandlerTransformerSink implements EventSink<HttpRequest> { |
| 22 final Encoding _defaultEncoding; | 22 final Encoding _defaultEncoding; |
| 23 final EventSink<HttpRequestBody> _outSink; | 23 final EventSink<HttpRequestBody> _outSink; |
| 24 int _pending = 0; |
| 25 bool _closed = false; |
| 24 | 26 |
| 25 _HttpBodyHandlerTransformerSink(this._defaultEncoding, this._outSink); | 27 _HttpBodyHandlerTransformerSink(this._defaultEncoding, this._outSink); |
| 26 | 28 |
| 27 void add(HttpRequest request) { | 29 void add(HttpRequest request) { |
| 30 _pending++; |
| 28 _HttpBodyHandler.processRequest(request, _defaultEncoding) | 31 _HttpBodyHandler.processRequest(request, _defaultEncoding) |
| 29 .then(_outSink.add, onError: _outSink.addError); | 32 .then(_outSink.add, onError: _outSink.addError) |
| 33 .whenComplete(() { |
| 34 _pending--; |
| 35 if (_closed && _pending == 0) _outSink.close(); |
| 36 }); |
| 30 } | 37 } |
| 31 void addError(Object error, [StackTrace stackTrace]) { | 38 void addError(Object error, [StackTrace stackTrace]) { |
| 32 _outSink.addError(error, stackTrace); | 39 _outSink.addError(error, stackTrace); |
| 33 } | 40 } |
| 34 void close() => _outSink.close(); | 41 void close() { |
| 42 _closed = true; |
| 43 if (_pending == 0) _outSink.close(); |
| 44 } |
| 35 } | 45 } |
| 36 | 46 |
| 37 class _HttpBodyHandler { | 47 class _HttpBodyHandler { |
| 38 static Future<HttpRequestBody> processRequest( | 48 static Future<HttpRequestBody> processRequest( |
| 39 HttpRequest request, | 49 HttpRequest request, |
| 40 Encoding defaultEncoding) { | 50 Encoding defaultEncoding) { |
| 41 return process(request, request.headers, defaultEncoding) | 51 return process(request, request.headers, defaultEncoding) |
| 42 .then((body) => new _HttpRequestBody(request, body), | 52 .then((body) => new _HttpRequestBody(request, body), |
| 43 onError: (error) { | 53 onError: (error) { |
| 44 // Try to send BAD_REQUEST response. | 54 // Try to send BAD_REQUEST response. |
| 45 request.response.statusCode = HttpStatus.BAD_REQUEST; | 55 request.response.statusCode = HttpStatus.BAD_REQUEST; |
| 46 request.response.close(); | 56 request.response.close(); |
| 47 request.response.done.catchError((_) {}); | |
| 48 throw error; | 57 throw error; |
| 49 }); | 58 }); |
| 50 } | 59 } |
| 51 | 60 |
| 52 static Future<HttpClientResponseBody> processResponse( | 61 static Future<HttpClientResponseBody> processResponse( |
| 53 HttpClientResponse response, | 62 HttpClientResponse response, |
| 54 Encoding defaultEncoding) { | 63 Encoding defaultEncoding) { |
| 55 return process(response, response.headers, defaultEncoding) | 64 return process(response, response.headers, defaultEncoding) |
| 56 .then((body) => new _HttpClientResponseBody(response, body)); | 65 .then((body) => new _HttpClientResponseBody(response, body)); |
| 57 } | 66 } |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 : super(body.type, body.body); | 196 : super(body.type, body.body); |
| 188 } | 197 } |
| 189 | 198 |
| 190 class _HttpClientResponseBody | 199 class _HttpClientResponseBody |
| 191 extends _HttpBody implements HttpClientResponseBody { | 200 extends _HttpBody implements HttpClientResponseBody { |
| 192 final HttpClientResponse response; | 201 final HttpClientResponse response; |
| 193 | 202 |
| 194 _HttpClientResponseBody(this.response, HttpBody body) | 203 _HttpClientResponseBody(this.response, HttpBody body) |
| 195 : super(body.type, body.body); | 204 : super(body.type, body.body); |
| 196 } | 205 } |
| OLD | NEW |