OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 | 7 |
8 class _HttpMultipartFormData extends Stream implements HttpMultipartFormData { | 8 class _HttpMultipartFormData extends Stream implements HttpMultipartFormData { |
9 final ContentType contentType; | 9 final ContentType contentType; |
10 final HeaderValue contentDisposition; | 10 final HeaderValue contentDisposition; |
(...skipping 23 matching lines...) Expand all Loading... |
34 StringBuffer buffer = new StringBuffer(); | 34 StringBuffer buffer = new StringBuffer(); |
35 Encoding encoding; | 35 Encoding encoding; |
36 if (contentType != null) { | 36 if (contentType != null) { |
37 encoding = Encoding.fromName(contentType.charset); | 37 encoding = Encoding.fromName(contentType.charset); |
38 } | 38 } |
39 if (encoding == null) encoding = Encoding.ISO_8859_1; | 39 if (encoding == null) encoding = Encoding.ISO_8859_1; |
40 _stream = _stream | 40 _stream = _stream |
41 .transform(new StringDecoder(encoding)) | 41 .transform(new StringDecoder(encoding)) |
42 .expand((data) { | 42 .expand((data) { |
43 buffer.write(data); | 43 buffer.write(data); |
44 var out = _HttpUtils.decodeHttpEntityString(buffer.toString()); | 44 var out = _decodeHttpEntityString(buffer.toString()); |
45 if (out != null) { | 45 if (out != null) { |
46 buffer = new StringBuffer(); | 46 buffer.clear(); |
47 return [out]; | 47 return [out]; |
48 } | 48 } |
49 return const []; | 49 return const []; |
50 }); | 50 }); |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 bool get isText => _isText; | 54 bool get isText => _isText; |
55 bool get isBinary => !_isText; | 55 bool get isBinary => !_isText; |
56 | 56 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 bool cancelOnError}) { | 91 bool cancelOnError}) { |
92 return _stream.listen(onData, | 92 return _stream.listen(onData, |
93 onDone: onDone, | 93 onDone: onDone, |
94 onError: onError, | 94 onError: onError, |
95 cancelOnError: cancelOnError); | 95 cancelOnError: cancelOnError); |
96 } | 96 } |
97 | 97 |
98 String value(String name) { | 98 String value(String name) { |
99 return _mimeMultipart.headers[name]; | 99 return _mimeMultipart.headers[name]; |
100 } | 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 } |
101 } | 138 } |
102 | 139 |
OLD | NEW |