Chromium Code Reviews| 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 http_server; | 5 part of http_server; |
| 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 14 matching lines...) Expand all Loading... | |
| 25 if (contentTransferEncoding != null) { | 25 if (contentTransferEncoding != null) { |
| 26 // TODO(ajohnsen): Support BASE64, etc. | 26 // TODO(ajohnsen): Support BASE64, etc. |
| 27 throw new HttpException("Unsupported contentTransferEncoding: " | 27 throw new HttpException("Unsupported contentTransferEncoding: " |
| 28 "${contentTransferEncoding.value}"); | 28 "${contentTransferEncoding.value}"); |
| 29 } | 29 } |
| 30 | 30 |
| 31 if (contentType == null || | 31 if (contentType == null || |
| 32 contentType.primaryType == 'text' || | 32 contentType.primaryType == 'text' || |
| 33 contentType.mimeType == 'application/json') { | 33 contentType.mimeType == 'application/json') { |
| 34 _isText = true; | 34 _isText = true; |
| 35 StringBuffer buffer = new StringBuffer(); | 35 StringBuffer buffer = new StringBuffer(); |
|
kustermann
2014/11/18 16:34:20
This StringBuffer is unused now, remove it?
Søren Gjesse
2014/11/19 07:41:05
Done.
| |
| 36 Encoding encoding; | 36 Encoding encoding; |
| 37 if (contentType != null) { | 37 if (contentType != null) { |
| 38 encoding = Encoding.getByName(contentType.charset); | 38 encoding = Encoding.getByName(contentType.charset); |
|
kustermann
2014/11/18 16:34:20
This should be only done
if (contentType != null
Søren Gjesse
2014/11/19 07:41:05
Encoding.getByName returns null if the argument is
| |
| 39 } | 39 } |
| 40 if (encoding == null) encoding = defaultEncoding; | 40 if (encoding == null) encoding = defaultEncoding; |
| 41 _stream = _stream | 41 _stream = _stream.transform(encoding.decoder); |
| 42 .transform(encoding.decoder) | |
| 43 .expand((data) { | |
| 44 buffer.write(data); | |
| 45 var out = _decodeHttpEntityString(buffer.toString()); | |
| 46 if (out != null) { | |
| 47 buffer.clear(); | |
| 48 return [out]; | |
| 49 } | |
| 50 return const []; | |
| 51 }); | |
| 52 } | 42 } |
| 53 } | 43 } |
| 54 | 44 |
| 55 bool get isText => _isText; | 45 bool get isText => _isText; |
| 56 bool get isBinary => !_isText; | 46 bool get isBinary => !_isText; |
| 57 | 47 |
| 58 static HttpMultipartFormData parse(MimeMultipart multipart, | 48 static HttpMultipartFormData parse(MimeMultipart multipart, |
| 59 Encoding defaultEncoding) { | 49 Encoding defaultEncoding) { |
| 60 var type; | 50 var type; |
| 61 var encoding; | 51 var encoding; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 95 bool cancelOnError}) { | 85 bool cancelOnError}) { |
| 96 return _stream.listen(onData, | 86 return _stream.listen(onData, |
| 97 onDone: onDone, | 87 onDone: onDone, |
| 98 onError: onError, | 88 onError: onError, |
| 99 cancelOnError: cancelOnError); | 89 cancelOnError: cancelOnError); |
| 100 } | 90 } |
| 101 | 91 |
| 102 String value(String name) { | 92 String value(String name) { |
| 103 return _mimeMultipart.headers[name]; | 93 return _mimeMultipart.headers[name]; |
| 104 } | 94 } |
| 105 | |
| 106 // Decode a string with HTTP entities. Returns null if the string ends in the | |
| 107 // middle of a http entity. | |
| 108 static String _decodeHttpEntityString(String input) { | |
| 109 int amp = input.lastIndexOf('&'); | |
| 110 if (amp < 0) return input; | |
| 111 int end = input.lastIndexOf(';'); | |
| 112 if (end < amp) return null; | |
| 113 | |
| 114 var buffer = new StringBuffer(); | |
| 115 int offset = 0; | |
| 116 | |
| 117 parse(amp, end) { | |
| 118 switch (input[amp + 1]) { | |
| 119 case '#': | |
| 120 if (input[amp + 2] == 'x') { | |
| 121 buffer.writeCharCode( | |
| 122 int.parse(input.substring(amp + 3, end), radix: 16)); | |
| 123 } else { | |
| 124 buffer.writeCharCode(int.parse(input.substring(amp + 2, end))); | |
| 125 } | |
| 126 break; | |
| 127 | |
| 128 default: | |
| 129 throw new HttpException('Unhandled HTTP entity token'); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 while ((amp = input.indexOf('&', offset)) >= 0) { | |
| 134 buffer.write(input.substring(offset, amp)); | |
| 135 int end = input.indexOf(';', amp); | |
| 136 parse(amp, end); | |
| 137 offset = end + 1; | |
| 138 } | |
| 139 buffer.write(input.substring(offset)); | |
| 140 return buffer.toString(); | |
| 141 } | |
| 142 } | 95 } |
| OLD | NEW |