swiftlang/swift-corelibs-foundation

Using `URLRequest.httpBodyStream` can cause Violation of HTTP Spec

Opened this issue · 2 comments

herzi commented

Description

When using URLRequest.httpBodyStream and specifying the Content-Length header, swift-corelibs-foundation's implementation of URLSession sends both the Content-Length and the Transfer-Encoding headers.

This seems to violate RFC 9112, Section 6.2, Content-Length:

A sender MUST NOT send a Content-Length header field in any message
that contains a Transfer-Encoding header field.

Steps to reproduce

As the reproduction of this issue is quite complicated, I created a repository with:

  1. A (swift-nio) server rejecting malformed requests as described above,
  2. An integration test to call this server via URLSession
  3. An orchestration using docker compose that can be used to reproduce this behavior using a few command line invocations (see readme.md for details).

Expected behavior

Darwin's Foundation solves this issue by trusting and sending the Content-Length header (instead of Transfer-Encoding) along with the httpBodyStream (without applying the chunked transfer encoding). swift-async-http-client seems to exhibit the same behavior.

I propose to have the current implementation of URLSession aligned with the implementations of Darwin's URLSession and swift-async-http-client.

Environment

Swift version 5.8.1 (swift-5.8.1-RELEASE)
Target: x86_64-unknown-linux-gnu
ktoso commented

Hmmm, since this is about URLSession and corelibs the bug should probably be moved over there -- I'll do that.

Do note however that rather than fixing corelibs it is likely that the correct behavior should instead be implemnted in the swift-foundation reimplementation over here: https://github.com/apple/swift-foundation

Later this year, the porting effort will continue. It will bring high quality Swift implementations of additional important Foundation API such as URL, Bundle, FileManager, FileHandle, Process, SortDescriptor, SortComparator and more.

FYI @parkera

herzi commented

Hmmm, since this is about URLSession and corelibs the bug should probably be moved over there -- I'll do that.

Thanks a lot. 🙏 I originally wanted to file the bug on swift-corelibs-foundation, but then decided to do it on swift instead because of CONTRIBUTING.md:

You can use the Foundation component in the bug reporter if you know your bug is specifically about Swift Foundation.

Should I file another bug to update the file?