Skip to content

Commit

Permalink
Merge #1
Browse files Browse the repository at this point in the history
Merged by hand, many of these changes were already made.
  • Loading branch information
samoconnor committed Jan 13, 2018
1 parent ea97668 commit 5bc54e5
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 143 deletions.
10 changes: 7 additions & 3 deletions src/AWS4AuthRequest.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module AWS4AuthRequest

using Base64
using Dates
using Unicode
using ..Base64
using ..Dates
using ..Unicode
using MbedTLS: digest, MD_SHA256, MD_MD5
import ..Layer, ..request, ..Headers
using ..URIs
Expand All @@ -27,10 +27,12 @@ export AWS4AuthLayer
function request(::Type{AWS4AuthLayer{Next}},
uri::URI, req, body; kw...) where Next

@static if VERSION > v"0.7.0-DEV.2915"
if !haskey(kw, :aws_access_key_id) &&
!haskey(ENV, "AWS_ACCESS_KEY_ID")
kw = merge(dot_aws_credentials(), kw)
end
end

sign_aws4!(req.method, uri, req.headers, req.body; kw...)

Expand Down Expand Up @@ -117,6 +119,7 @@ function sign_aws4!(method::String,
))
end

@static if VERSION > v"0.7.0-DEV.2915"

using IniFile

Expand Down Expand Up @@ -148,5 +151,6 @@ function dot_aws_credentials()::NamedTuple
aws_secret_access_key = String(get(ini, p, "aws_secret_access_key")))
end

end

end # module AWS4AuthRequest
4 changes: 1 addition & 3 deletions src/BasicAuthRequest.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module BasicAuthRequest

if VERSION > v"0.7.0-DEV.2338"
using Base64
end
using ..Base64

import ..Layer, ..request
using ..URIs
Expand Down
5 changes: 4 additions & 1 deletion src/ConnectionPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ function Base.readavailable(t::Transaction)::ByteView
end


@static if VERSION < v"0.7.0-DEV.2915" const copyto! = copy! end

@static if !isdefined(Base, :copyto!)
const copyto! = copy!
end

function Base.readbytes!(t::Transaction, a::Vector{UInt8}, nb::Int)

Expand Down
27 changes: 6 additions & 21 deletions src/HTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import MbedTLS.SSLContext


const DEBUG_LEVEL = 0
const minimal = false

include("compat.jl")
include("debug.jl")
Expand All @@ -15,18 +14,15 @@ include("Pairs.jl")
include("Strings.jl")
include("IOExtras.jl") ;import .IOExtras.IOError
include("URIs.jl") ;using .URIs
if !minimal
include("consts.jl")
include("utils.jl")
include("fifobuffer.jl") ;using .FIFOBuffers
include("cookies.jl") ;using .Cookies
include("multipart.jl")
end
include("Parsers.jl") ;import .Parsers: Parser, Headers, Header,
ParsingError, ByteView
include("ConnectionPool.jl")
include("Messages.jl") ;using .Messages
import .Messages: header, hasheader
include("Streams.jl") ;using .Streams


