| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, 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 | |
| 8 class _HttpMultipartFormData extends Stream implements HttpMultipartFormData { | |
| 9 final ContentType contentType; | |
| 10 final HeaderValue contentDisposition; | |
| 11 final HeaderValue contentTransferEncoding; | |
| 12 | |
| 13 final MimeMultipart _mimeMultipart; | |
| 14 | |
| 15 bool _isText = false; | |
| 16 | |
| 17 Stream _stream; | |
| 18 | |
| 19 _HttpMultipartFormData(ContentType this.contentType, | |
| 20 HeaderValue this.contentDisposition, | |
| 21 HeaderValue this.contentTransferEncoding, | |
| 22 MimeMultipart this._mimeMultipart) { | |
| 23 _stream = _mimeMultipart; | |
| 24 if (contentTransferEncoding != null) { | |
| 25 // TODO(ajohnsen): Support BASE64, etc. | |
| 26 throw new HttpException("Unsupported contentTransferEncoding: " | |
| 27 "${contentTransferEncoding.value}"); | |
| 28 } | |
| 29 | |
| 30 if (contentType == null || | |
| 31 contentType.primaryType == 'text' || | |
| 32 contentType.mimeType == 'application/json') { | |
| 33 _isText = true; | |
| 34 StringBuffer buffer = new StringBuffer(); | |
| 35 Encoding encoding; | |
| 36 if (contentType != null) { | |
| 37 encoding = Encoding.fromName(contentType.charset); | |
| 38 } | |
| 39 if (encoding == null) encoding = Encoding.ISO_8859_1; | |
| 40 _stream = _stream | |
| 41 .transform(new StringDecoder(encoding)) | |
| 42 .expand((data) { | |
| 43 buffer.write(data); | |
| 44 var out = _HttpUtils.decodeHttpEntityString(buffer.toString()); | |
| 45 if (out != null) { | |
| 46 buffer = new StringBuffer(); | |
| 47 return [out]; | |
| 48 } | |
| 49 return const []; | |
| 50 }); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 bool get isText => _isText; | |
| 55 bool get isBinary => !_isText; | |
| 56 | |
| 57 static HttpMultipartFormData parse(MimeMultipart multipart) { | |
| 58 var type; | |
| 59 var encoding; | |
| 60 var disposition; | |
| 61 var remaining = new Map<String, String>(); | |
| 62 for (String key in multipart.headers.keys) { | |
| 63 switch (key) { | |
| 64 case HttpHeaders.CONTENT_TYPE: | |
| 65 type = ContentType.parse(multipart.headers[key]); | |
| 66 break; | |
| 67 | |
| 68 case 'content-transfer-encoding': | |
| 69 encoding = HeaderValue.parse(multipart.headers[key]); | |
| 70 break; | |
| 71 | |
| 72 case 'content-disposition': | |
| 73 disposition = HeaderValue.parse(multipart.headers[key]); | |
| 74 break; | |
| 75 | |
| 76 default: | |
| 77 remaining[key] = multipart.headers[key]; | |
| 78 break; | |
| 79 } | |
| 80 } | |
| 81 if (disposition == null) { | |
| 82 throw new HttpException( | |
| 83 "Mime Multipart doesn't contain a Content-Disposition header value"); | |
| 84 } | |
| 85 return new _HttpMultipartFormData(type, disposition, encoding, multipart); | |
| 86 } | |
| 87 | |
| 88 StreamSubscription listen(void onData(data), | |
| 89 {void onDone(), | |
| 90 void onError(error), | |
| 91 bool cancelOnError}) { | |
| 92 return _stream.listen(onData, | |
| 93 onDone: onDone, | |
| 94 onError: onError, | |
| 95 cancelOnError: cancelOnError); | |
| 96 } | |
| 97 | |
| 98 String value(String name) { | |
| 99 return _mimeMultipart.headers[name]; | |
| 100 } | |
| 101 } | |
| 102 | |
| OLD | NEW |