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 || contentType.primaryType == 'text') { |
| 31 _isText = true; |
| 32 StringBuffer buffer = new StringBuffer(); |
| 33 Encoding encoding; |
| 34 if (contentType != null) { |
| 35 encoding = Encoding.fromName(contentType.charset); |
| 36 } |
| 37 if (encoding == null) encoding = Encoding.ISO_8859_1; |
| 38 _stream = _stream |
| 39 .transform(new StringDecoder(encoding)) |
| 40 .expand((data) { |
| 41 buffer.write(data); |
| 42 var out = _HttpUtils.parseHttpEntityString(buffer.toString()); |
| 43 if (out != null) { |
| 44 buffer = new StringBuffer(); |
| 45 return [out]; |
| 46 } |
| 47 return const []; |
| 48 }); |
| 49 } |
| 50 } |
| 51 |
| 52 bool get isText => _isText; |
| 53 bool get isBinary => !_isText; |
| 54 |
| 55 static HttpMultipartFormData parse(MimeMultipart multipart) { |
| 56 var type; |
| 57 var encoding; |
| 58 var disposition; |
| 59 var remaining = new Map<String, String>(); |
| 60 for (String key in multipart.headers.keys) { |
| 61 switch (key) { |
| 62 case HttpHeaders.CONTENT_TYPE: |
| 63 type = ContentType.parse(multipart.headers[key]); |
| 64 break; |
| 65 |
| 66 case 'content-transfer-encoding': |
| 67 encoding = HeaderValue.parse(multipart.headers[key]); |
| 68 break; |
| 69 |
| 70 case 'content-disposition': |
| 71 disposition = HeaderValue.parse(multipart.headers[key]); |
| 72 break; |
| 73 |
| 74 default: |
| 75 remaining[key] = multipart.headers[key]; |
| 76 break; |
| 77 } |
| 78 } |
| 79 if (disposition == null) { |
| 80 throw new HttpException( |
| 81 "Mime Multipart doesn't contain a Content-Disposition header value"); |
| 82 } |
| 83 return new _HttpMultipartFormData(type, disposition, encoding, multipart); |
| 84 } |
| 85 |
| 86 StreamSubscription listen(void onData(data), |
| 87 {void onDone(), |
| 88 void onError(error), |
| 89 bool cancelOnError}) { |
| 90 return _stream.listen(onData, |
| 91 onDone: onDone, |
| 92 onError: onError, |
| 93 cancelOnError: cancelOnError); |
| 94 } |
| 95 |
| 96 String value(String name) { |
| 97 return _mimeMultipart.headers[name]; |
| 98 } |
| 99 } |
| 100 |
OLD | NEW |