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

Separate HTTP.request from Layers.request #469

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,19 @@ Hello
- There is no enforcement of a "well-defined" stack, you can insert a layer anywhere in the stack even if it logically
does not make sense
- When creating a custom layer, you need to create a `request()`, see below for an example
- Custom layers is only implemented with the "low-level" `request()` calls, not the "convenience" functions such as
- Custom layers is only implemented with the internal `HTTP.Layers.request()` function, not the "convenience" functions such as
`HTTP.get()`, `HTTP.put()`, etc.

```julia
julia> module TestRequest
import HTTP: Layer, request, Response
import HTTP: Layer, Layers, Response

abstract type TestLayer{Next <: Layer} <: Layer{Next} end
export TestLayer, request
export TestLayer

function request(::Type{TestLayer{Next}}, io::IO, req, body; kw...)::Response where Next
function Layers.request(::Type{TestLayer{Next}}, io::IO, req, body; kw...)::Response where Next
println("Insert your custom layer logic here!")
return request(Next, io, req, body; kw...)
return Layers.request(Next, io, req, body; kw...)
end
end

Expand Down Expand Up @@ -165,7 +165,7 @@ Connection: keep-alive
}
"""

julia>
julia>
```

[docs-dev-img]: https://img.shields.io/badge/docs-dev-blue.svg
Expand Down
8 changes: 4 additions & 4 deletions src/AWS4AuthRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module AWS4AuthRequest
using ..Base64
using ..Dates
using MbedTLS: digest, MD_SHA256, MD_MD5
import ..Layer, ..request, ..Headers
import ..Layers, ..Layer, ..Headers
using URIs
using ..Pairs: getkv, setkv, rmkv
import ..@debug, ..DEBUG_LEVEL

"""
request(AWS4AuthLayer, ::URI, ::Request, body) -> HTTP.Response
Layers.request(AWS4AuthLayer, ::URI, ::Request, body) -> HTTP.Response

Add a [AWS Signature Version 4](http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html)
`Authorization` header to a `Request`.
Expand All @@ -21,7 +21,7 @@ Credentials are read from environment variables `AWS_ACCESS_KEY_ID`,
abstract type AWS4AuthLayer{Next <: Layer} <: Layer{Next} end
export AWS4AuthLayer

function request(::Type{AWS4AuthLayer{Next}},
function Layers.request(::Type{AWS4AuthLayer{Next}},
url::URI, req, body; kw...) where Next

if !haskey(kw, :aws_access_key_id) &&
Expand All @@ -31,7 +31,7 @@ function request(::Type{AWS4AuthLayer{Next}},

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

return request(Next, url, req, body; kw...)
return Layers.request(Next, url, req, body; kw...)
end

# Normalize whitespace to the form required in the canonical headers.
Expand Down
10 changes: 5 additions & 5 deletions src/BasicAuthRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module BasicAuthRequest

using ..Base64

import ..Layer, ..request
import ..Layer, ..Layers
using URIs
using ..Pairs: getkv, setkv
import ..@debug, ..DEBUG_LEVEL

"""
request(BasicAuthLayer, method, ::URI, headers, body) -> HTTP.Response
Layers.request(BasicAuthLayer, method, ::URI, headers, body) -> HTTP.Response

Add `Authorization: Basic` header using credentials from url userinfo.
"""
abstract type BasicAuthLayer{Next <: Layer} <: Layer{Next} end
export BasicAuthLayer

function request(::Type{BasicAuthLayer{Next}},
method::String, url::URI, headers, body; kw...) where Next
function Layers.request(::Type{BasicAuthLayer{Next}},
method::String, url::URI, headers, body; kw...) where Next

userinfo = unescapeuri(url.userinfo)

Expand All @@ -25,7 +25,7 @@ function request(::Type{BasicAuthLayer{Next}},
setkv(headers, "Authorization", "Basic $(base64encode(userinfo))")
end

return request(Next, method, url, headers, body; kw...)
return Layers.request(Next, method, url, headers, body; kw...)
end


Expand Down
8 changes: 4 additions & 4 deletions src/CanonicalizeRequest.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
module CanonicalizeRequest

import ..Layer, ..request
import ..Layer, ..Layers
using ..Messages
using ..Strings: tocameldash

"""
request(CanonicalizeLayer, method, ::URI, headers, body) -> HTTP.Response
Layers.request(CanonicalizeLayer, method, ::URI, headers, body) -> HTTP.Response

