| 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 library http_server.http_body_impl; |
| 6 | 6 |
| 7 class _HttpBodyHandlerTransformer | 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 import 'dart:io'; |
| 10 |
| 11 import 'package:mime/mime.dart'; |
| 12 |
| 13 import 'http_body.dart'; |
| 14 import 'http_multipart_form_data.dart'; |
| 15 |
| 16 class HttpBodyHandlerTransformer |
| 8 implements StreamTransformer<HttpRequest, HttpRequestBody> { | 17 implements StreamTransformer<HttpRequest, HttpRequestBody> { |
| 9 final Encoding _defaultEncoding; | 18 final Encoding _defaultEncoding; |
| 10 | 19 |
| 11 const _HttpBodyHandlerTransformer(this._defaultEncoding); | 20 const HttpBodyHandlerTransformer(this._defaultEncoding); |
| 12 | 21 |
| 13 Stream<HttpRequestBody> bind(Stream<HttpRequest> stream) { | 22 Stream<HttpRequestBody> bind(Stream<HttpRequest> stream) { |
| 14 return new Stream<HttpRequestBody>.eventTransformed( | 23 return new Stream<HttpRequestBody>.eventTransformed( |
| 15 stream, | 24 stream, |
| 16 (EventSink<HttpRequestBody> sink) => | 25 (EventSink<HttpRequestBody> sink) => |
| 17 new _HttpBodyHandlerTransformerSink(_defaultEncoding, sink)); | 26 new _HttpBodyHandlerTransformerSink(_defaultEncoding, sink)); |
| 18 } | 27 } |
| 19 } | 28 } |
| 20 | 29 |
| 21 class _HttpBodyHandlerTransformerSink implements EventSink<HttpRequest> { | 30 class _HttpBodyHandlerTransformerSink implements EventSink<HttpRequest> { |
| 22 final Encoding _defaultEncoding; | 31 final Encoding _defaultEncoding; |
| 23 final EventSink<HttpRequestBody> _outSink; | 32 final EventSink<HttpRequestBody> _outSink; |
| 24 int _pending = 0; | 33 int _pending = 0; |
| 25 bool _closed = false; | 34 bool _closed = false; |
| 26 | 35 |
| 27 _HttpBodyHandlerTransformerSink(this._defaultEncoding, this._outSink); | 36 _HttpBodyHandlerTransformerSink(this._defaultEncoding, this._outSink); |
| 28 | 37 |
| 29 void add(HttpRequest request) { | 38 void add(HttpRequest request) { |
| 30 _pending++; | 39 _pending++; |
| 31 _HttpBodyHandler.processRequest(request, _defaultEncoding) | 40 HttpBodyHandlerImpl.processRequest(request, _defaultEncoding) |
| 32 .then(_outSink.add, onError: _outSink.addError) | 41 .then(_outSink.add, onError: _outSink.addError) |
| 33 .whenComplete(() { | 42 .whenComplete(() { |
| 34 _pending--; | 43 _pending--; |
| 35 if (_closed && _pending == 0) _outSink.close(); | 44 if (_closed && _pending == 0) _outSink.close(); |
| 36 }); | 45 }); |
| 37 } | 46 } |
| 38 void addError(Object error, [StackTrace stackTrace]) { | 47 void addError(Object error, [StackTrace stackTrace]) { |
| 39 _outSink.addError(error, stackTrace); | 48 _outSink.addError(error, stackTrace); |
| 40 } | 49 } |
| 41 void close() { | 50 void close() { |
| 42 _closed = true; | 51 _closed = true; |
| 43 if (_pending == 0) _outSink.close(); | 52 if (_pending == 0) _outSink.close(); |
| 44 } | 53 } |
| 45 } | 54 } |
| 46 | 55 |
| 47 class _HttpBodyHandler { | 56 class HttpBodyHandlerImpl { |
| 48 static Future<HttpRequestBody> processRequest( | 57 static Future<HttpRequestBody> processRequest( |
| 49 HttpRequest request, | 58 HttpRequest request, |
| 50 Encoding defaultEncoding) { | 59 Encoding defaultEncoding) { |
| 51 return process(request, request.headers, defaultEncoding) | 60 return process(request, request.headers, defaultEncoding) |
| 52 .then((body) => new _HttpRequestBody(request, body), | 61 .then((body) => new _HttpRequestBody(request, body), |
| 53 onError: (error) { | 62 onError: (error) { |
| 54 // Try to send BAD_REQUEST response. | 63 // Try to send BAD_REQUEST response. |
| 55 request.response.statusCode = HttpStatus.BAD_REQUEST; | 64 request.response.statusCode = HttpStatus.BAD_REQUEST; |
| 56 request.response.close(); | 65 request.response.close(); |
| 57 throw error; | 66 throw error; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 : super(body.type, body.body); | 205 : super(body.type, body.body); |
| 197 } | 206 } |
| 198 | 207 |
| 199 class _HttpClientResponseBody | 208 class _HttpClientResponseBody |
| 200 extends _HttpBody implements HttpClientResponseBody { | 209 extends _HttpBody implements HttpClientResponseBody { |
| 201 final HttpClientResponse response; | 210 final HttpClientResponse response; |
| 202 | 211 |
| 203 _HttpClientResponseBody(this.response, HttpBody body) | 212 _HttpClientResponseBody(this.response, HttpBody body) |
| 204 : super(body.type, body.body); | 213 : super(body.type, body.body); |
| 205 } | 214 } |
| OLD | NEW |