Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using URLRequest.httpBodyStream can cause Violation of HTTP Spec #4818

Open
herzi opened this issue Sep 10, 2023 · 2 comments
Open

Using URLRequest.httpBodyStream can cause Violation of HTTP Spec #4818

herzi opened this issue Sep 10, 2023 · 2 comments
Labels

Comments

@herzi
Copy link

herzi commented Sep 10, 2023

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
@herzi herzi added the bug label Sep 10, 2023
@ktoso
Copy link
Member

ktoso commented Sep 11, 2023

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

@ktoso ktoso transferred this issue from apple/swift Sep 11, 2023
@herzi
Copy link
Author

herzi commented Sep 11, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants