Skip to content

Commit

Permalink
Re-implement automatic Content-Type detection using the sniff algorit…
Browse files Browse the repository at this point in the history
…hm (#175)
  • Loading branch information
quinnj authored Jan 25, 2018
1 parent cb50d03 commit fdc264e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/ContentTypeRequest.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module ContentTypeDetection

import ..Layer, ..request
using ..URIs
using ..Pairs: getkv, setkv
import ..sniff
import ..Form
using ..Messages
import ..MessageRequest: bodylength, bodybytes
import ..@debug, ..DEBUG_LEVEL

abstract type ContentTypeDetectionLayer{Next <: Layer} <: Layer end
export ContentTypeDetectionLayer

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

if getkv(headers, "Content-Type", "") == "" &&
!isa(body, Form) && bodylength(body) != unknown_length && bodylength(body) > 0
sn = sniff(bodybytes(body))
setkv(headers, "Content-Type", sn)
@debug 1 "setting Content-Type header to: $sn"
end
return request(Next, method, url, headers, body; kw...)
end

end # module
7 changes: 5 additions & 2 deletions src/HTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include("Strings.jl")
include("URIs.jl") ;using .URIs
include("utils.jl")
include("fifobuffer.jl") ;using .FIFOBuffers
include("sniff.jl")
include("cookies.jl") ;using .Cookies
include("multipart.jl")
include("Parsers.jl") ;import .Parsers: Parser, Headers, Header,
Expand Down Expand Up @@ -439,6 +440,7 @@ include("ExceptionRequest.jl"); using .ExceptionRequest
include("RetryRequest.jl"); using .RetryRequest
include("ConnectionRequest.jl"); using .ConnectionRequest
include("StreamRequest.jl"); using .StreamRequest
include("ContentTypeRequest.jl"); using .ContentTypeDetection

"""
The `stack()` function returns the default HTTP Layer-stack type.
Expand Down Expand Up @@ -559,12 +561,14 @@ function stack(;redirect=true,
retry=true,
status_exception=true,
readtimeout=0,
detect_content_type=true,
kw...)

NoLayer = Union

(redirect ? RedirectLayer : NoLayer){
(basic_authorization ? BasicAuthLayer : NoLayer){
(detect_content_type ? ContentTypeDetectionLayer : NoLayer){
(cookies ? CookieLayer : NoLayer){
(canonicalize_headers ? CanonicalizeLayer : NoLayer){
MessageLayer{
Expand All @@ -574,12 +578,11 @@ function stack(;redirect=true,
ConnectionPoolLayer{
(readtimeout > 0 ? TimeoutLayer : NoLayer){
StreamLayer
}}}}}}}}}}
}}}}}}}}}}}
end


include("client.jl")
include("sniff.jl")
include("Handlers.jl") ;using .Handlers
include("Servers.jl") ;using .Servers; using .Servers: listen
Base.@deprecate_binding(Nitrogen, Servers, false)
Expand Down

0 comments on commit fdc264e

Please sign in to comment.