Expand Down Expand Up @@ -416,22 +412,16 @@ end))
"""

abstract type Layer end
if !minimal
include("RedirectRequest.jl"); using .RedirectRequest
include("BasicAuthRequest.jl"); using .BasicAuthRequest
if VERSION > v"0.7.0-DEV.2338"
include("AWS4AuthRequest.jl"); using .AWS4AuthRequest
end
include("CookieRequest.jl"); using .CookieRequest
include("CanonicalizeRequest.jl"); using .CanonicalizeRequest
include("TimeoutRequest.jl"); using .TimeoutRequest
end
include("MessageRequest.jl"); using .MessageRequest
include("ExceptionRequest.jl"); using .ExceptionRequest
import .ExceptionRequest.StatusError
if !minimal
include("RetryRequest.jl"); using .RetryRequest
end
include("ConnectionRequest.jl"); using .ConnectionRequest
include("StreamRequest.jl"); using .StreamRequest

Expand Down Expand Up @@ -555,9 +545,7 @@ function stack(;redirect=true,
status_exception=true,
readtimeout=0,
kw...)
if minimal
MessageLayer{ExceptionLayer{ConnectionPoolLayer{StreamLayer}}}
else

NoLayer = Union

(redirect ? RedirectLayer : NoLayer){
Expand All @@ -572,19 +560,16 @@ function stack(;redirect=true,
(readtimeout > 0 ? TimeoutLayer : NoLayer){
StreamLayer
}}}}}}}}}}
end
end


if !minimal
if VERSION > v"0.7.0-DEV.2338"
include("WebSockets.jl") ;using .WebSockets
end
include("client.jl")
include("sniff.jl")
include("Handlers.jl"); using .Handlers
include("Servers.jl"); using .Servers.listen
end
include("Handlers.jl") ;using .Handlers
include("Servers.jl") ;using .Servers.listen

include("WebSockets.jl") ;using .WebSockets

include("precompile.jl")

end # module
5 changes: 0 additions & 5 deletions src/MessageRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ using ..URIs
using ..Messages
import ..Messages.bodylength
using ..Headers
using ..minimal
if !minimal
using ..Form
end


"""
Expand Down Expand Up @@ -50,9 +47,7 @@ end
bodylength(body) = unknown_length
bodylength(body::AbstractVector{UInt8}) = length(body)
bodylength(body::AbstractString) = sizeof(body)
if !minimal
bodylength(body::Form) = length(body)
end
bodylength(body::Vector{T}) where T <: AbstractString = sum(sizeof, body)
bodylength(body::Vector{T}) where T <: AbstractArray{UInt8,1} = sum(length, body)
bodylength(body::IOBuffer) = nb_available(body)
Expand Down
4 changes: 1 addition & 3 deletions src/Messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ export Message, Request, Response,
readstartline!, writestartline,
bodylength, unknown_length

if VERSION > v"0.7.0-DEV.2338"
using Unicode
end
using ..Unicode

import ..HTTP

Expand Down
7 changes: 2 additions & 5 deletions src/Servers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ if !isdefined(Base, :Nothing)
const Cvoid = Void
end

if VERSION < v"0.7.0-DEV.2575"
const Dates = Base.Dates
else
import Dates
end
import ..Dates

@static if !isdefined(Base, :Distributed)
using Distributed
end
Expand Down
4 changes: 1 addition & 3 deletions src/URIs.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module URIs

if VERSION >= v"0.7.0-DEV.2915"
using Unicode
end
using ..Unicode

import Base.==

Expand Down
12 changes: 6 additions & 6 deletions src/WebSockets.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module WebSockets

using Base64
using Unicode
using ..Base64
using ..Unicode
using MbedTLS: digest, MD_SHA1, SSLContext
import ..HTTP
using ..IOExtras
Expand Down Expand Up @@ -191,12 +191,12 @@ function wswrite(ws::WebSocket, opcode::UInt8, bytes::Vector{UInt8})
end


function mask!(out, in, l, mask=rand(UInt8, 4))
if length(out) < l
resize!(out, l)
function mask!(to, from, l, mask=rand(UInt8, 4))
if length(to) < l
resize!(to, l)
end
for i in 1:l
out[i] = in[i] mask[((i-1) % 4)+1]
to[i] = from[i] mask[((i-1) % 4)+1]
end
return mask
end
Expand Down
4 changes: 4 additions & 0 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

else # Julia v0.6

eval(:(module Base64 end))
eval(:(module Unicode end))
const Dates = Base.Dates

pairs(x) = [k => v for (k,v) in x]

macro debug(s) DEBUG_LEVEL > 0 ? :(("D- ", $(esc(s)))) : :() end
Expand All @@ -28,4 +31,5 @@ if !isdefined(Base, :Nothing)
const Cvoid = Void
end

# https://github.com/JuliaLang/julia/pull/25535
Base.String(x::SubArray{UInt8,1}) = String(Vector{UInt8}(x))
21 changes: 5 additions & 16 deletions src/cookies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,14 @@

module Cookies

if VERSION < v"0.7.0-DEV.2575"
const Dates = Base.Dates
else
import Dates
end

