| 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 dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 class _HttpBodyHandlerTransformer | 7 class _HttpBodyHandlerTransformer |
| 8 extends StreamEventTransformer<HttpRequest, HttpRequestBody> { | 8 extends StreamEventTransformer<HttpRequest, HttpRequestBody> { |
| 9 final Encoding _defaultEncoding; |
| 10 _HttpBodyHandlerTransformer(this._defaultEncoding); |
| 11 |
| 9 void handleData(HttpRequest request, EventSink<HttpRequestBody> sink) { | 12 void handleData(HttpRequest request, EventSink<HttpRequestBody> sink) { |
| 10 HttpBodyHandler.processRequest(request) | 13 _HttpBodyHandler.processRequest(request, _defaultEncoding) |
| 11 .then(sink.add, onError: sink.addError); | 14 .then(sink.add, onError: sink.addError); |
| 12 } | 15 } |
| 13 } | 16 } |
| 14 | 17 |
| 15 class _HttpBodyHandler { | 18 class _HttpBodyHandler { |
| 16 static Future<HttpRequestBody> processRequest(HttpRequest request) { | 19 static Future<HttpRequestBody> processRequest( |
| 17 return process(request, request.headers) | 20 HttpRequest request, |
| 21 Encoding defaultEncoding) { |
| 22 return process(request, request.headers, defaultEncoding) |
| 18 .then((body) => new _HttpRequestBody(request, body), | 23 .then((body) => new _HttpRequestBody(request, body), |
| 19 onError: (error) { | 24 onError: (error) { |
| 20 // Try to send BAD_REQUEST response. | 25 // Try to send BAD_REQUEST response. |
| 21 request.response.statusCode = HttpStatus.BAD_REQUEST; | 26 request.response.statusCode = HttpStatus.BAD_REQUEST; |
| 22 request.response.close(); | 27 request.response.close(); |
| 23 request.response.done.catchError((_) {}); | 28 request.response.done.catchError((_) {}); |
| 24 throw error; | 29 throw error; |
| 25 }); | 30 }); |
| 26 } | 31 } |
| 27 | 32 |
| 28 static Future<HttpClientResponseBody> processResponse( | 33 static Future<HttpClientResponseBody> processResponse( |
| 29 HttpClientResponse response) { | 34 HttpClientResponse response, |
| 30 return process(response, response.headers) | 35 Encoding defaultEncoding) { |
| 36 return process(response, response.headers, defaultEncoding) |
| 31 .then((body) => new _HttpClientResponseBody(response, body)); | 37 .then((body) => new _HttpClientResponseBody(response, body)); |
| 32 } | 38 } |
| 33 | 39 |
| 34 static Future<HttpBody> process(Stream<List<int>> stream, | 40 static Future<HttpBody> process(Stream<List<int>> stream, |
| 35 HttpHeaders headers) { | 41 HttpHeaders headers, |
| 42 Encoding defaultEncoding) { |
| 36 ContentType contentType = headers.contentType; | 43 ContentType contentType = headers.contentType; |
| 37 | 44 |
| 38 Future<HttpBody> asBinary() { | 45 Future<HttpBody> asBinary() { |
| 39 return stream | 46 return stream |
| 40 .fold(new _BufferList(), (buffer, data) => buffer..add(data)) | 47 .fold(new _BufferList(), (buffer, data) => buffer..add(data)) |
| 41 .then((buffer) => new _HttpBody(contentType, | 48 .then((buffer) => new _HttpBody(contentType, |
| 42 "binary", | 49 "binary", |
| 43 buffer.readBytes())); | 50 buffer.readBytes())); |
| 44 } | 51 } |
| 45 | 52 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 switch (contentType.subType) { | 113 switch (contentType.subType) { |
| 107 case "json": | 114 case "json": |
| 108 return asText(Encoding.UTF_8) | 115 return asText(Encoding.UTF_8) |
| 109 .then((body) => new _HttpBody(contentType, | 116 .then((body) => new _HttpBody(contentType, |
| 110 "json", | 117 "json", |
| 111 JSON.parse(body.body))); | 118 JSON.parse(body.body))); |
| 112 | 119 |
| 113 case "x-www-form-urlencoded": | 120 case "x-www-form-urlencoded": |
| 114 return asText(Encoding.ASCII) | 121 return asText(Encoding.ASCII) |
| 115 .then((body) { | 122 .then((body) { |
| 116 var map = _HttpUtils.splitQueryString(body.body); | 123 var map = _HttpUtils.splitQueryString( |
| 124 body.body, encoding: defaultEncoding); |
| 117 var result = {}; | 125 var result = {}; |
| 118 String parse(String s) { | 126 String parse(String s) { |
| 119 return _HttpUtils.decodeHttpEntityString( | 127 return _HttpUtils.decodeHttpEntityString(s); |
| 120 _HttpUtils.decodeUrlEncodedString(s)); | |
| 121 } | 128 } |
| 122 for (var key in map.keys) { | 129 for (var key in map.keys) { |
| 123 result[parse(key)] = parse(map[key]); | 130 result[parse(key)] = parse(map[key]); |
| 124 } | 131 } |
| 125 return new _HttpBody(contentType, "form", result); | 132 return new _HttpBody(contentType, "form", result); |
| 126 }); | 133 }); |
| 127 | 134 |
| 128 default: | 135 default: |
| 129 break; | 136 break; |
| 130 } | 137 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 _HttpClientResponseBody(HttpClientResponse response, HttpBody body) | 193 _HttpClientResponseBody(HttpClientResponse response, HttpBody body) |
| 187 : super(body.contentType, body.type, body.body), | 194 : super(body.contentType, body.type, body.body), |
| 188 this.response = response; | 195 this.response = response; |
| 189 | 196 |
| 190 int get statusCode => response.statusCode; | 197 int get statusCode => response.statusCode; |
| 191 | 198 |
| 192 String get reasonPhrase => response.reasonPhrase; | 199 String get reasonPhrase => response.reasonPhrase; |
| 193 | 200 |
| 194 HttpHeaders get headers => response.headers; | 201 HttpHeaders get headers => response.headers; |
| 195 } | 202 } |
| OLD | NEW |