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

Include port in Host header, fixes #673 #680

Merged
merged 2 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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