Rewrite request and response headers in Canonical-Camel-Dash-Format.
"""
abstract type CanonicalizeLayer{Next <: Layer} <: Layer{Next} end
export CanonicalizeLayer

function request(::Type{CanonicalizeLayer{Next}},
function Layers.request(::Type{CanonicalizeLayer{Next}},
method::String, url, headers, body; kw...) where Next

headers = canonicalizeheaders(headers)

res = request(Next, method, url, headers, body; kw...)
res = Layers.request(Next, method, url, headers, body; kw...)

res.headers = canonicalizeheaders(res.headers)

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

import ..Layer, ..request
import ..Layer, ..Layers
using URIs, ..Sockets
using ..Messages
using ..IOExtras
Expand Down Expand Up @@ -47,7 +47,7 @@ function getproxy(scheme, host)
end

"""
request(ConnectionPoolLayer, ::URI, ::Request, body) -> HTTP.Response
Layers.request(ConnectionPoolLayer, ::URI, ::Request, body) -> HTTP.Response

Retrieve an `IO` connection from the [`ConnectionPool`](@ref).

Expand All @@ -60,7 +60,7 @@ See [`isioerror`](@ref).
abstract type ConnectionPoolLayer{Next <: Layer} <: Layer{Next} end
export ConnectionPoolLayer

