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

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: Merge and fix tests. 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
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) {
Søren Gjesse 2013/05/06 12:28:10 Throw here for now?
Anders Johnsen 2013/05/06 13:38:16 Done.
25 // TODO(ajohnsen): Support BASE64, etc.
26 return;
27 }
28
29 if (contentType == null || contentType.primaryType == 'text') {
30 _isText = true;
31 StringBuffer buffer = new StringBuffer();
32 _stream = _stream
Søren Gjesse 2013/05/06 12:28:10 Use the content type charset for decoding if provi
Anders Johnsen 2013/05/06 13:38:16 Done.
33 .transform(new StringDecoder(Encoding.ISO_8859_1))
34 .expand((data) {
Søren Gjesse 2013/05/06 12:28:10 What if the HTML entites cross data boundaries?
Anders Johnsen 2013/05/06 13:38:16 That's what the 'null' check is for. It returns nu
35 buffer.write(data);
36 var out = _HttpUtils.parseHttpEntityString(buffer.toString());
37 if (out != null) {
38 buffer = new StringBuffer();
39 return [out];
40 }
41 return const [];
42 });
43 }
44 }
45
46 bool get isText => _isText;
47 bool get isBinary => !_isText;
48
49 static HttpMultipartFormData parse(MimeMultipart multipart) {
50 var type;
51 var encoding;
52 var disposition;
53 var remaining = new Map<String, String>();
54 for (String key in multipart.headers.keys) {
55 switch (key) {
56 case HttpHeaders.CONTENT_TYPE:
57 type = ContentType.parse(multipart.headers[key]);
58 break;
59
60 case 'content-transfer-encoding':
61 encoding = HeaderValue.parse(multipart.headers[key]);
62 break;
63
64 case 'content-disposition':
65 disposition = HeaderValue.parse(multipart.headers[key]);
66 break;
67
68 default:
69 remaining[key] = headers[key];
70 break;
71 }
72 }
73 if (disposition == null) {
74 throw new HttpException(
75 "Mime Multipart doesn't contain a Content-Disposition header value");
76 }
77 return new _HttpMultipartFormData(type, disposition, encoding, multipart);
78 }
79
80 StreamSubscription listen(void onData(data),
81 {void onDone(),
82 void onError(error),
83 bool cancelOnError}) {
84 return _stream.listen(onData,
85 onDone: onDone,
86 onError: onError,
87 cancelOnError: cancelOnError);
88 }
89
90 String value(String name) {
91 return _mimeMultipart.headers[name];
92 }
93 }
94
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698