Skip to content

Requesting a URL breaks when the content-length header contains trailing whitespace #51532

@amugofjava

Description

@amugofjava

Hi,

Issue #49305 introduced additional validation to the content-length header field to ensure it only contains valid digits; however, in practice some web servers seem to right-pad the content-length value with spaces. Attempting to fetch a URL that contains white space fails with a HttpException: Content-Length must contain only digits exception.

Could the content-length field be trimmed before applying the validation or the RegExp altered to handle this white space?

The code below shows two URLs; one fails and the other is successful. The failed URL has additional white space.

import 'dart:io';

void main() async {
  final client = HttpClient();

  final goodRequest = await client.openUrl('GET', Uri.parse('http://httpstat.us/302'))
    ..followRedirects = false;

  final badRequest = await client.openUrl('GET', Uri.parse('http://mp3s.nashownotes.com/pc20rss.xml'))
    ..followRedirects = false;

  final goodResponse = await goodRequest.close();

  print(goodResponse.headers.toString());

  final badResponse = await badRequest.close();

  /// Never gets here due to trailing spaces in content-length header
  print(badResponse.headers.toString());
}

The successful URL has no white space in the content-length field:

good_header

The failing URL has white-space padding:

bad_header

  • Dart SDK Version: 2.19.2

Metadata

Metadata

Assignees

Labels

area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-_http

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions