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

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

Issue 18438005: Move MimeMultipartTransformer and HttpBodyHandler to mime and http_server packages. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove import. Created 7 years, 5 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/io.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 ||
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 = _decodeHttpEntityString(buffer.toString());
45 if (out != null) {
46 buffer.clear();
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 // Decode a string with HTTP entities. Returns null if the string ends in the
103 // middle of a http entity.
104 static String _decodeHttpEntityString(String input) {
105 int amp = input.lastIndexOf('&');
106 if (amp < 0) return input;
107 int end = input.lastIndexOf(';');
108 if (end < amp) return null;
109
110 var buffer = new StringBuffer();
111 int offset = 0;
112
113 parse(amp, end) {
114 switch (input[amp + 1]) {
115 case '#':
116 if (input[amp + 2] == 'x') {
117 buffer.writeCharCode(
118 int.parse(input.substring(amp + 3, end), radix: 16));
119 } else {
120 buffer.writeCharCode(int.parse(input.substring(amp + 2, end)));
121 }
122 break;
123
124 default:
125 throw new HttpException('Unhandled HTTP entity token');
126 }
127 }
128
129 while ((amp = input.indexOf('&', offset)) >= 0) {
130 buffer.write(input.substring(offset, amp));
131 int end = input.indexOf(';', amp);
132 parse(amp, end);
133 offset = end + 1;
134 }
135 buffer.write(input.substring(offset));
136 return buffer.toString();
137 }
138 }
139
OLDNEW
« no previous file with comments | « sdk/lib/io/http_multipart_form_data.dart ('k') | sdk/lib/io/io.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698