Skip to content

Commit

Permalink
0.6/0.7 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Dec 16, 2017
1 parent 53e32d4 commit 8768b45
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 37 deletions.
11 changes: 11 additions & 0 deletions src/HTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 13 additions & 4 deletions src/cookies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.==
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/sniff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/uri.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion test/fifobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion test/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
44 changes: 22 additions & 22 deletions test/sniff.jl
Original file line number Diff line number Diff line change
@@ -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}("<HtMl><bOdY>blah blah blah</body></html>"), "text/html; charset=utf-8"),
("HTML document #2", Vector{UInt8}("<HTML></HTML>"), "text/html; charset=utf-8"),
("HTML document #3 (leading whitespace)", Vector{UInt8}(" <!DOCTYPE HTML>..."), "text/html; charset=utf-8"),
("HTML document #4 (leading CRLF)", Vector{UInt8}("\r\n<html>..."), "text/html; charset=utf-8"),
("HTML document #1", b"<HtMl><bOdY>blah blah blah</body></html>", "text/html; charset=utf-8"),
("HTML document #2", b"<HTML></HTML>", "text/html; charset=utf-8"),
("HTML document #3 (leading whitespace)", b" <!DOCTYPE HTML>...", "text/html; charset=utf-8"),
("HTML document #4 (leading CRLF)", b"\r\n<html>...", "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<?xml!"), "text/xml; charset=utf-8"),
("XML", b"\n<?xml!", "text/xml; charset=utf-8"),

# Image types.
("GIF 87a", Vector{UInt8}("GIF87a"), "image/gif"),
("GIF 89a", Vector{UInt8}("GIF89a..."), "image/gif"),
# Image types.
("GIF 87a", b"GIF87a", "image/gif"),
("GIF 89a", b"GIF89a...", "image/gif"),

# Audio types.
("MIDI audio", UInt8['M','T','h','d',0x00,0x00,0x00,0x06,0x00,0x01], "audio/midi"),
("MP3 audio/MPEG audio", UInt8['I','D','3',0x03,0x00,0x00,0x00,0x00,0x0f], "audio/mpeg"),
("WAV audio #1", UInt8['R','I','F','F','b',0xb8,0x00,0x00,'W','A','V','E','f','m','t',' ',0x12,0x00,0x00,0x00,0x06], "audio/wave"),
("WAV audio #2", UInt8['R','I','F','F',',',0x00,0x00,0x00,'W','A','V','E','f','m','t',' ',0x12,0x00,0x00,0x00,0x06], "audio/wave"),
("AIFF audio #1", UInt8['F','O','R','M',0x00,0x00,0x00,0x00,'A','I','F','F','C','O','M','M',0x00,0x00,0x00,0x12,0x00,0x01,0x00,0x00,0x57,0x55,0x00,0x10,0x40,0x0d,0xf3,0x34], "audio/aiff"),
("OGG audio", UInt8['O','g','g','S',0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0xf6,0xb4,0xfc,0x01,0x1e,0x01,0x76,0x6f,0x72], "application/ogg"),
# Audio types.
("MIDI audio", UInt8['M','T','h','d',0x00,0x00,0x00,0x06,0x00,0x01], "audio/midi"),
("MP3 audio/MPEG audio", UInt8['I','D','3',0x03,0x00,0x00,0x00,0x00,0x0f], "audio/mpeg"),
("WAV audio #1", UInt8['R','I','F','F','b',0xb8,0x00,0x00,'W','A','V','E','f','m','t',' ',0x12,0x00,0x00,0x00,0x06], "audio/wave"),
("WAV audio #2", UInt8['R','I','F','F',',',0x00,0x00,0x00,'W','A','V','E','f','m','t',' ',0x12,0x00,0x00,0x00,0x06], "audio/wave"),
("AIFF audio #1", UInt8['F','O','R','M',0x00,0x00,0x00,0x00,'A','I','F','F','C','O','M','M',0x00,0x00,0x00,0x12,0x00,0x01,0x00,0x00,0x57,0x55,0x00,0x10,0x40,0x0d,0xf3,0x34], "audio/aiff"),
("OGG audio", UInt8['O','g','g','S',0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7e,0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0xf6,0xb4,0xfc,0x01,0x1e,0x01,0x76,0x6f,0x72], "application/ogg"),

# Video types.
("MP4 video", UInt8[0x00,0x00,0x00,0x18,'f','t','y','p','m','p','4','2',0x00,0x00,0x00,0x00,'m','p','4','2','i','s','o','m','<',0x06,'t',0xbf,'m','d','a','t'], "video/mp4"),
("AVI video #1", UInt8['R','I','F','F',',','O','\n',0x00,'A','V','I',' ','L','I','S','T','À'], "video/avi"),
("AVI video #2", UInt8['R','I','F','F',',','\n',0x00,0x00,'A','V','I',' ','L','I','S','T','À'], "video/avi"),
# Video types.
("MP4 video", UInt8[0x00,0x00,0x00,0x18,'f','t','y','p','m','p','4','2',0x00,0x00,0x00,0x00,'m','p','4','2','i','s','o','m','<',0x06,'t',0xbf,'m','d','a','t'], "video/mp4"),
("AVI video #1", UInt8['R','I','F','F',',','O','\n',0x00,'A','V','I',' ','L','I','S','T','À'], "video/avi"),
("AVI video #2", UInt8['R','I','F','F',',','\n',0x00,0x00,'A','V','I',' ','L','I','S','T','À'], "video/avi"),
]

@testset "HTTP.sniff" begin
Expand Down

0 comments on commit 8768b45

Please sign in to comment.