parse method

  1. @override
Future<List<Part>> parse(
  1. InputStream inputStream,
  2. String boundary,
  3. String charset, [
  4. int? contentLength,
])
override

Parses a multipart HTTP request body into individual Part instances.

This method reads the inputStream fully, separates it by the given boundary, decodes headers using the specified charset, and constructs a list of Part objects representing each section of the multipart data.

Parameters

  • inputStream: The raw input stream of the HTTP request body.
  • boundary: The multipart boundary string used to separate parts.
  • charset: The character set used to decode headers and text fields.
  • contentLength: The total content length in bytes of the request body. If provided and greater than 0, the parser will read exactly this many bytes instead of waiting for EOF, improving performance for streams that don't signal end-of-stream promptly.

Returns

A Future that completes with a list of Part objects extracted from the multipart body.

Behavior

  1. Reads the entire input stream into memory (beware large uploads).
  2. Searches for boundaries matching the pattern --<boundary>.
  3. Parses headers for each part (using CRLFCRLF as the separator).
  4. Extracts the part's byte content and trims trailing CRLF if present.
  5. Constructs IoPart instances using headers, data, and the configured EncodingDecoder.
  6. Stops parsing when the end boundary --<boundary>-- is encountered.

Exceptions

Example

final parts = await parse(request.getBody(), boundary, 'utf-8');
for (final part in parts) {
  print('Part name: ${part.getName()}, size: ${part.getSize()} bytes');
}

Implementation

@override
Future<List<Part>> parse(InputStream inputStream, String boundary, String charset, [int? contentLength]) async {
  final parts = await super.parse(inputStream, boundary, charset, contentLength);

  // Convert parts to disk-based parts if they exceed the threshold
  final diskParts = <Part>[];

  for (final part in parts) {
    if (part.getSize() > getFileSizeThreshold() && isFilePart(part)) {
      diskParts.add(await _createDiskFilePart(part));
    } else {
      diskParts.add(part);
    }
  }

  return diskParts;
}