| 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 |
| 8 /** |
| 9 * A Mime Multipart class representing each part parsed by |
| 10 * [MimeMultipartTransformer]. The data is streamed in as it become available. |
| 11 */ |
| 12 abstract class MimeMultipart extends Stream<List<int>> { |
| 13 Map<String, String> get headers; |
| 14 } |
| 15 |
| 16 class _MimeMultipartImpl extends MimeMultipart { |
| 17 final Map<String, String> headers; |
| 18 final Stream<List<int>> _stream; |
| 19 |
| 20 _MimeMultipartImpl(this.headers, this._stream); |
| 21 |
| 22 StreamSubscription<List<int>> listen(void onData(List<int> data), |
| 23 {void onDone(), |
| 24 void onError(error), |
| 25 bool cancelOnError}) { |
| 26 return _stream.listen(onData, |
| 27 onDone: onDone, |
| 28 onError: onError, |
| 29 cancelOnError: cancelOnError); |
| 30 } |
| 31 } |
| 32 |
| 7 /** | 33 /** |
| 8 * Parser for MIME multipart types of data as described in RFC 2046 | 34 * Parser for MIME multipart types of data as described in RFC 2046 |
| 9 * section 5.1.1. The data to parse is supplied through the [:update:] | 35 * section 5.1.1. The data is transformed into [MimeMultipart] objects, each |
| 10 * method. As the data is parsed the following callbacks are called: | 36 * of them streaming the multipart data. |
| 11 * | |
| 12 * [:partStart; | |
| 13 * [:headerReceived; | |
| 14 * [:headersComplete; | |
| 15 * [:partDataReceived; | |
| 16 * [:partEnd; | |
| 17 * [:error:] | |
| 18 */ | 37 */ |
| 38 class MimeMultipartTransformer |
| 39 implements StreamTransformer<List<int>, MimeMultipart> { |
| 40 static const int _START = 0; |
| 41 static const int _FIRST_BOUNDARY_ENDING = 111; |
| 42 static const int _FIRST_BOUNDARY_END = 112; |
| 43 static const int _BOUNDARY_ENDING = 1; |
| 44 static const int _BOUNDARY_END = 2; |
| 45 static const int _HEADER_START = 3; |
| 46 static const int _HEADER_FIELD = 4; |
| 47 static const int _HEADER_VALUE_START = 5; |
| 48 static const int _HEADER_VALUE = 6; |
| 49 static const int _HEADER_VALUE_FOLDING_OR_ENDING = 7; |
| 50 static const int _HEADER_VALUE_FOLD_OR_END = 8; |
| 51 static const int _HEADER_ENDING = 9; |
| 52 static const int _CONTENT = 10; |
| 53 static const int _LAST_BOUNDARY_DASH2 = 11; |
| 54 static const int _LAST_BOUNDARY_ENDING = 12; |
| 55 static const int _LAST_BOUNDARY_END = 13; |
| 56 static const int _DONE = 14; |
| 57 static const int _FAILURE = 15; |
| 19 | 58 |
| 20 class _MimeMultipartParser { | 59 StreamController _controller; |
| 21 const int _START = 0; | 60 StreamSubscription _subscription; |
| 22 const int _FIRST_BOUNDARY_ENDING = 111; | |
| 23 const int _FIRST_BOUNDARY_END = 112; | |
| 24 const int _BOUNDARY_ENDING = 1; | |
| 25 const int _BOUNDARY_END = 2; | |
| 26 const int _HEADER_START = 3; | |
| 27 const int _HEADER_FIELD = 4; | |
| 28 const int _HEADER_VALUE_START = 5; | |
| 29 const int _HEADER_VALUE = 6; | |
| 30 const int _HEADER_VALUE_FOLDING_OR_ENDING = 7; | |
| 31 const int _HEADER_VALUE_FOLD_OR_END = 8; | |
| 32 const int _HEADER_ENDING = 9; | |
| 33 const int _CONTENT = 10; | |
| 34 const int _LAST_BOUNDARY_DASH2 = 11; | |
| 35 const int _LAST_BOUNDARY_ENDING = 12; | |
| 36 const int _LAST_BOUNDARY_END = 13; | |
| 37 const int _DONE = 14; | |
| 38 const int _FAILURE = 15; | |
| 39 | 61 |
| 40 // Construct a new MIME multipart parser with the boundary | 62 StreamController _multipartController; |
| 41 // [boundary]. The boundary should be as specified in the content | 63 Map<String, String> _headers; |
| 42 // type parameter, that is without the -- prefix. | 64 |
| 43 _MimeMultipartParser(String boundary) { | 65 List<int> _boundary; |
| 66 int _state = _START; |
| 67 int _boundaryIndex = 0; |
| 68 |
| 69 // Current index in the data buffer. If index is negative then it |
| 70 // is the index into the artificial prefix of the boundary string. |
| 71 int _index; |
| 72 List<int> _buffer; |
| 73 |
| 74 StringBuffer _headerField = new StringBuffer(); |
| 75 StringBuffer _headerValue = new StringBuffer(); |
| 76 |
| 77 /** |
| 78 * Construct a new MIME multipart parser with the boundary |
| 79 * [boundary]. The boundary should be as specified in the content |
| 80 * type parameter, that is without the -- prefix. |
| 81 */ |
| 82 MimeMultipartTransformer(String boundary) { |
| 44 List<int> charCodes = boundary.codeUnits; | 83 List<int> charCodes = boundary.codeUnits; |
| 45 _boundary = new List<int>(4 + charCodes.length); | 84 _boundary = new Uint8List(4 + charCodes.length); |
| 46 // Set-up the matching boundary preceding it with CRLF and two | 85 // Set-up the matching boundary preceding it with CRLF and two |
| 47 // dashes. | 86 // dashes. |
| 48 _boundary[0] = _CharCode.CR; | 87 _boundary[0] = _CharCode.CR; |
| 49 _boundary[1] = _CharCode.LF; | 88 _boundary[1] = _CharCode.LF; |
| 50 _boundary[2] = _CharCode.DASH; | 89 _boundary[2] = _CharCode.DASH; |
| 51 _boundary[3] = _CharCode.DASH; | 90 _boundary[3] = _CharCode.DASH; |
| 52 _boundary.setRange(4, 4 + charCodes.length, charCodes); | 91 _boundary.setRange(4, 4 + charCodes.length, charCodes); |
| 53 _state = _START; | |
| 54 _headerField = new StringBuffer(); | |
| 55 _headerValue = new StringBuffer(); | |
| 56 } | 92 } |
| 57 | 93 |
| 58 int update(List<int> buffer, int offset, int count) { | 94 void _resumeStream() { |
| 59 // Current index in the data buffer. If index is negative then it | 95 _subscription.resume(); |
| 60 // is the index into the artificial prefix of the boundary string. | 96 } |
| 61 int index; | 97 |
| 98 void _pauseStream() { |
| 99 _subscription.pause(); |
| 100 } |
| 101 |
| 102 Stream<MimeMultipart> bind(Stream<List<int>> stream) { |
| 103 _controller = new StreamController( |
| 104 onPause: () { |
| 105 _pauseStream(); |
| 106 }, |
| 107 onResume: () { |
| 108 _resumeStream(); |
| 109 }, |
| 110 onListen: () { |
| 111 _subscription = stream.listen( |
| 112 (data) { |
| 113 assert(_buffer == null); |
| 114 _pauseStream(); |
| 115 _buffer = data; |
| 116 _index = 0; |
| 117 _parse(); |
| 118 }, |
| 119 onDone: () { |
| 120 if (_state != _DONE) { |
| 121 _controller.addError( |
| 122 new MimeParserException("Bad multipart ending")); |
| 123 } |
| 124 _controller.close(); |
| 125 }, |
| 126 onError: (error) { |
| 127 _controller.addError(error); |
| 128 }); |
| 129 }, |
| 130 onCancel: () { |
| 131 _subscription.cancel(); |
| 132 }); |
| 133 return _controller.stream; |
| 134 } |
| 135 |
| 136 void _parse() { |
| 62 // Number of boundary bytes to artificially place before the supplied data. | 137 // Number of boundary bytes to artificially place before the supplied data. |
| 63 int boundaryPrefix = 0; | 138 int boundaryPrefix = 0; |
| 64 // Position where content starts. Will be null if no known content | 139 // Position where content starts. Will be null if no known content |
| 65 // start exists. Will be negative of the content starts in the | 140 // start exists. Will be negative of the content starts in the |
| 66 // boundary prefix. Will be zero or position if the content starts | 141 // boundary prefix. Will be zero or position if the content starts |
| 67 // in the current buffer. | 142 // in the current buffer. |
| 68 int contentStartIndex; | 143 int contentStartIndex; |
| 69 | 144 |
| 70 // Function to report content data for the current part. The data | 145 // Function to report content data for the current part. The data |
| 71 // reported is from the current content start index up til the | 146 // reported is from the current content start index up til the |
| 72 // current index. As the data can be artificially prefixed with a | 147 // current index. As the data can be artificially prefixed with a |
| 73 // prefix of the boundary both the content start index and index | 148 // prefix of the boundary both the content start index and index |
| 74 // can be negative. | 149 // can be negative. |
| 75 void reportData() { | 150 void reportData() { |
| 76 if (partDataReceived == null) return; | |
| 77 | |
| 78 if (contentStartIndex < 0) { | 151 if (contentStartIndex < 0) { |
| 79 var contentLength = boundaryPrefix + index - _boundaryIndex; | 152 var contentLength = boundaryPrefix + _index - _boundaryIndex; |
| 80 if (contentLength <= boundaryPrefix) { | 153 if (contentLength <= boundaryPrefix) { |
| 81 partDataReceived( | 154 _multipartController.add( |
| 82 _boundary.sublist(0, contentLength)); | 155 _boundary.sublist(0, contentLength)); |
| 83 } else { | 156 } else { |
| 84 partDataReceived( | 157 _multipartController.add( |
| 85 _boundary.sublist(0, boundaryPrefix)); | 158 _boundary.sublist(0, boundaryPrefix)); |
| 86 partDataReceived( | 159 _multipartController.add( |
| 87 buffer.sublist(0, contentLength - boundaryPrefix)); | 160 _buffer.sublist(0, contentLength - boundaryPrefix)); |
| 88 } | 161 } |
| 89 } else { | 162 } else { |
| 90 var contentEndIndex = index - _boundaryIndex; | 163 var contentEndIndex = _index - _boundaryIndex; |
| 91 partDataReceived( | 164 _multipartController.add( |
| 92 buffer.sublist(contentStartIndex, contentEndIndex)); | 165 _buffer.sublist(contentStartIndex, contentEndIndex)); |
| 93 } | 166 } |
| 94 } | 167 } |
| 95 | 168 |
| 96 // Prepare for processing the buffer. | |
| 97 index = offset; | |
| 98 int lastIndex = offset + count; | |
| 99 if (_state == _CONTENT && _boundaryIndex == 0) { | 169 if (_state == _CONTENT && _boundaryIndex == 0) { |
| 100 contentStartIndex = 0; | 170 contentStartIndex = 0; |
| 101 } else { | 171 } else { |
| 102 contentStartIndex = null; | 172 contentStartIndex = null; |
| 103 } | 173 } |
| 104 // The data to parse might be "artificially" prefixed with a | 174 // The data to parse might be "artificially" prefixed with a |
| 105 // partial match of the boundary. | 175 // partial match of the boundary. |
| 106 boundaryPrefix = _boundaryIndex; | 176 boundaryPrefix = _boundaryIndex; |
| 107 | 177 |
| 108 while ((index < lastIndex) && _state != _FAILURE && _state != _DONE) { | 178 while ((_index < _buffer.length) && _state != _FAILURE && _state != _DONE) { |
| 179 if (_multipartController != null && _multipartController.isPaused) { |
| 180 return; |
| 181 } |
| 109 int byte; | 182 int byte; |
| 110 if (index < 0) { | 183 if (_index < 0) { |
| 111 byte = _boundary[boundaryPrefix + index]; | 184 byte = _boundary[boundaryPrefix + _index]; |
| 112 } else { | 185 } else { |
| 113 byte = buffer[index]; | 186 byte = _buffer[_index]; |
| 114 } | 187 } |
| 115 switch (_state) { | 188 switch (_state) { |
| 116 case _START: | 189 case _START: |
| 117 if (_toLowerCase(byte) == _toLowerCase(_boundary[_boundaryIndex])) { | 190 if (_toLowerCase(byte) == _toLowerCase(_boundary[_boundaryIndex])) { |
| 118 _boundaryIndex++; | 191 _boundaryIndex++; |
| 119 if (_boundaryIndex == _boundary.length) { | 192 if (_boundaryIndex == _boundary.length) { |
| 120 _state = _FIRST_BOUNDARY_ENDING; | 193 _state = _FIRST_BOUNDARY_ENDING; |
| 121 _boundaryIndex = 0; | 194 _boundaryIndex = 0; |
| 122 } | 195 } |
| 123 } else { | 196 } else { |
| 124 // Restart matching of the boundary. | 197 // Restart matching of the boundary. |
| 125 index = index - _boundaryIndex; | 198 _index = _index - _boundaryIndex; |
| 126 _boundaryIndex = 0; | 199 _boundaryIndex = 0; |
| 127 } | 200 } |
| 128 break; | 201 break; |
| 129 | 202 |
| 130 case _FIRST_BOUNDARY_ENDING: | 203 case _FIRST_BOUNDARY_ENDING: |
| 131 if (byte == _CharCode.CR) { | 204 if (byte == _CharCode.CR) { |
| 132 _state = _FIRST_BOUNDARY_END; | 205 _state = _FIRST_BOUNDARY_END; |
| 133 } else { | 206 } else { |
| 134 _expectWS(byte); | 207 _expectWS(byte); |
| 135 } | 208 } |
| 136 break; | 209 break; |
| 137 | 210 |
| 138 case _FIRST_BOUNDARY_END: | 211 case _FIRST_BOUNDARY_END: |
| 139 _expect(byte, _CharCode.LF); | 212 _expect(byte, _CharCode.LF); |
| 140 _state = _HEADER_START; | 213 _state = _HEADER_START; |
| 141 break; | 214 break; |
| 142 | 215 |
| 143 case _BOUNDARY_ENDING: | 216 case _BOUNDARY_ENDING: |
| 144 if (byte == _CharCode.CR) { | 217 if (byte == _CharCode.CR) { |
| 145 _state = _BOUNDARY_END; | 218 _state = _BOUNDARY_END; |
| 146 } else if (byte == _CharCode.DASH) { | 219 } else if (byte == _CharCode.DASH) { |
| 147 _state = _LAST_BOUNDARY_DASH2; | 220 _state = _LAST_BOUNDARY_DASH2; |
| 148 } else { | 221 } else { |
| 149 _expectWS(byte); | 222 _expectWS(byte); |
| 150 } | 223 } |
| 151 break; | 224 break; |
| 152 | 225 |
| 153 case _BOUNDARY_END: | 226 case _BOUNDARY_END: |
| 154 _expect(byte, _CharCode.LF); | 227 _expect(byte, _CharCode.LF); |
| 155 if (partEnd != null) { | 228 _multipartController.close(); |
| 156 partEnd(false); | 229 _multipartController = null; |
| 157 } | |
| 158 _state = _HEADER_START; | 230 _state = _HEADER_START; |
| 159 break; | 231 break; |
| 160 | 232 |
| 161 case _HEADER_START: | 233 case _HEADER_START: |
| 234 _headers = new Map<String, String>(); |
| 162 if (byte == _CharCode.CR) { | 235 if (byte == _CharCode.CR) { |
| 163 _state = _HEADER_ENDING; | 236 _state = _HEADER_ENDING; |
| 237 } else { |
| 238 // Start of new header field. |
| 239 _headerField.writeCharCode(_toLowerCase(byte)); |
| 240 _state = _HEADER_FIELD; |
| 241 } |
| 242 break; |
| 243 |
| 244 case _HEADER_FIELD: |
| 245 if (byte == _CharCode.COLON) { |
| 246 _state = _HEADER_VALUE_START; |
| 247 } else { |
| 248 if (!_isTokenChar(byte)) { |
| 249 throw new MimeParserException("Invalid header field name"); |
| 250 } |
| 251 _headerField.writeCharCode(_toLowerCase(byte)); |
| 252 } |
| 253 break; |
| 254 |
| 255 case _HEADER_VALUE_START: |
| 256 if (byte == _CharCode.CR) { |
| 257 _state = _HEADER_VALUE_FOLDING_OR_ENDING; |
| 258 } else if (byte != _CharCode.SP && byte != _CharCode.HT) { |
| 259 // Start of new header value. |
| 260 _headerValue.writeCharCode(byte); |
| 261 _state = _HEADER_VALUE; |
| 262 } |
| 263 break; |
| 264 |
| 265 case _HEADER_VALUE: |
| 266 if (byte == _CharCode.CR) { |
| 267 _state = _HEADER_VALUE_FOLDING_OR_ENDING; |
| 268 } else { |
| 269 _headerValue.writeCharCode(byte); |
| 270 } |
| 271 break; |
| 272 |
| 273 case _HEADER_VALUE_FOLDING_OR_ENDING: |
| 274 _expect(byte, _CharCode.LF); |
| 275 _state = _HEADER_VALUE_FOLD_OR_END; |
| 276 break; |
| 277 |
| 278 case _HEADER_VALUE_FOLD_OR_END: |
| 279 if (byte == _CharCode.SP || byte == _CharCode.HT) { |
| 280 _state = _HEADER_VALUE_START; |
| 281 } else { |
| 282 String headerField = _headerField.toString(); |
| 283 String headerValue =_headerValue.toString(); |
| 284 _headers[headerField] = headerValue; |
| 285 _headerField = new StringBuffer(); |
| 286 _headerValue = new StringBuffer(); |
| 287 if (byte == _CharCode.CR) { |
| 288 _state = _HEADER_ENDING; |
| 164 } else { | 289 } else { |
| 165 // Start of new header field. | 290 // Start of new header field. |
| 166 _headerField.writeCharCode(_toLowerCase(byte)); | 291 _headerField.writeCharCode(_toLowerCase(byte)); |
| 167 _state = _HEADER_FIELD; | 292 _state = _HEADER_FIELD; |
| 168 } | 293 } |
| 169 break; | 294 } |
| 295 break; |
| 170 | 296 |
| 171 case _HEADER_FIELD: | 297 case _HEADER_ENDING: |
| 172 if (byte == _CharCode.COLON) { | 298 _expect(byte, _CharCode.LF); |
| 173 _state = _HEADER_VALUE_START; | 299 _multipartController = new StreamController( |
| 174 } else { | 300 onPause: () { |
| 175 if (!_isTokenChar(byte)) { | 301 _pauseStream(); |
| 176 throw new MimeParserException("Invalid header field name"); | 302 }, |
| 303 onResume: () { |
| 304 _resumeStream(); |
| 305 _parse(); |
| 306 }); |
| 307 _controller.add( |
| 308 new _MimeMultipartImpl(_headers, _multipartController.stream)); |
| 309 _headers = null; |
| 310 _state = _CONTENT; |
| 311 contentStartIndex = _index + 1; |
| 312 break; |
| 313 |
| 314 case _CONTENT: |
| 315 if (byte == _boundary[_boundaryIndex]) { |
| 316 _boundaryIndex++; |
| 317 if (_boundaryIndex == _boundary.length) { |
| 318 if (contentStartIndex != null) { |
| 319 _index++; |
| 320 reportData(); |
| 321 _index--; |
| 177 } | 322 } |
| 178 _headerField.writeCharCode(_toLowerCase(byte)); | 323 _multipartController.close(); |
| 324 _boundaryIndex = 0; |
| 325 _state = _BOUNDARY_ENDING; |
| 179 } | 326 } |
| 180 break; | 327 } else { |
| 181 | 328 // Restart matching of the boundary. |
| 182 case _HEADER_VALUE_START: | 329 _index = _index - _boundaryIndex; |
| 183 if (byte == _CharCode.CR) { | 330 if (contentStartIndex == null) contentStartIndex = _index; |
| 184 _state = _HEADER_VALUE_FOLDING_OR_ENDING; | 331 _boundaryIndex = 0; |
| 185 } else if (byte != _CharCode.SP && byte != _CharCode.HT) { | 332 } |
| 186 // Start of new header value. | 333 break; |
| 187 _headerValue.writeCharCode(byte); | |
| 188 _state = _HEADER_VALUE; | |
| 189 } | |
| 190 break; | |
| 191 | |
| 192 case _HEADER_VALUE: | |
| 193 if (byte == _CharCode.CR) { | |
| 194 _state = _HEADER_VALUE_FOLDING_OR_ENDING; | |
| 195 } else { | |
| 196 _headerValue.writeCharCode(byte); | |
| 197 } | |
| 198 break; | |
| 199 | |
| 200 case _HEADER_VALUE_FOLDING_OR_ENDING: | |
| 201 _expect(byte, _CharCode.LF); | |
| 202 _state = _HEADER_VALUE_FOLD_OR_END; | |
| 203 break; | |
| 204 | |
| 205 case _HEADER_VALUE_FOLD_OR_END: | |
| 206 if (byte == _CharCode.SP || byte == _CharCode.HT) { | |
| 207 _state = _HEADER_VALUE_START; | |
| 208 } else { | |
| 209 String headerField = _headerField.toString(); | |
| 210 String headerValue =_headerValue.toString(); | |
| 211 if (headerReceived != null) { | |
| 212 headerReceived(headerField, headerValue); | |
| 213 } | |
| 214 _headerField = new StringBuffer(); | |
| 215 _headerValue = new StringBuffer(); | |
| 216 if (byte == _CharCode.CR) { | |
| 217 _state = _HEADER_ENDING; | |
| 218 } else { | |
| 219 // Start of new header field. | |
| 220 _headerField.writeCharCode(_toLowerCase(byte)); | |
| 221 _state = _HEADER_FIELD; | |
| 222 } | |
| 223 } | |
| 224 break; | |
| 225 | |
| 226 case _HEADER_ENDING: | |
| 227 _expect(byte, _CharCode.LF); | |
| 228 if (headersComplete != null) headersComplete(); | |
| 229 _state = _CONTENT; | |
| 230 contentStartIndex = index + 1; | |
| 231 break; | |
| 232 | |
| 233 case _CONTENT: | |
| 234 if (_toLowerCase(byte) == _toLowerCase(_boundary[_boundaryIndex])) { | |
| 235 _boundaryIndex++; | |
| 236 if (_boundaryIndex == _boundary.length) { | |
| 237 if (contentStartIndex != null) { | |
| 238 index++; | |
| 239 reportData(); | |
| 240 index--; | |
| 241 } | |
| 242 _boundaryIndex = 0; | |
| 243 _state = _BOUNDARY_ENDING; | |
| 244 } | |
| 245 } else { | |
| 246 // Restart matching of the boundary. | |
| 247 index = index - _boundaryIndex; | |
| 248 if (contentStartIndex == null) contentStartIndex = index; | |
| 249 _boundaryIndex = 0; | |
| 250 } | |
| 251 break; | |
| 252 | 334 |
| 253 case _LAST_BOUNDARY_DASH2: | 335 case _LAST_BOUNDARY_DASH2: |
| 254 _expect(byte, _CharCode.DASH); | 336 _expect(byte, _CharCode.DASH); |
| 255 _state = _LAST_BOUNDARY_ENDING; | 337 _state = _LAST_BOUNDARY_ENDING; |
| 256 break; | 338 break; |
| 257 | 339 |
| 258 case _LAST_BOUNDARY_ENDING: | 340 case _LAST_BOUNDARY_ENDING: |
| 259 if (byte == _CharCode.CR) { | 341 if (byte == _CharCode.CR) { |
| 260 _state = _LAST_BOUNDARY_END; | 342 _state = _LAST_BOUNDARY_END; |
| 261 } else { | 343 } else { |
| 262 _expectWS(byte); | 344 _expectWS(byte); |
| 263 } | 345 } |
| 264 break; | 346 break; |
| 265 | 347 |
| 266 case _LAST_BOUNDARY_END: | 348 case _LAST_BOUNDARY_END: |
| 267 _expect(byte, _CharCode.LF); | 349 _expect(byte, _CharCode.LF); |
| 268 if (partEnd != null) { | 350 _multipartController.close(); |
| 269 partEnd(true); | 351 _multipartController = null; |
| 270 } | |
| 271 _state = _DONE; | 352 _state = _DONE; |
| 272 break; | 353 break; |
| 273 | 354 |
| 274 default: | 355 default: |
| 275 // Should be unreachable. | 356 // Should be unreachable. |
| 276 assert(false); | 357 assert(false); |
| 277 break; | 358 break; |
| 278 } | 359 } |
| 279 | 360 |
| 280 // Move to the next byte. | 361 // Move to the next byte. |
| 281 index++; | 362 _index++; |
| 282 } | 363 } |
| 283 | 364 |
| 284 // Report any known content. | 365 // Report any known content. |
| 285 if (_state == _CONTENT && contentStartIndex != null) { | 366 if (_state == _CONTENT && contentStartIndex != null) { |
| 286 reportData(); | 367 reportData(); |
| 287 } | 368 } |
| 288 return index - offset; | 369 |
| 370 // Resume if at end. |
| 371 if (_index == _buffer.length) { |
| 372 _buffer = null; |
| 373 _index = null; |
| 374 _resumeStream(); |
| 375 } |
| 289 } | 376 } |
| 290 | 377 |
| 291 bool _isTokenChar(int byte) { | 378 bool _isTokenChar(int byte) { |
| 292 return byte > 31 && byte < 128 && _Const.SEPARATORS.indexOf(byte) == -1; | 379 return byte > 31 && byte < 128 && _Const.SEPARATORS.indexOf(byte) == -1; |
| 293 } | 380 } |
| 294 | 381 |
| 295 int _toLowerCase(int byte) { | 382 int _toLowerCase(int byte) { |
| 296 final int aCode = "A".codeUnitAt(0); | 383 final int aCode = "A".codeUnitAt(0); |
| 297 final int zCode = "Z".codeUnitAt(0); | 384 final int zCode = "Z".codeUnitAt(0); |
| 298 final int delta = "a".codeUnitAt(0) - aCode; | 385 final int delta = "a".codeUnitAt(0) - aCode; |
| 299 return (aCode <= byte && byte <= zCode) ? byte + delta : byte; | 386 return (aCode <= byte && byte <= zCode) ? byte + delta : byte; |
| 300 } | 387 } |
| 301 | 388 |
| 302 void _expect(int val1, int val2) { | 389 void _expect(int val1, int val2) { |
| 303 if (val1 != val2) { | 390 if (val1 != val2) { |
| 304 throw new MimeParserException("Failed to parse multipart mime 1"); | 391 throw new MimeParserException("Failed to parse multipart mime 1"); |
| 305 } | 392 } |
| 306 } | 393 } |
| 307 | 394 |
| 308 void _expectWS(int byte) { | 395 void _expectWS(int byte) { |
| 309 if (byte != _CharCode.SP && byte != _CharCode.HT) { | 396 if (byte != _CharCode.SP && byte != _CharCode.HT) { |
| 310 throw new MimeParserException("Failed to parse multipart mime 2"); | 397 throw new MimeParserException("Failed to parse multipart mime 2"); |
| 311 } | 398 } |
| 312 } | 399 } |
| 313 | |
| 314 List<int> _boundary; | |
| 315 int _state; | |
| 316 int _boundaryIndex = 0; | |
| 317 | |
| 318 StringBuffer _headerField; | |
| 319 StringBuffer _headerValue; | |
| 320 | |
| 321 Function partStart; | |
| 322 Function headerReceived; | |
| 323 Function headersComplete; | |
| 324 Function partDataReceived; | |
| 325 Function partEnd; | |
| 326 } | 400 } |
| 327 | 401 |
| 328 | 402 |
| 329 class MimeParserException implements Exception { | 403 class MimeParserException implements Exception { |
| 330 const MimeParserException([String this.message = ""]); | 404 const MimeParserException([String this.message = ""]); |
| 331 String toString() => "MimeParserException: $message"; | 405 String toString() => "MimeParserException: $message"; |
| 332 final String message; | 406 final String message; |
| 333 } | 407 } |
| OLD | NEW |