Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Side by Side Diff: sdk/lib/io/http_multipart_form_data_impl.dart

Issue 14796015: Add new HttpMultipartFormData, used for parsing a MimeMultipart and extracting either text or binar… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update doc comments. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/http_multipart_form_data.dart ('k') | sdk/lib/io/http_utils.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
OLDNEW
« no previous file with comments | « sdk/lib/io/http_multipart_form_data.dart ('k') | sdk/lib/io/http_utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698