Chromium Code Reviews| 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 _HttpBodyHandler | |
| 8 extends StreamEventTransformer<HttpRequest, HttpRequestBody> | |
| 9 implements HttpBodyHandler { | |
| 10 | |
| 11 void handleData(HttpRequest request, EventSink<HttpRequestBody> sink) { | |
|
Mads Ager (google)
2013/04/10 08:20:26
You need to make this private. It is not part of t
Anders Johnsen
2013/04/10 12:52:49
Done.
| |
| 12 processRequest(request).then(sink.add, onError: sink.addError); | |
| 13 } | |
| 14 | |
| 15 static Future<HttpRequestBody> processRequest(HttpRequest request) { | |
| 16 return process(request, request.headers) | |
| 17 .then((body) => new _HttpRequestBody(request.response, body), | |
| 18 onError: (error) { | |
| 19 // Try to send BAD_REQUEST response. | |
| 20 request.response.statusCode = HttpStatus.BAD_REQUEST; | |
| 21 request.response.close(); | |
| 22 request.response.done.catchError((_) {}); | |
| 23 throw error; | |
| 24 }); | |
| 25 } | |
| 26 | |
| 27 static Future<HttpBody> processResponse(HttpClientResponse response) { | |
| 28 return process(response, response.headers); | |
| 29 } | |
| 30 | |
| 31 static Future<HttpBody> process(Stream<List<int>> stream, | |
|
Mads Ager (google)
2013/04/10 08:20:26
You need to make this private, it is not part of t
Anders Johnsen
2013/04/10 12:52:49
It's static, so it should be safe.
| |
| 32 HttpHeaders headers) { | |
|
Søren Gjesse
2013/04/10 07:09:11
Indentation.
Anders Johnsen
2013/04/10 12:52:49
Done.
| |
| 33 return stream.fold( | |
|
Søren Gjesse
2013/04/10 07:09:11
We could do a stream transformation to String here
Anders Johnsen
2013/04/10 12:52:49
It would complicate stuff, as we would have to fol
| |
| 34 new _BufferList(), | |
| 35 (buffer, data) { | |
| 36 // TODO(ajohnsen): Add limit for POST data. | |
| 37 buffer.add(data); | |
| 38 return buffer; | |
| 39 }) | |
| 40 .then((list) { | |
| 41 var content = list.readBytes(); | |
| 42 int type = HttpBody.BINARY; | |
| 43 var mimeType = headers.contentType.toString(); | |
| 44 String asText(Encoding defaultEncoding) { | |
| 45 var encoding; | |
| 46 var charset = headers.contentType.charset; | |
| 47 if (charset != null) encoding = Encoding.fromName(charset); | |
| 48 if (encoding == null) encoding = defaultEncoding; | |
| 49 return _decodeString(content, encoding); | |
| 50 } | |
| 51 switch (headers.contentType.primaryType) { | |
| 52 case "text": | |
| 53 type = HttpBody.TEXT; | |
| 54 content = asText(Encoding.ASCII); | |
| 55 break; | |
| 56 | |
| 57 case "application": | |
| 58 switch (headers.contentType.subType) { | |
| 59 case "json": | |
| 60 content = JSON.parse(asText(Encoding.UTF_8)); | |
| 61 type = HttpBody.JSON; | |
| 62 break; | |
| 63 | |
| 64 default: | |
| 65 break; | |
| 66 } | |
| 67 break; | |
| 68 | |
| 69 // TODO(ajohnsen): Add support for FORM data. | |
|
Mads Ager (google)
2013/04/10 08:20:26
For this commit, remove the FORM type so people ar
Anders Johnsen
2013/04/10 12:52:49
Done.
| |
| 70 | |
| 71 default: | |
| 72 break; | |
| 73 } | |
| 74 return new _HttpBody(mimeType, type, content); | |
| 75 }); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 class _HttpBody implements HttpBody { | |
| 80 final String mimeType; | |
| 81 final int type; | |
| 82 final dynamic body; | |
| 83 | |
| 84 _HttpBody(String this.mimeType, | |
| 85 int this.type, | |
| 86 dynamic this.body); | |
| 87 | |
| 88 Map get json => type == HttpBody.JSON ? body : null; | |
| 89 List<int> get binary => type == HttpBody.BINARY ? body : null; | |
| 90 Map get form => type == HttpBody.FORM ? body : null; | |
|
Mads Ager (google)
2013/04/10 08:20:26
Remove until supported?
Anders Johnsen
2013/04/10 12:52:49
Done.
| |
| 91 String get text => type == HttpBody.TEXT ? body : null; | |
| 92 } | |
| 93 | |
| 94 class _HttpRequestBody extends _HttpBody { | |
| 95 final HttpResponse response; | |
| 96 | |
| 97 _HttpRequestBody(HttpRequest this.response, HttpBody body) | |
| 98 : super(body.mimeType, body.type, body.body); | |
| 99 } | |
| OLD | NEW |