function request(::Type{ConnectionPoolLayer{Next}}, url::URI, req, body;
function Layers.request(::Type{ConnectionPoolLayer{Next}}, url::URI, req, body;
proxy=getproxy(url.scheme, url.host),
socket_type::Type=TCPSocket,
reuse_limit::Int=ConnectionPool.nolimit, kw...) where Next
Expand Down Expand Up @@ -105,7 +105,7 @@ function request(::Type{ConnectionPoolLayer{Next}}, url::URI, req, body;
req.headers = filter(x->x.first != "Proxy-Authorization", req.headers)
end

r = request(Next, io, req, body; kw...)
r = Layers.request(Next, io, req, body; kw...)

if (io.sequence >= reuse_limit
|| (proxy !== nothing && target_url.scheme == "https"))
Expand Down
6 changes: 3 additions & 3 deletions src/ContentTypeRequest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ContentTypeDetection

import ..Layer, ..request
import ..Layer, ..Layers
using URIs
using ..Pairs: getkv, setkv
import ..sniff
Expand All @@ -12,7 +12,7 @@ import ..@debug, ..DEBUG_LEVEL
abstract type ContentTypeDetectionLayer{Next <: Layer} <: Layer{Next} end
export ContentTypeDetectionLayer

function request(::Type{ContentTypeDetectionLayer{Next}},
function Layers.request(::Type{ContentTypeDetectionLayer{Next}},
method::String, url::URI, headers, body; kw...) where Next

if (getkv(headers, "Content-Type", "") == ""
Expand All @@ -24,7 +24,7 @@ function request(::Type{ContentTypeDetectionLayer{Next}},
setkv(headers, "Content-Type", sn)
@debug 1 "setting Content-Type header to: $sn"
end
return request(Next, method, url, headers, body; kw...)
return Layers.request(Next, method, url, headers, body; kw...)
end

end # module
8 changes: 4 additions & 4 deletions src/CookieRequest.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module CookieRequest

import ..Dates
import ..Layer, ..request
import ..Layer, ..Layers
using URIs
using ..Cookies
using ..Pairs: getkv, setkv
Expand All @@ -15,15 +15,15 @@ function __init__()
end

"""
request(CookieLayer, method, ::URI, headers, body) -> HTTP.Response
Layers.request(CookieLayer, method, ::URI, headers, body) -> HTTP.Response

Add locally stored Cookies to the request headers.
Store new Cookies found in the response headers.
"""
abstract type CookieLayer{Next <: Layer} <: Layer{Next} end
export CookieLayer

function request(::Type{CookieLayer{Next}},
function Layers.request(::Type{CookieLayer{Next}},
method::String, url::URI, headers, body;
cookies::Union{Bool, Dict{<:AbstractString, <:AbstractString}}=Dict{String, String}(),
cookiejar::Dict{String, Set{Cookie}}=default_cookiejar[Threads.threadid()],
Expand All @@ -41,7 +41,7 @@ function request(::Type{CookieLayer{Next}},
setkv(headers, "Cookie", stringify(getkv(headers, "Cookie", ""), cookiestosend))
end

res = request(Next, method, url, headers, body; kw...)
res = Layers.request(Next, method, url, headers, body; kw...)

setcookies(hostcookies, url.host, res.headers)

Expand Down
10 changes: 5 additions & 5 deletions src/DebugRequest.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
module DebugRequest

import ..Layer, ..request
import ..Layer, ..Layers
using ..IOExtras

const live_mode = true

include("IODebug.jl")

"""
request(DebugLayer, ::IO, ::Request, body) -> HTTP.Response
Layers.request(DebugLayer, ::IO, ::Request, body) -> HTTP.Response

Wrap the `IO` stream in an `IODebug` stream and print Message data.
"""
abstract type DebugLayer{Next <:Layer} <: Layer{Next} end
export DebugLayer

function request(::Type{DebugLayer{Next}}, io::IO, req, body; kw...) where Next
function Layers.request(::Type{DebugLayer{Next}}, io::IO, req, body; kw...) where Next

@static if live_mode
return request(Next, IODebug(io), req, body; kw...)
return Layers.request(Next, IODebug(io), req, body; kw...)
else
iod = IODebug(io)
try
return request(Next, iod, req, body; kw...)
return Layers.request(Next, iod, req, body; kw...)
finally
show_log(stdout, iod)
end
Expand Down
8 changes: 4 additions & 4 deletions src/ExceptionRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ module ExceptionRequest

export StatusError

import ..Layer, ..request
import ..Layer, ..Layers
import ..HTTP
using ..Messages: iserror

"""
request(ExceptionLayer, ::URI, ::Request, body) -> HTTP.Response
Layers.request(ExceptionLayer, ::URI, ::Request, body) -> HTTP.Response

Throw a `StatusError` if the request returns an error response status.
"""
abstract type ExceptionLayer{Next <: Layer} <: Layer{Next} end
export ExceptionLayer

function request(::Type{ExceptionLayer{Next}}, a...; kw...) where Next
function Layers.request(::Type{ExceptionLayer{Next}}, a...; kw...) where Next

res = request(Next, a...; kw...)
res = Layers.request(Next, a...; kw...)

if iserror(res)
throw(StatusError(res.status, res.request.method, res.request.target, res))
Expand Down
4 changes: 1 addition & 3 deletions src/HTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,9 @@ function request(method, url, h=Header[], b=nobody;
end
function request(stack::Type{<:Layer}, method, url, h=Header[], b=nobody;
headers=h, body=b, query=nothing, kw...)::Response
return request(stack, string(method), request_uri(url, query), mkheaders(headers), body; kw...)
return Layers.request(stack, string(method), request_uri(url, query), mkheaders(headers), body; kw...)
end

request(::Type{Union{}}, resp::Response) = resp

request_uri(url, query) = URI(URI(url); query=query)
request_uri(url, ::Nothing) = URI(url)

Expand Down
14 changes: 7 additions & 7 deletions src/MessageRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module MessageRequest
export body_is_a_stream, body_was_streamed, setuseragent!, resource


import ..Layer, ..request
import ..Layer, ..Layers
using ..IOExtras
using URIs
using ..Messages
Expand All @@ -18,15 +18,15 @@ resource(uri::URI) = string( isempty(uri.path) ? "/" : uri.path,
!isempty(uri.query) ? "?" : "", uri.query,
!isempty(uri.fragment) ? "#" : "", uri.fragment)

struct MessageLayer{Next <: Layer} <: Layer{Next} end
export MessageLayer

"""
request(MessageLayer, method, ::URI, headers, body) -> HTTP.Response
Layers.request(MessageLayer, method, ::URI, headers, body) -> HTTP.Response

Construct a [`Request`](@ref) object and set mandatory headers.
"""
struct MessageLayer{Next <: Layer} <: Layer{Next} end
export MessageLayer

function request(::Type{MessageLayer{Next}},
function Layers.request(::Type{MessageLayer{Next}},
method::String, url::URI, headers::Headers, body;
http_version=v"1.1",
target=resource(url),
Expand Down Expand Up @@ -56,7 +56,7 @@ function request(::Type{MessageLayer{Next}},
req = Request(method, target, headers, bodybytes(body);
parent=parent, version=http_version)

return request(Next, url, req, body; iofunction=iofunction, kw...)
return Layers.request(Next, url, req, body; iofunction=iofunction, kw...)
end

const USER_AGENT = Ref{Union{String, Nothing}}("HTTP.jl/$VERSION")
Expand Down
8 changes: 4 additions & 4 deletions src/RedirectRequest.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
module RedirectRequest

import ..Layer, ..request
import ..Layer, ..Layers
using URIs
using ..Messages
using ..Pairs: setkv
import ..Header
import ..@debug, ..DEBUG_LEVEL

"""
request(RedirectLayer, method, ::URI, headers, body) -> HTTP.Response
Layers.request(RedirectLayer, method, ::URI, headers, body) -> HTTP.Response

Redirects the request in the case of 3xx response status.
"""
abstract type RedirectLayer{Next <: Layer} <: Layer{Next} end
export RedirectLayer

function request(::Type{RedirectLayer{Next}},
function Layers.request(::Type{RedirectLayer{Next}},
method::String, url::URI, headers, body;
redirect_limit=3, forwardheaders=true, kw...) where Next
count = 0
while true

res = request(Next, method, url, headers, body; reached_redirect_limit=(count == redirect_limit), kw...)
res = Layers.request(Next, method, url, headers, body; reached_redirect_limit=(count == redirect_limit), kw...)

if (count == redirect_limit
|| !isredirect(res)
Expand Down
Loading