| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of dart.io; |
| 6 |
| 7 class _HttpBodyHandlerTransformer |
| 8 extends StreamEventTransformer<HttpRequest, HttpRequestBody> { |
| 9 void handleData(HttpRequest request, EventSink<HttpRequestBody> sink) { |
| 10 HttpBodyHandler.processRequest(request) |
| 11 .then(sink.add, onError: sink.addError); |
| 12 } |
| 13 } |
| 14 |
| 15 class _HttpBodyHandler implements HttpBodyHandler { |
| 16 Stream<HttpRequestBody> bind(Stream<HttpRequest> stream) { |
| 17 return new _HttpBodyHandlerTransformer().bind(stream); |
| 18 } |
| 19 |
| 20 static Future<HttpRequestBody> processRequest(HttpRequest request) { |
| 21 return process(request, request.headers) |
| 22 .then((body) => new _HttpRequestBody(request, body), |
| 23 onError: (error) { |
| 24 // Try to send BAD_REQUEST response. |
| 25 request.response.statusCode = HttpStatus.BAD_REQUEST; |
| 26 request.response.close(); |
| 27 request.response.done.catchError((_) {}); |
| 28 throw error; |
| 29 }); |
| 30 } |
| 31 |
| 32 static Future<HttpClientResponseBody> processResponse( |
| 33 HttpClientResponse response) { |
| 34 return process(response, response.headers) |
| 35 .then((body) => new _HttpClientResponseBody(response, body)); |
| 36 } |
| 37 |
| 38 static Future<HttpBody> process(Stream<List<int>> stream, |
| 39 HttpHeaders headers) { |
| 40 return stream.fold( |
| 41 new _BufferList(), |
| 42 (buffer, data) { |
| 43 // TODO(ajohnsen): Add limit for POST data. |
| 44 buffer.add(data); |
| 45 return buffer; |
| 46 }) |
| 47 .then((list) { |
| 48 var content = list.readBytes(); |
| 49 int type = HttpBodyType.BINARY; |
| 50 var mimeType = headers.contentType.toString(); |
| 51 String asText(Encoding defaultEncoding) { |
| 52 var encoding; |
| 53 var charset = headers.contentType.charset; |
| 54 if (charset != null) encoding = Encoding.fromName(charset); |
| 55 if (encoding == null) encoding = defaultEncoding; |
| 56 return _decodeString(content, encoding); |
| 57 } |
| 58 switch (headers.contentType.primaryType) { |
| 59 case "text": |
| 60 type = HttpBodyType.TEXT; |
| 61 content = asText(Encoding.ASCII); |
| 62 break; |
| 63 |
| 64 case "application": |
| 65 switch (headers.contentType.subType) { |
| 66 case "json": |
| 67 content = JSON.parse(asText(Encoding.UTF_8)); |
| 68 type = HttpBodyType.JSON; |
| 69 break; |
| 70 |
| 71 default: |
| 72 break; |
| 73 } |
| 74 break; |
| 75 |
| 76 default: |
| 77 break; |
| 78 } |
| 79 return new _HttpBody(mimeType, type, content); |
| 80 }); |
| 81 } |
| 82 } |
| 83 |
| 84 class _HttpBody implements HttpBody { |
| 85 final String mimeType; |
| 86 final HttpBodyType type; |
| 87 final dynamic body; |
| 88 |
| 89 _HttpBody(String this.mimeType, |
| 90 HttpBodyType this.type, |
| 91 dynamic this.body); |
| 92 } |
| 93 |
| 94 class _HttpRequestBody extends _HttpBody implements HttpRequestBody { |
| 95 final String method; |
| 96 final Uri uri; |
| 97 final HttpHeaders headers; |
| 98 final HttpResponse response; |
| 99 |
| 100 _HttpRequestBody(HttpRequest request, HttpBody body) |
| 101 : super(body.mimeType, body.type, body.body), |
| 102 method = request.method, |
| 103 uri = request.uri, |
| 104 headers = request.headers, |
| 105 response = request.response; |
| 106 } |
| 107 |
| 108 class _HttpClientResponseBody |
| 109 extends _HttpBody implements HttpClientResponseBody { |
| 110 final int statusCode; |
| 111 final String reasonPhrase; |
| 112 final HttpHeaders headers; |
| 113 |
| 114 _HttpClientResponseBody(HttpClientResponse response, HttpBody body) |
| 115 : super(body.mimeType, body.type, body.body), |
| 116 statusCode = response.statusCode, |
| 117 reasonPhrase = response.reasonPhrase, |
| 118 headers = response.headers; |
| 119 } |
| OLD | NEW |