Skip to content

Commit

Permalink
Include port in Host header, fixes #673 (#680)
Browse files Browse the repository at this point in the history
* Include port in Host header, fixes #673.

* Set version to 0.9.5.
  • Loading branch information
fredrikekre authored Feb 23, 2021
1 parent 34a11dc commit 4f0175c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HTTP"
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
authors = ["Jacob Quinn", "Sam O'Connor", "contributors: https://github.com/JuliaWeb/HTTP.jl/graphs/contributors"]
version = "0.9.4"
version = "0.9.5"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
9 changes: 8 additions & 1 deletion src/MessageRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ function request(::Type{MessageLayer{Next}},
target=resource(url),
parent=nothing, iofunction=nothing, kw...) where Next

defaultheader!(headers, "Host" => url.host)
if isempty(url.port) ||
(url.scheme == "http" && url.port == "80") ||
(url.scheme == "https" && url.port == "443")
hostheader = url.host
else
hostheader = url.host * ":" * url.port
end
defaultheader!(headers, "Host" => hostheader)
defaultheader!(headers, "Accept" => "*/*")
if USER_AGENT[] !== nothing
defaultheader!(headers, "User-Agent" => USER_AGENT[])
Expand Down
32 changes: 32 additions & 0 deletions test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,38 @@ end
end
end

@testset "Implicit request headers" begin
server = listen(IPv4(0), 8080)
tsk = @async HTTP.listen("0.0.0.0", 8080; server=server) do http
data = Dict{String,String}(http.message.headers)
HTTP.setstatus(http, 200)
HTTP.startwrite(http)
HTTP.write(http, sprint(JSON.print, data))
end
old_user_agent = HTTP.MessageRequest.USER_AGENT[]
default_user_agent = "HTTP.jl/$VERSION"
# Default values
HTTP.setuseragent!(default_user_agent)
d = JSON.parse(IOBuffer(HTTP.get("http://localhost:8080").body))
@test d["Host"] == "localhost:8080"
@test d["Accept"] == "*/*"
@test d["User-Agent"] == default_user_agent
# Overwriting behavior
headers = ["Host" => "http.jl", "Accept" => "application/json"]
HTTP.setuseragent!("HTTP.jl test")
d = JSON.parse(IOBuffer(HTTP.get("http://localhost:8080", headers).body))
@test d["Host"] == "http.jl"
@test d["Accept"] == "application/json"
@test d["User-Agent"] == "HTTP.jl test"
# No User-Agent
HTTP.setuseragent!(nothing)
d = JSON.parse(IOBuffer(HTTP.get("http://localhost:8080").body))
@test !haskey(d, "User-Agent")

HTTP.setuseragent!(old_user_agent)
close(server)
end

import NetworkOptions, MbedTLS
@testset "NetworkOptions for host verification" begin
# Set up server with self-signed cert
Expand Down

4 comments on commit 4f0175c

@quinnj
Copy link
Member

@quinnj quinnj commented on 4f0175c Feb 23, 2021

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Error while trying to register: Register Failed
@quinnj, it looks like you are not a publicly listed member/owner in the parent organization (JuliaWeb).
If you are a member/owner, you will need to change your membership to public. See GitHub Help

@quinnj
Copy link
Member

@quinnj quinnj commented on 4f0175c Feb 23, 2021

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/30691

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.5 -m "<description of version>" 4f0175c9c815b2339a4785039fb76b72d18e1d49
git push origin v0.9.5

Please sign in to comment.