Skip to content

Commit

Permalink
Do more validation of headers in mkheaders. Fixes #918.
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Aug 31, 2022
1 parent b4ac573 commit d399f51
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,14 @@ resource(uri::URI) = string( isempty(uri.path) ? "/" : uri.path,
!isempty(uri.fragment) ? "#" : "", uri.fragment)

mkheaders(h::Headers) = h
mkheaders(h)::Headers = Header[string(k) => string(v) for (k,v) in h]
function mkheaders(h)::Headers
# validation
foreach(h) do head
head isa String && throw(ArgumentError("header must be passed as key => value pair: `$head`"))
length(head) != 2 && throw(ArgumentError("invalid header key-value pair: $head"))
end
return Header[string(k) => string(v) for (k, v) in h]
end

method(r::Request) = getfield(r, :method)
target(r::Request) = getfield(r, :target)
Expand Down
9 changes: 8 additions & 1 deletion test/messages.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Test

using HTTP.Messages
import HTTP.Messages.appendheader
import HTTP.Messages: appendheader, mkheaders
import HTTP.URI
import HTTP.request
import HTTP: bytes, nbytes
Expand All @@ -17,6 +17,13 @@ using JSON
http_reads = ["GET", "HEAD", "OPTIONS"]
http_writes = ["POST", "PUT", "DELETE", "PATCH"]

@testset "Invalid headers" begin
@test_throws ArgumentError mkheaders(["hello"])
@test_throws ArgumentError mkheaders([(1,2,3,4,5)])
@test_throws ArgumentError mkheaders([1, 2])
@test_throws ArgumentError mkheaders(["hello", "world"])
end

@testset "Body Length" begin
@test nbytes(7) === nothing
@test nbytes(UInt8[1,2,3]) == 3
Expand Down

0 comments on commit d399f51

Please sign in to comment.