if VERSION >= v"0.7.0-DEV.2915"
using Unicode
end

if !isdefined(Base, :pairs)
pairs(x) = x
end


export Cookie

import ..Dates
using ..Unicode

import Base.==
import HTTP.URIs.isurlchar
import ..URIs.isurlchar
using ..pairs

"""
Cookie()
Expand Down
29 changes: 9 additions & 20 deletions src/handlers.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
module Handlers

if !isdefined(Base, :Nothing)
const Nothing = Void
const Cvoid = Void
end

function val(v)
@static if VERSION < v"0.7.0-DEV.1395"
Val{v}()
else
Val(v)
end
end

export handle, Handler, HandlerFunction, Router, register!

import ..Nothing, ..Cvoid, ..Val

using HTTP

"""
Expand Down Expand Up @@ -76,12 +65,12 @@ struct Router <: Handler
end
end

const SCHEMES = Dict{String, Val}("http" => val(:http), "https" => val(:https))
const SCHEMES = Dict{String, Val}("http" => Val(:http), "https" => Val(:https))
const METHODS = Dict{String, Val}()
for m in instances(HTTP.Method)
METHODS[string(m)] = val(Symbol(m))
METHODS[string(m)] = Val(Symbol(m))
end
const EMPTYVAL = val(())
const EMPTYVAL = Val(())

"""
HTTP.register!(r::Router, url, handler)
Expand Down Expand Up @@ -109,7 +98,7 @@ function register!(r::Router, method::String, url, handler)
# get scheme, host, split path into strings & vals
uri = url isa String ? HTTP.URI(url) : url
s = uri.scheme
sch = !isempty(s) ? typeof(get!(SCHEMES, s, val(s))) : Any
sch = !isempty(s) ? typeof(get!(SCHEMES, s, Val(s))) : Any
h = !isempty(uri.host) ? Val{Symbol(uri.host)} : Any
hand = handler isa Function ? HandlerFunction(handler) : handler
register!(r, m, sch, h, uri.path, hand)
Expand All @@ -121,7 +110,7 @@ function splitsegments(r::Router, h::Handler, segments)
if s == "*" #TODO: or variable, keep track of variable types and store in handler
T = Any
else
v = val(Symbol(s))
v = Val(Symbol(s))
r.segments[s] = v
T = typeof(v)
end
Expand All @@ -142,11 +131,11 @@ end

function handle(r::Router, req, resp)
# get the url/path of the request
m = val(Symbol(req.method))
m = Val(Symbol(req.method))
# get scheme, host, split path into strings and get Vals
uri = HTTP.URI(req.uri)
s = get(SCHEMES, uri.scheme, EMPTYVAL)
h = val(Symbol(uri.host))
h = Val(Symbol(uri.host))
p = uri.path
segments = split(p, '/'; keep=false)
# dispatch to the most specific handler, given the path
Expand Down
1 change: 1 addition & 0 deletions test/WebSockets.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using HTTP
using HTTP.Test
using HTTP.IOExtras

for s in ["ws", "wss"]
Expand Down
11 changes: 4 additions & 7 deletions test/async.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
@static if VERSION > v"0.7.0-DEV.2005"
using Test
using Base64
end
using HTTP
using HTTP.Test
using HTTP.Base64
using JSON
using MbedTLS: digest, MD_MD5, MD_SHA256

Expand Down Expand Up @@ -48,9 +46,8 @@ function dump_async_exception(e, st)
print(String(take!(buf)))
end

if VERSION > v"0.7.0-DEV.2338" &&
(haskey(ENV, "AWS_ACCESS_KEY_ID") ||
haskey(ENV, "AWS_DEFAULT_PROFILE"))
if haskey(ENV, "AWS_ACCESS_KEY_ID") ||
(VERSION > v"0.7.0-DEV.2338" && haskey(ENV, "AWS_DEFAULT_PROFILE"))
@testset "async s3 dup$dup, count$count, sz$sz, pipw$pipe, $http, $mode" for
count in [10, 100, 1000],
dup in [0, 7],
Expand Down
Loading

0 comments on commit 5bc54e5

Please sign in to comment.