Skip to content

The host header should also include the port #237

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

Merged
merged 5 commits into from
Jun 15, 2020

Conversation

adam-fowler
Copy link
Member

@adam-fowler adam-fowler commented Jun 12, 2020

See https://tools.ietf.org/html/rfc7230#section-5.4

  • If port is not 80 or 443 then add to host header.
  • Fixed up tests

@@ -253,6 +253,11 @@ extension HTTPClient {
return self.scheme == "https" || self.scheme == "https+unix"
}

/// Specified port.
public var specifiedPort: Int? {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to expose this in public API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah we don't need that I guess

Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is pretty good, in some cases we should omit the port, even if the user asked for it. In particular, we should omit the port when the explicitly specified port matches the implied port (e.g. 443 for https:// and 80 for http://). This parse is explicitly specified in the WHATWG URL specification, and while the Foundation URL type doesn't obey it, we should endeavour to.

@adam-fowler
Copy link
Member Author

@Lukasa as an alternative to what is there I could remove the specifiedPort and just output a port when it isn’t 80 or 443.

@Lukasa
Copy link
Collaborator

Lukasa commented Jun 15, 2020

I think that's the right choice.

@adam-fowler
Copy link
Member Author

Tests aren't working just now, seems like there were some changes on the main branch that broke a few tests.

@@ -772,7 +772,9 @@ extension TaskHandler: ChannelDuplexHandler {
var headers = request.headers

if !request.headers.contains(name: "Host") {
headers.add(name: "Host", value: request.host)
let port = request.port
let host = (port != 80 && port != 443) ? "\(request.host):\(port)" : request.host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we only exclude 80 if we're speaking (plain text) HTTP and only exclude 443 when we're speaking HTTPS?

Say I wanted to do http://localhost:443 (note the missing s in http), then I should get localhost:443 or am I missing something? CC @Lukasa ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From RFC7230 "If the port is equal to the default port for a scheme, the normal form is to omit the port subcomponent"
I guess 443 is not the normal port, for http so it should not be submitted. Do we want to support the other way. ie https://locahost:80/?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. I think the only instances where we can omit the port numbers are

  • http://...:80
  • https://...:443

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swift format is wanting me to output it as

if port != 80 || request.scheme != "http", port != 443 || request.scheme != "https" {

instead of

if (port != 80 || request.scheme != "http") && (port != 443 || request.scheme != "https") {

Is that what you are expecting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't like swift-format messing with those things but I guess the argument is consistency ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adam-fowler I think we're discussing on the wrong line here :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant applying De Morgan to the condition so switching from

   (port != 80 || request.scheme != "http") && (port != 443 || request.scheme != "https")

to

! ((port == 80 && request.scheme == "http") || (port == 443 && request.scheme == "https"))

but it's personal preference

Copy link
Contributor

@weissi weissi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you! LGTM

Copy link
Collaborator

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@weissi weissi merged commit 785ced5 into swift-server:master Jun 15, 2020
@adam-fowler adam-fowler deleted the host-includes-port branch June 15, 2020 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants