diff --git a/src/HTTP.jl b/src/HTTP.jl index 99f4f45f9..012c90107 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -20,6 +20,17 @@ end using Unicode end +macro uninit(expr) + if !isdefined(Base, :uninitialized) + splice!(expr.args, 2) + end + return esc(expr) +end + +if !isdefined(Base, :pairs) + pairs(x) = x +end + if VERSION < v"0.7.0-DEV.2575" const Dates = Base.Dates else diff --git a/src/client.jl b/src/client.jl index d19a4d608..710d250fd 100644 --- a/src/client.jl +++ b/src/client.jl @@ -332,7 +332,7 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo retry >= opts.retries::Int && throw(err) return request(client, req, opts, stream, history, retry + 1, verbose) end - @log "received response" + @log "received response: $response" opts.managecookies::Bool && !isempty(response.cookies) && (@log("caching received cookies for host: " * join(map(x->x.name, response.cookies), ", ")); union!(get!(client.cookies, host, Set{Cookie}()), response.cookies)) response.history = history if opts.allowredirects::Bool && req.method != HEAD && (300 <= status(response) < 400) diff --git a/src/cookies.jl b/src/cookies.jl index 1f527d193..9d1a65835 100644 --- a/src/cookies.jl +++ b/src/cookies.jl @@ -40,6 +40,11 @@ if VERSION >= v"0.7.0-DEV.2915" using Unicode end +if !isdefined(Base, :pairs) + pairs(x) = x +end + + export Cookie import Base.== @@ -85,7 +90,7 @@ mutable struct Cookie end function Cookie(cookie::Cookie; kwargs...) - for (k, v) in kwargs + for (k, v) in pairs(kwargs) setfield!(cookie, k, convert(fieldtype(Cookie, k), v)) end return cookie @@ -187,9 +192,13 @@ function readsetcookie(host, cookie) elseif lowerattr == "domain" c.domain = val elseif lowerattr == "max-age" - secs = tryparse(Int, val) - (isnull(secs) || val[1] == '0') && continue - c.maxage = max(Base.get(secs), -1) + try + secs = parse(Int, val) + val[1] == '0' && continue + c.maxage = max(secs, -1) + catch + continue + end elseif lowerattr == "expires" try c.expires = Dates.DateTime(val, Dates.RFC1123Format) diff --git a/src/server.jl b/src/server.jl index 2b89dcb4a..449e3cb36 100644 --- a/src/server.jl +++ b/src/server.jl @@ -5,6 +5,9 @@ if VERSION < v"0.7.0-DEV.2575" else import Dates end +@static if !isdefined(Base, :Distributed) + using Distributed +end using ..HTTP, ..Handlers diff --git a/src/sniff.jl b/src/sniff.jl index ccc9d8e3b..c997b5655 100644 --- a/src/sniff.jl +++ b/src/sniff.jl @@ -283,7 +283,7 @@ const LITTLE_F = UInt8('f') const LITTLE_A = UInt8('a') const LITTLE_S = UInt8('s') const PERIOD = UInt8('.') -const REF = Vector{Ptr{UInt8}}(uninitialized, 1) +const REF = @uninit Vector{Ptr{UInt8}}(uninitialized, 1) function isjson(bytes, i=0, maxlen=min(length(bytes), MAXSNIFFLENGTH)) # ignore leading whitespace diff --git a/src/types.jl b/src/types.jl index e56b132ee..80da22676 100644 --- a/src/types.jl +++ b/src/types.jl @@ -220,7 +220,7 @@ mutable struct Response cookies::Vector{Cookie} headers::Headers body::FIFOBuffer - request::Nullable{Request} + request::Union{Void,Request} history::Vector{Response} end @@ -247,11 +247,11 @@ Response(; status::Int=200, cookies::Vector{Cookie}=Cookie[], headers::Headers=Headers(), body::FIFOBuffer=FIFOBuffer(""), - request::Nullable{Request}=Nullable{Request}(), + request::Union{Void,Request}=nothing, history::Vector{Response}=Response[]) = Response(status, Int16(1), Int16(1), cookies, headers, body, request, history) -Response(n::Integer, r::Request) = Response(; body=FIFOBuffer(n), request=Nullable(r)) +Response(n::Integer, r::Request) = Response(; body=FIFOBuffer(n), request=r) Response(s::Integer) = Response(; status=s) Response(s::Integer, msg) = Response(; status=s, body=FIFOBuffer(msg)) Response(b::Union{Vector{UInt8}, String}) = Response(; headers=defaultheaders(Response), body=FIFOBuffer(b)) @@ -302,8 +302,8 @@ end function hasmessagebody(r::Response) if 100 <= status(r) < 200 || status(r) == 204 || status(r) == 304 return false - elseif !Base.isnull(request(r)) - req = Base.get(request(r)) + elseif request(r) !== nothing + req = request(r) method(req) in (HEAD, CONNECT) && return false end return true diff --git a/src/uri.jl b/src/uri.jl index 86a23b527..74a096027 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -194,7 +194,9 @@ escape(str::AbstractString, safe::Function=issafe) = escape(bytes::Vector{UInt8}) = bytes escape(v::Number) = escape(string(v)) escape(v::Symbol) = escape(string(v)) +@static if VERSION < v"0.7.0-DEV.3017" escape(v::Nullable) = Base.isnull(v) ? "" : escape(get(v)) +end escape(key, value) = string(escape(key), "=", escape(value)) escape(key, values::Vector) = escape(key => v for v in values) diff --git a/test/client.jl b/test/client.jl index 43ec8e81b..5f7fbad60 100644 --- a/test/client.jl +++ b/test/client.jl @@ -203,7 +203,7 @@ for sch in ("http", "https") req = HTTP.Request(HTTP.GET, uri, HTTP.Headers(), HTTP.FIFOBuffer()) r = HTTP.request(req) @test HTTP.status(r) == 200 - @test !HTTP.isnull(HTTP.request(r)) + @test HTTP.request(r) !== nothing @test length(take!(r)) > 0 for c in HTTP.DEFAULT_CLIENT.httppool["httpbin.org"] diff --git a/test/fifobuffer.jl b/test/fifobuffer.jl index 3f53d0260..d3fd7c59e 100644 --- a/test/fifobuffer.jl +++ b/test/fifobuffer.jl @@ -183,7 +183,7 @@ # ensure that `read(..., ::Type{UInt8})` returns a `UInt8` # https://github.com/JuliaWeb/HTTP.jl/issues/41 f = HTTP.FIFOBuffer(5) - b = Array{UInt8}(3) + b = fill(0x00, 3) @test write(f, [0x01, 0x02, 0x03, 0x04]) == 4 @test readbytes!(f, b) == 3 @test b == [0x01, 0x02, 0x03] diff --git a/test/parser.jl b/test/parser.jl index c92f91e1a..0c1142473 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -25,7 +25,7 @@ end function Message(; name::String="", kwargs...) m = Message(name) - for (k, v) in kwargs + for (k, v) in pairs(kwargs) try setfield!(m, k, v) catch e diff --git a/test/runtests.jl b/test/runtests.jl index 40aadecd8..62fd69612 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,10 +1,19 @@ -using HTTP, Base.Test +using HTTP +@static if VERSION < v"0.7.0-DEV.2005" + using Base.Test +else + using Test +end if VERSION < v"0.7.0-DEV.2575" const Dates = Base.Dates else import Dates end +if !isdefined(Base, :pairs) + pairs(x) = x +end + @testset "HTTP" begin include("utils.jl"); diff --git a/test/sniff.jl b/test/sniff.jl index 496625ef7..b456feaf0 100644 --- a/test/sniff.jl +++ b/test/sniff.jl @@ -1,32 +1,32 @@ const test_cases = [ -("Empty", UInt8[], "text/plain; charset=utf-8"), -("Binary", UInt8[1, 2, 3], "application/octet-stream"), + ("Empty", UInt8[], "text/plain; charset=utf-8"), + ("Binary", UInt8[1, 2, 3], "application/octet-stream"), -("HTML document #1", Vector{UInt8}("blah blah blah"), "text/html; charset=utf-8"), -("HTML document #2", Vector{UInt8}(""), "text/html; charset=utf-8"), -("HTML document #3 (leading whitespace)", Vector{UInt8}(" ..."), "text/html; charset=utf-8"), -("HTML document #4 (leading CRLF)", Vector{UInt8}("\r\n..."), "text/html; charset=utf-8"), + ("HTML document #1", b"blah blah blah", "text/html; charset=utf-8"), + ("HTML document #2", b"", "text/html; charset=utf-8"), + ("HTML document #3 (leading whitespace)", b" ...", "text/html; charset=utf-8"), + ("HTML document #4 (leading CRLF)", b"\r\n...", "text/html; charset=utf-8"), -("Plain text", Vector{UInt8}("This is not HTML. It has ☃ though."), "text/plain; charset=utf-8"), + ("Plain text", b"This is not HTML. It has ☃ though.", "text/plain; charset=utf-8"), -("XML", Vector{UInt8}("\n