From d5725544c724e6f0fee4a5d20613c76231cb42e6 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 4 Nov 2017 14:52:27 +1100 Subject: [PATCH 001/182] Replace FIFOBuffer implementation with BufferStream/IOBuffer wrapper. Add mark/reset support to Form <: IO Dont call eof() in body(::IO, ...) if content length is known (eof() can block) Work around https://github.com/JuliaLang/julia/issues/24465 Use non-blocking readavailable() to read serverlog in test/server.jl Hack test/fifobuffer.jl to work with BufferStream/IOBuffer implementation. The BufferStream API does not expose the size of the buffer, so tests that passed a fixed size to FIFOBuffer(n) have been tweaked to pass with BufferStream's automatic buffer size managment behaviour. Add note re @test_broken and https://github.com/kennethreitz/httpbin/issues/340#issuecomment-330176449 --- src/HTTP.jl | 2 +- src/client.jl | 6 +- src/fifobuffer.jl | 299 +++++---------------------------------------- src/multipart.jl | 3 +- src/parser.jl | 6 +- src/precompile.jl | 42 +++---- src/server.jl | 3 +- src/types.jl | 36 ++++-- test/client.jl | 7 +- test/fifobuffer.jl | 60 ++++----- test/parser.jl | 2 +- test/server.jl | 12 +- test/types.jl | 2 +- 13 files changed, 131 insertions(+), 349 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 96c56b930..eeb8098c0 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -54,4 +54,4 @@ try HTTP.parse(HTTP.Response, "HTTP/1.1 200 OK\r\n\r\n") HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n\r\n") HTTP.get(HTTP.Client(nothing), "www.google.com") -end \ No newline at end of file +end diff --git a/src/client.jl b/src/client.jl index 7f774bbef..59a436da9 100644 --- a/src/client.jl +++ b/src/client.jl @@ -301,7 +301,7 @@ function processresponse!(client, conn, response, host, method, maintask, stream return true, StatusError(status(response), response) elseif stream && headerscomplete @log "processing the rest of response asynchronously" - response.body.task = @async processresponse!(client, conn, response, host, method, maintask, false, tm, canonicalizeheaders, false) + @async processresponse!(client, conn, response, host, method, maintask, false, tm, canonicalizeheaders, false) return true, StatusError(status(response), response) end end @@ -321,7 +321,7 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo p = port(u) conn = @retryif ClosedError 4 connectandsend(client, sch, host, ifelse(p == "", "80", p), req, opts, verbose) - response = Response(stream ? 2^24 : FIFOBuffers.DEFAULT_MAX, req) + response = Response(req) reset!(client.parser) success, err = processresponse!(client, conn, response, host, HTTP.method(req), current_task(), stream, opts.readtimeout::Float64, opts.canonicalizeheaders::Bool, verbose) if !success @@ -362,7 +362,7 @@ request(client::Client, req::Request; # build Request function request(client::Client, method, uri::URI; headers::Dict=Headers(), - body=FIFOBuffers.EMPTYBODY, + body=FIFOBuffer(), stream::Bool=false, verbose::Bool=false, args...) diff --git a/src/fifobuffer.jl b/src/fifobuffer.jl index 013e40a91..f1a22f374 100644 --- a/src/fifobuffer.jl +++ b/src/fifobuffer.jl @@ -1,294 +1,55 @@ module FIFOBuffers -import Base.== - export FIFOBuffer -""" - FIFOBuffer([max::Integer]) - FIFOBuffer(string_or_bytes_vector) - FIFOBuffer(io::IO) - -A `FIFOBuffer` is a first-in, first-out, in-memory, async-friendly IO buffer type. - -`FIFOBuffer([max])`: creates a "open" `FIFOBuffer` with a maximum size of `max`; this means that bytes can be written -up until `max` number of bytes have been written (with none being read). At this point, the `FIFOBuffer` is full -and will return 0 for all subsequent writes. If no `max` (`FIFOBuffer()`) argument is given, then a default size of `typemax(Int32)^2` is used; -this essentially allows all writes every time. Note that providing a string or byte vector argument mirrors the behavior of `Base.IOBuffer` -in that the `max` size of the `FIFOBuffer` is the length of the string/byte vector; it is also not writeable. - -Reading is supported via `readavailable(f)` and `read(f, nb)`, which returns all or `nb` bytes, respectively, starting at the earliest bytes written. -All read functions will return an empty byte vector, even if the buffer has been closed. Checking `eof` will correctly reflect when the buffer has -been closed and no more bytes will be available for reading. - -You may call `String(f::FIFOBuffer)` to view the current contents in the buffer without consuming them. - -A `FIFOBuffer` is built to be used asynchronously to allow buffered reading and writing. In particular, a `FIFOBuffer` -detects if it is being read from/written to the main task, or asynchronously, and will behave slightly differently depending on which. - -Specifically, when reading from a `FIFOBuffer`, if accessed from the main task, it will not block if there are no bytes available to read, instead returning an empty `UInt8[]`. -If being read from asynchronously, however, reading will block until additional bytes have been written. An example of this in action is: - -```julia -f = HTTP.FIFOBuffer(5) # create a FIFOBuffer that will hold at most 5 bytes, currently empty -f2 = HTTP.FIFOBuffer(5) # a 2nd buffer that we'll write to asynchronously - -# start an asynchronous writing task with the 2nd buffer -tsk = @async begin - while !eof(f) - write(f2, readavailable(f)) - end +struct FIFOBuffer{T <: Union{IOBuffer,BufferStream}} <: IO + io::T end -# now write some bytes to the first buffer -# writing triggers our async task to wake up and read the bytes we just wrote -# leaving the first buffer empty again and blocking again until more bytes have been written -write(f, [0x01, 0x02, 0x03, 0x04, 0x05]) - -# we can see that `f2` now holds the bytes we wrote to `f` -String(readavailable(f2)) - -# our async task will continue until `f` is closed -close(f) - -istaskdone(tsk) # true -``` -""" -mutable struct FIFOBuffer <: IO - len::Int64 # length of buffer in bytes - max::Int64 # the max size buffer is allowed to grow to - nb::Int64 # number of bytes available to read in buffer - f::Int64 # buffer index that should be read next, unless nb == 0, then buffer is empty - l::Int64 # buffer index that should be written to next, unless nb == len, then buffer is full - buffer::Vector{UInt8} - cond::Condition - task::Task - eof::Bool -end +FIFOBuffer() = FIFOBuffer{BufferStream}(BufferStream()) +FIFOBuffer(bytes::Vector{UInt8}) = FIFOBuffer{IOBuffer}(IOBuffer(bytes)) +FIFOBuffer(str::String) = FIFOBuffer{IOBuffer}(IOBuffer(str)) -const DEFAULT_MAX = Int64(typemax(Int32))^Int64(2) +FIFOBuffer(io::IOStream) = FIFOBuffer(read(io)) +FIFOBuffer(io::IO) = FIFOBuffer(readavailable(io)) FIFOBuffer(f::FIFOBuffer) = f -FIFOBuffer(max) = FIFOBuffer(0, max, 0, 1, 1, UInt8[], Condition(), current_task(), false) -FIFOBuffer() = FIFOBuffer(DEFAULT_MAX) -const EMPTYBODY = FIFOBuffer() +Base.String(f::FIFOBuffer{IOBuffer}) = String(f.io.data[f.io.ptr:f.io.size]) +Base.String(f::FIFOBuffer{BufferStream}) = String(FIFOBuffer(f.io.buffer)) -FIFOBuffer(str::String) = FIFOBuffer(Vector{UInt8}(str)) -function FIFOBuffer(bytes::Vector{UInt8}) - len = length(bytes) - return FIFOBuffer(len, len, len, 1, 1, bytes, Condition(), current_task(), true) +import Base.== +function ==(a::FIFOBuffer, b::FIFOBuffer) + (nb_available(a) == 0 && nb_available(b) == 0) || String(a) == String(b) end -FIFOBuffer(io::IOStream) = FIFOBuffer(read(io)) -FIFOBuffer(io::IO) = FIFOBuffer(readavailable(io)) -==(a::FIFOBuffer, b::FIFOBuffer) = String(a) == String(b) -Base.length(f::FIFOBuffer) = f.nb -Base.nb_available(f::FIFOBuffer) = f.nb -Base.wait(f::FIFOBuffer) = wait(f.cond) -Base.read(f::FIFOBuffer) = readavailable(f) -Base.flush(f::FIFOBuffer) = nothing -Base.position(f::FIFOBuffer) = f.f, f.l, f.nb -function Base.seek(f::FIFOBuffer, pos::Tuple{Int64, Int64, Int64}) - f.f = pos[1] - f.l = pos[2] - f.nb = pos[3] - return -end -Base.eof(f::FIFOBuffer) = f.eof && f.nb == 0 -Base.isopen(f::FIFOBuffer) = !f.eof -function Base.close(f::FIFOBuffer) - f.eof = true - notify(f.cond) - return -end +Base.readavailable(f::FIFOBuffer) = readavailable(f.io) -# 0 | 1 | 2 | 3 | 4 | 5 | -#---|---|---|---|---|---| -# |f/l| _ | _ | _ | _ | empty, f == l, nb = 0, can't read, can write from l to l-1, don't need to change f, l = l, nb = len -# | _ | _ |f/l| _ | _ | empty, f == l, nb = 0, can't read, can write from l:end, 1:l-1, don't need to change f, l = l, nb = len -# | _ | f | x | l | _ | where f < l, can read f:l-1, then set f = l, can write l:end, 1:f-1, then set l = f, nb = len -# | l | _ | _ | f | x | where l < f, can read f:end, 1:l-1, can write l:f-1, then set l = f -# |f/l| x | x | x | x | full l == f, nb = len, can read f:l-1, can't write -# | x | x |f/l| x | x | full l == f, nb = len, can read f:end, 1:l-1, can't write -function Base.readavailable(f::FIFOBuffer) - # no data to read - if f.nb == 0 - if current_task() == f.task || f.eof - return UInt8[] - else # async + still open: block till there's data to read - wait(f.cond) - f.nb == 0 && return UInt8[] - end - end - if f.f < f.l - @inbounds bytes = f.buffer[f.f:f.l-1] - else - # we've wrapped around - @inbounds bytes = f.buffer[f.f:end] - @inbounds append!(bytes, view(f.buffer, 1:f.l-1)) - end - f.f = f.l - f.nb = 0 - notify(f.cond) - return bytes -end +# See issue #24465: "mark/reset broken for BufferStream" +# https://github.com/JuliaLang/julia/issues/24465 +# So, need to reach down into IOBuffer for readavailable(): +Base.readavailable(f::FIFOBuffer{BufferStream}) = readavailable(f.io.buffer) -# read at most `nb` bytes -function Base.read(f::FIFOBuffer, nb::Int) - # no data to read - if f.nb == 0 - if current_task() == f.task || f.eof - return UInt8[] - else # async: block till there's data to read - wait(f.cond) - f.nb == 0 && return UInt8[] - end - end - if f.f < f.l - l = (f.l - f.f) <= nb ? (f.l - 1) : (f.f + nb - 1) - @inbounds bytes = f.buffer[f.f:l] - f.f = mod1(l + 1, f.max) - else - # we've wrapped around - if nb <= (f.len - f.f + 1) - # we can read all we need between f.f and f.len - @inbounds bytes = f.buffer[f.f:(f.f + nb - 1)] - f.f = mod1(f.f + nb, f.max) - else - @inbounds bytes = f.buffer[f.f:f.len] - l = min(f.l - 1, nb - length(bytes)) - @inbounds append!(bytes, view(f.buffer, 1:l)) - f.f = mod1(l + 1, f.max) - end - end - f.nb -= length(bytes) - notify(f.cond) - return bytes -end +Base.read(f::FIFOBuffer, a...) = read(f.io, a...) +Base.read(f::FIFOBuffer, ::Type{UInt8}) = read(f.io, UInt8) +Base.write(f::FIFOBuffer, bytes::Vector{UInt8}) = write(f.io, bytes) -function Base.read(f::FIFOBuffer, ::Type{Tuple{UInt8,Bool}}) - # no data to read - if f.nb == 0 - if current_task() == f.task || f.eof - return 0x00, false - else # async: block till there's data to read - f.eof && return 0x00, false - wait(f.cond) - f.nb == 0 && return 0x00, false - end - end - # data to read - @inbounds b = f.buffer[f.f] - f.f = mod1(f.f + 1, f.max) - f.nb -= 1 - notify(f.cond) - return b, true -end +map(eval, :(Base.$f(f::FIFOBuffer) = $f(f.io)) + for f in [:nb_available, :flush, :mark, :reset, :eof, :isopen, :close]) -function Base.read(f::FIFOBuffer, ::Type{UInt8}) - byte, valid = read(f, Tuple{UInt8,Bool}) - valid || throw(EOFError()) - return byte -end +Base.length(f::FIFOBuffer) = nb_available(f) -function Base.String(f::FIFOBuffer) - f.nb == 0 && return "" - if f.f < f.l - return String(f.buffer[f.f:f.l-1]) - else - bytes = f.buffer[f.f:end] - append!(bytes, view(f.buffer, 1:f.l-1)) - return String(bytes) +function Base.read(f::FIFOBuffer, ::Type{Tuple{UInt8,Bool}}) + if nb_available(f.io) == 0 + return 0x00, false end + return read(f.io, UInt8), true end -function Base.write(f::FIFOBuffer, b::UInt8) - # buffer full, check if we can grow it - if f.nb == f.len || f.len < f.l - if f.len < f.max - push!(f.buffer, 0x00) - f.len += 1 - else - if current_task() == f.task || f.eof - return 0 - else # async: block until there's room to write - wait(f.cond) - f.nb == f.len && return 0 - end - end - end - # write our byte - @inbounds f.buffer[f.l] = b - f.l = mod1(f.l + 1, f.max) - f.nb += 1 - notify(f.cond) - return 1 -end +Base.write(f::FIFOBuffer{BufferStream}, x::UInt8) = write(f.io, [x]) -function Base.write(f::FIFOBuffer, bytes::Vector{UInt8}, i, j) - len = j - i + 1 - if f.nb == f.len || f.len < f.l - # buffer full, check if we can grow it - if f.len < f.max - append!(f.buffer, zeros(UInt8, min(len, f.max - f.len))) - f.len = length(f.buffer) - else - if current_task() == f.task || f.eof - return 0 - else # async: block until there's room to write - wait(f.cond) - f.nb == f.len && return 0 - end - end - end - if f.f <= f.l - # non-wraparound - avail = f.len - f.l + 1 - if len > avail - # need to wrap around, and check if there's enough room to write full bytes - # write `avail` # of bytes to end of buffer - unsafe_copy!(f.buffer, f.l, bytes, i, avail) - if len - avail < f.f - # there's enough room to write the rest of bytes - unsafe_copy!(f.buffer, 1, bytes, avail + 1, len - avail) - f.l = len - avail + 1 - else - # not able to write all of bytes - unsafe_copy!(f.buffer, 1, bytes, avail + 1, f.f - 1) - f.l = f.f - f.nb += avail + f.f - 1 - notify(f.cond) - return avail + f.f - 1 - end - else - # there's enough room to write bytes through the end of the buffer - unsafe_copy!(f.buffer, f.l, bytes, i, len) - f.l = mod1(f.l + len, f.max) - end - else - # already in wrap-around state - if len > mod1(f.f - f.l, f.max) - # not able to write all of bytes - nb = f.f - f.l - unsafe_copy!(f.buffer, f.l, bytes, i, nb) - f.l = f.f - f.nb += nb - notify(f.cond) - return nb - else - # there's enough room to write bytes - unsafe_copy!(f.buffer, f.l, bytes, i, len) - f.l = mod1(f.l + len, f.max) - end - end - f.nb += len - notify(f.cond) - return len -end +Base.wait_readnb(f::FIFOBuffer{BufferStream}, nb::Int) = Base.wait_readnb(f.io, nb) -Base.write(f::FIFOBuffer, bytes::Vector{UInt8}) = write(f, bytes, 1, length(bytes)) -Base.write(f::FIFOBuffer, str::String) = write(f, Vector{UInt8}(str)) -end # module \ No newline at end of file +end # module diff --git a/src/multipart.jl b/src/multipart.jl index 1a88f2894..ca8cfd585 100644 --- a/src/multipart.jl +++ b/src/multipart.jl @@ -12,6 +12,7 @@ mutable struct Form <: IO data::Vector{IO} index::Int boundary::String + mark::Int end Form(f::Form) = f @@ -74,7 +75,7 @@ function Form(d::Dict) end seekstart(io) push!(data, io) - return Form(data, 1, boundary) + return Form(data, 1, boundary, 0) end function writemultipartheader(io::IOBuffer, i::IOStream) diff --git a/src/parser.jl b/src/parser.jl index 55cac9aca..c9f0625eb 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -110,15 +110,15 @@ function onbody(r, maintask, bytes, i, j) @debug(PARSING_DEBUG, String(bytes[i:j])) len = j - i + 1 #TODO: avoid copying the bytes here? can we somehow write the bytes to a FIFOBuffer more efficiently? - nb = write(r.body, bytes, i, j) + nb = write(r.body, bytes[i:j]) if nb < len # didn't write all available bytes if current_task() == maintask # main request function hasn't returned yet, so not safe to wait r.body.max += len - nb - write(r.body, bytes, i + nb, j) + write(r.body, bytes[i + nb:j]) else while nb < len - nb += write(r.body, bytes, i + nb, j) + nb += write(r.body, bytes[i + nb:j]) end end end diff --git a/src/precompile.jl b/src/precompile.jl index 165b2c3e9..5773c585d 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -19,20 +19,20 @@ function _precompile_() @assert precompile(HTTP.onheadervalue, (HTTP.Parser, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.Cookies.parsecookievalue, (String, Bool,)) @assert precompile(HTTP.processresponse!, (HTTP.Client, HTTP.Connection{Base.TCPSocket}, HTTP.Response, String, HTTP.Method, Task, Bool, Float64, Bool, Bool)) - @assert precompile(HTTP.Request, (HTTP.Method, HTTP.URIs.URI, Base.Dict{String, String}, HTTP.FIFOBuffers.FIFOBuffer,)) +# @assert precompile(HTTP.Request, (HTTP.Method, HTTP.URIs.URI, Base.Dict{String, String}, HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.Cookies.isCookieDomainName, (String,)) @assert precompile(HTTP.getbytes, (Base.TCPSocket, Float64)) - @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, Array{UInt8, 1}, Int64, Int64,)) +# @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.URIs.port, (HTTP.URIs.URI,)) @assert precompile(HTTP.read, (HTTP.Form,)) @assert precompile(HTTP.get, (HTTP.Nitrogen.ServerOptions, Symbol, Int64,)) @assert precompile(HTTP.ignorewhitespace, (String, Int64, Int64,)) @assert precompile(HTTP.Response, ()) @assert precompile(HTTP.Cookies.string, (String, Array{HTTP.Cookies.Cookie, 1}, Bool,)) - @assert precompile(HTTP.FIFOBuffers.read, (HTTP.FIFOBuffers.FIFOBuffer, Int64,)) +# @assert precompile(HTTP.FIFOBuffers.read, (HTTP.FIFOBuffers.FIFOBuffer, Int64,)) @assert precompile(HTTP.processresponse!, (HTTP.Client, HTTP.Connection{MbedTLS.SSLContext}, HTTP.Response, String, HTTP.Method, Task, Bool, Float64, Bool, Bool)) @assert precompile(HTTP.Form, (Base.Dict{String, String},)) - @assert precompile(HTTP.FIFOBuffers.String, (HTTP.FIFOBuffers.FIFOBuffer,)) + #@assert precompile(HTTP.FIFOBuffers.String, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.update!, (HTTP.RequestOptions, HTTP.RequestOptions,)) @assert precompile(HTTP.restofstring, (String, Int64, Int64,)) @assert precompile(HTTP.ismatch, (Type{HTTP.MP4Sig}, Array{UInt8, 1}, Int64,)) @@ -55,13 +55,13 @@ function _precompile_() @assert precompile(HTTP.sniff, (String,)) @assert precompile(HTTP.restofstring, (Array{UInt8, 1}, UInt64, Int64,)) @assert precompile(HTTP.stalebytes!, (Base.TCPSocket,)) - @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, UInt8,)) +# @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, UInt8,)) @assert precompile(HTTP.eof, (HTTP.Form,)) @assert precompile(HTTP.Cookies.readsetcookie, (String, String,)) @assert precompile(HTTP.Response, (Int64, HTTP.Request,)) @assert precompile(HTTP.URIs.http_parser_parse_url, (Array{UInt8, 1}, Int64, Int64, Bool,)) @assert precompile(HTTP.Response, (String,)) - @assert precompile(HTTP.FIFOBuffers.read, (HTTP.FIFOBuffers.FIFOBuffer, Type{Tuple{UInt8, Bool}},)) +# @assert precompile(HTTP.FIFOBuffers.read, (HTTP.FIFOBuffers.FIFOBuffer, Type{Tuple{UInt8, Bool}},)) @assert precompile(HTTP.Response, (Int64, Base.Dict{String, String}, String,)) @assert precompile(HTTP.ismatch, (HTTP.Masked, Array{UInt8, 1}, Int64,)) @assert precompile(HTTP.Cookies.sanitizeCookieValue, (String,)) @@ -78,17 +78,17 @@ function _precompile_() @assert precompile(HTTP.restofstring, (Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.dead!, (HTTP.Connection{Base.TCPSocket},)) @assert precompile(HTTP.addcookies!, (HTTP.Client, String, HTTP.Request, Bool,)) - @assert precompile(HTTP.FIFOBuffers.length, (HTTP.FIFOBuffers.FIFOBuffer,)) +# @assert precompile(HTTP.FIFOBuffers.length, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.Cookies.domainandtype, (String, String,)) @assert precompile(HTTP.sniff, (Array{UInt8, 1},)) @assert precompile(HTTP.mark, (HTTP.Multipart{Base.IOStream},)) @assert precompile(HTTP.seek, (HTTP.Form, Int64,)) @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Request, Array{UInt8, 1}, Int64, Int64, Bool, String, Base.RefValue{String}, Bool)) - @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, String,)) +# @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, String,)) @assert precompile(HTTP.URIs.escape, (String, String,)) @assert precompile(HTTP.dead!, (HTTP.Connection{MbedTLS.SSLContext},)) @assert precompile(HTTP.URIs.isvalid, (HTTP.URIs.URI,)) - @assert precompile(HTTP.FIFOBuffers.read, (HTTP.FIFOBuffers.FIFOBuffer, Type{UInt8},)) +# @assert precompile(HTTP.FIFOBuffers.read, (HTTP.FIFOBuffers.FIFOBuffer, Type{UInt8},)) @assert precompile(HTTP.getconnections, (Type{HTTP.http}, HTTP.Client, String,)) @assert precompile(HTTP.Cookies.validCookieDomain, (String,)) @assert precompile(HTTP.headers, (HTTP.Response,)) @@ -98,7 +98,7 @@ function _precompile_() @assert precompile(HTTP.body, (HTTP.Response,)) @assert precompile(HTTP.http_should_keep_alive, (HTTP.Parser, HTTP.Request,)) @assert precompile(HTTP.length, (HTTP.Form,)) - @assert precompile(HTTP.FIFOBuffers.position, (HTTP.FIFOBuffers.FIFOBuffer,)) +# @assert precompile(HTTP.FIFOBuffers.position, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.URIs.escape, (String,)) @assert precompile(HTTP.busy!, (HTTP.Connection{Base.TCPSocket},)) @assert precompile(HTTP.connect, (HTTP.Client, HTTP.http, String, String, HTTP.RequestOptions, Bool,)) @@ -120,45 +120,45 @@ function _precompile_() @assert precompile(HTTP.haskey, (Type{HTTP.http}, HTTP.Client, String,)) @assert precompile(HTTP.parse!, (HTTP.Response, HTTP.Parser, Array{UInt8, 1},)) @assert precompile(HTTP.reset, (HTTP.Multipart{Base.IOStream},)) - @assert precompile(HTTP.FIFOBuffers.seek, (HTTP.FIFOBuffers.FIFOBuffer, Tuple{Int64, Int64, Int64},)) - @assert precompile(HTTP.FIFOBuffers.wait, (HTTP.FIFOBuffers.FIFOBuffer,)) +# @assert precompile(HTTP.FIFOBuffers.seek, (HTTP.FIFOBuffers.FIFOBuffer, Tuple{Int64, Int64, Int64},)) +# @assert precompile(HTTP.FIFOBuffers.wait, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.initTLS!, (Type{HTTP.https}, String, HTTP.RequestOptions, Base.TCPSocket,)) @assert precompile(HTTP.stalebytes!, (MbedTLS.SSLContext,)) @assert precompile(HTTP.sniff, (Base.IOStream,)) @assert precompile(HTTP.request, (HTTP.Client, HTTP.Request, HTTP.RequestOptions, Bool, Array{HTTP.Response, 1}, Int, Bool,)) @assert precompile(HTTP.read, (HTTP.Multipart{Base.IOStream}, Int64,)) @assert precompile(HTTP.isjson, (Array{UInt8, 1},)) - @assert precompile(HTTP.FIFOBuffers.close, (HTTP.FIFOBuffers.FIFOBuffer,)) +# @assert precompile(HTTP.FIFOBuffers.close, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.headers, (HTTP.Request,)) @assert precompile(HTTP.busy!, (HTTP.Connection{MbedTLS.SSLContext},)) @assert precompile(HTTP.seek, (HTTP.Form, Tuple{Int64, Int64, Int64},)) - @assert precompile(HTTP.FIFOBuffers.eof, (HTTP.FIFOBuffers.FIFOBuffer,)) +# @assert precompile(HTTP.FIFOBuffers.eof, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.contenttype, (HTTP.Masked,)) @assert precompile(HTTP.get, (HTTP.RequestOptions, Symbol, MbedTLS.SSLConfig,)) @assert precompile(HTTP.contenttype, (HTTP.Exact,)) - @assert precompile(HTTP.FIFOBuffers.FIFOBuffer, (HTTP.FIFOBuffers.FIFOBuffer,)) +# @assert precompile(HTTP.FIFOBuffers.FIFOBuffer, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.haskey, (Type{HTTP.https}, HTTP.Client, String,)) - @assert precompile(HTTP.FIFOBuffers.readavailable, (HTTP.FIFOBuffers.FIFOBuffer,)) +# @assert precompile(HTTP.FIFOBuffers.readavailable, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.string, (HTTP.Response, HTTP.Nitrogen.ServerOptions,)) @assert precompile(HTTP.string, (HTTP.Response, HTTP.RequestOptions,)) @assert precompile(HTTP.string, (HTTP.Request, HTTP.RequestOptions,)) @assert precompile(HTTP.Request, (String,)) @assert precompile(HTTP.ismatch, (Type{HTTP.JSONSig}, Array{UInt8, 1}, Int64,)) @assert precompile(HTTP.hasmessagebody, (HTTP.Request,)) - @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, Array{UInt8, 1},)) +# @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, Array{UInt8, 1},)) @assert precompile(HTTP.readavailable, (HTTP.Form,)) @assert precompile(HTTP.get, (String,)) @assert precompile(HTTP.URL, (String,)) @assert precompile(HTTP.request, (HTTP.Client, HTTP.Method, HTTP.URI,)) @assert precompile(HTTP.RequestOptions, ()) - @assert precompile(HTTP.Request, (HTTP.Method, HTTP.URI, Dict{String, String}, HTTP.FIFOBuffer)) +# @assert precompile(HTTP.Request, (HTTP.Method, HTTP.URI, Dict{String, String}, HTTP.FIFOBuffer)) @assert precompile(HTTP.request, (HTTP.Request,)) @assert precompile(HTTP.request, (HTTP.Client, HTTP.Request)) @assert precompile(HTTP.request, (HTTP.Client, HTTP.Request, HTTP.RequestOptions, Bool, Vector{HTTP.Response}, Int, Bool)) @static if VERSION < v"0.7-DEV" @assert precompile(HTTP.Client, (Base.AbstractIOBuffer{Array{UInt8, 1}}, HTTP.RequestOptions,)) @assert precompile(HTTP.URIs.printuri, (Base.AbstractIOBuffer{Array{UInt8, 1}}, String, String, String, String, String, String, String,)) - @assert precompile(HTTP.FIFOBuffers.FIFOBuffer, (Base.AbstractIOBuffer{Array{UInt8, 1}},)) +# @assert precompile(HTTP.FIFOBuffers.FIFOBuffer, (Base.AbstractIOBuffer{Array{UInt8, 1}},)) @assert precompile(HTTP.startline, (Base.AbstractIOBuffer{Array{UInt8, 1}}, HTTP.Response,)) @assert precompile(HTTP.writemultipartheader, (Base.AbstractIOBuffer{Array{UInt8, 1}}, HTTP.Multipart{Base.IOStream},)) @assert precompile(HTTP.print, (Base.AbstractIOBuffer{Array{UInt8, 1}}, HTTP.Method,)) @@ -178,7 +178,7 @@ function _precompile_() else @assert precompile(HTTP.Client, (Base.GenericIOBuffer{Array{UInt8, 1}}, HTTP.RequestOptions,)) @assert precompile(HTTP.URIs.printuri, (Base.GenericIOBuffer{Array{UInt8, 1}}, String, String, String, String, String, String, String,)) - @assert precompile(HTTP.FIFOBuffers.FIFOBuffer, (Base.GenericIOBuffer{Array{UInt8, 1}},)) +# @assert precompile(HTTP.FIFOBuffers.FIFOBuffer, (Base.GenericIOBuffer{Array{UInt8, 1}},)) @assert precompile(HTTP.startline, (Base.GenericIOBuffer{Array{UInt8, 1}}, HTTP.Response,)) @assert precompile(HTTP.writemultipartheader, (Base.GenericIOBuffer{Array{UInt8, 1}}, HTTP.Multipart{Base.IOStream},)) @assert precompile(HTTP.print, (Base.GenericIOBuffer{Array{UInt8, 1}}, HTTP.Method,)) @@ -197,4 +197,4 @@ function _precompile_() @assert precompile(HTTP.startline, (Base.GenericIOBuffer{Array{UInt8, 1}}, HTTP.Request,)) end end -_precompile_() \ No newline at end of file +_precompile_() diff --git a/src/server.jl b/src/server.jl index b7a1d4bbb..ffccddc0a 100644 --- a/src/server.jl +++ b/src/server.jl @@ -93,7 +93,6 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, HTTP.@log "processing on connection i=$i..." try tsk = @async begin - request.body.task = current_task() while isopen(tcp) update!(rl, server.options.ratelimit) if rl.allowance > rate @@ -338,4 +337,4 @@ serve(; host::IPAddr=IPv4(127,0,0,1), args...) = serve(host, port, handler, logger; cert=cert, key=key, verbose=verbose, args...) -end # module \ No newline at end of file +end # module diff --git a/src/types.jl b/src/types.jl index 7abcc7f4b..87ff57fbd 100644 --- a/src/types.jl +++ b/src/types.jl @@ -175,7 +175,7 @@ Request(method, uri, h=Headers(), body=""; options::RequestOptions=RequestOption isa(uri, String) ? URI(uri; isconnect=(method == "CONNECT" || method == CONNECT)) : uri, h, body; options=options, logger=logger, verbose=verbose) -Request(; method::Method=GET, major::Integer=Int16(1), minor::Integer=Int16(1), uri=URI(""), headers=Headers(), body=FIFOBuffer("")) = +Request(; method::Method=GET, major::Integer=Int16(1), minor::Integer=Int16(1), uri=URI(""), headers=Headers(), body=FIFOBuffer()) = Request(method, major, minor, uri, headers, body) ==(a::Request,b::Request) = (a.method == b.method) && @@ -246,12 +246,12 @@ end Response(; status::Int=200, cookies::Vector{Cookie}=Cookie[], headers::Headers=Headers(), - body::FIFOBuffer=FIFOBuffer(""), + body::FIFOBuffer=FIFOBuffer(), request::Nullable{Request}=Nullable{Request}(), 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(r::Request) = Response(; body=FIFOBuffer(), request=Nullable(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)) @@ -310,20 +310,32 @@ function hasmessagebody(r::Response) end hasmessagebody(r::Request) = length(r.body) > 0 && !(r.method in (GET, HEAD, CONNECT)) +function bodylength(r::Union{Request, Response}) + + if haskey(r.headers, "Content-Length") + return Base.parse(Int, r.headers["Content-Length"]) + else + return length(r.body) + end +end + + function body(io::IO, r::Union{Request, Response}, opts) if !hasmessagebody(r) write(io, "$CRLF") return end chksz = get(opts, :chunksize, 0) - pos = position(r.body) - @sync begin - @async begin + + mark(r.body) +# @sync begin +# @async begin chunked = false bytes = UInt8[] - while !eof(r.body) - bytes = chksz == 0 ? read(r.body) : read(r.body, chksz) - eof(r.body) && !chunked && break + blength = bodylength(r) + while length(bytes) < blength && !eof(r.body) + bytes = chksz == 0 ? readavailable(r.body) : read(r.body, chksz) + (length(bytes) == blength || eof(r.body)) && !chunked && break if !chunked write(io, "Transfer-Encoding: chunked$CRLF$CRLF") end @@ -339,9 +351,9 @@ function body(io::IO, r::Union{Request, Response}, opts) write(io, "Content-Length: $(dec(length(bytes)))$CRLF$CRLF") write(io, bytes) end - end - end - seek(r.body, pos) +# end +# end + reset(r.body) return end diff --git a/test/client.jl b/test/client.jl index 43ec8e81b..dd7932bb4 100644 --- a/test/client.jl +++ b/test/client.jl @@ -88,6 +88,11 @@ for sch in ("http", "https") @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f)) == 200 # chunksize + # + # FIXME + # Currently httpbin.org responds with 411 status and “Length Required” + # message to any POST/PUT requests that are sent using chunked encoding + # See https://github.com/kennethreitz/httpbin/issues/340#issuecomment-330176449 println("client transfer-encoding chunked") @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body="hey", chunksize=2)) == 200 @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'], chunksize=2)) == 200 @@ -152,7 +157,7 @@ for sch in ("http", "https") f = HTTP.FIFOBuffer() write(f, "hey") t = @async HTTP.post("$sch://httpbin.org/post"; body=f) - wait(f) # wait for the async call to write it's first data + Base.wait_readnb(f, 1) # wait for the async call to write it's first data write(f, " there ") # as we write to f, it triggers another chunk to be sent in our async request write(f, "sailor") close(f) # setting eof on f causes the async request to send a final chunk and return the response diff --git a/test/fifobuffer.jl b/test/fifobuffer.jl index 3f53d0260..168ccc177 100644 --- a/test/fifobuffer.jl +++ b/test/fifobuffer.jl @@ -1,23 +1,23 @@ @testset "FIFOBuffer" begin - f = HTTP.FIFOBuffer(0) + f = HTTP.FIFOBuffer() @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) - f = HTTP.FIFOBuffer(1) + f = HTTP.FIFOBuffer() @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) @test write(f, 0x01) == 1 - @test write(f, 0x02) == 0 + @test write(f, 0x02) == 1 @test read(f, Tuple{UInt8,Bool}) == (0x01, true) - @test read(f, Tuple{UInt8,Bool}) == (0x00, false) + @test read(f, Tuple{UInt8,Bool}) == (0x02, true) @test isempty(readavailable(f)) - @test write(f, UInt8[0x01, 0x02]) == 1 - @test all(readavailable(f) .== UInt8[0x01]) + @test write(f, UInt8[0x01, 0x02]) == 2 + @test all(readavailable(f) .== UInt8[0x01, 0x02]) - f = HTTP.FIFOBuffer(5) + f = HTTP.FIFOBuffer() @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) @@ -57,8 +57,8 @@ write(f, 0x03) write(f, 0x04) write(f, 0x05) - write(f, 0x06) == 0 - @test all(readavailable(f) .== UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) + write(f, 0x06) + @test all(readavailable(f) .== UInt8[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) @@ -97,8 +97,8 @@ @test isempty(readavailable(f)) # overflow - write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) == 5 - @test all(readavailable(f) .== UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) + @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) == 6 + @test all(readavailable(f) .== UInt8[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) @@ -125,73 +125,75 @@ end tsk2 = @async begin for i = 1:N - @test all(readavailable(f) .== UInt8[0x4a, 0x61, 0x63, 0x6f, 0x62]) + @test all(read(f, 5) .== UInt8[0x4a, 0x61, 0x63, 0x6f, 0x62]) end end end # buffer growing - f = HTTP.FIFOBuffer(10) + f = HTTP.FIFOBuffer() @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 @test write(f, UInt8[0x06, 0x07, 0x08, 0x09, 0x0a]) == 5 @test all(readavailable(f) .== 0x01:0x0a) # read - f = HTTP.FIFOBuffer(5) + f = HTTP.FIFOBuffer() @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 @test all(read(f, 5) .== 0x01:0x05) @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 - @test all(read(f, 6) .== 0x01:0x05) + @test all(read(f, 5) .== 0x01:0x05) @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 @test isempty(read(f, 0)) @test all(read(f, 2) .== 0x01:0x02) @test write(f, 0x01) == 1 - @test all(read(f, 5) .== UInt8[0x03, 0x04, 0x05, 0x01]) + @test all(read(f, 4) .== UInt8[0x03, 0x04, 0x05, 0x01]) @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 @test all(read(f, 2) .== 0x01:0x02) r = read(f, 3) @test all(r .== 0x03:0x05) + f2 = HTTP.FIFOBuffer(f) @test f == f2 - f = HTTP.FIFOBuffer(5) - @test isempty(read(f, 1)) + f = HTTP.FIFOBuffer() + @test isempty(read(f, 0)) t = @async read(f, 1) write(f, 0x01) @test wait(t) == [0x01] @test write(f, [0x01, 0x02, 0x03, 0x04, 0x05]) == 5 - @test write(f, [0x01, 0x02]) == 0 + @test write(f, [0x01, 0x02]) == 2 - @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05] + @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x02] # ensure we're in a wrap-around state - f = HTTP.FIFOBuffer(5) + f = HTTP.FIFOBuffer() @test write(f, [0x01, 0x02, 0x03]) == 3 @test readavailable(f) == [0x01, 0x02, 0x03] @test write(f, [0x01, 0x02, 0x03, 0x04]) == 4 - @test f.f > f.l +# @test f.f > f.l @test write(f, [0x05]) == 1 @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05] @test write(f, [0x01, 0x02, 0x03, 0x04]) == 4 - @test write(f, [0x05, 0x06]) == 1 - @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05] + @test write(f, [0x05, 0x06]) == 2 + @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] # ensure that `read(..., ::Type{UInt8})` returns a `UInt8` # https://github.com/JuliaWeb/HTTP.jl/issues/41 - f = HTTP.FIFOBuffer(5) + f = HTTP.FIFOBuffer() b = Array{UInt8}(3) @test write(f, [0x01, 0x02, 0x03, 0x04]) == 4 + close(f) @test readbytes!(f, b) == 3 @test b == [0x01, 0x02, 0x03] @test read(f, UInt8) == 0x04 @test_throws EOFError read(f, UInt8) # ensure we return eof == false if there are still bytes to be read - f = HTTP.FIFOBuffer(5) + f = HTTP.FIFOBuffer() write(f, [0x01, 0x02, 0x03, 0x04]) close(f) @async begin @@ -200,9 +202,11 @@ # Issue #45 # Ensure that we don't encounter an EOF when reading before data is written - f = HTTP.FIFOBuffer(5) + f = HTTP.FIFOBuffer() bytes = [0x01, 0x02, 0x03, 0x04] - @test !eof(f) + @async begin + @test !eof(f) + end @sync begin @async begin bytes_read = UInt8[] diff --git a/test/parser.jl b/test/parser.jl index b5ad4221d..1ba2cf833 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1761,4 +1761,4 @@ const responses = Message[ r = HTTP.parse(HTTP.Request, "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\nAccept: */*\r\n\r\nHELLO") @test String(readavailable(HTTP.body(r))) == "" end -end # @testset HTTP.parse \ No newline at end of file +end # @testset HTTP.parse diff --git a/test/server.jl b/test/server.jl index 491a00836..097afebfd 100644 --- a/test/server.jl +++ b/test/server.jl @@ -20,14 +20,14 @@ r = HTTP.get("http://127.0.0.1:8081/"; readtimeout=30) @test HTTP.status(r) == 200 @test String(take!(r)) == "" -print(String(read(serverlog))) +print(String(readavailable(serverlog))) # invalid HTTP sleep(2.0) tcp = connect(ip"127.0.0.1", 8081) write(tcp, "GET / HTP/1.1\r\n\r\n") sleep(2.0) -log = String(read(serverlog)) +log = String(readavailable(serverlog)) print(log) @test contains(log, "invalid HTTP version") @@ -38,7 +38,7 @@ tcp = connect(ip"127.0.0.1", 8081) write(tcp, "BADMETHOD / HTTP/1.1\r\n\r\n") sleep(2.0) -log = String(read(serverlog)) +log = String(readavailable(serverlog)) print(log) @test contains(log, "invalid HTTP method") @@ -49,7 +49,7 @@ tcp = connect(ip"127.0.0.1", 8081) write(tcp, "POST / HTTP/1.1\r\nContent-Length: 15\r\nExpect: 100-continue\r\n\r\n") sleep(2.0) -log = String(read(serverlog)) +log = String(readavailable(serverlog)) @test contains(log, "sending 100 Continue response to get request body") client = String(readavailable(tcp)) @@ -57,7 +57,7 @@ client = String(readavailable(tcp)) write(tcp, "Body of Request") sleep(2.0) -log = String(read(serverlog)) +log = String(readavailable(serverlog)) client = String(readavailable(tcp)) print(client) @@ -113,4 +113,4 @@ r = HTTP.request(HTTP.Request(major=1, minor=0, uri=HTTP.URI("http://127.0.0.1:8 # other bad requests -end # @testset \ No newline at end of file +end # @testset diff --git a/test/types.jl b/test/types.jl index e3e34a765..7ccb383d0 100644 --- a/test/types.jl +++ b/test/types.jl @@ -23,4 +23,4 @@ showcompact(io, HTTP.Response(200)) showcompact(io, HTTP.Request()) @test String(take!(io)) == "Request(\"\", 0 headers, 0 bytes in body)" -end \ No newline at end of file +end From 9f01226794e063e9f6c871e50dea09e6d2e5817b Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 22 Nov 2017 10:57:50 +1100 Subject: [PATCH 002/182] Rename canonicalize!(::String) -> tocameldash!(::String) Move header canonicalization out of parser. --- src/client.jl | 11 +++++++---- src/parser.jl | 28 +++++++++++----------------- src/precompile.jl | 17 +++++++++-------- src/utils.jl | 6 ++++-- test/parser.jl | 6 +++--- test/utils.jl | 36 ++++++++++++++++++------------------ 6 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/client.jl b/src/client.jl index cae412c95..975393f05 100644 --- a/src/client.jl +++ b/src/client.jl @@ -281,13 +281,13 @@ function getbytes(socket, tm) end end -function processresponse!(client, conn, response, host, method, maintask, stream, tm, canonicalizeheaders, verbose) +function processresponse!(client, conn, response, host, method, maintask, stream, tm, verbose) logger = client.logger while true buffer, err = getbytes(conn.socket, tm) @log "received bytes from the wire, processing" # EH: throws a couple of "shouldn't get here" errors; probably not much we can do - errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(response, conn.parser, buffer; host=host, method=method, maintask=maintask, canonicalizeheaders=canonicalizeheaders) + errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(response, conn.parser, buffer; host=host, method=method, maintask=maintask) @log "parsed bytes received from wire" if length(buffer) == 0 && !isopen(conn.socket) && !messagecomplete @log "socket closed before full response received" @@ -306,7 +306,7 @@ function processresponse!(client, conn, response, host, method, maintask, stream return true, StatusError(status(response), response) elseif stream && headerscomplete @log "processing the rest of response asynchronously" - response.body.task = @async processresponse!(client, conn, response, host, method, maintask, false, tm, canonicalizeheaders, false) + response.body.task = @async processresponse!(client, conn, response, host, method, maintask, false, tm, false) return true, StatusError(status(response), response) end end @@ -328,12 +328,15 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo response = Response(stream ? 2^24 : FIFOBuffers.DEFAULT_MAX, req) reset!(conn.parser) - success, err = processresponse!(client, conn, response, host, HTTP.method(req), current_task(), stream, opts.readtimeout::Float64, opts.canonicalizeheaders::Bool, verbose) + success, err = processresponse!(client, conn, response, host, HTTP.method(req), current_task(), stream, opts.readtimeout::Float64, verbose) if !success retry >= opts.retries::Int && throw(err) return request(client, req, opts, stream, history, retry + 1, verbose) end @log "received response" + if opts.canonicalizeheaders::Bool + response.headers = canonicalizeheaders(response.headers) + end 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/parser.jl b/src/parser.jl index 55cac9aca..7d5c94d56 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -77,14 +77,10 @@ function onheadervalue(p::Parser, bytes, i, j) append!(p.valuebuffer, view(bytes, i:j)) return end -function onheadervalue(p, r, bytes, i, j, issetcookie, host, KEY, canonicalizeheaders) +function onheadervalue(p, r, bytes, i, j, issetcookie, host, KEY) @debug(PARSING_DEBUG, "onheadervalue2") append!(p.valuebuffer, view(bytes, i:j)) - if canonicalizeheaders - key = canonicalize!(unsafe_string(pointer(p.fieldbuffer), length(p.fieldbuffer))) - else - key = unsafe_string(pointer(p.fieldbuffer), length(p.fieldbuffer)) - end + key = unsafe_string(pointer(p.fieldbuffer), length(p.fieldbuffer)) val = unsafe_string(pointer(p.valuebuffer), length(p.valuebuffer)) if key == "" # the header value was parsed in two parts, @@ -144,12 +140,11 @@ function parse(T::Type{<:Union{Request, Response}}, str; extra::Ref{String}=Ref{String}(), lenient::Bool=true, maxuri::Int64=DEFAULT_MAX_URI, maxheader::Int64=DEFAULT_MAX_HEADER, maxbody::Int64=DEFAULT_MAX_BODY, - maintask::Task=current_task(), - canonicalizeheaders::Bool=true) + maintask::Task=current_task()) r = T(body=FIFOBuffer()) reset!(DEFAULT_PARSER) err, headerscomplete, messagecomplete, upgrade = parse!(r, DEFAULT_PARSER, Vector{UInt8}(str); - lenient=lenient, maxuri=maxuri, maxheader=maxheader, maxbody=maxbody, maintask=maintask, canonicalizeheaders=canonicalizeheaders) + lenient=lenient, maxuri=maxuri, maxheader=maxheader, maxbody=maxbody, maintask=maintask) err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) extra[] = upgrade return r @@ -157,18 +152,17 @@ end const start_state = s_start_req_or_res const DEFAULT_MAX_HEADER = Int64(80 * 1024) -const DEFAULT_MAX_URI = Int64(8000) +const DEFAULT_MAX_URI = Int64(8 * 1024) const DEFAULT_MAX_BODY = Int64(2)^32 # 4Gib const DEFAULT_PARSER = Parser() function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); lenient::Bool=true, host::String="", method::Method=GET, maxuri::Int64=DEFAULT_MAX_URI, maxheader::Int64=DEFAULT_MAX_HEADER, - maxbody::Int64=DEFAULT_MAX_BODY, maintask::Task=current_task(), - canonicalizeheaders::Bool=true)::Tuple{ParsingErrorCode, Bool, Bool, String} - return parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, maxbody, maintask, canonicalizeheaders) + maxbody::Int64=DEFAULT_MAX_BODY, maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, String} + return parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, maxbody, maintask) end -function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, maxbody, maintask, canonicalizeheaders) +function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, maxbody, maintask) strict = !lenient p_state = parser.state status_mark = url_mark = header_field_mark = header_field_end_mark = header_value_mark = body_mark = 0 @@ -810,7 +804,7 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, parser.header_state = h parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 1") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY, canonicalizeheaders) + onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY) header_value_mark = 0 break elseif ch == LF @@ -819,7 +813,7 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, parser.header_state = h parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 2") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY, canonicalizeheaders) + onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY) header_value_mark = 0 @goto reexecute elseif !lenient && !isheaderchar(ch) @@ -1024,7 +1018,7 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, p_state = s_header_field_start parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 3") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY, canonicalizeheaders) + onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY) header_value_mark = 0 @goto reexecute end diff --git a/src/precompile.jl b/src/precompile.jl index 165b2c3e9..2f1e75030 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -5,20 +5,21 @@ function _precompile_() @assert precompile(HTTP.Cookies.pathmatch, (HTTP.Cookies.Cookie, String,)) @assert precompile(HTTP.onheaderfield, (HTTP.Parser, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.isjson, (Array{UInt8, 1}, UInt64, Int64,)) - @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Response, Array{UInt8, 1}, Int64, Int64, Bool, String, Base.RefValue{String}, Bool)) + @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Response, Array{UInt8, 1}, Int64, Int64, Bool, String, Base.RefValue{String})) @assert precompile(HTTP.isjson, (Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.onurl, (HTTP.Response, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.Response, (Int64, String,)) @assert precompile(HTTP.URIs.getindex, (Array{UInt8, 1}, HTTP.URIs.Offset,)) @assert precompile(HTTP.iscompressed, (Array{UInt8, 1},)) @assert precompile(HTTP.Cookies.readcookies, (Base.Dict{String, String}, String,)) - @assert precompile(HTTP.canonicalize!, (String,)) + @assert precompile(HTTP.tocameldash!, (String,)) + @assert precompile(HTTP.canonicalizeheaders, (Dict{String,String},)) @assert precompile(HTTP.URIs.http_parse_host_char, (HTTP.URIs.http_host_state, Char,)) @assert precompile(HTTP.Form, (Base.Dict{String, Any},)) @assert precompile(HTTP.Cookies.hasdotsuffix, (String, String,)) @assert precompile(HTTP.onheadervalue, (HTTP.Parser, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.Cookies.parsecookievalue, (String, Bool,)) - @assert precompile(HTTP.processresponse!, (HTTP.Client, HTTP.Connection{Base.TCPSocket}, HTTP.Response, String, HTTP.Method, Task, Bool, Float64, Bool, Bool)) + @assert precompile(HTTP.processresponse!, (HTTP.Client, HTTP.Connection{Base.TCPSocket}, HTTP.Response, String, HTTP.Method, Task, Bool, Float64, Bool)) @assert precompile(HTTP.Request, (HTTP.Method, HTTP.URIs.URI, Base.Dict{String, String}, HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.Cookies.isCookieDomainName, (String,)) @assert precompile(HTTP.getbytes, (Base.TCPSocket, Float64)) @@ -30,7 +31,7 @@ function _precompile_() @assert precompile(HTTP.Response, ()) @assert precompile(HTTP.Cookies.string, (String, Array{HTTP.Cookies.Cookie, 1}, Bool,)) @assert precompile(HTTP.FIFOBuffers.read, (HTTP.FIFOBuffers.FIFOBuffer, Int64,)) - @assert precompile(HTTP.processresponse!, (HTTP.Client, HTTP.Connection{MbedTLS.SSLContext}, HTTP.Response, String, HTTP.Method, Task, Bool, Float64, Bool, Bool)) + @assert precompile(HTTP.processresponse!, (HTTP.Client, HTTP.Connection{MbedTLS.SSLContext}, HTTP.Response, String, HTTP.Method, Task, Bool, Float64, Bool)) @assert precompile(HTTP.Form, (Base.Dict{String, String},)) @assert precompile(HTTP.FIFOBuffers.String, (HTTP.FIFOBuffers.FIFOBuffer,)) @assert precompile(HTTP.update!, (HTTP.RequestOptions, HTTP.RequestOptions,)) @@ -83,7 +84,7 @@ function _precompile_() @assert precompile(HTTP.sniff, (Array{UInt8, 1},)) @assert precompile(HTTP.mark, (HTTP.Multipart{Base.IOStream},)) @assert precompile(HTTP.seek, (HTTP.Form, Int64,)) - @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Request, Array{UInt8, 1}, Int64, Int64, Bool, String, Base.RefValue{String}, Bool)) + @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Request, Array{UInt8, 1}, Int64, Int64, Bool, String, Base.RefValue{String})) @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, String,)) @assert precompile(HTTP.URIs.escape, (String, String,)) @assert precompile(HTTP.dead!, (HTTP.Connection{MbedTLS.SSLContext},)) @@ -104,8 +105,8 @@ function _precompile_() @assert precompile(HTTP.connect, (HTTP.Client, HTTP.http, String, String, HTTP.RequestOptions, Bool,)) @assert precompile(HTTP.string, (HTTP.Request,)) @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1},)) - @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Int64, Int64, Int64, Task, Bool)) - @assert precompile(HTTP.parse!, (HTTP.Response, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Int64, Int64, Int64, Task, Bool)) + @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Int64, Int64, Int64, Task)) + @assert precompile(HTTP.parse!, (HTTP.Response, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Int64, Int64, Int64, Task)) @assert precompile(HTTP.onbody, (HTTP.Request, Task, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.take!, (HTTP.Response,)) @assert precompile(HTTP.Response, (String,)) @@ -197,4 +198,4 @@ function _precompile_() @assert precompile(HTTP.startline, (Base.GenericIOBuffer{Array{UInt8, 1}}, HTTP.Request,)) end end -_precompile_() \ No newline at end of file +_precompile_() diff --git a/src/utils.jl b/src/utils.jl index 8432d0d0f..2fb5b875a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -161,8 +161,8 @@ macro strictcheck(cond) end # ensure the first character and subsequent characters that follow a '-' are uppercase -function canonicalize!(s::String) - toUpper = UInt8('A') - UInt8('a') +function tocameldash!(s::String) + const toUpper = UInt8('A') - UInt8('a') bytes = Vector{UInt8}(s) upper = true for i = 1:length(bytes) @@ -177,6 +177,8 @@ function canonicalize!(s::String) return s end +canonicalizeheaders{T}(h::T) = T(tocameldash!(k) => v for (k,v) in h) + iso8859_1_to_utf8(str::String) = iso8859_1_to_utf8(Vector{UInt8}(str)) function iso8859_1_to_utf8(bytes::Vector{UInt8}) io = IOBuffer() diff --git a/test/parser.jl b/test/parser.jl index b5ad4221d..bba0bd49d 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1365,7 +1365,7 @@ const responses = Message[ @test HTTP.port(HTTP.uri(r)) in (req.port, "80", "443") @test string(HTTP.uri(r)) == req.request_url @test length(HTTP.headers(r)) == req.num_headers - @test HTTP.headers(r) == req.headers + @test HTTP.canonicalizeheaders(HTTP.headers(r)) == req.headers @test String(readavailable(HTTP.body(r))) == req.body @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER, r) == req.should_keep_alive @test upgrade[] == req.upgrade @@ -1537,7 +1537,7 @@ const responses = Message[ @test HTTP.status(r) == resp.status_code @test HTTP.statustext(r) == resp.response_status @test length(HTTP.headers(r)) == resp.num_headers - @test HTTP.headers(r) == resp.headers + @test HTTP.canonicalizeheaders(HTTP.headers(r)) == resp.headers @test String(readavailable(HTTP.body(r))) == resp.body @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER, r) == resp.should_keep_alive end @@ -1761,4 +1761,4 @@ const responses = Message[ r = HTTP.parse(HTTP.Request, "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\nAccept: */*\r\n\r\nHELLO") @test String(readavailable(HTTP.body(r))) == "" end -end # @testset HTTP.parse \ No newline at end of file +end # @testset HTTP.parse diff --git a/test/utils.jl b/test/utils.jl index 25d56ebfc..848890ce2 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -26,23 +26,23 @@ end @test HTTP.ishex('1') @test !HTTP.ishex(']') -@test HTTP.canonicalize!("accept") == "Accept" -@test HTTP.canonicalize!("Accept") == "Accept" -@test HTTP.canonicalize!("eXcept-this") == "Except-This" -@test HTTP.canonicalize!("exCept-This") == "Except-This" -@test HTTP.canonicalize!("not-valid") == "Not-Valid" -@test HTTP.canonicalize!("♇") == "♇" -@test HTTP.canonicalize!("bλ-a") == "Bλ-A" -@test HTTP.canonicalize!("not fixable") == "Not fixable" -@test HTTP.canonicalize!("aaaaaaaaaaaaa") == "Aaaaaaaaaaaaa" -@test HTTP.canonicalize!("conTENT-Length") == "Content-Length" -@test HTTP.canonicalize!("Sec-WebSocket-Key2") == "Sec-Websocket-Key2" -@test HTTP.canonicalize!("User-agent") == "User-Agent" -@test HTTP.canonicalize!("Proxy-authorization") == "Proxy-Authorization" -@test HTTP.canonicalize!("HOST") == "Host" -@test HTTP.canonicalize!("ST") == "St" -@test HTTP.canonicalize!("X-\$PrototypeBI-Version") == "X-\$prototypebi-Version" -@test HTTP.canonicalize!("DCLK_imp") == "Dclk_imp" +@test HTTP.tocameldash!("accept") == "Accept" +@test HTTP.tocameldash!("Accept") == "Accept" +@test HTTP.tocameldash!("eXcept-this") == "Except-This" +@test HTTP.tocameldash!("exCept-This") == "Except-This" +@test HTTP.tocameldash!("not-valid") == "Not-Valid" +@test HTTP.tocameldash!("♇") == "♇" +@test HTTP.tocameldash!("bλ-a") == "Bλ-A" +@test HTTP.tocameldash!("not fixable") == "Not fixable" +@test HTTP.tocameldash!("aaaaaaaaaaaaa") == "Aaaaaaaaaaaaa" +@test HTTP.tocameldash!("conTENT-Length") == "Content-Length" +@test HTTP.tocameldash!("Sec-WebSocket-Key2") == "Sec-Websocket-Key2" +@test HTTP.tocameldash!("User-agent") == "User-Agent" +@test HTTP.tocameldash!("Proxy-authorization") == "Proxy-Authorization" +@test HTTP.tocameldash!("HOST") == "Host" +@test HTTP.tocameldash!("ST") == "St" +@test HTTP.tocameldash!("X-\$PrototypeBI-Version") == "X-\$prototypebi-Version" +@test HTTP.tocameldash!("DCLK_imp") == "Dclk_imp" for (bytes, utf8) in ( @@ -60,4 +60,4 @@ end # using StringEncodings # println(encode("ÄÆä", "ISO-8859-15")) -end \ No newline at end of file +end From 8d8841e49fc21ca9ba12139280c331142f4c70a4 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 22 Nov 2017 11:20:26 +1100 Subject: [PATCH 003/182] Remove URI, header and body size limits from parser. --- src/parser.jl | 27 +++++---------------------- src/precompile.jl | 4 ++-- src/server.jl | 13 ++----------- test/parser.jl | 8 +------- 4 files changed, 10 insertions(+), 42 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 7d5c94d56..538b99d4f 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -48,10 +48,7 @@ function reset!(p::Parser) end macro nread(n) - return esc(quote - parser.nread += UInt32($n) - @errorif(parser.nread > maxheader, HPE_HEADER_OVERFLOW) - end) + return esc(:(parser.nread += UInt32($n))) end onmessagebegin(r) = @debug(PARSING_DEBUG, "onmessagebegin") @@ -132,37 +129,30 @@ full request or response (but may include more than one). Supported keyword argu * `extra`: a `Ref{String}` that will be used to store any extra bytes beyond a full request or response * `lenient`: whether the request/response parsing should allow additional characters - * `maxuri`: the maximum allowed size of a uri in a request - * `maxheader`: the maximum allowed size of headers - * `maxbody`: the maximum allowed size of a request or response body """ function parse(T::Type{<:Union{Request, Response}}, str; extra::Ref{String}=Ref{String}(), lenient::Bool=true, - maxuri::Int64=DEFAULT_MAX_URI, maxheader::Int64=DEFAULT_MAX_HEADER, - maxbody::Int64=DEFAULT_MAX_BODY, maintask::Task=current_task()) r = T(body=FIFOBuffer()) reset!(DEFAULT_PARSER) err, headerscomplete, messagecomplete, upgrade = parse!(r, DEFAULT_PARSER, Vector{UInt8}(str); - lenient=lenient, maxuri=maxuri, maxheader=maxheader, maxbody=maxbody, maintask=maintask) + lenient=lenient, maintask=maintask) err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) extra[] = upgrade return r end const start_state = s_start_req_or_res -const DEFAULT_MAX_HEADER = Int64(80 * 1024) const DEFAULT_MAX_URI = Int64(8 * 1024) const DEFAULT_MAX_BODY = Int64(2)^32 # 4Gib const DEFAULT_PARSER = Parser() function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); lenient::Bool=true, host::String="", method::Method=GET, - maxuri::Int64=DEFAULT_MAX_URI, maxheader::Int64=DEFAULT_MAX_HEADER, - maxbody::Int64=DEFAULT_MAX_BODY, maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, String} - return parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, maxbody, maintask) + maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, String} + return parse!(r, parser, bytes, len, lenient, host, method, maintask) end -function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, maxbody, maintask) +function parse!(r, parser, bytes, len, lenient, host, method, maintask) strict = !lenient p_state = parser.state status_mark = url_mark = header_field_mark = header_field_end_mark = header_value_mark = body_mark = 0 @@ -501,7 +491,6 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, if ch == ' ' p_state = s_req_http_start parser.state = p_state - p - url_mark > maxuri && @err(HPE_URI_OVERFLOW) onurl(r, bytes, url_mark, p-1) url_mark = 0 elseif ch in (CR, LF) @@ -509,7 +498,6 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, r.minor = Int16(9) p_state = ifelse(ch == CR, s_req_line_almost_done, s_header_field_start) parser.state = p_state - p - url_mark > maxuri && @err(HPE_URI_OVERFLOW) onurl(r, bytes, url_mark, p-1) url_mark = 0 else @@ -825,7 +813,6 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, if h == h_general @debug(PARSING_DEBUG, parser.header_state) limit = len - p - limit = min(limit, maxheader) ptr = pointer(bytes, p) @debug(PARSING_DEBUG, Base.escape_string(string('\'', Char(bytes[p]), '\''))) p_cr = ccall(:memchr, Ptr{Void}, (Ptr{Void}, Cint, Csize_t), ptr, CR, limit) @@ -870,8 +857,6 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, if div(ULLONG_MAX - 10, 10) < t parser.header_state = h @err(HPE_INVALID_CONTENT_LENGTH) - elseif t > maxbody - @err(HPE_BODY_OVERFLOW) end parser.content_length = t end @@ -1122,7 +1107,6 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, elseif p_state == s_body_identity @debug(PARSING_DEBUG, ParsingStateCode(p_state)) to_read = UInt64(min(parser.content_length, len - p + 1)) - to_read > maxbody && @err(HPE_BODY_OVERFLOW) assert(parser.content_length != 0 && parser.content_length != ULLONG_MAX) #= The difference between advancing content_length and p is because @@ -1294,7 +1278,6 @@ function parse!(r, parser, bytes, len, lenient, host, method, maxuri, maxheader, @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, p) header_value_mark > 0 && onheadervalue(parser, bytes, header_value_mark, min(len, p)) - url_mark > 0 && (min(len, p) - url_mark > maxuri) && @err(HPE_URI_OVERFLOW) url_mark > 0 && onurl(r, bytes, url_mark, min(len, p)) @debug(PARSING_DEBUG, "this onbody 3") body_mark > 0 && onbody(r, maintask, bytes, body_mark, min(len, p - 1)) diff --git a/src/precompile.jl b/src/precompile.jl index 2f1e75030..b01ba0214 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -105,8 +105,8 @@ function _precompile_() @assert precompile(HTTP.connect, (HTTP.Client, HTTP.http, String, String, HTTP.RequestOptions, Bool,)) @assert precompile(HTTP.string, (HTTP.Request,)) @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1},)) - @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Int64, Int64, Int64, Task)) - @assert precompile(HTTP.parse!, (HTTP.Response, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Int64, Int64, Int64, Task)) + @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Task)) + @assert precompile(HTTP.parse!, (HTTP.Response, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Task)) @assert precompile(HTTP.onbody, (HTTP.Request, Task, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.take!, (HTTP.Response,)) @assert precompile(HTTP.Response, (String,)) diff --git a/src/server.jl b/src/server.jl index b7a1d4bbb..b98c9c114 100644 --- a/src/server.jl +++ b/src/server.jl @@ -34,9 +34,6 @@ mutable struct ServerOptions tlsconfig::HTTP.TLS.SSLConfig readtimeout::Float64 ratelimit::Rational{Int} - maxuri::Int64 - maxheader::Int64 - maxbody::Int64 support100continue::Bool chunksize::Union{Void, Int} logbody::Bool @@ -45,13 +42,10 @@ end ServerOptions(; tlsconfig::HTTP.TLS.SSLConfig=HTTP.TLS.SSLConfig(true), readtimeout::Float64=180.0, ratelimit::Rational{Int64}=Int64(5)//Int64(1), - maxuri::Int64=HTTP.DEFAULT_MAX_URI, - maxheader::Int64=HTTP.DEFAULT_MAX_HEADER, - maxbody::Int64=HTTP.DEFAULT_MAX_BODY, support100continue::Bool=true, chunksize::Union{Void, Int}=nothing, logbody::Bool=true) = - ServerOptions(tlsconfig, readtimeout, ratelimit, maxbody, maxuri, maxheader, support100continue, chunksize, logbody) + ServerOptions(tlsconfig, readtimeout, ratelimit, support100continue, chunksize, logbody) """ Server(handler, logger::IO=STDOUT; kwargs...) @@ -69,9 +63,6 @@ Supported keyword arguments include: * `tlsconfig`: pass in an already-constructed `HTTP.TLS.SSLConfig` instance * `readtimeout`: how long a client connection will be left open without receiving any bytes * `ratelimit`: a `Rational{Int}` of the form `5//1` indicating how many `messages//second` should be allowed per client IP address; requests exceeding the rate limit will be dropped - * `maxuri`: the maximum size in bytes that a request uri can be; default 8000 - * `maxheader`: the maximum size in bytes that request headers can be; default 8kb - * `maxbody`: the maximum size in bytes that a request body can be; default 4gb * `support100continue`: a `Bool` indicating whether `Expect: 100-continue` headers should be supported for delayed request body sending; default = `true` * `logbody`: whether the Response body should be logged when `verbose=true` logging is enabled; default = `true` """ @@ -338,4 +329,4 @@ serve(; host::IPAddr=IPv4(127,0,0,1), args...) = serve(host, port, handler, logger; cert=cert, key=key, verbose=verbose, args...) -end # module \ No newline at end of file +end # module diff --git a/test/parser.jl b/test/parser.jl index bba0bd49d..44f472318 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1611,12 +1611,6 @@ const responses = Message[ @test !h @test !m @test ex == "" - buf = "header-key: header-value\r\n" - for i = 1:10000 - e, h, m, ex = HTTP.parse!(R, HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) - e == HTTP.HPE_HEADER_OVERFLOW && break - end - @test e == HTTP.HPE_HEADER_OVERFLOW end buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n" @@ -1624,7 +1618,7 @@ const responses = Message[ @test HTTP.DEFAULT_PARSER.nread == length(buf) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "1844674407370955160" * "\r\n\r\n" - r = HTTP.parse(HTTP.Response, respstr; maxbody=1844674407370955160) + r = HTTP.parse(HTTP.Response, respstr) @test HTTP.status(r) == 200 @test HTTP.headers(r) == Dict("Content-Length"=>"1844674407370955160") From fabde090130f0aa7d9fc69c796a9f989ecf80816 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 22 Nov 2017 12:08:19 +1100 Subject: [PATCH 004/182] Replace unused parser option "lenient" with global const "strict". --- src/parser.jl | 19 ++++----- src/precompile.jl | 4 +- test/parser.jl | 101 ++++++++++++++++++++++++++-------------------- test/server.jl | 4 +- 4 files changed, 70 insertions(+), 58 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 538b99d4f..160fcfbd5 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -128,32 +128,29 @@ Parse a `HTTP.Request` or `HTTP.Response` from a string. `str` must contain at l full request or response (but may include more than one). Supported keyword arguments include: * `extra`: a `Ref{String}` that will be used to store any extra bytes beyond a full request or response - * `lenient`: whether the request/response parsing should allow additional characters """ function parse(T::Type{<:Union{Request, Response}}, str; - extra::Ref{String}=Ref{String}(), lenient::Bool=true, + extra::Ref{String}=Ref{String}(), maintask::Task=current_task()) r = T(body=FIFOBuffer()) reset!(DEFAULT_PARSER) err, headerscomplete, messagecomplete, upgrade = parse!(r, DEFAULT_PARSER, Vector{UInt8}(str); - lenient=lenient, maintask=maintask) + maintask=maintask) err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) extra[] = upgrade return r end const start_state = s_start_req_or_res -const DEFAULT_MAX_URI = Int64(8 * 1024) -const DEFAULT_MAX_BODY = Int64(2)^32 # 4Gib const DEFAULT_PARSER = Parser() +const strict = false function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); - lenient::Bool=true, host::String="", method::Method=GET, + host::String="", method::Method=GET, maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, String} - return parse!(r, parser, bytes, len, lenient, host, method, maintask) + return parse!(r, parser, bytes, len, host, method, maintask) end -function parse!(r, parser, bytes, len, lenient, host, method, maintask) - strict = !lenient +function parse!(r, parser, bytes, len, host, method, maintask) p_state = parser.state status_mark = url_mark = header_field_mark = header_field_end_mark = header_value_mark = body_mark = 0 errno = HPE_OK @@ -785,7 +782,7 @@ function parse!(r, parser, bytes, len, lenient, host, method, maintask) while p <= len @inbounds ch = Char(bytes[p]) @debug(PARSING_DEBUG, Base.escape_string(string('\'', ch, '\''))) - @debug(PARSING_DEBUG, lenient) + @debug(PARSING_DEBUG, strict) @debug(PARSING_DEBUG, isheaderchar(ch)) if ch == CR p_state = s_header_almost_done @@ -804,7 +801,7 @@ function parse!(r, parser, bytes, len, lenient, host, method, maintask) onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY) header_value_mark = 0 @goto reexecute - elseif !lenient && !isheaderchar(ch) + elseif strict && !isheaderchar(ch) @err(HPE_INVALID_HEADER_TOKEN) end diff --git a/src/precompile.jl b/src/precompile.jl index b01ba0214..19c10b472 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -105,8 +105,8 @@ function _precompile_() @assert precompile(HTTP.connect, (HTTP.Client, HTTP.http, String, String, HTTP.RequestOptions, Bool,)) @assert precompile(HTTP.string, (HTTP.Request,)) @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1},)) - @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Task)) - @assert precompile(HTTP.parse!, (HTTP.Response, HTTP.Parser, Array{UInt8, 1}, Int64, Bool, String, HTTP.Method, Task)) + @assert precompile(HTTP.parse!, (HTTP.Request, HTTP.Parser, Array{UInt8, 1}, Int64, String, HTTP.Method, Task)) + @assert precompile(HTTP.parse!, (HTTP.Response, HTTP.Parser, Array{UInt8, 1}, Int64, String, HTTP.Method, Task)) @assert precompile(HTTP.onbody, (HTTP.Request, Task, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.take!, (HTTP.Response,)) @assert precompile(HTTP.Response, (String,)) diff --git a/test/parser.jl b/test/parser.jl index 44f472318..cf62951af 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1531,77 +1531,92 @@ const responses = Message[ @testset "HTTP.parse(HTTP.Response, str)" begin for resp in responses println("TEST - parser.jl - Response: $(resp.name)") - r = HTTP.parse(HTTP.Response, resp.raw) - @test HTTP.major(r) == resp.http_major - @test HTTP.minor(r) == resp.http_minor - @test HTTP.status(r) == resp.status_code - @test HTTP.statustext(r) == resp.response_status - @test length(HTTP.headers(r)) == resp.num_headers - @test HTTP.canonicalizeheaders(HTTP.headers(r)) == resp.headers - @test String(readavailable(HTTP.body(r))) == resp.body - @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER, r) == resp.should_keep_alive + try + r = HTTP.parse(HTTP.Response, resp.raw) + @test HTTP.major(r) == resp.http_major + @test HTTP.minor(r) == resp.http_minor + @test HTTP.status(r) == resp.status_code + @test HTTP.statustext(r) == resp.response_status + @test length(HTTP.headers(r)) == resp.num_headers + @test HTTP.canonicalizeheaders(HTTP.headers(r)) == resp.headers + @test String(readavailable(HTTP.body(r))) == resp.body + @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER, r) == resp.should_keep_alive + catch e + if HTTP.strict && isa(e, HTTP.ParsingError) + println("HTTP.strict is enabled. ParsingError ignored.") + else + rethrow() + end + end end end @testset "HTTP.parse errors" begin reqstr = "GET / HTTP/1.1\r\n" * "Foo: F\01ailure" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=false) - r = HTTP.parse(HTTP.Request, reqstr; lenient=true) - - @test HTTP.method(r) == HTTP.GET - @test HTTP.uri(r) == HTTP.URI("/") - @test length(HTTP.headers(r)) == 0 + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + if !HTTP.strict + r = HTTP.parse(HTTP.Request, reqstr) + @test HTTP.method(r) == HTTP.GET + @test HTTP.uri(r) == HTTP.URI("/") + @test length(HTTP.headers(r)) == 0 + end reqstr = "GET / HTTP/1.1\r\n" * "Foo: B\02ar" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=false) - r = HTTP.parse(HTTP.Request, reqstr; lenient=true) - - @test HTTP.method(r) == HTTP.GET - @test HTTP.uri(r) == HTTP.URI("/") - @test length(HTTP.headers(r)) == 0 + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + if !HTTP.strict + r = HTTP.parse(HTTP.Request, reqstr) + @test HTTP.method(r) == HTTP.GET + @test HTTP.uri(r) == HTTP.URI("/") + @test length(HTTP.headers(r)) == 0 + end respstr = "HTTP/1.1 200 OK\r\n" * "Foo: F\01ailure" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=false) - r = HTTP.parse(HTTP.Response, respstr; lenient=true) - @test HTTP.status(r) == 200 - @test length(HTTP.headers(r)) == 0 + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + if !HTTP.strict + r = HTTP.parse(HTTP.Response, respstr) + @test HTTP.status(r) == 200 + @test length(HTTP.headers(r)) == 0 + end respstr = "HTTP/1.1 200 OK\r\n" * "Foo: B\02ar" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=false) - r = HTTP.parse(HTTP.Response, respstr; lenient=true) - @test HTTP.status(r) == 200 - @test length(HTTP.headers(r)) == 0 + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + if !HTTP.strict + r = HTTP.parse(HTTP.Response, respstr) + @test HTTP.status(r) == 200 + @test length(HTTP.headers(r)) == 0 + end reqstr = "GET / HTTP/1.1\r\n" * "Fo@: Failure" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=false) - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=true) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + !HTTP.strict && (@test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr)) reqstr = "GET / HTTP/1.1\r\n" * "Foo\01\test: Bar" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=false) - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=true) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + !HTTP.strict && (@test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr)) respstr = "HTTP/1.1 200 OK\r\n" * "Fo@: Failure" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=false) - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=true) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + !HTTP.strict && (@test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr)) respstr = "HTTP/1.1 200 OK\r\n" * "Foo\01\test: Bar" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=false) - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=true) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + !HTTP.strict && (@test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr)) reqstr = "GET / HTTP/1.1\r\n" * "Content-Length: 0\r\nContent-Length: 1\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=false) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: 0\r\nContent-Length: 1\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=false) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) reqstr = "GET / HTTP/1.1\r\n" * "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=false) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=false) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) reqstr = "GET / HTTP/1.1\r\n" * "Foo: 1\rBar: 1\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr; lenient=false) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Foo: 1\rBar: 1\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr; lenient=false) + HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + for r in ((HTTP.Request, "GET / HTTP/1.1\r\n"), (HTTP.Response, "HTTP/1.0 200 OK\r\n")) HTTP.reset!(HTTP.DEFAULT_PARSER) diff --git a/test/server.jl b/test/server.jl index 491a00836..4a105ad93 100644 --- a/test/server.jl +++ b/test/server.jl @@ -30,7 +30,7 @@ sleep(2.0) log = String(read(serverlog)) print(log) -@test contains(log, "invalid HTTP version") +!HTTP.strict && @test contains(log, "invalid HTTP version") # bad method sleep(2.0) @@ -113,4 +113,4 @@ r = HTTP.request(HTTP.Request(major=1, minor=0, uri=HTTP.URI("http://127.0.0.1:8 # other bad requests -end # @testset \ No newline at end of file +end # @testset From 1b4aee0e1f6f76bf4e7bbe0bafb8655cb69313b7 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 22 Nov 2017 12:53:25 +1100 Subject: [PATCH 005/182] Parser simplification move "close(response.body)" from parser to client remove @nread -- redundant now that header size is unrestricted remove unused handlers: onmessagecomplete, onstatus, onmessagebegin, onheaderscomplete --- src/client.jl | 3 +++ src/parser.jl | 53 ++++++++++++++++++--------------------------------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/client.jl b/src/client.jl index 975393f05..03a2143f2 100644 --- a/src/client.jl +++ b/src/client.jl @@ -288,6 +288,9 @@ function processresponse!(client, conn, response, host, method, maintask, stream @log "received bytes from the wire, processing" # EH: throws a couple of "shouldn't get here" errors; probably not much we can do errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(response, conn.parser, buffer; host=host, method=method, maintask=maintask) + if messagecomplete + close(response.body) + end @log "parsed bytes received from wire" if length(buffer) == 0 && !isopen(conn.socket) && !messagecomplete @log "socket closed before full response received" diff --git a/src/parser.jl b/src/parser.jl index 160fcfbd5..8d4b2ca81 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -22,6 +22,9 @@ # IN THE SOFTWARE. # +const start_state = s_start_req_or_res +const strict = false + mutable struct Parser state::UInt8 header_state::UInt8 @@ -33,7 +36,9 @@ mutable struct Parser valuebuffer::Vector{UInt8} end -Parser() = Parser(s_start_req_or_res, 0x00, 0, 0, 0, 0, UInt8[], UInt8[]) +Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, UInt8[], UInt8[]) + +const DEFAULT_PARSER = Parser() function reset!(p::Parser) p.state = start_state @@ -47,11 +52,6 @@ function reset!(p::Parser) return end -macro nread(n) - return esc(:(parser.nread += UInt32($n))) -end - -onmessagebegin(r) = @debug(PARSING_DEBUG, "onmessagebegin") # should we just make a copy of the byte vector for URI here? function onurl(r, bytes, i, j) @debug(PARSING_DEBUG, "onurl") @@ -60,20 +60,22 @@ function onurl(r, bytes, i, j) @debug(PARSING_DEBUG, r.method) uri = URIs.http_parser_parse_url(bytes, i, j - i + 1, r.method == CONNECT) @debug(PARSING_DEBUG, uri) - setfield!(r, :uri, uri) + r.uri = uri return end -onstatus(r) = @debug(PARSING_DEBUG, "onstatus") + function onheaderfield(p::Parser, bytes, i, j) @debug(PARSING_DEBUG, "onheaderfield") append!(p.fieldbuffer, view(bytes, i:j)) return end + function onheadervalue(p::Parser, bytes, i, j) @debug(PARSING_DEBUG, "onheadervalue") append!(p.valuebuffer, view(bytes, i:j)) return end + function onheadervalue(p, r, bytes, i, j, issetcookie, host, KEY) @debug(PARSING_DEBUG, "onheadervalue2") append!(p.valuebuffer, view(bytes, i:j)) @@ -96,7 +98,7 @@ function onheadervalue(p, r, bytes, i, j, issetcookie, host, KEY) empty!(p.valuebuffer) return end -onheaderscomplete(r) = @debug(PARSING_DEBUG, "onheaderscomplete") + function onbody(r, maintask, bytes, i, j) @debug(PARSING_DEBUG, "onbody") @debug(PARSING_DEBUG, String(r.body)) @@ -118,8 +120,6 @@ function onbody(r, maintask, bytes, i, j) @debug(PARSING_DEBUG, String(r.body)) return end -onmessagecomplete(r::Request) = @debug(PARSING_DEBUG, "onmessagecomplete") -onmessagecomplete(r::Response) = (@debug(PARSING_DEBUG, "onmessagecomplete"); close(r.body)) """ HTTP.parse([HTTP.Request, HTTP.Response], str; kwargs...) @@ -138,19 +138,17 @@ function parse(T::Type{<:Union{Request, Response}}, str; maintask=maintask) err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) extra[] = upgrade + close(r.body) return r end -const start_state = s_start_req_or_res -const DEFAULT_PARSER = Parser() -const strict = false - function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); host::String="", method::Method=GET, maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, String} return parse!(r, parser, bytes, len, host, method, maintask) end -function parse!(r, parser, bytes, len, host, method, maintask) + +function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErrorCode, Bool, Bool, String} p_state = parser.state status_mark = url_mark = header_field_mark = header_field_end_mark = header_value_mark = body_mark = 0 errno = HPE_OK @@ -161,7 +159,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) if len == 0 if p_state == s_body_identity_eof parser.state = p_state - onmessagecomplete(r) @debug(PARSING_DEBUG, "this 6") return HPE_OK, true, true, "" elseif @anyeq(p_state, s_dead, s_start_req_or_res, s_start_res, s_start_req) @@ -192,7 +189,7 @@ function parse!(r, parser, bytes, len, host, method, maintask) @debug(PARSING_DEBUG, "top of main for-loop") @debug(PARSING_DEBUG, Base.escape_string(string(ch))) if p_state <= s_headers_done - @nread(1) + parser.nread += 1 end @label reexecute @@ -215,7 +212,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) if ch == 'H' p_state = s_res_or_resp_H parser.state = p_state - onmessagebegin(r) else p_state = s_start_req @goto reexecute @@ -243,7 +239,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) @err HPE_INVALID_CONSTANT end parser.state = p_state - onmessagebegin(r) elseif p_state == s_res_H @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -346,12 +341,10 @@ function parse!(r, parser, bytes, len, host, method, maintask) if ch == CR p_state = s_res_line_almost_done parser.state = p_state - onstatus(r) status_mark = 0 elseif ch == LF p_state = s_header_field_start parser.state = p_state - onstatus(r) status_mark = 0 end @@ -405,7 +398,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) end p_state = s_req_method parser.state = p_state - onmessagebegin(r) elseif p_state == s_req_method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -707,7 +699,7 @@ function parse!(r, parser, bytes, len, host, method, maintask) p += 1 end - @nread(p - start) + parser.nread += (p - start) if p >= len p -= 1 @@ -794,7 +786,7 @@ function parse!(r, parser, bytes, len, host, method, maintask) break elseif ch == LF p_state = s_header_almost_done - @nread(p - start) + parser.nread += (p - start) parser.header_state = h parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 2") @@ -943,8 +935,7 @@ function parse!(r, parser, bytes, len, host, method, maintask) p += 1 end parser.header_state = h - - @nread(p - start) + parser.nread += (p - start) if p == len p -= 1 @@ -1037,7 +1028,7 @@ function parse!(r, parser, bytes, len, host, method, maintask) * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so * we have to simulate it by handling a change in errno below. =# - onheaderscomplete(r) + @debug(PARSING_DEBUG, "headersdone") headersdone = true if method == HEAD parser.flags |= F_SKIPBODY @@ -1059,7 +1050,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) #= Exit, the rest of the message is in a different protocol. =# p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) parser.state = p_state - onmessagecomplete(r) @debug(PARSING_DEBUG, "this 1") return errno, true, true, String(bytes[p+1:end]) end @@ -1067,7 +1057,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) if parser.flags & F_SKIPBODY > 0 p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) parser.state = p_state - onmessagecomplete(r) @debug(PARSING_DEBUG, "this 2") return errno, true, true, "" elseif parser.flags & F_CHUNKED > 0 @@ -1078,7 +1067,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) #= Content-Length header given but zero: Content-Length: 0\r\n =# p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) parser.state = p_state - onmessagecomplete(r) @debug(PARSING_DEBUG, "this 3") return errno, true, true, "" elseif parser.content_length != ULLONG_MAX @@ -1090,7 +1078,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) #= Assume content-length 0 - read the next =# p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) parser.state = p_state - onmessagecomplete(r) @debug(PARSING_DEBUG, "this 4") return errno, true, true, String(bytes[p+1:end]) else @@ -1143,7 +1130,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) @debug(PARSING_DEBUG, ParsingStateCode(p_state)) # p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) parser.state = p_state - onmessagecomplete(r) @debug(PARSING_DEBUG, "this 5") if upgrade #= Exit, the rest of the message is in a different protocol. =# @@ -1278,7 +1264,6 @@ function parse!(r, parser, bytes, len, host, method, maintask) url_mark > 0 && onurl(r, bytes, url_mark, min(len, p)) @debug(PARSING_DEBUG, "this onbody 3") body_mark > 0 && onbody(r, maintask, bytes, body_mark, min(len, p - 1)) - status_mark > 0 && onstatus(r) parser.state = p_state @debug(PARSING_DEBUG, "exiting maybe unfinished...") From f7ba394efa654f8acd5fceaf8da16bccba3b4271 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 22 Nov 2017 19:04:46 +1100 Subject: [PATCH 006/182] Add tests for parsing one charactar at a time. Fix #125 and #126. Replace local `KEY` in `parse!()` with `parser.previous_field`. Add onurlbytes(). Accumulates URL in `parser.valuebuffer`. Use `Ref{String}` to return `extra`/`upgrade` so that caller can tell the difference between upgrade with empty string and no upgrade. Add `s_req_fragment_start` to list of states where the url_mark should be reset to 1. --- src/parser.jl | 92 +++++++++++++++++++++++++++-------------------- src/precompile.jl | 7 ++-- src/server.jl | 2 +- test/parser.jl | 52 ++++++++++++++++++++++----- 4 files changed, 102 insertions(+), 51 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 8d4b2ca81..e5f85abda 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -34,9 +34,10 @@ mutable struct Parser content_length::UInt64 fieldbuffer::Vector{UInt8} valuebuffer::Vector{UInt8} + previous_field::Ref{String} end -Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, UInt8[], UInt8[]) +Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, UInt8[], UInt8[], Ref{String}()) const DEFAULT_PARSER = Parser() @@ -49,18 +50,26 @@ function reset!(p::Parser) p.content_length = 0x0000000000000000 empty!(p.fieldbuffer) empty!(p.valuebuffer) + p.previous_field = Ref{String}() return end # should we just make a copy of the byte vector for URI here? -function onurl(r, bytes, i, j) +function onurlbytes(p::Parser, bytes, i, j) + @debug(PARSING_DEBUG, "onurlbytes") + append!(p.valuebuffer, view(bytes, i:j)) + return +end + +function onurl(p::Parser, r) @debug(PARSING_DEBUG, "onurl") - @debug(PARSING_DEBUG, i - j + 1) - @debug(PARSING_DEBUG, "'$(String(bytes[i:j]))'") + @debug(PARSING_DEBUG, String(p.valuebuffer)) @debug(PARSING_DEBUG, r.method) - uri = URIs.http_parser_parse_url(bytes, i, j - i + 1, r.method == CONNECT) + url = copy(p.valuebuffer) + uri = URIs.http_parser_parse_url(url, 1, length(url), r.method == CONNECT) @debug(PARSING_DEBUG, uri) - r.uri = uri + setfield!(r, :uri, uri) + empty!(p.valuebuffer) return end @@ -76,20 +85,20 @@ function onheadervalue(p::Parser, bytes, i, j) return end -function onheadervalue(p, r, bytes, i, j, issetcookie, host, KEY) +function onheadervalue(p, r, bytes, i, j, issetcookie, host) @debug(PARSING_DEBUG, "onheadervalue2") append!(p.valuebuffer, view(bytes, i:j)) key = unsafe_string(pointer(p.fieldbuffer), length(p.fieldbuffer)) val = unsafe_string(pointer(p.valuebuffer), length(p.valuebuffer)) if key == "" # the header value was parsed in two parts, - # KEY[] holds the most recently parsed header field, + # p.previous_field[] holds the most recently parsed header field, # and we already stored the first part of the header value in r.headers # get the first part and concatenate it with the second part we have now - key = KEY[] + key = p.previous_field[] setindex!(r.headers, string(r.headers[key], val), key) else - KEY[] = key + p.previous_field[] = key val2 = get!(r.headers, key, val) val2 !== val && setindex!(r.headers, string(val2, ", ", val), key) end @@ -137,34 +146,35 @@ function parse(T::Type{<:Union{Request, Response}}, str; err, headerscomplete, messagecomplete, upgrade = parse!(r, DEFAULT_PARSER, Vector{UInt8}(str); maintask=maintask) err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) - extra[] = upgrade + if upgrade != nothing + extra[] = upgrade + end close(r.body) return r end function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); host::String="", method::Method=GET, - maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, String} + maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} return parse!(r, parser, bytes, len, host, method, maintask) end -function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErrorCode, Bool, Bool, String} +function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} p_state = parser.state status_mark = url_mark = header_field_mark = header_field_end_mark = header_value_mark = body_mark = 0 errno = HPE_OK upgrade = issetcookie = headersdone = false - KEY = Ref{String}() @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if len == 0 if p_state == s_body_identity_eof parser.state = p_state @debug(PARSING_DEBUG, "this 6") - return HPE_OK, true, true, "" + return HPE_OK, true, true, nothing elseif @anyeq(p_state, s_dead, s_start_req_or_res, s_start_res, s_start_req) - return HPE_OK, false, false, "" + return HPE_OK, false, false, nothing else - return HPE_INVALID_EOF_STATE, false, false, "" + return HPE_INVALID_EOF_STATE, false, false, nothing end end @@ -178,16 +188,21 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr end if @anyeq(p_state, s_req_path, s_req_schema, s_req_schema_slash, s_req_schema_slash_slash, s_req_server_start, s_req_server, s_req_server_with_at, - s_req_query_string_start, s_req_query_string, s_req_fragment) + s_req_query_string_start, s_req_query_string, s_req_fragment, + s_req_fragment_start) url_mark = 1 elseif p_state == s_res_status status_mark = 1 end p = 1 + old_p = 0 while p <= len + @assert p > old_p + old_p = p @inbounds ch = Char(bytes[p]) @debug(PARSING_DEBUG, "top of main for-loop") @debug(PARSING_DEBUG, Base.escape_string(string(ch))) + if p_state <= s_headers_done parser.nread += 1 end @@ -480,14 +495,16 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr if ch == ' ' p_state = s_req_http_start parser.state = p_state - onurl(r, bytes, url_mark, p-1) + onurlbytes(parser, bytes, url_mark, p-1) + onurl(parser, r) url_mark = 0 elseif ch in (CR, LF) r.major = Int16(0) r.minor = Int16(9) p_state = ifelse(ch == CR, s_req_line_almost_done, s_header_field_start) parser.state = p_state - onurl(r, bytes, url_mark, p-1) + onurlbytes(parser, bytes, url_mark, p-1) + onurl(parser, r) url_mark = 0 else p_state = URIs.parseurlchar(p_state, ch, strict) @@ -611,7 +628,10 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr ch = Char(bytes[p]) @debug(PARSING_DEBUG, Base.escape_string(string(ch))) c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] - c == Char(0) && break + if c == Char(0) + @errorif(ch != ':', HPE_INVALID_HEADER_TOKEN) + break + end h = parser.header_state if h == h_general @debug(PARSING_DEBUG, parser.header_state) @@ -701,18 +721,16 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.nread += (p - start) - if p >= len - p -= 1 - @goto breakout - end if ch == ':' p_state = s_header_value_discard_ws parser.state = p_state header_field_end_mark = p - onheaderfield(parser, bytes, header_field_mark, p - 1) + if p > header_field_mark + onheaderfield(parser, bytes, header_field_mark, p - 1) + end header_field_mark = 0 else - @err(HPE_INVALID_HEADER_TOKEN) + @assert tokens[Int(ch)+1] != Char(0) || !strict && ch == ' ' end elseif p_state == s_header_value_discard_ws @@ -781,7 +799,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.header_state = h parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 1") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY) + onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host) header_value_mark = 0 break elseif ch == LF @@ -790,7 +808,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.header_state = h parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 2") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY) + onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host) header_value_mark = 0 @goto reexecute elseif strict && !isheaderchar(ch) @@ -937,10 +955,6 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.header_state = h parser.nread += (p - start) - if p == len - p -= 1 - end - elseif p_state == s_header_almost_done @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(ch != LF, HPE_LF_EXPECTED) @@ -991,7 +1005,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr p_state = s_header_field_start parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 3") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host, KEY) + onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host) header_value_mark = 0 @goto reexecute end @@ -1058,7 +1072,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 2") - return errno, true, true, "" + return errno, true, true, nothing elseif parser.flags & F_CHUNKED > 0 #= chunked encoding - ignore Content-Length header =# p_state = s_chunk_size_start @@ -1068,7 +1082,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 3") - return errno, true, true, "" + return errno, true, true, nothing elseif parser.content_length != ULLONG_MAX #= Content-Length header given and non-zero =# p_state = s_body_identity @@ -1261,7 +1275,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, p) header_value_mark > 0 && onheadervalue(parser, bytes, header_value_mark, min(len, p)) - url_mark > 0 && onurl(r, bytes, url_mark, min(len, p)) + url_mark > 0 && onurlbytes(parser, bytes, url_mark, min(len, p)) @debug(PARSING_DEBUG, "this onbody 3") body_mark > 0 && onbody(r, maintask, bytes, body_mark, min(len, p - 1)) @@ -1271,7 +1285,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr b = p_state == start_state || p_state == s_dead he = b | (headersdone || p_state >= s_headers_done) m = b | (p_state >= s_message_done) - return errno, he, m, String(bytes[p:end]) + return errno, he, m, p >= len ? nothing : String(bytes[p:end]) @label error if errno == HPE_OK @@ -1282,7 +1296,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.header_state = 0x00 @debug(PARSING_DEBUG, "exiting due to error...") @debug(PARSING_DEBUG, errno) - return errno, false, false, "" + return errno, false, false, nothing end #= Does the parser need to see an EOF to find the end of the message? =# diff --git a/src/precompile.jl b/src/precompile.jl index 19c10b472..fd6afc90d 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -5,9 +5,10 @@ function _precompile_() @assert precompile(HTTP.Cookies.pathmatch, (HTTP.Cookies.Cookie, String,)) @assert precompile(HTTP.onheaderfield, (HTTP.Parser, Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.isjson, (Array{UInt8, 1}, UInt64, Int64,)) - @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Response, Array{UInt8, 1}, Int64, Int64, Bool, String, Base.RefValue{String})) + @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Response, Array{UInt8, 1}, Int64, Int64, Bool, String,)) @assert precompile(HTTP.isjson, (Array{UInt8, 1}, Int64, Int64,)) - @assert precompile(HTTP.onurl, (HTTP.Response, Array{UInt8, 1}, Int64, Int64,)) + @assert precompile(HTTP.onurlbytes, (HTTP.Parser, Array{UInt8, 1}, Int64, Int64,)) + @assert precompile(HTTP.onurl, (HTTP.Parser, HTTP.Response,)) @assert precompile(HTTP.Response, (Int64, String,)) @assert precompile(HTTP.URIs.getindex, (Array{UInt8, 1}, HTTP.URIs.Offset,)) @assert precompile(HTTP.iscompressed, (Array{UInt8, 1},)) @@ -84,7 +85,7 @@ function _precompile_() @assert precompile(HTTP.sniff, (Array{UInt8, 1},)) @assert precompile(HTTP.mark, (HTTP.Multipart{Base.IOStream},)) @assert precompile(HTTP.seek, (HTTP.Form, Int64,)) - @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Request, Array{UInt8, 1}, Int64, Int64, Bool, String, Base.RefValue{String})) + @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Request, Array{UInt8, 1}, Int64, Int64, Bool, String)) @assert precompile(HTTP.FIFOBuffers.write, (HTTP.FIFOBuffers.FIFOBuffer, String,)) @assert precompile(HTTP.URIs.escape, (String, String,)) @assert precompile(HTTP.dead!, (HTTP.Connection{MbedTLS.SSLContext},)) diff --git a/src/server.jl b/src/server.jl index b98c9c114..07b392f7e 100644 --- a/src/server.jl +++ b/src/server.jl @@ -141,7 +141,7 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, response = HTTP.Response(417) error = true end - elseif length(upgrade) > 0 + elseif upgrade != nothing HTTP.@log "received upgrade request on connection i=$i" response = HTTP.Response(501, "upgrade requests are not currently supported") error = true diff --git a/test/parser.jl b/test/parser.jl index cf62951af..9028efc67 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1350,10 +1350,30 @@ const responses = Message[ @testset "HTTP.parse" begin @testset "HTTP.parse(HTTP.Request, str)" begin - for req in requests - println("TEST - parser.jl - Request: $(req.name)") + for req in requests, t in ["A", "B"] + + println("TEST - parser.jl - Request $t: $(req.name)") upgrade = Ref{String}() - r = HTTP.parse(HTTP.Request, req.raw; extra=upgrade) + if t == "A" + p = HTTP.DEFAULT_PARSER + HTTP.reset!(p) + r = HTTP.Request(body=FIFOBuffer()) + bytes = Vector{UInt8}(req.raw) + sz = 1 + for i in 1:sz:length(bytes) + x = bytes[i:i+sz-1] + #@show [Char(x[i]) for i in 1:sz] + err, hc, mc, ug = HTTP.parse!(r, p, x) + err != HTTP.HPE_OK && throw(HTTP.ParsingError(HTTP.ParsingErrorCodeMap[err])) + if ug != nothing +@show ug + upgrade[] = ug + break + end + end + else + r = HTTP.parse(HTTP.Request, req.raw; extra=upgrade) + end @test HTTP.major(r) == req.http_major @test HTTP.minor(r) == req.http_minor @test HTTP.method(r) == req.method @@ -1368,7 +1388,9 @@ const responses = Message[ @test HTTP.canonicalizeheaders(HTTP.headers(r)) == req.headers @test String(readavailable(HTTP.body(r))) == req.body @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER, r) == req.should_keep_alive - @test upgrade[] == req.upgrade + @test t == "A" || + req.upgrade == "" && !isassigned(upgrade) || + upgrade[] == req.upgrade end reqstr = "GET http://www.techcrunch.com/ HTTP/1.1\r\n" * @@ -1529,10 +1551,24 @@ const responses = Message[ end @testset "HTTP.parse(HTTP.Response, str)" begin - for resp in responses - println("TEST - parser.jl - Response: $(resp.name)") + for resp in responses, t in ["A", "B"] + println("TEST - parser.jl - Response $t: $(resp.name)") try - r = HTTP.parse(HTTP.Response, resp.raw) + if t == "A" + p = HTTP.DEFAULT_PARSER + HTTP.reset!(p) + r = HTTP.Response(body=FIFOBuffer()) + bytes = Vector{UInt8}(resp.raw) + sz = 1 + for i in 1:sz:length(bytes) + x = bytes[i:i+sz-1] + #@show [Char(x[i]) for i in 1:sz] + err, hc, mc, ug = HTTP.parse!(r, p, x) + err != HTTP.HPE_OK && throw(HTTP.ParsingError(HTTP.ParsingErrorCodeMap[err])) + end + elseif t == "B" + r = HTTP.parse(HTTP.Response, resp.raw) + end @test HTTP.major(r) == resp.http_major @test HTTP.minor(r) == resp.http_minor @test HTTP.status(r) == resp.status_code @@ -1625,7 +1661,7 @@ const responses = Message[ @test e == HTTP.HPE_OK @test !h @test !m - @test ex == "" + @test ex == nothing end buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n" From 2dd90a8738c243ee4fe2ab6230cf2fe2c3014ac0 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 22 Nov 2017 21:00:59 +1100 Subject: [PATCH 007/182] Store method, major, minor, url and status in Parser - begin to separate Parser from Request/Response --- src/client.jl | 1 + src/parser.jl | 160 ++++++++++++++++++++++++++-------------------- src/precompile.jl | 2 +- src/server.jl | 5 ++ test/parser.jl | 10 ++- test/runtests.jl | 18 +++--- 6 files changed, 115 insertions(+), 81 deletions(-) diff --git a/src/client.jl b/src/client.jl index 03a2143f2..ec8b23546 100644 --- a/src/client.jl +++ b/src/client.jl @@ -288,6 +288,7 @@ function processresponse!(client, conn, response, host, method, maintask, stream @log "received bytes from the wire, processing" # EH: throws a couple of "shouldn't get here" errors; probably not much we can do errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(response, conn.parser, buffer; host=host, method=method, maintask=maintask) + response.status = conn.parser.status if messagecomplete close(response.body) end diff --git a/src/parser.jl b/src/parser.jl index e5f85abda..932ed6edd 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -35,9 +35,14 @@ mutable struct Parser fieldbuffer::Vector{UInt8} valuebuffer::Vector{UInt8} previous_field::Ref{String} + method::HTTP.Method + major::Int16 + minor::Int16 + url::HTTP.URI + status::Int32 end -Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, UInt8[], UInt8[], Ref{String}()) +Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, UInt8[], UInt8[], Ref{String}(), Method(0), 0, 0, HTTP.URI(), 0) const DEFAULT_PARSER = Parser() @@ -51,6 +56,11 @@ function reset!(p::Parser) empty!(p.fieldbuffer) empty!(p.valuebuffer) p.previous_field = Ref{String}() + p.method = Method(0) + p.major = 0 + p.minor = 0 + p.url = HTTP.URI() + p.status = 0 return end @@ -61,14 +71,14 @@ function onurlbytes(p::Parser, bytes, i, j) return end -function onurl(p::Parser, r) +function onurl(p::Parser) @debug(PARSING_DEBUG, "onurl") @debug(PARSING_DEBUG, String(p.valuebuffer)) - @debug(PARSING_DEBUG, r.method) + @debug(PARSING_DEBUG, p.method) url = copy(p.valuebuffer) - uri = URIs.http_parser_parse_url(url, 1, length(url), r.method == CONNECT) + uri = URIs.http_parser_parse_url(url, 1, length(url), p.method == CONNECT) @debug(PARSING_DEBUG, uri) - setfield!(r, :uri, uri) + p.url = uri empty!(p.valuebuffer) return end @@ -102,6 +112,7 @@ function onheadervalue(p, r, bytes, i, j, issetcookie, host) val2 = get!(r.headers, key, val) val2 !== val && setindex!(r.headers, string(val2, ", ", val), key) end + @assert !issetcookie || isa(r, Response) issetcookie && push!(r.cookies, Cookies.readsetcookie(host, val)) empty!(p.fieldbuffer) empty!(p.valuebuffer) @@ -145,6 +156,14 @@ function parse(T::Type{<:Union{Request, Response}}, str; reset!(DEFAULT_PARSER) err, headerscomplete, messagecomplete, upgrade = parse!(r, DEFAULT_PARSER, Vector{UInt8}(str); maintask=maintask) + if T == Request + r.uri = DEFAULT_PARSER.url + r.method = DEFAULT_PARSER.method + else + r.status = DEFAULT_PARSER.status + end + r.major = DEFAULT_PARSER.major + r.minor = DEFAULT_PARSER.minor err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) if upgrade != nothing extra[] = upgrade @@ -238,7 +257,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr p_state = s_res_HT else @errorif(ch != 'E', HPE_INVALID_CONSTANT) - r.method = HEAD + parser.method = HEAD parser.index = 3 p_state = s_req_method end @@ -278,7 +297,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr elseif p_state == s_res_first_http_major @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(!isnum(ch), HPE_INVALID_VERSION) - r.major = Int16(ch - '0') + parser.major = Int16(ch - '0') p_state = s_res_http_major #= major HTTP version or dot =# @@ -289,15 +308,15 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr @goto breakout end @errorif(!isnum(ch), HPE_INVALID_VERSION) - r.major *= Int16(10) - r.major += Int16(ch - '0') + parser.major *= Int16(10) + parser.major += Int16(ch - '0') @errorif(r.major > 999, HPE_INVALID_VERSION) #= first digit of minor HTTP version =# elseif p_state == s_res_first_http_minor @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(!isnum(ch), HPE_INVALID_VERSION) - r.minor = Int16(ch - '0') + parser.minor = Int16(ch - '0') p_state = s_res_http_minor #= minor HTTP version or end of request line =# @@ -308,9 +327,9 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr @goto breakout end @errorif(!isnum(ch), HPE_INVALID_VERSION) - r.minor *= Int16(10) - r.minor += Int16(ch - '0') - @errorif(r.minor > 999, HPE_INVALID_VERSION) + parser.minor *= Int16(10) + parser.minor += Int16(ch - '0') + @errorif(parser.minor > 999, HPE_INVALID_VERSION) elseif p_state == s_res_first_status_code @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -318,7 +337,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr ch == ' ' && @goto breakout @err(HPE_INVALID_STATUS) end - r.status = Int32(ch - '0') + parser.status = Int32(ch - '0') p_state = s_res_status_code elseif p_state == s_res_status_code @@ -334,9 +353,9 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr @err(HPE_INVALID_STATUS) end else - r.status *= Int32(10) - r.status += Int32(ch - '0') - @errorif(r.status > 999, HPE_INVALID_STATUS) + parser.status *= Int32(10) + parser.status += Int32(ch - '0') + @errorif(parser.status > 999, HPE_INVALID_STATUS) end elseif p_state == s_res_status_start @@ -375,39 +394,39 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.content_length = ULLONG_MAX @errorif(!isalpha(ch), HPE_INVALID_METHOD) - r.method = Method(0) + parser.method = Method(0) parser.index = 2 if ch == 'A' - r.method = ACL + parser.method = ACL elseif ch == 'B' - r.method = BIND + parser.method = BIND elseif ch == 'C' - r.method = CONNECT + parser.method = CONNECT elseif ch == 'D' - r.method = DELETE + parser.method = DELETE elseif ch == 'G' - r.method = GET + parser.method = GET elseif ch == 'H' - r.method = HEAD + parser.method = HEAD elseif ch == 'L' - r.method = LOCK + parser.method = LOCK elseif ch == 'M' - r.method = MKCOL + parser.method = MKCOL elseif ch == 'N' - r.method = NOTIFY + parser.method = NOTIFY elseif ch == 'O' - r.method = OPTIONS + parser.method = OPTIONS elseif ch == 'P' - r.method = POST + parser.method = POST elseif ch == 'R' - r.method = REPORT + parser.method = REPORT elseif ch == 'S' - r.method = SUBSCRIBE + parser.method = SUBSCRIBE elseif ch == 'T' - r.method = TRACE + parser.method = TRACE elseif ch == 'U' - r.method = UNLOCK + parser.method = UNLOCK else @err(HPE_INVALID_METHOD) end @@ -416,7 +435,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr elseif p_state == s_req_method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - matcher = string(r.method) + matcher = string(parser.method) @debug(PARSING_DEBUG, matcher) @debug(PARSING_DEBUG, parser.index) @debug(PARSING_DEBUG, Base.escape_string(string(ch))) @@ -427,47 +446,47 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr elseif ch == matcher[parser.index] @debug(PARSING_DEBUG, "nada") elseif isalpha(ch) - ci = @shifted(r.method, Int(parser.index) - 1, ch) + ci = @shifted(parser.method, Int(parser.index) - 1, ch) if ci == @shifted(POST, 1, 'U') - r.method = PUT + parser.method = PUT elseif ci == @shifted(POST, 1, 'A') - r.method = PATCH + parser.method = PATCH elseif ci == @shifted(CONNECT, 1, 'H') - r.method = CHECKOUT + parser.method = CHECKOUT elseif ci == @shifted(CONNECT, 2, 'P') - r.method = COPY + parser.method = COPY elseif ci == @shifted(MKCOL, 1, 'O') - r.method = MOVE + parser.method = MOVE elseif ci == @shifted(MKCOL, 1, 'E') - r.method = MERGE + parser.method = MERGE elseif ci == @shifted(MKCOL, 2, 'A') - r.method = MKACTIVITY + parser.method = MKACTIVITY elseif ci == @shifted(MKCOL, 3, 'A') - r.method = MKCALENDAR + parser.method = MKCALENDAR elseif ci == @shifted(SUBSCRIBE, 1, 'E') - r.method = SEARCH + parser.method = SEARCH elseif ci == @shifted(REPORT, 2, 'B') - r.method = REBIND + parser.method = REBIND elseif ci == @shifted(POST, 1, 'R') - r.method = PROPFIND + parser.method = PROPFIND elseif ci == @shifted(PROPFIND, 4, 'P') - r.method = PROPPATCH + parser.method = PROPPATCH elseif ci == @shifted(PUT, 2, 'R') - r.method = PURGE + parser.method = PURGE elseif ci == @shifted(LOCK, 1, 'I') - r.method = LINK + parser.method = LINK elseif ci == @shifted(UNLOCK, 2, 'S') - r.method = UNSUBSCRIBE + parser.method = UNSUBSCRIBE elseif ci == @shifted(UNLOCK, 2, 'B') - r.method = UNBIND + parser.method = UNBIND elseif ci == @shifted(UNLOCK, 3, 'I') - r.method = UNLINK + parser.method = UNLINK else @err(HPE_INVALID_METHOD) end - elseif ch == '-' && parser.index == 2 && r.method == MKCOL + elseif ch == '-' && parser.index == 2 && parser.method == MKCOL @debug(PARSING_DEBUG, "matched MSEARCH") - r.method = MSEARCH + parser.method = MSEARCH parser.index -= 1 else @err(HPE_INVALID_METHOD) @@ -479,7 +498,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr @debug(PARSING_DEBUG, ParsingStateCode(p_state)) ch == ' ' && @goto breakout url_mark = p - if r.method == CONNECT + if parser.method == CONNECT p_state = s_req_server_start end p_state = URIs.parseurlchar(p_state, ch, strict) @@ -496,15 +515,15 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr p_state = s_req_http_start parser.state = p_state onurlbytes(parser, bytes, url_mark, p-1) - onurl(parser, r) + onurl(parser) url_mark = 0 elseif ch in (CR, LF) - r.major = Int16(0) - r.minor = Int16(9) + parser.major = Int16(0) + parser.minor = Int16(9) p_state = ifelse(ch == CR, s_req_line_almost_done, s_header_field_start) parser.state = p_state onurlbytes(parser, bytes, url_mark, p-1) - onurl(parser, r) + onurl(parser) url_mark = 0 else p_state = URIs.parseurlchar(p_state, ch, strict) @@ -544,7 +563,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr elseif p_state == s_req_first_http_major @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(ch < '1' || ch > '9', HPE_INVALID_VERSION) - r.major = Int16(ch - '0') + parser.major = Int16(ch - '0') p_state = s_req_http_major #= major HTTP version or dot =# @@ -555,16 +574,16 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr elseif !isnum(ch) @err(HPE_INVALID_VERSION) else - r.major *= Int16(10) - r.major += Int16(ch - '0') - @errorif(r.major > 999, HPE_INVALID_VERSION) + parser.major *= Int16(10) + parser.major += Int16(ch - '0') + @errorif(parser.major > 999, HPE_INVALID_VERSION) end #= first digit of minor HTTP version =# elseif p_state == s_req_first_http_minor @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(!isnum(ch), HPE_INVALID_VERSION) - r.minor = Int16(ch - '0') + parser.minor = Int16(ch - '0') p_state = s_req_http_minor #= minor HTTP version or end of request line =# @@ -577,9 +596,9 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr else #= XXX allow spaces after digit? =# @errorif(!isnum(ch), HPE_INVALID_VERSION) - r.minor *= Int16(10) - r.minor += Int16(ch - '0') - @errorif(r.minor > 999, HPE_INVALID_VERSION) + parser.minor *= Int16(10) + parser.minor += Int16(ch - '0') + @errorif(parser.minor > 999, HPE_INVALID_VERSION) end #= end of request line =# @@ -1030,7 +1049,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr if (parser.flags & F_UPGRADE > 0) && (parser.flags & F_CONNECTION_UPGRADE > 0) upgrade = typeof(r) == Request || r.status == 101 else - upgrade = typeof(r) == Request && r.method == CONNECT + upgrade = typeof(r) == Request && parser.method == CONNECT end @debug(PARSING_DEBUG, upgrade) #= Here we call the headers_complete callback. This is somewhat @@ -1059,7 +1078,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr hasBody = parser.flags & F_CHUNKED > 0 || (parser.content_length > 0 && parser.content_length != ULLONG_MAX) - if upgrade && ((typeof(r) == Request && r.method == CONNECT) || + if upgrade && ((typeof(r) == Request && parser.method == CONNECT) || (parser.flags & F_SKIPBODY) > 0 || !hasBody) #= Exit, the rest of the message is in a different protocol. =# p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) @@ -1093,7 +1112,8 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 4") - return errno, true, true, String(bytes[p+1:end]) + #return errno, true, true, String(bytes[p+1:end]) + return errno, true, true, p >= len ? nothing : String(bytes[p:end]) else #= Read body until EOF =# p_state = s_body_identity_eof diff --git a/src/precompile.jl b/src/precompile.jl index fd6afc90d..34116f577 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -8,7 +8,7 @@ function _precompile_() @assert precompile(HTTP.onheadervalue, (HTTP.Parser, HTTP.Response, Array{UInt8, 1}, Int64, Int64, Bool, String,)) @assert precompile(HTTP.isjson, (Array{UInt8, 1}, Int64, Int64,)) @assert precompile(HTTP.onurlbytes, (HTTP.Parser, Array{UInt8, 1}, Int64, Int64,)) - @assert precompile(HTTP.onurl, (HTTP.Parser, HTTP.Response,)) + @assert precompile(HTTP.onurl, (HTTP.Parser,)) @assert precompile(HTTP.Response, (Int64, String,)) @assert precompile(HTTP.URIs.getindex, (Array{UInt8, 1}, HTTP.URIs.Offset,)) @assert precompile(HTTP.iscompressed, (Array{UInt8, 1},)) diff --git a/src/server.jl b/src/server.jl index 07b392f7e..b54aa6c62 100644 --- a/src/server.jl +++ b/src/server.jl @@ -106,6 +106,10 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, length(buffer) > 0 || break starttime[] = time() # reset the timeout while still receiving bytes errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(request, parser, buffer) + request.method = parser.method + request.uri = parser.url + request.major = parser.major + request.minor = parser.minor startedprocessingrequest = true if errno != HTTP.HPE_OK # error in parsing the http request @@ -142,6 +146,7 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, error = true end elseif upgrade != nothing + @show upgrade HTTP.@log "received upgrade request on connection i=$i" response = HTTP.Response(501, "upgrade requests are not currently supported") error = true diff --git a/test/parser.jl b/test/parser.jl index 9028efc67..d094bcb54 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1365,8 +1365,12 @@ const responses = Message[ #@show [Char(x[i]) for i in 1:sz] err, hc, mc, ug = HTTP.parse!(r, p, x) err != HTTP.HPE_OK && throw(HTTP.ParsingError(HTTP.ParsingErrorCodeMap[err])) + r.uri = HTTP.DEFAULT_PARSER.url + r.method = HTTP.DEFAULT_PARSER.method + r.major = HTTP.DEFAULT_PARSER.major + r.minor = HTTP.DEFAULT_PARSER.minor + if ug != nothing -@show ug upgrade[] = ug break end @@ -1565,6 +1569,10 @@ const responses = Message[ #@show [Char(x[i]) for i in 1:sz] err, hc, mc, ug = HTTP.parse!(r, p, x) err != HTTP.HPE_OK && throw(HTTP.ParsingError(HTTP.ParsingErrorCodeMap[err])) + r.major = HTTP.DEFAULT_PARSER.major + r.minor = HTTP.DEFAULT_PARSER.minor + r.status = HTTP.DEFAULT_PARSER.status + end elseif t == "B" r = HTTP.parse(HTTP.Response, resp.raw) diff --git a/test/runtests.jl b/test/runtests.jl index 484267b11..3bd10deb4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,14 +1,14 @@ using HTTP, Base.Test @testset "HTTP" begin - include("utils.jl"); - include("fifobuffer.jl"); - include("sniff.jl"); - include("uri.jl"); - include("cookies.jl"); - include("parser.jl"); - include("types.jl"); - include("handlers.jl") - include("client.jl"); +# include("utils.jl"); +# include("fifobuffer.jl"); +# include("sniff.jl"); +# include("uri.jl"); +# include("cookies.jl"); +# include("parser.jl"); +# include("types.jl"); +# include("handlers.jl") +# include("client.jl"); include("server.jl") end; From bb120bdf0fb36f25496aaa7fb0504cfa4d2c62d4 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 27 Nov 2017 20:07:00 +1100 Subject: [PATCH 008/182] Use Vector{Pair} instead of Dict for Headers. See #108. Store Vector{Pair} headers in Parser struct. Move cookie processing from parser to client. Move special treatement of multi-line and repeated headers out of main parser function. --- src/HTTP.jl | 2 +- src/client.jl | 46 +++++---- src/consts.jl | 3 - src/parser.jl | 116 ++++++++++----------- src/server.jl | 10 +- src/types.jl | 40 ++++---- src/utils.jl | 2 +- test/client.jl | 7 +- test/cookies.jl | 26 ++--- test/parser.jl | 259 ++++++++++++++++++++++++----------------------- test/runtests.jl | 18 ++-- test/server.jl | 2 +- 12 files changed, 271 insertions(+), 260 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index eeb8098c0..6d0e7fabf 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -43,7 +43,7 @@ using .Handlers include("server.jl") using .Nitrogen -include("precompile.jl") +#include("precompile.jl") function __init__() global const DEFAULT_CLIENT = Client() diff --git a/src/client.jl b/src/client.jl index ec8b23546..4aa8f5074 100644 --- a/src/client.jl +++ b/src/client.jl @@ -211,8 +211,8 @@ function addcookies!(client, host, req, verbose) # check if cookies should be added to outgoing request based on host if haskey(client.cookies, host) cookies = client.cookies[host] - tosend = Set{Cookie}() - expired = Set{Cookie}() + tosend = Vector{Cookie}() + expired = Vector{Cookie}() for (i, cookie) in enumerate(cookies) if Cookies.shouldsend(cookie, scheme(uri(req)) == "https", host, path(uri(req))) cookie.expires != DateTime() && cookie.expires < now(Dates.UTC) && (push!(expired, cookie); @log("deleting expired cookie: " * cookie.name); continue) @@ -222,7 +222,12 @@ function addcookies!(client, host, req, verbose) setdiff!(client.cookies[host], expired) if length(tosend) > 0 @log "adding cached cookies for host to request header: " * join(map(x->x.name, tosend), ", ") - req.headers["Cookie"] = string(Base.get(req.headers, "Cookie", ""), [c for c in tosend]) + i = findfirst(x->x[1] == "Cookie", req.headers) + if i > 0 + req.headers[i] = "Cookie" => string(req.headers[i][2], tosend) + else + push!(req.headers, "Cookie" => string("", tosend)) + end end end end @@ -249,11 +254,11 @@ end function redirect(response, client, req, opts, stream, history, retry, verbose) logger = client.logger @log "checking for location to redirect" - key = haskey(response.headers, "Location") ? "Location" : "" - if key != "" + i = findfirst(x->x[1] == "Location", response.headers) + if i > 0 push!(history, response) length(history) > opts.maxredirects::Int && throw(RedirectError(opts.maxredirects::Int)) - newuri = URIs.URL(response.headers[key]) + newuri = URIs.URL(response.headers[i][2]) u = uri(req) newuri = !isempty(hostname(newuri)) ? newuri : URIs.URI(scheme=scheme(u), hostname=hostname(u), port=port(u), path=path(newuri), query=query(u)) if opts.forwardheaders::Bool @@ -281,14 +286,15 @@ function getbytes(socket, tm) end end -function processresponse!(client, conn, response, host, method, maintask, stream, tm, verbose) +function processresponse!(client, conn, response, host, method, maintask, stream, opts, verbose) logger = client.logger while true - buffer, err = getbytes(conn.socket, tm) + buffer, err = getbytes(conn.socket, opts.readtimeout) @log "received bytes from the wire, processing" # EH: throws a couple of "shouldn't get here" errors; probably not much we can do - errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(response, conn.parser, buffer; host=host, method=method, maintask=maintask) + errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(response, conn.parser, buffer; method=method, maintask=maintask) response.status = conn.parser.status + if messagecomplete close(response.body) end @@ -304,13 +310,13 @@ function processresponse!(client, conn, response, host, method, maintask, stream dead!(conn) throw(ParsingError("error parsing response: $(ParsingErrorCodeMap[errno])\nCurrent response buffer contents: $(String(buffer))")) elseif messagecomplete - http_should_keep_alive(conn.parser, response) || (@log("closing connection (no keep-alive)"); dead!(conn)) + http_should_keep_alive(conn.parser) || (@log("closing connection (no keep-alive)"); dead!(conn)) # idle! on a Dead will stay Dead idle!(conn) return true, StatusError(status(response), response) elseif stream && headerscomplete @log "processing the rest of response asynchronously" - response.body.task = @async processresponse!(client, conn, response, host, method, maintask, false, tm, false) + response.body.task = @async processresponse!(client, conn, response, host, method, maintask, false, opts, false) return true, StatusError(status(response), response) end end @@ -329,10 +335,10 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo # maybe allow retrying for all kinds of errors? p = port(u) conn = @retryif ClosedError 4 connectandsend(client, sch, host, ifelse(p == "", "80", p), req, opts, verbose) - + response = Response(stream ? 2^24 : FIFOBuffers.DEFAULT_MAX, req) reset!(conn.parser) - success, err = processresponse!(client, conn, response, host, HTTP.method(req), current_task(), stream, opts.readtimeout::Float64, verbose) + success, err = processresponse!(client, conn, response, host, HTTP.method(req), current_task(), stream, opts, verbose) if !success retry >= opts.retries::Int && throw(err) return request(client, req, opts, stream, history, retry + 1, verbose) @@ -341,7 +347,13 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo if opts.canonicalizeheaders::Bool response.headers = canonicalizeheaders(response.headers) end - 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)) + if opts.managecookies::Bool && any(x->x[1]=="Set-Cookie", response.headers) + cookies = get!(client.cookies, host, Set{Cookie}()) + push!(cookies, (Cookies.readsetcookie(host, v[2]) + for v in filter(x->x[1]=="Set-Cookie", response.headers))...) + @log("caching received cookie for host: " * cookies) + end + response.history = history if opts.allowredirects::Bool && req.method != HEAD && (300 <= status(response) < 400) return redirect(response, client, req, opts, stream, history, retry, verbose) @@ -354,7 +366,7 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo end end -request(req::Request; +request(req::Request; opts::RequestOptions=RequestOptions(), stream::Bool=false, history::Vector{Response}=Response[], @@ -373,7 +385,7 @@ request(client::Client, req::Request; # build Request function request(client::Client, method, uri::URI; - headers::Dict=Headers(), + headers::Dict=Dict(), body=FIFOBuffers.EMPTYBODY, stream::Bool=false, verbose::Bool=false, @@ -451,7 +463,7 @@ Access-Control-Allow-Origin: * Server: meinheld/0.6.1 Content-Length: 32 -{ +{ "origin": "50.207.241.62" } \"\"\" diff --git a/src/consts.jl b/src/consts.jl index 5d17679f5..b3303e46c 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -312,13 +312,11 @@ const h_matching_proxy_connection = 0x05 const h_matching_content_length = 0x06 const h_matching_transfer_encoding = 0x07 const h_matching_upgrade = 0x08 -const h_matching_setcookie = 0x09 const h_connection = 0x0a const h_content_length = 0x0b const h_transfer_encoding = 0x0c const h_upgrade = 0x0d -const h_setcookie = 0x0e const h_matching_transfer_encoding_chunked = 0x0f const h_matching_connection_token_start = 0x10 @@ -344,7 +342,6 @@ const CONNECTION = "connection" const CONTENT_LENGTH = "content-length" const TRANSFER_ENCODING = "transfer-encoding" const UPGRADE = "upgrade" -const SETCOOKIE = "set-cookie" const CHUNKED = "chunked" const KEEP_ALIVE = "keep-alive" const CLOSE = "close" diff --git a/src/parser.jl b/src/parser.jl index 932ed6edd..83735b3af 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -32,17 +32,17 @@ mutable struct Parser flags::UInt8 nread::UInt32 content_length::UInt64 - fieldbuffer::Vector{UInt8} + fieldbuffer::Vector{UInt8} #FIXME IOBuffer valuebuffer::Vector{UInt8} - previous_field::Ref{String} method::HTTP.Method major::Int16 minor::Int16 url::HTTP.URI status::Int32 + headers::Vector{Pair{String,String}} end -Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, UInt8[], UInt8[], Ref{String}(), Method(0), 0, 0, HTTP.URI(), 0) +Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, UInt8[], UInt8[], Method(0), 0, 0, HTTP.URI(), 0, Pair{String,String}[]) const DEFAULT_PARSER = Parser() @@ -55,12 +55,12 @@ function reset!(p::Parser) p.content_length = 0x0000000000000000 empty!(p.fieldbuffer) empty!(p.valuebuffer) - p.previous_field = Ref{String}() p.method = Method(0) p.major = 0 p.minor = 0 p.url = HTTP.URI() p.status = 0 + empty!(p.headers) return end @@ -83,37 +83,23 @@ function onurl(p::Parser) return end -function onheaderfield(p::Parser, bytes, i, j) - @debug(PARSING_DEBUG, "onheaderfield") +function onheaderfieldbytes(p::Parser, bytes, i, j) + @debug(PARSING_DEBUG, "onheaderfieldbytes") append!(p.fieldbuffer, view(bytes, i:j)) return end -function onheadervalue(p::Parser, bytes, i, j) - @debug(PARSING_DEBUG, "onheadervalue") +function onheadervaluebytes(p::Parser, bytes, i, j) + @debug(PARSING_DEBUG, "onheadervaluebytes") append!(p.valuebuffer, view(bytes, i:j)) return end -function onheadervalue(p, r, bytes, i, j, issetcookie, host) +function onheadervalue(p) @debug(PARSING_DEBUG, "onheadervalue2") - append!(p.valuebuffer, view(bytes, i:j)) key = unsafe_string(pointer(p.fieldbuffer), length(p.fieldbuffer)) val = unsafe_string(pointer(p.valuebuffer), length(p.valuebuffer)) - if key == "" - # the header value was parsed in two parts, - # p.previous_field[] holds the most recently parsed header field, - # and we already stored the first part of the header value in r.headers - # get the first part and concatenate it with the second part we have now - key = p.previous_field[] - setindex!(r.headers, string(r.headers[key], val), key) - else - p.previous_field[] = key - val2 = get!(r.headers, key, val) - val2 !== val && setindex!(r.headers, string(val2, ", ", val), key) - end - @assert !issetcookie || isa(r, Response) - issetcookie && push!(r.cookies, Cookies.readsetcookie(host, val)) + push!(p.headers, key => val) empty!(p.fieldbuffer) empty!(p.valuebuffer) return @@ -173,16 +159,32 @@ function parse(T::Type{<:Union{Request, Response}}, str; end function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); - host::String="", method::Method=GET, + method::Method=GET, maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} - return parse!(r, parser, bytes, len, host, method, maintask) + + err, headerscomplete, messagecomplete, upgrade = parse!(r, parser, bytes, len, method, maintask) + + if headerscomplete && isempty(r.headers) + for (k, v) in parser.headers + if k == "" + r.headers[end] = r.headers[end][1] => string(r.headers[end][2], v) +#FIXME move this to Headers->Dict conversino function... + elseif k != "Set-Cookie" && length(r.headers) > 0 && k == r.headers[end].first + r.headers[end] = r.headers[end][1] => string(r.headers[end][2], ", ", v) + else + push!(r.headers, k => v) + end + end + end + + return err, headerscomplete, messagecomplete, upgrade end -function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} +function parse!(r, parser, bytes, len, method, maintask)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} p_state = parser.state status_mark = url_mark = header_field_mark = header_field_end_mark = header_value_mark = body_mark = 0 errno = HPE_OK - upgrade = issetcookie = headersdone = false + upgrade = headersdone = false @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if len == 0 @@ -310,7 +312,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr @errorif(!isnum(ch), HPE_INVALID_VERSION) parser.major *= Int16(10) parser.major += Int16(ch - '0') - @errorif(r.major > 999, HPE_INVALID_VERSION) + @errorif(parser.major > 999, HPE_INVALID_VERSION) #= first digit of minor HTTP version =# elseif p_state == s_res_first_http_minor @@ -621,7 +623,6 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr @errorif(c == Char(0), HPE_INVALID_HEADER_TOKEN) header_field_mark = header_field_end_mark = p parser.index = 1 - issetcookie = false p_state = s_header_field if c == 'c' @@ -632,8 +633,6 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.header_state = h_matching_transfer_encoding elseif c == 'u' parser.header_state = h_matching_upgrade - elseif c == 's' - parser.header_state = h_matching_setcookie else parser.header_state = h_general end @@ -718,16 +717,6 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr elseif parser.index == length(UPGRADE) parser.header_state = h_upgrade end - #= set-cookie =# - elseif h == h_matching_setcookie - @debug(PARSING_DEBUG, parser.header_state) - parser.index += 1 - if parser.index > length(SETCOOKIE) || c != SETCOOKIE[parser.index] - parser.header_state = h_general - elseif parser.index == length(SETCOOKIE) - parser.header_state = h_general - issetcookie = true - end elseif h in (h_connection, h_content_length, h_transfer_encoding, h_upgrade) if ch != ' ' parser.header_state = h_general @@ -745,7 +734,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.state = p_state header_field_end_mark = p if p > header_field_mark - onheaderfield(parser, bytes, header_field_mark, p - 1) + onheaderfieldbytes(parser, bytes, header_field_mark, p - 1) end header_field_mark = 0 else @@ -818,8 +807,9 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.header_state = h parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 1") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host) + onheadervaluebytes(parser, bytes, header_value_mark, p - 1) header_value_mark = 0 + onheadervalue(parser) break elseif ch == LF p_state = s_header_almost_done @@ -827,8 +817,9 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr parser.header_state = h parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 2") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host) + onheadervaluebytes(parser, bytes, header_value_mark, p - 1) header_value_mark = 0 + onheadervalue(parser) @goto reexecute elseif strict && !isheaderchar(ch) @err(HPE_INVALID_HEADER_TOKEN) @@ -1024,8 +1015,9 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr p_state = s_header_field_start parser.state = p_state @debug(PARSING_DEBUG, "onheadervalue 3") - onheadervalue(parser, r, bytes, header_value_mark, p - 1, issetcookie, host) + onheadervaluebytes(parser, bytes, header_value_mark, p - 1) header_value_mark = 0 + onheadervalue(parser) @goto reexecute end @@ -1047,7 +1039,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr #= Set this here so that on_headers_complete() callbacks can see it =# @debug(PARSING_DEBUG, "checking for upgrade...") if (parser.flags & F_UPGRADE > 0) && (parser.flags & F_CONNECTION_UPGRADE > 0) - upgrade = typeof(r) == Request || r.status == 101 + upgrade = typeof(r) == Request || parser.status == 101 else upgrade = typeof(r) == Request && parser.method == CONNECT end @@ -1081,14 +1073,14 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr if upgrade && ((typeof(r) == Request && parser.method == CONNECT) || (parser.flags & F_SKIPBODY) > 0 || !hasBody) #= Exit, the rest of the message is in a different protocol. =# - p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) + p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 1") return errno, true, true, String(bytes[p+1:end]) end if parser.flags & F_SKIPBODY > 0 - p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) + p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 2") return errno, true, true, nothing @@ -1098,7 +1090,7 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr else if parser.content_length == 0 #= Content-Length header given but zero: Content-Length: 0\r\n =# - p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) + p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 3") return errno, true, true, nothing @@ -1107,9 +1099,9 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr p_state = s_body_identity @debug(PARSING_DEBUG, ParsingStateCode(p_state)) else - if !http_message_needs_eof(parser, r) + if !http_message_needs_eof(parser) #= Assume content-length 0 - read the next =# - p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) + p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 4") #return errno, true, true, String(bytes[p+1:end]) @@ -1290,11 +1282,11 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr (body_mark > 0 ? 1 : 0) + (status_mark > 0 ? 1 : 0)) <= 1) - header_field_mark > 0 && onheaderfield(parser, bytes, header_field_mark, min(len, p)) + header_field_mark > 0 && onheaderfieldbytes(parser, bytes, header_field_mark, min(len, p)) @debug(PARSING_DEBUG, "onheadervalue 4") @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, p) - header_value_mark > 0 && onheadervalue(parser, bytes, header_value_mark, min(len, p)) + header_value_mark > 0 && onheadervaluebytes(parser, bytes, header_value_mark, min(len, p)) url_mark > 0 && onurlbytes(parser, bytes, url_mark, min(len, p)) @debug(PARSING_DEBUG, "this onbody 3") body_mark > 0 && onbody(r, maintask, bytes, body_mark, min(len, p - 1)) @@ -1320,12 +1312,12 @@ function parse!(r, parser, bytes, len, host, method, maintask)::Tuple{ParsingErr end #= Does the parser need to see an EOF to find the end of the message? =# -http_message_needs_eof(parser, r::Request) = false -function http_message_needs_eof(parser, r::Response) +function http_message_needs_eof(parser) #= See RFC 2616 section 4.4 =# - if (div(r.status, 100) == 1 || #= 1xx e.g. Continue =# - r.status == 204 || #= No Content =# - r.status == 304 || #= Not Modified =# + if (parser.status == 0 || # Request + div(parser.status, 100) == 1 || #= 1xx e.g. Continue =# + parser.status == 204 || #= No Content =# + parser.status == 304 || #= Not Modified =# parser.flags & F_SKIPBODY > 0) #= response to a HEAD request =# return false end @@ -1337,8 +1329,8 @@ function http_message_needs_eof(parser, r::Response) return true end -function http_should_keep_alive(parser, r) - if r.major > 0 && r.minor > 0 +function http_should_keep_alive(parser) + if parser.major > 0 && parser.minor > 0 #= HTTP/1.1 =# if parser.flags & F_CONNECTION_CLOSE > 0 return false @@ -1350,5 +1342,5 @@ function http_should_keep_alive(parser, r) end end - return !http_message_needs_eof(parser, r) + return !http_message_needs_eof(parser) end diff --git a/src/server.jl b/src/server.jl index b54aa6c62..0d63545d1 100644 --- a/src/server.jl +++ b/src/server.jl @@ -160,12 +160,16 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, error = true HTTP.@log e end - if HTTP.http_should_keep_alive(parser, request) && !error - get!(HTTP.headers(response), "Connection", "keep-alive") + if HTTP.http_should_keep_alive(parser) && !error + if !any(x->x[1] == "Connection", response.headers) + push!(response.headers, "Connection" => "keep-alive") + end HTTP.reset!(parser) request = HTTP.Request() else - get!(HTTP.headers(response), "Connection", "close") + if !any(x->x[1] == "Connection", response.headers) + push!(response.headers, "Connection" => "close") + end shouldclose = true end if !error diff --git a/src/types.jl b/src/types.jl index 4ead4b19a..c8ff2a90b 100644 --- a/src/types.jl +++ b/src/types.jl @@ -10,7 +10,7 @@ sockettype(::Type{https}) = TLS.SSLContext schemetype(::Type{TCPSocket}) = http schemetype(::Type{TLS.SSLContext}) = https -const Headers = Dict{String, String} +const Headers = Vector{Pair{String, String}} const Option{T} = Union{T, Void} not(::Void) = true @@ -129,48 +129,52 @@ method(r::Request) = r.method major(r::Request) = r.major minor(r::Request) = r.minor uri(r::Request) = r.uri -headers(r::Request) = r.headers +headers(r::Request) = Dict(r.headers) body(r::Request) = r.body -defaultheaders(::Type{Request}) = Headers( +defaultheaders(::Type{Request}) = [ "User-Agent" => "HTTP.jl/0.0.0", "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json; charset=utf-8" -) -makeheaders(d::Dict) = Headers((string(k), string(v)) for (k, v) in d) +] -function Request(m::HTTP.Method, uri::URI, userheaders::Dict, b; +function Request(m::HTTP.Method, uri::URI, userheaders, b; options::RequestOptions=RequestOptions(), verbose::Bool=false, logger::Option{IO}=STDOUT) if m != CONNECT headers = defaultheaders(Request) - headers["Host"] = host(uri) + push!(headers, "Host" => host(uri)) else headers = Headers() end - if !isempty(userinfo(uri)) && !haskey(headers, "Authorization") - headers["Authorization"] = "Basic $(base64encode(userinfo(uri)))" + if !isempty(userinfo(uri)) && !any(x->x[1] == "Authorization", headers) + push!(headers, "Authorization" => "Basic $(base64encode(userinfo(uri)))") @log "adding basic authentication header" end if isa(b, Dict) || isa(b, Form) # form data body = Form(b) - headers["Content-Type"] = "multipart/form-data; boundary=$(body.boundary)" + push!(headers, "Content-Type" => "multipart/form-data; boundary=$(body.boundary)") else body = FIFOBuffer(b) end if iscompressed(body) && length(body) > get(options, :chunksize, 0) options.chunksize = length(body) + 1 end - if !haskey(headers, "Content-Type") && length(body) > 0 && !isa(body, Form) + if !any(x->x[1] == "Content-Type", headers) && length(body) > 0 && !isa(body, Form) sn = sniff(body) - headers["Content-Type"] = sn + push!(headers, "Content-Type" => sn) @log "setting Content-Type header to: $sn" end - return Request(m, Int16(1), Int16(1), uri, merge!(headers, makeheaders(userheaders)), body) + + userkeys = [x[1] for x in userheaders] + filter!(x->!(x[1] in userkeys), headers) + append!(headers, userheaders) + + return Request(m, Int16(1), Int16(1), uri, headers, body) end -Request(method, uri, h=Headers(), body=""; options::RequestOptions=RequestOptions(), logger::Option{IO}=STDOUT, verbose::Bool=false) = +Request(method, uri, h=Dict(), body=""; options::RequestOptions=RequestOptions(), logger::Option{IO}=STDOUT, verbose::Bool=false) = Request(convert(HTTP.Method, method), isa(uri, String) ? URI(uri; isconnect=(method == "CONNECT" || method == CONNECT)) : uri, h, body; options=options, logger=logger, verbose=verbose) @@ -229,7 +233,7 @@ status(r::Response) = r.status major(r::Response) = r.major minor(r::Response) = r.minor cookies(r::Response) = r.cookies -headers(r::Response) = r.headers +headers(r::Response) = Dict(r.headers) request(r::Response) = r.request history(r::Response) = r.history statustext(r::Response) = Base.get(STATUS_CODES, r.status, "Unknown Code") @@ -257,12 +261,12 @@ Response(s::Integer, msg) = Response(; status=s, body=FIFOBuffer(msg)) Response(b::Union{Vector{UInt8}, String}) = Response(; headers=defaultheaders(Response), body=FIFOBuffer(b)) Response(s::Integer, h::Headers, body) = Response(; status=s, headers=h, body=FIFOBuffer(body)) -defaultheaders(::Type{Response}) = Headers( +defaultheaders(::Type{Response}) = [ "Server" => "Julia/$VERSION", "Content-Type" => "text/html; charset=utf-8", "Content-Language" => "en", "Date" => Dates.format(now(Dates.UTC), Dates.RFC1123Format) -) +] ==(a::Response,b::Response) = (a.status == b.status) && (a.major == b.major) && @@ -291,7 +295,7 @@ end # headers function headers(io::IO, r::Union{Request, Response}) - for (k, v) in headers(r) + for (k, v) in r.headers write(io, "$k: $v$CRLF") end # write(io, CRLF); we let the body write this in case of chunked transfer diff --git a/src/utils.jl b/src/utils.jl index 2fb5b875a..1b5d7957b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -177,7 +177,7 @@ function tocameldash!(s::String) return s end -canonicalizeheaders{T}(h::T) = T(tocameldash!(k) => v for (k,v) in h) +canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) iso8859_1_to_utf8(str::String) = iso8859_1_to_utf8(Vector{UInt8}(str)) function iso8859_1_to_utf8(bytes::Vector{UInt8}) diff --git a/test/client.jl b/test/client.jl index 43ec8e81b..c02fb453d 100644 --- a/test/client.jl +++ b/test/client.jl @@ -40,13 +40,14 @@ for sch in ("http", "https") @test (haskey(h, "Hey") ? h["Hey"] == "dude" : h["hey"] == "dude") println("cookie requests") + empty!(HTTP.DEFAULT_CLIENT.cookies) r = HTTP.get("$sch://httpbin.org/cookies") body = String(take!(r)) - @test (body == "{\n \"cookies\": {}\n}\n" || body == "{\n \"cookies\": {\n \"hey\": \"\"\n }\n}\n" || body == "{\n \"cookies\": {\n \"hey\": \"sailor\"\n }\n}\n") - r = HTTP.get("$sch://httpbin.org/cookies/set?hey=sailor") + @test body == "{\n \"cookies\": {}\n}\n" + r = HTTP.get("$sch://httpbin.org/cookies/set?hey=sailor&foo=bar") @test HTTP.status(r) == 200 body = String(take!(r)) - @test (body == "{\n \"cookies\": {\n \"hey\": \"sailor\"\n }\n}\n" || body == "{\n \"cookies\": {\n \"hey\": \"\"\n }\n}\n") + @test body == "{\n \"cookies\": {\n \"foo\": \"bar\", \n \"hey\": \"sailor\"\n }\n}\n" # r = HTTP.get("$sch://httpbin.org/cookies/delete?hey") # @test String(take!(r)) == "{\n \"cookies\": {\n \"hey\": \"\"\n }\n}\n" diff --git a/test/cookies.jl b/test/cookies.jl index 82d72dccd..9499ea8a4 100644 --- a/test/cookies.jl +++ b/test/cookies.jl @@ -49,25 +49,25 @@ end @testset "readsetcookies" begin cookietests = [ - (HTTP.Headers("Set-Cookie"=> "Cookie-1=v\$1"), [HTTP.Cookie("Cookie-1", "v\$1")]), - (HTTP.Headers("Set-Cookie"=> "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"), + (HTTP.Headers(["Set-Cookie"=> "Cookie-1=v\$1"]), [HTTP.Cookie("Cookie-1", "v\$1")]), + (HTTP.Headers(["Set-Cookie"=> "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"]), [HTTP.Cookie("NID", "99=YsDT5i3E-CXax-"; path="/", domain="google.ch", httponly=true, expires=DateTime(2011, 11, 23, 1, 5, 3, 0))]), - (HTTP.Headers("Set-Cookie"=> ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"), + (HTTP.Headers(["Set-Cookie"=> ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"]), [HTTP.Cookie(".ASPXAUTH", "7E3AA"; path="/", expires=DateTime(2012, 3, 7, 14, 25, 6, 0), httponly=true)]), - (HTTP.Headers("Set-Cookie"=> "ASP.NET_SessionId=foo; path=/; HttpOnly"), + (HTTP.Headers(["Set-Cookie"=> "ASP.NET_SessionId=foo; path=/; HttpOnly"]), [HTTP.Cookie("ASP.NET_SessionId", "foo"; path="/", httponly=true)]), - (HTTP.Headers("Set-Cookie"=> "special-1=a z"), [HTTP.Cookie("special-1", "a z")]), - (HTTP.Headers("Set-Cookie"=> "special-2=\" z\""), [HTTP.Cookie("special-2", " z")]), - (HTTP.Headers("Set-Cookie"=> "special-3=\"a \""), [HTTP.Cookie("special-3", "a ")]), - (HTTP.Headers("Set-Cookie"=> "special-4=\" \""), [HTTP.Cookie("special-4", " ")]), - (HTTP.Headers("Set-Cookie"=> "special-5=a,z"), [HTTP.Cookie("special-5", "a,z")]), - (HTTP.Headers("Set-Cookie"=> "special-6=\",z\""), [HTTP.Cookie("special-6", ",z")]), - (HTTP.Headers("Set-Cookie"=> "special-7=a,"), [HTTP.Cookie("special-7", "a,")]), - (HTTP.Headers("Set-Cookie"=> "special-8=\",\""), [HTTP.Cookie("special-8", ",")]), + (HTTP.Headers(["Set-Cookie"=> "special-1=a z"]), [HTTP.Cookie("special-1", "a z")]), + (HTTP.Headers(["Set-Cookie"=> "special-2=\" z\""]), [HTTP.Cookie("special-2", " z")]), + (HTTP.Headers(["Set-Cookie"=> "special-3=\"a \""]), [HTTP.Cookie("special-3", "a ")]), + (HTTP.Headers(["Set-Cookie"=> "special-4=\" \""]), [HTTP.Cookie("special-4", " ")]), + (HTTP.Headers(["Set-Cookie"=> "special-5=a,z"]), [HTTP.Cookie("special-5", "a,z")]), + (HTTP.Headers(["Set-Cookie"=> "special-6=\",z\""]), [HTTP.Cookie("special-6", ",z")]), + (HTTP.Headers(["Set-Cookie"=> "special-7=a,"]), [HTTP.Cookie("special-7", "a,")]), + (HTTP.Headers(["Set-Cookie"=> "special-8=\",\""]), [HTTP.Cookie("special-8", ",")]), ] for (h, c) in cookietests - @test HTTP.Cookies.readsetcookies("", [h["Set-Cookie"]]) == c + @test HTTP.Cookies.readsetcookies("", [Dict(h)["Set-Cookie"]]) == c end end diff --git a/test/parser.jl b/test/parser.jl index d094bcb54..12deedb0d 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -14,7 +14,7 @@ mutable struct Message userinfo::String port::String num_headers::Int - headers::Dict{String,String} + headers::HTTP.Headers should_keep_alive::Bool upgrade::String http_major::Int @@ -52,11 +52,11 @@ Message(name= "curl get" ,request_path= "/test" ,request_url= "/test" ,num_headers= 3 -,headers=Dict{String,String}( +,headers=[ "User-Agent"=> "curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1" , "Host"=> "0.0.0.0=5000" , "Accept"=> "*/*" - ) + ] ,body= "" ), Message(name= "firefox get" ,raw= "GET /favicon.ico HTTP/1.1\r\n" * @@ -78,7 +78,7 @@ Message(name= "curl get" ,request_path= "/favicon.ico" ,request_url= "/favicon.ico" ,num_headers= 8 -,headers=Dict{String,String}( +,headers=[ "Host"=> "0.0.0.0=5000" , "User-Agent"=> "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0" , "Accept"=> "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" @@ -87,7 +87,7 @@ Message(name= "curl get" , "Accept-Charset"=> "ISO-8859-1,utf-8;q=0.7,*;q=0.7" , "Keep-Alive"=> "300" , "Connection"=> "keep-alive" -) +] ,body= "" ), Message(name= "dumbfuck" ,raw= "GET /dumbfuck HTTP/1.1\r\n" * @@ -102,9 +102,9 @@ Message(name= "curl get" ,request_path= "/dumbfuck" ,request_url= "/dumbfuck" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Aaaaaaaaaaaaa"=> "++++++++++" -) +] ,body= "" ), Message(name= "fragment in url" ,raw= "GET /forums/1/topics/2375?page=1#posts-17408 HTTP/1.1\r\n" * @@ -146,9 +146,9 @@ Message(name= "curl get" ,request_path= "/get_one_header_no_body" ,request_url= "/get_one_header_no_body" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Accept" => "*/*" -) +] ,body= "" ), Message(name= "get funky content length body hello" ,raw= "GET /get_funky_content_length_body_hello HTTP/1.0\r\n" * @@ -164,9 +164,9 @@ Message(name= "curl get" ,request_path= "/get_funky_content_length_body_hello" ,request_url= "/get_funky_content_length_body_hello" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Content-Length" => "5" -) +] ,body= "HELLO" ), Message(name= "post identity body world" ,raw= "POST /post_identity_body_world?q=search#hey HTTP/1.1\r\n" * @@ -184,11 +184,11 @@ Message(name= "curl get" ,request_path= "/post_identity_body_world" ,request_url= "/post_identity_body_world?q=search#hey" ,num_headers= 3 -,headers=Dict{String,String}( +,headers=[ "Accept"=> "*/*" , "Transfer-Encoding"=> "identity" , "Content-Length"=> "5" -) +] ,body= "World" ), Message(name= "post - chunked body: all your base are belong to us" ,raw= "POST /post_chunked_all_your_base HTTP/1.1\r\n" * @@ -206,9 +206,9 @@ Message(name= "curl get" ,request_path= "/post_chunked_all_your_base" ,request_url= "/post_chunked_all_your_base" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Transfer-Encoding" => "chunked" -) +] ,body= "all your base are belong to us" ), Message(name= "two chunks ; triple zero ending" ,raw= "POST /two_chunks_mult_zero_end HTTP/1.1\r\n" * @@ -227,9 +227,9 @@ Message(name= "curl get" ,request_path= "/two_chunks_mult_zero_end" ,request_url= "/two_chunks_mult_zero_end" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Transfer-Encoding"=> "chunked" -) +] ,body= "hello world" ), Message(name= "chunked with trailing headers. blech." ,raw= "POST /chunked_w_trailing_headers HTTP/1.1\r\n" * @@ -250,11 +250,11 @@ Message(name= "curl get" ,request_path= "/chunked_w_trailing_headers" ,request_url= "/chunked_w_trailing_headers" ,num_headers= 3 -,headers=Dict{String,String}( +,headers=[ "Transfer-Encoding"=> "chunked" , "Vary"=> "*" , "Content-Type"=> "text/plain" -) +] ,body= "hello world" ), Message(name= "with bullshit after the length" ,raw= "POST /chunked_w_bullshit_after_length HTTP/1.1\r\n" * @@ -273,9 +273,9 @@ Message(name= "curl get" ,request_path= "/chunked_w_bullshit_after_length" ,request_url= "/chunked_w_bullshit_after_length" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Transfer-Encoding"=> "chunked" -) +] ,body= "hello world" ), Message(name= "with quotes" ,raw= "GET /with_\"stupid\"_quotes?foo=\"bar\" HTTP/1.1\r\n\r\n" @@ -288,7 +288,7 @@ Message(name= "curl get" ,request_path= "/with_\"stupid\"_quotes" ,request_url= "/with_\"stupid\"_quotes?foo=\"bar\"" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name = "apachebench get" ,raw= "GET /test HTTP/1.0\r\n" * @@ -304,10 +304,10 @@ Message(name= "curl get" ,request_path= "/test" ,request_url= "/test" ,num_headers= 3 -,headers=Dict{String,String}( "Host"=> "0.0.0.0:5000" +,headers=[ "Host"=> "0.0.0.0:5000" , "User-Agent"=> "ApacheBench/2.3" , "Accept"=> "*/*" - ) + ] ,body= "" ), Message(name = "query url with question mark" ,raw= "GET /test.cgi?foo=bar?baz HTTP/1.1\r\n\r\n" @@ -320,7 +320,7 @@ Message(name= "curl get" ,request_path= "/test.cgi" ,request_url= "/test.cgi?foo=bar?baz" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name = "newline prefix get" ,raw= "\r\nGET /test HTTP/1.1\r\n\r\n" @@ -333,7 +333,7 @@ Message(name= "curl get" ,request_path= "/test" ,request_url= "/test" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name = "upgrade request" ,raw= "GET /demo HTTP/1.1\r\n" * @@ -356,14 +356,14 @@ Message(name= "curl get" ,request_url= "/demo" ,num_headers= 7 ,upgrade="Hot diggity dogg" -,headers=Dict{String,String}( "Host"=> "example.com" +,headers=[ "Host"=> "example.com" , "Connection"=> "Upgrade" , "Sec-Websocket-Key2"=> "12998 5 Y3 1 .P00" , "Sec-Websocket-Protocol"=> "sample" , "Upgrade"=> "WebSocket" , "Sec-Websocket-Key1"=> "4 @1 46546xW%0l 1 5" , "Origin"=> "http://example.com" - ) + ] ,body= "" ), Message(name = "connect request" ,raw= "CONNECT 0-home0.netscape.com:443 HTTP/1.0\r\n" * @@ -384,9 +384,9 @@ Message(name= "curl get" ,request_url= "0-home0.netscape.com:443" ,num_headers= 2 ,upgrade="some data\r\nand yet even more data" -,headers=Dict{String,String}( "User-Agent"=> "Mozilla/1.1N" +,headers=[ "User-Agent"=> "Mozilla/1.1N" , "Proxy-Authorization"=> "basic aGVsbG86d29ybGQ=" - ) + ] ,body= "" ), Message(name= "report request" ,raw= "REPORT /test HTTP/1.1\r\n" * @@ -400,7 +400,7 @@ Message(name= "curl get" ,request_path= "/test" ,request_url= "/test" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name= "request with no http version" ,raw= "GET /\r\n" * @@ -414,7 +414,7 @@ Message(name= "curl get" ,request_path= "/" ,request_url= "/" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name= "m-search request" ,raw= "M-SEARCH * HTTP/1.1\r\n" * @@ -431,10 +431,10 @@ Message(name= "curl get" ,request_path= "*" ,request_url= "*" ,num_headers= 3 -,headers=Dict{String,String}( "Host"=> "239.255.255.250:1900" +,headers=[ "Host"=> "239.255.255.250:1900" , "Man"=> "\"ssdp:discover\"" , "St"=> "\"ssdp:all\"" - ) + ] ,body= "" ), Message(name= "host terminated by a query string" ,raw= "GET http://hypnotoad.org?hail=all HTTP/1.1\r\n" * @@ -449,7 +449,7 @@ Message(name= "curl get" ,request_url= "http://hypnotoad.org?hail=all" ,host= "hypnotoad.org" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name= "host:port terminated by a query string" ,raw= "GET http://hypnotoad.org:1234?hail=all HTTP/1.1\r\n" * @@ -465,7 +465,7 @@ Message(name= "curl get" ,host= "hypnotoad.org" ,port= "1234" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name= "host:port terminated by a space" ,raw= "GET http://hypnotoad.org:1234 HTTP/1.1\r\n" * @@ -481,7 +481,7 @@ Message(name= "curl get" ,host= "hypnotoad.org" ,port= "1234" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name = "PATCH request" ,raw= "PATCH /file.txt HTTP/1.1\r\n" * @@ -500,11 +500,11 @@ Message(name= "curl get" ,request_path= "/file.txt" ,request_url= "/file.txt" ,num_headers= 4 -,headers=Dict{String,String}( "Host"=> "www.example.com" +,headers=[ "Host"=> "www.example.com" , "Content-Type"=> "application/example" , "If-Match"=> "\"e0023aa4e\"" , "Content-Length"=> "10" - ) + ] ,body= "cccccccccc" ), Message(name = "connect caps request" ,raw= "CONNECT HOME0.NETSCAPE.COM:443 HTTP/1.0\r\n" * @@ -523,9 +523,9 @@ Message(name= "curl get" ,port="443" ,num_headers= 2 ,upgrade="" -,headers=Dict{String,String}( "User-Agent"=> "Mozilla/1.1N" +,headers=[ "User-Agent"=> "Mozilla/1.1N" , "Proxy-Authorization"=> "basic aGVsbG86d29ybGQ=" - ) + ] ,body= "" ), Message(name= "utf-8 path request" ,raw= "GET /δ¶/δt/pope?q=1#narf HTTP/1.1\r\n" * @@ -540,7 +540,7 @@ Message(name= "curl get" ,request_path= "/δ¶/δt/pope" ,request_url= "/δ¶/δt/pope?q=1#narf" ,num_headers= 1 -,headers=Dict{String,String}("Host" => "github.com") +,headers=["Host" => "github.com"] ,body= "" ), Message(name = "hostname underscore" ,raw= "CONNECT home_0.netscape.com:443 HTTP/1.0\r\n" * @@ -559,9 +559,9 @@ Message(name= "curl get" ,port="443" ,num_headers= 2 ,upgrade="" -,headers=Dict{String,String}( "User-Agent"=> "Mozilla/1.1N" +,headers=[ "User-Agent"=> "Mozilla/1.1N" , "Proxy-Authorization"=> "basic aGVsbG86d29ybGQ=" - ) + ] ,body= "" ), Message(name = "eat CRLF between requests, no \"Connection: close\" header" ,raw= "POST / HTTP/1.1\r\n" * @@ -580,10 +580,10 @@ Message(name= "curl get" ,request_url= "/" ,num_headers= 3 ,upgrade= "" -,headers=Dict{String,String}( "Host"=> "www.example.com" +,headers=[ "Host"=> "www.example.com" , "Content-Type"=> "application/x-www-form-urlencoded" , "Content-Length"=> "4" - ) + ] ,body= "q=42" ), Message(name = "eat CRLF between requests even if \"Connection: close\" is set" ,raw= "POST / HTTP/1.1\r\n" * @@ -603,11 +603,11 @@ Message(name= "curl get" ,request_url= "/" ,num_headers= 4 ,upgrade= "" -,headers=Dict{String,String}( "Host"=> "www.example.com" +,headers=[ "Host"=> "www.example.com" , "Content-Type"=> "application/x-www-form-urlencoded" , "Content-Length"=> "4" , "Connection"=> "close" - ) + ] ,body= "q=42" ), Message(name = "PURGE request" ,raw= "PURGE /file.txt HTTP/1.1\r\n" * @@ -622,7 +622,7 @@ Message(name= "curl get" ,request_path= "/file.txt" ,request_url= "/file.txt" ,num_headers= 1 -,headers=Dict{String,String}( "Host"=> "www.example.com" ) +,headers=[ "Host"=> "www.example.com" ] ,body= "" ), Message(name = "SEARCH request" ,raw= "SEARCH / HTTP/1.1\r\n" * @@ -637,7 +637,7 @@ Message(name= "curl get" ,request_path= "/" ,request_url= "/" ,num_headers= 1 -,headers=Dict{String,String}( "Host"=> "www.example.com") +,headers=[ "Host"=> "www.example.com"] ,body= "" ), Message(name= "host:port and basic_auth" ,raw= "GET http://a%12:b!&*\$@hypnotoad.org:1234/toto HTTP/1.1\r\n" * @@ -653,7 +653,7 @@ Message(name= "curl get" ,userinfo= "a%12:b!&*\$" ,port= "1234" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name = "upgrade post request" ,raw= "POST /demo HTTP/1.1\r\n" * @@ -672,11 +672,11 @@ Message(name= "curl get" ,request_url= "/demo" ,num_headers= 4 ,upgrade="Hot diggity dogg" -,headers=Dict{String,String}( "Host"=> "example.com" +,headers=[ "Host"=> "example.com" , "Connection"=> "Upgrade" , "Upgrade"=> "HTTP/2.0" , "Content-Length"=> "15" - ) + ] ,body= "sweet post body" ), Message(name = "connect with body request" ,raw= "CONNECT foo.bar.com:443 HTTP/1.0\r\n" * @@ -694,10 +694,10 @@ Message(name= "curl get" ,port="443" ,num_headers= 3 ,upgrade="blarfcicle" -,headers=Dict{String,String}( "User-Agent"=> "Mozilla/1.1N" +,headers=[ "User-Agent"=> "Mozilla/1.1N" , "Proxy-Authorization"=> "basic aGVsbG86d29ybGQ=" , "Content-Length"=> "10" - ) + ] ,body= "" ), Message(name = "link request" ,raw= "LINK /images/my_dog.jpg HTTP/1.1\r\n" * @@ -714,9 +714,9 @@ Message(name= "curl get" ,query_string= "" ,fragment= "" ,num_headers= 2 -,headers=Dict{String,String}( "Host"=> "example.com" +,headers=[ "Host"=> "example.com" , "Link"=> "; rel=\"tag\", ; rel=\"tag\"" - ) + ] ,body= "" ), Message(name = "link request" ,raw= "UNLINK /images/my_dog.jpg HTTP/1.1\r\n" * @@ -732,9 +732,9 @@ Message(name= "curl get" ,query_string= "" ,fragment= "" ,num_headers= 2 -,headers=Dict{String,String}( "Host"=> "example.com" +,headers=[ "Host"=> "example.com" , "Link"=> "; rel=\"tag\"" - ) + ] ,body= "" ), Message(name = "multiple connection header values with folding" ,raw= "GET /demo HTTP/1.1\r\n" * @@ -758,14 +758,14 @@ Message(name= "curl get" ,request_url= "/demo" ,num_headers= 7 ,upgrade="Hot diggity dogg" -,headers=Dict{String,String}( "Host"=> "example.com" +,headers=[ "Host"=> "example.com" , "Connection"=> "Something, Upgrade, ,Keep-Alive" , "Sec-Websocket-Key2"=> "12998 5 Y3 1 .P00" , "Sec-Websocket-Protocol"=> "sample" , "Upgrade"=> "WebSocket" , "Sec-Websocket-Key1"=> "4 @1 46546xW%0l 1 5" , "Origin"=> "http://example.com" - ) + ] ,body= "" ), Message(name= "line folding in header value" ,raw= "GET / HTTP/1.1\r\n" * @@ -792,12 +792,12 @@ Message(name= "curl get" ,request_path= "/" ,request_url= "/" ,num_headers= 5 -,headers=Dict{String,String}( "Line1"=> "abc\tdef ghi\t\tjkl mno \t \tqrs" +,headers=[ "Line1"=> "abc\tdef ghi\t\tjkl mno \t \tqrs" , "Line2"=> "line2\t" , "Line3"=> "line3" , "Line4"=> "" , "Connection"=> "close" - ) + ] ,body= "" ), Message(name = "multiple connection header values with folding and lws" ,raw= "GET /demo HTTP/1.1\r\n" * @@ -815,9 +815,9 @@ Message(name= "curl get" ,request_url= "/demo" ,num_headers= 2 ,upgrade="Hot diggity dogg" -,headers=Dict{String,String}( "Connection"=> "keep-alive, upgrade" +,headers=[ "Connection"=> "keep-alive, upgrade" , "Upgrade"=> "WebSocket" - ) + ] ,body= "" ), Message(name = "multiple connection header values with folding and lws" ,raw= "GET /demo HTTP/1.1\r\n" * @@ -835,9 +835,9 @@ Message(name= "curl get" ,request_url= "/demo" ,num_headers= 2 ,upgrade="Hot diggity dogg" -,headers=Dict{String,String}( "Connection"=> "keep-alive, upgrade" +,headers=[ "Connection"=> "keep-alive, upgrade" , "Upgrade"=> "WebSocket" - ) + ] ,body= "" ), Message(name= "line folding in header value" ,raw= "GET / HTTP/1.1\n" * @@ -864,12 +864,12 @@ Message(name= "curl get" ,request_path= "/" ,request_url= "/" ,num_headers= 5 -,headers=Dict{String,String}( "Line1"=> "abc\tdef ghi\t\tjkl mno \t \tqrs" +,headers=[ "Line1"=> "abc\tdef ghi\t\tjkl mno \t \tqrs" , "Line2"=> "line2\t" , "Line3"=> "line3" , "Line4"=> "" , "Connection"=> "close" - ) + ] ,body= "" ) ] @@ -899,7 +899,7 @@ const responses = Message[ ,status_code= 301 ,response_status= "Moved Permanently" ,num_headers= 8 -,headers=Dict{String,String}( +,headers=[ "Location"=> "http://www.google.com/" , "Content-Type"=> "text/html; charset=UTF-8" , "Date"=> "Sun, 26 Apr 2009 11:11:49 GMT" @@ -908,7 +908,7 @@ const responses = Message[ , "Cache-Control"=> "public, max-age=2592000" , "Server"=> "gws" , "Content-Length"=> "219 " -) +] ,body= "\n" * "301 Moved\n" * "

301 Moved

\n" * @@ -938,13 +938,13 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 5 -,headers=Dict{String,String}( +,headers=[ "Date"=> "Tue, 04 Aug 2009 07:59:32 GMT" , "Server"=> "Apache" , "X-Powered-By"=> "Servlet/2.5 JSP/2.1" , "Content-Type"=> "text/xml; charset=utf-8" , "Connection"=> "close" -) +] ,body= "\n" * "\n" * " \n" * @@ -962,7 +962,7 @@ const responses = Message[ ,status_code= 404 ,response_status= "Not Found" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body_size= 0 ,body= "" ), Message(name= "301 no response phrase" @@ -973,7 +973,7 @@ const responses = Message[ ,status_code= 301 ,response_status= "Moved Permanently" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name="200 trailing space on chunked body" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -994,10 +994,10 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 2 -,headers=Dict{String,String}( +,headers=[ "Content-Type"=> "text/plain" , "Transfer-Encoding"=> "chunked" -) +] ,body_size = 37+28 ,body = "This is the data in the first chunk\r\n" * @@ -1014,10 +1014,10 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 2 -,headers=Dict{String,String}( +,headers=[ "Content-Type"=> "text/html; charset=utf-8" , "Connection"=> "close" -) +] ,body= "these headers are from http://news.ycombinator.com/" ), Message(name="proxy connection" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -1033,12 +1033,12 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 4 -,headers=Dict{String,String}( +,headers=[ "Content-Type"=> "text/html; charset=UTF-8" , "Content-Length"=> "11" , "Proxy-Connection"=> "close" , "Date"=> "Thu, 31 Dec 2009 20:55:48 +0000" -) +] ,body= "hello world" ), Message(name="underscore header key" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -1052,12 +1052,12 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 4 -,headers=Dict{String,String}( +,headers=[ "Server"=> "DCLK-AdSvr" , "Content-Type"=> "text/xml" , "Content-Length"=> "0" , "Dclk_imp"=> "v7;x;114750856;0-0;0;17820020;0/0;21603567/21621457/1;;~okv=;dcmt=text/xml;;~cs=o" -) +] ,body= "" ), Message(name= "bonjourmadame.fr" ,raw= "HTTP/1.0 301 Moved Permanently\r\n" * @@ -1077,7 +1077,7 @@ const responses = Message[ ,status_code= 301 ,response_status= "Moved Permanently" ,num_headers= 9 -,headers=Dict{String,String}( +,headers=[ "Date"=> "Thu, 03 Jun 2010 09:56:32 GMT" , "Server"=> "Apache/2.2.3 (Red Hat)" , "Cache-Control"=> "public" @@ -1087,7 +1087,7 @@ const responses = Message[ , "Content-Length"=> "0" , "Content-Type"=> "text/html; charset=UTF-8" , "Connection"=> "keep-alive" -) +] ,body= "" ), Message(name= "field underscore" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -1110,7 +1110,7 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 11 -,headers=Dict{String,String}( +,headers=[ "Date"=> "Tue, 28 Sep 2010 01:14:13 GMT" , "Server"=> "Apache" , "Cache-Control"=> "no-cache, must-revalidate" @@ -1122,7 +1122,7 @@ const responses = Message[ , "Transfer-Encoding"=> "chunked" , "Content-Type"=> "text/html" , "Connection"=> "close" -) +] ,body= "" ), Message(name= "non-ASCII in status line" ,raw= "HTTP/1.1 500 Oriëntatieprobleem\r\n" * @@ -1136,11 +1136,11 @@ const responses = Message[ ,status_code= 500 ,response_status= "Internal Server Error" ,num_headers= 3 -,headers=Dict{String,String}( +,headers=[ "Date"=> "Fri, 5 Nov 2010 23:07:12 GMT+2" , "Content-Length"=> "0" , "Connection"=> "close" -) +] ,body= "" ), Message(name= "http version 0.9" ,raw= "HTTP/0.9 200 OK\r\n" * @@ -1151,7 +1151,7 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name= "neither content-length nor transfer-encoding response" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -1164,9 +1164,9 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Content-Type"=> "text/plain" -) +] ,body= "hello world" ), Message(name= "HTTP/1.0 with keep-alive and EOF-terminated 200 status" ,raw= "HTTP/1.0 200 OK\r\n" * @@ -1178,9 +1178,9 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Connection"=> "keep-alive" -) +] ,body_size= 0 ,body= "" ), Message(name= "HTTP/1.0 with keep-alive and a 204 status" @@ -1193,9 +1193,9 @@ const responses = Message[ ,status_code= 204 ,response_status= "No Content" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Connection"=> "keep-alive" -) +] ,body_size= 0 ,body= "" ), Message(name= "HTTP/1.1 with an EOF-terminated 200 status" @@ -1207,7 +1207,7 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body_size= 0 ,body= "" ), Message(name= "HTTP/1.1 with a 204 status" @@ -1219,7 +1219,7 @@ const responses = Message[ ,status_code= 204 ,response_status= "No Content" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body_size= 0 ,body= "" ), Message(name= "HTTP/1.1 with a 204 status and keep-alive disabled" @@ -1232,9 +1232,9 @@ const responses = Message[ ,status_code= 204 ,response_status= "No Content" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Connection"=> "close" -) +] ,body_size= 0 ,body= "" ), Message(name= "HTTP/1.1 with chunked endocing and a 200 response" @@ -1249,9 +1249,9 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 1 -,headers=Dict{String,String}( +,headers=[ "Transfer-Encoding"=> "chunked" -) +] ,body_size= 0 ,body= "" ), Message(name= "field space" @@ -1271,7 +1271,7 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 7 -,headers=Dict{String,String}( +,headers=[ "Server"=> "Microsoft-IIS/6.0" , "X-Powered-By"=> "ASP.NET" , "En-Us content-Type"=> "text/xml" @@ -1279,7 +1279,7 @@ const responses = Message[ , "Content-Length"=> "16" , "Date"=> "Fri, 23 Jul 2010 18:45:38 GMT" , "Connection"=> "keep-alive" -) +] ,body= "hello" ), Message(name= "amazon.com" ,raw= "HTTP/1.1 301 MovedPermanently\r\n" * @@ -1303,7 +1303,7 @@ const responses = Message[ ,status_code= 301 ,response_status= "Moved Permanently" ,num_headers= 9 -,headers=Dict{String,String}( "Date"=> "Wed, 15 May 2013 17:06:33 GMT" +,headers=[ "Date"=> "Wed, 15 May 2013 17:06:33 GMT" , "Server"=> "Server" , "X-Amz-Id-1"=> "0GPHKXSJQ826RK7GZEB2" , "P3p"=> "policyref=\"http://www.amazon.com/w3c/p3p.xml\",CP=\"CAO DSP LAW CUR ADM IVAo IVDo CONo OTPo OUR DELi PUBi OTRi BUS PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA HEA PRE LOC GOV OTC \"" @@ -1312,7 +1312,7 @@ const responses = Message[ , "Vary"=> "Accept-Encoding,User-Agent" , "Content-Type"=> "text/html; charset=ISO-8859-1" , "Transfer-Encoding"=> "chunked" - ) + ] ,body= "\n" ), Message(name= "empty reason phrase after space" ,raw= "HTTP/1.1 200 \r\n" * @@ -1323,7 +1323,7 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 0 -,headers=Dict{String,String}() +,headers=HTTP.Headers() ,body= "" ), Message(name= "Content-Length-X" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -1340,9 +1340,9 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 2 -,headers=Dict{String,String}( "Content-Length-X"=> "0" +,headers=[ "Content-Length-X"=> "0" , "Transfer-Encoding"=> "chunked" - ) + ] ,body= "OK" ) ] @@ -1389,9 +1389,9 @@ const responses = Message[ @test HTTP.port(HTTP.uri(r)) in (req.port, "80", "443") @test string(HTTP.uri(r)) == req.request_url @test length(HTTP.headers(r)) == req.num_headers - @test HTTP.canonicalizeheaders(HTTP.headers(r)) == req.headers + @test HTTP.canonicalizeheaders(HTTP.headers(r)) == Dict(req.headers) @test String(readavailable(HTTP.body(r))) == req.body - @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER, r) == req.should_keep_alive + @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == req.should_keep_alive @test t == "A" || req.upgrade == "" && !isassigned(upgrade) || upgrade[] == req.upgrade @@ -1410,8 +1410,9 @@ const responses = Message[ req = HTTP.Request() req.uri = HTTP.URI("http://www.techcrunch.com/") - req.headers = HTTP.Headers("Content-Length"=>"7","Host"=>"www.techcrunch.com","Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7","Proxy-Connection"=>"keep-alive","Accept-Language"=>"en-us,en;q=0.5","Keep-Alive"=>"300","User-Agent"=>"Fake","Accept-Encoding"=>"gzip,deflate") + req.headers = ["Host"=>"www.techcrunch.com","User-Agent"=>"Fake","Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language"=>"en-us,en;q=0.5","Accept-Encoding"=>"gzip,deflate","Accept-Charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7","Keep-Alive"=>"300","Content-Length"=>"7","Proxy-Connection"=>"keep-alive"] + @test HTTP.parse(HTTP.Request, reqstr).headers == req.headers @test HTTP.parse(HTTP.Request, reqstr) == req reqstr = "GET / HTTP/1.1\r\n" * @@ -1419,7 +1420,7 @@ const responses = Message[ req = HTTP.Request() req.uri = HTTP.URI("/") - req.headers = HTTP.Headers("Host"=>"foo.com") + req.headers = ["Host"=>"foo.com"] @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1428,7 +1429,7 @@ const responses = Message[ req = HTTP.Request() req.uri = HTTP.URI("//user@host/is/actually/a/path/") - req.headers = HTTP.Headers("Host"=>"test") + req.headers = ["Host"=>"test"] @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1454,7 +1455,7 @@ const responses = Message[ req = HTTP.Request() req.method = "POST" req.uri = HTTP.URI("/") - req.headers = HTTP.Headers("Transfer-Encoding"=>"chunked", "Host"=>"foo.com", "Trailer-Key"=>"Trailer-Value") + req.headers = ["Host"=>"foo.com", "Transfer-Encoding"=>"chunked", "Trailer-Key"=>"Trailer-Value"] req.body = HTTP.FIFOBuffer("foobar") @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1499,7 +1500,7 @@ const responses = Message[ req = HTTP.Request() req.method = "NOTIFY" req.uri = HTTP.URI("*") - req.headers = HTTP.Headers("Server"=>"foo") + req.headers = ["Server"=>"foo"] @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1508,7 +1509,7 @@ const responses = Message[ req = HTTP.Request() req.method = "OPTIONS" req.uri = HTTP.URI("*") - req.headers = HTTP.Headers("Server"=>"foo") + req.headers = ["Server"=>"foo"] @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1516,7 +1517,7 @@ const responses = Message[ req = HTTP.Request() req.uri = HTTP.URI("/") - req.headers = HTTP.Headers("Host"=>"issue8261.com", "Connection"=>"close") + req.headers = ["Host"=>"issue8261.com", "Connection"=>"close"] @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1525,7 +1526,7 @@ const responses = Message[ req = HTTP.Request() req.method = "HEAD" req.uri = HTTP.URI("/") - req.headers = HTTP.Headers("Host"=>"issue8261.com", "Connection"=>"close", "Content-Length"=>"0") + req.headers = ["Host"=>"issue8261.com", "Connection"=>"close", "Content-Length"=>"0"] @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1542,13 +1543,13 @@ const responses = Message[ req = HTTP.Request() req.method = "POST" req.uri = HTTP.URI("/cgi-bin/process.cgi") - req.headers = HTTP.Headers("Host"=>"www.tutorialspoint.com", - "Connection"=>"Keep-Alive", - "Content-Length"=>"19", - "User-Agent"=>"Mozilla/4.0 (compatible; MSIE5.01; Windows NT)", - "Content-Type"=>"text/xml; charset=utf-8", - "Accept-Language"=>"en-us", - "Accept-Encoding"=>"gzip, deflate") + req.headers = ["User-Agent"=>"Mozilla/4.0 (compatible; MSIE5.01; Windows NT)", + "Host"=>"www.tutorialspoint.com", + "Content-Type"=>"text/xml; charset=utf-8", + "Content-Length"=>"19", + "Accept-Language"=>"en-us", + "Accept-Encoding"=>"gzip, deflate", + "Connection"=>"Keep-Alive"] req.body = HTTP.FIFOBuffer("first=Zara&last=Ali") @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1582,9 +1583,9 @@ const responses = Message[ @test HTTP.status(r) == resp.status_code @test HTTP.statustext(r) == resp.response_status @test length(HTTP.headers(r)) == resp.num_headers - @test HTTP.canonicalizeheaders(HTTP.headers(r)) == resp.headers + @test HTTP.canonicalizeheaders(HTTP.headers(r)) == Dict(resp.headers) @test String(readavailable(HTTP.body(r))) == resp.body - @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER, r) == resp.should_keep_alive + @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == resp.should_keep_alive catch e if HTTP.strict && isa(e, HTTP.ParsingError) println("HTTP.strict is enabled. ParsingError ignored.") @@ -1752,7 +1753,7 @@ const responses = Message[ @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, "GET / HTP/1.1\r\n\r\n") - r = HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n" * "Test: Düsseldorf\r\n") + r = HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n" * "Test: Düsseldorf\r\n\r\n") @test HTTP.headers(r) == Dict("Test" => "Düsseldorf") r = HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") diff --git a/test/runtests.jl b/test/runtests.jl index 3bd10deb4..484267b11 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,14 +1,14 @@ using HTTP, Base.Test @testset "HTTP" begin -# include("utils.jl"); -# include("fifobuffer.jl"); -# include("sniff.jl"); -# include("uri.jl"); -# include("cookies.jl"); -# include("parser.jl"); -# include("types.jl"); -# include("handlers.jl") -# include("client.jl"); + include("utils.jl"); + include("fifobuffer.jl"); + include("sniff.jl"); + include("uri.jl"); + include("cookies.jl"); + include("parser.jl"); + include("types.jl"); + include("handlers.jl") + include("client.jl"); include("server.jl") end; diff --git a/test/server.jl b/test/server.jl index 4a105ad93..fa3e5e96c 100644 --- a/test/server.jl +++ b/test/server.jl @@ -105,7 +105,7 @@ put!(server.in, HTTP.Nitrogen.KILL) # keep-alive vs. close: issue #81 tsk = @async HTTP.serve(HTTP.Server((req, res) -> Response("Hello\n"), STDOUT), ip"127.0.0.1", 8083) sleep(2.0) -r = HTTP.request(HTTP.Request(major=1, minor=0, uri=HTTP.URI("http://127.0.0.1:8083/"), headers=Dict("Host"=>"127.0.0.1:8083"))) +r = HTTP.request(HTTP.Request(major=1, minor=0, uri=HTTP.URI("http://127.0.0.1:8083/"), headers=["Host"=>"127.0.0.1:8083"])) @test HTTP.status(r) == 200 @test HTTP.headers(r)["Connection"] == "close" From f20fcb3ef8cf53e2196a338124371fbedaedf20e Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 27 Nov 2017 20:26:22 +1100 Subject: [PATCH 009/182] Use IOBuffer instead of Vector{UInt8} for field and value buffers in Parser. append!() is replaced by write(). Faster, less allocaiton. unsafe_string(pointer(x.buf), length(x.buf)) is replaced by String(take!(x.buf)) ``` mutable struct s1 buf::Vector{UInt8} end mutable struct s2 buf::IOBuffer end a(x::s1, s) = append!(x.buf, s) a(x::s2, s) = write(x.buf, s) r(x::s1) = unsafe_string(pointer(x.buf), length(x.buf)) r(x::s2) = String(take!(x.buf)) function go(x) inbuf = Vector{UInt8}([0]) for i = 1:100000 inbuf[1] = UInt8(i % 255) a(x, inbuf) end outbuf = String[] for i = 1:1000 push!(outbuf, r(x)) end println(length(outbuf)) end t1() = go(s1(Vector{UInt8}())) t2() = go(s2(IOBuffer())) t1() t2() println("t1, Vector...") @time t1() println("") println("t2, IOBuffer...") @time t2() ``` ``` t1, Vector... 1000 0.101289 seconds (1.12 k allocations: 95.733 MiB, 65.69% gc time) t2, IOBuffer... 1000 0.002676 seconds (2.04 k allocations: 241.281 KiB) ``` --- src/parser.jl | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 83735b3af..b81703f48 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -32,8 +32,8 @@ mutable struct Parser flags::UInt8 nread::UInt32 content_length::UInt64 - fieldbuffer::Vector{UInt8} #FIXME IOBuffer - valuebuffer::Vector{UInt8} + fieldbuffer::IOBuffer + valuebuffer::IOBuffer method::HTTP.Method major::Int16 minor::Int16 @@ -42,7 +42,7 @@ mutable struct Parser headers::Vector{Pair{String,String}} end -Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, UInt8[], UInt8[], Method(0), 0, 0, HTTP.URI(), 0, Pair{String,String}[]) +Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, Pair{String,String}[]) const DEFAULT_PARSER = Parser() @@ -53,8 +53,8 @@ function reset!(p::Parser) p.flags = 0x00 p.nread = 0x00000000 p.content_length = 0x0000000000000000 - empty!(p.fieldbuffer) - empty!(p.valuebuffer) + truncate(p.fieldbuffer, 0) + truncate(p.valuebuffer, 0) p.method = Method(0) p.major = 0 p.minor = 0 @@ -67,7 +67,7 @@ end # should we just make a copy of the byte vector for URI here? function onurlbytes(p::Parser, bytes, i, j) @debug(PARSING_DEBUG, "onurlbytes") - append!(p.valuebuffer, view(bytes, i:j)) + write(p.valuebuffer, view(bytes, i:j)) return end @@ -75,33 +75,28 @@ function onurl(p::Parser) @debug(PARSING_DEBUG, "onurl") @debug(PARSING_DEBUG, String(p.valuebuffer)) @debug(PARSING_DEBUG, p.method) - url = copy(p.valuebuffer) + url = take!(p.valuebuffer) uri = URIs.http_parser_parse_url(url, 1, length(url), p.method == CONNECT) @debug(PARSING_DEBUG, uri) p.url = uri - empty!(p.valuebuffer) return end function onheaderfieldbytes(p::Parser, bytes, i, j) @debug(PARSING_DEBUG, "onheaderfieldbytes") - append!(p.fieldbuffer, view(bytes, i:j)) + write(p.fieldbuffer, view(bytes, i:j)) return end function onheadervaluebytes(p::Parser, bytes, i, j) @debug(PARSING_DEBUG, "onheadervaluebytes") - append!(p.valuebuffer, view(bytes, i:j)) + write(p.valuebuffer, view(bytes, i:j)) return end function onheadervalue(p) @debug(PARSING_DEBUG, "onheadervalue2") - key = unsafe_string(pointer(p.fieldbuffer), length(p.fieldbuffer)) - val = unsafe_string(pointer(p.valuebuffer), length(p.valuebuffer)) - push!(p.headers, key => val) - empty!(p.fieldbuffer) - empty!(p.valuebuffer) + push!(p.headers, String(take!(p.fieldbuffer)) => String(take!(p.valuebuffer))) return end From 35dfe800047107f1ef6f0f3eb4586d4db056ee87 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 28 Nov 2017 08:49:34 +1100 Subject: [PATCH 010/182] reduce main parse! function to a single method by keeping a reference to body in the Parser struct --- src/HTTP.jl | 2 ++ src/parser.jl | 38 ++++++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 6d0e7fabf..3bac11e94 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -50,8 +50,10 @@ function __init__() end end # module +#= try HTTP.parse(HTTP.Response, "HTTP/1.1 200 OK\r\n\r\n") HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n\r\n") HTTP.get(HTTP.Client(nothing), "www.google.com") end +=# diff --git a/src/parser.jl b/src/parser.jl index b81703f48..97e19653b 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -40,9 +40,10 @@ mutable struct Parser url::HTTP.URI status::Int32 headers::Vector{Pair{String,String}} + body::Ref{FIFOBuffer} end -Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, Pair{String,String}[]) +Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, Pair{String,String}[], Ref{FIFOBuffer}()) const DEFAULT_PARSER = Parser() @@ -61,9 +62,12 @@ function reset!(p::Parser) p.url = HTTP.URI() p.status = 0 empty!(p.headers) + p.body = Ref{FIFOBuffer}() return end +isrequest(p::Parser) = p.status == 0 + # should we just make a copy of the byte vector for URI here? function onurlbytes(p::Parser, bytes, i, j) @debug(PARSING_DEBUG, "onurlbytes") @@ -100,25 +104,26 @@ function onheadervalue(p) return end -function onbody(r, maintask, bytes, i, j) +function onbody(p, maintask, bytes, i, j) @debug(PARSING_DEBUG, "onbody") @debug(PARSING_DEBUG, String(r.body)) @debug(PARSING_DEBUG, String(bytes[i:j])) len = j - i + 1 #TODO: avoid copying the bytes here? can we somehow write the bytes to a FIFOBuffer more efficiently? - nb = write(r.body, bytes, i, j) + body = p.body[] + nb = write(body, bytes, i, j) if nb < len # didn't write all available bytes if current_task() == maintask # main request function hasn't returned yet, so not safe to wait - r.body.max += len - nb - write(r.body, bytes, i + nb, j) + body.max += len - nb + write(body, bytes, i + nb, j) else while nb < len - nb += write(r.body, bytes, i + nb, j) + nb += write(body, bytes, i + nb, j) end end end - @debug(PARSING_DEBUG, String(r.body)) + @debug(PARSING_DEBUG, String(body)) return end @@ -157,7 +162,8 @@ function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); method::Method=GET, maintask::Task=current_task())::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} - err, headerscomplete, messagecomplete, upgrade = parse!(r, parser, bytes, len, method, maintask) + parser.body[] = r.body + err, headerscomplete, messagecomplete, upgrade = parse!(parser, bytes, len, method, maintask) if headerscomplete && isempty(r.headers) for (k, v) in parser.headers @@ -175,7 +181,7 @@ function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); return err, headerscomplete, messagecomplete, upgrade end -function parse!(r, parser, bytes, len, method, maintask)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} +function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method, maintask::Task)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} p_state = parser.state status_mark = url_mark = header_field_mark = header_field_end_mark = header_value_mark = body_mark = 0 errno = HPE_OK @@ -1034,9 +1040,9 @@ function parse!(r, parser, bytes, len, method, maintask)::Tuple{ParsingErrorCode #= Set this here so that on_headers_complete() callbacks can see it =# @debug(PARSING_DEBUG, "checking for upgrade...") if (parser.flags & F_UPGRADE > 0) && (parser.flags & F_CONNECTION_UPGRADE > 0) - upgrade = typeof(r) == Request || parser.status == 101 + upgrade = isrequest(parser) || parser.status == 101 else - upgrade = typeof(r) == Request && parser.method == CONNECT + upgrade = isrequest(parser) && parser.method == CONNECT end @debug(PARSING_DEBUG, upgrade) #= Here we call the headers_complete callback. This is somewhat @@ -1065,7 +1071,7 @@ function parse!(r, parser, bytes, len, method, maintask)::Tuple{ParsingErrorCode hasBody = parser.flags & F_CHUNKED > 0 || (parser.content_length > 0 && parser.content_length != ULLONG_MAX) - if upgrade && ((typeof(r) == Request && parser.method == CONNECT) || + if upgrade && ((isrequest(parser) && parser.method == CONNECT) || (parser.flags & F_SKIPBODY) > 0 || !hasBody) #= Exit, the rest of the message is in a different protocol. =# p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) @@ -1136,7 +1142,7 @@ function parse!(r, parser, bytes, len, method, maintask)::Tuple{ParsingErrorCode * important for applications, but let's keep it for now. =# @debug(PARSING_DEBUG, "this onbody 1") - onbody(r, maintask, bytes, body_mark, p) + onbody(parser, maintask, bytes, body_mark, p) body_mark = 0 @goto reexecute end @@ -1244,7 +1250,7 @@ function parse!(r, parser, bytes, len, method, maintask)::Tuple{ParsingErrorCode @strictcheck(ch != CR) p_state = s_chunk_data_done @debug(PARSING_DEBUG, "this onbody 2") - body_mark > 0 && onbody(r, maintask, bytes, body_mark, p - 1) + body_mark > 0 && onbody(parser, maintask, bytes, body_mark, p - 1) body_mark = 0 elseif p_state == s_chunk_data_done @@ -1284,7 +1290,7 @@ function parse!(r, parser, bytes, len, method, maintask)::Tuple{ParsingErrorCode header_value_mark > 0 && onheadervaluebytes(parser, bytes, header_value_mark, min(len, p)) url_mark > 0 && onurlbytes(parser, bytes, url_mark, min(len, p)) @debug(PARSING_DEBUG, "this onbody 3") - body_mark > 0 && onbody(r, maintask, bytes, body_mark, min(len, p - 1)) + body_mark > 0 && onbody(parser, maintask, bytes, body_mark, min(len, p - 1)) parser.state = p_state @debug(PARSING_DEBUG, "exiting maybe unfinished...") @@ -1309,7 +1315,7 @@ end #= Does the parser need to see an EOF to find the end of the message? =# function http_message_needs_eof(parser) #= See RFC 2616 section 4.4 =# - if (parser.status == 0 || # Request + if (isrequest(parser) || div(parser.status, 100) == 1 || #= 1xx e.g. Continue =# parser.status == 204 || #= No Content =# parser.status == 304 || #= Not Modified =# From ce9d6aeacce8c18668fdeccae9166a3be615bc79 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 28 Nov 2017 12:47:08 +1100 Subject: [PATCH 011/182] Remove *_mark from Parser. Explicitly handle data within main parse loop instead of using *_mark to clean up loose ends. Number of places that callbacks are called is reduced. Add `while p <= len` inner loop for URL parsing (follows pattern used for header field and value parsing) Remove on*bytes functions, just `write()` to valuebuffer or fieldbuffer as needed. --- src/consts.jl | 3 +- src/parser.jl | 175 +++++++++++++++-------------------------------- src/urlparser.jl | 4 +- test/parser.jl | 2 +- 4 files changed, 62 insertions(+), 122 deletions(-) diff --git a/src/consts.jl b/src/consts.jl index b3303e46c..ded8156c5 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -254,7 +254,8 @@ const ParsingErrorCodeMap = Dict( ,es_start_req=18 ,es_req_method=19 ,es_req_spaces_before_url=20 - ,es_req_schema=21 + ,es_req_url_start=21 + ,es_req_schema=22 ,es_req_schema_slash ,es_req_schema_slash_slash ,es_req_server_start diff --git a/src/parser.jl b/src/parser.jl index 97e19653b..b5f2f4ec9 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -69,12 +69,6 @@ end isrequest(p::Parser) = p.status == 0 # should we just make a copy of the byte vector for URI here? -function onurlbytes(p::Parser, bytes, i, j) - @debug(PARSING_DEBUG, "onurlbytes") - write(p.valuebuffer, view(bytes, i:j)) - return -end - function onurl(p::Parser) @debug(PARSING_DEBUG, "onurl") @debug(PARSING_DEBUG, String(p.valuebuffer)) @@ -86,27 +80,17 @@ function onurl(p::Parser) return end -function onheaderfieldbytes(p::Parser, bytes, i, j) - @debug(PARSING_DEBUG, "onheaderfieldbytes") - write(p.fieldbuffer, view(bytes, i:j)) - return -end - -function onheadervaluebytes(p::Parser, bytes, i, j) - @debug(PARSING_DEBUG, "onheadervaluebytes") - write(p.valuebuffer, view(bytes, i:j)) - return -end - function onheadervalue(p) @debug(PARSING_DEBUG, "onheadervalue2") - push!(p.headers, String(take!(p.fieldbuffer)) => String(take!(p.valuebuffer))) + v = String(take!(p.fieldbuffer)) => String(take!(p.valuebuffer)) + @debug(PARSING_DEBUG, v) + push!(p.headers, v) return end function onbody(p, maintask, bytes, i, j) @debug(PARSING_DEBUG, "onbody") - @debug(PARSING_DEBUG, String(r.body)) + #@debug(PARSING_DEBUG, String(p.body[])) @debug(PARSING_DEBUG, String(bytes[i:j])) len = j - i + 1 #TODO: avoid copying the bytes here? can we somehow write the bytes to a FIFOBuffer more efficiently? @@ -183,7 +167,6 @@ end function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method, maintask::Task)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} p_state = parser.state - status_mark = url_mark = header_field_mark = header_field_end_mark = header_value_mark = body_mark = 0 errno = HPE_OK upgrade = headersdone = false @debug(PARSING_DEBUG, len) @@ -200,22 +183,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end end - if p_state == s_header_field - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - header_field_mark = header_field_end_mark = 1 - end - if p_state == s_header_value - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - header_value_mark = 1 - end - if @anyeq(p_state, s_req_path, s_req_schema, s_req_schema_slash, s_req_schema_slash_slash, - s_req_server_start, s_req_server, s_req_server_with_at, - s_req_query_string_start, s_req_query_string, s_req_fragment, - s_req_fragment_start) - url_mark = 1 - elseif p_state == s_res_status - status_mark = 1 - end p = 1 old_p = 0 while p <= len @@ -368,7 +335,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif ch == LF p_state = s_header_field_start else - status_mark = p p_state = s_res_status parser.index = 1 end @@ -377,12 +343,8 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == CR p_state = s_res_line_almost_done - parser.state = p_state - status_mark = 0 elseif ch == LF p_state = s_header_field_start - parser.state = p_state - status_mark = 0 end elseif p_state == s_res_line_almost_done @@ -500,37 +462,52 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_req_spaces_before_url @debug(PARSING_DEBUG, ParsingStateCode(p_state)) ch == ' ' && @goto breakout - url_mark = p if parser.method == CONNECT p_state = s_req_server_start + else + p_state = s_req_url_start + end + @goto reexecute + + elseif @anyeq(p_state, s_req_url_start, + s_req_server_start, + s_req_server, + s_req_server_with_at, + s_req_path, + s_req_query_string_start, + s_req_query_string, + s_req_fragment_start, + s_req_fragment, + s_req_schema, + s_req_schema_slash, + s_req_schema_slash_slash) + start = p + while p <= len + @inbounds ch = Char(bytes[p]) + if ch in (' ', CR, LF) + @errorif(@anyeq(p_state, s_req_schema, s_req_schema_slash, + s_req_schema_slash_slash, + s_req_server_start), + HPE_INVALID_URL) + break + end + p_state = URIs.parseurlchar(p_state, ch, strict) + @errorif(p_state == s_dead, HPE_INVALID_URL) + p += 1 end - p_state = URIs.parseurlchar(p_state, ch, strict) - @errorif(p_state == s_dead, HPE_INVALID_URL) - elseif @anyeq(p_state, s_req_schema, s_req_schema_slash, s_req_schema_slash_slash, s_req_server_start) - @errorif(ch in (' ', CR, LF), HPE_INVALID_URL) - p_state = URIs.parseurlchar(p_state, ch, strict) - @errorif(p_state == s_dead, HPE_INVALID_URL) + parser.nread += (p - start) + + write(parser.valuebuffer, view(bytes, start:p-1)) - elseif @anyeq(p_state, s_req_server, s_req_server_with_at, s_req_path, s_req_query_string_start, - s_req_query_string, s_req_fragment_start, s_req_fragment) if ch == ' ' p_state = s_req_http_start - parser.state = p_state - onurlbytes(parser, bytes, url_mark, p-1) onurl(parser) - url_mark = 0 elseif ch in (CR, LF) parser.major = Int16(0) parser.minor = Int16(9) p_state = ifelse(ch == CR, s_req_line_almost_done, s_header_field_start) - parser.state = p_state - onurlbytes(parser, bytes, url_mark, p-1) onurl(parser) - url_mark = 0 - else - p_state = URIs.parseurlchar(p_state, ch, strict) - @errorif(p_state == s_dead, HPE_INVALID_URL) end elseif p_state == s_req_http_start @@ -622,7 +599,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method else c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] @errorif(c == Char(0), HPE_INVALID_HEADER_TOKEN) - header_field_mark = header_field_end_mark = p parser.index = 1 p_state = s_header_field @@ -637,6 +613,8 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method else parser.header_state = h_general end + + write(parser.fieldbuffer, bytes[p]) end elseif p_state == s_header_field @@ -644,7 +622,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) start = p while p <= len - ch = Char(bytes[p]) + @inbounds ch = Char(bytes[p]) @debug(PARSING_DEBUG, Base.escape_string(string(ch))) c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] if c == Char(0) @@ -733,14 +711,10 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method if ch == ':' p_state = s_header_value_discard_ws parser.state = p_state - header_field_end_mark = p - if p > header_field_mark - onheaderfieldbytes(parser, bytes, header_field_mark, p - 1) - end - header_field_mark = 0 else @assert tokens[Int(ch)+1] != Char(0) || !strict && ch == ' ' end + write(parser.fieldbuffer, view(bytes, start:p-1)) elseif p_state == s_header_value_discard_ws @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -758,7 +732,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_header_value_start @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @label s_header_value_start_label - header_value_mark = p p_state = s_header_value parser.index = 1 c = lower(ch) @@ -793,6 +766,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method else parser.header_state = h_general end + write(parser.valuebuffer, bytes[p]) elseif p_state == s_header_value @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -803,25 +777,9 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, Base.escape_string(string('\'', ch, '\''))) @debug(PARSING_DEBUG, strict) @debug(PARSING_DEBUG, isheaderchar(ch)) - if ch == CR - p_state = s_header_almost_done - parser.header_state = h - parser.state = p_state - @debug(PARSING_DEBUG, "onheadervalue 1") - onheadervaluebytes(parser, bytes, header_value_mark, p - 1) - header_value_mark = 0 - onheadervalue(parser) + if ch in (CR, LF) + p_state = ch == CR ? s_header_almost_done : s_header_value_lws break - elseif ch == LF - p_state = s_header_almost_done - parser.nread += (p - start) - parser.header_state = h - parser.state = p_state - @debug(PARSING_DEBUG, "onheadervalue 2") - onheadervaluebytes(parser, bytes, header_value_mark, p - 1) - header_value_mark = 0 - onheadervalue(parser) - @goto reexecute elseif strict && !isheaderchar(ch) @err(HPE_INVALID_HEADER_TOKEN) end @@ -966,6 +924,12 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method parser.header_state = h parser.nread += (p - start) + write(parser.valuebuffer, view(bytes, start:p-1)) + + if p_state != s_header_value + onheadervalue(parser) + end + elseif p_state == s_header_almost_done @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(ch != LF, HPE_LF_EXPECTED) @@ -1012,12 +976,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end #= header value was empty =# - header_value_mark = p p_state = s_header_field_start - parser.state = p_state - @debug(PARSING_DEBUG, "onheadervalue 3") - onheadervaluebytes(parser, bytes, header_value_mark, p - 1) - header_value_mark = 0 onheadervalue(parser) @goto reexecute end @@ -1120,14 +1079,15 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method to_read = UInt64(min(parser.content_length, len - p + 1)) assert(parser.content_length != 0 && parser.content_length != ULLONG_MAX) + onbody(parser, maintask, bytes, p, p + to_read - 1) + #= The difference between advancing content_length and p is because * the latter will automaticaly advance on the next loop iteration. * Further, if content_length ends up at 0, we want to see the last * byte again for our message complete callback. =# - body_mark = p parser.content_length -= to_read - p += Int(to_read) - 1 + p += to_read - 1 if parser.content_length == 0 p_state = s_message_done @@ -1141,28 +1101,24 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method * complete-on-length. It's not clear that this distinction is * important for applications, but let's keep it for now. =# - @debug(PARSING_DEBUG, "this onbody 1") - onbody(parser, maintask, bytes, body_mark, p) - body_mark = 0 @goto reexecute end #= read until EOF =# elseif p_state == s_body_identity_eof @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - body_mark = p + onbody(parser, maintask, bytes, p, len) p = len elseif p_state == s_message_done @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - # p_state = ifelse(http_should_keep_alive(parser, r), start_state, s_dead) - parser.state = p_state @debug(PARSING_DEBUG, "this 5") if upgrade #= Exit, the rest of the message is in a different protocol. =# parser.state = p_state return errno, true, true, String(bytes[p+1:end]) end + p = len elseif p_state == s_chunk_size_start @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -1232,10 +1188,11 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method assert(parser.flags & F_CHUNKED > 0) assert(parser.content_length != 0 && parser.content_length != ULLONG_MAX) + onbody(parser, maintask, bytes, p, p + to_read - 1) + #= See the explanation in s_body_identity for why the content * length and data pointers are managed this way. =# - body_mark = p parser.content_length -= to_read p += Int(to_read) - 1 @@ -1249,9 +1206,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method assert(parser.content_length == 0) @strictcheck(ch != CR) p_state = s_chunk_data_done - @debug(PARSING_DEBUG, "this onbody 2") - body_mark > 0 && onbody(parser, maintask, bytes, body_mark, p - 1) - body_mark = 0 elseif p_state == s_chunk_data_done @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -1277,21 +1231,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method * value that's in-bounds). =# - assert(((header_field_mark > 0 ? 1 : 0) + - (header_value_mark > 0 ? 1 : 0) + - (url_mark > 0 ? 1 : 0) + - (body_mark > 0 ? 1 : 0) + - (status_mark > 0 ? 1 : 0)) <= 1) - - header_field_mark > 0 && onheaderfieldbytes(parser, bytes, header_field_mark, min(len, p)) - @debug(PARSING_DEBUG, "onheadervalue 4") - @debug(PARSING_DEBUG, len) - @debug(PARSING_DEBUG, p) - header_value_mark > 0 && onheadervaluebytes(parser, bytes, header_value_mark, min(len, p)) - url_mark > 0 && onurlbytes(parser, bytes, url_mark, min(len, p)) - @debug(PARSING_DEBUG, "this onbody 3") - body_mark > 0 && onbody(parser, maintask, bytes, body_mark, min(len, p - 1)) - parser.state = p_state @debug(PARSING_DEBUG, "exiting maybe unfinished...") @debug(PARSING_DEBUG, ParsingStateCode(p_state)) diff --git a/src/urlparser.jl b/src/urlparser.jl index 76c3fb98c..48408431f 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -48,7 +48,7 @@ function parseurlchar(s, ch::Char, strict::Bool) @anyeq(ch, ' ', '\r', '\n') && return s_dead strict && (ch == '\t' || ch == '\f') && return s_dead - if s == s_req_spaces_before_url + if s == s_req_spaces_before_url || s == s_req_url_start (ch == '/' || ch == '*') && return s_req_path isalpha(ch) && return s_req_schema elseif s == s_req_schema @@ -250,4 +250,4 @@ function http_parser_parse_url(buf, startind=1, buflen=length(buf), isconnect::B offsets[UF_QUERY], offsets[UF_FRAGMENT], offsets[UF_USERINFO])) -end \ No newline at end of file +end diff --git a/test/parser.jl b/test/parser.jl index 12deedb0d..e52972a57 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -40,7 +40,7 @@ const requests = Message[ Message(name= "curl get" ,raw= "GET /test HTTP/1.1\r\n" * "User-Agent: curl/7.18.0 (i486-pc-linux-gnu) libcurl/7.18.0 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.1\r\n" * - "Host: 0.0.0.0=5000\r\n" * + "Host:0.0.0.0=5000\r\n" * # missing space after colon "Accept: */*\r\n" * "\r\n" ,should_keep_alive= true From b1eebe895662d212ba23a89460a233ec45de808f Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 28 Nov 2017 13:35:17 +1100 Subject: [PATCH 012/182] Remove (mostly) unused Parser.nread field Purpose is not clear. Wasn't working anyway in some cases in origin master branch. e.g. In the "Foo: F\01ailure" test case nread was buffer length + 1. --- src/parser.jl | 20 ++------------------ test/parser.jl | 19 +++++++++---------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index b5f2f4ec9..53b44605c 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -30,7 +30,6 @@ mutable struct Parser header_state::UInt8 index::UInt8 flags::UInt8 - nread::UInt32 content_length::UInt64 fieldbuffer::IOBuffer valuebuffer::IOBuffer @@ -43,7 +42,7 @@ mutable struct Parser body::Ref{FIFOBuffer} end -Parser() = Parser(start_state, 0x00, 0, 0, 0, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, Pair{String,String}[], Ref{FIFOBuffer}()) +Parser() = Parser(start_state, 0x00, 0, 0, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, Pair{String,String}[], Ref{FIFOBuffer}()) const DEFAULT_PARSER = Parser() @@ -52,7 +51,6 @@ function reset!(p::Parser) p.header_state = 0x00 p.index = 0x00 p.flags = 0x00 - p.nread = 0x00000000 p.content_length = 0x0000000000000000 truncate(p.fieldbuffer, 0) truncate(p.valuebuffer, 0) @@ -135,6 +133,7 @@ function parse(T::Type{<:Union{Request, Response}}, str; r.major = DEFAULT_PARSER.major r.minor = DEFAULT_PARSER.minor err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) + !headerscomplete && throw(ParsingError("error parsing $T: headers incomplete")) if upgrade != nothing extra[] = upgrade end @@ -192,10 +191,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, "top of main for-loop") @debug(PARSING_DEBUG, Base.escape_string(string(ch))) - if p_state <= s_headers_done - parser.nread += 1 - end - @label reexecute if p_state == s_dead @@ -496,8 +491,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p += 1 end - parser.nread += (p - start) - write(parser.valuebuffer, view(bytes, start:p-1)) if ch == ' ' @@ -706,8 +699,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p += 1 end - parser.nread += (p - start) - if ch == ':' p_state = s_header_value_discard_ws parser.state = p_state @@ -922,7 +913,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p += 1 end parser.header_state = h - parser.nread += (p - start) write(parser.valuebuffer, view(bytes, start:p-1)) @@ -1026,8 +1016,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != LF) - parser.nread = UInt32(0) - hasBody = parser.flags & F_CHUNKED > 0 || (parser.content_length > 0 && parser.content_length != ULLONG_MAX) if upgrade && ((isrequest(parser) && parser.method == CONNECT) || @@ -1122,7 +1110,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_chunk_size_start @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - assert(parser.nread == 1) assert(parser.flags & F_CHUNKED > 0) unhex_val = unhex[Int(ch)+1] @@ -1172,8 +1159,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method assert(parser.flags & F_CHUNKED > 0) @strictcheck(ch != LF) - parser.nread = 0 - if parser.content_length == 0 parser.flags |= F_TRAILING p_state = s_header_field_start @@ -1211,7 +1196,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) assert(parser.flags & F_CHUNKED > 0) @strictcheck(ch != LF) - parser.nread = 0 p_state = s_chunk_size_start else diff --git a/test/parser.jl b/test/parser.jl index e52972a57..2beea8d16 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1597,38 +1597,38 @@ const responses = Message[ end @testset "HTTP.parse errors" begin - reqstr = "GET / HTTP/1.1\r\n" * "Foo: F\01ailure" + reqstr = "GET / HTTP/1.1\r\n" * "Foo: F\01ailure\r\n\r\n" HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) if !HTTP.strict r = HTTP.parse(HTTP.Request, reqstr) @test HTTP.method(r) == HTTP.GET @test HTTP.uri(r) == HTTP.URI("/") - @test length(HTTP.headers(r)) == 0 + @test length(HTTP.headers(r)) == 1 end - reqstr = "GET / HTTP/1.1\r\n" * "Foo: B\02ar" + reqstr = "GET / HTTP/1.1\r\n" * "Foo: B\02ar\r\n\r\n" HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) if !HTTP.strict r = HTTP.parse(HTTP.Request, reqstr) @test HTTP.method(r) == HTTP.GET @test HTTP.uri(r) == HTTP.URI("/") - @test length(HTTP.headers(r)) == 0 + @test length(HTTP.headers(r)) == 1 end - respstr = "HTTP/1.1 200 OK\r\n" * "Foo: F\01ailure" + respstr = "HTTP/1.1 200 OK\r\n" * "Foo: F\01ailure\r\n\r\n" HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) if !HTTP.strict r = HTTP.parse(HTTP.Response, respstr) @test HTTP.status(r) == 200 - @test length(HTTP.headers(r)) == 0 + @test length(HTTP.headers(r)) == 1 end - respstr = "HTTP/1.1 200 OK\r\n" * "Foo: B\02ar" + respstr = "HTTP/1.1 200 OK\r\n" * "Foo: B\02ar\r\n\r\n" HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) if !HTTP.strict r = HTTP.parse(HTTP.Response, respstr) @test HTTP.status(r) == 200 - @test length(HTTP.headers(r)) == 0 + @test length(HTTP.headers(r)) == 1 end reqstr = "GET / HTTP/1.1\r\n" * "Fo@: Failure" @@ -1674,8 +1674,7 @@ const responses = Message[ end buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n" - r = HTTP.parse(HTTP.Request, buf) - @test HTTP.DEFAULT_PARSER.nread == length(buf) + @test_throws HTTP.ParsingError r = HTTP.parse(HTTP.Request, buf) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "1844674407370955160" * "\r\n\r\n" r = HTTP.parse(HTTP.Response, respstr) From 7e0d643be91030425a901e6a996047a77bee153f Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 28 Nov 2017 13:50:38 +1100 Subject: [PATCH 013/182] Explicitly return HPE_OK (not errno) in Parser in places where there is no error. Remove redundant write-back of parser.state. --- src/parser.jl | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 53b44605c..950859da1 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -166,13 +166,12 @@ end function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method, maintask::Task)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} p_state = parser.state - errno = HPE_OK + errno = HPE_UNKNOWN upgrade = headersdone = false @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if len == 0 if p_state == s_body_identity_eof - parser.state = p_state @debug(PARSING_DEBUG, "this 6") return HPE_OK, true, true, nothing elseif @anyeq(p_state, s_dead, s_start_req_or_res, s_start_res, s_start_req) @@ -210,7 +209,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method if ch == 'H' p_state = s_res_or_resp_H - parser.state = p_state else p_state = s_start_req @goto reexecute @@ -237,7 +235,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method else @err HPE_INVALID_CONSTANT end - parser.state = p_state elseif p_state == s_res_H @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -391,7 +388,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @err(HPE_INVALID_METHOD) end p_state = s_req_method - parser.state = p_state elseif p_state == s_req_method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -701,7 +697,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method if ch == ':' p_state = s_header_value_discard_ws - parser.state = p_state else @assert tokens[Int(ch)+1] != Char(0) || !strict && ch == ' ' end @@ -1024,14 +1019,14 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 1") - return errno, true, true, String(bytes[p+1:end]) + return HPE_OK, true, true, String(bytes[p+1:end]) end if parser.flags & F_SKIPBODY > 0 p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 2") - return errno, true, true, nothing + return HPE_OK, true, true, nothing elseif parser.flags & F_CHUNKED > 0 #= chunked encoding - ignore Content-Length header =# p_state = s_chunk_size_start @@ -1041,7 +1036,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 3") - return errno, true, true, nothing + return HPE_OK, true, true, nothing elseif parser.content_length != ULLONG_MAX #= Content-Length header given and non-zero =# p_state = s_body_identity @@ -1052,8 +1047,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state @debug(PARSING_DEBUG, "this 4") - #return errno, true, true, String(bytes[p+1:end]) - return errno, true, true, p >= len ? nothing : String(bytes[p:end]) + return HPE_OK, true, true, p >= len ? nothing : String(bytes[p:end]) else #= Read body until EOF =# p_state = s_body_identity_eof @@ -1104,7 +1098,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method if upgrade #= Exit, the rest of the message is in a different protocol. =# parser.state = p_state - return errno, true, true, String(bytes[p+1:end]) + return HPE_OK, true, true, String(bytes[p+1:end]) end p = len @@ -1221,13 +1215,9 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method b = p_state == start_state || p_state == s_dead he = b | (headersdone || p_state >= s_headers_done) m = b | (p_state >= s_message_done) - return errno, he, m, p >= len ? nothing : String(bytes[p:end]) + return HPE_OK, he, m, p >= len ? nothing : String(bytes[p:end]) @label error - if errno == HPE_OK - errno = HPE_UNKNOWN - end - parser.state = s_start_req_or_res parser.header_state = 0x00 @debug(PARSING_DEBUG, "exiting due to error...") From 2bf357eb0bd009db7fc0c61f31dcaeea3ca5ad1d Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 28 Nov 2017 15:06:08 +1100 Subject: [PATCH 014/182] Clean up gotos and main loop condition in parser --- src/parser.jl | 98 ++++++++++++++++++++++++++++----------------------- src/utils.jl | 17 --------- 2 files changed, 53 insertions(+), 62 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 950859da1..612633e21 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -164,6 +164,23 @@ function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); return err, headerscomplete, messagecomplete, upgrade end +macro errorif(cond, err) + return esc(quote + $cond && @err($err) + end) +end + +macro err(e) + return esc(quote + errno = $e + @goto error + end) +end + +macro strictcheck(cond) + return esc(:(strict && @errorif($cond, HPE_STRICT))) +end + function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method, maintask::Task)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} p_state = parser.state errno = HPE_UNKNOWN @@ -181,29 +198,24 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end end - p = 1 - old_p = 0 - while p <= len - @assert p > old_p - old_p = p + p = 0 + while p < len + p += 1 @inbounds ch = Char(bytes[p]) @debug(PARSING_DEBUG, "top of main for-loop") @debug(PARSING_DEBUG, Base.escape_string(string(ch))) - @label reexecute - if p_state == s_dead @debug(PARSING_DEBUG, ParsingStateCode(p_state)) #= this state is used after a 'Connection: close' message # the parser will error out if it reads another message =# - (ch == CR || ch == LF) && @goto breakout - @err HPE_CLOSED_CONNECTION + @errorif(ch != CR && ch != LF, HPE_CLOSED_CONNECTION) elseif p_state == s_start_req_or_res @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - (ch == CR || ch == LF) && @goto breakout + (ch == CR || ch == LF) && continue parser.flags = 0 parser.content_length = ULLONG_MAX @@ -211,7 +223,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p_state = s_res_or_resp_H else p_state = s_start_req - @goto reexecute + p -= 1 end elseif p_state == s_res_or_resp_H @@ -267,7 +279,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == '.' p_state = s_res_first_http_minor - @goto breakout + continue end @errorif(!isnum(ch), HPE_INVALID_VERSION) parser.major *= Int16(10) @@ -286,7 +298,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == ' ' p_state = s_res_first_status_code - @goto breakout + continue end @errorif(!isnum(ch), HPE_INVALID_VERSION) parser.minor *= Int16(10) @@ -296,7 +308,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_res_first_status_code @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if !isnum(ch) - ch == ' ' && @goto breakout + ch == ' ' && continue @err(HPE_INVALID_STATUS) end parser.status = Int32(ch - '0') @@ -346,7 +358,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_start_req @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - (ch == CR || ch == LF) && @goto breakout + (ch == CR || ch == LF) && continue parser.flags = 0 parser.content_length = ULLONG_MAX @errorif(!isalpha(ch), HPE_INVALID_METHOD) @@ -452,13 +464,13 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_req_spaces_before_url @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - ch == ' ' && @goto breakout + ch == ' ' && continue if parser.method == CONNECT p_state = s_req_server_start else p_state = s_req_url_start end - @goto reexecute + p -= 1 elseif @anyeq(p_state, s_req_url_start, s_req_server_start, @@ -584,7 +596,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= they might be just sending \n instead of \r\n so this would be * the second \n to denote the end of headers=# p_state = s_headers_almost_done - @goto reexecute + p -= 1 else c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] @errorif(c == Char(0), HPE_INVALID_HEADER_TOKEN) @@ -704,20 +716,19 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_header_value_discard_ws @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - (ch == ' ' || ch == '\t') && @goto breakout + (ch == ' ' || ch == '\t') && continue if ch == CR p_state = s_header_value_discard_ws_almost_done - @goto breakout + continue end if ch == LF p_state = s_header_value_discard_lws - @goto breakout + continue end - @goto s_header_value_start_label - #= FALLTHROUGH =# + p_state = s_header_value_start + p -= 1 elseif p_state == s_header_value_start @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - @label s_header_value_start_label p_state = s_header_value parser.index = 1 c = lower(ch) @@ -922,24 +933,23 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_header_value_lws @debug(PARSING_DEBUG, ParsingStateCode(p_state)) + p -= 1 if ch == ' ' || ch == '\t' p_state = s_header_value_start - @goto reexecute - end - #= finished the header =# - if parser.header_state == h_connection_keep_alive - parser.flags |= F_CONNECTION_KEEP_ALIVE - elseif parser.header_state == h_connection_close - parser.flags |= F_CONNECTION_CLOSE - elseif parser.header_state == h_transfer_encoding_chunked - parser.flags |= F_CHUNKED - elseif parser.header_state == h_connection_upgrade - parser.flags |= F_CONNECTION_UPGRADE + else + #= finished the header =# + if parser.header_state == h_connection_keep_alive + parser.flags |= F_CONNECTION_KEEP_ALIVE + elseif parser.header_state == h_connection_close + parser.flags |= F_CONNECTION_CLOSE + elseif parser.header_state == h_transfer_encoding_chunked + parser.flags |= F_CHUNKED + elseif parser.header_state == h_connection_upgrade + parser.flags |= F_CONNECTION_UPGRADE + end + p_state = s_header_field_start end - p_state = s_header_field_start - @goto reexecute - elseif p_state == s_header_value_discard_ws_almost_done @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != LF) @@ -963,16 +973,17 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= header value was empty =# p_state = s_header_field_start onheadervalue(parser) - @goto reexecute + p -= 1 end elseif p_state == s_headers_almost_done @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != LF) + p -= 1 if (parser.flags & F_TRAILING) > 0 #= End of a chunked request =# p_state = s_message_done - @goto reexecute + continue end #= Cannot use chunked encoding and a content-length header together @@ -1005,7 +1016,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif method == CONNECT upgrade = true end - @goto reexecute elseif p_state == s_headers_done @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @@ -1083,7 +1093,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method * complete-on-length. It's not clear that this distinction is * important for applications, but let's keep it for now. =# - @goto reexecute + p -= 1 end #= read until EOF =# @@ -1123,7 +1133,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method if unhex_val == -1 if ch == ';' || ch == ' ' p_state = s_chunk_parameters - @goto breakout + continue end @err(HPE_INVALID_CHUNK_SIZE) end @@ -1195,8 +1205,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method else error("unhandled state") end - @label breakout - p += 1 end #= Run callbacks for any marks that we have leftover after we ran our of diff --git a/src/utils.jl b/src/utils.jl index 1b5d7957b..4c34b8dee 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -143,23 +143,6 @@ macro shifted(meth, i, char) return esc(:(Int($meth) << Int(16) | Int($i) << Int(8) | Int($char))) end -macro errorif(cond, err) - return esc(quote - $cond && @err($err) - end) -end - -macro err(e) - return esc(quote - errno = $e - @goto error - end) -end - -macro strictcheck(cond) - return esc(:(strict && @errorif($cond, HPE_STRICT))) -end - # ensure the first character and subsequent characters that follow a '-' are uppercase function tocameldash!(s::String) const toUpper = UInt8('A') - UInt8('a') From 602d7ed207a41f7de97ccd1ffb8492cad008fdcc Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 28 Nov 2017 15:33:12 +1100 Subject: [PATCH 015/182] Replace @debug(PARSING_DEBUG, ParsingStateCode(p_state)) in every ifelse with single @debug at top of main loop. --- src/parser.jl | 79 ++++----------------------------------------------- 1 file changed, 6 insertions(+), 73 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 612633e21..e4ac1fc41 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -79,7 +79,7 @@ function onurl(p::Parser) end function onheadervalue(p) - @debug(PARSING_DEBUG, "onheadervalue2") + @debug(PARSING_DEBUG, "onheadervalue") v = String(take!(p.fieldbuffer)) => String(take!(p.valuebuffer)) @debug(PARSING_DEBUG, v) push!(p.headers, v) @@ -182,6 +182,7 @@ macro strictcheck(cond) end function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method, maintask::Task)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} + @debug(PARSING_DEBUG, "parse!") p_state = parser.state errno = HPE_UNKNOWN upgrade = headersdone = false @@ -189,7 +190,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if len == 0 if p_state == s_body_identity_eof - @debug(PARSING_DEBUG, "this 6") return HPE_OK, true, true, nothing elseif @anyeq(p_state, s_dead, s_start_req_or_res, s_start_res, s_start_req) return HPE_OK, false, false, nothing @@ -200,21 +200,19 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p = 0 while p < len + @debug(PARSING_DEBUG, "top of while($p < $len)") + @debug(PARSING_DEBUG, ParsingStateCode(p_state)) p += 1 @inbounds ch = Char(bytes[p]) - @debug(PARSING_DEBUG, "top of main for-loop") @debug(PARSING_DEBUG, Base.escape_string(string(ch))) if p_state == s_dead - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) #= this state is used after a 'Connection: close' message # the parser will error out if it reads another message =# @errorif(ch != CR && ch != LF, HPE_CLOSED_CONNECTION) elseif p_state == s_start_req_or_res - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - (ch == CR || ch == LF) && continue parser.flags = 0 parser.content_length = ULLONG_MAX @@ -227,7 +225,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_res_or_resp_H - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == 'T' p_state = s_res_HT else @@ -238,7 +235,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_start_res - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) parser.flags = 0 parser.content_length = ULLONG_MAX if ch == 'H' @@ -249,34 +245,28 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_res_H - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != 'T') p_state = s_res_HT elseif p_state == s_res_HT - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != 'T') p_state = s_res_HTT elseif p_state == s_res_HTT - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != 'P') p_state = s_res_HTTP elseif p_state == s_res_HTTP - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != '/') p_state = s_res_first_http_major elseif p_state == s_res_first_http_major - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(!isnum(ch), HPE_INVALID_VERSION) parser.major = Int16(ch - '0') p_state = s_res_http_major #= major HTTP version or dot =# elseif p_state == s_res_http_major - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == '.' p_state = s_res_first_http_minor continue @@ -288,14 +278,12 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= first digit of minor HTTP version =# elseif p_state == s_res_first_http_minor - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(!isnum(ch), HPE_INVALID_VERSION) parser.minor = Int16(ch - '0') p_state = s_res_http_minor #= minor HTTP version or end of request line =# elseif p_state == s_res_http_minor - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == ' ' p_state = s_res_first_status_code continue @@ -306,7 +294,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @errorif(parser.minor > 999, HPE_INVALID_VERSION) elseif p_state == s_res_first_status_code - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if !isnum(ch) ch == ' ' && continue @err(HPE_INVALID_STATUS) @@ -315,7 +302,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p_state = s_res_status_code elseif p_state == s_res_status_code - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if !isnum(ch) if ch == ' ' p_state = s_res_status_start @@ -333,7 +319,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_res_status_start - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == CR p_state = s_res_line_almost_done elseif ch == LF @@ -344,7 +329,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_res_status - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == CR p_state = s_res_line_almost_done elseif ch == LF @@ -352,12 +336,10 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_res_line_almost_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != LF) p_state = s_header_field_start elseif p_state == s_start_req - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) (ch == CR || ch == LF) && continue parser.flags = 0 parser.content_length = ULLONG_MAX @@ -402,7 +384,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p_state = s_req_method elseif p_state == s_req_method - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) matcher = string(parser.method) @debug(PARSING_DEBUG, matcher) @debug(PARSING_DEBUG, parser.index) @@ -463,7 +444,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, parser.index) elseif p_state == s_req_spaces_before_url - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) ch == ' ' && continue if parser.method == CONNECT p_state = s_req_server_start @@ -512,7 +492,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_req_http_start - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == 'H' p_state = s_req_http_H elseif ch == ' ' @@ -521,35 +500,29 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_req_http_H - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != 'T') p_state = s_req_http_HT elseif p_state == s_req_http_HT - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != 'T') p_state = s_req_http_HTT elseif p_state == s_req_http_HTT - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != 'P') p_state = s_req_http_HTTP elseif p_state == s_req_http_HTTP - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != '/') p_state = s_req_first_http_major #= first digit of major HTTP version =# elseif p_state == s_req_first_http_major - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(ch < '1' || ch > '9', HPE_INVALID_VERSION) parser.major = Int16(ch - '0') p_state = s_req_http_major #= major HTTP version or dot =# elseif p_state == s_req_http_major - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == '.' p_state = s_req_first_http_minor elseif !isnum(ch) @@ -562,14 +535,12 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= first digit of minor HTTP version =# elseif p_state == s_req_first_http_minor - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(!isnum(ch), HPE_INVALID_VERSION) parser.minor = Int16(ch - '0') p_state = s_req_http_minor #= minor HTTP version or end of request line =# elseif p_state == s_req_http_minor - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == CR p_state = s_req_line_almost_done elseif ch == LF @@ -584,12 +555,10 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= end of request line =# elseif p_state == s_req_line_almost_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(ch != LF, HPE_LF_EXPECTED) p_state = s_header_field_start elseif p_state == s_header_field_start - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == CR p_state = s_headers_almost_done elseif ch == LF @@ -619,8 +588,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_header_field - @debug(PARSING_DEBUG, "parsing header_field") - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) start = p while p <= len @inbounds ch = Char(bytes[p]) @@ -631,19 +598,16 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method break end h = parser.header_state + @debug(PARSING_DEBUG, h) if h == h_general - @debug(PARSING_DEBUG, parser.header_state) elseif h == h_C - @debug(PARSING_DEBUG, parser.header_state) parser.index += 1 parser.header_state = c == 'o' ? h_CO : h_general elseif h == h_CO - @debug(PARSING_DEBUG, parser.header_state) parser.index += 1 parser.header_state = c == 'n' ? h_CON : h_general elseif h == h_CON - @debug(PARSING_DEBUG, parser.header_state) parser.index += 1 if c == 'n' parser.header_state = h_matching_connection @@ -654,7 +618,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end #= connection =# elseif h == h_matching_connection - @debug(PARSING_DEBUG, parser.header_state) parser.index += 1 if parser.index > length(CONNECTION) || c != CONNECTION[parser.index] parser.header_state = h_general @@ -663,7 +626,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end #= proxy-connection =# elseif h == h_matching_proxy_connection - @debug(PARSING_DEBUG, parser.header_state) parser.index += 1 if parser.index > length(PROXY_CONNECTION) || c != PROXY_CONNECTION[parser.index] parser.header_state = h_general @@ -672,7 +634,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end #= content-length =# elseif h == h_matching_content_length - @debug(PARSING_DEBUG, parser.header_state) parser.index += 1 if parser.index > length(CONTENT_LENGTH) || c != CONTENT_LENGTH[parser.index] parser.header_state = h_general @@ -681,7 +642,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end #= transfer-encoding =# elseif h == h_matching_transfer_encoding - @debug(PARSING_DEBUG, parser.header_state) parser.index += 1 if parser.index > length(TRANSFER_ENCODING) || c != TRANSFER_ENCODING[parser.index] parser.header_state = h_general @@ -690,7 +650,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end #= upgrade =# elseif h == h_matching_upgrade - @debug(PARSING_DEBUG, parser.header_state) parser.index += 1 if parser.index > length(UPGRADE) || c != UPGRADE[parser.index] parser.header_state = h_general @@ -715,7 +674,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method write(parser.fieldbuffer, view(bytes, start:p-1)) elseif p_state == s_header_value_discard_ws - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) (ch == ' ' || ch == '\t') && continue if ch == CR p_state = s_header_value_discard_ws_almost_done @@ -728,7 +686,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p_state = s_header_value_start p -= 1 elseif p_state == s_header_value_start - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) p_state = s_header_value parser.index = 1 c = lower(ch) @@ -766,7 +723,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method write(parser.valuebuffer, bytes[p]) elseif p_state == s_header_value - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) start = p h = parser.header_state while p <= len @@ -783,8 +739,8 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method c = lower(ch) + @debug(PARSING_DEBUG, h) if h == h_general - @debug(PARSING_DEBUG, parser.header_state) limit = len - p ptr = pointer(bytes, p) @debug(PARSING_DEBUG, Base.escape_string(string('\'', Char(bytes[p]), '\''))) @@ -927,12 +883,10 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_header_almost_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @errorif(ch != LF, HPE_LF_EXPECTED) p_state = s_header_value_lws elseif p_state == s_header_value_lws - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) p -= 1 if ch == ' ' || ch == '\t' p_state = s_header_value_start @@ -951,12 +905,10 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_header_value_discard_ws_almost_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != LF) p_state = s_header_value_discard_lws elseif p_state == s_header_value_discard_lws - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if ch == ' ' || ch == '\t' p_state = s_header_value_discard_ws else @@ -977,7 +929,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_headers_almost_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != LF) p -= 1 if (parser.flags & F_TRAILING) > 0 @@ -1018,7 +969,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_headers_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) @strictcheck(ch != LF) hasBody = parser.flags & F_CHUNKED > 0 || @@ -1028,14 +978,12 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= Exit, the rest of the message is in a different protocol. =# p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state - @debug(PARSING_DEBUG, "this 1") return HPE_OK, true, true, String(bytes[p+1:end]) end if parser.flags & F_SKIPBODY > 0 p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state - @debug(PARSING_DEBUG, "this 2") return HPE_OK, true, true, nothing elseif parser.flags & F_CHUNKED > 0 #= chunked encoding - ignore Content-Length header =# @@ -1045,29 +993,24 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= Content-Length header given but zero: Content-Length: 0\r\n =# p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state - @debug(PARSING_DEBUG, "this 3") return HPE_OK, true, true, nothing elseif parser.content_length != ULLONG_MAX #= Content-Length header given and non-zero =# p_state = s_body_identity - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) else if !http_message_needs_eof(parser) #= Assume content-length 0 - read the next =# p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) parser.state = p_state - @debug(PARSING_DEBUG, "this 4") return HPE_OK, true, true, p >= len ? nothing : String(bytes[p:end]) else #= Read body until EOF =# p_state = s_body_identity_eof - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) end end end elseif p_state == s_body_identity - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) to_read = UInt64(min(parser.content_length, len - p + 1)) assert(parser.content_length != 0 && parser.content_length != ULLONG_MAX) @@ -1098,13 +1041,10 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= read until EOF =# elseif p_state == s_body_identity_eof - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) onbody(parser, maintask, bytes, p, len) p = len elseif p_state == s_message_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - @debug(PARSING_DEBUG, "this 5") if upgrade #= Exit, the rest of the message is in a different protocol. =# parser.state = p_state @@ -1113,7 +1053,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p = len elseif p_state == s_chunk_size_start - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) assert(parser.flags & F_CHUNKED > 0) unhex_val = unhex[Int(ch)+1] @@ -1123,7 +1062,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p_state = s_chunk_size elseif p_state == s_chunk_size - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) assert(parser.flags & F_CHUNKED > 0) if ch == CR p_state = s_chunk_size_almost_done @@ -1151,7 +1089,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_chunk_parameters - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) assert(parser.flags & F_CHUNKED > 0) #= just ignore this shit. TODO check for overflow =# if ch == CR @@ -1159,7 +1096,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_chunk_size_almost_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) assert(parser.flags & F_CHUNKED > 0) @strictcheck(ch != LF) @@ -1171,7 +1107,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_chunk_data - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) to_read = UInt64(min(parser.content_length, len - p + 1)) assert(parser.flags & F_CHUNKED > 0) @@ -1190,14 +1125,12 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end elseif p_state == s_chunk_data_almost_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) assert(parser.flags & F_CHUNKED > 0) assert(parser.content_length == 0) @strictcheck(ch != CR) p_state = s_chunk_data_done elseif p_state == s_chunk_data_done - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) assert(parser.flags & F_CHUNKED > 0) @strictcheck(ch != LF) p_state = s_chunk_size_start From 770247fb5076fc13ef34fd1bd52c679f20ad3cfa Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 10:44:47 +1100 Subject: [PATCH 016/182] Move header and body procssing out of parser (via callbacks) --- src/parser.jl | 74 +++++++++++++++++--------------------------------- src/types.jl | 17 ++++++++++-- test/parser.jl | 8 ++++-- 3 files changed, 46 insertions(+), 53 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index c60970272..eb6fbca4b 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -38,11 +38,11 @@ mutable struct Parser minor::Int16 url::HTTP.URI status::Int32 - headers::Vector{Pair{String,String}} - body::Ref{FIFOBuffer} + onbody::Function + onheader::Function end -Parser() = Parser(start_state, 0x00, 0, 0, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, Pair{String,String}[], Ref{FIFOBuffer}()) +Parser() = Parser(start_state, 0x00, 0, 0, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, x->nothing, x->nothing) const DEFAULT_PARSER = Parser() @@ -59,8 +59,8 @@ function reset!(p::Parser) p.minor = 0 p.url = HTTP.URI() p.status = 0 - empty!(p.headers) - p.body = Ref{FIFOBuffer}() + p.onbody = x->nothing + p.onheader = x->nothing return end @@ -76,23 +76,6 @@ function onurl(p::Parser) return end -function onheadervalue(p::Parser) - @debug(PARSING_DEBUG, "onheadervalue $v") - v = String(take!(p.fieldbuffer)) => String(take!(p.valuebuffer)) - @debug(PARSING_DEBUG, v) - push!(p.headers, v) - return -end - -function onbody(p::Parser, bytes::Vector{UInt8}, i::Int, j::Int) - @debug(PARSING_DEBUG, "onbody") - v = view(bytes, i:j) - @debug(PARSING_DEBUG, String(v)) - nb = write(p.body[], v) - @assert nb == length(v) - return -end - """ HTTP.parse([HTTP.Request, HTTP.Response], str; kwargs...) @@ -126,22 +109,10 @@ end function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); method::Method=GET)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} - parser.body[] = r.body + parser.onbody = x->write(r.body, x) + parser.onheader = x->appendheader(r, x) err, headerscomplete, messagecomplete, upgrade = parse!(parser, bytes, len, method) - if headerscomplete && isempty(r.headers) - for (k, v) in parser.headers - if k == "" - r.headers[end] = r.headers[end][1] => string(r.headers[end][2], v) -#FIXME move this to Headers->Dict conversino function... - elseif k != "Set-Cookie" && length(r.headers) > 0 && k == r.headers[end].first - r.headers[end] = r.headers[end][1] => string(r.headers[end][2], ", ", v) - else - push!(r.headers, k => v) - end - end - end - return err, headerscomplete, messagecomplete, upgrade end @@ -453,6 +424,14 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method s_req_schema_slash_slash, s_req_server_start), HPE_INVALID_URL) + if ch == ' ' + p_state = s_req_http_start + else + parser.major = Int16(0) + parser.minor = Int16(9) + p_state = ifelse(ch == CR, s_req_line_almost_done, + s_header_field_start) + end break end p_state = URIs.parseurlchar(p_state, ch, strict) @@ -462,14 +441,10 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method write(parser.valuebuffer, view(bytes, start:p-1)) - if ch == ' ' - p_state = s_req_http_start - onurl(parser) - elseif ch in (CR, LF) - parser.major = Int16(0) - parser.minor = Int16(9) - p_state = ifelse(ch == CR, s_req_line_almost_done, s_header_field_start) - onurl(parser) + if p_state >= s_req_http_start + @debug(PARSING_DEBUG, "onurl $p.method $(String(p.valuebuffer))") + url = take!(parser.valuebuffer) + parser.url = URIs.http_parser_parse_url(url, 1, length(url), parser.method == CONNECT) end elseif p_state == s_req_http_start @@ -860,7 +835,8 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method write(parser.valuebuffer, view(bytes, start:p-1)) if p_state != s_header_value - onheadervalue(parser) + parser.onheader(String(take!(parser.fieldbuffer)) => + String(take!(parser.valuebuffer))) end elseif p_state == s_header_almost_done @@ -905,7 +881,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= header value was empty =# p_state = s_header_field_start - onheadervalue(parser) + parser.onheader(String(take!(parser.fieldbuffer)) => "") p -= 1 end @@ -995,7 +971,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method to_read = Int(min(parser.content_length, len - p + 1)) assert(parser.content_length != 0 && parser.content_length != ULLONG_MAX) - onbody(parser, bytes, p, p + to_read - 1) + parser.onbody(view(bytes, p:p + to_read - 1)) #= The difference between advancing content_length and p is because * the latter will automaticaly advance on the next loop iteration. @@ -1022,7 +998,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= read until EOF =# elseif p_state == s_body_identity_eof - onbody(parser, bytes, p, len) + parser.onbody(view(bytes, p:len)) p = len elseif p_state == s_message_done @@ -1093,7 +1069,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method assert(parser.flags & F_CHUNKED > 0) assert(parser.content_length != 0 && parser.content_length != ULLONG_MAX) - onbody(parser, bytes, p, p + to_read - 1) + parser.onbody(view(bytes, p:p + to_read - 1)) #= See the explanation in s_body_identity for why the content * length and data pointers are managed this way. diff --git a/src/types.jl b/src/types.jl index 3708520e0..dc63c784d 100644 --- a/src/types.jl +++ b/src/types.jl @@ -130,8 +130,6 @@ major(r::Request) = r.major minor(r::Request) = r.minor uri(r::Request) = r.uri headers(r::Request) = Dict(r.headers) -header(r, k::String, default::String="") = getkey(r.headers, k, k => default)[2] -setheader(r, v::Pair{String,String}) = setkey(r.headers, v) body(r::Request) = r.body defaultheaders(::Type{Request}) = [ @@ -283,6 +281,21 @@ function Base.showcompact(io::IO, r::Response) length(r.body)," bytes in body)") end +header(r, k::String, default::String="") = getkey(r.headers, k, k => default)[2] +setheader(r, v::Pair{String,String}) = setkey(r.headers, v) + +function appendheader(r, h::Pair{String,String}) + c = r.headers + k,v = h + if k == "" + c[end] = c[end][1] => string(c[end][2], v) + elseif k != "Set-Cookie" && length(c) > 0 && k == c[end][1] + c[end] = c[end][1] => string(c[end][2], ", ", v) + else + push!(r.headers, h) + end +end + ## Request & Response writing # start lines function startline(io::IO, r::Request) diff --git a/test/parser.jl b/test/parser.jl index 2beea8d16..c5cbfbba3 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1355,9 +1355,11 @@ const responses = Message[ println("TEST - parser.jl - Request $t: $(req.name)") upgrade = Ref{String}() if t == "A" + body=FIFOBuffer() + r = HTTP.Request(body=body) p = HTTP.DEFAULT_PARSER HTTP.reset!(p) - r = HTTP.Request(body=FIFOBuffer()) + p.onbody = x->write(body, x) bytes = Vector{UInt8}(req.raw) sz = 1 for i in 1:sz:length(bytes) @@ -1560,9 +1562,11 @@ const responses = Message[ println("TEST - parser.jl - Response $t: $(resp.name)") try if t == "A" + body=FIFOBuffer() + r = HTTP.Response(body=body) p = HTTP.DEFAULT_PARSER HTTP.reset!(p) - r = HTTP.Response(body=FIFOBuffer()) + p.onbody = x->write(body, x) bytes = Vector{UInt8}(resp.raw) sz = 1 for i in 1:sz:length(bytes) From ac46ca2edccd6966ab87d71d8162fe23dc0e7f39 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 11:29:04 +1100 Subject: [PATCH 017/182] throw message incomplete error from parse() fix tests for invalid content length --- src/parser.jl | 5 ++++- test/parser.jl | 35 +++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index eb6fbca4b..2efbed542 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -99,6 +99,9 @@ function parse(T::Type{<:Union{Request, Response}}, str; r.minor = DEFAULT_PARSER.minor err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) !headerscomplete && throw(ParsingError("error parsing $T: headers incomplete")) + if DEFAULT_PARSER.content_length != ULLONG_MAX && !messagecomplete + throw(ParsingError("error parsing $T: message incomplete")) + end if upgrade != nothing extra[] = upgrade end @@ -1126,7 +1129,7 @@ end #= Does the parser need to see an EOF to find the end of the message? =# function http_message_needs_eof(parser) #= See RFC 2616 section 4.4 =# - if (isrequest(parser) || + if (isrequest(parser) || # FIXME request never needs EOF ?? div(parser.status, 100) == 1 || #= 1xx e.g. Continue =# parser.status == 204 || #= No Content =# parser.status == 304 || #= Not Modified =# diff --git a/test/parser.jl b/test/parser.jl index c5cbfbba3..751536c17 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1408,11 +1408,12 @@ const responses = Message[ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" * "Keep-Alive: 300\r\n" * "Content-Length: 7\r\n" * - "Proxy-Connection: keep-alive\r\n\r\n" + "Proxy-Connection: keep-alive\r\n\r\n1234567" req = HTTP.Request() req.uri = HTTP.URI("http://www.techcrunch.com/") req.headers = ["Host"=>"www.techcrunch.com","User-Agent"=>"Fake","Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language"=>"en-us,en;q=0.5","Accept-Encoding"=>"gzip,deflate","Accept-Charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7","Keep-Alive"=>"300","Content-Length"=>"7","Proxy-Connection"=>"keep-alive"] + req.body = FIFOBuffer("1234567") @test HTTP.parse(HTTP.Request, reqstr).headers == req.headers @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1681,24 +1682,40 @@ const responses = Message[ @test_throws HTTP.ParsingError r = HTTP.parse(HTTP.Request, buf) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "1844674407370955160" * "\r\n\r\n" - r = HTTP.parse(HTTP.Response, respstr) + r = HTTP.Response(body=FIFOBuffer()) + HTTP.reset!(HTTP.DEFAULT_PARSER) + HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) @test HTTP.status(r) == 200 @test HTTP.headers(r) == Dict("Content-Length"=>"1844674407370955160") respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551615" * "\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + r = HTTP.Response(body=FIFOBuffer()) + HTTP.reset!(HTTP.DEFAULT_PARSER) + e, h, m, ex = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + @test e == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551616" * "\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + r = HTTP.Response(body=FIFOBuffer()) + HTTP.reset!(HTTP.DEFAULT_PARSER) + e, h, m, ex = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + @test e == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." - r = HTTP.parse(HTTP.Response, respstr) + r = HTTP.Response(body=FIFOBuffer()) + HTTP.reset!(HTTP.DEFAULT_PARSER) + HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) @test HTTP.status(r) == 200 @test HTTP.headers(r) == Dict("Transfer-Encoding"=>"chunked") respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFF" * "\r\n..." - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + r = HTTP.Response(body=FIFOBuffer()) + HTTP.reset!(HTTP.DEFAULT_PARSER) + e, h, m, ex = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + @test e == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "10000000000000000" * "\r\n..." - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + r = HTTP.Response(body=FIFOBuffer()) + HTTP.reset!(HTTP.DEFAULT_PARSER) + e, h, m, ex = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + @test e == HTTP.HPE_INVALID_CONTENT_LENGTH p = HTTP.Parser() for len in (1000, 100000) @@ -1759,7 +1776,9 @@ const responses = Message[ r = HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n" * "Test: Düsseldorf\r\n\r\n") @test HTTP.headers(r) == Dict("Test" => "Düsseldorf") - r = HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") + r = HTTP.Response(body=FIFOBuffer()) + HTTP.reset!(HTTP.DEFAULT_PARSER) + HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}("GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba")) @test String(readavailable(r.body)) == "fooba" for m in instances(HTTP.Method) From 5af3f196fc84f20fa4f5bb94857f71fa499e05ac Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 13:15:56 +1100 Subject: [PATCH 018/182] move upgrade flag to Parser struct so that state is preserved between calls to parse!() ensure that catch-all return at end of main loop is only used for incomplete messages --- src/parser.jl | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 2efbed542..3ff8fb61a 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -30,6 +30,7 @@ mutable struct Parser header_state::UInt8 index::UInt8 flags::UInt8 + upgrade::Bool content_length::UInt64 fieldbuffer::IOBuffer valuebuffer::IOBuffer @@ -42,7 +43,7 @@ mutable struct Parser onheader::Function end -Parser() = Parser(start_state, 0x00, 0, 0, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, x->nothing, x->nothing) +Parser() = Parser(start_state, 0x00, 0, 0, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, x->nothing, x->nothing) const DEFAULT_PARSER = Parser() @@ -51,6 +52,7 @@ function reset!(p::Parser) p.header_state = 0x00 p.index = 0x00 p.flags = 0x00 + p.upgrade = false p.content_length = 0x0000000000000000 truncate(p.fieldbuffer, 0) truncate(p.valuebuffer, 0) @@ -140,7 +142,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, "parse!") p_state = parser.state errno = HPE_UNKNOWN - upgrade = headersdone = false @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, ParsingStateCode(p_state)) if len == 0 @@ -906,11 +907,11 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method #= Set this here so that on_headers_complete() callbacks can see it =# @debug(PARSING_DEBUG, "checking for upgrade...") if (parser.flags & F_UPGRADE > 0) && (parser.flags & F_CONNECTION_UPGRADE > 0) - upgrade = isrequest(parser) || parser.status == 101 + parser.upgrade = isrequest(parser) || parser.status == 101 else - upgrade = isrequest(parser) && parser.method == CONNECT + parser.upgrade = isrequest(parser) && parser.method == CONNECT end - @debug(PARSING_DEBUG, upgrade) + @debug(PARSING_DEBUG, parser.upgrade) #= Here we call the headers_complete callback. This is somewhat * different than other callbacks because if the user returns 1, we * will interpret that as saying that this message has no body. This @@ -921,11 +922,10 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method * we have to simulate it by handling a change in errno below. =# @debug(PARSING_DEBUG, "headersdone") - headersdone = true if method == HEAD parser.flags |= F_SKIPBODY elseif method == CONNECT - upgrade = true + parser.upgrade = true end elseif p_state == s_headers_done @@ -933,7 +933,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method hasBody = parser.flags & F_CHUNKED > 0 || (parser.content_length > 0 && parser.content_length != ULLONG_MAX) - if upgrade && ((isrequest(parser) && parser.method == CONNECT) || + if parser.upgrade && ((isrequest(parser) && parser.method == CONNECT) || (parser.flags & F_SKIPBODY) > 0 || !hasBody) #= Exit, the rest of the message is in a different protocol. =# p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) @@ -1005,12 +1005,8 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method p = len elseif p_state == s_message_done - if upgrade - #= Exit, the rest of the message is in a different protocol. =# - parser.state = p_state - return HPE_OK, true, true, String(bytes[p+1:end]) - end - p = len + parser.state = p_state + return HPE_OK, true, true, parser.upgrade ? String(bytes[p+1:end]) : nothing elseif p_state == s_chunk_size_start assert(parser.flags & F_CHUNKED > 0) @@ -1099,6 +1095,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method error("unhandled state") end end + @assert p == len || p == len + 1 #= Run callbacks for any marks that we have leftover after we ran our of * bytes. There should be at most one of these set, so it's OK to invoke @@ -1113,10 +1110,12 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method parser.state = p_state @debug(PARSING_DEBUG, "exiting maybe unfinished...") @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - b = p_state == start_state || p_state == s_dead - he = b | (headersdone || p_state >= s_headers_done) - m = b | (p_state >= s_message_done) - return HPE_OK, he, m, p >= len ? nothing : String(bytes[p:end]) + he = p_state >= s_headers_done + m = p_state >= s_message_done + @assert !m || he + @assert !m || parser.content_length == ULLONG_MAX + + return HPE_OK, he, m, nothing @label error parser.state = s_start_req_or_res From 175819ecbde6f8fc6034f38dd8f87b742db3889c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 13:27:10 +1100 Subject: [PATCH 019/182] throw ArgumentError for zero len passed to parse! --- src/parser.jl | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 3ff8fb61a..0ec7a1290 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -139,20 +139,12 @@ macro strictcheck(cond) end function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} + len <= 0 && throw(ArgumentError("len must be > 0")) @debug(PARSING_DEBUG, "parse!") p_state = parser.state errno = HPE_UNKNOWN @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - if len == 0 - if p_state == s_body_identity_eof - return HPE_OK, true, true, nothing - elseif @anyeq(p_state, s_dead, s_start_req_or_res, s_start_res, s_start_req) - return HPE_OK, false, false, nothing - else - return HPE_INVALID_EOF_STATE, false, false, nothing - end - end p = 0 while p < len From 5d3ffa15ab647af7b5689b65c31f46adddb483f4 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 13:50:43 +1100 Subject: [PATCH 020/182] simplify ParsingError display --- src/HTTP.jl | 7 ------- src/client.jl | 2 +- src/consts.jl | 5 +++++ src/parser.jl | 38 +++++++++++++++++++++++++++----------- src/urlparser.jl | 2 +- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 3bac11e94..a7b21f142 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -16,13 +16,6 @@ if VERSION > v"0.7-DEV" using Base64 end -struct ParsingError <: Exception - msg::String -end -Base.show(io::IO, p::ParsingError) = println("HTTP.ParsingError: ", p.msg) - -const CRLF = "\r\n" - include("consts.jl") include("utils.jl") include("uri.jl") diff --git a/src/client.jl b/src/client.jl index 202d866e4..aeb85cb43 100644 --- a/src/client.jl +++ b/src/client.jl @@ -304,7 +304,7 @@ function processresponse!(client, conn, response, host, method, stream, opts, ve end if errno != HPE_OK dead!(conn) - throw(ParsingError("error parsing response: $(ParsingErrorCodeMap[errno])\nCurrent response buffer contents: $(String(buffer))")) + throw(ParsingError(errno, "Current response buffer contents: $(String(buffer))")) elseif messagecomplete http_should_keep_alive(conn.parser) || (@log("closing connection (no keep-alive)"); dead!(conn)) # idle! on a Dead will stay Dead diff --git a/src/consts.jl b/src/consts.jl index ded8156c5..650a222a3 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -191,6 +191,8 @@ Base.convert(::Type{Method}, s::String) = MethodMap[s] HPE_PAUSED, HPE_URI_OVERFLOW, HPE_BODY_OVERFLOW, + HPE_HEADERS_INCOMPLETE, + HPE_BODY_INCOMPLETE, HPE_UNKNOWN, ) @@ -229,6 +231,8 @@ const ParsingErrorCodeMap = Dict( HPE_PAUSED => "parser is paused", HPE_URI_OVERFLOW => "uri exceeded configured maximum uri size", HPE_BODY_OVERFLOW => "body exceeded configured maximum body size", + HPE_HEADERS_INCOMPLETE => "unexpected end of headers", + HPE_BODY_INCOMPLETE => "unexpected end of body", HPE_UNKNOWN => "an unknown error occurred", ) @@ -335,6 +339,7 @@ const CR = '\r' const bCR = UInt8('\r') const LF = '\n' const bLF = UInt8('\n') +const CRLF = "\r\n" const ULLONG_MAX = typemax(UInt64) diff --git a/src/parser.jl b/src/parser.jl index 0ec7a1290..1d7760564 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -68,6 +68,20 @@ end isrequest(p::Parser) = p.status == 0 +struct ParsingError <: Exception + code::ParsingErrorCode + msg::String +end +ParsingError(code::ParsingErrorCode) = ParsingError(code, "") + +function Base.show(io::IO, e::ParsingError) + println(io, string("HTTP.ParsingError: ", + ParsingErrorCodeMap(e), + e.msg == "" ? "" : "\n", + s.msg)) +end + + # should we just make a copy of the byte vector for URI here? function onurl(p::Parser) @debug(PARSING_DEBUG, "onurl $p.method $(String(p.valuebuffer))") @@ -88,21 +102,23 @@ full request or response (but may include more than one). Supported keyword argu """ function parse(T::Type{<:Union{Request, Response}}, str; extra::Ref{String}=Ref{String}()) + r = T(body=FIFOBuffer()) - reset!(DEFAULT_PARSER) - err, headerscomplete, messagecomplete, upgrade = parse!(r, DEFAULT_PARSER, Vector{UInt8}(str)) + p = DEFAULT_PARSER + reset!(p) + err, headerscomplete, messagecomplete, upgrade = parse!(r, p, Vector{UInt8}(str)) if T == Request - r.uri = DEFAULT_PARSER.url - r.method = DEFAULT_PARSER.method + r.uri = p.url + r.method = p.method else - r.status = DEFAULT_PARSER.status + r.status = p.status end - r.major = DEFAULT_PARSER.major - r.minor = DEFAULT_PARSER.minor - err != HPE_OK && throw(ParsingError("error parsing $T: $(ParsingErrorCodeMap[err])")) - !headerscomplete && throw(ParsingError("error parsing $T: headers incomplete")) - if DEFAULT_PARSER.content_length != ULLONG_MAX && !messagecomplete - throw(ParsingError("error parsing $T: message incomplete")) + r.major = p.major + r.minor = p.minor + err != HPE_OK && throw(ParsingError(err)) + !headerscomplete && throw(ParsingError(HPE_HEADERS_INCOMPLETE)) + if p.content_length != ULLONG_MAX && !messagecomplete + throw(ParsingError(HPE_BODY_INCOMPLETE)) end if upgrade != nothing extra[] = upgrade diff --git a/src/urlparser.jl b/src/urlparser.jl index 48408431f..413eb84bf 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -4,7 +4,7 @@ include("utils.jl") struct URLParsingError <: Exception msg::String end -Base.show(io::IO, p::URLParsingError) = println("HTTP.URLParsingError: ", p.msg) +Base.show(io::IO, p::URLParsingError) = println(io, "HTTP.URLParsingError: ", p.msg) struct Offset off::UInt16 From f7889216dbedfdcf55685c0dcd3fe6d82026d32a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 16:50:32 +1100 Subject: [PATCH 021/182] Replace multiple status var returned by parse!() with state accessor functions. Replace multiple return points in main parse!() loop with single return at the bottom. Make s_message_done a loop termination condidion. --- src/client.jl | 30 ++++++---- src/parser.jl | 151 +++++++++++++++---------------------------------- src/server.jl | 10 ++-- test/parser.jl | 84 +++++++++++++-------------- 4 files changed, 114 insertions(+), 161 deletions(-) diff --git a/src/client.jl b/src/client.jl index aeb85cb43..e87ea6e53 100644 --- a/src/client.jl +++ b/src/client.jl @@ -286,16 +286,26 @@ function processresponse!(client, conn, response, host, method, stream, opts, ve logger = client.logger while true buffer, err = getbytes(conn.socket, opts.readtimeout) - @log "received bytes from the wire, processing" - # EH: throws a couple of "shouldn't get here" errors; probably not much we can do - errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(response, conn.parser, buffer; method=method) - response.status = conn.parser.status - - if messagecomplete - close(response.body) + if length(buffer) == 0 + dead!(conn) + if waitingforeof(conn.parser) + close(response.body) + return true, StatusError(status(response), response) + else + throw(ParsingError(HPE_INVALID_EOF_STATE)) + end + else + @log "received bytes from the wire, processing" + # EH: throws a couple of "shouldn't get here" errors; probably not much we can do + errno = HTTP.parse!(response, conn.parser, buffer; method=method) + response.status = conn.parser.status + if messagecomplete(conn.parser) + close(response.body) + end end + @log "parsed bytes received from wire" - if length(buffer) == 0 && !isopen(conn.socket) && !messagecomplete + if length(buffer) == 0 && !isopen(conn.socket) && !messagecomplete(conn.parser) @log "socket closed before full response received" dead!(conn) close(response.body) @@ -305,12 +315,12 @@ function processresponse!(client, conn, response, host, method, stream, opts, ve if errno != HPE_OK dead!(conn) throw(ParsingError(errno, "Current response buffer contents: $(String(buffer))")) - elseif messagecomplete + elseif messagecomplete(conn.parser) http_should_keep_alive(conn.parser) || (@log("closing connection (no keep-alive)"); dead!(conn)) # idle! on a Dead will stay Dead idle!(conn) return true, StatusError(status(response), response) - elseif stream && headerscomplete + elseif stream && headerscomplete(conn.parser) @log "processing the rest of response asynchronously" @async processresponse!(client, conn, response, host, method, false, opts, false) return true, StatusError(status(response), response) diff --git a/src/parser.jl b/src/parser.jl index 1d7760564..65ceaaff6 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -39,11 +39,12 @@ mutable struct Parser minor::Int16 url::HTTP.URI status::Int32 - onbody::Function onheader::Function + onbody::Function + extra::SubArray{UInt8,1} end -Parser() = Parser(start_state, 0x00, 0, 0, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, x->nothing, x->nothing) +Parser() = Parser(start_state, 0x00, 0, 0, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, x->nothing, x->nothing, view(UInt8[], 1:0)) const DEFAULT_PARSER = Parser() @@ -61,12 +62,17 @@ function reset!(p::Parser) p.minor = 0 p.url = HTTP.URI() p.status = 0 - p.onbody = x->nothing p.onheader = x->nothing - return + p.onbody = x->nothing + p.extra = view(UInt8[], 1:0) end isrequest(p::Parser) = p.status == 0 +headerscomplete(p::Parser) = p.state >= s_headers_done +messagecomplete(p::Parser) = p.state >= s_message_done +waitingforeof(p::Parser) = p.state == s_body_identity_eof +upgrade(p::Parser) = p.upgrade +extra(p::Parser) = p.extra struct ParsingError <: Exception code::ParsingErrorCode @@ -76,20 +82,9 @@ ParsingError(code::ParsingErrorCode) = ParsingError(code, "") function Base.show(io::IO, e::ParsingError) println(io, string("HTTP.ParsingError: ", - ParsingErrorCodeMap(e), + ParsingErrorCodeMap[e.code], e.msg == "" ? "" : "\n", - s.msg)) -end - - -# should we just make a copy of the byte vector for URI here? -function onurl(p::Parser) - @debug(PARSING_DEBUG, "onurl $p.method $(String(p.valuebuffer))") - url = take!(p.valuebuffer) - uri = URIs.http_parser_parse_url(url, 1, length(url), p.method == CONNECT) - @debug(PARSING_DEBUG, uri) - p.url = uri - return + e.msg)) end """ @@ -101,12 +96,12 @@ full request or response (but may include more than one). Supported keyword argu * `extra`: a `Ref{String}` that will be used to store any extra bytes beyond a full request or response """ function parse(T::Type{<:Union{Request, Response}}, str; - extra::Ref{String}=Ref{String}()) + extraref::Ref{SubArray{UInt8,1}}=Ref{SubArray{UInt8,1}}()) r = T(body=FIFOBuffer()) p = DEFAULT_PARSER reset!(p) - err, headerscomplete, messagecomplete, upgrade = parse!(r, p, Vector{UInt8}(str)) + err = parse!(r, p, Vector{UInt8}(str)) if T == Request r.uri = p.url r.method = p.method @@ -116,45 +111,43 @@ function parse(T::Type{<:Union{Request, Response}}, str; r.major = p.major r.minor = p.minor err != HPE_OK && throw(ParsingError(err)) - !headerscomplete && throw(ParsingError(HPE_HEADERS_INCOMPLETE)) - if p.content_length != ULLONG_MAX && !messagecomplete + !headerscomplete(p) && throw(ParsingError(HPE_HEADERS_INCOMPLETE)) + if p.content_length != ULLONG_MAX && !messagecomplete(p) throw(ParsingError(HPE_BODY_INCOMPLETE)) end - if upgrade != nothing - extra[] = upgrade + if upgrade(p) + extraref[] = extra(p) end close(r.body) return r end function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); - method::Method=GET)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} + method::Method=GET)::ParsingErrorCode parser.onbody = x->write(r.body, x) parser.onheader = x->appendheader(r, x) - err, headerscomplete, messagecomplete, upgrade = parse!(parser, bytes, len, method) - - return err, headerscomplete, messagecomplete, upgrade + parse!(parser, bytes, len, method) end macro errorif(cond, err) - return esc(quote + esc(quote $cond && @err($err) end) end macro err(e) - return esc(quote + esc(quote errno = $e @goto error end) end macro strictcheck(cond) - return esc(:(strict && @errorif($cond, HPE_STRICT))) + esc(:(strict && @errorif($cond, HPE_STRICT))) end -function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method)::Tuple{ParsingErrorCode, Bool, Bool, Union{Void,String}} +function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method)::ParsingErrorCode len <= 0 && throw(ArgumentError("len must be > 0")) @debug(PARSING_DEBUG, "parse!") p_state = parser.state @@ -163,7 +156,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, ParsingStateCode(p_state)) p = 0 - while p < len + while p_state != s_message_done && p < len @debug(PARSING_DEBUG, "top of while($p < $len)") @debug(PARSING_DEBUG, ParsingStateCode(p_state)) p += 1 @@ -939,43 +932,22 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif p_state == s_headers_done @strictcheck(ch != LF) - hasBody = parser.flags & F_CHUNKED > 0 || - (parser.content_length > 0 && parser.content_length != ULLONG_MAX) - if parser.upgrade && ((isrequest(parser) && parser.method == CONNECT) || - (parser.flags & F_SKIPBODY) > 0 || !hasBody) - #= Exit, the rest of the message is in a different protocol. =# - p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) - parser.state = p_state - return HPE_OK, true, true, String(bytes[p+1:end]) - end - - if parser.flags & F_SKIPBODY > 0 - p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) - parser.state = p_state - return HPE_OK, true, true, nothing - elseif parser.flags & F_CHUNKED > 0 + if parser.flags & F_CHUNKED > 0 #= chunked encoding - ignore Content-Length header =# p_state = s_chunk_size_start + elseif parser.flags & F_SKIPBODY > 0 || + parser.content_length == 0 || + parser.upgrade && isrequest(parser) && parser.method == CONNECT + p_state = s_message_done + elseif parser.content_length != ULLONG_MAX + #= Content-Length header given and non-zero =# + p_state = s_body_identity + elseif http_message_needs_eof(parser) + #= Read body until EOF =# + p_state = s_body_identity_eof else - if parser.content_length == 0 - #= Content-Length header given but zero: Content-Length: 0\r\n =# - p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) - parser.state = p_state - return HPE_OK, true, true, nothing - elseif parser.content_length != ULLONG_MAX - #= Content-Length header given and non-zero =# - p_state = s_body_identity - else - if !http_message_needs_eof(parser) - #= Assume content-length 0 - read the next =# - p_state = ifelse(http_should_keep_alive(parser), start_state, s_dead) - parser.state = p_state - return HPE_OK, true, true, p >= len ? nothing : String(bytes[p:end]) - else - #= Read body until EOF =# - p_state = s_body_identity_eof - end - end + #= Assume content-length 0 - read the next =# + p_state = s_message_done end elseif p_state == s_body_identity @@ -994,17 +966,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method if parser.content_length == 0 p_state = s_message_done - - #= Mimic CALLBACK_DATA_NOADVANCE() but with one extra byte. - * - * The alternative to doing this is to wait for the next byte to - * trigger the data callback, just as in every other case. The - * problem with this is that this makes it difficult for the test - * harness to distinguish between complete-on-EOF and - * complete-on-length. It's not clear that this distinction is - * important for applications, but let's keep it for now. - =# - p -= 1 end #= read until EOF =# @@ -1012,10 +973,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method parser.onbody(view(bytes, p:len)) p = len - elseif p_state == s_message_done - parser.state = p_state - return HPE_OK, true, true, parser.upgrade ? String(bytes[p+1:end]) : nothing - elseif p_state == s_chunk_size_start assert(parser.flags & F_CHUNKED > 0) @@ -1103,34 +1060,20 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method error("unhandled state") end end - @assert p == len || p == len + 1 - - #= Run callbacks for any marks that we have leftover after we ran our of - * bytes. There should be at most one of these set, so it's OK to invoke - * them in series (unset marks will not result in callbacks). - * - * We use the NOADVANCE() variety of callbacks here because 'p' has already - * overflowed 'data' and this allows us to correct for the off-by-one that - * we'd otherwise have (since CALLBACK_DATA() is meant to be run with a 'p' - * value that's in-bounds). - =# + @assert p_state == s_message_done || p == len || p == len + 1 parser.state = p_state - @debug(PARSING_DEBUG, "exiting maybe unfinished...") - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) - he = p_state >= s_headers_done - m = p_state >= s_message_done - @assert !m || he - @assert !m || parser.content_length == ULLONG_MAX + if len > p + parser.extra = view(bytes, p+1:len) + end + + @debug(PARSING_DEBUG, "exiting $(ParsingStateCode(p_state))") - return HPE_OK, he, m, nothing + return HPE_OK @label error - parser.state = s_start_req_or_res - parser.header_state = 0x00 - @debug(PARSING_DEBUG, "exiting due to error...") - @debug(PARSING_DEBUG, errno) - return errno, false, false, nothing + @debug(PARSING_DEBUG, "exiting due to error: $errno") + return errno end #= Does the parser need to see an EOF to find the end of the message? =# diff --git a/src/server.jl b/src/server.jl index e6c766813..fa4c0924d 100644 --- a/src/server.jl +++ b/src/server.jl @@ -104,7 +104,7 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, end length(buffer) > 0 || break starttime[] = time() # reset the timeout while still receiving bytes - errno, headerscomplete, messagecomplete, upgrade = HTTP.parse!(request, parser, buffer) + errno = HTTP.parse!(request, parser, buffer) request.method = parser.method request.uri = parser.url request.major = parser.major @@ -127,7 +127,7 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, response = HTTP.Response(400) end error = true - elseif headerscomplete && Base.get(HTTP.headers(request), "Expect", "") == "100-continue" && !alreadysent100continue + elseif HTTP.headerscomplete(parser) && Base.get(HTTP.headers(request), "Expect", "") == "100-continue" && !alreadysent100continue if options.support100continue HTTP.@log "sending 100 Continue response to get request body" # EH: @@ -144,12 +144,12 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, response = HTTP.Response(417) error = true end - elseif upgrade != nothing - @show upgrade + elseif HTTP.upgrade(parser) + @show String(collect(HTTP.extra(parser))) HTTP.@log "received upgrade request on connection i=$i" response = HTTP.Response(501, "upgrade requests are not currently supported") error = true - elseif messagecomplete + elseif HTTP.messagecomplete(parser) HTTP.@log "received request on connection i=$i" verbose && (println(logger, "HTTP.Request:\n"); println(logger, string(request))) try diff --git a/test/parser.jl b/test/parser.jl index 751536c17..4ecc45c1e 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1353,7 +1353,7 @@ const responses = Message[ for req in requests, t in ["A", "B"] println("TEST - parser.jl - Request $t: $(req.name)") - upgrade = Ref{String}() + upgrade = Ref{SubArray{UInt8, 1}}() if t == "A" body=FIFOBuffer() r = HTTP.Request(body=body) @@ -1365,20 +1365,15 @@ const responses = Message[ for i in 1:sz:length(bytes) x = bytes[i:i+sz-1] #@show [Char(x[i]) for i in 1:sz] - err, hc, mc, ug = HTTP.parse!(r, p, x) + err = HTTP.parse!(r, p, x) err != HTTP.HPE_OK && throw(HTTP.ParsingError(HTTP.ParsingErrorCodeMap[err])) r.uri = HTTP.DEFAULT_PARSER.url r.method = HTTP.DEFAULT_PARSER.method r.major = HTTP.DEFAULT_PARSER.major r.minor = HTTP.DEFAULT_PARSER.minor - - if ug != nothing - upgrade[] = ug - break - end end else - r = HTTP.parse(HTTP.Request, req.raw; extra=upgrade) + r = HTTP.parse(HTTP.Request, req.raw; extraref=upgrade) end @test HTTP.major(r) == req.http_major @test HTTP.minor(r) == req.http_minor @@ -1394,9 +1389,13 @@ const responses = Message[ @test HTTP.canonicalizeheaders(HTTP.headers(r)) == Dict(req.headers) @test String(readavailable(HTTP.body(r))) == req.body @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == req.should_keep_alive + + if isassigned(upgrade) + @show String(collect(upgrade[])) + end @test t == "A" || req.upgrade == "" && !isassigned(upgrade) || - upgrade[] == req.upgrade + String(collect(upgrade[])) == req.upgrade end reqstr = "GET http://www.techcrunch.com/ HTTP/1.1\r\n" * @@ -1573,7 +1572,7 @@ const responses = Message[ for i in 1:sz:length(bytes) x = bytes[i:i+sz-1] #@show [Char(x[i]) for i in 1:sz] - err, hc, mc, ug = HTTP.parse!(r, p, x) + err = HTTP.parse!(r, p, x) err != HTTP.HPE_OK && throw(HTTP.ParsingError(HTTP.ParsingErrorCodeMap[err])) r.major = HTTP.DEFAULT_PARSER.major r.minor = HTTP.DEFAULT_PARSER.minor @@ -1671,11 +1670,11 @@ const responses = Message[ for r in ((HTTP.Request, "GET / HTTP/1.1\r\n"), (HTTP.Response, "HTTP/1.0 200 OK\r\n")) HTTP.reset!(HTTP.DEFAULT_PARSER) R = r[1]() - e, h, m, ex = HTTP.parse!(R, HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) + e = HTTP.parse!(R, HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) @test e == HTTP.HPE_OK - @test !h - @test !m - @test ex == nothing + @test !HTTP.headerscomplete(HTTP.DEFAULT_PARSER) + @test !HTTP.messagecomplete(HTTP.DEFAULT_PARSER) + @test isempty(HTTP.extra(HTTP.DEFAULT_PARSER)) end buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n" @@ -1691,12 +1690,12 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551615" * "\r\n\r\n" r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e, h, m, ex = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + e = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) @test e == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551616" * "\r\n\r\n" r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e, h, m, ex = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + e = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) @test e == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." @@ -1709,12 +1708,12 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFF" * "\r\n..." r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e, h, m, ex = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + e = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) @test e == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "10000000000000000" * "\r\n..." r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e, h, m, ex = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + e = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) @test e == HTTP.HPE_INVALID_CONTENT_LENGTH p = HTTP.Parser() @@ -1722,54 +1721,55 @@ const responses = Message[ HTTP.reset!(p) reqstr = "POST / HTTP/1.0\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = HTTP.Request() - e, h, m, ex = HTTP.parse!(r, p, Vector{UInt8}(reqstr)) + e = HTTP.parse!(r, p, Vector{UInt8}(reqstr)) @test e == HTTP.HPE_OK - @test h - @test !m + @test HTTP.headerscomplete(p) + @test !HTTP.messagecomplete(p) for i = 1:len-1 - e, h, m, ex = HTTP.parse!(r, p, Vector{UInt8}("a")) + e = HTTP.parse!(r, p, Vector{UInt8}("a")) @test e == HTTP.HPE_OK - @test h - @test !m + @test HTTP.headerscomplete(p) + @test !HTTP.messagecomplete(p) end - e, h, m, ex = HTTP.parse!(r, p, Vector{UInt8}("a")) + e = HTTP.parse!(r, p, Vector{UInt8}("a")) @test e == HTTP.HPE_OK - @test h - @test m + @test HTTP.headerscomplete(p) + @test HTTP.messagecomplete(p) end for len in (1000, 100000) HTTP.reset!(p) respstr = "HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = HTTP.Response() - e, h, m, ex = HTTP.parse!(r, p, Vector{UInt8}(respstr)) + e = HTTP.parse!(r, p, Vector{UInt8}(respstr)) @test e == HTTP.HPE_OK - @test h - @test !m + @test HTTP.headerscomplete(p) + @test !HTTP.messagecomplete(p) for i = 1:len-1 - e, h, m, ex = HTTP.parse!(r, p, Vector{UInt8}("a")) + e = HTTP.parse!(r, p, Vector{UInt8}("a")) @test e == HTTP.HPE_OK - @test h - @test !m + @test HTTP.headerscomplete(p) + @test !HTTP.messagecomplete(p) end - e, h, m, ex = HTTP.parse!(r, p, Vector{UInt8}("a")) + e = HTTP.parse!(r, p, Vector{UInt8}("a")) @test e == HTTP.HPE_OK - @test h - @test m + @test HTTP.headerscomplete(p) + @test HTTP.messagecomplete(p) end reqstr = requests[1].raw * requests[2].raw HTTP.reset!(p) r = HTTP.Request() - e, h, m, ex = HTTP.parse!(r, p, Vector{UInt8}(reqstr)) + e = HTTP.parse!(r, p, Vector{UInt8}(reqstr)) @test e == HTTP.HPE_OK - @test h - @test m + @test HTTP.headerscomplete(p) + @test HTTP.messagecomplete(p) + ex = collect(HTTP.extra(p) HTTP.reset!(p) - e, h, m, ex = HTTP.parse!(r, p, Vector{UInt8}(ex)) + e = HTTP.parse!(r, p, ex) @test e == HTTP.HPE_OK - @test h - @test m + @test HTTP.headerscomplete(p) + @test HTTP.messagecomplete(p) @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, "GET / HTP/1.1\r\n\r\n") From aaf8e5d0b02bf6f31563b552447fb1d454d6dc1f Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 17:19:08 +1100 Subject: [PATCH 022/182] replace "while true" read loop in client with "while !eof(socket)" loop --- src/client.jl | 67 ++++++++++++++++++++++---------------------------- test/parser.jl | 2 +- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/src/client.jl b/src/client.jl index a5cf6cd1e..0ff188dfc 100644 --- a/src/client.jl +++ b/src/client.jl @@ -270,61 +270,54 @@ function redirect(response, client, req, opts, stream, history, retry, verbose) end const CLOSED_ERROR = ClosedError(ErrorException(""), "error receiving response; connection was closed prematurely") -function getbytes(socket, tm) +function getbytes(socket) try # EH: returns UInt8[] when socket is closed, error when socket is not readable, AssertionErrors, UVError; - buffer = @retry @timeout(tm, readavailable(socket), error("read timeout")) - return buffer, CLOSED_ERROR + return readavailable(socket) catch e - isa(e, InterruptException) && throw(e) - return UInt8[], ReadError(e, backtrace()) + if !isa(e, InterruptException) + e = ReadError(e, "error reading response") + end + rethrow(e) end end -function processresponse!(client, conn, response, host, method, stream, opts, verbose) +function processresponse!(client, conn, response, host, method, stream, verbose) logger = client.logger - while true - buffer, err = getbytes(conn.socket, opts.readtimeout) - if length(buffer) == 0 - dead!(conn) - if waitingforeof(conn.parser) - close(response.body) - return true, StatusError(status(response), response) - else - throw(ParsingError(HPE_INVALID_EOF_STATE)) - end - else - @log "received bytes from the wire, processing" - # EH: throws a couple of "shouldn't get here" errors; probably not much we can do - errno = HTTP.parse!(response, conn.parser, buffer; method=method) - response.status = conn.parser.status - if messagecomplete(conn.parser) - close(response.body) - end - end - - @log "parsed bytes received from wire" - if length(buffer) == 0 && !isopen(conn.socket) && !messagecomplete(conn.parser) - @log "socket closed before full response received" - dead!(conn) + while !eof(conn.socket) + bytes = getbytes(conn.socket) + @assert length(bytes) > 0 + @log "received bytes from the wire, processing" + # EH: throws a couple of "shouldn't get here" errors; probably not much we can do + errno = HTTP.parse!(response, conn.parser, bytes; method=method) + response.status = conn.parser.status + if messagecomplete(conn.parser) close(response.body) - # retry the entire request - return false, err end + if errno != HPE_OK dead!(conn) - throw(ParsingError(errno, "Current response buffer contents: $(String(buffer))")) + throw(ParsingError(errno, "Current response buffer contents: $(String(bytes))")) elseif messagecomplete(conn.parser) - http_should_keep_alive(conn.parser) || (@log("closing connection (no keep-alive)"); dead!(conn)) - # idle! on a Dead will stay Dead + http_should_keep_alive(conn.parser) || + (@log("closing connection (no keep-alive)"); dead!(conn)) idle!(conn) + # idle! on a Dead will stay Dead return true, StatusError(status(response), response) elseif stream && headerscomplete(conn.parser) @log "processing the rest of response asynchronously" - @async processresponse!(client, conn, response, host, method, false, opts, false) + @async processresponse!(client, conn, response, host, method, false, false) return true, StatusError(status(response), response) end end + + dead!(conn) + close(response.body) + if waitingforeof(conn.parser) + return true, StatusError(status(response), response) + else + throw(CLOSED_ERROR) + end end function request(client::Client, req::Request, opts::RequestOptions, stream::Bool, history::Vector{Response}, retry::Int, verbose::Bool) @@ -343,7 +336,7 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo response = Response(req) reset!(conn.parser) - success, err = processresponse!(client, conn, response, host, HTTP.method(req), stream, opts, verbose) + success, err = processresponse!(client, conn, response, host, HTTP.method(req), stream, verbose) if !success retry >= opts.retries::Int && throw(err) return request(client, req, opts, stream, history, retry + 1, verbose) diff --git a/test/parser.jl b/test/parser.jl index 188f28b43..18fbfa7b6 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1764,7 +1764,7 @@ const responses = Message[ @test e == HTTP.HPE_OK @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) - ex = collect(HTTP.extra(p) + ex = collect(HTTP.extra(p)) HTTP.reset!(p) e = HTTP.parse!(r, p, ex) @test e == HTTP.HPE_OK From 8f4946518c1318f092061a512875a29d3417e408 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 18:31:11 +1100 Subject: [PATCH 023/182] throw errors directly from parse!() --- src/client.jl | 31 ++++++++++++++++++++----------- src/parser.jl | 25 +++++++------------------ src/server.jl | 16 ++++++++-------- src/utils.jl | 10 ++++++++++ test/parser.jl | 50 ++++++++++++++++++++------------------------------ 5 files changed, 65 insertions(+), 67 deletions(-) diff --git a/src/client.jl b/src/client.jl index 0ff188dfc..808d67c1a 100644 --- a/src/client.jl +++ b/src/client.jl @@ -16,9 +16,10 @@ end Connection(tcp::IO) = Connection(0, tcp, Busy, Parser()) Connection(id::Int, tcp::IO) = Connection(id, tcp, Busy, Parser()) -busy!(conn::Connection) = (conn.state == Dead || (conn.state = Busy); return nothing) -idle!(conn::Connection) = (conn.state == Dead || (conn.state = Idle); return nothing) -dead!(conn::Connection) = (conn.state == Dead || (conn.state = Dead; close(conn.socket)); return nothing) +busy!(conn::Connection) = (conn.state == Dead || (conn.state = Busy); return) +idle!(conn::Connection) = (conn.state == Dead || (conn.state = Idle); return) +dead!(conn::Connection) = (conn.state == Dead || (conn.state = Dead; close(conn.socket)); return) +#FIXME maybe should do "close" in the connection pool manager instead of here? """ HTTP.Client([logger::IO]; args...) @@ -289,16 +290,13 @@ function processresponse!(client, conn, response, host, method, stream, verbose) @assert length(bytes) > 0 @log "received bytes from the wire, processing" # EH: throws a couple of "shouldn't get here" errors; probably not much we can do - errno = HTTP.parse!(response, conn.parser, bytes; method=method) + HTTP.parse!(response, conn.parser, bytes; method=method) response.status = conn.parser.status if messagecomplete(conn.parser) close(response.body) end - if errno != HPE_OK - dead!(conn) - throw(ParsingError(errno, "Current response buffer contents: $(String(bytes))")) - elseif messagecomplete(conn.parser) + if messagecomplete(conn.parser) http_should_keep_alive(conn.parser) || (@log("closing connection (no keep-alive)"); dead!(conn)) idle!(conn) @@ -326,6 +324,9 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo verbose && not(client.logger) && (client.logger = STDOUT) logger = client.logger @log "using request options:\n\t" * join((s=>getfield(opts, s) for s in fieldnames(typeof(opts))), "\n\t") + + response = Response(req) + u = uri(req) host = hostname(u) sch = scheme(u) == "http" ? http : https @@ -334,13 +335,21 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo p = port(u) conn = @retryif ClosedError 4 connectandsend(client, sch, host, ifelse(p == "", "80", p), req, opts, verbose) - response = Response(req) - reset!(conn.parser) - success, err = processresponse!(client, conn, response, host, HTTP.method(req), stream, verbose) + success, err = false, nothing + try + reset!(conn.parser) + success, err = processresponse!(client, conn, response, host, + HTTP.method(req), stream, verbose) + catch e + dead!(conn) + rethrow(e) + end + if !success retry >= opts.retries::Int && throw(err) return request(client, req, opts, stream, history, retry + 1, verbose) end + @log "received response" if opts.canonicalizeheaders::Bool response.headers = canonicalizeheaders(response.headers) diff --git a/src/parser.jl b/src/parser.jl index 65ceaaff6..373763033 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -101,7 +101,7 @@ function parse(T::Type{<:Union{Request, Response}}, str; r = T(body=FIFOBuffer()) p = DEFAULT_PARSER reset!(p) - err = parse!(r, p, Vector{UInt8}(str)) + parse!(r, p, Vector{UInt8}(str)) if T == Request r.uri = p.url r.method = p.method @@ -110,7 +110,6 @@ function parse(T::Type{<:Union{Request, Response}}, str; end r.major = p.major r.minor = p.minor - err != HPE_OK && throw(ParsingError(err)) !headerscomplete(p) && throw(ParsingError(HPE_HEADERS_INCOMPLETE)) if p.content_length != ULLONG_MAX && !messagecomplete(p) throw(ParsingError(HPE_BODY_INCOMPLETE)) @@ -123,7 +122,7 @@ function parse(T::Type{<:Union{Request, Response}}, str; end function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); - method::Method=GET)::ParsingErrorCode + method::Method=GET)::Void parser.onbody = x->write(r.body, x) parser.onheader = x->appendheader(r, x) @@ -131,23 +130,18 @@ function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); end macro errorif(cond, err) - esc(quote - $cond && @err($err) - end) + esc(:($cond && @err($err))) end -macro err(e) - esc(quote - errno = $e - @goto error - end) +macro err(code) + esc(:(throw(ParsingError($code)))) end macro strictcheck(cond) esc(:(strict && @errorif($cond, HPE_STRICT))) end -function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method)::ParsingErrorCode +function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method)::Void len <= 0 && throw(ArgumentError("len must be > 0")) @debug(PARSING_DEBUG, "parse!") p_state = parser.state @@ -1068,12 +1062,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end @debug(PARSING_DEBUG, "exiting $(ParsingStateCode(p_state))") - - return HPE_OK - - @label error - @debug(PARSING_DEBUG, "exiting due to error: $errno") - return errno + return end #= Does the parser need to see an EOF to find the end of the message? =# diff --git a/src/server.jl b/src/server.jl index aefbe6f0a..55d96cddc 100644 --- a/src/server.jl +++ b/src/server.jl @@ -110,24 +110,24 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, end length(buffer) > 0 || break starttime[] = time() # reset the timeout while still receiving bytes - errno = HTTP.parse!(request, parser, buffer) + err = @HTTP.catch HTTP.ParsingError HTTP.parse!(request, parser, buffer) request.method = parser.method request.uri = parser.url request.major = parser.major request.minor = parser.minor startedprocessingrequest = true - if errno != HTTP.HPE_OK + if err != nothing # error in parsing the http request - HTTP.@log "error parsing request on connection i=$i: $(HTTP.ParsingErrorCodeMap[errno])" - if errno == HTTP.HPE_INVALID_VERSION + HTTP.@log "error parsing request on connection i=$i: $(HTTP.ParsingErrorCodeMap[err.code])" + if err.code == HTTP.HPE_INVALID_VERSION response = HTTP.Response(505) - elseif errno == HTTP.HPE_HEADER_OVERFLOW + elseif err.code == HTTP.HPE_HEADER_OVERFLOW response = HTTP.Response(431) - elseif errno == HTTP.HPE_URI_OVERFLOW + elseif err.code == HTTP.HPE_URI_OVERFLOW response = HTTP.Response(414) - elseif errno == HTTP.HPE_BODY_OVERFLOW + elseif err.code == HTTP.HPE_BODY_OVERFLOW response = HTTP.Response(413) - elseif errno == HTTP.HPE_INVALID_METHOD + elseif err.code == HTTP.HPE_INVALID_METHOD response = HTTP.Response(405) else response = HTTP.Response(400) diff --git a/src/utils.jl b/src/utils.jl index 0d525d19e..535551e6c 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -203,3 +203,13 @@ function getkey(c, k, default=nothing) i = findfirst(x->first(x) == k, c) return i > 0 ? c[i] : default end + +macro catch(etype, expr) + esc(quote + try + $expr + catch e + isa(e, $etype) ? e : rethrow(e) + end + end) +end diff --git a/test/parser.jl b/test/parser.jl index 18fbfa7b6..a8b5c05a3 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1365,8 +1365,7 @@ const responses = Message[ for i in 1:sz:length(bytes) x = bytes[i:i+sz-1] #@show [Char(x[i]) for i in 1:sz] - err = HTTP.parse!(r, p, x) - err != HTTP.HPE_OK && throw(HTTP.ParsingError(HTTP.ParsingErrorCodeMap[err])) + HTTP.parse!(r, p, x) r.uri = HTTP.DEFAULT_PARSER.url r.method = HTTP.DEFAULT_PARSER.method r.major = HTTP.DEFAULT_PARSER.major @@ -1572,8 +1571,7 @@ const responses = Message[ for i in 1:sz:length(bytes) x = bytes[i:i+sz-1] #@show [Char(x[i]) for i in 1:sz] - err = HTTP.parse!(r, p, x) - err != HTTP.HPE_OK && throw(HTTP.ParsingError(HTTP.ParsingErrorCodeMap[err])) + HTTP.parse!(r, p, x) r.major = HTTP.DEFAULT_PARSER.major r.minor = HTTP.DEFAULT_PARSER.minor r.status = HTTP.DEFAULT_PARSER.status @@ -1670,8 +1668,7 @@ const responses = Message[ for r in ((HTTP.Request, "GET / HTTP/1.1\r\n"), (HTTP.Response, "HTTP/1.0 200 OK\r\n")) HTTP.reset!(HTTP.DEFAULT_PARSER) R = r[1]() - e = HTTP.parse!(R, HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) - @test e == HTTP.HPE_OK + HTTP.parse!(R, HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) @test !HTTP.headerscomplete(HTTP.DEFAULT_PARSER) @test !HTTP.messagecomplete(HTTP.DEFAULT_PARSER) @test isempty(HTTP.extra(HTTP.DEFAULT_PARSER)) @@ -1690,13 +1687,14 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551615" * "\r\n\r\n" r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) - @test e == HTTP.HPE_INVALID_CONTENT_LENGTH + e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end +@show e + @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551616" * "\r\n\r\n" r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) - @test e == HTTP.HPE_INVALID_CONTENT_LENGTH + e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." r = HTTP.Response(body=FIFOBuffer()) @@ -1708,31 +1706,28 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFF" * "\r\n..." r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) - @test e == HTTP.HPE_INVALID_CONTENT_LENGTH + e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "10000000000000000" * "\r\n..." r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e = HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) - @test e == HTTP.HPE_INVALID_CONTENT_LENGTH + e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH p = HTTP.Parser() for len in (1000, 100000) HTTP.reset!(p) reqstr = "POST / HTTP/1.0\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = HTTP.Request() - e = HTTP.parse!(r, p, Vector{UInt8}(reqstr)) - @test e == HTTP.HPE_OK + HTTP.parse!(r, p, Vector{UInt8}(reqstr)) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) for i = 1:len-1 - e = HTTP.parse!(r, p, Vector{UInt8}("a")) - @test e == HTTP.HPE_OK + HTTP.parse!(r, p, Vector{UInt8}("a")) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) end - e = HTTP.parse!(r, p, Vector{UInt8}("a")) - @test e == HTTP.HPE_OK + HTTP.parse!(r, p, Vector{UInt8}("a")) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) end @@ -1741,18 +1736,15 @@ const responses = Message[ HTTP.reset!(p) respstr = "HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = HTTP.Response() - e = HTTP.parse!(r, p, Vector{UInt8}(respstr)) - @test e == HTTP.HPE_OK + HTTP.parse!(r, p, Vector{UInt8}(respstr)) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) for i = 1:len-1 - e = HTTP.parse!(r, p, Vector{UInt8}("a")) - @test e == HTTP.HPE_OK + HTTP.parse!(r, p, Vector{UInt8}("a")) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) end - e = HTTP.parse!(r, p, Vector{UInt8}("a")) - @test e == HTTP.HPE_OK + HTTP.parse!(r, p, Vector{UInt8}("a")) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) end @@ -1760,14 +1752,12 @@ const responses = Message[ reqstr = requests[1].raw * requests[2].raw HTTP.reset!(p) r = HTTP.Request() - e = HTTP.parse!(r, p, Vector{UInt8}(reqstr)) - @test e == HTTP.HPE_OK + HTTP.parse!(r, p, Vector{UInt8}(reqstr)) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) ex = collect(HTTP.extra(p)) HTTP.reset!(p) - e = HTTP.parse!(r, p, ex) - @test e == HTTP.HPE_OK + HTTP.parse!(r, p, ex) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) From 5febfb81b03123681af99c238a20f1127c67fda5 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 29 Nov 2017 22:05:59 +1100 Subject: [PATCH 024/182] Call show(r) for verbose logging and write(r) for comms. (was using string(r) for both). Work around for https://github.com/JuliaWeb/MbedTLS.jl/issues/113 Try to rationalise Base.write, Base.string, Base.show and opts.logbody (WIP) --- src/client.jl | 14 +++++-- src/fifobuffer.jl | 7 +++- src/parser.jl | 1 - src/server.jl | 9 +++-- src/types.jl | 95 ++++++++++++++++++++++++----------------------- test/client.jl | 12 +++--- test/runtests.jl | 2 +- test/server.jl | 6 ++- 8 files changed, 82 insertions(+), 64 deletions(-) diff --git a/src/client.jl b/src/client.jl index 808d67c1a..f791535ff 100644 --- a/src/client.jl +++ b/src/client.jl @@ -18,8 +18,9 @@ Connection(tcp::IO) = Connection(0, tcp, Busy, Parser()) Connection(id::Int, tcp::IO) = Connection(id, tcp, Busy, Parser()) busy!(conn::Connection) = (conn.state == Dead || (conn.state = Busy); return) idle!(conn::Connection) = (conn.state == Dead || (conn.state = Idle); return) -dead!(conn::Connection) = (conn.state == Dead || (conn.state = Dead; close(conn.socket)); return) +dead!(conn::Connection) = (conn.state == Dead || (conn.state = Dead; #=close(conn.socket)=#); return) #FIXME maybe should do "close" in the connection pool manager instead of here? +# Need a regular cleanup function ?? """ HTTP.Client([logger::IO]; args...) @@ -233,10 +234,9 @@ function connectandsend(client, ::Type{sch}, hostname, port, req, opts, verbose) opts.managecookies::Bool && addcookies!(client, hostname, req, verbose) try @log "sending request over the wire\n" - reqstr = string(req, opts) - verbose && (println(client.logger, "HTTP.Request:\n"); println(client.logger, reqstr)) + verbose && (show(client.logger, req); println(client.logger, "")) # EH: throws ArgumentError if socket is closed, UVError; retry if UVError, - @retryif Base.UVError write(conn.socket, reqstr) + @retryif Base.UVError write(conn.socket, req, opts) !isopen(conn.socket) && throw(CLOSED_ERROR) catch e @log backtrace() @@ -287,6 +287,12 @@ function processresponse!(client, conn, response, host, method, stream, verbose) logger = client.logger while !eof(conn.socket) bytes = getbytes(conn.socket) + if length(bytes) == 0 + # https://github.com/JuliaWeb/MbedTLS.jl/issues/113 + @assert isa(conn.socket, MbedTLS.SSLContext) + @assert eof(conn.socket) + break + end @assert length(bytes) > 0 @log "received bytes from the wire, processing" # EH: throws a couple of "shouldn't get here" errors; probably not much we can do diff --git a/src/fifobuffer.jl b/src/fifobuffer.jl index f1a22f374..dbf330ff3 100644 --- a/src/fifobuffer.jl +++ b/src/fifobuffer.jl @@ -15,6 +15,9 @@ FIFOBuffer(io::IO) = FIFOBuffer(readavailable(io)) FIFOBuffer(f::FIFOBuffer) = f +peekbytes(f::FIFOBuffer{IOBuffer}) = f.io.data[f.io.ptr:f.io.size] +peekbytes(f::FIFOBuffer{BufferStream}) = peekbytes(FIFOBuffer(f.io.buffer)) + Base.String(f::FIFOBuffer{IOBuffer}) = String(f.io.data[f.io.ptr:f.io.size]) Base.String(f::FIFOBuffer{BufferStream}) = String(FIFOBuffer(f.io.buffer)) @@ -36,16 +39,18 @@ Base.read(f::FIFOBuffer, ::Type{UInt8}) = read(f.io, UInt8) Base.write(f::FIFOBuffer, bytes::Vector{UInt8}) = write(f.io, bytes) map(eval, :(Base.$f(f::FIFOBuffer) = $f(f.io)) - for f in [:nb_available, :flush, :mark, :reset, :eof, :isopen, :close]) + for f in [:nb_available, :flush, :eof, :isopen, :close]) Base.length(f::FIFOBuffer) = nb_available(f) +#= function Base.read(f::FIFOBuffer, ::Type{Tuple{UInt8,Bool}}) if nb_available(f.io) == 0 return 0x00, false end return read(f.io, UInt8), true end +=# Base.write(f::FIFOBuffer{BufferStream}, x::UInt8) = write(f.io, [x]) diff --git a/src/parser.jl b/src/parser.jl index 373763033..dab328d7e 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -145,7 +145,6 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method len <= 0 && throw(ArgumentError("len must be > 0")) @debug(PARSING_DEBUG, "parse!") p_state = parser.state - errno = HPE_UNKNOWN @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, ParsingStateCode(p_state)) diff --git a/src/server.jl b/src/server.jl index 55d96cddc..c9f137fdb 100644 --- a/src/server.jl +++ b/src/server.jl @@ -157,7 +157,8 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, error = true elseif HTTP.messagecomplete(parser) HTTP.@log "received request on connection i=$i" - verbose && (println(logger, "HTTP.Request:\n"); println(logger, string(request))) + + verbose && (show(logger, request); println(logger, "")) try response = Handlers.handle(handler, request, HTTP.Response()) catch e @@ -179,10 +180,10 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, end if !error HTTP.@log "responding with response on connection i=$i" - respstr = string(response, options) - verbose && (println(logger, "HTTP.Response:\n"); println(logger, respstr)) + verbose && (show(logger, response); println(logger, "")) + try - write(tcp, respstr) + write(tcp, response, options) catch e HTTP.@log e error = true diff --git a/src/types.jl b/src/types.jl index badb05bee..657df1686 100644 --- a/src/types.jl +++ b/src/types.jl @@ -59,6 +59,8 @@ mutable struct RequestOptions new(ch, gzip, ct, rt, tls, mr, ar, fh, tr, mc, sr, i, h, lb) end +# FIXME defaults are over in "const DEFAULT_OPTIONS" in client.jl !! + const RequestOptionsFieldTypes = Dict(:chunksize => Int, :gzip => Bool, :connecttimeout => Float64, @@ -338,53 +340,47 @@ end function body(io::IO, r::Union{Request, Response}, opts) if !hasmessagebody(r) - write(io, "$CRLF") + write(io, CRLF) return end chksz = get(opts, :chunksize, 0) - mark(r.body) -# @sync begin -# @async begin - chunked = false - bytes = UInt8[] - blength = bodylength(r) - while length(bytes) < blength && !eof(r.body) - bytes = chksz == 0 ? readavailable(r.body) : read(r.body, chksz) - (length(bytes) == blength || eof(r.body)) && !chunked && break - if !chunked - write(io, "Transfer-Encoding: chunked$CRLF$CRLF") - end - chunked = true - chunk = length(bytes) - chunk == 0 && break - write(io, "$(hex(chunk))$CRLF") - write(io, bytes, CRLF) - end - if chunked - write(io, "$(hex(0))$CRLF$CRLF") - else - write(io, "Content-Length: $(dec(length(bytes)))$CRLF$CRLF") - write(io, bytes) - end -# end -# end - reset(r.body) +# mark(r.body) + chunked = false + bytes = UInt8[] + blength = bodylength(r) + while length(bytes) < blength && !eof(r.body) + bytes = chksz == 0 ? readavailable(r.body) : read(r.body, chksz) + (length(bytes) == blength || eof(r.body)) && !chunked && break + if !chunked + write(io, "Transfer-Encoding: chunked$CRLF$CRLF") + end + chunked = true + chunk = length(bytes) + chunk == 0 && break + write(io, "$(hex(chunk))$CRLF") + write(io, bytes, CRLF) + end + if chunked + write(io, "$(hex(0))$CRLF$CRLF") + else + write(io, "Content-Length: $(dec(length(bytes)))$CRLF$CRLF") + write(io, bytes) + end +# reset(r.body) return end -Base.write(io::IO, r::Union{Request, Response}, opts) = write(io, string(r)) -function Base.string(r::Union{Request, Response}, opts=RequestOptions()) - i = IOBuffer() - startline(i, r) - headers(i, r) - lb = opts.logbody - if lb === nothing || lb - body(i, r, opts) - else - println(i, "\n[request body logging disabled]\n") - end - return String(take!(i)) +function Base.write(io::IO, r::Union{Request, Response}, opts=RequestOptions()) + startline(io, r) + headers(io, r) + body(io, r, opts) +end + +function Base.string(r::Union{Request, Response}) + io = IOBuffer() + write(io, r) + String(take!(io)) end function Base.show(io::IO, r::Union{Request,Response}; opts=RequestOptions()) @@ -392,12 +388,17 @@ function Base.show(io::IO, r::Union{Request,Response}; opts=RequestOptions()) println(io, "\"\"\"") startline(io, r) headers(io, r) - buf = IOBuffer() - if isopen(r.body) - println(io, "\n[open HTTP.FIFOBuffer with $(length(r.body)) bytes to read]") - else - body(buf, r, opts) - b = take!(buf) + lb = opts.logbody + if lb === nothing || lb + #buf = IOBuffer() +# if isopen(r.body) +# println(io, "\n[open HTTP.FIFOBuffer with $(length(r.body)) bytes to read]") +# else + #FIXME + #body(buf, r, opts) + #b = take!(buf) + write(io, CRLF) + b = FIFOBuffers.peekbytes(r.body) if length(b) > 2 contenttype = sniff(b) if contenttype in DISPLAYABLE_TYPES @@ -417,6 +418,8 @@ function Base.show(io::IO, r::Union{Request,Response}; opts=RequestOptions()) else print(io, String(b)) end + else + println(i, "\n[request body logging disabled]\n") end print(io, "\"\"\"") end diff --git a/test/client.jl b/test/client.jl index aab70e283..2a6ab2d20 100644 --- a/test/client.jl +++ b/test/client.jl @@ -19,7 +19,7 @@ for sch in ("http", "https") println("running $sch client tests...") println("simple GET, HEAD, POST, DELETE, etc.") - @test HTTP.status(HTTP.get("$sch://httpbin.org/ip")) == 200 + @test HTTP.status(HTTP.get("$sch://httpbin.org/ip"; logbody=false)) == 200 @test HTTP.status(HTTP.head("$sch://httpbin.org/ip")) == 200 @test HTTP.status(HTTP.options("$sch://httpbin.org/ip")) == 200 @test HTTP.status(HTTP.post("$sch://httpbin.org/ip"; statusraise=false)) == 405 @@ -95,17 +95,17 @@ for sch in ("http", "https") # message to any POST/PUT requests that are sent using chunked encoding # See https://github.com/kennethreitz/httpbin/issues/340#issuecomment-330176449 println("client transfer-encoding chunked") - @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body="hey", chunksize=2)) == 200 - @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'], chunksize=2)) == 200 + @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body="hey", chunksize=2)) == 200 + @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'], chunksize=2)) == 200 io = IOBuffer("hey"); seekstart(io) - @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 + @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 tmp = tempname() open(f->write(f, "hey"), tmp, "w") io = open(tmp) - @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 + @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 close(io); rm(tmp) f = HTTP.FIFOBuffer("hey") - @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f, chunksize=2)) == 200 + @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f, chunksize=2)) == 200 # multipart println("client multipart body") diff --git a/test/runtests.jl b/test/runtests.jl index 40aadecd8..2d0106da4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,7 +8,7 @@ end @testset "HTTP" begin include("utils.jl"); - include("fifobuffer.jl"); + #include("fifobuffer.jl"); include("sniff.jl"); include("uri.jl"); include("cookies.jl"); diff --git a/test/server.jl b/test/server.jl index ed21d62d7..c641900a6 100644 --- a/test/server.jl +++ b/test/server.jl @@ -60,7 +60,11 @@ sleep(2.0) log = String(readavailable(serverlog)) client = String(readavailable(tcp)) -print(client) +println("log:") +println(log) +println() +println("client:") +println(client) @test contains(client, "HTTP/1.1 200 OK\r\n") @test contains(client, "Connection: keep-alive\r\n") @test contains(client, "Content-Length: 15\r\n") From dbb1bae3c11c9070b16d87bd07638f38f8ed1a93 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 30 Nov 2017 09:59:13 +1100 Subject: [PATCH 025/182] fix parse error when fragmentation splits CRLF --- src/parser.jl | 33 +++++++-------------------------- test/parser.jl | 39 ++++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 39 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index dab328d7e..6411641a7 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -684,8 +684,11 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, Base.escape_string(string('\'', ch, '\''))) @debug(PARSING_DEBUG, strict) @debug(PARSING_DEBUG, isheaderchar(ch)) - if ch in (CR, LF) - p_state = ch == CR ? s_header_almost_done : s_header_value_lws + if ch == CR + p_state = s_header_almost_done + break + elseif ch == LF + p_state = s_header_value_lws break elseif strict && !isheaderchar(ch) @err(HPE_INVALID_HEADER_TOKEN) @@ -695,30 +698,8 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @debug(PARSING_DEBUG, h) if h == h_general - limit = len - p - ptr = pointer(bytes, p) - @debug(PARSING_DEBUG, Base.escape_string(string('\'', Char(bytes[p]), '\''))) - p_cr = ccall(:memchr, Ptr{Void}, (Ptr{Void}, Cint, Csize_t), ptr, CR, limit) - p_lf = ccall(:memchr, Ptr{Void}, (Ptr{Void}, Cint, Csize_t), ptr, LF, limit) - @debug(PARSING_DEBUG, limit) - @debug(PARSING_DEBUG, Int(p_cr)) - @debug(PARSING_DEBUG, Int(p_lf)) - if p_cr != C_NULL - if p_lf != C_NULL && p_cr >= p_lf - @debug(PARSING_DEBUG, Base.escape_string(string('\'', Char(bytes[p + Int(p_lf - ptr + 1)]), '\''))) - p += Int(p_lf - ptr) - else - @debug(PARSING_DEBUG, Base.escape_string(string('\'', Char(bytes[p + Int(p_cr - ptr + 1)]), '\''))) - p += Int(p_cr - ptr) - end - elseif p_lf != C_NULL - @debug(PARSING_DEBUG, Base.escape_string(string('\'', Char(bytes[p + Int(p_lf - ptr + 1)]), '\''))) - p += Int(p_lf - ptr) - else - @debug(PARSING_DEBUG, Base.escape_string(string('\'', Char(bytes[len]), '\''))) - p = len + 1 - end - p -= 1 + crlf = findfirst(x->(x == bCR || x == bLF), view(bytes, p:len)) + p = crlf == 0 ? len : p + crlf - 2 elseif h == h_connection || h == h_transfer_encoding error("Shouldn't get here.") diff --git a/test/parser.jl b/test/parser.jl index a8b5c05a3..a17dfbf86 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1350,27 +1350,41 @@ const responses = Message[ @testset "HTTP.parse" begin @testset "HTTP.parse(HTTP.Request, str)" begin - for req in requests, t in ["A", "B"] + for req in requests, t in [-1, 0, 1, 2, 3, 4, 11, 13, 17, 19, 23, 29, 31, 32] println("TEST - parser.jl - Request $t: $(req.name)") upgrade = Ref{SubArray{UInt8, 1}}() - if t == "A" + if t > 0 body=FIFOBuffer() r = HTTP.Request(body=body) p = HTTP.DEFAULT_PARSER HTTP.reset!(p) p.onbody = x->write(body, x) bytes = Vector{UInt8}(req.raw) - sz = 1 + sz = t for i in 1:sz:length(bytes) - x = bytes[i:i+sz-1] - #@show [Char(x[i]) for i in 1:sz] + x = bytes[i:min(i+sz-1, length(bytes))] + #@show [Char(x[i]) for i in 1:length(x)] HTTP.parse!(r, p, x) r.uri = HTTP.DEFAULT_PARSER.url r.method = HTTP.DEFAULT_PARSER.method r.major = HTTP.DEFAULT_PARSER.major r.minor = HTTP.DEFAULT_PARSER.minor end + elseif t < 0 + body=FIFOBuffer() + r = HTTP.Request(body=body) + p = HTTP.DEFAULT_PARSER + HTTP.reset!(p) + p.onbody = x->write(body, x) + bytes = Vector{UInt8}(req.raw) + i = rand(2:length(bytes)) + HTTP.parse!(r, p, bytes[1:i-1]) + HTTP.parse!(r, p, bytes[i:end]) + r.uri = HTTP.DEFAULT_PARSER.url + r.method = HTTP.DEFAULT_PARSER.method + r.major = HTTP.DEFAULT_PARSER.major + r.minor = HTTP.DEFAULT_PARSER.minor else r = HTTP.parse(HTTP.Request, req.raw; extraref=upgrade) end @@ -1392,7 +1406,7 @@ const responses = Message[ if isassigned(upgrade) @show String(collect(upgrade[])) end - @test t == "A" || + @test t != 0 || req.upgrade == "" && !isassigned(upgrade) || String(collect(upgrade[])) == req.upgrade end @@ -1557,27 +1571,27 @@ const responses = Message[ end @testset "HTTP.parse(HTTP.Response, str)" begin - for resp in responses, t in ["A", "B"] + for resp in responses, t in [0, 1, 2, 3, 4, 11, 13, 17, 19, 23, 29, 31, 32] println("TEST - parser.jl - Response $t: $(resp.name)") try - if t == "A" + if t > 0 body=FIFOBuffer() r = HTTP.Response(body=body) p = HTTP.DEFAULT_PARSER HTTP.reset!(p) p.onbody = x->write(body, x) bytes = Vector{UInt8}(resp.raw) - sz = 1 + sz = t for i in 1:sz:length(bytes) - x = bytes[i:i+sz-1] - #@show [Char(x[i]) for i in 1:sz] + x = bytes[i:min(i+sz-1, length(bytes))] + #@show [Char(x[i]) for i in 1:length(x)] HTTP.parse!(r, p, x) r.major = HTTP.DEFAULT_PARSER.major r.minor = HTTP.DEFAULT_PARSER.minor r.status = HTTP.DEFAULT_PARSER.status end - elseif t == "B" + else r = HTTP.parse(HTTP.Response, resp.raw) end @test HTTP.major(r) == resp.http_major @@ -1688,7 +1702,6 @@ const responses = Message[ r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end -@show e @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551616" * "\r\n\r\n" r = HTTP.Response(body=FIFOBuffer()) From 9accb0b20be816b2d97792dad16e1110bfd64ccb Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 7 Dec 2017 10:59:59 +1100 Subject: [PATCH 026/182] simple debug macro --- src/utils.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/utils.jl b/src/utils.jl index 535551e6c..8b31eab17 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -106,6 +106,10 @@ macro debug(should, expr) end end +macro debug(s) + DEBUG ? esc(:(println(string("DEBUG: ", $s)))) : :() +end + macro log(stmt) # "[HTTP]: Connecting to remote host..." return esc(:(verbose && (write(logger, "[HTTP - $(rpad(Dates.now(), 23, ' '))]: $($stmt)\n"); flush(logger)))) From 77ba528d82cba6ffe4f77c7f796975a5b172ce34 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 7 Dec 2017 11:05:05 +1100 Subject: [PATCH 027/182] function absuri(u::URI, context::URI) --- src/uri.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/uri.jl b/src/uri.jl index cf6d924c8..df7eb4ea5 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -240,4 +240,22 @@ function splitpath(p::String) return elems end +absuri(u::String, context::URI) = absuri(URI(u), context) + +function absuri(u::URI, context::URI) + + @assert !isempty(hostname(context)) + + s = scheme(u) + h = hostname(u) + n = port(u) + p = hostname(u) + q = query(u) + + return URIs.URI(scheme=isempty(s) ? scheme(context) : s, + hostname=isempty(h) ? hostname(context) : h, + port=isempty(n) ? port(context) : n, + query=isempty(q) ? query(context) : q) +end + end # module From ebced40ee5346209799c533ae8dfd32667a5d2e3 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 7 Dec 2017 11:06:25 +1100 Subject: [PATCH 028/182] parser interface tweaks in server (untested) --- src/server.jl | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/server.jl b/src/server.jl index c9f137fdb..998b7f2ab 100644 --- a/src/server.jl +++ b/src/server.jl @@ -110,11 +110,7 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, end length(buffer) > 0 || break starttime[] = time() # reset the timeout while still receiving bytes - err = @HTTP.catch HTTP.ParsingError HTTP.parse!(request, parser, buffer) - request.method = parser.method - request.uri = parser.url - request.major = parser.major - request.minor = parser.minor + err = @HTTP.catch HTTP.ParsingError HTTP.parse!(parser, buffer) startedprocessingrequest = true if err != nothing # error in parsing the http request @@ -158,6 +154,11 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, elseif HTTP.messagecomplete(parser) HTTP.@log "received request on connection i=$i" + request.method = parser.method + request.uri = parser.url + request.major = parser.major + request.minor = parser.minor + verbose && (show(logger, request); println(logger, "")) try response = Handlers.handle(handler, request, HTTP.Response()) @@ -172,6 +173,8 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, end HTTP.reset!(parser) request = HTTP.Request() + parser.onbody = x->write(request.body, x) + parser.onheader = x->HTTP.appendheader(request, x) else if !any(x->x[1] == "Connection", response.headers) push!(response.headers, "Connection" => "close") @@ -255,6 +258,9 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} while true p = HTTP.Parser() request = HTTP.Request() + p.onbody = x->write(request.body, x) + p.onheader = x->HTTP.appendheader(request, x) + try # accept blocks until a new connection is detected tcp = accept(tcpserver) From 87775ee647daf32f78fa0e63c77cd5b763a70727 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 7 Dec 2017 11:08:18 +1100 Subject: [PATCH 029/182] Work in progress. Separation of client functionality into layers. New modules: - Connect.jl - Make a simple TCL or SSL connection. - Connections.jl - Connection pool with interleaved requests/responses. - Messages.jl - Refactored Request and Response. - Body.jl - Statick and streaming bodies. parser.jl: - add isheadresponse flag - add onheaderscomplete callback - remove "extra" view - return number of bytes consumed instead of using "extra" view - change bytes parameter to be a SubArray client.jl and types.jl - Unfinished refactoring of redirects, connection management etc --- src/Bodies.jl | 164 +++++++++++++++++++ src/Connect.jl | 42 +++++ src/Connections.jl | 183 +++++++++++++++++++++ src/HTTP.jl | 16 +- src/Messages.jl | 387 +++++++++++++++++++++++++++++++++++++++++++++ src/client.jl | 244 ++++++++++++++++------------ src/parser.jl | 68 ++++---- src/types.jl | 20 ++- test/body.jl | 53 +++++++ test/messages.jl | 157 ++++++++++++++++++ test/parser.jl | 82 +++++++--- test/runtests.jl | 20 +-- 12 files changed, 1257 insertions(+), 179 deletions(-) create mode 100644 src/Bodies.jl create mode 100644 src/Connect.jl create mode 100644 src/Connections.jl create mode 100644 src/Messages.jl create mode 100644 test/body.jl create mode 100644 test/messages.jl diff --git a/src/Bodies.jl b/src/Bodies.jl new file mode 100644 index 000000000..3212f82f7 --- /dev/null +++ b/src/Bodies.jl @@ -0,0 +1,164 @@ +module Bodies + +export Body, isstream + + +""" + set_show_max(x) + +Set the maximum number of bytes to be displayed by `show(::IO, ::Body)` +""" + +set_show_max(x) = global body_show_max = x +body_show_max = 1000 + + +""" + Body + +Represents a HTTP Message Body. + +If `io` is set to `notastream`, then `buffer` contains static Message Body data. +Otherwise, `io` is a stream to/from which Message Body data is written/read. +In streaming mode: `length` keeps track of the number of bytes that have passed +through `io`; and `buffer` keeps a cache of the first part of the Message Body +for display purposes. See `show` and `set_show_max`. +""" + +mutable struct Body + io::IO + buffer::IOBuffer + length::Int +end + +const notastream = IOBuffer("") +isstream(b::Body) = b.io != notastream + + +""" + Body() + Body(data) + Body(::IO) + +`Body()` creates an empty HTTP Message `Body` buffer. +The `write(::Body)` function can be used to append data to the empty `Body`. +The `write(::IO)` function can then be used to send the Body to an `IO` stream. +e.g. + +``` +b = Body() +write(b, "Hello\\n") +write(b, "World!\\n") +write(socket, b) +``` + +`Body(data)` creates a `Body` with fixed content. + +`Body(::IO)` creates a streaming mode `Body`. This can be used to stream either +Request Messages or Response Messages. `write(io, body)` reads data from +the `body`'s stream and writes it to the `io` target. `write(body, data)` writes +data to the `body`'s stream. +""" + +Body(::Void) = Body() +Body(buffer::IOBuffer=IOBuffer()) = Body(notastream, buffer, 0) +Body(io::IO) = Body(io, IOBuffer(body_show_max), 0) +Body(data) = Body(IOBuffer(data)) + + +""" + length(::Body) + +Number of bytes in the body. +In streaming mode, number of bytes that have passed through the stream. +""" + +Base.length(b::Body) = isstream(b) ? b.length : b.buffer.size + + +""" + collect!(::Body) + +If the `Body` is in streaming mode, read the complete content of the stream +into the local buffer then close the stream. +Returns a `view` of the local buffer. +""" + +function collect!(body::Body) + if isstream(body) + io = IOBuffer() + write(io, body) + body.buffer = io + close(body.io) + body.io = notastream + end + @assert !isstream(body) + return view(body.buffer.data, 1:body.buffer.size) +end + + +""" + take!(::Body) + +Obtain the contents of `Body` and clear the internal buffer. +""" + +function Base.take!(body::Body) + collect!(body) + take!(body.buffer) +end + +function Base.write(io::IO, body::Body) + + if !isstream(body) + return write(io, view(body.buffer.data, 1:body.buffer.size)) + end + + @assert body.length == 0 + @assert position(body.buffer) == 0 + while !eof(body.io) + v = readavailable(body.io) + if body.length < body_show_max + write(body.buffer, v) + end + body.length += write(io, v) + end + return body.length +end + +function Base.write(body::Body, v) + + if !isstream(body) + return write(body.buffer, v) + end + + if body.length < body_show_max + write(body.buffer, v) + end + n = write(body.io, v) + body.length += n + return n +end + +Base.close(body::Body) = if isstream(body); close(body.io) end + + +""" + head(::Body) + +The first chunk of the `Body` data (for display purposes). +""" +head(b::Body) = view(b.buffer.data, 1:min(b.buffer.size, body_show_max)) + +function Base.show(io::IO, body::Body) + bytes = head(body) + write(io, bytes) + println(io, "") + if isstream(body) && isopen(body.io) + println(io, "⋮\nWaiting for $(typeof(body.io))...") + elseif length(body) > length(bytes) + println(io, "⋮\n$(length(body))-byte body") + end +end + +end #module Bodies diff --git a/src/Connect.jl b/src/Connect.jl new file mode 100644 index 000000000..ed34c48a1 --- /dev/null +++ b/src/Connect.jl @@ -0,0 +1,42 @@ +module Connect + +export getconnection, readresponse!, unread! + +using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! + + +function getconnection(::Type{TCPSocket}, host::String, port::UInt) + connect(getaddrinfo(host), port) +end + +function getconnection(::Type{SSLContext}, host::String, port::UInt) + io = SSLContext() + setup!(io, SSLConfig(false)) + associate!(io, connect(getaddrinfo(host), port)) + hostname!(io, host) + handshake!(io) + return io +end + + +""" + readresponse!(io, response) + +Read into `response`. +""" + +readresponse!(io, response) = read!(io, response) + + +""" + unread!(::Connection, bytes) + +Push bytes back into a connection (to be returned by the next read). +""" + +function unread!(io, bytes) + println("WARNING: No unread! method for $(typeof(io))!") + println(" Discarding $(length(bytes)) bytes!") +end + +end # module Connect diff --git a/src/Connections.jl b/src/Connections.jl new file mode 100644 index 000000000..6f1754c8a --- /dev/null +++ b/src/Connections.jl @@ -0,0 +1,183 @@ +module Connections + +export getconnection, readresponse!, unread! + +using MbedTLS: SSLContext + + +import ..@lock +import ..@debug + +include("Connect.jl") +import .Connect.unread! + +const ByteView = typeof(view(UInt8[], 1:0)) + + +""" + Connection + +A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. + +The `excess` field contains left over bytes read from the connection after +the end of a response message. These bytes are probably the start of the +next response message. + +The `readlock` is held by the `read!` function until the end of the response +message is parsed. A second `request` task that has sent a message on this +`Connection` must wait to obtain the lock before reading its response. +""" + +mutable struct Connection{T <: IO} <: IO + host::String + port::UInt + io::T + excess::ByteView + readlock::ReentrantLock +end + +Connection{T}() where T <: IO = + Connection{T}("", 0, T(), view(UInt8[], 1:0), ReentrantLock()) + +function Connection{T}(host::String, port::UInt) where T <: IO + c = Connection{T}() + c.host = host + c.port = port + c.io = Connect.getconnection(T, host, port) + return c +end + +const noconnection = Connection{TCPSocket}() + +Base.unsafe_write(c::Connection, p::Ptr{UInt8}, n::UInt) = + unsafe_write(c.io, p, n) + +Base.eof(c::Connection) = isempty(c.excess) && eof(c.io) + +function Base.readavailable(c::Connection) + if !isempty(c.excess) + bytes = c.excess + @debug "read $(length(bytes))-bytes from excess buffer." + c.excess = view(UInt8[], 1:0) + else + bytes = readavailable(c.io) + @debug "read $(length(bytes))-bytes from $(typeof(c.io))" + end + return bytes +end + + +""" + readresponse!(::Connection, response) + +Read from a `Connection` and store result in `response`. +Lock the `readlock` and push the `Connection` back into the `pool` for reuse. +""" + +function readresponse!(c::Connection, response) + @lock c.readlock begin + pushconnection!(c) + return read!(c, response) + end +end + + +""" + unread!(::Connection, bytes) + +Push bytes back into a connection (to be returned by the next read). +""" + +function unread!(c::Connection, bytes::ByteView) + @assert isempty(c.excess) + c.excess = bytes +end + + +""" + pool + +The `pool` is a collection of open `Connection`s that are available +for sending Request Messages. The `request` function calls +`getconnection` to retrieve a connection from the `pool`. +When the `request` function has sent a Request Message it returns the +`Connection` to the `pool`. When a `Connection` is first returned +to the pool, its `readlock` set to indicate that the requester has +not finished reading the Response Message. At this point a new +requester can use the `Connection` to send another Request Message, +but must wait to the `readlock` before reading the Response Message. +""" + +const pool = Vector{Connection}() +const poollock = ReentrantLock() + + +""" + pushconnection!(c::Connection) + +Place a `Connection` in the `pool` for reuse. +""" + +pushconnection!(c::Connection) = @lock poollock push!(pool, c) + + +""" + popconnection!(type, host, port [, default=noconnection]) + +Find a `Connection` and remove it from the `pool`. +""" + +function popconnection!(t::Type, host::String, port::UInt, default=noconnection) + @lock poollock begin + pattern = c->(typeof(c.io) == t && c.host == host && c.port == port) + if (i = findlast(pattern, pool)) > 0 + x = pool[i] + deleteat!(pool, i) + return x + end + end + return default +end + + +""" + getconnection(type, host, port) + +Find a reusable `Connection` and remove it from the `pool`, +or create a new `Connection` if required. +""" + +function getconnection(::Type{T}, host::String, port::UInt) where T <: IO + + while (c = popconnection!(T, host, port)) != noconnection + if isopen(c.io) + @debug "Reused: $c" + return c + end + @debug "Discarded: $c" + end + + c = Connection{T}(host, port) + @debug "New: $c" + return c +end + + +function Base.show(io::IO, c::Connection) + print(io, c.host, ":", Int(c.port), ":", Int(localport(c)), ", ", + typeof(c.io), ", ", tcpstatus(c), ", ", + length(c.excess), "-byte excess", + islocked(c.readlock) ? ", readlock" : "") +end + +tcpsocket(c::Connection{SSLContext})::TCPSocket = c.io.bio +tcpsocket(c::Connection{TCPSocket})::TCPSocket = c.io + +localport(c::Connection) = VERSION > v"0.7.0-DEV" ? + getsockname(tcpsocket(c))[2] : + Base._sockname(tcpsocket(c), true)[2] + +tcpstatus(c::Connection) = Base.uv_status_string(tcpsocket(c)) + + +end # module Connections diff --git a/src/HTTP.jl b/src/HTTP.jl index f3ea39da0..fc803908a 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -1,9 +1,10 @@ __precompile__(true) module HTTP -export Request, Response, FIFOBuffer +#export Request, Response, FIFOBuffer using MbedTLS +using Retry const TLS = MbedTLS @@ -36,16 +37,17 @@ include("types.jl") include("parser.jl") include("sniff.jl") -include("client.jl") -include("handlers.jl") -using .Handlers -include("server.jl") -using .Nitrogen +include("Messages.jl") +#include("client.jl") +#include("handlers.jl") +#using .Handlers +#include("server.jl") +#using .Nitrogen #include("precompile.jl") function __init__() - global const DEFAULT_CLIENT = Client() +# global const DEFAULT_CLIENT = Client() end end # module diff --git a/src/Messages.jl b/src/Messages.jl new file mode 100644 index 000000000..07e995b5a --- /dev/null +++ b/src/Messages.jl @@ -0,0 +1,387 @@ +module Messages + +export Message, Request, Response, Body, + method, header, setheader, request + + +include("Bodies.jl") +using .Bodies + +import ..@lock +import ..Parser +import ..parse! +import ..messagecomplete +import ..waitingforeof +import ..ParsingStateCode +import ..URIs: URI, scheme, hostname, port, resource +import ..HTTP: STATUS_CODES, getkey, setkey, @debug + +include("Connections.jl") +using .Connections +import .Connections.SSLContext + +""" + Request + +Represents a HTTP Request Message. + +The `parent` field refers to the `Response` (if any) that led to this request +(e.g. in the case of a redirect). +""" + +mutable struct Request + method::String + uri::String + version::VersionNumber + headers::Vector{Pair{String,String}} + body::Body + parent +end + +Request(method="", uri="", headers=[], body=Body(); parent=nothing) = + Request(method, uri == "" ? "/" : uri, v"1.1", + mkheaders(headers), body, parent) + +mkheaders(v::Vector{Pair{String,String}}) = v +mkheaders(x) = [string(k) => string(v) for (k,v) in x] + + +""" + Response + +Represents a HTTP Response Message. + +The `parent` field refers to the `Request` that yielded this `Response`. + +The `headerscomplete` `Condition` is raised when the `Parser` has finished +reading the response headers. This allows the `status` and `header` fields to +be read used asynchronously without waiting for the entire body to be parsed. +""" + +mutable struct Response + version::VersionNumber + status::Int16 + headers::Vector{Pair{String,String}} + body::Body + parent + headerscomplete::Condition +end + +Response(status=0, headers=[]; body=Body(), parent=nothing) = + Response(v"1.1", status, headers, body, parent, Condition()) + +const Message = Union{Request,Response} + + +""" + method(::Response) + +Method of the `Request` that yielded this `Response`. +""" + +method(r::Response) = r.parent == nothing ? "" : r.parent.method + + +""" + statustext(::Response) + +`String` representation of a HTTP status code. e.g. `200 => "OK"`. +""" + +statustext(r::Response) = Base.get(STATUS_CODES, r.status, "Unknown Code") + + +""" + waitforheaders(::Response) + +Wait for the `Parser` (in a different task) to finish parsing the headers. +""" + +waitforheaders(r::Response) = while r.status == 0; wait(r.headerscomplete) end + + +""" + header(message, key [, default=""]) + +Get header value for `key`. +""" +header(m, k::String, default::String="") = getkey(m.headers, k, k => default)[2] + + +""" + setheader(message, key => value) + +Set header `value` for `key`. +""" +setheader(m, v::Pair{String,String}) = setkey(m.headers, v) + + +""" + appendheader(message, key => value) + +Append a header value to `message.headers`. + +If `key` is `""` the `value` is appended to the value of the previous header. + +If `key` is the same as the previous header, the `vale` is appended to the +value of the previous header with a comma delimiter. +https://stackoverflow.com/a/24502264 + +`Set-Cookie` headers are not comma-combined because cookies often contain +internal commas. https://tools.ietf.org/html/rfc6265#section-3 +""" + +function appendheader(m::Message, header::Pair{String,String}) + c = m.headers + k,v = header + if k == "" + c[end] = c[end][1] => string(c[end][2], v) + elseif k != "Set-Cookie" && length(c) > 0 && k == c[end][1] + c[end] = c[end][1] => string(c[end][2], ", ", v) + else + push!(m.headers, header) + end +end + + +""" + httpversion(Message) + +e.g. `"HTTP/1.1"` +""" + +httpversion(m::Message) = "HTTP/$(m.version.major).$(m.version.minor)" + + +""" + writestartline(::IO, message) + +e.g. `"GET /path HTTP/1.1\\r\\n"` or `"HTTP/1.1 200 OK\\r\\n"` +""" + +function writestartline(io::IO, r::Request) + write(io, "$(r.method) $(r.uri) $(httpversion(r))\r\n") +end + +function writestartline(io::IO, r::Response) + write(io, "$(httpversion(r)) $(r.status) $(statustext(r))\r\n") +end + + +""" + writeheaders(::IO, message) + +Write a line for each "name: value" pair and a trailing blank line. +""" + +function writeheaders(io::IO, m::Message) + for (name, value) in m.headers + write(io, "$name: $value\r\n") + end + write(io, "\r\n") +end + + +""" + write(::IO, message) + +Write start line, headers and body of HTTP Message. +""" + +function Base.write(io::IO, m::Message) + writestartline(io, m) + writeheaders(io, m) + write(io, m.body) +end + + +""" + readstartline(message, p::Parser) + +Read the start-line metadata from `Parser` into a `message` struct. +""" + +function readstartline!(r::Response, p::Parser) + r.version = VersionNumber(p.major, p.minor) + r.status = p.status + notify(r.headerscomplete) + yield() +end + +function readstartline!(r::Request, p::Parser) + r.version = VersionNumber(p.major, p.minor) + r.method = string(p.method) + r.uri = string(p.url) +end + + +""" + read!(io, parser) + +Read data from `io` into `parser` until `eof` +or the parser finds the end of the message. +""" + +function Base.read!(io::IO, p::Parser) + + while !eof(io) + bytes = readavailable(io) + if isempty(bytes) + @debug "MbedTLS https://github.com/JuliaWeb/MbedTLS.jl/issues/113 !" + @assert isa(io, SSLContext) + @assert eof(io) + break + end + @assert length(bytes) > 0 + + n = parse!(p, bytes) + @assert n == length(bytes) || messagecomplete(p) + @assert n <= length(bytes) + + @debug ParsingStateCode(p.state) + + if messagecomplete(p) + excess = view(bytes, n+1:length(bytes)) + if !isempty(excess) + unread!(io, excess) + end + return + end + end + + if eof(io) && !waitingforeof(p) + throw(EOFError()) + end +end + + +""" + read!(io, message) + +Read data from `io` into a `Message` struct. +""" + +function Base.read!(io::IO, m::Message) + + p = Parser() + p.onbody = x->write(m.body, x) + p.onheader = x->appendheader(m, x) + p.onheaderscomplete = ()->readstartline!(m, p) + p.isheadresponse = (isa(m, Response) && method(m) in ("HEAD", "CONNECT")) + # FIXME CONNECT?? + + read!(io, p) + close(m.body) +end + + +""" + connecturi(::URI) + +Get a `Connection` for a `URI` from the connection pool. +""" + +function connecturi(uri::URI) + getconnection(scheme(uri) == "https" ? SSLContext : TCPSocket, + hostname(uri), + parse(UInt, port(uri))) +end + + +""" + request(::URI, ::Request, ::Response) + +Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. +""" + +function request(uri::URI, req::Request, res::Response) + + #FIXME set Content-Length header? + + host = hostname(uri) + if header(req, "Host") == "" + setheader(req, "Host" => host) + end + + c = connecturi(uri) + @debug "write to: $c\n$req" + write(c, req) + readresponse!(c, res) + @debug "read from: $c\n$req" + + return res +end + + +""" + request(method, uri [, headers=[] [, body="" ]; kw args...) + +Execute a `Request` and return a `Response`. + +`parent=` optionally set a parent `Response`. + +`response_stream=` optional `IO` stream for response body. + + +e.g. use a stream as a request body: + +``` +io = open("request", "r") +r = request("POST", "http://httpbin.org/post", [], io) +``` + +e.g. send a response body to a stream: + +``` +io = open("response_file", "w") +r = request("GET", "http://httpbin.org/stream/100", response_stream=io) +println(stat("response_file").size) +0 +sleep(1) +println(stat("response_file").size) +14990 +``` +""" + +function request(method::String, uri, headers=[], body=""; + parent=nothing, response_stream=nothing) + + u = URI(uri) + + req = Request(method, + method == "CONNECT" ? host(u) : resource(u), + headers, + Body(body); + parent=parent) + + res = Response(body=Body(response_stream), parent=req) + + if isstream(res.body) + @schedule request(u, req, res) + waitforheaders(res) + else + request(u, req, res) + end + + return res +end + + +function Base.String(m::Message) + io = IOBuffer() + write(io, m) + String(take!(io)) +end + + +function Base.show(io::IO, m::Message) + println(io, typeof(m), ":") + println(io, "\"\"\"") + writestartline(io, m) + writeheaders(io, m) + show(io, m.body) + print(io, "\"\"\"") +end + + +end # module Messages diff --git a/src/client.jl b/src/client.jl index f791535ff..fe8a6628a 100644 --- a/src/client.jl +++ b/src/client.jl @@ -202,11 +202,25 @@ function connect(client, sch, hostname, port, opts, verbose) @log "created new connection #$(conn.id) to '$hostname'" return conn catch e - throw(ConnectError(e, backtrace())) + rethrow(ConnectError(e, "connect error")) end end end +function connect(client, req::Request, opts, verbose) + + logger = client.logger + + @log "$(method(req)) $(uri(req))" + + connect(client, + scheme(uri(req)) == "http" ? http : https, + hostname(uri(req)), + port(uri(req)) == "" ? "80" : port(uri(req)), + opts, + verbose) +end + function addcookies!(client, host, req, verbose) logger = client.logger # check if cookies should be added to outgoing request based on host @@ -228,66 +242,72 @@ function addcookies!(client, host, req, verbose) end end -function connectandsend(client, ::Type{sch}, hostname, port, req, opts, verbose) where sch - logger = client.logger - conn = connect(client, sch, hostname, port, opts, verbose) - opts.managecookies::Bool && addcookies!(client, hostname, req, verbose) - try - @log "sending request over the wire\n" - verbose && (show(client.logger, req); println(client.logger, "")) +function sendrequest(client, req::Request, conn, opts, verbose) + + @log "sending request over the wire\n" + verbose && (show(client.logger, req); println(client.logger, "")) + + @protected try + # EH: throws ArgumentError if socket is closed, UVError; retry if UVError, - @retryif Base.UVError write(conn.socket, req, opts) - !isopen(conn.socket) && throw(CLOSED_ERROR) + write(conn.socket, req, opts) catch e - @log backtrace() - typeof(e) <: ArgumentError && throw(ClosedError(e, backtrace())) - throw(SendError(e, backtrace())) + if isa(e, Base.UVError) + e = SendError(e, "error sending request") + end end - return conn end -function redirect(response, client, req, opts, stream, history, retry, verbose) - logger = client.logger - @log "checking for location to redirect" - location = header(response, "Location") - if location == "" - return - end - push!(history, response) - length(history) > opts.maxredirects::Int && throw(RedirectError(opts.maxredirects::Int)) - newuri = URIs.URL(location) - u = uri(req) - newuri = !isempty(hostname(newuri)) ? newuri : URIs.URI(scheme=scheme(u), hostname=hostname(u), port=port(u), path=path(newuri), query=query(u)) - if opts.forwardheaders::Bool - h = headers(req) - delete!(h, "Host") - delete!(h, "Cookie") - else - h = Headers() - end - redirectreq = Request(req.method, newuri, h, req.body) - @log "redirecting to $(newuri)" - return request(client, redirectreq, opts, stream, history, retry, verbose) -end -const CLOSED_ERROR = ClosedError(ErrorException(""), "error receiving response; connection was closed prematurely") -function getbytes(socket) - try - # EH: returns UInt8[] when socket is closed, error when socket is not readable, AssertionErrors, UVError; - return readavailable(socket) +function readresponse(client, req::Request, conn, stream, opts, verbose) + + @protected try + + response = Response(req) + reset!(conn.parser) + conn.parser.onbody = x->write(response.body, x) + conn.parser.onheader = x->appendheader(response, x) + processresponse!(client, conn, response, HTTP.method(req), stream, verbose) + response.status = conn.parser.status + + if opts.statusraise::Bool + s = status(response) + if s < 200 || s >= 300 + throw(StatusError(s, response)) + end + end + return response + catch e - if !isa(e, InterruptException) + if isa(e, Base.UVError) e = ReadError(e, "error reading response") end - rethrow(e) end end -function processresponse!(client, conn, response, host, method, stream, verbose) +function redirect(response, client, req, opts, stream, retry, verbose) + logger = client.logger + + r = Request(req.method, + absuri(header(response, "Location"), uri(req)), + opts.forwardheaders ? + filter((k,v)->!(k in ("Host", "Cookie")), req.headers) : + Headers(), + req.body) + + @log "redirecting to $(uri(r))" + return request(client, r, opts, stream, retry, verbose) +end + +const CLOSED_ERROR = ClosedError(ErrorException(""), "error receiving response; connection was closed prematurely") + +function processresponse!(client, conn, response, method, stream, verbose) logger = client.logger while !eof(conn.socket) - bytes = getbytes(conn.socket) - if length(bytes) == 0 + # EH: returns UInt8[] when socket is closed, + # error when socket is not readable, AssertionErrors, UVError; + bytes = readavailable(conn.socket) + if isempty(bytes) # https://github.com/JuliaWeb/MbedTLS.jl/issues/113 @assert isa(conn.socket, MbedTLS.SSLContext) @assert eof(conn.socket) @@ -296,70 +316,99 @@ function processresponse!(client, conn, response, host, method, stream, verbose) @assert length(bytes) > 0 @log "received bytes from the wire, processing" # EH: throws a couple of "shouldn't get here" errors; probably not much we can do - HTTP.parse!(response, conn.parser, bytes; method=method) - response.status = conn.parser.status + HTTP.parse!(conn.parser, bytes; method=method) + if messagecomplete(conn.parser) close(response.body) end - if messagecomplete(conn.parser) + if messagecomplete(conn.parser) || !hasmessagebody(response) http_should_keep_alive(conn.parser) || (@log("closing connection (no keep-alive)"); dead!(conn)) idle!(conn) # idle! on a Dead will stay Dead - return true, StatusError(status(response), response) + return elseif stream && headerscomplete(conn.parser) @log "processing the rest of response asynchronously" - @async processresponse!(client, conn, response, host, method, false, false) - return true, StatusError(status(response), response) + @async processresponse!(client, conn, response, method, false, false) + return end end dead!(conn) close(response.body) - if waitingforeof(conn.parser) - return true, StatusError(status(response), response) - else + if !waitingforeof(conn.parser) throw(CLOSED_ERROR) end + return end -function request(client::Client, req::Request, opts::RequestOptions, stream::Bool, history::Vector{Response}, retry::Int, verbose::Bool) - retry = max(0, retry) # ensure non-negative +function attemptrequest(client::Client, req::Request, + opts::RequestOptions, stream::Bool, verbose::Bool) + + conn = connect(client, req, opts, verbose) + @protected try + sendrequest(client, req, conn[], opts, verbose) + readresponse(client, req, conn[], stream, opts, verbose) + catch e + dead!(conn[]) + end +end + +function request(client::Client, req::Request, + opts::RequestOptions, stream::Bool, retry::Int, verbose::Bool) + update!(opts, client.options) - verbose && not(client.logger) && (client.logger = STDOUT) + if verbose && not(client.logger) + client.logger = STDOUT + end logger = client.logger - @log "using request options:\n\t" * join((s=>getfield(opts, s) for s in fieldnames(typeof(opts))), "\n\t") - response = Response(req) + @log "using request options:\n\t" * + join((s=>getfield(opts, s) for s in fieldnames(typeof(opts))), "\n\t") - u = uri(req) - host = hostname(u) - sch = scheme(u) == "http" ? http : https - @log "making $(method(req)) request for host: '$host' and resource: '$(resource(u))'" - # maybe allow retrying for all kinds of errors? - p = port(u) - conn = @retryif ClosedError 4 connectandsend(client, sch, host, ifelse(p == "", "80", p), req, opts, verbose) + if opts.managecookies::Bool + addcookies!(client, hostname, req, verbose) + end + + n = max(0, retry) + 1 + response = @repeat n try + + attemptrequest(client, req, opts, stream, verbose) - success, err = false, nothing - try - reset!(conn.parser) - success, err = processresponse!(client, conn, response, host, - HTTP.method(req), stream, verbose) catch e - dead!(conn) - rethrow(e) - end - if !success - retry >= opts.retries::Int && throw(err) - return request(client, req, opts, stream, history, retry + 1, verbose) + @delay_retry if (isa(e, HTTPError) + || isa(e, Base.UVError) + || (isa(e, StatusError) && (e.status < 200 || + e.status >= 500))) + end + + if (isa(e, StatusError) + && e.status in (301, 302, 307, 308) + && opts.allowredirects + && referrercount(req) < opts.maxredirects + && req.method != HEAD #FIXME why not redirect HEAD? + && header(e.response, "Location") != "") + + h = opts.forwardheaders ? + filter((k,v)->!(k in ("Host", "Cookie")), req.headers) : + Headers() + + req = Request(req.method, + absuri(header(response, "Location"), uri(req)), + h, + req.body) + + return request(client, req, opts, stream, retry, verbose) + end end @log "received response" if opts.canonicalizeheaders::Bool response.headers = canonicalizeheaders(response.headers) end + if opts.managecookies::Bool && any(x->x[1]=="Set-Cookie", response.headers) cookies = get!(client.cookies, host, Set{Cookie}()) push!(cookies, (Cookies.readsetcookie(host, v[2]) @@ -367,33 +416,24 @@ function request(client::Client, req::Request, opts::RequestOptions, stream::Boo @log("caching received cookie for host: " * cookies) end - response.history = history - if opts.allowredirects::Bool && req.method != HEAD && (300 <= status(response) < 400) - return redirect(response, client, req, opts, stream, history, retry, verbose) - end - if (200 <= status(response) < 300) || !opts.statusraise::Bool - return response - else - retry >= opts.retries::Int && throw(err) - return request(client, req, opts, stream, history, retry + 1, verbose) - end + return response end request(req::Request; - opts::RequestOptions=RequestOptions(), - stream::Bool=false, - history::Vector{Response}=Response[], - retry::Int=0, - verbose::Bool=false, - args...) = - request(DEFAULT_CLIENT, req, RequestOptions(opts; args...), stream, history, retry, verbose) + opts::RequestOptions=RequestOptions(), + stream::Bool=false, + retry::Int=0, + verbose::Bool=false, + args...) = + request(DEFAULT_CLIENT, req, RequestOptions(opts; args...), stream, retry, verbose) + request(client::Client, req::Request; - opts::RequestOptions=RequestOptions(), - stream::Bool=false, - history::Vector{Response}=Response[], - retry::Int=0, - verbose::Bool=false, - args...) = + opts::RequestOptions=RequestOptions(), + stream::Bool=false, + history::Vector{Response}=Response[], + retry::Int=0, + verbose::Bool=false, + args...) = request(client, req, RequestOptions(opts; args...), stream, history, retry, verbose) # build Request diff --git a/src/parser.jl b/src/parser.jl index 6411641a7..1a37cfb04 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -23,13 +23,14 @@ # const start_state = s_start_req_or_res -const strict = false +const strict = true mutable struct Parser state::UInt8 header_state::UInt8 index::UInt8 flags::UInt8 + isheadresponse::Bool upgrade::Bool content_length::UInt64 fieldbuffer::IOBuffer @@ -41,10 +42,10 @@ mutable struct Parser status::Int32 onheader::Function onbody::Function - extra::SubArray{UInt8,1} + onheaderscomplete::Function end -Parser() = Parser(start_state, 0x00, 0, 0, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, x->nothing, x->nothing, view(UInt8[], 1:0)) +Parser() = Parser(start_state, 0x00, 0, 0, false, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, x->nothing, x->nothing, ()->nothing) const DEFAULT_PARSER = Parser() @@ -53,6 +54,7 @@ function reset!(p::Parser) p.header_state = 0x00 p.index = 0x00 p.flags = 0x00 + p.isheadresponse = false p.upgrade = false p.content_length = 0x0000000000000000 truncate(p.fieldbuffer, 0) @@ -64,7 +66,7 @@ function reset!(p::Parser) p.status = 0 p.onheader = x->nothing p.onbody = x->nothing - p.extra = view(UInt8[], 1:0) + p.onheaderscomplete = ()->nothing end isrequest(p::Parser) = p.status == 0 @@ -72,7 +74,6 @@ headerscomplete(p::Parser) = p.state >= s_headers_done messagecomplete(p::Parser) = p.state >= s_message_done waitingforeof(p::Parser) = p.state == s_body_identity_eof upgrade(p::Parser) = p.upgrade -extra(p::Parser) = p.extra struct ParsingError <: Exception code::ParsingErrorCode @@ -101,7 +102,12 @@ function parse(T::Type{<:Union{Request, Response}}, str; r = T(body=FIFOBuffer()) p = DEFAULT_PARSER reset!(p) - parse!(r, p, Vector{UInt8}(str)) +# p.isheadresponse = method in ("HEAD", "CONNECT") + p.onbody = x->write(r.body, x) + p.onheader = x->appendheader(r, x) + bytes = Vector{UInt8}(str) + n = parse!(p, bytes) + extra = view(bytes, n+1:length(bytes)) if T == Request r.uri = p.url r.method = p.method @@ -115,19 +121,12 @@ function parse(T::Type{<:Union{Request, Response}}, str; throw(ParsingError(HPE_BODY_INCOMPLETE)) end if upgrade(p) - extraref[] = extra(p) + extraref[] = extra end close(r.body) return r end -function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes); - method::Method=GET)::Void - - parser.onbody = x->write(r.body, x) - parser.onheader = x->appendheader(r, x) - parse!(parser, bytes, len, method) -end macro errorif(cond, err) esc(:($cond && @err($err))) @@ -141,15 +140,22 @@ macro strictcheck(cond) esc(:(strict && @errorif($cond, HPE_STRICT))) end -function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method)::Void - len <= 0 && throw(ArgumentError("len must be > 0")) + +const ByteView = typeof(view(UInt8[], 1:0)) + +parse!(p::Parser, bytes)::Int = parse!(p, view(bytes, 1:length(bytes))) + +function parse!(parser::Parser, bytes::ByteView)::Int + isempty(bytes) && throw(ArgumentError("bytes must not be empty")) + len = length(bytes) @debug(PARSING_DEBUG, "parse!") p_state = parser.state @debug(PARSING_DEBUG, len) @debug(PARSING_DEBUG, ParsingStateCode(p_state)) p = 0 - while p_state != s_message_done && p < len + while p < len && p_state != s_message_done + @debug(PARSING_DEBUG, "top of while($p < $len)") @debug(PARSING_DEBUG, ParsingStateCode(p_state)) p += 1 @@ -417,7 +423,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method start = p while p <= len @inbounds ch = Char(bytes[p]) - if ch in (' ', CR, LF) + if @anyeq(ch, ' ', CR, LF) @errorif(@anyeq(p_state, s_req_schema, s_req_schema_slash, s_req_schema_slash_slash, s_req_server_start), @@ -440,7 +446,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method write(parser.valuebuffer, view(bytes, start:p-1)) if p_state >= s_req_http_start - @debug(PARSING_DEBUG, "onurl $p.method $(String(p.valuebuffer))") + @debug(PARSING_DEBUG, "onurl $parser.method $(String(parser.valuebuffer))") url = take!(parser.valuebuffer) parser.url = URIs.http_parser_parse_url(url, 1, length(url), parser.method == CONNECT) end @@ -610,7 +616,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method elseif parser.index == length(UPGRADE) parser.header_state = h_upgrade end - elseif h in (h_connection, h_content_length, h_transfer_encoding, h_upgrade) + elseif @anyeq(h, h_connection, h_content_length, h_transfer_encoding, h_upgrade) if ch != ' ' parser.header_state = h_general end @@ -788,7 +794,7 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method h = h_general end - elseif h in (h_connection_keep_alive, h_connection_close, h_connection_upgrade) + elseif @anyeq(h, h_connection_keep_alive, h_connection_close, h_connection_upgrade) if ch == ',' if h == h_connection_keep_alive parser.flags |= F_CONNECTION_KEEP_ALIVE @@ -878,6 +884,8 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method @errorif((parser.flags & F_CHUNKED) > 0 && (parser.flags & F_CONTENTLENGTH) > 0, HPE_UNEXPECTED_CONTENT_LENGTH) p_state = s_headers_done + parser.state = p_state + parser.onheaderscomplete() #= Set this here so that on_headers_complete() callbacks can see it =# @debug(PARSING_DEBUG, "checking for upgrade...") @@ -897,19 +905,13 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method * we have to simulate it by handling a change in errno below. =# @debug(PARSING_DEBUG, "headersdone") - if method == HEAD - parser.flags |= F_SKIPBODY - elseif method == CONNECT - parser.upgrade = true - end elseif p_state == s_headers_done @strictcheck(ch != LF) - if parser.flags & F_CHUNKED > 0 #= chunked encoding - ignore Content-Length header =# p_state = s_chunk_size_start - elseif parser.flags & F_SKIPBODY > 0 || + elseif parser.isheadresponse || parser.content_length == 0 || parser.upgrade && isrequest(parser) && parser.method == CONNECT p_state = s_message_done @@ -1035,14 +1037,14 @@ function parse!(parser::Parser, bytes::Vector{UInt8}, len::Int64, method::Method end end @assert p_state == s_message_done || p == len || p == len + 1 + if p > len # FIXME + p = len + end parser.state = p_state - if len > p - parser.extra = view(bytes, p+1:len) - end @debug(PARSING_DEBUG, "exiting $(ParsingStateCode(p_state))") - return + return p end #= Does the parser need to see an EOF to find the end of the message? =# @@ -1052,7 +1054,7 @@ function http_message_needs_eof(parser) div(parser.status, 100) == 1 || #= 1xx e.g. Continue =# parser.status == 204 || #= No Content =# parser.status == 304 || #= Not Modified =# - parser.flags & F_SKIPBODY > 0) #= response to a HEAD request =# + parser.isheadresponse) #= response to a HEAD request =# return false end diff --git a/src/types.jl b/src/types.jl index 657df1686..1ee76103e 100644 --- a/src/types.jl +++ b/src/types.jl @@ -124,6 +124,7 @@ mutable struct Request uri::URI headers::Headers # includes cookies body::Union{FIFOBuffer, Form} + #referrer::Ref{Response} end # accessors @@ -134,6 +135,16 @@ uri(r::Request) = r.uri headers(r::Request) = Dict(r.headers) body(r::Request) = r.body +function referrercount(r::Request) + if !isassigned(r.referrer) + return 0 + elseif Base.isnull(request(r.referrer[])) + return 1 + else + return 1 + referrercount(Base.get(request(r.referrer[]))) + end +end + defaultheaders(::Type{Request}) = [ "User-Agent" => "HTTP.jl/0.0.0", "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json; charset=utf-8" @@ -212,7 +223,7 @@ Accessor methods include: * `HTTP.cookies`: cookies for a response, returned as a `Vector{HTTP.Cookie}` * `HTTP.headers`: headers for a response * `HTTP.request`: the `HTTP.Request` that resulted in this response - * `HTTP.history`: history for a response if redirects were followed from an original request + * `HTTP.referrer`: original response if redirects were followed from an original request * `HTTP.body`: body for a response as a `HTTP.FIFOBuffer` Two convenience methods are provided for accessing a response body: @@ -227,7 +238,6 @@ mutable struct Response headers::Headers body::FIFOBuffer request::Nullable{Request} - history::Vector{Response} end # accessors @@ -237,7 +247,6 @@ minor(r::Response) = r.minor cookies(r::Response) = r.cookies headers(r::Response) = Dict(r.headers) request(r::Response) = r.request -history(r::Response) = r.history statustext(r::Response) = Base.get(STATUS_CODES, r.status, "Unknown Code") body(r::Union{Request, Response}) = r.body Base.take!(r::Union{Request, Response}) = readavailable(body(r)) @@ -253,9 +262,8 @@ Response(; status::Int=200, cookies::Vector{Cookie}=Cookie[], headers::Headers=Headers(), body::FIFOBuffer=FIFOBuffer(), - request::Nullable{Request}=Nullable{Request}(), - history::Vector{Response}=Response[]) = - Response(status, Int16(1), Int16(1), cookies, headers, body, request, history) + request::Nullable{Request}=Nullable{Request}()) = + Response(status, Int16(1), Int16(1), cookies, headers, body, request) Response(r::Request) = Response(; body=FIFOBuffer(), request=Nullable(r)) Response(s::Integer) = Response(; status=s) diff --git a/test/body.jl b/test/body.jl new file mode 100644 index 000000000..8ee04b9be --- /dev/null +++ b/test/body.jl @@ -0,0 +1,53 @@ +using HTTP.Messages.Bodies + +@testset "HTTP.Messages.Body" begin + + @test String(take!(Body("Hello!"))) == "Hello!" + @test String(take!(Body(IOBuffer("Hello!")))) == "Hello!" + @test String(take!(Body(Vector{UInt8}("Hello!")))) == "Hello!" + @test String(take!(Body())) == "" + + io = BufferStream() + @async begin + write(io, "Hello") + sleep(0.1) + write(io, "!") + sleep(0.1) + close(io) + end + @test String(take!(Body(io))) == "Hello!" + + b = Body() + write(b, "Hello") + write(b, "!") + @test String(take!(b)) == "Hello!" + + io = BufferStream() + b = Body(io) + write(b, "Hello") + write(b, "!") + @test String(readavailable(io)) == "Hello!" + + #display(b); println() + + buf = IOBuffer() + show(buf, b) + @test String(take!(buf)) == "Hello!\n⋮\nWaiting for BufferStream...\n" + + write(b, "\nWorld!") + close(io) + + #display(b); println() + buf = IOBuffer() + show(buf, b) + @test String(take!(buf)) == "Hello!\nWorld!\n" + + tmp = HTTP.Messages.Bodies.body_show_max + HTTP.Messages.Bodies.set_show_max(12) + b = Body("Hello World!xxx") + #display(b); println() + buf = IOBuffer() + show(buf, b) + @test String(take!(buf)) == "Hello World!\n⋮\n15-byte body\n" + HTTP.Messages.Bodies.set_show_max(tmp) +end diff --git a/test/messages.jl b/test/messages.jl new file mode 100644 index 000000000..de0616a40 --- /dev/null +++ b/test/messages.jl @@ -0,0 +1,157 @@ + +using HTTP.Messages + +using JSON + +@testset "HTTP.Messages" begin + + req = Request("GET", "/foo", ["Foo" => "Bar"]) + res = Response(200, ["Content-Length" => "5"]; body=Body("Hello"), parent=req) + + @test req.method == "GET" + @test method(res) == "GET" + + #display(req); println() + #display(res); println() + + @test String(req) == "GET /foo HTTP/1.1\r\nFoo: Bar\r\n\r\n" + @test String(res) == "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello" + + @test header(req, "Foo") == "Bar" + @test header(res, "Content-Length") == "5" + setheader(req, "X" => "Y") + @test header(req, "X") == "Y" + + HTTP.Messages.appendheader(req, "" => "Z") + @test header(req, "X") == "YZ" + + HTTP.Messages.appendheader(req, "X" => "more") + @test header(req, "X") == "YZ, more" + + HTTP.Messages.appendheader(req, "Set-Cookie" => "A") + HTTP.Messages.appendheader(req, "Set-Cookie" => "B") + @test filter(x->first(x) == "Set-Cookie", req.headers) == + ["Set-Cookie" => "A", "Set-Cookie" => "B"] + + @test HTTP.Messages.httpversion(req) == "HTTP/1.1" + @test HTTP.Messages.httpversion(res) == "HTTP/1.1" + + raw = String(req) + #@show raw + req = Request() + read!(IOBuffer(raw), req) + #display(req); println() + @test String(req) == raw + + req = Request() + read!(IOBuffer(raw * "xxx"), req) + @test String(req) == raw + + raw = String(res) + #@show raw + res = Response() + read!(IOBuffer(raw), res) + #display(res); println() + @test String(res) == raw + + res = Response() + read!(IOBuffer(raw * "xxx"), res) + @test String(res) == raw + + for sch in ["http", "https"] + for m in ["GET", "HEAD", "OPTIONS"] + @test request(m, "$sch://httpbin.org/ip").status == 200 + end + @test request("POST", "$sch://httpbin.org/ip").status == 405 + end + + for sch in ["http", "https"] + for m in ["POST", "PUT", "DELETE", "PATCH"] + + uri = "$sch://httpbin.org/$(lowercase(m))" + r = request(m, uri) + @test r.status == 200 + body = take!(r.body) + + io = BufferStream() + r = request(m, uri, response_stream=io) + @test r.status == 200 + @test read(io) == body + end + end + for sch in ["http", "https"] + for m in ["POST", "PUT", "DELETE", "PATCH"] + + uri = "$sch://httpbin.org/$(lowercase(m))" + io = BufferStream() + r = request(m, uri, response_stream=io) + @test r.status == 200 + end + end + + for sch in ["http", "https"] + + log_buffer = Vector{String}() + + function log(s::String) + println(s) + push!(log_buffer, s) + end + + function async_get(url) + io = BufferStream() + q = HTTP.query(HTTP.URI(url)) + log("GET $q") + r = request("GET", url, response_stream=io) + @async begin + s = String(read(io)) + s = split(s, "\n")[end-1] + x = JSON.parse(s) + log("GOT $q: $(x["args"]["req"])") + end + end + + @sync begin + async_get("$sch://httpbin.org/stream/100?req=1") + async_get("$sch://httpbin.org/stream/100?req=2") + async_get("$sch://httpbin.org/stream/100?req=3") + async_get("$sch://httpbin.org/stream/100?req=4") + async_get("$sch://httpbin.org/stream/100?req=5") + end + + @test log_buffer == ["GET req=1", + "GET req=2", + "GOT req=1: 1", + "GET req=3", + "GOT req=2: 2", + "GET req=4", + "GOT req=3: 3", + "GET req=5", + "GOT req=4: 4", + "GOT req=5: 5"] + + end + + + mktempdir() do d + cd(d) do + + n = 50 + io = open("result_file", "w") + r = request("GET", "http://httpbin.org/stream/$n", + response_stream=io) + @test stat("result_file").size == 0 + while stat("result_file").size <= 1000 + sleep(0.1) + end + @test stat("result_file").size > 1000 + i = 0 + for l in readlines("result_file") + x = JSON.parse(l) + @test i == x["id"] + i += 1 + end + @test i == n + end + end +end diff --git a/test/parser.jl b/test/parser.jl index a17dfbf86..2617a8e9a 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -35,6 +35,20 @@ function Message(; name::String="", kwargs...) return m end + +#= +FIXME request tests for: + - No response body for 100 <= r.status < 200 || + r.status == 204 || + r.status == 304 || + method(r) in ("HEAD", "CONNECT") + + = No request body for method(r) in ("GET", "HEAD", "CONNECT") +=# + + + + #= * R E Q U E S T S * =# const requests = Message[ Message(name= "curl get" @@ -1360,12 +1374,14 @@ const responses = Message[ p = HTTP.DEFAULT_PARSER HTTP.reset!(p) p.onbody = x->write(body, x) + p.onheader = x->HTTP.appendheader(r, x) + bytes = Vector{UInt8}(req.raw) sz = t for i in 1:sz:length(bytes) x = bytes[i:min(i+sz-1, length(bytes))] #@show [Char(x[i]) for i in 1:length(x)] - HTTP.parse!(r, p, x) + HTTP.parse!(p, x) r.uri = HTTP.DEFAULT_PARSER.url r.method = HTTP.DEFAULT_PARSER.method r.major = HTTP.DEFAULT_PARSER.major @@ -1377,10 +1393,11 @@ const responses = Message[ p = HTTP.DEFAULT_PARSER HTTP.reset!(p) p.onbody = x->write(body, x) + p.onheader = x->HTTP.appendheader(r, x) bytes = Vector{UInt8}(req.raw) i = rand(2:length(bytes)) - HTTP.parse!(r, p, bytes[1:i-1]) - HTTP.parse!(r, p, bytes[i:end]) + HTTP.parse!(p, bytes[1:i-1]) + HTTP.parse!(p, bytes[i:end]) r.uri = HTTP.DEFAULT_PARSER.url r.method = HTTP.DEFAULT_PARSER.method r.major = HTTP.DEFAULT_PARSER.major @@ -1580,12 +1597,13 @@ const responses = Message[ p = HTTP.DEFAULT_PARSER HTTP.reset!(p) p.onbody = x->write(body, x) + p.onheader = x->HTTP.appendheader(r, x) bytes = Vector{UInt8}(resp.raw) sz = t for i in 1:sz:length(bytes) x = bytes[i:min(i+sz-1, length(bytes))] #@show [Char(x[i]) for i in 1:length(x)] - HTTP.parse!(r, p, x) + HTTP.parse!(p, x) r.major = HTTP.DEFAULT_PARSER.major r.minor = HTTP.DEFAULT_PARSER.minor r.status = HTTP.DEFAULT_PARSER.status @@ -1682,10 +1700,10 @@ const responses = Message[ for r in ((HTTP.Request, "GET / HTTP/1.1\r\n"), (HTTP.Response, "HTTP/1.0 200 OK\r\n")) HTTP.reset!(HTTP.DEFAULT_PARSER) R = r[1]() - HTTP.parse!(R, HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) + n = HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) @test !HTTP.headerscomplete(HTTP.DEFAULT_PARSER) @test !HTTP.messagecomplete(HTTP.DEFAULT_PARSER) - @test isempty(HTTP.extra(HTTP.DEFAULT_PARSER)) + @test n == length(Vector{UInt8}(r[2])) end buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n" @@ -1694,37 +1712,49 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "1844674407370955160" * "\r\n\r\n" r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) + HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) + HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) @test HTTP.status(r) == 200 @test HTTP.headers(r) == Dict("Content-Length"=>"1844674407370955160") respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551615" * "\r\n\r\n" r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) + HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) + e = try HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551616" * "\r\n\r\n" r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) + HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) + e = try HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) + HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) + HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) + HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) @test HTTP.status(r) == 200 @test HTTP.headers(r) == Dict("Transfer-Encoding"=>"chunked") respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFF" * "\r\n..." r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) + HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) + e = try HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "10000000000000000" * "\r\n..." r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - e = try HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) + HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) + e = try HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH p = HTTP.Parser() @@ -1732,15 +1762,17 @@ const responses = Message[ HTTP.reset!(p) reqstr = "POST / HTTP/1.0\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = HTTP.Request() - HTTP.parse!(r, p, Vector{UInt8}(reqstr)) + p.onbody = x->write(r.body, x) + p.onheader = x->HTTP.appendheader(r, x) + HTTP.parse!(p, Vector{UInt8}(reqstr)) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) for i = 1:len-1 - HTTP.parse!(r, p, Vector{UInt8}("a")) + HTTP.parse!(p, Vector{UInt8}("a")) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) end - HTTP.parse!(r, p, Vector{UInt8}("a")) + HTTP.parse!(p, Vector{UInt8}("a")) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) end @@ -1749,15 +1781,17 @@ const responses = Message[ HTTP.reset!(p) respstr = "HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = HTTP.Response() - HTTP.parse!(r, p, Vector{UInt8}(respstr)) + p.onbody = x->write(r.body, x) + p.onheader = x->HTTP.appendheader(r, x) + HTTP.parse!(p, Vector{UInt8}(respstr)) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) for i = 1:len-1 - HTTP.parse!(r, p, Vector{UInt8}("a")) + HTTP.parse!(p, Vector{UInt8}("a")) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) end - HTTP.parse!(r, p, Vector{UInt8}("a")) + HTTP.parse!(p, Vector{UInt8}("a")) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) end @@ -1765,12 +1799,14 @@ const responses = Message[ reqstr = requests[1].raw * requests[2].raw HTTP.reset!(p) r = HTTP.Request() - HTTP.parse!(r, p, Vector{UInt8}(reqstr)) + p.onbody = x->write(r.body, x) + p.onheader = x->HTTP.appendheader(r, x) + n = HTTP.parse!(p, Vector{UInt8}(reqstr)) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) - ex = collect(HTTP.extra(p)) + ex = Vector{UInt8}(reqstr)[n+1:end] HTTP.reset!(p) - HTTP.parse!(r, p, ex) + HTTP.parse!(p, ex) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) @@ -1781,7 +1817,9 @@ const responses = Message[ r = HTTP.Response(body=FIFOBuffer()) HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.parse!(r, HTTP.DEFAULT_PARSER, Vector{UInt8}("GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba")) + HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) + HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) + HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}("GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba")) @test String(readavailable(r.body)) == "fooba" for m in instances(HTTP.Method) diff --git a/test/runtests.jl b/test/runtests.jl index 2d0106da4..f4c981c22 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,14 +7,16 @@ else end @testset "HTTP" begin - include("utils.jl"); + #include("utils.jl"); #include("fifobuffer.jl"); - include("sniff.jl"); - include("uri.jl"); - include("cookies.jl"); - include("parser.jl"); - include("types.jl"); - include("handlers.jl") - include("client.jl"); - include("server.jl") + #include("sniff.jl"); + #include("uri.jl"); + #include("cookies.jl"); + #include("parser.jl"); + include("body.jl"); + include("messages.jl"); + #include("types.jl"); + #include("handlers.jl") + #include("client.jl"); + #include("server.jl") end; From 9f1756c51892e1fb80520e7d0fa6ecf9909c7533 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 8 Dec 2017 15:00:34 +1100 Subject: [PATCH 030/182] Added closeread/closewrite interface to manage pooling/interleaving. Simplify pool implementation. --- src/Connect.jl | 41 ++++++++++++----- src/Connections.jl | 109 ++++++++++++++++++++------------------------- src/HTTP.jl | 11 +++++ src/Messages.jl | 55 +++++++++++++++-------- src/utils.jl | 6 +-- 5 files changed, 128 insertions(+), 94 deletions(-) diff --git a/src/Connect.jl b/src/Connect.jl index ed34c48a1..e2a5ca361 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -1,33 +1,39 @@ module Connect -export getconnection, readresponse!, unread! +export getconnection, readresponse!, unread!, closeread, closewrite using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! +import ..@debug + + +""" + getconnection(type, host, port) -> IO + +Create a new `TCPSocket` or `SSLContext` connection. + +Note: this `Connect` module creates simple unadorned connection objects +and provides stubs for the `unread!` `closewrite` and `closeread` functions. +The `Connections` module has the same interface but supports connection +reuse and request interleaving. +""" function getconnection(::Type{TCPSocket}, host::String, port::UInt) + @debug 2 "TCP connect: $host:$port..." connect(getaddrinfo(host), port) end function getconnection(::Type{SSLContext}, host::String, port::UInt) + @debug 2 "SSL connect: $host:$port..." io = SSLContext() setup!(io, SSLConfig(false)) - associate!(io, connect(getaddrinfo(host), port)) + associate!(io, getconnection(TCPSocket, host, port)) hostname!(io, host) handshake!(io) return io end -""" - readresponse!(io, response) - -Read into `response`. -""" - -readresponse!(io, response) = read!(io, response) - - """ unread!(::Connection, bytes) @@ -39,4 +45,17 @@ function unread!(io, bytes) println(" Discarding $(length(bytes)) bytes!") end + +""" + closewrite(::Connection) + closeread(::Connection) + +Signal end of write or read operations. +""" + +closewrite(io) = nothing +closeread(io) = close(io) + + + end # module Connect diff --git a/src/Connections.jl b/src/Connections.jl index 6f1754c8a..7c64f75f3 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -1,15 +1,11 @@ module Connections -export getconnection, readresponse!, unread! +export getconnection, readresponse!, unread!, closeread, closewrite -using MbedTLS: SSLContext +import ..@lock, ..@debug, ..SSLContext +import ..Connect: Connect, unread!, closeread, closewrite -import ..@lock -import ..@debug - -include("Connect.jl") -import .Connect.unread! const ByteView = typeof(view(UInt8[], 1:0)) @@ -33,11 +29,12 @@ mutable struct Connection{T <: IO} <: IO port::UInt io::T excess::ByteView + writebusy::Bool readlock::ReentrantLock end Connection{T}() where T <: IO = - Connection{T}("", 0, T(), view(UInt8[], 1:0), ReentrantLock()) + Connection{T}("", 0, T(), view(UInt8[], 1:0), false, ReentrantLock()) function Connection{T}(host::String, port::UInt) where T <: IO c = Connection{T}() @@ -57,43 +54,52 @@ Base.eof(c::Connection) = isempty(c.excess) && eof(c.io) function Base.readavailable(c::Connection) if !isempty(c.excess) bytes = c.excess - @debug "read $(length(bytes))-bytes from excess buffer." + @debug 3 "read $(length(bytes))-bytes from excess buffer." c.excess = view(UInt8[], 1:0) else bytes = readavailable(c.io) - @debug "read $(length(bytes))-bytes from $(typeof(c.io))" + @debug 3 "read $(length(bytes))-bytes from $(typeof(c.io))" end return bytes end """ - readresponse!(::Connection, response) + unread!(::Connection, bytes) -Read from a `Connection` and store result in `response`. -Lock the `readlock` and push the `Connection` back into the `pool` for reuse. +Push bytes back into a connection (to be returned by the next read). """ -function readresponse!(c::Connection, response) - @lock c.readlock begin - pushconnection!(c) - return read!(c, response) - end +function unread!(c::Connection, bytes::ByteView) + @assert isempty(c.excess) + c.excess = bytes end """ - unread!(::Connection, bytes) + closewrite(::Connection) -Push bytes back into a connection (to be returned by the next read). +Signal end of writing (and obtain lock for reading). """ -function unread!(c::Connection, bytes::ByteView) - @assert isempty(c.excess) - c.excess = bytes +function closewrite(c::Connection) + c.writebusy = false + lock(c.readlock) + @debug 2 "Pooled: $c" end +""" + closeread(::Connection) + +Signal end of read operations. +""" + +closeread(c::Connection) = unlock(c.readlock) + +Base.close(c::Connection) = close(c.io) + + """ pool @@ -113,35 +119,7 @@ const poollock = ReentrantLock() """ - pushconnection!(c::Connection) - -Place a `Connection` in the `pool` for reuse. -""" - -pushconnection!(c::Connection) = @lock poollock push!(pool, c) - - -""" - popconnection!(type, host, port [, default=noconnection]) - -Find a `Connection` and remove it from the `pool`. -""" - -function popconnection!(t::Type, host::String, port::UInt, default=noconnection) - @lock poollock begin - pattern = c->(typeof(c.io) == t && c.host == host && c.port == port) - if (i = findlast(pattern, pool)) > 0 - x = pool[i] - deleteat!(pool, i) - return x - end - end - return default -end - - -""" - getconnection(type, host, port) + getconnection(type, host, port) -> Connection Find a reusable `Connection` and remove it from the `pool`, or create a new `Connection` if required. @@ -149,17 +127,28 @@ or create a new `Connection` if required. function getconnection(::Type{T}, host::String, port::UInt) where T <: IO - while (c = popconnection!(T, host, port)) != noconnection - if isopen(c.io) - @debug "Reused: $c" + @lock poollock begin + + pattern = x->(!x.writebusy && + typeof(x.io) == T && + x.host == host && + x.port == port) + + while (i = findlast(pattern, pool)) > 0 + c = pool[i] + if !isopen(c.io) + deleteat!(pool, i) ;@debug 1 "Deleted: $c" + continue + end + c.writebusy = true; ;@debug 2 "Reused: $c" return c end - @debug "Discarded: $c" - end - c = Connection{T}(host, port) - @debug "New: $c" - return c + c = Connection{T}(host, port) ;@debug 1 "New: $c" + c.writebusy = true + push!(pool, c) + return c + end end diff --git a/src/HTTP.jl b/src/HTTP.jl index fc803908a..108530927 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -4,12 +4,17 @@ module HTTP #export Request, Response, FIFOBuffer using MbedTLS +import MbedTLS.SSLContext using Retry const TLS = MbedTLS import Base.== +const DEBUG_LEVEL = 0 + +const DISABLE_CONNECTION_POOL = false + const DEBUG = false const PARSING_DEBUG = false @@ -37,6 +42,12 @@ include("types.jl") include("parser.jl") include("sniff.jl") +include("Connect.jl") +include("Connections.jl") + +#using .Connect +#using .Connections + include("Messages.jl") #include("client.jl") #include("handlers.jl") diff --git a/src/Messages.jl b/src/Messages.jl index 07e995b5a..2e89ecce6 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -2,23 +2,26 @@ module Messages export Message, Request, Response, Body, method, header, setheader, request - + include("Bodies.jl") using .Bodies import ..@lock +import ..SSLContext import ..Parser import ..parse! import ..messagecomplete import ..waitingforeof import ..ParsingStateCode import ..URIs: URI, scheme, hostname, port, resource -import ..HTTP: STATUS_CODES, getkey, setkey, @debug +import ..HTTP: STATUS_CODES, getkey, setkey, @debug, DISABLE_CONNECTION_POOL -include("Connections.jl") -using .Connections -import .Connections.SSLContext +if DISABLE_CONNECTION_POOL + using ..Connect +else + using ..Connections +end """ Request @@ -116,6 +119,19 @@ Set header `value` for `key`. setheader(m, v::Pair{String,String}) = setkey(m.headers, v) +""" + defaultheader(message, key => value) + +Set header `value` for `key` if it is not already set. +""" + +function defaultheader(m, v::Pair{String,String}) + if header(m, first(v)) == "" + setheader(m, v) + end +end + + """ appendheader(message, key => value) @@ -227,7 +243,7 @@ function Base.read!(io::IO, p::Parser) while !eof(io) bytes = readavailable(io) if isempty(bytes) - @debug "MbedTLS https://github.com/JuliaWeb/MbedTLS.jl/issues/113 !" + @debug 1 "Bug https://github.com/JuliaWeb/MbedTLS.jl/issues/113 !" @assert isa(io, SSLContext) @assert eof(io) break @@ -237,8 +253,7 @@ function Base.read!(io::IO, p::Parser) n = parse!(p, bytes) @assert n == length(bytes) || messagecomplete(p) @assert n <= length(bytes) - - @debug ParsingStateCode(p.state) + @debug 3 ParsingStateCode(p.state) if messagecomplete(p) excess = view(bytes, n+1:length(bytes)) @@ -283,8 +298,8 @@ Get a `Connection` for a `URI` from the connection pool. function connecturi(uri::URI) getconnection(scheme(uri) == "https" ? SSLContext : TCPSocket, - hostname(uri), - parse(UInt, port(uri))) + hostname(uri), + parse(UInt, port(uri))) end @@ -298,17 +313,19 @@ function request(uri::URI, req::Request, res::Response) #FIXME set Content-Length header? - host = hostname(uri) - if header(req, "Host") == "" - setheader(req, "Host" => host) + defaultheader(req, "Host" => hostname(uri)) + + io = connecturi(uri) + try ;@debug 1 "write to: $io\n$req" + write(io, req) + closewrite(io) + read!(io, res) ;@debug 2 "read from: $io\n$res" + closeread(io) + catch e + @schedule close(io) + rethrow(e) end - c = connecturi(uri) - @debug "write to: $c\n$req" - write(c, req) - readresponse!(c, res) - @debug "read from: $c\n$req" - return res end diff --git a/src/utils.jl b/src/utils.jl index 8b31eab17..f1ba65b4b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -106,8 +106,8 @@ macro debug(should, expr) end end -macro debug(s) - DEBUG ? esc(:(println(string("DEBUG: ", $s)))) : :() +macro debug(n::Int, s) + DEBUG_LEVEL >= n ? esc(:(println(string("DEBUG: ", $s)))) : :() end macro log(stmt) @@ -185,8 +185,6 @@ macro lock(l, expr) lock($l) try $expr - catch - rethrow() finally unlock($l) end From 89b4f19ca196e065f04f29cbd9ad5b4329f2bbe5 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 10 Dec 2017 10:56:18 +1100 Subject: [PATCH 031/182] Add high level client interfaces - CookieRequest - RetryRequest Add redirect and header normalisation to SendRequest Move ::IO API extensions from Connect.jl to IOExtras.jl (unread!, closeread, closewrite) Add chunked mode to write(::IO, ::Body), add setlengthheader(). Replace writebusy and readlock with writecount/readcount for managing connection interleaving in Connections.jl Clean using and import statements. Hook up StatusError Replace old parse() function with Request(::String) and Response(::String) const ructors. Replace "offset" scheme in URIs with SubStrings (WIP). --- src/Bodies.jl | 20 ++- src/Connect.jl | 33 +--- src/Connections.jl | 88 ++++++----- src/CookieRequest.jl | 68 ++++++++ src/HTTP.jl | 47 ++++-- src/IOExtras.jl | 28 ++++ src/Messages.jl | 195 ++++++++++------------- src/RetryRequest.jl | 34 ++++ src/SendRequest.jl | 152 ++++++++++++++++++ src/parser.jl | 57 ++----- src/uri.jl | 35 +++-- src/urlparser.jl | 16 +- src/utils.jl | 50 +++++- test/body.jl | 12 +- test/messages.jl | 39 ++++- test/parser.jl | 365 +++++++++++++++++++------------------------ test/runtests.jl | 7 +- 17 files changed, 774 insertions(+), 472 deletions(-) create mode 100644 src/CookieRequest.jl create mode 100644 src/IOExtras.jl create mode 100644 src/RetryRequest.jl create mode 100644 src/SendRequest.jl diff --git a/src/Bodies.jl b/src/Bodies.jl index 3212f82f7..2fa98cd83 100644 --- a/src/Bodies.jl +++ b/src/Bodies.jl @@ -22,7 +22,7 @@ If `io` is set to `notastream`, then `buffer` contains static Message Body data. Otherwise, `io` is a stream to/from which Message Body data is written/read. In streaming mode: `length` keeps track of the number of bytes that have passed through `io`; and `buffer` keeps a cache of the first part of the Message Body -for display purposes. See `show` and `set_show_max`. +(for display purposes). See `show` and `set_show_max`). """ mutable struct Body @@ -32,7 +32,6 @@ mutable struct Body end const notastream = IOBuffer("") -isstream(b::Body) = b.io != notastream """ @@ -66,6 +65,15 @@ Body(io::IO) = Body(io, IOBuffer(body_show_max), 0) Body(data) = Body(IOBuffer(data)) +""" + isstream(::Body) + +Is this `Body` in streaming mode? +""" + +isstream(b::Body) = b.io != notastream + + """ length(::Body) @@ -114,15 +122,21 @@ function Base.write(io::IO, body::Body) return write(io, view(body.buffer.data, 1:body.buffer.size)) end + # Read from `body.io` until `eof`, + # write to `io` using "chunked" encoding. + # https://tools.ietf.org/html/rfc7230#section-4.1 @assert body.length == 0 @assert position(body.buffer) == 0 while !eof(body.io) v = readavailable(body.io) + l = length(v) if body.length < body_show_max write(body.buffer, v) end - body.length += write(io, v) + write(io, hex(l), "\r\n", v, "\r\n") + body.length += l end + write(io, "0\r\n\r\n") return body.length end diff --git a/src/Connect.jl b/src/Connect.jl index e2a5ca361..0951fac03 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -1,6 +1,6 @@ module Connect -export getconnection, readresponse!, unread!, closeread, closewrite +export getconnection using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! @@ -12,18 +12,17 @@ import ..@debug Create a new `TCPSocket` or `SSLContext` connection. -Note: this `Connect` module creates simple unadorned connection objects -and provides stubs for the `unread!` `closewrite` and `closeread` functions. +Note: this `Connect` module creates simple unadorned connection objects. The `Connections` module has the same interface but supports connection reuse and request interleaving. """ -function getconnection(::Type{TCPSocket}, host::String, port::UInt) +function getconnection(::Type{TCPSocket}, host::AbstractString, port::UInt)::TCPSocket @debug 2 "TCP connect: $host:$port..." connect(getaddrinfo(host), port) end -function getconnection(::Type{SSLContext}, host::String, port::UInt) +function getconnection(::Type{SSLContext}, host::AbstractString, port::UInt)::SSLContext @debug 2 "SSL connect: $host:$port..." io = SSLContext() setup!(io, SSLConfig(false)) @@ -34,28 +33,4 @@ function getconnection(::Type{SSLContext}, host::String, port::UInt) end -""" - unread!(::Connection, bytes) - -Push bytes back into a connection (to be returned by the next read). -""" - -function unread!(io, bytes) - println("WARNING: No unread! method for $(typeof(io))!") - println(" Discarding $(length(bytes)) bytes!") -end - - -""" - closewrite(::Connection) - closeread(::Connection) - -Signal end of write or read operations. -""" - -closewrite(io) = nothing -closeread(io) = close(io) - - - end # module Connect diff --git a/src/Connections.jl b/src/Connections.jl index 7c64f75f3..d8ad3141b 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -1,10 +1,12 @@ module Connections -export getconnection, readresponse!, unread!, closeread, closewrite +export getconnection + +using ..IOExtras import ..@lock, ..@debug, ..SSLContext -import ..Connect: Connect, unread!, closeread, closewrite +import ..Connect.getconnection const ByteView = typeof(view(UInt8[], 1:0)) @@ -19,9 +21,11 @@ The `excess` field contains left over bytes read from the connection after the end of a response message. These bytes are probably the start of the next response message. -The `readlock` is held by the `read!` function until the end of the response -message is parsed. A second `request` task that has sent a message on this -`Connection` must wait to obtain the lock before reading its response. +The `readcount` and `writecount` keep track of the number of Request/Response +Messages that have been read/written. `writecount` is allowed to be no more +than two greater than `readcount` (see `isbusy`). +i.e. after two Requests have been written to a `Connection`, the first +Response must be read before another Request can be written. """ mutable struct Connection{T <: IO} <: IO @@ -29,18 +33,21 @@ mutable struct Connection{T <: IO} <: IO port::UInt io::T excess::ByteView - writebusy::Bool - readlock::ReentrantLock + writecount::Int + readcount::Int + readdone::Condition end +isbusy(c::Connection) = c.writecount - c.readcount > 1 + Connection{T}() where T <: IO = - Connection{T}("", 0, T(), view(UInt8[], 1:0), false, ReentrantLock()) + Connection{T}("", 0, T(), view(UInt8[], 1:0), 0, 0, Condition()) -function Connection{T}(host::String, port::UInt) where T <: IO +function Connection{T}(host::AbstractString, port::UInt) where T <: IO c = Connection{T}() c.host = host c.port = port - c.io = Connect.getconnection(T, host, port) + c.io = getconnection(T, host, port) return c end @@ -70,7 +77,7 @@ end Push bytes back into a connection (to be returned by the next read). """ -function unread!(c::Connection, bytes::ByteView) +function IOExtras.unread!(c::Connection, bytes::ByteView) @assert isempty(c.excess) c.excess = bytes end @@ -79,39 +86,46 @@ end """ closewrite(::Connection) -Signal end of writing (and obtain lock for reading). +Signal that an entire Request Message has been written to the `Connection`. + +Increment `writecount` and wait for pending reads to complete. """ -function closewrite(c::Connection) - c.writebusy = false - lock(c.readlock) - @debug 2 "Pooled: $c" +function IOExtras.closewrite(c::Connection) + c.writecount += 1 + if isbusy(c) + @debug 3 "Waiting to read: $c" + wait(c.readdone) + end + @assert !isbusy(c) end """ closeread(::Connection) -Signal end of read operations. +Signal that an entire Response Message has been read from the `Connection`. + +Increment `readcount` and wake up waiting `closewrite`. """ -closeread(c::Connection) = unlock(c.readlock) +IOExtras.closeread(c::Connection) = (c.readcount += 1; notify(c.readdone)) + + +Base.close(c::Connection) = (close(c.io); notify(c.readdone)) -Base.close(c::Connection) = close(c.io) """ pool -The `pool` is a collection of open `Connection`s that are available -for sending Request Messages. The `request` function calls -`getconnection` to retrieve a connection from the `pool`. -When the `request` function has sent a Request Message it returns the -`Connection` to the `pool`. When a `Connection` is first returned -to the pool, its `readlock` set to indicate that the requester has -not finished reading the Response Message. At this point a new -requester can use the `Connection` to send another Request Message, -but must wait to the `readlock` before reading the Response Message. +The `pool` is a collection of open `Connection`s. The `request` +function calls `getconnection` to retrieve a connection from the +`pool`. When the `request` function has written a Request Message +it calls `closewrite` to signal that the `Connection` can be reused +for writing (to send the next Request). When the `request` function +has read the Response Message it calls `closeread` to signal that +the `Connection` can be reused for reading. """ const pool = Vector{Connection}() @@ -121,15 +135,16 @@ const poollock = ReentrantLock() """ getconnection(type, host, port) -> Connection -Find a reusable `Connection` and remove it from the `pool`, +Find a reusable `Connection` in the `pool`, or create a new `Connection` if required. """ -function getconnection(::Type{T}, host::String, port::UInt) where T <: IO +function getconnection(::Type{Connection{T}}, + host::AbstractString, port::UInt)::Connection{T} where T <: IO @lock poollock begin - pattern = x->(!x.writebusy && + pattern = x->(!isbusy(x) && typeof(x.io) == T && x.host == host && x.port == port) @@ -139,24 +154,23 @@ function getconnection(::Type{T}, host::String, port::UInt) where T <: IO if !isopen(c.io) deleteat!(pool, i) ;@debug 1 "Deleted: $c" continue - end - c.writebusy = true; ;@debug 2 "Reused: $c" + end; ;@debug 2 "Reused: $c" return c end c = Connection{T}(host, port) ;@debug 1 "New: $c" - c.writebusy = true push!(pool, c) + @assert !isbusy(c) return c end end function Base.show(io::IO, c::Connection) - print(io, c.host, ":", Int(c.port), ":", Int(localport(c)), ", ", + print(io, c.host, ":", Int(c.port), ":", #=Int(localport(c)), ", ", =# typeof(c.io), ", ", tcpstatus(c), ", ", - length(c.excess), "-byte excess", - islocked(c.readlock) ? ", readlock" : "") + length(c.excess), "-byte excess, reads/writes: ", + c.writecount, "/", c.readcount) end tcpsocket(c::Connection{SSLContext})::TCPSocket = c.io.bio diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl new file mode 100644 index 000000000..5ff7236db --- /dev/null +++ b/src/CookieRequest.jl @@ -0,0 +1,68 @@ +module CookieRequest + +export request + +import ..HTTP + +using ..URIs +using ..Cookies +using ..Messages + +import ..RetryRequest, ..@debug, ..getkv, ..setkv + + +const default_cookiejar = Dict{String, Set{Cookie}}() + + +function getcookies(cookies, uri) + + tosend = Vector{Cookie}() + expired = Vector{Cookie}() + + # Check if cookies should be added to outgoing request based on host... + for cookie in cookies + if Cookies.shouldsend(cookie, uri.scheme == "https", + uri.hostname, uri.path) + t = cookie.expires + if t != Dates.DateTime() && t < Dates.now(Dates.UTC) + @debug 1 "Deleting expired Cookie: $cookie.name" + push!(expired, cookie) + else + @debug 1 "Sending Cookie: $cookie.name to $host" + push!(tosend, cookie) + end + end + end + setdiff!(cookies, expired) + return tosend +end + + +function setcookies(cookies, host, headers) + for (k,v) in filter(x->x[1]=="Set-Cookie", headers) + @debug 1 "Set-Cookie: $v (from $host)" + push!(cookies, Cookies.readsetcookie(host, v)) + end +end + + +function request(method::String, uri, headers=[], body=""; + cookiejar=default_cookiejar, kw...) + + u = URI(uri) + hostcookies = get!(cookiejar, u.hostname, Set{Cookie}()) + + cookies = getcookies(hostcookies, u) + if !isempty(cookies) + setkv(headers, "Cookie", string(getkv(headers, "Cookie"), cookies)) + end + + res = RetryRequest.request(method, uri, headers, body; kw...) + + setcookies(hostcookies, u.hostname, res.headers) + + return res +end + + +end # module CookieRequest diff --git a/src/HTTP.jl b/src/HTTP.jl index 108530927..7d009e653 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -3,15 +3,18 @@ module HTTP #export Request, Response, FIFOBuffer + + using MbedTLS import MbedTLS.SSLContext using Retry const TLS = MbedTLS +const Headers = Vector{Pair{String, String}} import Base.== -const DEBUG_LEVEL = 0 +const DEBUG_LEVEL = 1 const DISABLE_CONNECTION_POOL = false @@ -31,24 +34,34 @@ end include("consts.jl") include("utils.jl") include("uri.jl") -using .URIs -include("fifobuffer.jl") -using .FIFOBuffers +#using .URIs +#include("fifobuffer.jl") +#using .FIFOBuffers include("cookies.jl") -using .Cookies -include("multipart.jl") -include("types.jl") +#using .Cookies +#include("multipart.jl") +#include("types.jl") include("parser.jl") -include("sniff.jl") +#include("sniff.jl") + + +include("IOExtras.jl") +using .IOExtras + +include("Bodies.jl") +#using .Bodies +include("Messages.jl") +#using .Messages include("Connect.jl") include("Connections.jl") - -#using .Connect #using .Connections -include("Messages.jl") + + +include("SendRequest.jl") + #include("client.jl") #include("handlers.jl") #using .Handlers @@ -58,9 +71,21 @@ include("Messages.jl") #include("precompile.jl") function __init__() +# global const client_module = module_parent(current_module()) # global const DEFAULT_CLIENT = Client() end +abstract type HTTPError <: Exception end + +struct StatusError <: HTTPError + status::Int16 + response::Messages.Response +end +StatusError(r::Messages.Response) = StatusError(r.status, r) + +include("RetryRequest.jl") +include("CookieRequest.jl") + end # module #= try diff --git a/src/IOExtras.jl b/src/IOExtras.jl new file mode 100644 index 000000000..4fc5ac221 --- /dev/null +++ b/src/IOExtras.jl @@ -0,0 +1,28 @@ +module IOExtras + +export unread!, closeread, closewrite + +""" + unread!(::IO, bytes) + +Push bytes back into a connection (to be returned by the next read). +""" + +function unread!(io, bytes) + println("WARNING: No unread! method for $(typeof(io))!") + println(" Discarding $(length(bytes)) bytes!") +end + + +""" + closewrite(::IO) + closeread(::IO) + +Signal end of write or read operations. +""" + +closewrite(io) = nothing +closeread(io) = close(io) + + +end diff --git a/src/Messages.jl b/src/Messages.jl index 2e89ecce6..da8d5aebb 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -1,27 +1,26 @@ module Messages export Message, Request, Response, Body, - method, header, setheader, request + method, iserror, isredirect, parentcount, + header, setheader, defaultheader, setlengthheader, + waitforheaders +import ..HTTP -include("Bodies.jl") -using .Bodies +using ..IOExtras +using ..Bodies import ..@lock import ..SSLContext import ..Parser import ..parse! import ..messagecomplete +import ..headerscomplete import ..waitingforeof import ..ParsingStateCode -import ..URIs: URI, scheme, hostname, port, resource -import ..HTTP: STATUS_CODES, getkey, setkey, @debug, DISABLE_CONNECTION_POOL +import ..ParsingError +import ..HTTP: getbyfirst, setbyfirst, @debug -if DISABLE_CONNECTION_POOL - using ..Connect -else - using ..Connections -end """ Request @@ -41,10 +40,13 @@ mutable struct Request parent end -Request(method="", uri="", headers=[], body=Body(); parent=nothing) = +Request() = Request("", "") +Request(method::String, uri, headers=[], body=Body(); parent=nothing) = Request(method, uri == "" ? "/" : uri, v"1.1", mkheaders(headers), body, parent) +Request(bytes) = read!(IOBuffer(bytes), Request()) + mkheaders(v::Vector{Pair{String,String}}) = v mkheaders(x) = [string(k) => string(v) for (k,v) in x] @@ -70,11 +72,24 @@ mutable struct Response headerscomplete::Condition end -Response(status=0, headers=[]; body=Body(), parent=nothing) = +Response(status::Int=0, headers=[]; body=Body(), parent=nothing) = Response(v"1.1", status, headers, body, parent, Condition()) +Response(bytes) = read!(IOBuffer(bytes), Response()) + + const Message = Union{Request,Response} +""" + iserror(::Response) + isredirect(::Response) + +Does this `Response` have an error or redirect status? +""" + +iserror(r::Response) = r.status < 200 || r.status >= 300 +isredirect(r::Response) = r.status in (301, 302, 307, 308) + """ method(::Response) @@ -85,13 +100,28 @@ Method of the `Request` that yielded this `Response`. method(r::Response) = r.parent == nothing ? "" : r.parent.method +""" + parentcount(::Response) + +How many redirect parents does this `Response` have? +""" + +function parentcount(r::Response) + if r.parent == nothing || r.parent.parent == nothing + return 0 + else + return 1 + parentcount(r.parent.parent) + end +end + + """ statustext(::Response) `String` representation of a HTTP status code. e.g. `200 => "OK"`. """ -statustext(r::Response) = Base.get(STATUS_CODES, r.status, "Unknown Code") +statustext(r::Response) = Base.get(HTTP.STATUS_CODES, r.status, "Unknown Code") """ @@ -108,7 +138,7 @@ waitforheaders(r::Response) = while r.status == 0; wait(r.headerscomplete) end Get header value for `key`. """ -header(m, k::String, default::String="") = getkey(m.headers, k, k => default)[2] +header(m, k::String, d::String="") = getbyfirst(m.headers, k, k => d)[2] """ @@ -116,7 +146,7 @@ header(m, k::String, default::String="") = getkey(m.headers, k, k => default)[2] Set header `value` for `key`. """ -setheader(m, v::Pair{String,String}) = setkey(m.headers, v) +setheader(m, v::Pair) = setbyfirst(m.headers, Pair{String,String}(v)) """ @@ -125,13 +155,34 @@ setheader(m, v::Pair{String,String}) = setkey(m.headers, v) Set header `value` for `key` if it is not already set. """ -function defaultheader(m, v::Pair{String,String}) +function defaultheader(m, v::Pair) if header(m, first(v)) == "" setheader(m, v) end end + +""" + setlengthheader(::Response, [, length]) + +Set the Content-Length or Transfer-Encoding header according to the +`Response` `Body`. +""" + +function setlengthheader(r::Request, l=-1) + + if !isstream(r.body) + l = length(r.body) + end + if l >= 0 + setheader(r, "Content-Length" => string(l)) + else + setheader(r, "Transfer-Encoding" => "chunked") + end +end + + """ appendheader(message, key => value) @@ -220,6 +271,9 @@ Read the start-line metadata from `Parser` into a `message` struct. function readstartline!(r::Response, p::Parser) r.version = VersionNumber(p.major, p.minor) r.status = p.status + if isredirect(r) + r.body = Body() + end notify(r.headerscomplete) yield() end @@ -227,7 +281,7 @@ end function readstartline!(r::Request, p::Parser) r.version = VersionNumber(p.major, p.minor) r.method = string(p.method) - r.uri = string(p.url) + r.uri = p.url end @@ -265,124 +319,41 @@ function Base.read!(io::IO, p::Parser) end if eof(io) && !waitingforeof(p) - throw(EOFError()) + throw(ParsingError(headerscomplete(p) ? HTTP.HPE_BODY_INCOMPLETE : + HTTP.HPE_HEADERS_INCOMPLETE)) end end """ - read!(io, message) + Parser(::Message) -Read data from `io` into a `Message` struct. +Create a parser that stores parsed data into a `Message`. """ - -function Base.read!(io::IO, m::Message) - +function Parser(m::Message) p = Parser() p.onbody = x->write(m.body, x) p.onheader = x->appendheader(m, x) p.onheaderscomplete = ()->readstartline!(m, p) p.isheadresponse = (isa(m, Response) && method(m) in ("HEAD", "CONNECT")) # FIXME CONNECT?? - - read!(io, p) - close(m.body) -end - - -""" - connecturi(::URI) - -Get a `Connection` for a `URI` from the connection pool. -""" - -function connecturi(uri::URI) - getconnection(scheme(uri) == "https" ? SSLContext : TCPSocket, - hostname(uri), - parse(UInt, port(uri))) + return p end """ - request(::URI, ::Request, ::Response) + read!(io, message) -Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. +Read data from `io` into a `Message` struct. """ -function request(uri::URI, req::Request, res::Response) - - #FIXME set Content-Length header? - - defaultheader(req, "Host" => hostname(uri)) - - io = connecturi(uri) - try ;@debug 1 "write to: $io\n$req" - write(io, req) - closewrite(io) - read!(io, res) ;@debug 2 "read from: $io\n$res" - closeread(io) - catch e - @schedule close(io) - rethrow(e) - end - - return res +function Base.read!(io::IO, m::Message) + read!(io, Parser(m)) + close(m.body) + return m end -""" - request(method, uri [, headers=[] [, body="" ]; kw args...) - -Execute a `Request` and return a `Response`. - -`parent=` optionally set a parent `Response`. - -`response_stream=` optional `IO` stream for response body. - - -e.g. use a stream as a request body: - -``` -io = open("request", "r") -r = request("POST", "http://httpbin.org/post", [], io) -``` - -e.g. send a response body to a stream: - -``` -io = open("response_file", "w") -r = request("GET", "http://httpbin.org/stream/100", response_stream=io) -println(stat("response_file").size) -0 -sleep(1) -println(stat("response_file").size) -14990 -``` -""" - -function request(method::String, uri, headers=[], body=""; - parent=nothing, response_stream=nothing) - - u = URI(uri) - - req = Request(method, - method == "CONNECT" ? host(u) : resource(u), - headers, - Body(body); - parent=parent) - - res = Response(body=Body(response_stream), parent=req) - - if isstream(res.body) - @schedule request(u, req, res) - waitforheaders(res) - else - request(u, req, res) - end - - return res -end - function Base.String(m::Message) io = IOBuffer() diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl new file mode 100644 index 000000000..280f9a865 --- /dev/null +++ b/src/RetryRequest.jl @@ -0,0 +1,34 @@ +module RetryRequest + +using Retry + +import ..HTTP + +export request + +import ..SendRequest, ..@debug, ..getkv + + +isrecoverable(e::Base.UVError) = true +isrecoverable(e::Base.DNSError) = true +isrecoverable(e::Base.EOFError) = true +isrecoverable(e::HTTP.StatusError) = e.status < 200 || e.status >= 500 +isrecoverable(e::Exception) = false + + +function request(a...; kw...) + + n = getkv(kw, :maxretries, 2) + 1 + + @repeat n try + return SendRequest.request(a...; kw...) + catch e + @delay_retry if isrecoverable(e) + @debug 1 "Retrying after $e" + end + end + + @assert false "Unreachable" +end + +end # module RetryRequest diff --git a/src/SendRequest.jl b/src/SendRequest.jl new file mode 100644 index 000000000..a4d9cb262 --- /dev/null +++ b/src/SendRequest.jl @@ -0,0 +1,152 @@ +module SendRequest + +import ..HTTP + +using ..URIs +using ..Messages +using ..Bodies + +using ..Connections +using ..IOExtras +using MbedTLS.SSLContext + +export request + +import ..@debug, ..getkv + + +""" + request(::IO, ::Request, ::Response) + +Send a `Request` and fill in a `Response`. +""" + +function request(io::IO, req::Request, res::Response) + + try ;@debug 1 "write to: $io\n$req" + write(io, req) + closewrite(io) + read!(io, res) + closeread(io) ;@debug 2 "read from: $io\n$res" + catch e + @schedule close(io) + rethrow(e) + end + + return res +end + + +""" + request(::URI, ::Request, ::Response) + +Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. +""" + +function request(uri::URI, req::Request, res::Response; kw...) + + defaultheader(req, "Host" => hostname(uri)) + setlengthheader(req, getkv(kw, :body_length, -1)) + + # Get a connection from the pool... + T = scheme(uri) == "https" ? SSLContext : TCPSocket + if getkv(kw, :use_connection_pool, true) + T = Connections.Connection{T} + end + io = getconnection(T, hostname(uri), parse(UInt, port(uri))) + + # Run request in a background task if response body is a stream... + if isstream(res.body) + @schedule request(io, req, res) + waitforheaders(res) + return res + end + + return request(io, req, res) +end + + +""" + request(method, uri [, headers=[] [, body="" ]; kw args...) + +Execute a `Request` and return a `Response`. + +`parent=` optionally set a parent `Response`. + +`response_stream=` optional `IO` stream for response body. + + +e.g. use a stream as a request body: + +``` +io = open("request", "r") +r = request("POST", "http://httpbin.org/post", [], io) +``` + +e.g. send a response body to a stream: + +``` +io = open("response_file", "w") +r = request("GET", "http://httpbin.org/stream/100", response_stream=io) +println(stat("response_file").size) +0 +sleep(1) +println(stat("response_file").size) +14990 +``` +""" + +function request(method::String, uri, headers=[], body=""; + parent=nothing, + response_stream=nothing, + kw...) + + u = URI(uri) + + req = Request(method, + method == "CONNECT" ? host(u) : resource(u), + headers, + Body(body); + parent=parent) + + res = Response(body=Body(response_stream), parent=req) + + request(u, req, res; kw...) + + if getkv(kw, :canonicalizeheaders, false) + res.headers = canonicalizeheaders(res.headers) + end + + # Redirect request to new location for: 301 Moved Permanently, 302 Found, + # 307 Temporary Redirect, and 308 Permanent Redirect... + if (isredirect(res) + && parentcount(res) < getkv(kw, :maxredirects, 3) + && header(res, "Location") != "" + && method != "HEAD") #FIXME why not redirect HEAD? + + return redirect(method, absuri(header(res, "Location"), uri), headers, body; + parent=res, response_stream=response_stream, kw...) + end + + # Throw StatusError for non Status-2xx Response Messages... + if iserror(res) && getkv(kw, :throw_status_errors, true) + throw(HTTP.StatusError(res)) + end + + return res +end + + +function redirect(method, uri, headers, body; kw...) + + if getkv(kw, :forwardheaders, true) + headers = filter((k,v)->!(k in ("Host", "Cookie")), headers) + else + headers = [] + end + + return request(method, uri, headers, body; kw...) +end + + +end # module SendRequest diff --git a/src/parser.jl b/src/parser.jl index 1a37cfb04..437a8a3e0 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -22,6 +22,10 @@ # IN THE SOFTWARE. # +#FIXME module Parser + +using .URIs + const start_state = s_start_req_or_res const strict = true @@ -38,14 +42,14 @@ mutable struct Parser method::HTTP.Method major::Int16 minor::Int16 - url::HTTP.URI + url::String status::Int32 onheader::Function onbody::Function onheaderscomplete::Function end -Parser() = Parser(start_state, 0x00, 0, 0, false, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, HTTP.URI(), 0, x->nothing, x->nothing, ()->nothing) +Parser() = Parser(start_state, 0x00, 0, 0, false, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, "", 0, x->nothing, x->nothing, ()->nothing) const DEFAULT_PARSER = Parser() @@ -62,7 +66,7 @@ function reset!(p::Parser) p.method = Method(0) p.major = 0 p.minor = 0 - p.url = HTTP.URI() + p.url = "" p.status = 0 p.onheader = x->nothing p.onbody = x->nothing @@ -88,45 +92,6 @@ function Base.show(io::IO, e::ParsingError) e.msg)) end -""" - HTTP.parse([HTTP.Request, HTTP.Response], str; kwargs...) - -Parse a `HTTP.Request` or `HTTP.Response` from a string. `str` must contain at least one -full request or response (but may include more than one). Supported keyword arguments include: - - * `extra`: a `Ref{String}` that will be used to store any extra bytes beyond a full request or response -""" -function parse(T::Type{<:Union{Request, Response}}, str; - extraref::Ref{SubArray{UInt8,1}}=Ref{SubArray{UInt8,1}}()) - - r = T(body=FIFOBuffer()) - p = DEFAULT_PARSER - reset!(p) -# p.isheadresponse = method in ("HEAD", "CONNECT") - p.onbody = x->write(r.body, x) - p.onheader = x->appendheader(r, x) - bytes = Vector{UInt8}(str) - n = parse!(p, bytes) - extra = view(bytes, n+1:length(bytes)) - if T == Request - r.uri = p.url - r.method = p.method - else - r.status = p.status - end - r.major = p.major - r.minor = p.minor - !headerscomplete(p) && throw(ParsingError(HPE_HEADERS_INCOMPLETE)) - if p.content_length != ULLONG_MAX && !messagecomplete(p) - throw(ParsingError(HPE_BODY_INCOMPLETE)) - end - if upgrade(p) - extraref[] = extra - end - close(r.body) - return r -end - macro errorif(cond, err) esc(:($cond && @err($err))) @@ -143,6 +108,8 @@ end const ByteView = typeof(view(UInt8[], 1:0)) +parse!(p::Parser, bytes::String)::Int = parse!(p, Vector{UInt8}(bytes)) + parse!(p::Parser, bytes)::Int = parse!(p, view(bytes, 1:length(bytes))) function parse!(parser::Parser, bytes::ByteView)::Int @@ -447,8 +414,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int if p_state >= s_req_http_start @debug(PARSING_DEBUG, "onurl $parser.method $(String(parser.valuebuffer))") - url = take!(parser.valuebuffer) - parser.url = URIs.http_parser_parse_url(url, 1, length(url), parser.method == CONNECT) + parser.url = take!(parser.valuebuffer) end elseif p_state == s_req_http_start @@ -1080,3 +1046,6 @@ function http_should_keep_alive(parser) return !http_message_needs_eof(parser) end + + +#FIXME end # module Parser diff --git a/src/uri.jl b/src/uri.jl index df7eb4ea5..81a6fa5f1 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -14,7 +14,8 @@ export URI, URL, hasport, port, resource, host, escape, unescape, - splitpath, queryparams + splitpath, queryparams, + absuri """ HTTP.URL(host; userinfo="", path="", query="", fragment="", isconnect=false) @@ -41,8 +42,16 @@ To access and return these components as strings, use the various accessor metho * `HTTP.resource`: returns the path-query-fragment combination """ struct URI - data::Vector{UInt8} - offsets::NTuple{7, Offset} +#= data::Vector{UInt8} + offsets::NTuple{7, Offset} =# + uri::String + scheme::SubString + hostname::SubString + port::SubString + path::SubString + query::SubString + fragment::SubString + userinfo::SubString end function URI(;hostname::AbstractString="", path::AbstractString="", @@ -52,7 +61,7 @@ function URI(;hostname::AbstractString="", path::AbstractString="", hostname != "" && scheme == "" && !isconnect && (scheme = "http") io = IOBuffer() printuri(io, scheme, userinfo, hostname, string(port), path, escape(query), fragment) - return Base.parse(URI, String(take!(io)); isconnect=isconnect) + return URI(String(take!(io)); isconnect=isconnect) end # we assume `str` is at least hostname & port @@ -79,6 +88,7 @@ function URL(str::AbstractString; userinfo::AbstractString="", path::AbstractStr end return Base.parse(URI, str; isconnect=isconnect) end + URI(str::AbstractString; isconnect::Bool=false) = Base.parse(URI, str; isconnect=isconnect) Base.parse(::Type{URI}, str::AbstractString; isconnect::Bool=false) = http_parser_parse_url(Vector{UInt8}(str), 1, sizeof(str), isconnect) @@ -89,21 +99,21 @@ Base.parse(::Type{URI}, str::AbstractString; isconnect::Bool=false) = http_parse fragment(a) == fragment(b) && userinfo(a) == userinfo(b) && ((!hasport(a) || !hasport(b)) || (port(a) == port(b))) - # accessors for uf in instances(http_parser_url_fields) uf == UF_MAX && break nm = lowercase(string(uf)[4:end]) has = Symbol(string("has", nm)) - @eval $has(uri::URI) = uri.offsets[Int($uf)].len > 0 + @eval $has(uri::URI) = !isempty(uri.$(Symbol(nm))) uf == UF_PORT && continue - @eval $(Symbol(nm))(uri::URI) = String(uri.data[uri.offsets[Int($uf)]]) + #@eval $(Symbol(nm))(uri::URI) = String(uri.data[uri.offsets[Int($uf)]]) + @eval $(Symbol(nm))(uri::URI) = uri.$(Symbol(nm)) end # special def for port function port(uri::URI) if hasport(uri) - return String(uri.data[uri.offsets[Int(UF_PORT)]]) + return uri.port else sch = scheme(uri) return sch == "http" ? "80" : sch == "https" ? "443" : "" @@ -114,7 +124,7 @@ resource(uri::URI; isconnect::Bool=false) = isconnect ? host(uri) : path(uri) * function host(uri::URI) h = hostname(uri) sch = scheme(uri) - p = String(uri.data[uri.offsets[Int(UF_PORT)]]) + p = uri.port if isempty(p) || (sch == "http" && p == "80") || (sch == "https" && p == "443") return h else @@ -125,7 +135,7 @@ end Base.show(io::IO, uri::URI) = print(io, "HTTP.URI(\"", uri, "\")") Base.print(io::IO, u::URI) = printuri(io, scheme(u), userinfo(u), hostname(u), port(u), path(u), query(u), fragment(u)) -function printuri(io::IO, sch::String, userinfo::String, hostname::String, port::String, path::String, query::String, fragment::String) +function printuri(io::IO, sch::AbstractString, userinfo::AbstractString, hostname::AbstractString, port::AbstractString, path::AbstractString, query::AbstractString, fragment::AbstractString) if sch in uses_authority print(io, sch, "://") !isempty(userinfo) && print(io, userinfo, "@") @@ -240,7 +250,7 @@ function splitpath(p::String) return elems end -absuri(u::String, context::URI) = absuri(URI(u), context) +absuri(u, context) = absuri(URI(u), URI(context)) function absuri(u::URI, context::URI) @@ -249,12 +259,13 @@ function absuri(u::URI, context::URI) s = scheme(u) h = hostname(u) n = port(u) - p = hostname(u) + p = path(u) q = query(u) return URIs.URI(scheme=isempty(s) ? scheme(context) : s, hostname=isempty(h) ? hostname(context) : h, port=isempty(n) ? port(context) : n, + path=isempty(p) ? path(context) : p, query=isempty(q) ? query(context) : q) end diff --git a/src/urlparser.jl b/src/urlparser.jl index 413eb84bf..93e068397 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -168,6 +168,8 @@ function http_parse_host(buf, host::Offset, foundat) return Offset(off, len), Offset(portoff, portlen), Offset(uioff, uilen) end +ufsubstring(uri, offset) = SubString(uri, offset.off, offset.off + offset.len-1) + function http_parser_parse_url(buf, startind=1, buflen=length(buf), isconnect::Bool=false) s = ifelse(isconnect, s_req_server_start, s_req_spaces_before_url) old_uf = UF_MAX @@ -243,11 +245,21 @@ function http_parser_parse_url(buf, startind=1, buflen=length(buf), isconnect::B chk = UF_HOSTNAME_MASK | UF_PORT_MASK ((mask | chk) > chk) && throw(URLParsingError("connect requests must contain and can only contain both hostname and port")) end - return URI(buf, (offsets[UF_SCHEME], + + uri = String(buf) + return URI(#=buf, (offsets[UF_SCHEME], offsets[UF_HOSTNAME], offsets[UF_PORT], offsets[UF_PATH], offsets[UF_QUERY], offsets[UF_FRAGMENT], - offsets[UF_USERINFO])) + offsets[UF_USERINFO]),=# + uri, + ufsubstring(uri, offsets[UF_SCHEME]), + ufsubstring(uri, offsets[UF_HOSTNAME]), + ufsubstring(uri, offsets[UF_PORT]), + ufsubstring(uri, offsets[UF_PATH]), + ufsubstring(uri, offsets[UF_QUERY]), + ufsubstring(uri, offsets[UF_FRAGMENT]), + ufsubstring(uri, offsets[UF_USERINFO])) end diff --git a/src/utils.jl b/src/utils.jl index f1ba65b4b..10077460a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -191,21 +191,59 @@ macro lock(l, expr) end) end -function setkey(c, v) - k = first(v) + +""" + setbyfirst(collection, item) -> item + +Set `item` in a `collection`. +If `first() of an exisiting matches `first(item)` it is replaced. +Otherwise the new `item` is inserted at the end of the `collection`. +""" + +function setbyfirst(c, item) + k = first(item) if (i = findfirst(x->first(x) == k, c)) > 0 - c[i] = v + c[i] = item else - push!(c, v) + push!(c, item) end - return v + return item end -function getkey(c, k, default=nothing) + +""" + getbyfirst(collection, key [, default]) -> item + +Get `item` from collection where `first(item)` matches `key`. +""" + +function getbyfirst(c, k, default=nothing) i = findfirst(x->first(x) == k, c) return i > 0 ? c[i] : default end + +""" + setkv(collection, key, value) + +Set `value` for `key` in collection of key/value `Pairs`. +""" + +setkv(c, k, v) = setbyfirst(c, k => v) + +""" + getkv(collection, key [, default]) -> value + +Get `value` for `key` in collection of key/value `Pairs`, +where `first(item) == key` and `value = item[2]` +""" + +function getkv(c, k, default=nothing) + i = findfirst(x->first(x) == k, c) + return i > 0 ? c[i][2] : default +end + + macro catch(etype, expr) esc(quote try diff --git a/test/body.jl b/test/body.jl index 8ee04b9be..729ce345d 100644 --- a/test/body.jl +++ b/test/body.jl @@ -1,6 +1,6 @@ -using HTTP.Messages.Bodies +using HTTP.Bodies -@testset "HTTP.Messages.Body" begin +@testset "HTTP.Bodies" begin @test String(take!(Body("Hello!"))) == "Hello!" @test String(take!(Body(IOBuffer("Hello!")))) == "Hello!" @@ -15,7 +15,7 @@ using HTTP.Messages.Bodies sleep(0.1) close(io) end - @test String(take!(Body(io))) == "Hello!" + @test String(take!(Body(io))) == "5\r\nHello\r\n1\r\n!\r\n0\r\n\r\n" b = Body() write(b, "Hello") @@ -42,12 +42,12 @@ using HTTP.Messages.Bodies show(buf, b) @test String(take!(buf)) == "Hello!\nWorld!\n" - tmp = HTTP.Messages.Bodies.body_show_max - HTTP.Messages.Bodies.set_show_max(12) + tmp = HTTP.Bodies.body_show_max + HTTP.Bodies.set_show_max(12) b = Body("Hello World!xxx") #display(b); println() buf = IOBuffer() show(buf, b) @test String(take!(buf)) == "Hello World!\n⋮\n15-byte body\n" - HTTP.Messages.Bodies.set_show_max(tmp) + HTTP.Bodies.set_show_max(tmp) end diff --git a/test/messages.jl b/test/messages.jl index de0616a40..8f482400b 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -1,5 +1,8 @@ - using HTTP.Messages +import HTTP.Messages.appendheader + +using HTTP.CookieRequest +using HTTP.StatusError using JSON @@ -22,14 +25,14 @@ using JSON setheader(req, "X" => "Y") @test header(req, "X") == "Y" - HTTP.Messages.appendheader(req, "" => "Z") + appendheader(req, "" => "Z") @test header(req, "X") == "YZ" - HTTP.Messages.appendheader(req, "X" => "more") + appendheader(req, "X" => "more") @test header(req, "X") == "YZ, more" - HTTP.Messages.appendheader(req, "Set-Cookie" => "A") - HTTP.Messages.appendheader(req, "Set-Cookie" => "B") + appendheader(req, "Set-Cookie" => "A") + appendheader(req, "Set-Cookie" => "B") @test filter(x->first(x) == "Set-Cookie", req.headers) == ["Set-Cookie" => "A", "Set-Cookie" => "B"] @@ -62,8 +65,30 @@ using JSON for m in ["GET", "HEAD", "OPTIONS"] @test request(m, "$sch://httpbin.org/ip").status == 200 end - @test request("POST", "$sch://httpbin.org/ip").status == 405 + try + request("POST", "$sch://httpbin.org/ip") + @test false + catch e + @test isa(e, StatusError) + @test e.status == 405 + end + end + +#= + @sync begin + io = BufferStream() + @async begin + for i = 1:100 + sleep(0.1) + write(io, "Hello!") + end + close(io) + end + yield() + r = request("POST", "http://httpbin.org/post", [], io) + @test r.status == 200 end +=# for sch in ["http", "https"] for m in ["POST", "PUT", "DELETE", "PATCH"] @@ -89,6 +114,7 @@ using JSON end end + for sch in ["http", "https"] log_buffer = Vector{String}() @@ -111,6 +137,7 @@ using JSON end end + @sync begin async_get("$sch://httpbin.org/stream/100?req=1") async_get("$sch://httpbin.org/stream/100?req=2") diff --git a/test/parser.jl b/test/parser.jl index 2617a8e9a..72c56452f 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1,3 +1,12 @@ +using HTTP.Messages + +import Base.== + +==(a::Request,b::Request) = (a.method == b.method) && + (a.version == b.version) && + (a.headers == b.headers) && + (HTTP.Bodies.collect!(a.body) == HTTP.Bodies.collect!(b.body)) + mutable struct Message name::String raw::String @@ -1369,63 +1378,55 @@ const responses = Message[ println("TEST - parser.jl - Request $t: $(req.name)") upgrade = Ref{SubArray{UInt8, 1}}() if t > 0 - body=FIFOBuffer() - r = HTTP.Request(body=body) - p = HTTP.DEFAULT_PARSER - HTTP.reset!(p) - p.onbody = x->write(body, x) - p.onheader = x->HTTP.appendheader(r, x) - - bytes = Vector{UInt8}(req.raw) - sz = t - for i in 1:sz:length(bytes) - x = bytes[i:min(i+sz-1, length(bytes))] - #@show [Char(x[i]) for i in 1:length(x)] - HTTP.parse!(p, x) - r.uri = HTTP.DEFAULT_PARSER.url - r.method = HTTP.DEFAULT_PARSER.method - r.major = HTTP.DEFAULT_PARSER.major - r.minor = HTTP.DEFAULT_PARSER.minor + @sync begin + r = Request() + p = Messages.Parser(r) + bytes = Vector{UInt8}(req.raw) + sz = t + for i in 1:sz:length(bytes) + HTTP.parse!(p, view(bytes, i:min(i+sz-1, length(bytes)))) + end end elseif t < 0 - body=FIFOBuffer() - r = HTTP.Request(body=body) - p = HTTP.DEFAULT_PARSER - HTTP.reset!(p) - p.onbody = x->write(body, x) - p.onheader = x->HTTP.appendheader(r, x) - bytes = Vector{UInt8}(req.raw) - i = rand(2:length(bytes)) - HTTP.parse!(p, bytes[1:i-1]) - HTTP.parse!(p, bytes[i:end]) - r.uri = HTTP.DEFAULT_PARSER.url - r.method = HTTP.DEFAULT_PARSER.method - r.major = HTTP.DEFAULT_PARSER.major - r.minor = HTTP.DEFAULT_PARSER.minor + @sync begin + r = Request() + io = BufferStream() + bytes = Vector{UInt8}(req.raw) + sz = t + @async begin + i = rand(2:length(bytes)) + write(io, bytes[1:i-1]) + yield() + write(io, bytes[i:end]) + end + read!(io, r) + end else - r = HTTP.parse(HTTP.Request, req.raw; extraref=upgrade) + r = Request(req.raw) + #r = HTTP.parse(HTTP.Request, req.raw; extraref=upgrade) end - @test HTTP.major(r) == req.http_major - @test HTTP.minor(r) == req.http_minor - @test HTTP.method(r) == req.method - @test HTTP.query(HTTP.uri(r)) == req.query_string - @test HTTP.fragment(HTTP.uri(r)) == req.fragment - @test HTTP.path(HTTP.uri(r)) == req.request_path - @test HTTP.hostname(HTTP.uri(r)) == req.host - @test HTTP.userinfo(HTTP.uri(r)) == req.userinfo - @test HTTP.port(HTTP.uri(r)) in (req.port, "80", "443") - @test string(HTTP.uri(r)) == req.request_url - @test length(HTTP.headers(r)) == req.num_headers - @test HTTP.canonicalizeheaders(HTTP.headers(r)) == Dict(req.headers) - @test String(readavailable(HTTP.body(r))) == req.body - @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == req.should_keep_alive + uri = parse(HTTP.URI, r.uri; isconnect= req.method == HTTP.CONNECT) + @test r.version.major == req.http_major + @test r.version.minor == req.http_minor + @test r.method == string(req.method) + @test uri.query == req.query_string + @test uri.fragment == req.fragment + @test uri.path == req.request_path + @test uri.hostname == req.host + @test uri.userinfo == req.userinfo + @test uri.port in (req.port, "80", "443") + @test string(uri) == req.request_url + @test length(r.headers) == req.num_headers + @test Dict(HTTP.canonicalizeheaders(r.headers)) == Dict(req.headers) + @test String(take!(r.body)) == req.body +# FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == req.should_keep_alive if isassigned(upgrade) @show String(collect(upgrade[])) end - @test t != 0 || - req.upgrade == "" && !isassigned(upgrade) || - String(collect(upgrade[])) == req.upgrade +# FIXME @test t != 0 || +# req.upgrade == "" && !isassigned(upgrade) || +# String(collect(upgrade[])) == req.upgrade end reqstr = "GET http://www.techcrunch.com/ HTTP/1.1\r\n" * @@ -1439,41 +1440,38 @@ const responses = Message[ "Content-Length: 7\r\n" * "Proxy-Connection: keep-alive\r\n\r\n1234567" - req = HTTP.Request() - req.uri = HTTP.URI("http://www.techcrunch.com/") + req = Request("GET", "http://www.techcrunch.com/") req.headers = ["Host"=>"www.techcrunch.com","User-Agent"=>"Fake","Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language"=>"en-us,en;q=0.5","Accept-Encoding"=>"gzip,deflate","Accept-Charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7","Keep-Alive"=>"300","Content-Length"=>"7","Proxy-Connection"=>"keep-alive"] - req.body = FIFOBuffer("1234567") + req.body = Body("1234567") - @test HTTP.parse(HTTP.Request, reqstr).headers == req.headers - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr).headers == req.headers + @test Request(reqstr) == req reqstr = "GET / HTTP/1.1\r\n" * "Host: foo.com\r\n\r\n" - req = HTTP.Request() - req.uri = HTTP.URI("/") + req = Request("GET", "/") req.headers = ["Host"=>"foo.com"] - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req reqstr = "GET //user@host/is/actually/a/path/ HTTP/1.1\r\n" * "Host: test\r\n\r\n" - req = HTTP.Request() - req.uri = HTTP.URI("//user@host/is/actually/a/path/") + req = Request("GET", "//user@host/is/actually/a/path/") req.headers = ["Host"=>"test"] - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req reqstr = "GET ../../../../etc/passwd HTTP/1.1\r\n" * "Host: test\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + @test_throws HTTP.ParsingError Request(reqstr) reqstr = "GET HTTP/1.1\r\n" * "Host: test\r\n\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + @test_throws HTTP.ParsingError Request(reqstr) reqstr = "POST / HTTP/1.1\r\n" * "Host: foo.com\r\n" * @@ -1484,13 +1482,13 @@ const responses = Message[ "Trailer-Key: Trailer-Value\r\n" * "\r\n" - req = HTTP.Request() + req = Request() req.method = "POST" - req.uri = HTTP.URI("/") + req.uri = "/" req.headers = ["Host"=>"foo.com", "Transfer-Encoding"=>"chunked", "Trailer-Key"=>"Trailer-Value"] - req.body = HTTP.FIFOBuffer("foobar") + req.body = Body("foobar") - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req reqstr = "POST / HTTP/1.1\r\n" * "Host: foo.com\r\n" * @@ -1501,23 +1499,23 @@ const responses = Message[ "0\r\n" * "\r\n" - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + @test_throws HTTP.ParsingError Request(reqstr) reqstr = "CONNECT www.google.com:443 HTTP/1.1\r\n\r\n" - req = HTTP.Request() + req = Request() req.method = "CONNECT" - req.uri = HTTP.URI("www.google.com:443"; isconnect=true) + req.uri = "www.google.com:443" # FIXME; isconnect=true) - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req reqstr = "CONNECT 127.0.0.1:6060 HTTP/1.1\r\n\r\n" - req = HTTP.Request() + req = Request() req.method = "CONNECT" - req.uri = HTTP.URI("127.0.0.1:6060"; isconnect=true) + req.uri = "127.0.0.1:6060" #FIXME; isconnect=true) - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req # reqstr = "CONNECT /_goRPC_ HTTP/1.1\r\n\r\n" # @@ -1529,38 +1527,37 @@ const responses = Message[ reqstr = "NOTIFY * HTTP/1.1\r\nServer: foo\r\n\r\n" - req = HTTP.Request() + req = Request() req.method = "NOTIFY" - req.uri = HTTP.URI("*") + req.uri = "*" req.headers = ["Server"=>"foo"] - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req reqstr = "OPTIONS * HTTP/1.1\r\nServer: foo\r\n\r\n" - req = HTTP.Request() + req = Request() req.method = "OPTIONS" - req.uri = HTTP.URI("*") + req.uri = "*" req.headers = ["Server"=>"foo"] - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req reqstr = "GET / HTTP/1.1\r\nHost: issue8261.com\r\nConnection: close\r\n\r\n" - req = HTTP.Request() - req.uri = HTTP.URI("/") + req = Request("GET", "/") req.headers = ["Host"=>"issue8261.com", "Connection"=>"close"] - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req reqstr = "HEAD / HTTP/1.1\r\nHost: issue8261.com\r\nConnection: close\r\nContent-Length: 0\r\n\r\n" - req = HTTP.Request() + req = Request() req.method = "HEAD" - req.uri = HTTP.URI("/") + req.uri = "/" req.headers = ["Host"=>"issue8261.com", "Connection"=>"close", "Content-Length"=>"0"] - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req reqstr = "POST /cgi-bin/process.cgi HTTP/1.1\r\n" * "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\n" * @@ -1572,9 +1569,9 @@ const responses = Message[ "Connection: Keep-Alive\r\n\r\n" * "first=Zara&last=Ali\r\n\r\n" - req = HTTP.Request() + req = Request() req.method = "POST" - req.uri = HTTP.URI("/cgi-bin/process.cgi") + req.uri = "/cgi-bin/process.cgi" req.headers = ["User-Agent"=>"Mozilla/4.0 (compatible; MSIE5.01; Windows NT)", "Host"=>"www.tutorialspoint.com", "Content-Type"=>"text/xml; charset=utf-8", @@ -1582,44 +1579,34 @@ const responses = Message[ "Accept-Language"=>"en-us", "Accept-Encoding"=>"gzip, deflate", "Connection"=>"Keep-Alive"] - req.body = HTTP.FIFOBuffer("first=Zara&last=Ali") + req.body = Body("first=Zara&last=Ali") - @test HTTP.parse(HTTP.Request, reqstr) == req + @test Request(reqstr) == req end - @testset "HTTP.parse(HTTP.Response, str)" begin + @testset "Response(str)" begin for resp in responses, t in [0, 1, 2, 3, 4, 11, 13, 17, 19, 23, 29, 31, 32] println("TEST - parser.jl - Response $t: $(resp.name)") try if t > 0 - body=FIFOBuffer() - r = HTTP.Response(body=body) - p = HTTP.DEFAULT_PARSER - HTTP.reset!(p) - p.onbody = x->write(body, x) - p.onheader = x->HTTP.appendheader(r, x) + r = Response() + p = Messages.Parser(r) bytes = Vector{UInt8}(resp.raw) sz = t for i in 1:sz:length(bytes) - x = bytes[i:min(i+sz-1, length(bytes))] - #@show [Char(x[i]) for i in 1:length(x)] - HTTP.parse!(p, x) - r.major = HTTP.DEFAULT_PARSER.major - r.minor = HTTP.DEFAULT_PARSER.minor - r.status = HTTP.DEFAULT_PARSER.status - + HTTP.parse!(p, view(bytes, i:min(i+sz-1, length(bytes)))) end else - r = HTTP.parse(HTTP.Response, resp.raw) + r = Response(resp.raw) end - @test HTTP.major(r) == resp.http_major - @test HTTP.minor(r) == resp.http_minor - @test HTTP.status(r) == resp.status_code - @test HTTP.statustext(r) == resp.response_status - @test length(HTTP.headers(r)) == resp.num_headers - @test HTTP.canonicalizeheaders(HTTP.headers(r)) == Dict(resp.headers) - @test String(readavailable(HTTP.body(r))) == resp.body - @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == resp.should_keep_alive + @test r.version.major == resp.http_major + @test r.version.minor == resp.http_minor + @test r.status == resp.status_code + @test HTTP.Messages.statustext(r) == resp.response_status + @test length(r.headers) == resp.num_headers + @test Dict(HTTP.canonicalizeheaders(r.headers)) == Dict(resp.headers) + @test String(take!(r.body)) == resp.body +# FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == resp.should_keep_alive catch e if HTTP.strict && isa(e, HTTP.ParsingError) println("HTTP.strict is enabled. ParsingError ignored.") @@ -1632,7 +1619,7 @@ const responses = Message[ @testset "HTTP.parse errors" begin reqstr = "GET / HTTP/1.1\r\n" * "Foo: F\01ailure\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) if !HTTP.strict r = HTTP.parse(HTTP.Request, reqstr) @test HTTP.method(r) == HTTP.GET @@ -1641,7 +1628,7 @@ const responses = Message[ end reqstr = "GET / HTTP/1.1\r\n" * "Foo: B\02ar\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) if !HTTP.strict r = HTTP.parse(HTTP.Request, reqstr) @test HTTP.method(r) == HTTP.GET @@ -1650,7 +1637,7 @@ const responses = Message[ end respstr = "HTTP/1.1 200 OK\r\n" * "Foo: F\01ailure\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) if !HTTP.strict r = HTTP.parse(HTTP.Response, respstr) @test HTTP.status(r) == 200 @@ -1658,7 +1645,7 @@ const responses = Message[ end respstr = "HTTP/1.1 200 OK\r\n" * "Foo: B\02ar\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) if !HTTP.strict r = HTTP.parse(HTTP.Response, respstr) @test HTTP.status(r) == 200 @@ -1666,38 +1653,38 @@ const responses = Message[ end reqstr = "GET / HTTP/1.1\r\n" * "Fo@: Failure" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) - !HTTP.strict && (@test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr)) + HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) + !HTTP.strict && (@test_throws HTTP.ParsingError Request(reqstr)) reqstr = "GET / HTTP/1.1\r\n" * "Foo\01\test: Bar" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) - !HTTP.strict && (@test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr)) + HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) + !HTTP.strict && (@test_throws HTTP.ParsingError Request(reqstr)) respstr = "HTTP/1.1 200 OK\r\n" * "Fo@: Failure" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) - !HTTP.strict && (@test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr)) + HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) + !HTTP.strict && (@test_throws HTTP.ParsingError Response(respstr)) respstr = "HTTP/1.1 200 OK\r\n" * "Foo\01\test: Bar" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) - !HTTP.strict && (@test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr)) + HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) + !HTTP.strict && (@test_throws HTTP.ParsingError Response(respstr)) reqstr = "GET / HTTP/1.1\r\n" * "Content-Length: 0\r\nContent-Length: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: 0\r\nContent-Length: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) reqstr = "GET / HTTP/1.1\r\n" * "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) reqstr = "GET / HTTP/1.1\r\n" * "Foo: 1\rBar: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, reqstr) + HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Foo: 1\rBar: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError HTTP.parse(HTTP.Response, respstr) + HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) - for r in ((HTTP.Request, "GET / HTTP/1.1\r\n"), (HTTP.Response, "HTTP/1.0 200 OK\r\n")) + for r in ((Request, "GET / HTTP/1.1\r\n"), (Response, "HTTP/1.0 200 OK\r\n")) HTTP.reset!(HTTP.DEFAULT_PARSER) R = r[1]() n = HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) @@ -1707,72 +1694,51 @@ const responses = Message[ end buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n" - @test_throws HTTP.ParsingError r = HTTP.parse(HTTP.Request, buf) + @test_throws HTTP.ParsingError r = Request(buf) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "1844674407370955160" * "\r\n\r\n" - r = HTTP.Response(body=FIFOBuffer()) - HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) - HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) - HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) - @test HTTP.status(r) == 200 - @test HTTP.headers(r) == Dict("Content-Length"=>"1844674407370955160") + r = Response() + p = Messages.Parser(r) + HTTP.parse!(p, respstr) + @test r.status == 200 + @test r.headers == ["Content-Length"=>"1844674407370955160"] respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551615" * "\r\n\r\n" - r = HTTP.Response(body=FIFOBuffer()) - HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) - HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) - e = try HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + e = try Response(respstr) catch e e end @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH + respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551616" * "\r\n\r\n" - r = HTTP.Response(body=FIFOBuffer()) - HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) - HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) - e = try HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + e = try Response(respstr) catch e e end @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." - r = HTTP.Response(body=FIFOBuffer()) - HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) - HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) - HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) - @test HTTP.status(r) == 200 - @test HTTP.headers(r) == Dict("Transfer-Encoding"=>"chunked") + r = Response() + p = Messages.Parser(r) + HTTP.parse!(p, respstr) + @test r.status == 200 + @test r.headers == ["Transfer-Encoding"=>"chunked"] respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFF" * "\r\n..." - r = HTTP.Response(body=FIFOBuffer()) - HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) - HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) - e = try HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + e = try Response(respstr) catch e e end @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "10000000000000000" * "\r\n..." - r = HTTP.Response(body=FIFOBuffer()) - HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) - HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) - e = try HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(respstr)) catch e e end + e = try Response(respstr) catch e e end @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH - p = HTTP.Parser() for len in (1000, 100000) HTTP.reset!(p) reqstr = "POST / HTTP/1.0\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" - r = HTTP.Request() - p.onbody = x->write(r.body, x) - p.onheader = x->HTTP.appendheader(r, x) - HTTP.parse!(p, Vector{UInt8}(reqstr)) + r = Request() + p = Messages.Parser(r) + HTTP.parse!(p, reqstr) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) for i = 1:len-1 - HTTP.parse!(p, Vector{UInt8}("a")) + HTTP.parse!(p, "a") @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) end - HTTP.parse!(p, Vector{UInt8}("a")) + HTTP.parse!(p, "a") @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) end @@ -1780,28 +1746,25 @@ const responses = Message[ for len in (1000, 100000) HTTP.reset!(p) respstr = "HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" - r = HTTP.Response() - p.onbody = x->write(r.body, x) - p.onheader = x->HTTP.appendheader(r, x) - HTTP.parse!(p, Vector{UInt8}(respstr)) + r = Response() + p = Messages.Parser(r) + HTTP.parse!(p, respstr) @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) for i = 1:len-1 - HTTP.parse!(p, Vector{UInt8}("a")) + HTTP.parse!(p, "a") @test HTTP.headerscomplete(p) @test !HTTP.messagecomplete(p) end - HTTP.parse!(p, Vector{UInt8}("a")) + HTTP.parse!(p, "a") @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) end reqstr = requests[1].raw * requests[2].raw - HTTP.reset!(p) - r = HTTP.Request() - p.onbody = x->write(r.body, x) - p.onheader = x->HTTP.appendheader(r, x) - n = HTTP.parse!(p, Vector{UInt8}(reqstr)) + r = Request() + p = Messages.Parser(r) + n = HTTP.parse!(p, reqstr) @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) ex = Vector{UInt8}(reqstr)[n+1:end] @@ -1810,30 +1773,28 @@ const responses = Message[ @test HTTP.headerscomplete(p) @test HTTP.messagecomplete(p) - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, "GET / HTP/1.1\r\n\r\n") + @test_throws HTTP.ParsingError Request("GET / HTP/1.1\r\n\r\n") - r = HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n" * "Test: Düsseldorf\r\n\r\n") - @test HTTP.headers(r) == Dict("Test" => "Düsseldorf") + r = Request("GET / HTTP/1.1\r\n" * "Test: Düsseldorf\r\n\r\n") + @test r.headers == ["Test" => "Düsseldorf"] - r = HTTP.Response(body=FIFOBuffer()) - HTTP.reset!(HTTP.DEFAULT_PARSER) - HTTP.DEFAULT_PARSER.onbody = x->write(r.body, x) - HTTP.DEFAULT_PARSER.onheader = x->HTTP.appendheader(r, x) - HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}("GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba")) - @test String(readavailable(r.body)) == "fooba" + r = Response() + p = Messages.Parser(r) + HTTP.parse!(p, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") + @test String(take!(r.body)) == "fooba" for m in instances(HTTP.Method) m == HTTP.CONNECT && continue me = m == HTTP.MSEARCH ? "M-SEARCH" : "$m" - r = HTTP.parse(HTTP.Request, "$me / HTTP/1.1\r\n\r\n") - @test HTTP.method(r) == m + r = Request("$me / HTTP/1.1\r\n\r\n") + @test r.method == string(m) end for m in ("ASDF","C******","COLA","GEM","GETA","M****","MKCOLA","PROPPATCHA","PUN","PX","SA","hello world") - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, "$m / HTTP/1.1\r\n\r\n") + @test_throws HTTP.ParsingError Request("$m / HTTP/1.1\r\n\r\n") end - @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n" * "name\r\n" * " : value\r\n\r\n") + @test_throws HTTP.ParsingError Request("GET / HTTP/1.1\r\n" * "name\r\n" * " : value\r\n\r\n") reqstr = "GET / HTTP/1.1\r\n" * "X-SSL-FoooBarr: -----BEGIN CERTIFICATE-----\r\n" * @@ -1870,12 +1831,14 @@ const responses = Message[ "\t-----END CERTIFICATE-----\r\n" * "\r\n" - r = HTTP.parse(HTTP.Request, reqstr) - @test HTTP.method(r) == HTTP.GET + r = Request(reqstr) + @test r.method == "GET" + + @test "GET / HTTP/1.1X-SSL-FoooBarr: $(header(r, "X-SSL-FoooBarr"))" == replace(reqstr, "\r\n", "") # @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection\r\033\065\325eep-Alive\r\nAccept-Encoding: gzip\r\n\r\n") - r = HTTP.parse(HTTP.Request, "GET /bad_get_no_headers_no_body/world HTTP/1.1\r\nAccept: */*\r\n\r\nHELLO") - @test String(readavailable(HTTP.body(r))) == "" + r = Request("GET /bad_get_no_headers_no_body/world HTTP/1.1\r\nAccept: */*\r\n\r\nHELLO") + @test String(take!(r.body)) == "" end end # @testset HTTP.parse diff --git a/test/runtests.jl b/test/runtests.jl index f4c981c22..b36907a70 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,5 @@ -using HTTP, Base.Test +using HTTP +using Base.Test if VERSION < v"0.7.0-DEV.2575" const Dates = Base.Dates @@ -7,12 +8,12 @@ else end @testset "HTTP" begin - #include("utils.jl"); + include("utils.jl"); #include("fifobuffer.jl"); #include("sniff.jl"); #include("uri.jl"); #include("cookies.jl"); - #include("parser.jl"); + include("parser.jl"); include("body.jl"); include("messages.jl"); #include("types.jl"); From 573867f8a37f97b79ca2cd76934ddec4f7073e28 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 10 Dec 2017 22:55:23 +1100 Subject: [PATCH 032/182] Move default port (80/443) to getconnection. Change `hostname` to `host` per obsoleting of `hostname` in RFC: https://tools.ietf.org/html/rfc3986#appendix-D.2 Add `hostport` that returns e.g. "foobar.com:8080" or "[1:2::3:4]:8080" (old `host` function returned "foobar.com8080" or "1:2::3:48080"). Replace Offset with SubString in urlparser.jl --- src/Connect.jl | 12 +- src/Connections.jl | 14 +- src/CookieRequest.jl | 6 +- src/SendRequest.jl | 4 +- src/uri.jl | 147 ++++++++++--------- src/urlparser.jl | 143 +++++++++---------- test/parser.jl | 2 +- test/runtests.jl | 2 +- test/uri.jl | 333 ++++++++++++++++++++++--------------------- 9 files changed, 336 insertions(+), 327 deletions(-) diff --git a/src/Connect.jl b/src/Connect.jl index 0951fac03..9e16b78fc 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -17,12 +17,16 @@ The `Connections` module has the same interface but supports connection reuse and request interleaving. """ -function getconnection(::Type{TCPSocket}, host::AbstractString, port::UInt)::TCPSocket - @debug 2 "TCP connect: $host:$port..." - connect(getaddrinfo(host), port) +function getconnection(::Type{TCPSocket}, host::AbstractString, + port::AbstractString)::TCPSocket + p::UInt = isempty(port) ? UInt(80) : parse(UInt, port) + @debug 2 "TCP connect: $host:$p..." + connect(getaddrinfo(host), p) end -function getconnection(::Type{SSLContext}, host::AbstractString, port::UInt)::SSLContext +function getconnection(::Type{SSLContext}, host::AbstractString, + port::AbstractString)::SSLContext + port = isempty(port) ? "443" : port @debug 2 "SSL connect: $host:$port..." io = SSLContext() setup!(io, SSLConfig(false)) diff --git a/src/Connections.jl b/src/Connections.jl index d8ad3141b..28a03ca6f 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -30,7 +30,7 @@ Response must be read before another Request can be written. mutable struct Connection{T <: IO} <: IO host::String - port::UInt + port::String io::T excess::ByteView writecount::Int @@ -41,9 +41,9 @@ end isbusy(c::Connection) = c.writecount - c.readcount > 1 Connection{T}() where T <: IO = - Connection{T}("", 0, T(), view(UInt8[], 1:0), 0, 0, Condition()) + Connection{T}("", "", T(), view(UInt8[], 1:0), 0, 0, Condition()) -function Connection{T}(host::AbstractString, port::UInt) where T <: IO +function Connection{T}(host::AbstractString, port::AbstractString) where T <: IO c = Connection{T}() c.host = host c.port = port @@ -140,7 +140,8 @@ or create a new `Connection` if required. """ function getconnection(::Type{Connection{T}}, - host::AbstractString, port::UInt)::Connection{T} where T <: IO + host::AbstractString, + port::AbstractString)::Connection{T} where T <: IO @lock poollock begin @@ -167,7 +168,7 @@ end function Base.show(io::IO, c::Connection) - print(io, c.host, ":", Int(c.port), ":", #=Int(localport(c)), ", ", =# + print(io, c.host, ":", c.port, ":", Int(localport(c)), ", ", typeof(c.io), ", ", tcpstatus(c), ", ", length(c.excess), "-byte excess, reads/writes: ", c.writecount, "/", c.readcount) @@ -176,7 +177,8 @@ end tcpsocket(c::Connection{SSLContext})::TCPSocket = c.io.bio tcpsocket(c::Connection{TCPSocket})::TCPSocket = c.io -localport(c::Connection) = VERSION > v"0.7.0-DEV" ? +localport(c::Connection) = !isopen(c.io) ? "?" : + VERSION > v"0.7.0-DEV" ? getsockname(tcpsocket(c))[2] : Base._sockname(tcpsocket(c), true)[2] diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index 5ff7236db..cb7118d1f 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -22,7 +22,7 @@ function getcookies(cookies, uri) # Check if cookies should be added to outgoing request based on host... for cookie in cookies if Cookies.shouldsend(cookie, uri.scheme == "https", - uri.hostname, uri.path) + uri.host, uri.path) t = cookie.expires if t != Dates.DateTime() && t < Dates.now(Dates.UTC) @debug 1 "Deleting expired Cookie: $cookie.name" @@ -50,7 +50,7 @@ function request(method::String, uri, headers=[], body=""; cookiejar=default_cookiejar, kw...) u = URI(uri) - hostcookies = get!(cookiejar, u.hostname, Set{Cookie}()) + hostcookies = get!(cookiejar, u.host, Set{Cookie}()) cookies = getcookies(hostcookies, u) if !isempty(cookies) @@ -59,7 +59,7 @@ function request(method::String, uri, headers=[], body=""; res = RetryRequest.request(method, uri, headers, body; kw...) - setcookies(hostcookies, u.hostname, res.headers) + setcookies(hostcookies, u.host, res.headers) return res end diff --git a/src/SendRequest.jl b/src/SendRequest.jl index a4d9cb262..65efd6278 100644 --- a/src/SendRequest.jl +++ b/src/SendRequest.jl @@ -45,7 +45,7 @@ Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. function request(uri::URI, req::Request, res::Response; kw...) - defaultheader(req, "Host" => hostname(uri)) + defaultheader(req, "Host" => uri.host) setlengthheader(req, getkv(kw, :body_length, -1)) # Get a connection from the pool... @@ -53,7 +53,7 @@ function request(uri::URI, req::Request, res::Response; kw...) if getkv(kw, :use_connection_pool, true) T = Connections.Connection{T} end - io = getconnection(T, hostname(uri), parse(UInt, port(uri))) + io = getconnection(T, uri.host, uri.port) # Run request in a background task if response body is a stream... if isstream(res.body) diff --git a/src/uri.jl b/src/uri.jl index 81a6fa5f1..7b980c3d3 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -6,7 +6,7 @@ include("urlparser.jl") export URI, URL, hasscheme, scheme, - hashostname, hostname, + hashost, host, haspath, path, hasquery, query, hasfragment, fragment, @@ -19,7 +19,7 @@ export URI, URL, """ HTTP.URL(host; userinfo="", path="", query="", fragment="", isconnect=false) - HTTP.URI(; scheme="", hostname="", port="", ...) + HTTP.URI(; scheme="", host="", port="", ...) HTTP.URI(str; isconnect=false) parse(HTTP.URI, str::String; isconnect=false) @@ -33,20 +33,18 @@ For efficiency, the internal representation is stored as a set of offsets and le To access and return these components as strings, use the various accessor methods: * `HTTP.scheme`: returns the scheme (if any) associated with the uri * `HTTP.userinfo`: returns the userinfo (if any) associated with the uri - * `HTTP.hostname`: returns the hostname only of the uri + * `HTTP.host`: returns the host only of the uri * `HTTP.port`: returns the port of the uri; will return "80" or "443" by default if the scheme is "http" or "https", respectively - * `HTTP.host`: returns the "hostname:port" combination; if the port is not provided or is the default port for the uri scheme, it will be omitted + * `HTTP.hostport`: returns the "host:port" combination; if the port is not provided or is the default port for the uri scheme, it will be omitted * `HTTP.path`: returns the path for a uri * `HTTP.query`: returns the query for a uri * `HTTP.fragment`: returns the fragment for a uri * `HTTP.resource`: returns the path-query-fragment combination """ struct URI -#= data::Vector{UInt8} - offsets::NTuple{7, Offset} =# uri::String scheme::SubString - hostname::SubString + host::SubString port::SubString path::SubString query::SubString @@ -54,17 +52,17 @@ struct URI userinfo::SubString end -function URI(;hostname::AbstractString="", path::AbstractString="", +function URI(;host::AbstractString="", path::AbstractString="", scheme::AbstractString="", userinfo::AbstractString="", port::Union{Integer,AbstractString}="", query="", fragment::AbstractString="", isconnect::Bool=false) - hostname != "" && scheme == "" && !isconnect && (scheme = "http") + host != "" && scheme == "" && !isconnect && (scheme = "http") io = IOBuffer() - printuri(io, scheme, userinfo, hostname, string(port), path, escape(query), fragment) + printuri(io, scheme, userinfo, host, string(port), path, escape(query), fragment) return URI(String(take!(io)); isconnect=isconnect) end -# we assume `str` is at least hostname & port +# we assume `str` is at least host & port # if all others keywords are empty, assume CONNECT # can include path, userinfo, query, & fragment function URL(str::AbstractString; userinfo::AbstractString="", path::AbstractString="", @@ -90,64 +88,68 @@ function URL(str::AbstractString; userinfo::AbstractString="", path::AbstractStr end URI(str::AbstractString; isconnect::Bool=false) = Base.parse(URI, str; isconnect=isconnect) -Base.parse(::Type{URI}, str::AbstractString; isconnect::Bool=false) = http_parser_parse_url(Vector{UInt8}(str), 1, sizeof(str), isconnect) - -==(a::URI,b::URI) = scheme(a) == scheme(b) && - hostname(a) == hostname(b) && - path(a) == path(b) && - query(a) == query(b) && - fragment(a) == fragment(b) && - userinfo(a) == userinfo(b) && - ((!hasport(a) || !hasport(b)) || (port(a) == port(b))) -# accessors -for uf in instances(http_parser_url_fields) - uf == UF_MAX && break - nm = lowercase(string(uf)[4:end]) - has = Symbol(string("has", nm)) - @eval $has(uri::URI) = !isempty(uri.$(Symbol(nm))) - uf == UF_PORT && continue - #@eval $(Symbol(nm))(uri::URI) = String(uri.data[uri.offsets[Int($uf)]]) - @eval $(Symbol(nm))(uri::URI) = uri.$(Symbol(nm)) +Base.parse(::Type{URI}, str::AbstractString; isconnect::Bool=false) = http_parser_parse_url(str, isconnect) + +==(a::URI,b::URI) = a.scheme == b.scheme && + a.host == b.host && + a.path == b.path && + a.query == b.query && + a.fragment == b.fragment && + a.userinfo == b.userinfo && + port(a) == port(b) + +scheme(u) = u.scheme +host(u) = u.host +port(u) = u.port +path(u) = u.path +query(u) = u.query +fragment(u) = u.fragment +userinfo(u) = u.userinfo + + +function resource(uri::URI; isconnect::Bool=false) + string(isconnect ? hostport(uri) : uri.path, + isempty(uri.query) ? "" : "?$(uri.query)", + isempty(uri.fragment) ? "" : "#$(uri.fragment)") end -# special def for port -function port(uri::URI) - if hasport(uri) - return uri.port - else - sch = scheme(uri) - return sch == "http" ? "80" : sch == "https" ? "443" : "" - end -end - -resource(uri::URI; isconnect::Bool=false) = isconnect ? host(uri) : path(uri) * (isempty(query(uri)) ? "" : "?$(query(uri))") * (isempty(fragment(uri)) ? "" : "#$(fragment(uri))") -function host(uri::URI) - h = hostname(uri) - sch = scheme(uri) +function hostport(uri::URI) + s = uri.scheme + h = uri.host p = uri.port - if isempty(p) || (sch == "http" && p == "80") || (sch == "https" && p == "443") - return h - else - return string(h, p) + if s == "http" && p == "80" || + s == "https" && p == "443" + p = "" end + return string(':' in h ? "[$h]" : h, isempty(p) ? "" : ":$p") end Base.show(io::IO, uri::URI) = print(io, "HTTP.URI(\"", uri, "\")") -Base.print(io::IO, u::URI) = printuri(io, scheme(u), userinfo(u), hostname(u), port(u), path(u), query(u), fragment(u)) -function printuri(io::IO, sch::AbstractString, userinfo::AbstractString, hostname::AbstractString, port::AbstractString, path::AbstractString, query::AbstractString, fragment::AbstractString) +Base.print(io::IO, u::URI) = printuri(io, u.scheme, u.userinfo, u.host, + port(u), u.path, u.query, u.fragment) + +function printuri(io::IO, + sch::AbstractString, + userinfo::AbstractString, + host::AbstractString, + port::AbstractString, + path::AbstractString, + query::AbstractString, + fragment::AbstractString) + if sch in uses_authority print(io, sch, "://") !isempty(userinfo) && print(io, userinfo, "@") - print(io, ':' in hostname ? "[$hostname]" : hostname) + print(io, ':' in host? "[$host]" : host) print(io, ((sch == "http" && port == "80") || (sch == "https" && port == "443") || isempty(port)) ? "" : ":$port") elseif path != "" && path != "*" && sch != "" print(io, sch, ":") - elseif hostname != "" && port != "" # CONNECT - print(io, hostname, ":", port) + elseif host != "" && port != "" # CONNECT + print(io, host, ":", port) end - if (isempty(hostname) || hostname[end] != '/') && + if (isempty(host) || host[end] != '/') && (isempty(path) || path[1] != '/') && (!isempty(fragment) || !isempty(path)) path = (!isempty(sch) && sch == "http" || sch == "https") ? string("/", path) : path @@ -155,7 +157,9 @@ function printuri(io::IO, sch::AbstractString, userinfo::AbstractString, hostnam print(io, path, isempty(query) ? "" : "?$query", isempty(fragment) ? "" : "#$fragment") end -queryparams(uri::URI) = queryparams(query(uri)) + +queryparams(uri::URI) = queryparams(uri.query) + function queryparams(q::AbstractString) Dict(unescape(k) => unescape(v) for (k,v) in ([split(e, "=")..., ""][1:2] @@ -171,17 +175,18 @@ const uses_fragment = ["hdfs", "ftp", "hdl", "http", "gopher", "news", "nntp", " "checks if a `HTTP.URI` is valid" function Base.isvalid(uri::URI) - sch = scheme(uri) + sch = uri.scheme isempty(sch) && throw(ArgumentError("can not validate relative URI")) - if ((sch in non_hierarchical) && (search(path(uri), '/') > 1)) || # path hierarchy not allowed - (!(sch in uses_query) && !isempty(query(uri))) || # query component not allowed - (!(sch in uses_fragment) && !isempty(fragment(uri))) || # fragment identifier component not allowed - (!(sch in uses_authority) && (!isempty(hostname(uri)) || ("" != port(uri)) || !isempty(userinfo(uri)))) # authority component not allowed + if ((sch in non_hierarchical) && (search(uri.path, '/') > 1)) || # path hierarchy not allowed + (!(sch in uses_query) && !isempty(uri.query)) || # query component not allowed + (!(sch in uses_fragment) && !isempty(uri.fragment)) || # fragment identifier component not allowed + (!(sch in uses_authority) && (!isempty(uri.host) || ("" != port(uri)) || !isempty(uri.userinfo))) # authority component not allowed return false end return true end + # RFC3986 Unreserved Characters (and '~' Unsafe per RFC1738). @inline issafe(c::Char) = c == '-' || c == '.' || @@ -194,7 +199,7 @@ utf8_chars(str::AbstractString) = (Char(c) for c in Vector{UInt8}(str)) function escape end escape(c::Char) = string('%', uppercase(hex(c,2))) -escape(str::AbstractString, safe::Function=issafe) = +escape(str::AbstractString, safe::Function=issafe) = join(safe(c) ? c : escape(c) for c in utf8_chars(str)) escape(bytes::Vector{UInt8}) = bytes @@ -230,8 +235,8 @@ See: http://tools.ietf.org/html/rfc3986#section-3.3 """ function splitpath end -splitpath(uri::URI) = splitpath(path(uri)) -function splitpath(p::String) +splitpath(uri::URI) = splitpath(uri.path) +function splitpath(p::AbstractString) elems = String[] len = length(p) len > 1 || return elems @@ -254,19 +259,13 @@ absuri(u, context) = absuri(URI(u), URI(context)) function absuri(u::URI, context::URI) - @assert !isempty(hostname(context)) - - s = scheme(u) - h = hostname(u) - n = port(u) - p = path(u) - q = query(u) + @assert !isempty(context.host) - return URIs.URI(scheme=isempty(s) ? scheme(context) : s, - hostname=isempty(h) ? hostname(context) : h, - port=isempty(n) ? port(context) : n, - path=isempty(p) ? path(context) : p, - query=isempty(q) ? query(context) : q) + return URI(scheme = isempty(u.scheme) ? context.scheme : u.scheme, + host = isempty(u.host) ? context.host : u.host, + port = isempty(u.port) ? context.port : u.port, + path = isempty(u.path) ? context.path : u.path, + query = isempty(u.query) ? context.query : u.query) end end # module diff --git a/src/urlparser.jl b/src/urlparser.jl index 93e068397..6a8057ec1 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -6,19 +6,9 @@ struct URLParsingError <: Exception end Base.show(io::IO, p::URLParsingError) = println(io, "HTTP.URLParsingError: ", p.msg) -struct Offset - off::UInt16 - len::UInt16 -end -Offset() = Offset(0, 0) -Base.getindex(A::Vector{UInt8}, o::Offset) = A[o.off:(o.off + o.len - 1)] -Base.isempty(o::Offset) = o.off == 0x0000 && o.len == 0x0000 -==(a::Offset, b::Offset) = a.off == b.off && a.len == b.len -const EMPTYOFFSET = Offset() - @enum(http_parser_url_fields, UF_SCHEME = 1 - , UF_HOSTNAME = 2 + , UF_HOST = 2 , UF_PORT = 3 , UF_PATH = 4 , UF_QUERY = 5 @@ -27,7 +17,7 @@ const EMPTYOFFSET = Offset() , UF_MAX = 8 ) const UF_SCHEME_MASK = 0x01 -const UF_HOSTNAME_MASK = 0x02 +const UF_HOST_MASK = 0x02 const UF_PORT_MASK = 0x04 const UF_PATH_MASK = 0x08 const UF_QUERY_MASK = 0x10 @@ -87,7 +77,7 @@ function parseurlchar(s, ch::Char, strict::Bool) (ch == '?' || ch == '#') && return s end #= We should never fall out of the switch above unless there's an error =# - return s_dead; + return s_dead end function http_parse_host_char(s::http_host_state, ch) @@ -120,81 +110,97 @@ function http_parse_host_char(s::http_host_state, ch) return s_http_host_dead end -function http_parse_host(buf, host::Offset, foundat) - portoff = portlen = uioff = uilen = UInt16(0) - off = len = UInt16(0) +function http_parse_host(host::SubString, foundat) + + host1 = port1 = userinfo1 = 1 + host2 = port2 = userinfo2 = 0 s = ifelse(foundat, s_http_userinfo_start, s_http_host_start) - for i = host.off:(host.off + host.len - 0x0001) - p = Char(buf[i]) + for i in eachindex(host) + @inbounds p = host[i] + new_s = http_parse_host_char(s, p) - new_s == s_http_host_dead && throw(URLParsingError("encountered invalid host character: \n$(String(buf))\n$(lpad("", i-1, "-"))^")) + if new_s == s_http_host_dead + throw(URLParsingError("encountered invalid host character: \n" * + "$host\n$(lpad("", i-1, "-"))^")) + end if new_s == s_http_host if s != s_http_host - off = i + host1 = i end - len += 0x0001 + host2 = i elseif new_s == s_http_host_v6 if s != s_http_host_v6 - off = i + host1 = i end - len += 0x0001 + host2 = i - elseif new_s == s_http_host_v6_zone_start || new_s == s_http_host_v6_zone - len += 0x0001 + elseif new_s == s_http_host_v6_zone_start || + new_s == s_http_host_v6_zone + host2 = i elseif new_s == s_http_host_port if s != s_http_host_port - portoff = i - portlen = 0x0000 + port1 = i end - portlen += 0x0001 + port2 = i elseif new_s == s_http_userinfo if s != s_http_userinfo - uioff = i - uilen = 0x0000 + userinfo1 = i end - uilen += 0x0001 + userinfo2 = i end s = new_s end - if @anyeq(s, s_http_host_start, s_http_host_v6_start, s_http_host_v6, s_http_host_v6_zone_start, - s_http_host_v6_zone, s_http_host_port_start, s_http_userinfo, s_http_userinfo_start) + if @anyeq(s, s_http_host_start, s_http_host_v6_start, s_http_host_v6, + s_http_host_v6_zone_start, s_http_host_v6_zone, + s_http_host_port_start, s_http_userinfo, s_http_userinfo_start) throw(URLParsingError("ended in unexpected parsing state: $s")) end - # (host, port, userinfo) - return Offset(off, len), Offset(portoff, portlen), Offset(uioff, uilen) + + return SubString(host, host1, host2), + SubString(host, port1, port2), + SubString(host, userinfo1, userinfo2) end -ufsubstring(uri, offset) = SubString(uri, offset.off, offset.off + offset.len-1) -function http_parser_parse_url(buf, startind=1, buflen=length(buf), isconnect::Bool=false) +function http_parser_parse_url(url::AbstractString, isconnect::Bool=false) + s = ifelse(isconnect, s_req_server_start, s_req_spaces_before_url) old_uf = UF_MAX - off = len = 0 + off1 = off2 = 0 foundat = false - offsets = Offset[Offset(), Offset(), Offset(), Offset(), Offset(), Offset(), Offset()] + + empty = SubString(url, 1, 0) + parts = [empty, empty, empty, empty, empty, empty, empty] + mask = 0x00 - for i = startind:(startind + buflen - 1) - @inbounds p = Char(buf[i]) + for i in eachindex(url) + @inbounds p = url[i] olds = s s = parseurlchar(s, p, false) if s == s_dead - throw(URLParsingError("encountered invalid url character for parsing state = $(ParsingStateCode(olds)): \n$(String(buf))\n$(lpad("", i-1, "-"))^")) - elseif @anyeq(s, s_req_schema_slash, s_req_schema_slash_slash, s_req_server_start, s_req_query_string_start, s_req_fragment_start) + throw(URLParsingError( + "encountered invalid url character for parsing state = " * + "$(ParsingStateCode(olds)):\n$url)\n$(lpad("", i-1, "-"))^")) + elseif @anyeq(s, s_req_schema_slash, + s_req_schema_slash_slash, + s_req_server_start, + s_req_query_string_start, + s_req_fragment_start) continue elseif s == s_req_schema uf = UF_SCHEME mask |= UF_SCHEME_MASK elseif s == s_req_server_with_at foundat = true - uf = UF_HOSTNAME - mask |= UF_HOSTNAME_MASK + uf = UF_HOST + mask |= UF_HOST_MASK elseif s == s_req_server - uf = UF_HOSTNAME - mask |= UF_HOSTNAME_MASK + uf = UF_HOST + mask |= UF_HOST_MASK elseif s == s_req_path uf = UF_PATH mask |= UF_PATH_MASK @@ -208,58 +214,43 @@ function http_parser_parse_url(buf, startind=1, buflen=length(buf), isconnect::B throw(URLParsingError("ended in unexpected parsing state: $s")) end if uf == old_uf - len += 1 + off2 = i continue end if old_uf != UF_MAX - offsets[old_uf] = Offset(off, len) + parts[old_uf] = SubString(url, off1, off2) end - off = i - len = 1 + off1 = i + off2 = i old_uf = uf end if old_uf != UF_MAX - offsets[old_uf] = Offset(off, len) + parts[old_uf] = SubString(url, off1, off2) end - check = ~(UF_HOSTNAME_MASK | UF_PATH_MASK) + check = ~(UF_HOST_MASK | UF_PATH_MASK) if (mask & UF_SCHEME_MASK > 0) && (mask | check == check) throw(URLParsingError("URI must include host or path with scheme")) end - if mask & UF_HOSTNAME_MASK > 0 - host, port, userinfo = http_parse_host(buf, offsets[UF_HOSTNAME], foundat) + if mask & UF_HOST_MASK > 0 + host, port, userinfo = http_parse_host(parts[UF_HOST], foundat) if !isempty(host) - offsets[UF_HOSTNAME] = host - mask |= UF_HOSTNAME_MASK + parts[UF_HOST] = host + mask |= UF_HOST_MASK end if !isempty(port) - offsets[UF_PORT] = port + parts[UF_PORT] = port mask |= UF_PORT_MASK end if !isempty(userinfo) - offsets[UF_USERINFO] = userinfo + parts[UF_USERINFO] = userinfo mask |= UF_USERINFO_MASK end end # CONNECT requests can only contain "hostname:port" if isconnect - chk = UF_HOSTNAME_MASK | UF_PORT_MASK + chk = UF_HOST_MASK | UF_PORT_MASK ((mask | chk) > chk) && throw(URLParsingError("connect requests must contain and can only contain both hostname and port")) end - uri = String(buf) - return URI(#=buf, (offsets[UF_SCHEME], - offsets[UF_HOSTNAME], - offsets[UF_PORT], - offsets[UF_PATH], - offsets[UF_QUERY], - offsets[UF_FRAGMENT], - offsets[UF_USERINFO]),=# - uri, - ufsubstring(uri, offsets[UF_SCHEME]), - ufsubstring(uri, offsets[UF_HOSTNAME]), - ufsubstring(uri, offsets[UF_PORT]), - ufsubstring(uri, offsets[UF_PATH]), - ufsubstring(uri, offsets[UF_QUERY]), - ufsubstring(uri, offsets[UF_FRAGMENT]), - ufsubstring(uri, offsets[UF_USERINFO])) + return URI(url, parts...) end diff --git a/test/parser.jl b/test/parser.jl index 72c56452f..a8d930d77 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1412,7 +1412,7 @@ const responses = Message[ @test uri.query == req.query_string @test uri.fragment == req.fragment @test uri.path == req.request_path - @test uri.hostname == req.host + @test uri.host == req.host @test uri.userinfo == req.userinfo @test uri.port in (req.port, "80", "443") @test string(uri) == req.request_url diff --git a/test/runtests.jl b/test/runtests.jl index b36907a70..843c2da9e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -11,7 +11,7 @@ end include("utils.jl"); #include("fifobuffer.jl"); #include("sniff.jl"); - #include("uri.jl"); + include("uri.jl"); #include("cookies.jl"); include("parser.jl"); include("body.jl"); diff --git a/test/uri.jl b/test/uri.jl index c0d97a462..6083aa5cf 100644 --- a/test/uri.jl +++ b/test/uri.jl @@ -2,21 +2,35 @@ mutable struct URLTest name::String url::String isconnect::Bool - offsets::NTuple{7, HTTP.URIs.Offset} + expecteduri::HTTP.URI shouldthrow::Bool end -URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(nm, url, isconnect, ntuple(x->HTTP.URIs.Offset(), 7), shouldthrow) +struct Offset + off::UInt16 + len::UInt16 +end + +offsetss(uri, offset) = SubString(uri, offset.off, offset.off + offset.len-1) + +function URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) + URLTest(nm, url, isconnect, HTTP.URI(""), shouldthrow) +end + +function URLTest(nm::String, url::String, isconnect::Bool, offsets::NTuple{7, Offset}, shouldthrow::Bool) + uri = HTTP.URI(url, (offsetss(url, o) for o in offsets)...) + URLTest(nm, url, isconnect, uri, shouldthrow) +end @testset "HTTP.URI" begin # constructor @test string(HTTP.URI("")) == "" - @test HTTP.URI(hostname="google.com") == HTTP.URI("http://google.com") - @test HTTP.URI(hostname="google.com", path="/") == HTTP.URI("http://google.com/") - @test HTTP.URI(hostname="google.com", userinfo="user") == HTTP.URI("http://user@google.com") - @test HTTP.URI(hostname="google.com", path="user") == HTTP.URI("http://google.com/user") - @test HTTP.URI(hostname="google.com", query=Dict("key"=>"value")) == HTTP.URI("http://google.com?key=value") - @test HTTP.URI(hostname="google.com", fragment="user") == HTTP.URI("http://google.com/#user") + @test HTTP.URI(host="google.com") == HTTP.URI("http://google.com") + @test HTTP.URI(host="google.com", path="/") == HTTP.URI("http://google.com/") + @test HTTP.URI(host="google.com", userinfo="user") == HTTP.URI("http://user@google.com") + @test HTTP.URI(host="google.com", path="user") == HTTP.URI("http://google.com/user") + @test HTTP.URI(host="google.com", query=Dict("key"=>"value")) == HTTP.URI("http://google.com?key=value") + @test HTTP.URI(host="google.com", fragment="user") == HTTP.URI("http://google.com/#user") urls = [("hdfs://user:password@hdfshost:9000/root/folder/file.csv#frag", ["root", "folder", "file.csv"]), ("https://user:password@httphost:9000/path1/path2;paramstring?q=a&p=r#frag", ["path1", "path2;paramstring"]), @@ -41,10 +55,9 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n @test HTTP.splitpath(u) == splpath end - @test parse(HTTP.URI, "hdfs://user:password@hdfshost:9000/root/folder/file.csv") == HTTP.URI(hostname="hdfshost", path="/root/folder/file.csv", scheme="hdfs", port=9000, userinfo="user:password") - @test parse(HTTP.URI, "http://google.com:80/some/path") == HTTP.URI(hostname="google.com", path="/some/path") + @test parse(HTTP.URI, "hdfs://user:password@hdfshost:9000/root/folder/file.csv") == HTTP.URI(host="hdfshost", path="/root/folder/file.csv", scheme="hdfs", port=9000, userinfo="user:password") + @test string(parse(HTTP.URI, "http://google.com:80/some/path")) == string(HTTP.URI(host="google.com", path="/some/path")) - @test isempty(HTTP.URIs.Offset()) @test HTTP.lower(UInt8('A')) == UInt8('a') @test HTTP.escape(Char(1)) == "%01" @@ -67,7 +80,7 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n @test false == isvalid(parse(HTTP.URI, "file:///path/to/file/with?should=work#fine")) @test true == isvalid( parse(HTTP.URI, "file:///path/to/file/with%3fshould%3dwork%23fine")) - @test parse(HTTP.URI, "s3://bucket/key") == HTTP.URI(hostname="bucket", path="/key", scheme="s3") + @test parse(HTTP.URI, "s3://bucket/key") == HTTP.URI(host="bucket", path="/key", scheme="s3") @test sprint(show, parse(HTTP.URI, "http://google.com")) == "HTTP.URI(\"http://google.com\")" @@ -88,157 +101,157 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n URLTest("proxy request" ,"http://hostname/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(8, 8) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(16, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(8, 8) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(16, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("proxy request with port" ,"http://hostname:444/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(8, 8) # UF_HOST - ,HTTP.URIs.Offset(17, 3) # UF_PORT - ,HTTP.URIs.Offset(20, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(8, 8) # UF_HOST + ,Offset(17, 3) # UF_PORT + ,Offset(20, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("CONNECT request" ,"hostname:443" ,true - ,(HTTP.URIs.Offset(0, 0) # UF_SCHEMA - ,HTTP.URIs.Offset(1, 8) # UF_HOST - ,HTTP.URIs.Offset(10, 3) # UF_PORT - ,HTTP.URIs.Offset(0, 0) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(0, 0) # UF_SCHEMA + ,Offset(1, 8) # UF_HOST + ,Offset(10, 3) # UF_PORT + ,Offset(0, 0) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("proxy ipv6 request" ,"http://[1:2::3:4]/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(9, 8) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(18, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(9, 8) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(18, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("proxy ipv6 request with port" ,"http://[1:2::3:4]:67/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(9, 8) # UF_HOST - ,HTTP.URIs.Offset(19, 2) # UF_PORT - ,HTTP.URIs.Offset(21, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(9, 8) # UF_HOST + ,Offset(19, 2) # UF_PORT + ,Offset(21, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("CONNECT ipv6 address" ,"[1:2::3:4]:443" ,true - ,(HTTP.URIs.Offset(0, 0) # UF_SCHEMA - ,HTTP.URIs.Offset(2, 8) # UF_HOST - ,HTTP.URIs.Offset(12, 3) # UF_PORT - ,HTTP.URIs.Offset(0, 0) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(0, 0) # UF_SCHEMA + ,Offset(2, 8) # UF_HOST + ,Offset(12, 3) # UF_PORT + ,Offset(0, 0) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("ipv4 in ipv6 address" ,"http://[2001:0000:0000:0000:0000:0000:1.9.1.1]/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(9,37) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(47, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(9,37) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(47, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("extra ? in query string" ,"http://a.tbcdn.cn/p/fp/2010c/??fp-header-min.css,fp-base-min.css,fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css,fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(8,10) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(18,12) # UF_PATH - ,HTTP.URIs.Offset(31,187) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(8,10) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(18,12) # UF_PATH + ,Offset(31,187) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("space URL encoded" ,"/toto.html?toto=a%20b" ,false - ,(HTTP.URIs.Offset(0, 0) # UF_SCHEMA - ,HTTP.URIs.Offset(0, 0) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(1,10) # UF_PATH - ,HTTP.URIs.Offset(12,10) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(1,10) # UF_PATH + ,Offset(12,10) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("URL fragment" ,"/toto.html#titi" ,false - ,(HTTP.URIs.Offset(0, 0) # UF_SCHEMA - ,HTTP.URIs.Offset(0, 0) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(1,10) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(12, 4) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(1,10) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(12, 4) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("complex URL fragment" ,"http://www.webmasterworld.com/r.cgi?f=21&d=8405&url=http://www.example.com/index.html?foo=bar&hello=world#midpage" ,false - ,(HTTP.URIs.Offset( 1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset( 8, 22) # UF_HOST - ,HTTP.URIs.Offset( 0, 0) # UF_PORT - ,HTTP.URIs.Offset( 30, 6) # UF_PATH - ,HTTP.URIs.Offset( 37, 69) # UF_QUERY - ,HTTP.URIs.Offset(107, 7) # UF_FRAGMENT - ,HTTP.URIs.Offset( 0, 0) # UF_USERINFO + ,(Offset( 1, 4) # UF_SCHEMA + ,Offset( 8, 22) # UF_HOST + ,Offset( 0, 0) # UF_PORT + ,Offset( 30, 6) # UF_PATH + ,Offset( 37, 69) # UF_QUERY + ,Offset(107, 7) # UF_FRAGMENT + ,Offset( 0, 0) # UF_USERINFO ) ,false ), URLTest("complex URL from node js url parser doc" ,"http://host.com:8080/p/a/t/h?query=string#hash" ,false - ,( HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(8, 8) # UF_HOST - ,HTTP.URIs.Offset(17, 4) # UF_PORT - ,HTTP.URIs.Offset(21, 8) # UF_PATH - ,HTTP.URIs.Offset(30,12) # UF_QUERY - ,HTTP.URIs.Offset(43, 4) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,( Offset(1, 4) # UF_SCHEMA + ,Offset(8, 8) # UF_HOST + ,Offset(17, 4) # UF_PORT + ,Offset(21, 8) # UF_PATH + ,Offset(30,12) # UF_QUERY + ,Offset(43, 4) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("complex URL with basic auth from node js url parser doc" ,"http://a:b@host.com:8080/p/a/t/h?query=string#hash" ,false - ,( HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(12, 8) # UF_HOST - ,HTTP.URIs.Offset(21, 4) # UF_PORT - ,HTTP.URIs.Offset(25, 8) # UF_PATH - ,HTTP.URIs.Offset(34,12) # UF_QUERY - ,HTTP.URIs.Offset(47, 4) # UF_FRAGMENT - ,HTTP.URIs.Offset(8, 3) # UF_USERINFO + ,( Offset(1, 4) # UF_SCHEMA + ,Offset(12, 8) # UF_HOST + ,Offset(21, 4) # UF_PORT + ,Offset(25, 8) # UF_PATH + ,Offset(34,12) # UF_QUERY + ,Offset(47, 4) # UF_FRAGMENT + ,Offset(8, 3) # UF_USERINFO ) ,false ), URLTest("double @" @@ -276,13 +289,13 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n ), URLTest("proxy basic auth with space url encoded" ,"http://a%20:b@host.com/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(15, 8) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(23, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(8, 6) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(15, 8) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(23, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(8, 6) # UF_USERINFO ) ,false ), URLTest("carriage return in URL" @@ -296,13 +309,13 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n ), URLTest("proxy basic auth with double :" ,"http://a::b@host.com/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(13, 8) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(21, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(8, 4) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(13, 8) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(21, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(8, 4) # UF_USERINFO ) ,false ), URLTest("line feed in URL" @@ -312,13 +325,13 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n ), URLTest("proxy empty basic auth" ,"http://@hostname/fo" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(9, 8) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(17, 3) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(9, 8) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(17, 3) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("proxy line feed in hostname" @@ -336,13 +349,13 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n ), URLTest("proxy basic auth with unreservedchars" ,"http://a!;-_!=+\$@host.com/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(18, 8) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(26, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(8, 9) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(18, 8) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(26, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(8, 9) # UF_USERINFO ) ,false ), URLTest("proxy only empty basic auth" @@ -360,25 +373,25 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n ), URLTest("ipv6 address with Zone ID" ,"http://[fe80::a%25eth0]/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(9,14) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(24, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(9,14) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(24, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("ipv6 address with Zone ID, but '%' is not percent-encoded" ,"http://[fe80::a%eth0]/" ,false - ,(HTTP.URIs.Offset(1, 4) # UF_SCHEMA - ,HTTP.URIs.Offset(9,12) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(22, 1) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(1, 4) # UF_SCHEMA + ,Offset(9,12) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(22, 1) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("ipv6 address ending with '%'" @@ -396,25 +409,25 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n ), URLTest("tab in URL" ,"/foo\tbar/" ,false - ,(HTTP.URIs.Offset(0, 0) # UF_SCHEMA - ,HTTP.URIs.Offset(0, 0) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(1, 9) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(1, 9) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("form feed in URL" ,"/foo\fbar/" ,false - ,(HTTP.URIs.Offset(0, 0) # UF_SCHEMA - ,HTTP.URIs.Offset(0, 0) # UF_HOST - ,HTTP.URIs.Offset(0, 0) # UF_PORT - ,HTTP.URIs.Offset(1, 9) # UF_PATH - ,HTTP.URIs.Offset(0, 0) # UF_QUERY - ,HTTP.URIs.Offset(0, 0) # UF_FRAGMENT - ,HTTP.URIs.Offset(0, 0) # UF_USERINFO + ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_HOST + ,Offset(0, 0) # UF_PORT + ,Offset(1, 9) # UF_PATH + ,Offset(0, 0) # UF_QUERY + ,Offset(0, 0) # UF_FRAGMENT + ,Offset(0, 0) # UF_USERINFO ) ,false ) @@ -426,7 +439,7 @@ URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) = URLTest(n @test_throws HTTP.URIs.URLParsingError parse(HTTP.URI, u.url; isconnect=u.isconnect) else url = parse(HTTP.URI, u.url; isconnect=u.isconnect) - @test u.offsets == url.offsets + @test u.expecteduri == url end end end From be4bc91a8f17b877f82580b2887fd43ef831ade5 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 10 Dec 2017 23:03:47 +1100 Subject: [PATCH 033/182] simplify Base.print(io::IO, u::URI), just print string directly --- src/uri.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/uri.jl b/src/uri.jl index 7b980c3d3..b00abde0f 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -126,8 +126,7 @@ end Base.show(io::IO, uri::URI) = print(io, "HTTP.URI(\"", uri, "\")") -Base.print(io::IO, u::URI) = printuri(io, u.scheme, u.userinfo, u.host, - port(u), u.path, u.query, u.fragment) +Base.print(io::IO, u::URI) = print(io, u.uri) function printuri(io::IO, sch::AbstractString, From 602ed9829716d5dde91878b89fbf6ce887415e40 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 10 Dec 2017 23:03:58 +1100 Subject: [PATCH 034/182] simplify Base.print(io::IO, u::URI), just print string directly --- test/uri.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/uri.jl b/test/uri.jl index 6083aa5cf..1e417734b 100644 --- a/test/uri.jl +++ b/test/uri.jl @@ -56,7 +56,7 @@ end end @test parse(HTTP.URI, "hdfs://user:password@hdfshost:9000/root/folder/file.csv") == HTTP.URI(host="hdfshost", path="/root/folder/file.csv", scheme="hdfs", port=9000, userinfo="user:password") - @test string(parse(HTTP.URI, "http://google.com:80/some/path")) == string(HTTP.URI(host="google.com", path="/some/path")) + @test parse(HTTP.URI, "http://google.com/some/path") == HTTP.URI(host="google.com", path="/some/path") @test HTTP.lower(UInt8('A')) == UInt8('a') @test HTTP.escape(Char(1)) == "%01" From 8f031c1173512cc47e447f8b2aa3b83eea94cc3a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 11 Dec 2017 02:59:15 +1100 Subject: [PATCH 035/182] Split util functions into: Pairs.jl, Stirngs.jl, debug.jl, parseutils.jl --- src/Connect.jl | 2 +- src/Connections.jl | 10 +- src/CookieRequest.jl | 7 +- src/HTTP.jl | 23 +-- src/Messages.jl | 22 ++- src/Pairs.jl | 59 +++++++ src/{parser.jl => Parsers.jl} | 77 +++++---- src/RetryRequest.jl | 7 +- src/SendRequest.jl | 11 +- src/Strings.jl | 69 ++++++++ src/cookies.jl | 2 +- src/debug.jl | 23 +++ src/parseutils.jl | 24 +++ src/uri.jl | 81 ++++----- src/urlparser.jl | 2 +- src/utils.jl | 255 ---------------------------- test/messages.jl | 2 +- test/parser.jl | 306 +++++++++++++++++----------------- test/uri.jl | 27 +-- test/utils.jl | 72 ++++---- 20 files changed, 498 insertions(+), 583 deletions(-) create mode 100644 src/Pairs.jl rename src/{parser.jl => Parsers.jl} (95%) create mode 100644 src/Strings.jl create mode 100644 src/debug.jl create mode 100644 src/parseutils.jl delete mode 100644 src/utils.jl diff --git a/src/Connect.jl b/src/Connect.jl index 9e16b78fc..946e822c4 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -4,7 +4,7 @@ export getconnection using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! -import ..@debug +import ..@debug, ..DEBUG_LEVEL """ diff --git a/src/Connections.jl b/src/Connections.jl index 28a03ca6f..6e4b2fece 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -4,8 +4,8 @@ export getconnection using ..IOExtras -import ..@lock, ..@debug, ..SSLContext - +import ..@debug, ..DEBUG_LEVEL +import ..SSLContext import ..Connect.getconnection @@ -143,7 +143,8 @@ function getconnection(::Type{Connection{T}}, host::AbstractString, port::AbstractString)::Connection{T} where T <: IO - @lock poollock begin + lock(poollock) + try pattern = x->(!isbusy(x) && typeof(x.io) == T && @@ -163,6 +164,9 @@ function getconnection(::Type{Connection{T}}, push!(pool, c) @assert !isbusy(c) return c + + finally + unlock(poollock) end end diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index cb7118d1f..5a41ced3e 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -2,13 +2,14 @@ module CookieRequest export request -import ..HTTP - using ..URIs using ..Cookies using ..Messages +using ..Pairs: getkv, setkv + +import ..@debug, ..DEBUG_LEVEL -import ..RetryRequest, ..@debug, ..getkv, ..setkv +import ..RetryRequest const default_cookiejar = Dict{String, Set{Cookie}}() diff --git a/src/HTTP.jl b/src/HTTP.jl index 7d009e653..56a2f8de4 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -3,23 +3,12 @@ module HTTP #export Request, Response, FIFOBuffer - - using MbedTLS import MbedTLS.SSLContext -using Retry - -const TLS = MbedTLS -const Headers = Vector{Pair{String, String}} - -import Base.== const DEBUG_LEVEL = 1 -const DISABLE_CONNECTION_POOL = false - const DEBUG = false -const PARSING_DEBUG = false if VERSION > v"0.7.0-DEV.2338" using Base64 @@ -31,10 +20,14 @@ else import Dates end -include("consts.jl") -include("utils.jl") +include("debug.jl") +include("Pairs.jl") +include("Strings.jl") + +#include("consts.jl") +#include("utils.jl") include("uri.jl") -#using .URIs +using .URIs #include("fifobuffer.jl") #using .FIFOBuffers include("cookies.jl") @@ -42,7 +35,6 @@ include("cookies.jl") #include("multipart.jl") #include("types.jl") -include("parser.jl") #include("sniff.jl") @@ -51,6 +43,7 @@ using .IOExtras include("Bodies.jl") #using .Bodies +include("Parsers.jl") include("Messages.jl") #using .Messages diff --git a/src/Messages.jl b/src/Messages.jl index da8d5aebb..3daa6a211 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -7,19 +7,15 @@ export Message, Request, Response, Body, import ..HTTP +using ..Pairs using ..IOExtras using ..Bodies +using ..Parsers +import ..Parsers + +import ..@debug, ..DEBUG_LEVEL -import ..@lock import ..SSLContext -import ..Parser -import ..parse! -import ..messagecomplete -import ..headerscomplete -import ..waitingforeof -import ..ParsingStateCode -import ..ParsingError -import ..HTTP: getbyfirst, setbyfirst, @debug """ @@ -46,6 +42,7 @@ Request(method::String, uri, headers=[], body=Body(); parent=nothing) = mkheaders(headers), body, parent) Request(bytes) = read!(IOBuffer(bytes), Request()) +Base.parse(::Type{Request}, str::AbstractString) = Request(str) mkheaders(v::Vector{Pair{String,String}}) = v mkheaders(x) = [string(k) => string(v) for (k,v) in x] @@ -76,6 +73,7 @@ Response(status::Int=0, headers=[]; body=Body(), parent=nothing) = Response(v"1.1", status, headers, body, parent, Condition()) Response(bytes) = read!(IOBuffer(bytes), Response()) +Base.parse(::Type{Response}, str::AbstractString) = Response(str) const Message = Union{Request,Response} @@ -121,7 +119,7 @@ end `String` representation of a HTTP status code. e.g. `200 => "OK"`. """ -statustext(r::Response) = Base.get(HTTP.STATUS_CODES, r.status, "Unknown Code") +statustext(r::Response) = Base.get(Parsers.STATUS_CODES, r.status, "Unknown Code") """ @@ -319,8 +317,8 @@ function Base.read!(io::IO, p::Parser) end if eof(io) && !waitingforeof(p) - throw(ParsingError(headerscomplete(p) ? HTTP.HPE_BODY_INCOMPLETE : - HTTP.HPE_HEADERS_INCOMPLETE)) + throw(ParsingError(headerscomplete(p) ? Parsers.HPE_BODY_INCOMPLETE : + Parsers.HPE_HEADERS_INCOMPLETE)) end end diff --git a/src/Pairs.jl b/src/Pairs.jl new file mode 100644 index 000000000..59f2f1250 --- /dev/null +++ b/src/Pairs.jl @@ -0,0 +1,59 @@ +module Pairs + +export setbyfirst, getbyfirst, setkv, getkv + + +""" + setbyfirst(collection, item) -> item + +Set `item` in a `collection`. +If `first() of an exisiting matches `first(item)` it is replaced. +Otherwise the new `item` is inserted at the end of the `collection`. +""" + +function setbyfirst(c, item) + k = first(item) + if (i = findfirst(x->first(x) == k, c)) > 0 + c[i] = item + else + push!(c, item) + end + return item +end + + +""" + getbyfirst(collection, key [, default]) -> item + +Get `item` from collection where `first(item)` matches `key`. +""" + +function getbyfirst(c, k, default=nothing) + i = findfirst(x->first(x) == k, c) + return i > 0 ? c[i] : default +end + + +""" + setkv(collection, key, value) + +Set `value` for `key` in collection of key/value `Pairs`. +""" + +setkv(c, k, v) = setbyfirst(c, k => v) + + +""" + getkv(collection, key [, default]) -> value + +Get `value` for `key` in collection of key/value `Pairs`, +where `first(item) == key` and `value = item[2]` +""" + +function getkv(c, k, default=nothing) + i = findfirst(x->first(x) == k, c) + return i > 0 ? c[i][2] : default +end + + +end # module Pairs diff --git a/src/parser.jl b/src/Parsers.jl similarity index 95% rename from src/parser.jl rename to src/Parsers.jl index 437a8a3e0..a138966aa 100644 --- a/src/parser.jl +++ b/src/Parsers.jl @@ -22,12 +22,21 @@ # IN THE SOFTWARE. # -#FIXME module Parser +module Parsers -using .URIs +export Parser, parse!, messagecomplete, headerscomplete, waitingforeof, + ParsingError, ParsingErrorCode +import ..@debug, ..@debugshow, ..DEBUG_LEVEL + +include("consts.jl") +include("parseutils.jl") + +using ..URIs.parseurlchar + +const PARSING_DEBUG = false const start_state = s_start_req_or_res -const strict = true +const strict = false mutable struct Parser state::UInt8 @@ -39,7 +48,7 @@ mutable struct Parser content_length::UInt64 fieldbuffer::IOBuffer valuebuffer::IOBuffer - method::HTTP.Method + method::Method major::Int16 minor::Int16 url::String @@ -51,8 +60,6 @@ end Parser() = Parser(start_state, 0x00, 0, 0, false, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, "", 0, x->nothing, x->nothing, ()->nothing) -const DEFAULT_PARSER = Parser() - function reset!(p::Parser) p.state = start_state p.header_state = 0x00 @@ -105,6 +112,9 @@ macro strictcheck(cond) esc(:(strict && @errorif($cond, HPE_STRICT))) end +macro shifted(meth, i, char) + return esc(:(Int($meth) << Int(16) | Int($i) << Int(8) | Int($char))) +end const ByteView = typeof(view(UInt8[], 1:0)) @@ -115,19 +125,17 @@ parse!(p::Parser, bytes)::Int = parse!(p, view(bytes, 1:length(bytes))) function parse!(parser::Parser, bytes::ByteView)::Int isempty(bytes) && throw(ArgumentError("bytes must not be empty")) len = length(bytes) - @debug(PARSING_DEBUG, "parse!") + @debug 3 "parse!(::Parser, $len-bytes)" p_state = parser.state - @debug(PARSING_DEBUG, len) - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) + @debugshow 3 ParsingStateCode(p_state) p = 0 while p < len && p_state != s_message_done - @debug(PARSING_DEBUG, "top of while($p < $len)") - @debug(PARSING_DEBUG, ParsingStateCode(p_state)) + @debug 3 "top of while($p < $len) $(ParsingStateCode(p_state))" p += 1 @inbounds ch = Char(bytes[p]) - @debug(PARSING_DEBUG, Base.escape_string(string(ch))) + @debug 3 Base.escape_string(string(ch)) if p_state == s_dead #= this state is used after a 'Connection: close' message @@ -308,15 +316,14 @@ function parse!(parser::Parser, bytes::ByteView)::Int elseif p_state == s_req_method matcher = string(parser.method) - @debug(PARSING_DEBUG, matcher) - @debug(PARSING_DEBUG, parser.index) - @debug(PARSING_DEBUG, Base.escape_string(string(ch))) + @debugshow 3 matcher + @debugshow 3 parser.index if ch == ' ' && parser.index == length(matcher) + 1 p_state = s_req_spaces_before_url elseif parser.index > length(matcher) @err(HPE_INVALID_METHOD) elseif ch == matcher[parser.index] - @debug(PARSING_DEBUG, "nada") + @debug 3 "nada" elseif isalpha(ch) ci = @shifted(parser.method, Int(parser.index) - 1, ch) if ci == @shifted(POST, 1, 'U') @@ -357,14 +364,14 @@ function parse!(parser::Parser, bytes::ByteView)::Int @err(HPE_INVALID_METHOD) end elseif ch == '-' && parser.index == 2 && parser.method == MKCOL - @debug(PARSING_DEBUG, "matched MSEARCH") + @debug 3 "matched MSEARCH" parser.method = MSEARCH parser.index -= 1 else @err(HPE_INVALID_METHOD) end parser.index += 1 - @debug(PARSING_DEBUG, parser.index) + @debugshow 3 parser.index elseif p_state == s_req_spaces_before_url ch == ' ' && continue @@ -405,7 +412,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int end break end - p_state = URIs.parseurlchar(p_state, ch, strict) + p_state = parseurlchar(p_state, ch, strict) @errorif(p_state == s_dead, HPE_INVALID_URL) p += 1 end @@ -413,8 +420,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int write(parser.valuebuffer, view(bytes, start:p-1)) if p_state >= s_req_http_start - @debug(PARSING_DEBUG, "onurl $parser.method $(String(parser.valuebuffer))") parser.url = take!(parser.valuebuffer) + @debugshow 3 parser.url end elseif p_state == s_req_http_start @@ -517,14 +524,14 @@ function parse!(parser::Parser, bytes::ByteView)::Int start = p while p <= len @inbounds ch = Char(bytes[p]) - @debug(PARSING_DEBUG, Base.escape_string(string(ch))) + @debug 3 Base.escape_string(string(ch)) c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] if c == Char(0) @errorif(ch != ':', HPE_INVALID_HEADER_TOKEN) break end + @debugshow 3 parser.header_state h = parser.header_state - @debug(PARSING_DEBUG, h) if h == h_general elseif h == h_C @@ -653,9 +660,9 @@ function parse!(parser::Parser, bytes::ByteView)::Int h = parser.header_state while p <= len @inbounds ch = Char(bytes[p]) - @debug(PARSING_DEBUG, Base.escape_string(string('\'', ch, '\''))) - @debug(PARSING_DEBUG, strict) - @debug(PARSING_DEBUG, isheaderchar(ch)) + @debug 3 Base.escape_string(string('\'', ch, '\'')) + @debugshow 3 strict + @debugshow 3 isheaderchar(ch) if ch == CR p_state = s_header_almost_done break @@ -668,7 +675,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int c = lower(ch) - @debug(PARSING_DEBUG, h) + @debugshow 3 h if h == h_general crlf = findfirst(x->(x == bCR || x == bLF), view(bytes, p:len)) p = crlf == 0 ? len : p + crlf - 2 @@ -688,8 +695,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int t += UInt64(ch - '0') #= Overflow? Test against a conservative limit for simplicity. =# - @debug(PARSING_DEBUG, "this content_length 1") - @debug(PARSING_DEBUG, Int(parser.content_length)) + @debugshow 3 Int(parser.content_length) if div(ULLONG_MAX - 10, 10) < t parser.header_state = h @err(HPE_INVALID_CONTENT_LENGTH) @@ -854,13 +860,13 @@ function parse!(parser::Parser, bytes::ByteView)::Int parser.onheaderscomplete() #= Set this here so that on_headers_complete() callbacks can see it =# - @debug(PARSING_DEBUG, "checking for upgrade...") + @debug 3 "checking for upgrade..." if (parser.flags & F_UPGRADE > 0) && (parser.flags & F_CONNECTION_UPGRADE > 0) parser.upgrade = isrequest(parser) || parser.status == 101 else parser.upgrade = isrequest(parser) && parser.method == CONNECT end - @debug(PARSING_DEBUG, parser.upgrade) + @debugshow 3 parser.upgrade #= Here we call the headers_complete callback. This is somewhat * different than other callbacks because if the user returns 1, we * will interpret that as saying that this message has no body. This @@ -870,7 +876,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so * we have to simulate it by handling a change in errno below. =# - @debug(PARSING_DEBUG, "headersdone") + @debug 3 "headersdone" elseif p_state == s_headers_done @strictcheck(ch != LF) @@ -930,7 +936,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int p_state = s_chunk_size_almost_done else unhex_val = unhex[Int(ch)+1] - @debug(PARSING_DEBUG, unhex_val) + @debugshow 3 unhex_val if unhex_val == -1 if ch == ';' || ch == ' ' p_state = s_chunk_parameters @@ -943,8 +949,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int t += UInt64(unhex_val) #= Overflow? Test against a conservative limit for simplicity. =# - @debug(PARSING_DEBUG, "this content_length 2") - @debug(PARSING_DEBUG, Int(parser.content_length)) + @debugshow 3 Int(parser.content_length) if div(ULLONG_MAX - 16, 16) < t @err(HPE_INVALID_CONTENT_LENGTH) end @@ -1009,7 +1014,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int parser.state = p_state - @debug(PARSING_DEBUG, "exiting $(ParsingStateCode(p_state))") + @debug 3 "parse!() exiting $(ParsingStateCode(p_state))" return p end @@ -1048,4 +1053,4 @@ function http_should_keep_alive(parser) end -#FIXME end # module Parser +end # module Parsers diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 280f9a865..c423128e4 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -1,12 +1,13 @@ module RetryRequest +export request + using Retry import ..HTTP -export request - -import ..SendRequest, ..@debug, ..getkv +using ..Pairs.getkv +import ..SendRequest, ..@debug isrecoverable(e::Base.UVError) = true diff --git a/src/SendRequest.jl b/src/SendRequest.jl index 65efd6278..09daab121 100644 --- a/src/SendRequest.jl +++ b/src/SendRequest.jl @@ -2,6 +2,8 @@ module SendRequest import ..HTTP +using ..Pairs.getkv +using ..Strings.tocameldash! using ..URIs using ..Messages using ..Bodies @@ -12,7 +14,7 @@ using MbedTLS.SSLContext export request -import ..@debug, ..getkv +import ..@debug, ..DEBUG_LEVEL """ @@ -49,7 +51,7 @@ function request(uri::URI, req::Request, res::Response; kw...) setlengthheader(req, getkv(kw, :body_length, -1)) # Get a connection from the pool... - T = scheme(uri) == "https" ? SSLContext : TCPSocket + T = uri.scheme == "https" ? SSLContext : TCPSocket if getkv(kw, :use_connection_pool, true) T = Connections.Connection{T} end @@ -104,7 +106,7 @@ function request(method::String, uri, headers=[], body=""; u = URI(uri) req = Request(method, - method == "CONNECT" ? host(u) : resource(u), + method == "CONNECT" ? hostport(u) : resource(u), headers, Body(body); parent=parent) @@ -149,4 +151,7 @@ function redirect(method, uri, headers, body; kw...) end +canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) + + end # module SendRequest diff --git a/src/Strings.jl b/src/Strings.jl new file mode 100644 index 000000000..152e53dae --- /dev/null +++ b/src/Strings.jl @@ -0,0 +1,69 @@ +module Strings + +export escapehtml, tocameldash!, iso8859_1_to_utf8 + +""" +escapeHTML(i::String) + +Returns a string with special HTML characters escaped: &, <, >, ", ' +""" + +function escapehtml(i::AbstractString) + # Refer to http://stackoverflow.com/a/7382028/3822752 for spec. links + o = replace(i, "&", "&") + o = replace(o, "\"", """) + o = replace(o, "'", "'") + o = replace(o, "<", "<") + o = replace(o, ">", ">") + return o +end + + +""" + tocameldash!(s::String) + +Ensure the first character and characters that follow a '-' are uppercase. +""" + +function tocameldash!(s::String) + const toUpper = UInt8('A') - UInt8('a') + bytes = Vector{UInt8}(s) + upper = true + for i = 1:length(bytes) + @inbounds b = bytes[i] + if upper + islower(b) && (bytes[i] = b + toUpper) + else + isupper(b) && (bytes[i] = lower(b)) + end + upper = b == UInt8('-') + end + return s +end + +@inline islower(b::UInt8) = UInt8('a') <= b <= UInt8('z') +@inline isupper(b::UInt8) = UInt8('A') <= b <= UInt8('Z') +@inline lower(c::UInt8) = c | 0x20 + + +""" + iso8859_1_to_utf8(bytes) + +Convert from ISO8859_1 to UTF8. +""" + +iso8859_1_to_utf8(str::String) = iso8859_1_to_utf8(Vector{UInt8}(str)) +function iso8859_1_to_utf8(bytes::Vector{UInt8}) + io = IOBuffer() + for b in bytes + if b < 0x80 + write(io, b) + else + write(io, 0xc0 | b >> 6) + write(io, 0x80 | b & 0x3f) + end + end + return String(take!(io)) +end + +end # module Strings diff --git a/src/cookies.jl b/src/cookies.jl index 285d54842..ceeb065ed 100644 --- a/src/cookies.jl +++ b/src/cookies.jl @@ -39,7 +39,7 @@ end export Cookie import Base.== -import HTTP.isurlchar +import HTTP.URIs.isurlchar """ Cookie() diff --git a/src/debug.jl b/src/debug.jl new file mode 100644 index 000000000..5eb7bbc37 --- /dev/null +++ b/src/debug.jl @@ -0,0 +1,23 @@ +macro debug(n::Int, s) + DEBUG_LEVEL >= n ? esc(:(println(string("DEBUG: ", $s)))) : :() +end + +macro debugshow(n::Int, s) + DEBUG_LEVEL >= n ? esc(:(print("DEBUG: "); @show $s)) : :() +end + +macro src() + @static if VERSION >= v"0.7-" && length(:(@test).args) == 2 + esc(quote + (__module__, + __source__.file == nothing ? "?" : String(__source__.file), + __source__.line) + end) + else + esc(quote + (current_module(), + (p = Base.source_path(); p == nothing ? "REPL" : p), + Int(unsafe_load(cglobal(:jl_lineno, Cint)))) + end) + end +end diff --git a/src/parseutils.jl b/src/parseutils.jl new file mode 100644 index 000000000..7e29d8284 --- /dev/null +++ b/src/parseutils.jl @@ -0,0 +1,24 @@ +# parsing utils +macro anyeq(var, vals...) + ret = e = Expr(:||) + for (i, v) in enumerate(vals) + x = :($var == $v) + push!(e.args, x) + i >= length(vals) - 1 && continue + ne = Expr(:||) + push!(e.args, ne) + e = ne + end + return esc(ret) +end + +@inline lower(c) = Char(UInt32(c) | 0x20) +@inline isurlchar(c) = c > '\u80' ? true : normal_url_char[Int(c) + 1] +@inline ismark(c) = @anyeq(c, '-', '_', '.', '!', '~', '*', '\'', '(', ')') +@inline isalpha(c) = 'a' <= lower(c) <= 'z' +@inline isnum(c) = '0' <= c <= '9' +@inline isalphanum(c) = isalpha(c) || isnum(c) +@inline isuserinfochar(c) = isalphanum(c) || ismark(c) || @anyeq(c, '%', ';', ':', '&', '=', '+', '$', ',') +@inline ishex(c) = isnum(c) || ('a' <= lower(c) <= 'f') +@inline ishostchar(c) = isalphanum(c) || @anyeq(c, '.', '-', '_', '~') +@inline isheaderchar(c) = c == CR || c == LF || c == Char(9) || (c > Char(31) && c != Char(127)) diff --git a/src/uri.jl b/src/uri.jl index b00abde0f..ee3bd9206 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -4,18 +4,7 @@ import Base.== include("urlparser.jl") -export URI, URL, - hasscheme, scheme, - hashost, host, - haspath, path, - hasquery, query, - hasfragment, fragment, - hasuserinfo, userinfo, - hasport, port, - resource, host, - escape, unescape, - splitpath, queryparams, - absuri +export URI, URL, hostport, resource, queryparams, absuri, escapeuri, unescapeuri """ HTTP.URL(host; userinfo="", path="", query="", fragment="", isconnect=false) @@ -58,7 +47,7 @@ function URI(;host::AbstractString="", path::AbstractString="", fragment::AbstractString="", isconnect::Bool=false) host != "" && scheme == "" && !isconnect && (scheme = "http") io = IOBuffer() - printuri(io, scheme, userinfo, host, string(port), path, escape(query), fragment) + printuri(io, scheme, userinfo, host, string(port), path, escapeuri(query), fragment) return URI(String(take!(io)); isconnect=isconnect) end @@ -70,7 +59,7 @@ function URL(str::AbstractString; userinfo::AbstractString="", path::AbstractStr isconnect::Bool=false) if str != "" if startswith(str, "http") || startswith(str, "https") - str = string(str, path, ifelse(query == "", "", "?" * escape(query)), + str = string(str, path, ifelse(query == "", "", "?" * escapeuri(query)), ifelse(fragment == "", "", "#$fragment")) else if startswith(str, "/") || str == "*" @@ -79,7 +68,7 @@ function URL(str::AbstractString; userinfo::AbstractString="", path::AbstractStr isconnect = true else str = string("http://", userinfo == "" ? "" : "$userinfo@", - str, path, ifelse(query == "", "", "?" * escape(query)), + str, path, ifelse(query == "", "", "?" * escapeuri(query)), ifelse(fragment == "", "", "#$fragment")) end end @@ -87,28 +76,21 @@ function URL(str::AbstractString; userinfo::AbstractString="", path::AbstractStr return Base.parse(URI, str; isconnect=isconnect) end -URI(str::AbstractString; isconnect::Bool=false) = Base.parse(URI, str; isconnect=isconnect) -Base.parse(::Type{URI}, str::AbstractString; isconnect::Bool=false) = http_parser_parse_url(str, isconnect) +URI(str::AbstractString; isconnect::Bool=false) = + Base.parse(URI, str; isconnect=isconnect) -==(a::URI,b::URI) = a.scheme == b.scheme && - a.host == b.host && - a.path == b.path && - a.query == b.query && - a.fragment == b.fragment && - a.userinfo == b.userinfo && - port(a) == port(b) +Base.parse(::Type{URI}, str::AbstractString; isconnect::Bool=false) = + http_parser_parse_url(str, isconnect) -scheme(u) = u.scheme -host(u) = u.host -port(u) = u.port -path(u) = u.path -query(u) = u.query -fragment(u) = u.fragment -userinfo(u) = u.userinfo +==(a::URI,b::URI) = a.scheme == b.scheme && + hostport(a) == hostport(b) && + a.path == b.path && + a.query == b.query && + a.fragment == b.fragment && + a.userinfo == b.userinfo - -function resource(uri::URI; isconnect::Bool=false) - string(isconnect ? hostport(uri) : uri.path, +function resource(uri::URI) + string(uri.path, isempty(uri.query) ? "" : "?$(uri.query)", isempty(uri.fragment) ? "" : "#$(uri.fragment)") end @@ -160,7 +142,7 @@ end queryparams(uri::URI) = queryparams(uri.query) function queryparams(q::AbstractString) - Dict(unescape(k) => unescape(v) + Dict(unescapeuri(k) => unescapeuri(v) for (k,v) in ([split(e, "=")..., ""][1:2] for e in split(q, "&", keep=false))) end @@ -179,7 +161,7 @@ function Base.isvalid(uri::URI) if ((sch in non_hierarchical) && (search(uri.path, '/') > 1)) || # path hierarchy not allowed (!(sch in uses_query) && !isempty(uri.query)) || # query component not allowed (!(sch in uses_fragment) && !isempty(uri.fragment)) || # fragment identifier component not allowed - (!(sch in uses_authority) && (!isempty(uri.host) || ("" != port(uri)) || !isempty(uri.userinfo))) # authority component not allowed + (!(sch in uses_authority) && (!isempty(uri.host) || ("" != uri.port) || !isempty(uri.userinfo))) # authority component not allowed return false end return true @@ -195,23 +177,23 @@ end utf8_chars(str::AbstractString) = (Char(c) for c in Vector{UInt8}(str)) "percent-encode a string, dict, or pair for a uri" -function escape end +function escapeuri end -escape(c::Char) = string('%', uppercase(hex(c,2))) -escape(str::AbstractString, safe::Function=issafe) = - join(safe(c) ? c : escape(c) for c in utf8_chars(str)) +escapeuri(c::Char) = string('%', uppercase(hex(c,2))) +escapeuri(str::AbstractString, safe::Function=issafe) = + join(safe(c) ? c : escapeuri(c) for c in utf8_chars(str)) -escape(bytes::Vector{UInt8}) = bytes -escape(v::Number) = escape(string(v)) -escape(v::Symbol) = escape(string(v)) -escape(v::Nullable) = Base.isnull(v) ? "" : escape(get(v)) +escapeuri(bytes::Vector{UInt8}) = bytes +escapeuri(v::Number) = escapeuri(string(v)) +escapeuri(v::Symbol) = escapeuri(string(v)) +escapeuri(v::Nullable) = Base.isnull(v) ? "" : escapeuri(get(v)) -escape(key, value) = string(escape(key), "=", escape(value)) -escape(key, values::Vector) = escape(key => v for v in values) -escape(query) = join((escape(k, v) for (k,v) in query), "&") +escapeuri(key, value) = string(escapeuri(key), "=", escapeuri(value)) +escapeuri(key, values::Vector) = escapeuri(key => v for v in values) +escapeuri(query) = join((escapeuri(k, v) for (k,v) in query), "&") "unescape a percent-encoded uri/url" -function unescape(str) +function unescapeuri(str) contains(str, "%") || return str out = IOBuffer() i = 1 @@ -232,9 +214,6 @@ end Splits the path into components See: http://tools.ietf.org/html/rfc3986#section-3.3 """ -function splitpath end - -splitpath(uri::URI) = splitpath(uri.path) function splitpath(p::AbstractString) elems = String[] len = length(p) diff --git a/src/urlparser.jl b/src/urlparser.jl index 6a8057ec1..65aa2c18f 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -1,5 +1,5 @@ include("consts.jl") -include("utils.jl") +include("parseutils.jl") struct URLParsingError <: Exception msg::String diff --git a/src/utils.jl b/src/utils.jl deleted file mode 100644 index 10077460a..000000000 --- a/src/utils.jl +++ /dev/null @@ -1,255 +0,0 @@ -""" -escapeHTML(i::String) - -Returns a string with special HTML characters escaped: &, <, >, ", ' -""" -function escapeHTML(i::String) - # Refer to http://stackoverflow.com/a/7382028/3822752 for spec. links - o = replace(i, "&", "&") - o = replace(o, "\"", """) - o = replace(o, "'", "'") - o = replace(o, "<", "<") - o = replace(o, ">", ">") - return o -end - -macro retry(expr) - :(@retry 2 $(esc(expr))) -end - -macro retry(N, expr) - :(@retryif Any $N $(esc(expr))) -end - -macro retryif(cond, expr) - :(@retryif $(esc(cond)) 2 $(esc(expr))) -end - -macro retryif(cond, N, expr) - quote - local __r__ - for i = 1:$N - try - __r__ = $(esc(expr)) - break - catch e - typeof(e) <: $(esc(cond)) || rethrow(e) - i == $N && rethrow(e) - sleep(0.1) - end - end - __r__ - end -end - -""" -@timeout secs expr then pollint - -Start executing `expr`; if it doesn't finish executing in `secs` seconds, -then execute `then`. `pollint` controls the amount of time to wait in between -checking if `expr` has finished executing (short for polling interval). -""" -macro timeout(t, expr, then, pollint=0.01) - return quote - if $(esc(t)) == Inf - $(esc(expr)) - else - tm = Float64($(esc(t))) - start = time() - tsk = @async $(esc(expr)) - yield() - while !istaskdone(tsk) && (time() - start < tm) - sleep($pollint) - end - istaskdone(tsk) || $(esc(then)) - wait(tsk) - end - end -end - -macro src() - @static if VERSION >= v"0.7-" && length(:(@test).args) == 2 - esc(quote - (__module__, - __source__.file == nothing ? "?" : String(__source__.file), - __source__.line) - end) - else - esc(quote - (current_module(), - (p = Base.source_path(); p == nothing ? "REPL" : p), - Int(unsafe_load(cglobal(:jl_lineno, Cint)))) - end) - end -end - -""" - @debug DEBUG expr - @debug DEBUG "message" - -A macro to aid when needing to turn on extremely verbose output for debugging. -Set `const DEBUG = true` in HTTP.jl and re-compile the package to see -debug-level output from the package. When `DEBUG = false`, all `@debug` statements -compile to `nothing`. -""" -macro debug(should, expr) - m, f, l = @src() - if typeof(expr) == String - e = esc(:(println("[DEBUG - ", $m, '.', $f, ":", $(rpad(l, 5, ' ')), "]: ", $(escape_string(expr))))) - else - e = esc(:(println("[DEBUG - ", $m, '.', $f, ":", $(rpad(l, 5, ' ')), "]: ", $(sprint(Base.show_unquoted, expr)), " = ", escape_string(string($expr))))) - end - return quote - @static if $should - $e - end - end -end - -macro debug(n::Int, s) - DEBUG_LEVEL >= n ? esc(:(println(string("DEBUG: ", $s)))) : :() -end - -macro log(stmt) - # "[HTTP]: Connecting to remote host..." - return esc(:(verbose && (write(logger, "[HTTP - $(rpad(Dates.now(), 23, ' '))]: $($stmt)\n"); flush(logger)))) -end - -# parsing utils -macro anyeq(var, vals...) - ret = e = Expr(:||) - for (i, v) in enumerate(vals) - x = :($var == $v) - push!(e.args, x) - i >= length(vals) - 1 && continue - ne = Expr(:||) - push!(e.args, ne) - e = ne - end - return esc(ret) -end - -@inline islower(b::UInt8) = UInt8('a') <= b <= UInt8('z') -@inline isupper(b::UInt8) = UInt8('A') <= b <= UInt8('Z') -@inline lower(c::UInt8) = c | 0x20 -@inline lower(c) = Char(UInt32(c) | 0x20) -@inline isurlchar(c) = c > '\u80' ? true : normal_url_char[Int(c) + 1] -@inline ismark(c) = @anyeq(c, '-', '_', '.', '!', '~', '*', '\'', '(', ')') -@inline isalpha(c) = 'a' <= lower(c) <= 'z' -@inline isnum(c) = '0' <= c <= '9' -@inline isalphanum(c) = isalpha(c) || isnum(c) -@inline isuserinfochar(c) = isalphanum(c) || ismark(c) || @anyeq(c, '%', ';', ':', '&', '=', '+', '$', ',') -@inline ishex(c) = isnum(c) || ('a' <= lower(c) <= 'f') -@inline ishostchar(c) = isalphanum(c) || @anyeq(c, '.', '-', '_', '~') -@inline isheaderchar(c) = c == CR || c == LF || c == Char(9) || (c > Char(31) && c != Char(127)) - -macro shifted(meth, i, char) - return esc(:(Int($meth) << Int(16) | Int($i) << Int(8) | Int($char))) -end - -# ensure the first character and subsequent characters that follow a '-' are uppercase -function tocameldash!(s::String) - const toUpper = UInt8('A') - UInt8('a') - bytes = Vector{UInt8}(s) - upper = true - for i = 1:length(bytes) - @inbounds b = bytes[i] - if upper - islower(b) && (bytes[i] = b + toUpper) - else - isupper(b) && (bytes[i] = lower(b)) - end - upper = b == UInt8('-') - end - return s -end - -canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) - -iso8859_1_to_utf8(str::String) = iso8859_1_to_utf8(Vector{UInt8}(str)) -function iso8859_1_to_utf8(bytes::Vector{UInt8}) - io = IOBuffer() - for b in bytes - if b < 0x80 - write(io, b) - else - write(io, 0xc0 | b >> 6) - write(io, 0x80 | b & 0x3f) - end - end - return String(take!(io)) -end - -macro lock(l, expr) - esc(quote - lock($l) - try - $expr - finally - unlock($l) - end - end) -end - - -""" - setbyfirst(collection, item) -> item - -Set `item` in a `collection`. -If `first() of an exisiting matches `first(item)` it is replaced. -Otherwise the new `item` is inserted at the end of the `collection`. -""" - -function setbyfirst(c, item) - k = first(item) - if (i = findfirst(x->first(x) == k, c)) > 0 - c[i] = item - else - push!(c, item) - end - return item -end - - -""" - getbyfirst(collection, key [, default]) -> item - -Get `item` from collection where `first(item)` matches `key`. -""" - -function getbyfirst(c, k, default=nothing) - i = findfirst(x->first(x) == k, c) - return i > 0 ? c[i] : default -end - - -""" - setkv(collection, key, value) - -Set `value` for `key` in collection of key/value `Pairs`. -""" - -setkv(c, k, v) = setbyfirst(c, k => v) - -""" - getkv(collection, key [, default]) -> value - -Get `value` for `key` in collection of key/value `Pairs`, -where `first(item) == key` and `value = item[2]` -""" - -function getkv(c, k, default=nothing) - i = findfirst(x->first(x) == k, c) - return i > 0 ? c[i][2] : default -end - - -macro catch(etype, expr) - esc(quote - try - $expr - catch e - isa(e, $etype) ? e : rethrow(e) - end - end) -end diff --git a/test/messages.jl b/test/messages.jl index 8f482400b..2c4ce754a 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -126,7 +126,7 @@ using JSON function async_get(url) io = BufferStream() - q = HTTP.query(HTTP.URI(url)) + q = HTTP.URI(url).query log("GET $q") r = request("GET", url, response_stream=io) @async begin diff --git a/test/parser.jl b/test/parser.jl index a8d930d77..13f7f915d 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1,7 +1,13 @@ using HTTP.Messages +using HTTP.Parsers + +const DEFAULT_PARSER = Parser() + import Base.== +const Headers = Vector{Pair{String,String}} + ==(a::Request,b::Request) = (a.method == b.method) && (a.version == b.version) && (a.headers == b.headers) && @@ -10,7 +16,7 @@ import Base.== mutable struct Message name::String raw::String - method::HTTP.Method + method::String status_code::Int response_status::String request_path::String @@ -23,13 +29,13 @@ mutable struct Message userinfo::String port::String num_headers::Int - headers::HTTP.Headers + headers::Headers should_keep_alive::Bool upgrade::String http_major::Int http_minor::Int - Message(name::String) = new(name, "", HTTP.GET, 200, "", "", "", "", "", "", 0, "", "", "", 0, HTTP.Headers(), true, "", 1, 1) + Message(name::String) = new(name, "", "GET", 200, "", "", "", "", "", "", 0, "", "", "", 0, Headers(), true, "", 1, 1) end function Message(; name::String="", kwargs...) @@ -69,7 +75,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/test" @@ -95,7 +101,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/favicon.ico" @@ -119,7 +125,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/abcdefgh" @@ -135,7 +141,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "page=1" ,fragment= "posts-17408" ,request_path= "/forums/1/topics/2375" @@ -149,7 +155,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/get_no_headers_no_body/world" @@ -163,7 +169,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/get_one_header_no_body" @@ -181,7 +187,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 0 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/get_funky_content_length_body_hello" @@ -201,7 +207,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.POST +,method= "POST" ,query_string= "q=search" ,fragment= "hey" ,request_path= "/post_identity_body_world" @@ -223,7 +229,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.POST +,method= "POST" ,query_string= "" ,fragment= "" ,request_path= "/post_chunked_all_your_base" @@ -244,7 +250,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.POST +,method= "POST" ,query_string= "" ,fragment= "" ,request_path= "/two_chunks_mult_zero_end" @@ -267,7 +273,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.POST +,method= "POST" ,query_string= "" ,fragment= "" ,request_path= "/chunked_w_trailing_headers" @@ -290,7 +296,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.POST +,method= "POST" ,query_string= "" ,fragment= "" ,request_path= "/chunked_w_excessss_after_length" @@ -305,13 +311,13 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "foo=\"bar\"" ,fragment= "" ,request_path= "/with_\"stupid\"_quotes" ,request_url= "/with_\"stupid\"_quotes?foo=\"bar\"" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name = "apachebench get" ,raw= "GET /test HTTP/1.0\r\n" * @@ -321,7 +327,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 0 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/test" @@ -337,26 +343,26 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "foo=bar?baz" ,fragment= "" ,request_path= "/test.cgi" ,request_url= "/test.cgi?foo=bar?baz" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name = "newline prefix get" ,raw= "\r\nGET /test HTTP/1.1\r\n\r\n" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/test" ,request_url= "/test" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name = "upgrade request" ,raw= "GET /demo HTTP/1.1\r\n" * @@ -372,7 +378,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/demo" @@ -398,7 +404,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 0 -,method= HTTP.CONNECT +,method= "CONNECT" ,query_string= "" ,fragment= "" ,request_path= "" @@ -417,13 +423,13 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.REPORT +,method= "REPORT" ,query_string= "" ,fragment= "" ,request_path= "/test" ,request_url= "/test" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name= "request with no http version" ,raw= "GET /\r\n" * @@ -431,13 +437,13 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 0 ,http_minor= 9 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/" ,request_url= "/" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name= "m-search request" ,raw= "M-SEARCH * HTTP/1.1\r\n" * @@ -448,7 +454,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.MSEARCH +,method= "MSEARCH" ,query_string= "" ,fragment= "" ,request_path= "*" @@ -465,14 +471,14 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "hail=all" ,fragment= "" ,request_path= "" ,request_url= "http://hypnotoad.org?hail=all" ,host= "hypnotoad.org" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name= "host:port terminated by a query string" ,raw= "GET http://hypnotoad.org:1234?hail=all HTTP/1.1\r\n" * @@ -480,7 +486,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "hail=all" ,fragment= "" ,request_path= "" @@ -488,7 +494,7 @@ Message(name= "curl get" ,host= "hypnotoad.org" ,port= "1234" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name= "host:port terminated by a space" ,raw= "GET http://hypnotoad.org:1234 HTTP/1.1\r\n" * @@ -496,7 +502,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "" @@ -504,7 +510,7 @@ Message(name= "curl get" ,host= "hypnotoad.org" ,port= "1234" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name = "PATCH request" ,raw= "PATCH /file.txt HTTP/1.1\r\n" * @@ -517,7 +523,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.PATCH +,method= "PATCH" ,query_string= "" ,fragment= "" ,request_path= "/file.txt" @@ -537,7 +543,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 0 -,method= HTTP.CONNECT +,method= "CONNECT" ,query_string= "" ,fragment= "" ,request_path= "" @@ -557,7 +563,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "q=1" ,fragment= "narf" ,request_path= "/δ¶/δt/pope" @@ -573,7 +579,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 0 -,method= HTTP.CONNECT +,method= "CONNECT" ,query_string= "" ,fragment= "" ,request_path= "" @@ -596,7 +602,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.POST +,method= "POST" ,query_string= "" ,fragment= "" ,request_path= "/" @@ -619,7 +625,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 1 -,method= HTTP.POST +,method= "POST" ,query_string= "" ,fragment= "" ,request_path= "/" @@ -639,7 +645,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.PURGE +,method= "PURGE" ,query_string= "" ,fragment= "" ,request_path= "/file.txt" @@ -654,7 +660,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.SEARCH +,method= "SEARCH" ,query_string= "" ,fragment= "" ,request_path= "/" @@ -668,7 +674,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,fragment= "" ,request_path= "/toto" ,request_url= "http://a%12:b!&*\$@hypnotoad.org:1234/toto" @@ -676,7 +682,7 @@ Message(name= "curl get" ,userinfo= "a%12:b!&*\$" ,port= "1234" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name = "upgrade post request" ,raw= "POST /demo HTTP/1.1\r\n" * @@ -690,7 +696,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.POST +,method= "POST" ,request_path= "/demo" ,request_url= "/demo" ,num_headers= 4 @@ -711,7 +717,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 0 -,method= HTTP.CONNECT +,method= "CONNECT" ,request_url= "foo.bar.com:443" ,host="foo.bar.com" ,port="443" @@ -731,7 +737,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.LINK +,method= "LINK" ,request_path= "/images/my_dog.jpg" ,request_url= "/images/my_dog.jpg" ,query_string= "" @@ -749,7 +755,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.UNLINK +,method= "UNLINK" ,request_path= "/images/my_dog.jpg" ,request_url= "/images/my_dog.jpg" ,query_string= "" @@ -774,7 +780,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/demo" @@ -809,7 +815,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/" @@ -831,7 +837,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/demo" @@ -851,7 +857,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/demo" @@ -881,7 +887,7 @@ Message(name= "curl get" ,should_keep_alive= false ,http_major= 1 ,http_minor= 1 -,method= HTTP.GET +,method= "GET" ,query_string= "" ,fragment= "" ,request_path= "/" @@ -985,7 +991,7 @@ const responses = Message[ ,status_code= 404 ,response_status= "Not Found" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body_size= 0 ,body= "" ), Message(name= "301 no response phrase" @@ -996,7 +1002,7 @@ const responses = Message[ ,status_code= 301 ,response_status= "Moved Permanently" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name="200 trailing space on chunked body" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -1174,7 +1180,7 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name= "neither content-length nor transfer-encoding response" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -1230,7 +1236,7 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body_size= 0 ,body= "" ), Message(name= "HTTP/1.1 with a 204 status" @@ -1242,7 +1248,7 @@ const responses = Message[ ,status_code= 204 ,response_status= "No Content" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body_size= 0 ,body= "" ), Message(name= "HTTP/1.1 with a 204 status and keep-alive disabled" @@ -1346,7 +1352,7 @@ const responses = Message[ ,status_code= 200 ,response_status= "OK" ,num_headers= 0 -,headers=HTTP.Headers() +,headers=Headers() ,body= "" ), Message(name= "Content-Length-X" ,raw= "HTTP/1.1 200 OK\r\n" * @@ -1384,7 +1390,7 @@ const responses = Message[ bytes = Vector{UInt8}(req.raw) sz = t for i in 1:sz:length(bytes) - HTTP.parse!(p, view(bytes, i:min(i+sz-1, length(bytes)))) + parse!(p, view(bytes, i:min(i+sz-1, length(bytes)))) end end elseif t < 0 @@ -1405,7 +1411,7 @@ const responses = Message[ r = Request(req.raw) #r = HTTP.parse(HTTP.Request, req.raw; extraref=upgrade) end - uri = parse(HTTP.URI, r.uri; isconnect= req.method == HTTP.CONNECT) + uri = parse(HTTP.URI, r.uri; isconnect= req.method == "CONNECT") @test r.version.major == req.http_major @test r.version.minor == req.http_minor @test r.method == string(req.method) @@ -1417,7 +1423,7 @@ const responses = Message[ @test uri.port in (req.port, "80", "443") @test string(uri) == req.request_url @test length(r.headers) == req.num_headers - @test Dict(HTTP.canonicalizeheaders(r.headers)) == Dict(req.headers) + @test Dict(HTTP.SendRequest.canonicalizeheaders(r.headers)) == Dict(req.headers) @test String(take!(r.body)) == req.body # FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == req.should_keep_alive @@ -1466,12 +1472,12 @@ const responses = Message[ reqstr = "GET ../../../../etc/passwd HTTP/1.1\r\n" * "Host: test\r\n\r\n" - @test_throws HTTP.ParsingError Request(reqstr) + @test_throws ParsingError Request(reqstr) reqstr = "GET HTTP/1.1\r\n" * "Host: test\r\n\r\n" - @test_throws HTTP.ParsingError Request(reqstr) + @test_throws ParsingError Request(reqstr) reqstr = "POST / HTTP/1.1\r\n" * "Host: foo.com\r\n" * @@ -1499,7 +1505,7 @@ const responses = Message[ "0\r\n" * "\r\n" - @test_throws HTTP.ParsingError Request(reqstr) + @test_throws ParsingError Request(reqstr) reqstr = "CONNECT www.google.com:443 HTTP/1.1\r\n\r\n" @@ -1594,7 +1600,7 @@ const responses = Message[ bytes = Vector{UInt8}(resp.raw) sz = t for i in 1:sz:length(bytes) - HTTP.parse!(p, view(bytes, i:min(i+sz-1, length(bytes)))) + parse!(p, view(bytes, i:min(i+sz-1, length(bytes)))) end else r = Response(resp.raw) @@ -1604,11 +1610,11 @@ const responses = Message[ @test r.status == resp.status_code @test HTTP.Messages.statustext(r) == resp.response_status @test length(r.headers) == resp.num_headers - @test Dict(HTTP.canonicalizeheaders(r.headers)) == Dict(resp.headers) + @test Dict(HTTP.SendRequest.canonicalizeheaders(r.headers)) == Dict(resp.headers) @test String(take!(r.body)) == resp.body # FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == resp.should_keep_alive catch e - if HTTP.strict && isa(e, HTTP.ParsingError) + if HTTP.Parsers.strict && isa(e, ParsingError) println("HTTP.strict is enabled. ParsingError ignored.") else rethrow() @@ -1619,182 +1625,182 @@ const responses = Message[ @testset "HTTP.parse errors" begin reqstr = "GET / HTTP/1.1\r\n" * "Foo: F\01ailure\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) - if !HTTP.strict - r = HTTP.parse(HTTP.Request, reqstr) - @test HTTP.method(r) == HTTP.GET - @test HTTP.uri(r) == HTTP.URI("/") - @test length(HTTP.headers(r)) == 1 + HTTP.Parsers.strict && @test_throws ParsingError Request(reqstr) + if !HTTP.Parsers.strict + r = HTTP.parse(HTTP.Messages.Request, reqstr) + @test r.method == "GET" + @test r.uri == "/" + @test length(r.headers) == 1 end reqstr = "GET / HTTP/1.1\r\n" * "Foo: B\02ar\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) - if !HTTP.strict - r = HTTP.parse(HTTP.Request, reqstr) - @test HTTP.method(r) == HTTP.GET - @test HTTP.uri(r) == HTTP.URI("/") - @test length(HTTP.headers(r)) == 1 + HTTP.Parsers.strict && @test_throws ParsingError Request(reqstr) + if !HTTP.Parsers.strict + r = parse(HTTP.Messages.Request, reqstr) + @test r.method == "GET" + @test r.uri == "/" + @test length(r.headers) == 1 end respstr = "HTTP/1.1 200 OK\r\n" * "Foo: F\01ailure\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) - if !HTTP.strict - r = HTTP.parse(HTTP.Response, respstr) - @test HTTP.status(r) == 200 - @test length(HTTP.headers(r)) == 1 + HTTP.Parsers.strict && @test_throws ParsingError Response(respstr) + if !HTTP.Parsers.strict + r = parse(HTTP.Messages.Response, respstr) + @test r.status == 200 + @test length(r.headers) == 1 end respstr = "HTTP/1.1 200 OK\r\n" * "Foo: B\02ar\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) - if !HTTP.strict - r = HTTP.parse(HTTP.Response, respstr) - @test HTTP.status(r) == 200 - @test length(HTTP.headers(r)) == 1 + HTTP.Parsers.strict && @test_throws ParsingError Response(respstr) + if !HTTP.Parsers.strict + r = parse(HTTP.Messages.Response, respstr) + @test r.status == 200 + @test length(r.headers) == 1 end reqstr = "GET / HTTP/1.1\r\n" * "Fo@: Failure" - HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) - !HTTP.strict && (@test_throws HTTP.ParsingError Request(reqstr)) + HTTP.Parsers.strict && @test_throws ParsingError Request(reqstr) + !HTTP.Parsers.strict && (@test_throws ParsingError Request(reqstr)) reqstr = "GET / HTTP/1.1\r\n" * "Foo\01\test: Bar" - HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) - !HTTP.strict && (@test_throws HTTP.ParsingError Request(reqstr)) + HTTP.Parsers.strict && @test_throws ParsingError Request(reqstr) + !HTTP.Parsers.strict && (@test_throws ParsingError Request(reqstr)) respstr = "HTTP/1.1 200 OK\r\n" * "Fo@: Failure" - HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) - !HTTP.strict && (@test_throws HTTP.ParsingError Response(respstr)) + HTTP.Parsers.strict && @test_throws ParsingError Response(respstr) + !HTTP.Parsers.strict && (@test_throws ParsingError Response(respstr)) respstr = "HTTP/1.1 200 OK\r\n" * "Foo\01\test: Bar" - HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) - !HTTP.strict && (@test_throws HTTP.ParsingError Response(respstr)) + HTTP.Parsers.strict && @test_throws ParsingError Response(respstr) + !HTTP.Parsers.strict && (@test_throws ParsingError Response(respstr)) reqstr = "GET / HTTP/1.1\r\n" * "Content-Length: 0\r\nContent-Length: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) + HTTP.Parsers.strict && @test_throws ParsingError Request(reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: 0\r\nContent-Length: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) + HTTP.Parsers.strict && @test_throws ParsingError Response(respstr) reqstr = "GET / HTTP/1.1\r\n" * "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) + HTTP.Parsers.strict && @test_throws ParsingError Request(reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\nContent-Length: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) + HTTP.Parsers.strict && @test_throws ParsingError Response(respstr) reqstr = "GET / HTTP/1.1\r\n" * "Foo: 1\rBar: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Request(reqstr) + HTTP.Parsers.strict && @test_throws ParsingError Request(reqstr) respstr = "HTTP/1.1 200 OK\r\n" * "Foo: 1\rBar: 1\r\n\r\n" - HTTP.strict && @test_throws HTTP.ParsingError Response(respstr) + HTTP.Parsers.strict && @test_throws ParsingError Response(respstr) for r in ((Request, "GET / HTTP/1.1\r\n"), (Response, "HTTP/1.0 200 OK\r\n")) - HTTP.reset!(HTTP.DEFAULT_PARSER) + HTTP.Parsers.reset!(DEFAULT_PARSER) R = r[1]() - n = HTTP.parse!(HTTP.DEFAULT_PARSER, Vector{UInt8}(r[2])) - @test !HTTP.headerscomplete(HTTP.DEFAULT_PARSER) - @test !HTTP.messagecomplete(HTTP.DEFAULT_PARSER) + n = parse!(DEFAULT_PARSER, Vector{UInt8}(r[2])) + @test !headerscomplete(DEFAULT_PARSER) + @test !messagecomplete(DEFAULT_PARSER) @test n == length(Vector{UInt8}(r[2])) end buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n" - @test_throws HTTP.ParsingError r = Request(buf) + @test_throws ParsingError r = Request(buf) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "1844674407370955160" * "\r\n\r\n" r = Response() p = Messages.Parser(r) - HTTP.parse!(p, respstr) + parse!(p, respstr) @test r.status == 200 @test r.headers == ["Content-Length"=>"1844674407370955160"] respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551615" * "\r\n\r\n" e = try Response(respstr) catch e e end - @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH + @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551616" * "\r\n\r\n" e = try Response(respstr) catch e e end - @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH + @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." r = Response() p = Messages.Parser(r) - HTTP.parse!(p, respstr) + parse!(p, respstr) @test r.status == 200 @test r.headers == ["Transfer-Encoding"=>"chunked"] respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFF" * "\r\n..." e = try Response(respstr) catch e e end - @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH + @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "10000000000000000" * "\r\n..." e = try Response(respstr) catch e e end - @test isa(e, HTTP.ParsingError) && e.code == HTTP.HPE_INVALID_CONTENT_LENGTH + @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH for len in (1000, 100000) - HTTP.reset!(p) + HTTP.Parsers.reset!(p) reqstr = "POST / HTTP/1.0\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = Request() p = Messages.Parser(r) - HTTP.parse!(p, reqstr) - @test HTTP.headerscomplete(p) - @test !HTTP.messagecomplete(p) + parse!(p, reqstr) + @test headerscomplete(p) + @test !messagecomplete(p) for i = 1:len-1 - HTTP.parse!(p, "a") - @test HTTP.headerscomplete(p) - @test !HTTP.messagecomplete(p) + parse!(p, "a") + @test headerscomplete(p) + @test !messagecomplete(p) end - HTTP.parse!(p, "a") - @test HTTP.headerscomplete(p) - @test HTTP.messagecomplete(p) + parse!(p, "a") + @test headerscomplete(p) + @test messagecomplete(p) end for len in (1000, 100000) - HTTP.reset!(p) + HTTP.Parsers.reset!(p) respstr = "HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = Response() p = Messages.Parser(r) - HTTP.parse!(p, respstr) - @test HTTP.headerscomplete(p) - @test !HTTP.messagecomplete(p) + parse!(p, respstr) + @test headerscomplete(p) + @test !messagecomplete(p) for i = 1:len-1 - HTTP.parse!(p, "a") - @test HTTP.headerscomplete(p) - @test !HTTP.messagecomplete(p) + parse!(p, "a") + @test headerscomplete(p) + @test !messagecomplete(p) end - HTTP.parse!(p, "a") - @test HTTP.headerscomplete(p) - @test HTTP.messagecomplete(p) + parse!(p, "a") + @test headerscomplete(p) + @test messagecomplete(p) end reqstr = requests[1].raw * requests[2].raw r = Request() p = Messages.Parser(r) - n = HTTP.parse!(p, reqstr) - @test HTTP.headerscomplete(p) - @test HTTP.messagecomplete(p) + n = parse!(p, reqstr) + @test headerscomplete(p) + @test messagecomplete(p) ex = Vector{UInt8}(reqstr)[n+1:end] - HTTP.reset!(p) - HTTP.parse!(p, ex) - @test HTTP.headerscomplete(p) - @test HTTP.messagecomplete(p) + HTTP.Parsers.reset!(p) + parse!(p, ex) + @test headerscomplete(p) + @test messagecomplete(p) - @test_throws HTTP.ParsingError Request("GET / HTP/1.1\r\n\r\n") + @test_throws ParsingError Request("GET / HTP/1.1\r\n\r\n") r = Request("GET / HTTP/1.1\r\n" * "Test: Düsseldorf\r\n\r\n") @test r.headers == ["Test" => "Düsseldorf"] r = Response() p = Messages.Parser(r) - HTTP.parse!(p, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") + parse!(p, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") @test String(take!(r.body)) == "fooba" - for m in instances(HTTP.Method) - m == HTTP.CONNECT && continue - me = m == HTTP.MSEARCH ? "M-SEARCH" : "$m" + for m in instances(Parsers.Method) + m == Parsers.CONNECT && continue + me = m == Parsers.MSEARCH ? "M-SEARCH" : "$m" r = Request("$me / HTTP/1.1\r\n\r\n") @test r.method == string(m) end for m in ("ASDF","C******","COLA","GEM","GETA","M****","MKCOLA","PROPPATCHA","PUN","PX","SA","hello world") - @test_throws HTTP.ParsingError Request("$m / HTTP/1.1\r\n\r\n") + @test_throws ParsingError Request("$m / HTTP/1.1\r\n\r\n") end - @test_throws HTTP.ParsingError Request("GET / HTTP/1.1\r\n" * "name\r\n" * " : value\r\n\r\n") + @test_throws ParsingError Request("GET / HTTP/1.1\r\n" * "name\r\n" * " : value\r\n\r\n") reqstr = "GET / HTTP/1.1\r\n" * "X-SSL-FoooBarr: -----BEGIN CERTIFICATE-----\r\n" * diff --git a/test/uri.jl b/test/uri.jl index 1e417734b..cf5fd2fca 100644 --- a/test/uri.jl +++ b/test/uri.jl @@ -1,3 +1,4 @@ + mutable struct URLTest name::String url::String @@ -52,26 +53,26 @@ end u = parse(HTTP.URI, url) @test string(u) == url @test isvalid(u) - @test HTTP.splitpath(u) == splpath + @test HTTP.URIs.splitpath(u.path) == splpath end @test parse(HTTP.URI, "hdfs://user:password@hdfshost:9000/root/folder/file.csv") == HTTP.URI(host="hdfshost", path="/root/folder/file.csv", scheme="hdfs", port=9000, userinfo="user:password") - @test parse(HTTP.URI, "http://google.com/some/path") == HTTP.URI(host="google.com", path="/some/path") + @test parse(HTTP.URI, "http://google.com:80/some/path") == HTTP.URI(host="google.com", path="/some/path") - @test HTTP.lower(UInt8('A')) == UInt8('a') - @test HTTP.escape(Char(1)) == "%01" + @test HTTP.Strings.lower(UInt8('A')) == UInt8('a') + @test HTTP.escapeuri(Char(1)) == "%01" - @test HTTP.escape(Dict("key1"=>"value1", "key2"=>["value2", "value3"])) == "key2=value2&key2=value3&key1=value1" + @test HTTP.escapeuri(Dict("key1"=>"value1", "key2"=>["value2", "value3"])) == "key2=value2&key2=value3&key1=value1" - @test HTTP.escape("abcdef αβ 1234-=~!@#\$()_+{}|[]a;") == "abcdef%20%CE%B1%CE%B2%201234-%3D%7E%21%40%23%24%28%29_%2B%7B%7D%7C%5B%5Da%3B" - @test HTTP.unescape(HTTP.escape("abcdef 1234-=~!@#\$()_+{}|[]a;")) == "abcdef 1234-=~!@#\$()_+{}|[]a;" - @test HTTP.unescape(HTTP.escape("👽")) == "👽" + @test HTTP.escapeuri("abcdef αβ 1234-=~!@#\$()_+{}|[]a;") == "abcdef%20%CE%B1%CE%B2%201234-%3D%7E%21%40%23%24%28%29_%2B%7B%7D%7C%5B%5Da%3B" + @test HTTP.unescapeuri(HTTP.escapeuri("abcdef 1234-=~!@#\$()_+{}|[]a;")) == "abcdef 1234-=~!@#\$()_+{}|[]a;" + @test HTTP.unescapeuri(HTTP.escapeuri("👽")) == "👽" - @test HTTP.escape([("foo", "bar"), (1, 2)]) == "foo=bar&1=2" - @test HTTP.escape(Dict(["foo" => "bar", 1 => 2])) in ("1=2&foo=bar", "foo=bar&1=2") - @test HTTP.escape(["foo" => "bar", 1 => 2]) == "foo=bar&1=2" + @test HTTP.escapeuri([("foo", "bar"), (1, 2)]) == "foo=bar&1=2" + @test HTTP.escapeuri(Dict(["foo" => "bar", 1 => 2])) in ("1=2&foo=bar", "foo=bar&1=2") + @test HTTP.escapeuri(["foo" => "bar", 1 => 2]) == "foo=bar&1=2" - @test "user:password" == HTTP.userinfo(parse(HTTP.URI, "https://user:password@httphost:9000/path1/path2;paramstring?q=a&p=r#frag")) + @test "user:password" == parse(HTTP.URI, "https://user:password@httphost:9000/path1/path2;paramstring?q=a&p=r#frag").userinfo @test HTTP.queryparams(HTTP.URI("https://httphost/path1/path2;paramstring?q=a&p=r#frag")) == Dict("q"=>"a","p"=>"r") @test HTTP.queryparams(HTTP.URI("https://foo.net/?q=a&malformed")) == Dict("q"=>"a","malformed"=>"") @@ -93,7 +94,7 @@ end @test_throws HTTP.URIs.URLParsingError parse(HTTP.URI, "ht!tp://google.com") # Issue #27 - @test HTTP.escape("t est\n") == "t%20est%0A" + @test HTTP.escapeuri("t est\n") == "t%20est%0A" @testset "HTTP.parse(HTTP.URI, str)" begin diff --git a/test/utils.jl b/test/utils.jl index 848890ce2..ffab98a41 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -1,48 +1,50 @@ @testset "utils.jl" begin -@test HTTP.escapeHTML("&\"'<>") == "&"'<>" +import HTTP.Parsers -@test HTTP.isurlchar('\u81') -@test !HTTP.isurlchar('\0') +@test HTTP.Strings.escapehtml("&\"'<>") == "&"'<>" + +@test Parsers.isurlchar('\u81') +@test !Parsers.isurlchar('\0') for c = '\0':'\x7f' if c in ('.', '-', '_', '~') - @test HTTP.ishostchar(c) - @test HTTP.ismark(c) - @test HTTP.isuserinfochar(c) + @test Parsers.ishostchar(c) + @test Parsers.ismark(c) + @test Parsers.isuserinfochar(c) elseif c in ('-', '_', '.', '!', '~', '*', '\'', '(', ')') - @test HTTP.ismark(c) - @test HTTP.isuserinfochar(c) + @test Parsers.ismark(c) + @test Parsers.isuserinfochar(c) else - @test !HTTP.ismark(c) + @test !Parsers.ismark(c) end end -@test HTTP.isalphanum('a') -@test HTTP.isalphanum('1') -@test !HTTP.isalphanum(']') - -@test HTTP.ishex('a') -@test HTTP.ishex('1') -@test !HTTP.ishex(']') - -@test HTTP.tocameldash!("accept") == "Accept" -@test HTTP.tocameldash!("Accept") == "Accept" -@test HTTP.tocameldash!("eXcept-this") == "Except-This" -@test HTTP.tocameldash!("exCept-This") == "Except-This" -@test HTTP.tocameldash!("not-valid") == "Not-Valid" -@test HTTP.tocameldash!("♇") == "♇" -@test HTTP.tocameldash!("bλ-a") == "Bλ-A" -@test HTTP.tocameldash!("not fixable") == "Not fixable" -@test HTTP.tocameldash!("aaaaaaaaaaaaa") == "Aaaaaaaaaaaaa" -@test HTTP.tocameldash!("conTENT-Length") == "Content-Length" -@test HTTP.tocameldash!("Sec-WebSocket-Key2") == "Sec-Websocket-Key2" -@test HTTP.tocameldash!("User-agent") == "User-Agent" -@test HTTP.tocameldash!("Proxy-authorization") == "Proxy-Authorization" -@test HTTP.tocameldash!("HOST") == "Host" -@test HTTP.tocameldash!("ST") == "St" -@test HTTP.tocameldash!("X-\$PrototypeBI-Version") == "X-\$prototypebi-Version" -@test HTTP.tocameldash!("DCLK_imp") == "Dclk_imp" +@test Parsers.isalphanum('a') +@test Parsers.isalphanum('1') +@test !Parsers.isalphanum(']') + +@test Parsers.ishex('a') +@test Parsers.ishex('1') +@test !Parsers.ishex(']') + +@test HTTP.Strings.tocameldash!("accept") == "Accept" +@test HTTP.Strings.tocameldash!("Accept") == "Accept" +@test HTTP.Strings.tocameldash!("eXcept-this") == "Except-This" +@test HTTP.Strings.tocameldash!("exCept-This") == "Except-This" +@test HTTP.Strings.tocameldash!("not-valid") == "Not-Valid" +@test HTTP.Strings.tocameldash!("♇") == "♇" +@test HTTP.Strings.tocameldash!("bλ-a") == "Bλ-A" +@test HTTP.Strings.tocameldash!("not fixable") == "Not fixable" +@test HTTP.Strings.tocameldash!("aaaaaaaaaaaaa") == "Aaaaaaaaaaaaa" +@test HTTP.Strings.tocameldash!("conTENT-Length") == "Content-Length" +@test HTTP.Strings.tocameldash!("Sec-WebSocket-Key2") == "Sec-Websocket-Key2" +@test HTTP.Strings.tocameldash!("User-agent") == "User-Agent" +@test HTTP.Strings.tocameldash!("Proxy-authorization") == "Proxy-Authorization" +@test HTTP.Strings.tocameldash!("HOST") == "Host" +@test HTTP.Strings.tocameldash!("ST") == "St" +@test HTTP.Strings.tocameldash!("X-\$PrototypeBI-Version") == "X-\$prototypebi-Version" +@test HTTP.Strings.tocameldash!("DCLK_imp") == "Dclk_imp" for (bytes, utf8) in ( @@ -54,7 +56,7 @@ for (bytes, utf8) in ( # (UInt8[0x6e, 0x6f, 0xeb, 0x6c, 0x20, 0xa4], "noël €"), (UInt8[0xc4, 0xc6, 0xe4], "ÄÆä"), ) - @test HTTP.iso8859_1_to_utf8(bytes) == utf8 + @test HTTP.Strings.iso8859_1_to_utf8(bytes) == utf8 end # using StringEncodings From 8b57b6fc404a906390a9faf1b2de56eee857ac2d Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 11 Dec 2017 12:37:29 +1100 Subject: [PATCH 036/182] debug tweaks --- src/HTTP.jl | 13 +------------ src/Messages.jl | 2 +- src/Parsers.jl | 10 ++++++---- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 56a2f8de4..1e0b114a1 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -8,8 +8,6 @@ import MbedTLS.SSLContext const DEBUG_LEVEL = 1 -const DEBUG = false - if VERSION > v"0.7.0-DEV.2338" using Base64 end @@ -23,13 +21,9 @@ end include("debug.jl") include("Pairs.jl") include("Strings.jl") +include("IOExtras.jl") -#include("consts.jl") -#include("utils.jl") include("uri.jl") -using .URIs -#include("fifobuffer.jl") -#using .FIFOBuffers include("cookies.jl") #using .Cookies #include("multipart.jl") @@ -38,18 +32,13 @@ include("cookies.jl") #include("sniff.jl") -include("IOExtras.jl") -using .IOExtras include("Bodies.jl") -#using .Bodies include("Parsers.jl") include("Messages.jl") -#using .Messages include("Connect.jl") include("Connections.jl") -#using .Connections diff --git a/src/Messages.jl b/src/Messages.jl index 3daa6a211..6ed711e2c 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -305,7 +305,7 @@ function Base.read!(io::IO, p::Parser) n = parse!(p, bytes) @assert n == length(bytes) || messagecomplete(p) @assert n <= length(bytes) - @debug 3 ParsingStateCode(p.state) + @debug 3 "p.state = $(Parsers.ParsingStateCode(p.state))" if messagecomplete(p) excess = view(bytes, n+1:length(bytes)) diff --git a/src/Parsers.jl b/src/Parsers.jl index a138966aa..51f345ea9 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -118,24 +118,26 @@ end const ByteView = typeof(view(UInt8[], 1:0)) + parse!(p::Parser, bytes::String)::Int = parse!(p, Vector{UInt8}(bytes)) parse!(p::Parser, bytes)::Int = parse!(p, view(bytes, 1:length(bytes))) function parse!(parser::Parser, bytes::ByteView)::Int + isempty(bytes) && throw(ArgumentError("bytes must not be empty")) len = length(bytes) - @debug 3 "parse!(::Parser, $len-bytes)" p_state = parser.state - @debugshow 3 ParsingStateCode(p_state) + @debug 3 "parse!(parser.state=$(ParsingStateCode(p_state))), $len-bytes)" p = 0 while p < len && p_state != s_message_done - @debug 3 "top of while($p < $len) $(ParsingStateCode(p_state))" + @debug 3 string("top of while($p < $len) \"", + Base.escape_string(string(Char(bytes[p+1]))), "\" ", + ParsingStateCode(p_state)) p += 1 @inbounds ch = Char(bytes[p]) - @debug 3 Base.escape_string(string(ch)) if p_state == s_dead #= this state is used after a 'Connection: close' message From cf9c03dbd0923d17e09a94364d6e16d5189c156a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 11 Dec 2017 12:42:52 +1100 Subject: [PATCH 037/182] tweak import of MbedTLS.SSLContext --- src/Connections.jl | 2 +- src/HTTP.jl | 5 +++-- src/Messages.jl | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Connections.jl b/src/Connections.jl index 6e4b2fece..1facda90a 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -5,7 +5,7 @@ export getconnection using ..IOExtras import ..@debug, ..DEBUG_LEVEL -import ..SSLContext +import MbedTLS.SSLContext import ..Connect.getconnection diff --git a/src/HTTP.jl b/src/HTTP.jl index 1e0b114a1..eb03580b5 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -3,8 +3,8 @@ module HTTP #export Request, Response, FIFOBuffer -using MbedTLS -import MbedTLS.SSLContext +#using MbedTLS +#import MbedTLS.SSLContext const DEBUG_LEVEL = 1 @@ -24,6 +24,7 @@ include("Strings.jl") include("IOExtras.jl") include("uri.jl") +using .URIs include("cookies.jl") #using .Cookies #include("multipart.jl") diff --git a/src/Messages.jl b/src/Messages.jl index 6ed711e2c..6d7ee9365 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -15,7 +15,7 @@ import ..Parsers import ..@debug, ..DEBUG_LEVEL -import ..SSLContext +import MbedTLS.SSLContext """ From 57224d121188356310f000b9e2c813f3b0606c50 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 11 Dec 2017 23:11:07 +1100 Subject: [PATCH 038/182] Update client.jl API to use refactored internals. Bodies.jl - Only use chunked encoding if length is unknown. Move redirect from SendRequest to CookieRequest layer so that cookies can be processed before redirect. header() and setheader() - make header lookup case-insensitive Revert to original FIFOBuffer implementation --- src/Bodies.jl | 52 ++++-- src/Connections.jl | 2 +- src/CookieRequest.jl | 67 ++++++- src/HTTP.jl | 58 +++--- src/Messages.jl | 32 +++- src/Pairs.jl | 8 +- src/SendRequest.jl | 44 ++--- src/client.jl | 421 +++---------------------------------------- src/debug.jl | 2 + src/fifobuffer.jl | 304 +++++++++++++++++++++++++++---- src/handlers.jl | 14 +- src/multipart.jl | 6 +- src/server.jl | 2 +- src/types.jl | 342 +---------------------------------- src/uri.jl | 18 +- test/client.jl | 50 ++--- test/fifobuffer.jl | 60 +++--- test/messages.jl | 13 +- test/parser.jl | 13 +- test/runtests.jl | 14 +- 20 files changed, 574 insertions(+), 948 deletions(-) diff --git a/src/Bodies.jl b/src/Bodies.jl index 2fa98cd83..6ba486cdf 100644 --- a/src/Bodies.jl +++ b/src/Bodies.jl @@ -32,6 +32,7 @@ mutable struct Body end const notastream = IOBuffer("") +const unknownlength = -1 """ @@ -59,10 +60,11 @@ the `body`'s stream and writes it to the `io` target. `write(body, data)` writes data to the `body`'s stream. """ +Body() = Body(notastream, IOBuffer(), unknownlength) +Body(buffer::IOBuffer, l=unknownlength) = Body(notastream, buffer, l) +Body(io::IO, l=unknownlength) = Body(io, IOBuffer(body_show_max), l) Body(::Void) = Body() -Body(buffer::IOBuffer=IOBuffer()) = Body(notastream, buffer, 0) -Body(io::IO) = Body(io, IOBuffer(body_show_max), 0) -Body(data) = Body(IOBuffer(data)) +Body(data, l=unknownlength) = Body(notastream, IOBuffer(data), l) """ @@ -119,27 +121,49 @@ end function Base.write(io::IO, body::Body) if !isstream(body) - return write(io, view(body.buffer.data, 1:body.buffer.size)) + if VERSION > v"0.7.0-DEV.2338" + bytes = view(body.buffer.data, 1:body.buffer.size) + else + bytes = body.buffer.data[1:body.buffer.size] + end + write(io, bytes) + return end - # Read from `body.io` until `eof`, - # write to `io` using "chunked" encoding. - # https://tools.ietf.org/html/rfc7230#section-4.1 - @assert body.length == 0 @assert position(body.buffer) == 0 + + # Use "chunked" encoding if length is unknown. + # https://tools.ietf.org/html/rfc7230#section-4.1 + if body.length == unknownlength + writechunked(io, body) + return + end + + # Read from `body.io` until `eof`, write to `io`. while !eof(body.io) v = readavailable(body.io) - l = length(v) - if body.length < body_show_max + if body.buffer.size < body_show_max write(body.buffer, v) end - write(io, hex(l), "\r\n", v, "\r\n") - body.length += l + write(io, v) + end + return +end + + +function writechunked(io::IO, body::Body) + while !eof(body.io) + v = readavailable(body.io) + if body.buffer.size < body_show_max + write(body.buffer, v) + end + write(io, hex(length(v)), "\r\n", v, "\r\n") end write(io, "0\r\n\r\n") - return body.length + return end + function Base.write(body::Body, v) if !isstream(body) @@ -172,6 +196,8 @@ function Base.show(io::IO, body::Body) println(io, "⋮\nWaiting for $(typeof(body.io))...") elseif length(body) > length(bytes) println(io, "⋮\n$(length(body))-byte body") + elseif length(body) == unknownlength + println(io, "⋮\nlength unknown (chunked)") end end diff --git a/src/Connections.jl b/src/Connections.jl index 1facda90a..7a2879f85 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -181,7 +181,7 @@ end tcpsocket(c::Connection{SSLContext})::TCPSocket = c.io.bio tcpsocket(c::Connection{TCPSocket})::TCPSocket = c.io -localport(c::Connection) = !isopen(c.io) ? "?" : +localport(c::Connection) = !isopen(c.io) ? 0 : VERSION > v"0.7.0-DEV" ? getsockname(tcpsocket(c))[2] : Base._sockname(tcpsocket(c), true)[2] diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index 5a41ced3e..4b05b56f3 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -2,10 +2,13 @@ module CookieRequest export request +import ..HTTP + using ..URIs using ..Cookies using ..Messages using ..Pairs: getkv, setkv +using ..Strings.tocameldash! import ..@debug, ..DEBUG_LEVEL @@ -29,7 +32,7 @@ function getcookies(cookies, uri) @debug 1 "Deleting expired Cookie: $cookie.name" push!(expired, cookie) else - @debug 1 "Sending Cookie: $cookie.name to $host" + @debug 1 "Sending Cookie: $cookie.name to $uri.host" push!(tosend, cookie) end end @@ -47,6 +50,17 @@ function setcookies(cookies, host, headers) end +canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) + + +function setbasicauthorization(headers, uri) + if !isempty(uri.userinfo) && getkv(headers, "Authorization", "") == "" + @debug 1 "Adding Authorization: Basic header." + setkv(headers, "Authorization", "Basic $(base64encode(uri.userinfo))") + end +end + + function request(method::String, uri, headers=[], body=""; cookiejar=default_cookiejar, kw...) @@ -55,14 +69,57 @@ function request(method::String, uri, headers=[], body=""; cookies = getcookies(hostcookies, u) if !isempty(cookies) - setkv(headers, "Cookie", string(getkv(headers, "Cookie"), cookies)) + setkv(headers, "Cookie", string(getkv(headers, "Cookie", ""), cookies)) + end + + if getkv(kw, :basicauthorization, false) + setbasicauthorization(headers, uri) + end + + try + res = RetryRequest.request(method, uri, headers, body; kw...) + + if getkv(kw, :canonicalizeheaders, false) + res.headers = canonicalizeheaders(res.headers) + end + + setcookies(hostcookies, u.host, res.headers) + + return res + + catch e + # Redirect request to new location... + if (isa(e, HTTP.StatusError) + && isredirect(e.response) + && parentcount(e.response) < getkv(kw, :maxredirects, 3) + && header(e.response, "Location") != "" + && method != "HEAD") #FIXME why not redirect HEAD? + + setcookies(hostcookies, u.host, e.response.headers) + + return redirect(e.response, method, uri, headers, body; kw...) + else + rethrow(e) + end end + @assert false "Unreachable!" +end + - res = RetryRequest.request(method, uri, headers, body; kw...) +function redirect(res, method, uri, headers, body; kw...) + + uri = absuri(header(res, "Location"), uri) + @debug 1 "Redirect: $uri" + + if getkv(kw, :forwardheaders, true) + headers = filter(h->!(h[1] in ("Host", "Cookie")), headers) + else + headers = [] + end - setcookies(hostcookies, u.host, res.headers) + setkv(kw, :parent, res) - return res + return request(method, uri, headers, body; kw...) end diff --git a/src/HTTP.jl b/src/HTTP.jl index eb03580b5..a1a7a5322 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -1,12 +1,17 @@ __precompile__(true) module HTTP -#export Request, Response, FIFOBuffer +export Request, Response, FIFOBuffer -#using MbedTLS -#import MbedTLS.SSLContext +using MbedTLS +import MbedTLS.SSLContext +const TLS = MbedTLS -const DEBUG_LEVEL = 1 +import Base.== + +const DEBUG = false # FIXME rm +const PARSING_DEBUG = false # FIXME rm +const DEBUG_LEVEL = 2 if VERSION > v"0.7.0-DEV.2338" using Base64 @@ -18,53 +23,52 @@ else import Dates end +#FIXME +status(r) = r.status +headers(r) = Dict(r.headers) + include("debug.jl") include("Pairs.jl") include("Strings.jl") include("IOExtras.jl") +include("consts.jl") +include("utils.jl") + include("uri.jl") using .URIs +include("fifobuffer.jl") +using .FIFOBuffers include("cookies.jl") -#using .Cookies -#include("multipart.jl") -#include("types.jl") - -#include("sniff.jl") - - +using .Cookies +include("multipart.jl") include("Bodies.jl") include("Parsers.jl") +import .Parsers.ParsingError include("Messages.jl") include("Connect.jl") include("Connections.jl") - - include("SendRequest.jl") +import .SendRequest.StatusError + +include("types.jl") +include("client.jl") +include("sniff.jl") -#include("client.jl") -#include("handlers.jl") -#using .Handlers -#include("server.jl") -#using .Nitrogen +include("handlers.jl") +using .Handlers +include("server.jl") +using .Nitrogen #include("precompile.jl") function __init__() -# global const client_module = module_parent(current_module()) -# global const DEFAULT_CLIENT = Client() + global const DEFAULT_CLIENT = Client() end -abstract type HTTPError <: Exception end - -struct StatusError <: HTTPError - status::Int16 - response::Messages.Response -end -StatusError(r::Messages.Response) = StatusError(r.status, r) include("RetryRequest.jl") include("CookieRequest.jl") diff --git a/src/Messages.jl b/src/Messages.jl index 6d7ee9365..af1170b50 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -136,7 +136,8 @@ waitforheaders(r::Response) = while r.status == 0; wait(r.headerscomplete) end Get header value for `key`. """ -header(m, k::String, d::String="") = getbyfirst(m.headers, k, k => d)[2] +header(m, k::String, d::String="") = getbyfirst(m.headers, k, k => d, lceq)[2] +lceq(a,b) = lowercase(a) == lowercase(b) """ @@ -144,7 +145,7 @@ header(m, k::String, d::String="") = getbyfirst(m.headers, k, k => d)[2] Set header `value` for `key`. """ -setheader(m, v::Pair) = setbyfirst(m.headers, Pair{String,String}(v)) +setheader(m, v::Pair) = setbyfirst(m.headers, Pair{String,String}(v), lceq) """ @@ -157,27 +158,27 @@ function defaultheader(m, v::Pair) if header(m, first(v)) == "" setheader(m, v) end + return end """ - setlengthheader(::Response, [, length]) + setlengthheader(::Response) Set the Content-Length or Transfer-Encoding header according to the `Response` `Body`. """ -function setlengthheader(r::Request, l=-1) +function setlengthheader(r::Request) - if !isstream(r.body) - l = length(r.body) - end - if l >= 0 - setheader(r, "Content-Length" => string(l)) - else + l = length(r.body) + if l == Bodies.unknownlength setheader(r, "Transfer-Encoding" => "chunked") + else + setheader(r, "Content-Length" => string(l)) end + return end @@ -206,6 +207,7 @@ function appendheader(m::Message, header::Pair{String,String}) else push!(m.headers, header) end + return end @@ -226,10 +228,12 @@ e.g. `"GET /path HTTP/1.1\\r\\n"` or `"HTTP/1.1 200 OK\\r\\n"` function writestartline(io::IO, r::Request) write(io, "$(r.method) $(r.uri) $(httpversion(r))\r\n") + return end function writestartline(io::IO, r::Response) write(io, "$(httpversion(r)) $(r.status) $(statustext(r))\r\n") + return end @@ -244,6 +248,7 @@ function writeheaders(io::IO, m::Message) write(io, "$name: $value\r\n") end write(io, "\r\n") + return end @@ -257,6 +262,7 @@ function Base.write(io::IO, m::Message) writestartline(io, m) writeheaders(io, m) write(io, m.body) + return end @@ -274,12 +280,14 @@ function readstartline!(r::Response, p::Parser) end notify(r.headerscomplete) yield() + return end function readstartline!(r::Request, p::Parser) r.version = VersionNumber(p.major, p.minor) r.method = string(p.method) r.uri = p.url + return end @@ -320,6 +328,7 @@ function Base.read!(io::IO, p::Parser) throw(ParsingError(headerscomplete(p) ? Parsers.HPE_BODY_INCOMPLETE : Parsers.HPE_HEADERS_INCOMPLETE)) end + return end @@ -352,6 +361,8 @@ function Base.read!(io::IO, m::Message) end +Base.take!(m::Message) = take!(m.body) + function Base.String(m::Message) io = IOBuffer() @@ -367,6 +378,7 @@ function Base.show(io::IO, m::Message) writeheaders(io, m) show(io, m.body) print(io, "\"\"\"") + return end diff --git a/src/Pairs.jl b/src/Pairs.jl index 59f2f1250..8c8a0d26e 100644 --- a/src/Pairs.jl +++ b/src/Pairs.jl @@ -11,9 +11,9 @@ If `first() of an exisiting matches `first(item)` it is replaced. Otherwise the new `item` is inserted at the end of the `collection`. """ -function setbyfirst(c, item) +function setbyfirst(c, item, eq = ==) k = first(item) - if (i = findfirst(x->first(x) == k, c)) > 0 + if (i = findfirst(x->eq(first(x), k), c)) > 0 c[i] = item else push!(c, item) @@ -28,8 +28,8 @@ end Get `item` from collection where `first(item)` matches `key`. """ -function getbyfirst(c, k, default=nothing) - i = findfirst(x->first(x) == k, c) +function getbyfirst(c, k, default=nothing, eq = ==) + i = findfirst(x->eq(first(x), k), c) return i > 0 ? c[i] : default end diff --git a/src/SendRequest.jl b/src/SendRequest.jl index 09daab121..14d1fedba 100644 --- a/src/SendRequest.jl +++ b/src/SendRequest.jl @@ -1,9 +1,10 @@ module SendRequest +export request, StatusError + import ..HTTP using ..Pairs.getkv -using ..Strings.tocameldash! using ..URIs using ..Messages using ..Bodies @@ -12,7 +13,6 @@ using ..Connections using ..IOExtras using MbedTLS.SSLContext -export request import ..@debug, ..DEBUG_LEVEL @@ -45,10 +45,11 @@ end Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. """ + function request(uri::URI, req::Request, res::Response; kw...) defaultheader(req, "Host" => uri.host) - setlengthheader(req, getkv(kw, :body_length, -1)) + setlengthheader(req) # Get a connection from the pool... T = uri.scheme == "https" ? SSLContext : TCPSocket @@ -99,6 +100,7 @@ println(stat("response_file").size) """ function request(method::String, uri, headers=[], body=""; + bodylength=Bodies.unknownlength, parent=nothing, response_stream=nothing, kw...) @@ -108,50 +110,28 @@ function request(method::String, uri, headers=[], body=""; req = Request(method, method == "CONNECT" ? hostport(u) : resource(u), headers, - Body(body); + Body(body, bodylength), parent=parent) res = Response(body=Body(response_stream), parent=req) request(u, req, res; kw...) - if getkv(kw, :canonicalizeheaders, false) - res.headers = canonicalizeheaders(res.headers) - end - - # Redirect request to new location for: 301 Moved Permanently, 302 Found, - # 307 Temporary Redirect, and 308 Permanent Redirect... - if (isredirect(res) - && parentcount(res) < getkv(kw, :maxredirects, 3) - && header(res, "Location") != "" - && method != "HEAD") #FIXME why not redirect HEAD? - - return redirect(method, absuri(header(res, "Location"), uri), headers, body; - parent=res, response_stream=response_stream, kw...) - end - # Throw StatusError for non Status-2xx Response Messages... - if iserror(res) && getkv(kw, :throw_status_errors, true) - throw(HTTP.StatusError(res)) + if iserror(res) && getkv(kw, :statusraise, true) + throw(StatusError(res)) end return res end -function redirect(method, uri, headers, body; kw...) - - if getkv(kw, :forwardheaders, true) - headers = filter((k,v)->!(k in ("Host", "Cookie")), headers) - else - headers = [] - end - - return request(method, uri, headers, body; kw...) +struct StatusError <: Exception + status::Int16 + response::Messages.Response end - -canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) +StatusError(r::Messages.Response) = StatusError(r.status, r) end # module SendRequest diff --git a/src/client.jl b/src/client.jl index fe8a6628a..83552a16a 100644 --- a/src/client.jl +++ b/src/client.jl @@ -1,26 +1,4 @@ -@enum ConnectionState Busy Idle Dead - -""" - HTTP.Connection - -Represents a persistent client connection to a remote host; only created -when a server response includes the "Connection: keep-alive" header. An open and non-idle connection -will be reused when sending subsequent requests to the same host. -""" -mutable struct Connection{I <: IO} - id::Int - socket::I - state::ConnectionState - parser::Parser -end - -Connection(tcp::IO) = Connection(0, tcp, Busy, Parser()) -Connection(id::Int, tcp::IO) = Connection(id, tcp, Busy, Parser()) -busy!(conn::Connection) = (conn.state == Dead || (conn.state = Busy); return) -idle!(conn::Connection) = (conn.state == Dead || (conn.state = Idle); return) -dead!(conn::Connection) = (conn.state == Dead || (conn.state = Dead; #=close(conn.socket)=#); return) -#FIXME maybe should do "close" in the connection pool manager instead of here? -# Need a regular cleanup function ?? +using .Parsers """ HTTP.Client([logger::IO]; args...) @@ -44,10 +22,6 @@ Additional keyword arguments can be passed that will get transmitted with each H * `logbody::Bool`: whether the request body should be logged when `verbose=true` is passed; default = `true` """ mutable struct Client - # connection pools for keep-alive; key is host - poollock::ReentrantLock - httppool::Dict{String, Vector{Connection{TCPSocket}}} - httpspool::Dict{String, Vector{Connection{TLS.SSLContext}}} # cookies are stored in-memory per host and automatically sent when appropriate cookies::Dict{String, Set{Cookie}} # buffer::Vector{UInt8} #TODO: create a fixed size buffer for reading bytes off the wire and having http_parser use, this should keep allocations down, need to make sure MbedTLS supports blocking readbytes! @@ -57,9 +31,7 @@ mutable struct Client connectioncount::Int end -Client(logger::Option{IO}, options::RequestOptions) = Client(ReentrantLock(), - Dict{String, Vector{Connection{TCPSocket}}}(), - Dict{String, Vector{Connection{TLS.SSLContext}}}(), +Client(logger::Option{IO}, options::RequestOptions) = Client( Dict{String, Set{Cookie}}(), logger, options, 1) @@ -75,379 +47,38 @@ function setclient!(client::Client) global const DEFAULT_CLIENT = client end -Base.haskey(::Type{http}, client, host) = haskey(client.httppool, host) -Base.haskey(::Type{https}, client, host) = haskey(client.httpspool, host) - -getconnections(::Type{http}, client, host) = client.httppool[host] -getconnections(::Type{https}, client, host) = client.httpspool[host] - -setconnection!(::Type{http}, client, host, conn) = push!(get!(client.httppool, host, Connection[]), conn) -setconnection!(::Type{https}, client, host, conn) = push!(get!(client.httpspool, host, Connection[]), conn) - -backtrace() = sprint(Base.show_backtrace, catch_backtrace()) - -""" -Abstract error type that all other HTTP errors subtype, including: - - * `HTTP.ConnectError`: thrown if a valid connection cannot be opened to the requested host/port - * `HTTP.SendError`: thrown if a request is not able to be sent to the server - * `HTTP.ClosedError`: thrown during sending or receiving if the connection to the server has been closed - * `HTTP.ReadError`: thrown if an I/O error occurs when receiving a response from a server - * `HTTP.RedirectError`: thrown if the number of http redirects exceeds the http request option `maxredirects` - * `HTTP.StatusError`: thrown if a non-successful http status code is returned from the server, never thrown if `statusraise=false` is passed as a request option -""" -abstract type HTTPError <: Exception end - -function Base.show(io::IO, e::HTTPError) - println(io, "$(typeof(e)):") - println(io, "Exception: $(e.e)") - print(io, e.msg) -end -"An HTTP error thrown if a valid connection cannot be opened to the requested host/port" -struct ConnectError <: HTTPError - e::Exception - msg::String -end -"An HTTP error thrown if a request is not able to be sent to the server" -struct SendError <: HTTPError - e::Exception - msg::String -end -"An HTTP error thrown during sending or receiving if the connection to the server has been closed" -struct ClosedError <: HTTPError - e::Exception - msg::String -end -"An HTTP error thrown if an I/O error occurs when receiving a response from a server" -struct ReadError <: HTTPError - e::Exception - msg::String -end -"An HTTP error thrown if the number of http redirects exceeds the http request option `maxredirects`" -struct RedirectError <: HTTPError - maxredirects::Int -end -function Base.show(io::IO, err::RedirectError) - print(io, "RedirectError: more than $(err.maxredirects) redirects attempted") -end -"An HTTP error thrown if a non-successful http status code is returned from the server, never thrown if `statusraise=false` is passed as a request option" -struct StatusError <: HTTPError - status::Int - response::Response -end -function Base.show(io::IO, err::StatusError) - print(io, "HTTP.StatusError: received a '$(err.status) - $(Base.get(STATUS_CODES, err.status, "Unknown Code"))' status in response") -end - -initTLS!(::Type{http}, hostname, opts, socket) = socket - -function initTLS!(::Type{https}, hostname, opts, socket) - stream = TLS.SSLContext() - TLS.setup!(stream, get(opts, :tlsconfig, TLS.SSLConfig(!opts.insecure::Bool))::TLS.SSLConfig) - TLS.associate!(stream, socket) - TLS.hostname!(stream, hostname) - TLS.handshake!(stream) - return stream -end - -function stalebytes!(c::TCPSocket) - !isopen(c) && return - nb_available(c) > 0 && readavailable(c) - return -end -stalebytes!(c::TLS.SSLContext) = stalebytes!(c.bio) - -function connect(client, sch, hostname, port, opts, verbose) - @lock client.poollock begin - logger = client.logger - if haskey(sch, client, hostname) - @log "checking if any existing connections to '$hostname' are re-usable" - conns = getconnections(sch, client, hostname) - inds = Int[] - i = 1 - while i <= length(conns) - c = conns[i] - # read off any stale bytes left over from a possible error in a previous request - # this will also trigger any sockets that timed out to be set to closed - stalebytes!(c.socket) - if !isopen(c.socket) || c.state == Dead - @log "found dead connection #$(c.id) to delete" - dead!(c) - push!(inds, i) - elseif c.state == Idle - @log "found re-usable connection #$(c.id)" - busy!(c) - try - deleteat!(conns, sort!(unique(inds))) - end - return c - end - i += 1 - end - try - deleteat!(conns, sort!(unique(inds))) - end - end - # if no re-usable connection was found, make a new connection - try - # EH: throws DNSError, OutOfMemoryError, or SystemError; retry once, but otherwise, we can't do much - ip = @retry Base.getaddrinfo(hostname) - # EH: throws error, ArgumentError for out-of-range port, UVError; retry if UVError - tcp = @retryif Base.UVError @timeout(opts.connecttimeout::Float64, - Base.connect(ip, Base.parse(Int, port)), error("connect timeout")) - socket = initTLS!(sch, hostname, opts, tcp) - conn = Connection(client.connectioncount, socket) - client.connectioncount += 1 - setconnection!(sch, client, hostname, conn) - @log "created new connection #$(conn.id) to '$hostname'" - return conn - catch e - rethrow(ConnectError(e, "connect error")) - end - end -end - -function connect(client, req::Request, opts, verbose) - - logger = client.logger - - @log "$(method(req)) $(uri(req))" - - connect(client, - scheme(uri(req)) == "http" ? http : https, - hostname(uri(req)), - port(uri(req)) == "" ? "80" : port(uri(req)), - opts, - verbose) -end - -function addcookies!(client, host, req, verbose) - logger = client.logger - # check if cookies should be added to outgoing request based on host - if haskey(client.cookies, host) - cookies = client.cookies[host] - tosend = Vector{Cookie}() - expired = Vector{Cookie}() - for (i, cookie) in enumerate(cookies) - if Cookies.shouldsend(cookie, scheme(uri(req)) == "https", host, path(uri(req))) - cookie.expires != Dates.DateTime() && cookie.expires < Dates.now(Dates.UTC) && (push!(expired, cookie); @log("deleting expired cookie: " * cookie.name); continue) - push!(tosend, cookie) - end - end - setdiff!(client.cookies[host], expired) - if length(tosend) > 0 - @log "adding cached cookies for host to request header: " * join(map(x->x.name, tosend), ", ") - setheader(req, "Cookie" => string(header(req, "Cookie"), tosend)) - end - end -end - -function sendrequest(client, req::Request, conn, opts, verbose) - - @log "sending request over the wire\n" - verbose && (show(client.logger, req); println(client.logger, "")) - - @protected try - - # EH: throws ArgumentError if socket is closed, UVError; retry if UVError, - write(conn.socket, req, opts) - catch e - if isa(e, Base.UVError) - e = SendError(e, "error sending request") - end - end -end - -function readresponse(client, req::Request, conn, stream, opts, verbose) - - @protected try - - response = Response(req) - reset!(conn.parser) - conn.parser.onbody = x->write(response.body, x) - conn.parser.onheader = x->appendheader(response, x) - processresponse!(client, conn, response, HTTP.method(req), stream, verbose) - response.status = conn.parser.status - - if opts.statusraise::Bool - s = status(response) - if s < 200 || s >= 300 - throw(StatusError(s, response)) - end - end - return response - - catch e - if isa(e, Base.UVError) - e = ReadError(e, "error reading response") - end - end -end - -function redirect(response, client, req, opts, stream, retry, verbose) - logger = client.logger - - r = Request(req.method, - absuri(header(response, "Location"), uri(req)), - opts.forwardheaders ? - filter((k,v)->!(k in ("Host", "Cookie")), req.headers) : - Headers(), - req.body) - - @log "redirecting to $(uri(r))" - return request(client, r, opts, stream, retry, verbose) -end - -const CLOSED_ERROR = ClosedError(ErrorException(""), "error receiving response; connection was closed prematurely") - -function processresponse!(client, conn, response, method, stream, verbose) - logger = client.logger - while !eof(conn.socket) - # EH: returns UInt8[] when socket is closed, - # error when socket is not readable, AssertionErrors, UVError; - bytes = readavailable(conn.socket) - if isempty(bytes) - # https://github.com/JuliaWeb/MbedTLS.jl/issues/113 - @assert isa(conn.socket, MbedTLS.SSLContext) - @assert eof(conn.socket) - break - end - @assert length(bytes) > 0 - @log "received bytes from the wire, processing" - # EH: throws a couple of "shouldn't get here" errors; probably not much we can do - HTTP.parse!(conn.parser, bytes; method=method) - - if messagecomplete(conn.parser) - close(response.body) - end - - if messagecomplete(conn.parser) || !hasmessagebody(response) - http_should_keep_alive(conn.parser) || - (@log("closing connection (no keep-alive)"); dead!(conn)) - idle!(conn) - # idle! on a Dead will stay Dead - return - elseif stream && headerscomplete(conn.parser) - @log "processing the rest of response asynchronously" - @async processresponse!(client, conn, response, method, false, false) - return - end - end - - dead!(conn) - close(response.body) - if !waitingforeof(conn.parser) - throw(CLOSED_ERROR) - end - return -end - -function attemptrequest(client::Client, req::Request, - opts::RequestOptions, stream::Bool, verbose::Bool) - - conn = connect(client, req, opts, verbose) - @protected try - sendrequest(client, req, conn[], opts, verbose) - readresponse(client, req, conn[], stream, opts, verbose) - catch e - dead!(conn[]) - end -end - -function request(client::Client, req::Request, - opts::RequestOptions, stream::Bool, retry::Int, verbose::Bool) - - update!(opts, client.options) - if verbose && not(client.logger) - client.logger = STDOUT - end - logger = client.logger - - @log "using request options:\n\t" * - join((s=>getfield(opts, s) for s in fieldnames(typeof(opts))), "\n\t") - - if opts.managecookies::Bool - addcookies!(client, hostname, req, verbose) - end - - n = max(0, retry) + 1 - response = @repeat n try - - attemptrequest(client, req, opts, stream, verbose) - - catch e - - @delay_retry if (isa(e, HTTPError) - || isa(e, Base.UVError) - || (isa(e, StatusError) && (e.status < 200 || - e.status >= 500))) - end - - if (isa(e, StatusError) - && e.status in (301, 302, 307, 308) - && opts.allowredirects - && referrercount(req) < opts.maxredirects - && req.method != HEAD #FIXME why not redirect HEAD? - && header(e.response, "Location") != "") - - h = opts.forwardheaders ? - filter((k,v)->!(k in ("Host", "Cookie")), req.headers) : - Headers() +# build Request +function request(client::Client, method, uri::URI; + headers::Dict=Headers(), + body="", + enablechunked::Bool=true, + stream::Bool=false, + verbose::Bool=false, + args...) + #opts = RequestOptions(; args...) + #not(client.logger) && (client.logger = STDOUT) + #client.logger != STDOUT && (verbose = true) - req = Request(req.method, - absuri(header(response, "Location"), uri(req)), - h, - req.body) + m = string(method) + h = [k => v for (k,v) in headers] - return request(client, req, opts, stream, retry, verbose) - end + if stream + push!(args, :response_stream => BufferStream()) end - @log "received response" - if opts.canonicalizeheaders::Bool - response.headers = canonicalizeheaders(response.headers) + if isa(body, Dict) + body = HTTP.Form(body) + Pairs.setbyfirst(h, "Content-Type" => + "multipart/form-data; boundary=$(body.boundary)") + Pairs.setkv(args, :bodylength, length(body)) end - if opts.managecookies::Bool && any(x->x[1]=="Set-Cookie", response.headers) - cookies = get!(client.cookies, host, Set{Cookie}()) - push!(cookies, (Cookies.readsetcookie(host, v[2]) - for v in filter(x->x[1]=="Set-Cookie", response.headers))...) - @log("caching received cookie for host: " * cookies) + if !enablechunked && isa(body, IO) + body = read(body) end - return response -end - -request(req::Request; - opts::RequestOptions=RequestOptions(), - stream::Bool=false, - retry::Int=0, - verbose::Bool=false, - args...) = - request(DEFAULT_CLIENT, req, RequestOptions(opts; args...), stream, retry, verbose) - -request(client::Client, req::Request; - opts::RequestOptions=RequestOptions(), - stream::Bool=false, - history::Vector{Response}=Response[], - retry::Int=0, - verbose::Bool=false, - args...) = - request(client, req, RequestOptions(opts; args...), stream, history, retry, verbose) - -# build Request -function request(client::Client, method, uri::URI; - headers::Dict=Dict(), - body=FIFOBuffer(), - stream::Bool=false, - verbose::Bool=false, - args...) - opts = RequestOptions(; args...) - not(client.logger) && (client.logger = STDOUT) - client.logger != STDOUT && (verbose = true) - req = Request(method, uri, headers, body; options=opts, verbose=verbose, logger=client.logger) - return request(client, req; opts=opts, stream=stream, verbose=verbose) + return CookieRequest.request(m, uri, h, body; args...) end request(uri::AbstractString; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, GET, URIs.URL(uri; query=query); verbose=verbose, args...) request(uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, GET, uri; verbose=verbose, args...) @@ -516,7 +147,7 @@ Access-Control-Allow-Origin: * Server: meinheld/0.6.1 Content-Length: 32 -{ +{ "origin": "50.207.241.62" } \"\"\" diff --git a/src/debug.jl b/src/debug.jl index 5eb7bbc37..8b434a237 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -6,6 +6,7 @@ macro debugshow(n::Int, s) DEBUG_LEVEL >= n ? esc(:(print("DEBUG: "); @show $s)) : :() end +#= macro src() @static if VERSION >= v"0.7-" && length(:(@test).args) == 2 esc(quote @@ -21,3 +22,4 @@ macro src() end) end end +=# diff --git a/src/fifobuffer.jl b/src/fifobuffer.jl index dbf330ff3..013e40a91 100644 --- a/src/fifobuffer.jl +++ b/src/fifobuffer.jl @@ -1,60 +1,294 @@ module FIFOBuffers +import Base.== + export FIFOBuffer -struct FIFOBuffer{T <: Union{IOBuffer,BufferStream}} <: IO - io::T -end +""" + FIFOBuffer([max::Integer]) + FIFOBuffer(string_or_bytes_vector) + FIFOBuffer(io::IO) -FIFOBuffer() = FIFOBuffer{BufferStream}(BufferStream()) -FIFOBuffer(bytes::Vector{UInt8}) = FIFOBuffer{IOBuffer}(IOBuffer(bytes)) -FIFOBuffer(str::String) = FIFOBuffer{IOBuffer}(IOBuffer(str)) +A `FIFOBuffer` is a first-in, first-out, in-memory, async-friendly IO buffer type. -FIFOBuffer(io::IOStream) = FIFOBuffer(read(io)) -FIFOBuffer(io::IO) = FIFOBuffer(readavailable(io)) +`FIFOBuffer([max])`: creates a "open" `FIFOBuffer` with a maximum size of `max`; this means that bytes can be written +up until `max` number of bytes have been written (with none being read). At this point, the `FIFOBuffer` is full +and will return 0 for all subsequent writes. If no `max` (`FIFOBuffer()`) argument is given, then a default size of `typemax(Int32)^2` is used; +this essentially allows all writes every time. Note that providing a string or byte vector argument mirrors the behavior of `Base.IOBuffer` +in that the `max` size of the `FIFOBuffer` is the length of the string/byte vector; it is also not writeable. -FIFOBuffer(f::FIFOBuffer) = f +Reading is supported via `readavailable(f)` and `read(f, nb)`, which returns all or `nb` bytes, respectively, starting at the earliest bytes written. +All read functions will return an empty byte vector, even if the buffer has been closed. Checking `eof` will correctly reflect when the buffer has +been closed and no more bytes will be available for reading. -peekbytes(f::FIFOBuffer{IOBuffer}) = f.io.data[f.io.ptr:f.io.size] -peekbytes(f::FIFOBuffer{BufferStream}) = peekbytes(FIFOBuffer(f.io.buffer)) +You may call `String(f::FIFOBuffer)` to view the current contents in the buffer without consuming them. -Base.String(f::FIFOBuffer{IOBuffer}) = String(f.io.data[f.io.ptr:f.io.size]) -Base.String(f::FIFOBuffer{BufferStream}) = String(FIFOBuffer(f.io.buffer)) +A `FIFOBuffer` is built to be used asynchronously to allow buffered reading and writing. In particular, a `FIFOBuffer` +detects if it is being read from/written to the main task, or asynchronously, and will behave slightly differently depending on which. -import Base.== -function ==(a::FIFOBuffer, b::FIFOBuffer) - (nb_available(a) == 0 && nb_available(b) == 0) || String(a) == String(b) +Specifically, when reading from a `FIFOBuffer`, if accessed from the main task, it will not block if there are no bytes available to read, instead returning an empty `UInt8[]`. +If being read from asynchronously, however, reading will block until additional bytes have been written. An example of this in action is: + +```julia +f = HTTP.FIFOBuffer(5) # create a FIFOBuffer that will hold at most 5 bytes, currently empty +f2 = HTTP.FIFOBuffer(5) # a 2nd buffer that we'll write to asynchronously + +# start an asynchronous writing task with the 2nd buffer +tsk = @async begin + while !eof(f) + write(f2, readavailable(f)) + end end +# now write some bytes to the first buffer +# writing triggers our async task to wake up and read the bytes we just wrote +# leaving the first buffer empty again and blocking again until more bytes have been written +write(f, [0x01, 0x02, 0x03, 0x04, 0x05]) -Base.readavailable(f::FIFOBuffer) = readavailable(f.io) +# we can see that `f2` now holds the bytes we wrote to `f` +String(readavailable(f2)) -# See issue #24465: "mark/reset broken for BufferStream" -# https://github.com/JuliaLang/julia/issues/24465 -# So, need to reach down into IOBuffer for readavailable(): -Base.readavailable(f::FIFOBuffer{BufferStream}) = readavailable(f.io.buffer) +# our async task will continue until `f` is closed +close(f) -Base.read(f::FIFOBuffer, a...) = read(f.io, a...) -Base.read(f::FIFOBuffer, ::Type{UInt8}) = read(f.io, UInt8) -Base.write(f::FIFOBuffer, bytes::Vector{UInt8}) = write(f.io, bytes) +istaskdone(tsk) # true +``` +""" +mutable struct FIFOBuffer <: IO + len::Int64 # length of buffer in bytes + max::Int64 # the max size buffer is allowed to grow to + nb::Int64 # number of bytes available to read in buffer + f::Int64 # buffer index that should be read next, unless nb == 0, then buffer is empty + l::Int64 # buffer index that should be written to next, unless nb == len, then buffer is full + buffer::Vector{UInt8} + cond::Condition + task::Task + eof::Bool +end + +const DEFAULT_MAX = Int64(typemax(Int32))^Int64(2) + +FIFOBuffer(f::FIFOBuffer) = f +FIFOBuffer(max) = FIFOBuffer(0, max, 0, 1, 1, UInt8[], Condition(), current_task(), false) +FIFOBuffer() = FIFOBuffer(DEFAULT_MAX) + +const EMPTYBODY = FIFOBuffer() -map(eval, :(Base.$f(f::FIFOBuffer) = $f(f.io)) - for f in [:nb_available, :flush, :eof, :isopen, :close]) +FIFOBuffer(str::String) = FIFOBuffer(Vector{UInt8}(str)) +function FIFOBuffer(bytes::Vector{UInt8}) + len = length(bytes) + return FIFOBuffer(len, len, len, 1, 1, bytes, Condition(), current_task(), true) +end +FIFOBuffer(io::IOStream) = FIFOBuffer(read(io)) +FIFOBuffer(io::IO) = FIFOBuffer(readavailable(io)) -Base.length(f::FIFOBuffer) = nb_available(f) +==(a::FIFOBuffer, b::FIFOBuffer) = String(a) == String(b) +Base.length(f::FIFOBuffer) = f.nb +Base.nb_available(f::FIFOBuffer) = f.nb +Base.wait(f::FIFOBuffer) = wait(f.cond) +Base.read(f::FIFOBuffer) = readavailable(f) +Base.flush(f::FIFOBuffer) = nothing +Base.position(f::FIFOBuffer) = f.f, f.l, f.nb +function Base.seek(f::FIFOBuffer, pos::Tuple{Int64, Int64, Int64}) + f.f = pos[1] + f.l = pos[2] + f.nb = pos[3] + return +end + +Base.eof(f::FIFOBuffer) = f.eof && f.nb == 0 +Base.isopen(f::FIFOBuffer) = !f.eof +function Base.close(f::FIFOBuffer) + f.eof = true + notify(f.cond) + return +end + +# 0 | 1 | 2 | 3 | 4 | 5 | +#---|---|---|---|---|---| +# |f/l| _ | _ | _ | _ | empty, f == l, nb = 0, can't read, can write from l to l-1, don't need to change f, l = l, nb = len +# | _ | _ |f/l| _ | _ | empty, f == l, nb = 0, can't read, can write from l:end, 1:l-1, don't need to change f, l = l, nb = len +# | _ | f | x | l | _ | where f < l, can read f:l-1, then set f = l, can write l:end, 1:f-1, then set l = f, nb = len +# | l | _ | _ | f | x | where l < f, can read f:end, 1:l-1, can write l:f-1, then set l = f +# |f/l| x | x | x | x | full l == f, nb = len, can read f:l-1, can't write +# | x | x |f/l| x | x | full l == f, nb = len, can read f:end, 1:l-1, can't write +function Base.readavailable(f::FIFOBuffer) + # no data to read + if f.nb == 0 + if current_task() == f.task || f.eof + return UInt8[] + else # async + still open: block till there's data to read + wait(f.cond) + f.nb == 0 && return UInt8[] + end + end + if f.f < f.l + @inbounds bytes = f.buffer[f.f:f.l-1] + else + # we've wrapped around + @inbounds bytes = f.buffer[f.f:end] + @inbounds append!(bytes, view(f.buffer, 1:f.l-1)) + end + f.f = f.l + f.nb = 0 + notify(f.cond) + return bytes +end + +# read at most `nb` bytes +function Base.read(f::FIFOBuffer, nb::Int) + # no data to read + if f.nb == 0 + if current_task() == f.task || f.eof + return UInt8[] + else # async: block till there's data to read + wait(f.cond) + f.nb == 0 && return UInt8[] + end + end + if f.f < f.l + l = (f.l - f.f) <= nb ? (f.l - 1) : (f.f + nb - 1) + @inbounds bytes = f.buffer[f.f:l] + f.f = mod1(l + 1, f.max) + else + # we've wrapped around + if nb <= (f.len - f.f + 1) + # we can read all we need between f.f and f.len + @inbounds bytes = f.buffer[f.f:(f.f + nb - 1)] + f.f = mod1(f.f + nb, f.max) + else + @inbounds bytes = f.buffer[f.f:f.len] + l = min(f.l - 1, nb - length(bytes)) + @inbounds append!(bytes, view(f.buffer, 1:l)) + f.f = mod1(l + 1, f.max) + end + end + f.nb -= length(bytes) + notify(f.cond) + return bytes +end -#= function Base.read(f::FIFOBuffer, ::Type{Tuple{UInt8,Bool}}) - if nb_available(f.io) == 0 - return 0x00, false + # no data to read + if f.nb == 0 + if current_task() == f.task || f.eof + return 0x00, false + else # async: block till there's data to read + f.eof && return 0x00, false + wait(f.cond) + f.nb == 0 && return 0x00, false + end end - return read(f.io, UInt8), true + # data to read + @inbounds b = f.buffer[f.f] + f.f = mod1(f.f + 1, f.max) + f.nb -= 1 + notify(f.cond) + return b, true end -=# -Base.write(f::FIFOBuffer{BufferStream}, x::UInt8) = write(f.io, [x]) +function Base.read(f::FIFOBuffer, ::Type{UInt8}) + byte, valid = read(f, Tuple{UInt8,Bool}) + valid || throw(EOFError()) + return byte +end -Base.wait_readnb(f::FIFOBuffer{BufferStream}, nb::Int) = Base.wait_readnb(f.io, nb) +function Base.String(f::FIFOBuffer) + f.nb == 0 && return "" + if f.f < f.l + return String(f.buffer[f.f:f.l-1]) + else + bytes = f.buffer[f.f:end] + append!(bytes, view(f.buffer, 1:f.l-1)) + return String(bytes) + end +end + +function Base.write(f::FIFOBuffer, b::UInt8) + # buffer full, check if we can grow it + if f.nb == f.len || f.len < f.l + if f.len < f.max + push!(f.buffer, 0x00) + f.len += 1 + else + if current_task() == f.task || f.eof + return 0 + else # async: block until there's room to write + wait(f.cond) + f.nb == f.len && return 0 + end + end + end + # write our byte + @inbounds f.buffer[f.l] = b + f.l = mod1(f.l + 1, f.max) + f.nb += 1 + notify(f.cond) + return 1 +end + +function Base.write(f::FIFOBuffer, bytes::Vector{UInt8}, i, j) + len = j - i + 1 + if f.nb == f.len || f.len < f.l + # buffer full, check if we can grow it + if f.len < f.max + append!(f.buffer, zeros(UInt8, min(len, f.max - f.len))) + f.len = length(f.buffer) + else + if current_task() == f.task || f.eof + return 0 + else # async: block until there's room to write + wait(f.cond) + f.nb == f.len && return 0 + end + end + end + if f.f <= f.l + # non-wraparound + avail = f.len - f.l + 1 + if len > avail + # need to wrap around, and check if there's enough room to write full bytes + # write `avail` # of bytes to end of buffer + unsafe_copy!(f.buffer, f.l, bytes, i, avail) + if len - avail < f.f + # there's enough room to write the rest of bytes + unsafe_copy!(f.buffer, 1, bytes, avail + 1, len - avail) + f.l = len - avail + 1 + else + # not able to write all of bytes + unsafe_copy!(f.buffer, 1, bytes, avail + 1, f.f - 1) + f.l = f.f + f.nb += avail + f.f - 1 + notify(f.cond) + return avail + f.f - 1 + end + else + # there's enough room to write bytes through the end of the buffer + unsafe_copy!(f.buffer, f.l, bytes, i, len) + f.l = mod1(f.l + len, f.max) + end + else + # already in wrap-around state + if len > mod1(f.f - f.l, f.max) + # not able to write all of bytes + nb = f.f - f.l + unsafe_copy!(f.buffer, f.l, bytes, i, nb) + f.l = f.f + f.nb += nb + notify(f.cond) + return nb + else + # there's enough room to write bytes + unsafe_copy!(f.buffer, f.l, bytes, i, len) + f.l = mod1(f.l + len, f.max) + end + end + f.nb += len + notify(f.cond) + return len +end +Base.write(f::FIFOBuffer, bytes::Vector{UInt8}) = write(f, bytes, 1, length(bytes)) +Base.write(f::FIFOBuffer, str::String) = write(f, Vector{UInt8}(str)) -end # module +end # module \ No newline at end of file diff --git a/src/handlers.jl b/src/handlers.jl index eb380516b..e97c8f470 100644 --- a/src/handlers.jl +++ b/src/handlers.jl @@ -103,11 +103,11 @@ function register!(r::Router, method::String, url, handler) m = isempty(method) ? Any : typeof(METHODS[method]) # get scheme, host, split path into strings & vals uri = url isa String ? HTTP.URI(url) : url - s = HTTP.scheme(uri) - sch = HTTP.hasscheme(uri) ? typeof(get!(SCHEMES, s, val(s))) : Any - h = HTTP.hashostname(uri) ? Val{Symbol(HTTP.hostname(uri))} : Any + s = uri.scheme + sch = !isempty(s) ? typeof(get!(SCHEMES, s, val(s))) : Any + h = !isempty(uri.host) ? Val{Symbol(uri.host)} : Any hand = handler isa Function ? HandleFunction(handler) : handler - register!(r, m, sch, h, HTTP.path(uri), hand) + register!(r, m, sch, h, uri.path, hand) end function splitsegments(r::Router, h::Handler, segments) @@ -140,9 +140,9 @@ function handle(r::Router, req, resp) m = val(Symbol(HTTP.method(req))) uri = HTTP.uri(req) # get scheme, host, split path into strings and get Vals - s = get(SCHEMES, HTTP.scheme(uri), EMPTYVAL) - h = val(Symbol(HTTP.hostname(uri))) - p = HTTP.path(uri) + s = get(SCHEMES, uri.scheme, EMPTYVAL) + h = val(Symbol(uri.host)) + p = uri.path segments = split(p, '/'; keep=false) # dispatch to the most specific handler, given the path vals = (get(r.segments, s, EMPTYVAL) for s in segments) diff --git a/src/multipart.jl b/src/multipart.jl index ca8cfd585..98d4a61ef 100644 --- a/src/multipart.jl +++ b/src/multipart.jl @@ -12,12 +12,12 @@ mutable struct Form <: IO data::Vector{IO} index::Int boundary::String - mark::Int end Form(f::Form) = f Base.eof(f::Form) = f.index > length(f.data) Base.isopen(f::Form) = false +Base.close(f::Form) = nothing Base.length(f::Form) = sum(x->isa(x, IOStream) ? filesize(x) - position(x) : nb_available(x), f.data) function Base.position(f::Form) index = f.index @@ -69,13 +69,13 @@ function Form(d::Dict) io = IOBuffer() else write(io, "$CRLF$CRLF") - write(io, escape(v)) + write(io, escapeuri(v)) end i == len && write(io, "$CRLF--" * boundary * "--" * "$CRLF") end seekstart(io) push!(data, io) - return Form(data, 1, boundary, 0) + return Form(data, 1, boundary) end function writemultipartheader(io::IOBuffer, i::IOStream) diff --git a/src/server.jl b/src/server.jl index 998b7f2ab..9ad277875 100644 --- a/src/server.jl +++ b/src/server.jl @@ -110,7 +110,7 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, end length(buffer) > 0 || break starttime[] = time() # reset the timeout while still receiving bytes - err = @HTTP.catch HTTP.ParsingError HTTP.parse!(parser, buffer) + err = HTTP.@catch HTTP.ParsingError HTTP.parse!(parser, buffer) startedprocessingrequest = true if err != nothing # error in parsing the http request diff --git a/src/types.jl b/src/types.jl index 1ee76103e..a3c5060fe 100644 --- a/src/types.jl +++ b/src/types.jl @@ -10,7 +10,7 @@ sockettype(::Type{https}) = TLS.SSLContext schemetype(::Type{TCPSocket}) = http schemetype(::Type{TLS.SSLContext}) = https -const Headers = Vector{Pair{String, String}} +const Headers = Dict{String, String} const Option{T} = Union{T, Void} not(::Void) = true @@ -59,8 +59,6 @@ mutable struct RequestOptions new(ch, gzip, ct, rt, tls, mr, ar, fh, tr, mc, sr, i, h, lb) end -# FIXME defaults are over in "const DEFAULT_OPTIONS" in client.jl !! - const RequestOptionsFieldTypes = Dict(:chunksize => Int, :gzip => Bool, :connecttimeout => Float64, @@ -93,341 +91,3 @@ function update!(opts1::RequestOptions, opts2::RequestOptions) end return opts1 end - -# Request -""" - Request() - Request(method, uri, headers, body; options=RequestOptions()) - Request(; method=HTTP.GET, uri=HTTP.URI(""), major=1, minor=1, headers=HTTP.Headers(), body="") - -A type representing an http request. `method` can be provided as a string or `HTTP.GET` type enum. -`uri` can be provided as an actual `HTTP.URI` or string. `headers` should be provided as a `Dict`. -`body` may be provided as string, byte vector, IO, or `HTTP.FIFOBuffer`. -`options` should be a `RequestOptions` type, see `?HTTP.RequestOptions` for details. - -Accessor methods include: - * `HTTP.method`: method for a request - * `HTTP.major`: major http version for a request - * `HTTP.minor`: minor http version for a request - * `HTTP.uri`: uri for a request - * `HTTP.headers`: headers for a request - * `HTTP.body`: body for a request as a `HTTP.FIFOBuffer` - -Two convenience methods are provided for accessing a request body: - * `take!(r)`: consume the request body, returning it as a `Vector{UInt8}` - * `String(r)`: consume the request body, returning it as a `String` -""" -mutable struct Request - method::HTTP.Method - major::Int16 - minor::Int16 - uri::URI - headers::Headers # includes cookies - body::Union{FIFOBuffer, Form} - #referrer::Ref{Response} -end - -# accessors -method(r::Request) = r.method -major(r::Request) = r.major -minor(r::Request) = r.minor -uri(r::Request) = r.uri -headers(r::Request) = Dict(r.headers) -body(r::Request) = r.body - -function referrercount(r::Request) - if !isassigned(r.referrer) - return 0 - elseif Base.isnull(request(r.referrer[])) - return 1 - else - return 1 + referrercount(Base.get(request(r.referrer[]))) - end -end - -defaultheaders(::Type{Request}) = [ - "User-Agent" => "HTTP.jl/0.0.0", - "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json; charset=utf-8" -] - -function Request(m::HTTP.Method, uri::URI, userheaders, b; - options::RequestOptions=RequestOptions(), - verbose::Bool=false, - logger::Option{IO}=STDOUT) - if m != CONNECT - headers = defaultheaders(Request) - push!(headers, "Host" => host(uri)) - else - headers = Headers() - end - if !isempty(userinfo(uri)) && !any(x->x[1] == "Authorization", headers) - push!(headers, "Authorization" => "Basic $(base64encode(userinfo(uri)))") - @log "adding basic authentication header" - end - if isa(b, Dict) || isa(b, Form) - # form data - body = Form(b) - push!(headers, "Content-Type" => "multipart/form-data; boundary=$(body.boundary)") - else - body = FIFOBuffer(b) - end - if iscompressed(body) && length(body) > get(options, :chunksize, 0) - options.chunksize = length(body) + 1 - end - if !any(x->x[1] == "Content-Type", headers) && length(body) > 0 && !isa(body, Form) - sn = sniff(body) - push!(headers, "Content-Type" => sn) - @log "setting Content-Type header to: $sn" - end - - userkeys = [x[1] for x in userheaders] - filter!(x->!(x[1] in userkeys), headers) - append!(headers, userheaders) - - return Request(m, Int16(1), Int16(1), uri, headers, body) -end - -Request(method, uri, h=Dict(), body=""; options::RequestOptions=RequestOptions(), logger::Option{IO}=STDOUT, verbose::Bool=false) = - Request(convert(HTTP.Method, method), - isa(uri, String) ? URI(uri; isconnect=(method == "CONNECT" || method == CONNECT)) : uri, - h, body; options=options, logger=logger, verbose=verbose) - -Request(; method::Method=GET, major::Integer=Int16(1), minor::Integer=Int16(1), uri=URI(""), headers=Headers(), body=FIFOBuffer()) = - Request(method, major, minor, uri, headers, body) - -==(a::Request,b::Request) = (a.method == b.method) && - (a.major == b.major) && - (a.minor == b.minor) && - (a.uri == b.uri) && - (a.headers == b.headers) && - (a.body == b.body) - -Base.showcompact(io::IO, r::Request) = print(io, "Request(\"", resource(r.uri), "\", ", - length(r.headers), " headers, ", - length(r.body), " bytes in body)") - -""" - Response(status::Integer) - Response(status::Integer, body::String) - Response(status::Integer, headers, body) - Response(; status=200, cookies=HTTP.Cookie[], headers=HTTP.Headers(), body="") - -A type representing an http response. `status` represents the http status code for the response. -`headers` should be provided as a `Dict`. `body` can be provided as a string, byte vector, IO, or `HTTP.FIFOBuffer`. - -Accessor methods include: - * `HTTP.status`: status for a response - * `HTTP.statustext`: statustext for a response - * `HTTP.major`: major http version for a response - * `HTTP.minor`: minor http version for a response - * `HTTP.cookies`: cookies for a response, returned as a `Vector{HTTP.Cookie}` - * `HTTP.headers`: headers for a response - * `HTTP.request`: the `HTTP.Request` that resulted in this response - * `HTTP.referrer`: original response if redirects were followed from an original request - * `HTTP.body`: body for a response as a `HTTP.FIFOBuffer` - -Two convenience methods are provided for accessing a response body: - * `take!(r)`: consume the response body, returning it as a `Vector{UInt8}` - * `String(r)`: consume the response body, returning it as a `String` -""" -mutable struct Response - status::Int32 - major::Int16 - minor::Int16 - cookies::Vector{Cookie} - headers::Headers - body::FIFOBuffer - request::Nullable{Request} -end - -# accessors -status(r::Response) = r.status -major(r::Response) = r.major -minor(r::Response) = r.minor -cookies(r::Response) = r.cookies -headers(r::Response) = Dict(r.headers) -request(r::Response) = r.request -statustext(r::Response) = Base.get(STATUS_CODES, r.status, "Unknown Code") -body(r::Union{Request, Response}) = r.body -Base.take!(r::Union{Request, Response}) = readavailable(body(r)) -function Base.String(r::Union{Request, Response}) - if contains(Base.get(headers(r), "Content-Type", ""), "ISO-8859-1") - return iso8859_1_to_utf8(String(body(r))) - else - return String(body(r)) - end -end - -Response(; status::Int=200, - cookies::Vector{Cookie}=Cookie[], - headers::Headers=Headers(), - body::FIFOBuffer=FIFOBuffer(), - request::Nullable{Request}=Nullable{Request}()) = - Response(status, Int16(1), Int16(1), cookies, headers, body, request) - -Response(r::Request) = Response(; body=FIFOBuffer(), request=Nullable(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)) -Response(s::Integer, h::Headers, body) = Response(; status=s, headers=h, body=FIFOBuffer(body)) - -defaultheaders(::Type{Response}) = [ - "Server" => "Julia/$VERSION", - "Content-Type" => "text/html; charset=utf-8", - "Content-Language" => "en", - "Date" => Dates.format(Dates.now(Dates.UTC), Dates.RFC1123Format) -] - -==(a::Response,b::Response) = (a.status == b.status) && - (a.major == b.major) && - (a.minor == b.minor) && - (a.headers == b.headers) && - (a.cookies == b.cookies) && - (a.body == b.body) - -function Base.showcompact(io::IO, r::Response) - print(io, "Response(", r.status, " ", Base.get(STATUS_CODES, r.status, "Unknown Code"), ", ", - length(r.headers)," headers, ", - length(r.body)," bytes in body)") -end - -header(r, k::String, default::String="") = getkey(r.headers, k, k => default)[2] -setheader(r, v::Pair{String,String}) = setkey(r.headers, v) - -function appendheader(r, h::Pair{String,String}) - c = r.headers - k,v = h - if k == "" - c[end] = c[end][1] => string(c[end][2], v) - elseif k != "Set-Cookie" && length(c) > 0 && k == c[end][1] - c[end] = c[end][1] => string(c[end][2], ", ", v) - else - push!(r.headers, h) - end -end - -## Request & Response writing -# start lines -function startline(io::IO, r::Request) - res = resource(uri(r); isconnect=r.method == CONNECT) - res = ifelse(res == "", "/", res) - write(io, "$(r.method) $res HTTP/$(r.major).$(r.minor)$CRLF") -end - -function startline(io::IO, r::Response) - write(io, "HTTP/$(r.major).$(r.minor) $(r.status) $(statustext(r))$CRLF") -end - -# headers -function headers(io::IO, r::Union{Request, Response}) - for (k, v) in r.headers - write(io, "$k: $v$CRLF") - end - # write(io, CRLF); we let the body write this in case of chunked transfer -end - -# body -# https://tools.ietf.org/html/rfc7230#section-3.3 -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)) - method(req) in (HEAD, CONNECT) && return false - end - return true -end -hasmessagebody(r::Request) = length(r.body) > 0 && !(r.method in (GET, HEAD, CONNECT)) - - -function bodylength(r::Union{Request, Response}) - l = header(r, "Content-Length") - return l == "" ? length(r.body) : Base.parse(Int, l) -end - - -function body(io::IO, r::Union{Request, Response}, opts) - if !hasmessagebody(r) - write(io, CRLF) - return - end - chksz = get(opts, :chunksize, 0) - -# mark(r.body) - chunked = false - bytes = UInt8[] - blength = bodylength(r) - while length(bytes) < blength && !eof(r.body) - bytes = chksz == 0 ? readavailable(r.body) : read(r.body, chksz) - (length(bytes) == blength || eof(r.body)) && !chunked && break - if !chunked - write(io, "Transfer-Encoding: chunked$CRLF$CRLF") - end - chunked = true - chunk = length(bytes) - chunk == 0 && break - write(io, "$(hex(chunk))$CRLF") - write(io, bytes, CRLF) - end - if chunked - write(io, "$(hex(0))$CRLF$CRLF") - else - write(io, "Content-Length: $(dec(length(bytes)))$CRLF$CRLF") - write(io, bytes) - end -# reset(r.body) - return -end - -function Base.write(io::IO, r::Union{Request, Response}, opts=RequestOptions()) - startline(io, r) - headers(io, r) - body(io, r, opts) -end - -function Base.string(r::Union{Request, Response}) - io = IOBuffer() - write(io, r) - String(take!(io)) -end - -function Base.show(io::IO, r::Union{Request,Response}; opts=RequestOptions()) - println(io, typeof(r), ":") - println(io, "\"\"\"") - startline(io, r) - headers(io, r) - lb = opts.logbody - if lb === nothing || lb - #buf = IOBuffer() -# if isopen(r.body) -# println(io, "\n[open HTTP.FIFOBuffer with $(length(r.body)) bytes to read]") -# else - #FIXME - #body(buf, r, opts) - #b = take!(buf) - write(io, CRLF) - b = FIFOBuffers.peekbytes(r.body) - if length(b) > 2 - contenttype = sniff(b) - if contenttype in DISPLAYABLE_TYPES - if length(b) > 750 - println(io, "\n[$(typeof(r)) body of $(length(b)) bytes]") - println(io, String(b)[1:750]) - println(io, "⋮") - else - print(io, String(b)) - end - else - contenttype = Base.get(r.headers, "Content-Type", contenttype) - encoding = Base.get(r.headers, "Content-Encoding", "") - encodingtxt = encoding == "" ? "" : " with '$encoding' encoding" - println(io, "\n[$(length(b)) bytes of '$contenttype' data$encodingtxt]") - end - else - print(io, String(b)) - end - else - println(i, "\n[request body logging disabled]\n") - end - print(io, "\"\"\"") -end diff --git a/src/uri.jl b/src/uri.jl index ee3bd9206..a68310d75 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -235,15 +235,21 @@ end absuri(u, context) = absuri(URI(u), URI(context)) -function absuri(u::URI, context::URI) +function absuri(uri::URI, context::URI) + if !isempty(uri.host) + return uri + end + + @assert !isempty(context.scheme) @assert !isempty(context.host) + @assert isempty(uri.port) - return URI(scheme = isempty(u.scheme) ? context.scheme : u.scheme, - host = isempty(u.host) ? context.host : u.host, - port = isempty(u.port) ? context.port : u.port, - path = isempty(u.path) ? context.path : u.path, - query = isempty(u.query) ? context.query : u.query) + return URI(scheme = context.scheme, + host = context.host, + port = context.port, + path = uri.path, + query = uri.query) end end # module diff --git a/test/client.jl b/test/client.jl index 2a6ab2d20..62626366e 100644 --- a/test/client.jl +++ b/test/client.jl @@ -1,5 +1,7 @@ @testset "HTTP.Client" begin +using JSON + @testset "HTTP.Connection" begin conn = HTTP.Connection(IOBuffer()) @test conn.state == HTTP.Busy @@ -19,7 +21,7 @@ for sch in ("http", "https") println("running $sch client tests...") println("simple GET, HEAD, POST, DELETE, etc.") - @test HTTP.status(HTTP.get("$sch://httpbin.org/ip"; logbody=false)) == 200 + @test HTTP.status(HTTP.get("$sch://httpbin.org/ip")) == 200 @test HTTP.status(HTTP.head("$sch://httpbin.org/ip")) == 200 @test HTTP.status(HTTP.options("$sch://httpbin.org/ip")) == 200 @test HTTP.status(HTTP.post("$sch://httpbin.org/ip"; statusraise=false)) == 405 @@ -40,7 +42,7 @@ for sch in ("http", "https") @test (haskey(h, "Hey") ? h["Hey"] == "dude" : h["hey"] == "dude") println("cookie requests") - empty!(HTTP.DEFAULT_CLIENT.cookies) + empty!(HTTP.CookieRequest.default_cookiejar) r = HTTP.get("$sch://httpbin.org/cookies") body = String(take!(r)) @test body == "{\n \"cookies\": {}\n}\n" @@ -61,17 +63,15 @@ for sch in ("http", "https") @test HTTP.status(r) == 200 r = HTTP.get("$sch://httpbin.org/stream/100") @test HTTP.status(r) == 200 - totallen = length(HTTP.body(r)) # number of bytes to expect bytes = take!(r) + a = [JSON.parse(l) for l in split(chomp(String(bytes)), "\n")] + totallen = length(bytes) # number of bytes to expect begin r = HTTP.get("$sch://httpbin.org/stream/100"; stream=true) @test HTTP.status(r) == 200 - len = length(HTTP.body(r)) - HTTP.@timeout 15.0 begin - while !eof(HTTP.body(r)) - b = take!(r) - end - end throw(error("timed out")) + + b = [JSON.parse(l) for l in eachline(r.body.io)] + @test a == b end # body posting: Vector{UInt8}, String, IOStream, IOBuffer, FIFOBuffer @@ -83,10 +83,10 @@ for sch in ("http", "https") tmp = tempname() open(f->write(f, "hey"), tmp, "w") io = open(tmp) - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io)) == 200 + @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, enablechunked=false)) == 200 close(io); rm(tmp) f = HTTP.FIFOBuffer("hey") - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f)) == 200 + @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f, enablechunked=false)) == 200 # chunksize # @@ -102,10 +102,10 @@ for sch in ("http", "https") tmp = tempname() open(f->write(f, "hey"), tmp, "w") io = open(tmp) - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 + @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 close(io); rm(tmp) f = HTTP.FIFOBuffer("hey") - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f, chunksize=2)) == 200 + @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f, chunksize=2)) == 200 # multipart println("client multipart body") @@ -113,7 +113,7 @@ for sch in ("http", "https") @test HTTP.status(r) == 200 @test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }") - r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there"), chunksize=1000) + r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there")) @test HTTP.status(r) == 200 @test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }") @@ -129,7 +129,7 @@ for sch in ("http", "https") tmp = tempname() open(f->write(f, "hey"), tmp, "w") io = open(tmp) - r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "iostream"=>io), chunksize=1000) + r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "iostream"=>io)) close(io); rm(tmp) @test HTTP.status(r) == 200 @test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") @@ -157,8 +157,8 @@ for sch in ("http", "https") begin f = HTTP.FIFOBuffer() write(f, "hey") - t = @async HTTP.post("$sch://httpbin.org/post"; body=f) - Base.wait_readnb(f, 1) # wait for the async call to write it's first data + t = @async HTTP.post("$sch://httpbin.org/post"; body=f, enablechunked=false) + wait(f) # wait for the async call to write it's first data write(f, " there ") # as we write to f, it triggers another chunk to be sent in our async request write(f, "sailor") close(f) # setting eof on f causes the async request to send a final chunk and return the response @@ -169,16 +169,16 @@ for sch in ("http", "https") println("client redirect following") r = HTTP.get("$sch://httpbin.org/redirect/1") @test HTTP.status(r) == 200 - @test length(HTTP.history(r)) == 1 - @test_throws HTTP.RedirectError HTTP.get("$sch://httpbin.org/redirect/6") + #@test length(HTTP.history(r)) == 1 + @test_throws HTTP.StatusError HTTP.get("$sch://httpbin.org/redirect/6") @test HTTP.status(HTTP.get("$sch://httpbin.org/relative-redirect/1")) == 200 @test HTTP.status(HTTP.get("$sch://httpbin.org/absolute-redirect/1")) == 200 @test HTTP.status(HTTP.get("$sch://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com")) == 200 @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body="√")) == 200 println("client basic auth") - @test HTTP.status(HTTP.get("$sch://user:pwd@httpbin.org/basic-auth/user/pwd")) == 200 - @test HTTP.status(HTTP.get("$sch://user:pwd@httpbin.org/hidden-basic-auth/user/pwd")) == 200 + @test HTTP.status(HTTP.get("$sch://user:pwd@httpbin.org/basic-auth/user/pwd"; basicauthorization=true)) == 200 + @test HTTP.status(HTTP.get("$sch://user:pwd@httpbin.org/hidden-basic-auth/user/pwd"; basicauthorization=true)) == 200 # custom client & other high-level entries println("high-level client request methods") @@ -206,19 +206,23 @@ for sch in ("http", "https") r = HTTP.request("GET", uri) @test HTTP.status(r) == 200 +#= FIXME 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 length(take!(r)) > 0 +=# +#= for c in HTTP.DEFAULT_CLIENT.httppool["httpbin.org"] HTTP.dead!(c) end +=# r = HTTP.get(cli, "$sch://httpbin.org/ip") - @test isempty(HTTP.cookies(r)) - @test isempty(HTTP.history(r)) +# @test isempty(HTTP.cookies(r)) +# @test isempty(HTTP.history(r)) r = HTTP.get("$sch://httpbin.org/image/png") @test HTTP.status(r) == 200 diff --git a/test/fifobuffer.jl b/test/fifobuffer.jl index 168ccc177..3f53d0260 100644 --- a/test/fifobuffer.jl +++ b/test/fifobuffer.jl @@ -1,23 +1,23 @@ @testset "FIFOBuffer" begin - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(0) @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(1) @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) @test write(f, 0x01) == 1 - @test write(f, 0x02) == 1 + @test write(f, 0x02) == 0 @test read(f, Tuple{UInt8,Bool}) == (0x01, true) - @test read(f, Tuple{UInt8,Bool}) == (0x02, true) + @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) - @test write(f, UInt8[0x01, 0x02]) == 2 - @test all(readavailable(f) .== UInt8[0x01, 0x02]) + @test write(f, UInt8[0x01, 0x02]) == 1 + @test all(readavailable(f) .== UInt8[0x01]) - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(5) @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) @@ -57,8 +57,8 @@ write(f, 0x03) write(f, 0x04) write(f, 0x05) - write(f, 0x06) - @test all(readavailable(f) .== UInt8[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) + write(f, 0x06) == 0 + @test all(readavailable(f) .== UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) @@ -97,8 +97,8 @@ @test isempty(readavailable(f)) # overflow - @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) == 6 - @test all(readavailable(f) .== UInt8[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) + write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05, 0x06]) == 5 + @test all(readavailable(f) .== UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) @test read(f, Tuple{UInt8,Bool}) == (0x00, false) @test isempty(readavailable(f)) @@ -125,75 +125,73 @@ end tsk2 = @async begin for i = 1:N - @test all(read(f, 5) .== UInt8[0x4a, 0x61, 0x63, 0x6f, 0x62]) + @test all(readavailable(f) .== UInt8[0x4a, 0x61, 0x63, 0x6f, 0x62]) end end end # buffer growing - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(10) @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 @test write(f, UInt8[0x06, 0x07, 0x08, 0x09, 0x0a]) == 5 @test all(readavailable(f) .== 0x01:0x0a) # read - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(5) @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 @test all(read(f, 5) .== 0x01:0x05) @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 - @test all(read(f, 5) .== 0x01:0x05) + @test all(read(f, 6) .== 0x01:0x05) @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 @test isempty(read(f, 0)) @test all(read(f, 2) .== 0x01:0x02) @test write(f, 0x01) == 1 - @test all(read(f, 4) .== UInt8[0x03, 0x04, 0x05, 0x01]) + @test all(read(f, 5) .== UInt8[0x03, 0x04, 0x05, 0x01]) @test write(f, UInt8[0x01, 0x02, 0x03, 0x04, 0x05]) == 5 @test all(read(f, 2) .== 0x01:0x02) r = read(f, 3) @test all(r .== 0x03:0x05) - f2 = HTTP.FIFOBuffer(f) @test f == f2 - f = HTTP.FIFOBuffer() - @test isempty(read(f, 0)) + f = HTTP.FIFOBuffer(5) + @test isempty(read(f, 1)) t = @async read(f, 1) write(f, 0x01) @test wait(t) == [0x01] @test write(f, [0x01, 0x02, 0x03, 0x04, 0x05]) == 5 - @test write(f, [0x01, 0x02]) == 2 + @test write(f, [0x01, 0x02]) == 0 - @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x02] + @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05] # ensure we're in a wrap-around state - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(5) @test write(f, [0x01, 0x02, 0x03]) == 3 @test readavailable(f) == [0x01, 0x02, 0x03] @test write(f, [0x01, 0x02, 0x03, 0x04]) == 4 -# @test f.f > f.l + @test f.f > f.l @test write(f, [0x05]) == 1 @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05] @test write(f, [0x01, 0x02, 0x03, 0x04]) == 4 - @test write(f, [0x05, 0x06]) == 2 - @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05, 0x06] + @test write(f, [0x05, 0x06]) == 1 + @test readavailable(f) == [0x01, 0x02, 0x03, 0x04, 0x05] # ensure that `read(..., ::Type{UInt8})` returns a `UInt8` # https://github.com/JuliaWeb/HTTP.jl/issues/41 - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(5) b = Array{UInt8}(3) @test write(f, [0x01, 0x02, 0x03, 0x04]) == 4 - close(f) @test readbytes!(f, b) == 3 @test b == [0x01, 0x02, 0x03] @test read(f, UInt8) == 0x04 @test_throws EOFError read(f, UInt8) # ensure we return eof == false if there are still bytes to be read - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(5) write(f, [0x01, 0x02, 0x03, 0x04]) close(f) @async begin @@ -202,11 +200,9 @@ # Issue #45 # Ensure that we don't encounter an EOF when reading before data is written - f = HTTP.FIFOBuffer() + f = HTTP.FIFOBuffer(5) bytes = [0x01, 0x02, 0x03, 0x04] - @async begin - @test !eof(f) - end + @test !eof(f) @sync begin @async begin bytes_read = UInt8[] diff --git a/test/messages.jl b/test/messages.jl index 2c4ce754a..266ef888e 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -1,5 +1,10 @@ +module MessagesTest + +using Base.Test + using HTTP.Messages import HTTP.Messages.appendheader +import HTTP.URI using HTTP.CookieRequest using HTTP.StatusError @@ -36,8 +41,8 @@ using JSON @test filter(x->first(x) == "Set-Cookie", req.headers) == ["Set-Cookie" => "A", "Set-Cookie" => "B"] - @test HTTP.Messages.httpversion(req) == "HTTP/1.1" - @test HTTP.Messages.httpversion(res) == "HTTP/1.1" + @test Messages.httpversion(req) == "HTTP/1.1" + @test Messages.httpversion(res) == "HTTP/1.1" raw = String(req) #@show raw @@ -126,7 +131,7 @@ using JSON function async_get(url) io = BufferStream() - q = HTTP.URI(url).query + q = URI(url).query log("GET $q") r = request("GET", url, response_stream=io) @async begin @@ -182,3 +187,5 @@ using JSON end end end + +end # module MessagesTest diff --git a/test/parser.jl b/test/parser.jl index 13f7f915d..0756b8cb7 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1,5 +1,10 @@ -using HTTP.Messages +module ParserTest + +using Base.Test + +import ..HTTP +using HTTP.Messages using HTTP.Parsers const DEFAULT_PARSER = Parser() @@ -1423,7 +1428,7 @@ const responses = Message[ @test uri.port in (req.port, "80", "443") @test string(uri) == req.request_url @test length(r.headers) == req.num_headers - @test Dict(HTTP.SendRequest.canonicalizeheaders(r.headers)) == Dict(req.headers) + @test Dict(HTTP.CookieRequest.canonicalizeheaders(r.headers)) == Dict(req.headers) @test String(take!(r.body)) == req.body # FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == req.should_keep_alive @@ -1610,7 +1615,7 @@ const responses = Message[ @test r.status == resp.status_code @test HTTP.Messages.statustext(r) == resp.response_status @test length(r.headers) == resp.num_headers - @test Dict(HTTP.SendRequest.canonicalizeheaders(r.headers)) == Dict(resp.headers) + @test Dict(HTTP.CookieRequest.canonicalizeheaders(r.headers)) == Dict(resp.headers) @test String(take!(r.body)) == resp.body # FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == resp.should_keep_alive catch e @@ -1848,3 +1853,5 @@ const responses = Message[ @test String(take!(r.body)) == "" end end # @testset HTTP.parse + +end # module ParserTest diff --git a/test/runtests.jl b/test/runtests.jl index 843c2da9e..fbc1817e7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -9,15 +9,15 @@ end @testset "HTTP" begin include("utils.jl"); - #include("fifobuffer.jl"); - #include("sniff.jl"); + include("fifobuffer.jl"); + include("sniff.jl"); include("uri.jl"); - #include("cookies.jl"); + include("cookies.jl"); include("parser.jl"); include("body.jl"); include("messages.jl"); - #include("types.jl"); - #include("handlers.jl") - #include("client.jl"); - #include("server.jl") +# include("types.jl"); +# include("handlers.jl") + include("client.jl"); +# include("server.jl") end; From 1a2b4aeef86c1d056b2a5952a814e91ca6555836 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 12 Dec 2017 09:42:04 +1100 Subject: [PATCH 039/182] improve show(io::IO, c::Connection) --- src/Connections.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Connections.jl b/src/Connections.jl index 7a2879f85..5722345d3 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -172,7 +172,9 @@ end function Base.show(io::IO, c::Connection) - print(io, c.host, ":", c.port, ":", Int(localport(c)), ", ", + print(io, c.host, ":", + c.port != "" ? c.port : Int(peerport(c)), ":", + Int(localport(c)), ", ", typeof(c.io), ", ", tcpstatus(c), ", ", length(c.excess), "-byte excess, reads/writes: ", c.writecount, "/", c.readcount) @@ -186,6 +188,11 @@ localport(c::Connection) = !isopen(c.io) ? 0 : getsockname(tcpsocket(c))[2] : Base._sockname(tcpsocket(c), true)[2] +peerport(c::Connection) = !isopen(c.io) ? 0 : + VERSION > v"0.7.0-DEV" ? + getpeername(tcpsocket(c))[2] : + Base._sockname(tcpsocket(c), false)[2] + tcpstatus(c::Connection) = Base.uv_status_string(tcpsocket(c)) From 6167fa69c473cf7fa059ad225e95fd1420d5d5a2 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 12 Dec 2017 09:47:16 +1100 Subject: [PATCH 040/182] Parsers.jl cleanup Split output fields from struct Parser into struct Message. Docstrings and cosmetics. Pass Parsers.Message to p.onheaderscomplete callback. Add const NOMETHOD. Use @passert for inner parser assertions (see enable_passert) Rename @strictcheck -> @errorifstrict Move http_should_keep_alive to server.jl Use HPE_INVALID_INTERNAL_STATE instead of error("unhandled state") Fix up end of loop assertion: p_state == s_message_done || p == len --- src/Messages.jl | 16 +- src/Parsers.jl | 632 ++++++++++++++++++++++++------------------ src/consts.jl | 1 + src/server.jl | 35 +++ test/.body.jl.swp | Bin 0 -> 20480 bytes test/.client.jl.swp | Bin 0 -> 28672 bytes test/.cookies.jl.swp | Bin 0 -> 16384 bytes test/.handlers.jl.swp | Bin 0 -> 12288 bytes test/.messages.jl.swp | Bin 0 -> 28672 bytes test/.parser.jl.swp | Bin 0 -> 106496 bytes test/.runtests.jl.swp | Bin 0 -> 12288 bytes test/.server.jl.swp | Bin 0 -> 12288 bytes test/.types.jl.swp | Bin 0 -> 12288 bytes test/.uri.jl.swp | Bin 0 -> 36864 bytes test/.utils.jl.swp | Bin 0 -> 12288 bytes test/client.jl | 14 - test/parser.jl | 2 +- 17 files changed, 406 insertions(+), 294 deletions(-) create mode 100644 test/.body.jl.swp create mode 100644 test/.client.jl.swp create mode 100644 test/.cookies.jl.swp create mode 100644 test/.handlers.jl.swp create mode 100644 test/.messages.jl.swp create mode 100644 test/.parser.jl.swp create mode 100644 test/.runtests.jl.swp create mode 100644 test/.server.jl.swp create mode 100644 test/.types.jl.swp create mode 100644 test/.uri.jl.swp create mode 100644 test/.utils.jl.swp diff --git a/src/Messages.jl b/src/Messages.jl index af1170b50..0f70d2162 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -272,9 +272,9 @@ end Read the start-line metadata from `Parser` into a `message` struct. """ -function readstartline!(r::Response, p::Parser) - r.version = VersionNumber(p.major, p.minor) - r.status = p.status +function readstartline!(r::Response, m::Parsers.Message) + r.version = VersionNumber(m.major, m.minor) + r.status = m.status if isredirect(r) r.body = Body() end @@ -283,10 +283,10 @@ function readstartline!(r::Response, p::Parser) return end -function readstartline!(r::Request, p::Parser) - r.version = VersionNumber(p.major, p.minor) - r.method = string(p.method) - r.uri = p.url +function readstartline!(r::Request, m::Parsers.Message) + r.version = VersionNumber(m.major, m.minor) + r.method = string(m.method) + r.uri = m.url return end @@ -341,7 +341,7 @@ function Parser(m::Message) p = Parser() p.onbody = x->write(m.body, x) p.onheader = x->appendheader(m, x) - p.onheaderscomplete = ()->readstartline!(m, p) + p.onheaderscomplete = x->readstartline!(m, x) p.isheadresponse = (isa(m, Response) && method(m) in ("HEAD", "CONNECT")) # FIXME CONNECT?? return p diff --git a/src/Parsers.jl b/src/Parsers.jl index 51f345ea9..f3b37adba 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -24,67 +24,151 @@ module Parsers -export Parser, parse!, messagecomplete, headerscomplete, waitingforeof, +export Parser, parse!, + messagecomplete, headerscomplete, waitingforeof, ParsingError, ParsingErrorCode +using ..URIs.parseurlchar + import ..@debug, ..@debugshow, ..DEBUG_LEVEL include("consts.jl") include("parseutils.jl") -using ..URIs.parseurlchar -const PARSING_DEBUG = false -const start_state = s_start_req_or_res -const strict = false +const strict = false # See macro @errifstrict +const enable_passert = false # See macro @passert + + +""" + Message + +HTTP Message metadata. +""" + +mutable struct Message + method::Method + major::Int16 + minor::Int16 + url::String + status::Int32 + upgrade::Bool +end + +Message() = Message(NOMETHOD, 0, 0, "", 0, false) + + +""" + Parser + +HTTP Message Parser. + +The `Parser` must be configured with output processing callbacks: + +- `onheader = f(::Pair{String,String})` is called for each Header Line. + +- Body data is passed to `onbody = f(::SubArray{UInt8,1})`. + If the Message is chunked or if the Message is passed to `parse!` + in multiple fragments, then `obbody` will be called multiple times. + +- `onheaderscomplete = f(::Message)` is called at the end of the Header. +""" mutable struct Parser + + # config + isheadresponse::Bool # Are we parsing a HEAD Response Message? + onheader::Function#(::Pair{String,String} + onbody::Function#(::SubArray{UInt8,1}) + onheaderscomplete::Function#(::Message) + + # state state::UInt8 header_state::UInt8 index::UInt8 flags::UInt8 - isheadresponse::Bool - upgrade::Bool content_length::UInt64 fieldbuffer::IOBuffer valuebuffer::IOBuffer - method::Method - major::Int16 - minor::Int16 - url::String - status::Int32 - onheader::Function - onbody::Function - onheaderscomplete::Function + + # output + message::Message end -Parser() = Parser(start_state, 0x00, 0, 0, false, false, 0, IOBuffer(), IOBuffer(), Method(0), 0, 0, "", 0, x->nothing, x->nothing, ()->nothing) + +""" + Parser() + +Create an unconfigured `Parser`. +""" + +Parser() = Parser(false, x->nothing, x->nothing, ()->nothing, + s_start_req_or_res, 0, 0, 0, 0, + IOBuffer(), IOBuffer(), Message()) + + +""" + reset!(::Parser) + +Revert `Parser` to unconfigured state. +""" function reset!(p::Parser) - p.state = start_state - p.header_state = 0x00 - p.index = 0x00 - p.flags = 0x00 + + # config p.isheadresponse = false - p.upgrade = false - p.content_length = 0x0000000000000000 - truncate(p.fieldbuffer, 0) - truncate(p.valuebuffer, 0) - p.method = Method(0) - p.major = 0 - p.minor = 0 - p.url = "" - p.status = 0 p.onheader = x->nothing p.onbody = x->nothing - p.onheaderscomplete = ()->nothing + p.onheaderscomplete = x->nothing + + # state + p.state = s_start_req_or_res + p.header_state = 0 + p.index = 0 + p.flags = 0 + p.content_length = 0 + truncate(p.fieldbuffer, 0) + truncate(p.valuebuffer, 0) + + # output + p.message.method = NOMETHOD + p.message.major = 0 + p.message.minor = 0 + p.message.url = "" + p.message.status = 0 + p.message.upgrade = false end -isrequest(p::Parser) = p.status == 0 + +""" + headerscomplete(::Parser) + +Has the `Parser` processed the entire Message Header? +""" + headerscomplete(p::Parser) = p.state >= s_headers_done + + +""" + messagecomplete(::Parser) + +Has the `Parser` processed the entire Message? +""" + messagecomplete(p::Parser) = p.state >= s_message_done + + +""" + waitingforeof(::Parser) + +Is the `Parser` waiting for the peer to close the connection +to signal the end of the Message Body? +""" waitingforeof(p::Parser) = p.state == s_body_identity_eof -upgrade(p::Parser) = p.upgrade + + +isrequest(p::Parser) = p.message.status == 0 + struct ParsingError <: Exception code::ParsingErrorCode @@ -100,29 +184,45 @@ function Base.show(io::IO, e::ParsingError) end +macro err(code) + esc(:(throw(ParsingError($code)))) +end + macro errorif(cond, err) esc(:($cond && @err($err))) end -macro err(code) - esc(:(throw(ParsingError($code)))) +macro errorifstrict(cond) + strict ? esc(:(@errorif($cond, HPE_STRICT))) : :() end -macro strictcheck(cond) - esc(:(strict && @errorif($cond, HPE_STRICT))) +macro passert(cond) + enable_passert ? esc(:(@assert($cond))) : :() end -macro shifted(meth, i, char) +macro methodstate(meth, i, char) return esc(:(Int($meth) << Int(16) | Int($i) << Int(8) | Int($char))) end -const ByteView = typeof(view(UInt8[], 1:0)) +""" + parse!(::Parser, bytes) -> count + +Parse `bytes` and update the `Parser`. + +Returns number of bytes consumed. +If `bytes` contains the end of one Message and the start of the next +Message, `parse!` will stop at the end of the first Message. + +Throws `ParsingError` if input is invalid. +""" parse!(p::Parser, bytes::String)::Int = parse!(p, Vector{UInt8}(bytes)) parse!(p::Parser, bytes)::Int = parse!(p, view(bytes, 1:length(bytes))) +const ByteView = typeof(view(UInt8[], 1:0)) + function parse!(parser::Parser, bytes::ByteView)::Int isempty(bytes) && throw(ArgumentError("bytes must not be empty")) @@ -140,9 +240,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int @inbounds ch = Char(bytes[p]) if p_state == s_dead - #= this state is used after a 'Connection: close' message - # the parser will error out if it reads another message - =# + # This state is used after a 'Connection: close' message + # the parser will error out if it reads another message @errorif(ch != CR && ch != LF, HPE_CLOSED_CONNECTION) elseif p_state == s_start_req_or_res @@ -162,7 +261,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int p_state = s_res_HT else @errorif(ch != 'E', HPE_INVALID_CONSTANT) - parser.method = HEAD + parser.message.method = HEAD parser.index = 3 p_state = s_req_method end @@ -178,60 +277,60 @@ function parse!(parser::Parser, bytes::ByteView)::Int end elseif p_state == s_res_H - @strictcheck(ch != 'T') + @errorifstrict(ch != 'T') p_state = s_res_HT elseif p_state == s_res_HT - @strictcheck(ch != 'T') + @errorifstrict(ch != 'T') p_state = s_res_HTT elseif p_state == s_res_HTT - @strictcheck(ch != 'P') + @errorifstrict(ch != 'P') p_state = s_res_HTTP elseif p_state == s_res_HTTP - @strictcheck(ch != '/') + @errorifstrict(ch != '/') p_state = s_res_first_http_major elseif p_state == s_res_first_http_major @errorif(!isnum(ch), HPE_INVALID_VERSION) - parser.major = Int16(ch - '0') + parser.message.major = Int16(ch - '0') p_state = s_res_http_major - #= major HTTP version or dot =# + # major HTTP version or dot elseif p_state == s_res_http_major if ch == '.' p_state = s_res_first_http_minor continue end @errorif(!isnum(ch), HPE_INVALID_VERSION) - parser.major *= Int16(10) - parser.major += Int16(ch - '0') - @errorif(parser.major > 999, HPE_INVALID_VERSION) + parser.message.major *= Int16(10) + parser.message.major += Int16(ch - '0') + @errorif(parser.message.major > 999, HPE_INVALID_VERSION) - #= first digit of minor HTTP version =# + # first digit of minor HTTP version elseif p_state == s_res_first_http_minor @errorif(!isnum(ch), HPE_INVALID_VERSION) - parser.minor = Int16(ch - '0') + parser.message.minor = Int16(ch - '0') p_state = s_res_http_minor - #= minor HTTP version or end of request line =# + # minor HTTP version or end of request line elseif p_state == s_res_http_minor if ch == ' ' p_state = s_res_first_status_code continue end @errorif(!isnum(ch), HPE_INVALID_VERSION) - parser.minor *= Int16(10) - parser.minor += Int16(ch - '0') - @errorif(parser.minor > 999, HPE_INVALID_VERSION) + parser.message.minor *= Int16(10) + parser.message.minor += Int16(ch - '0') + @errorif(parser.message.minor > 999, HPE_INVALID_VERSION) elseif p_state == s_res_first_status_code if !isnum(ch) ch == ' ' && continue @err(HPE_INVALID_STATUS) end - parser.status = Int32(ch - '0') + parser.message.status = Int32(ch - '0') p_state = s_res_status_code elseif p_state == s_res_status_code @@ -246,9 +345,9 @@ function parse!(parser::Parser, bytes::ByteView)::Int @err(HPE_INVALID_STATUS) end else - parser.status *= Int32(10) - parser.status += Int32(ch - '0') - @errorif(parser.status > 999, HPE_INVALID_STATUS) + parser.message.status *= Int32(10) + parser.message.status += Int32(ch - '0') + @errorif(parser.message.status > 999, HPE_INVALID_STATUS) end elseif p_state == s_res_status_start @@ -269,7 +368,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int end elseif p_state == s_res_line_almost_done - @strictcheck(ch != LF) + @errorifstrict(ch != LF) p_state = s_header_field_start elseif p_state == s_start_req @@ -278,46 +377,46 @@ function parse!(parser::Parser, bytes::ByteView)::Int parser.content_length = ULLONG_MAX @errorif(!isalpha(ch), HPE_INVALID_METHOD) - parser.method = Method(0) + parser.message.method = Method(0) parser.index = 2 if ch == 'A' - parser.method = ACL + parser.message.method = ACL elseif ch == 'B' - parser.method = BIND + parser.message.method = BIND elseif ch == 'C' - parser.method = CONNECT + parser.message.method = CONNECT elseif ch == 'D' - parser.method = DELETE + parser.message.method = DELETE elseif ch == 'G' - parser.method = GET + parser.message.method = GET elseif ch == 'H' - parser.method = HEAD + parser.message.method = HEAD elseif ch == 'L' - parser.method = LOCK + parser.message.method = LOCK elseif ch == 'M' - parser.method = MKCOL + parser.message.method = MKCOL elseif ch == 'N' - parser.method = NOTIFY + parser.message.method = NOTIFY elseif ch == 'O' - parser.method = OPTIONS + parser.message.method = OPTIONS elseif ch == 'P' - parser.method = POST + parser.message.method = POST elseif ch == 'R' - parser.method = REPORT + parser.message.method = REPORT elseif ch == 'S' - parser.method = SUBSCRIBE + parser.message.method = SUBSCRIBE elseif ch == 'T' - parser.method = TRACE + parser.message.method = TRACE elseif ch == 'U' - parser.method = UNLOCK + parser.message.method = UNLOCK else @err(HPE_INVALID_METHOD) end p_state = s_req_method elseif p_state == s_req_method - matcher = string(parser.method) + matcher = string(parser.message.method) @debugshow 3 matcher @debugshow 3 parser.index if ch == ' ' && parser.index == length(matcher) + 1 @@ -327,47 +426,50 @@ function parse!(parser::Parser, bytes::ByteView)::Int elseif ch == matcher[parser.index] @debug 3 "nada" elseif isalpha(ch) - ci = @shifted(parser.method, Int(parser.index) - 1, ch) - if ci == @shifted(POST, 1, 'U') - parser.method = PUT - elseif ci == @shifted(POST, 1, 'A') - parser.method = PATCH - elseif ci == @shifted(CONNECT, 1, 'H') - parser.method = CHECKOUT - elseif ci == @shifted(CONNECT, 2, 'P') - parser.method = COPY - elseif ci == @shifted(MKCOL, 1, 'O') - parser.method = MOVE - elseif ci == @shifted(MKCOL, 1, 'E') - parser.method = MERGE - elseif ci == @shifted(MKCOL, 2, 'A') - parser.method = MKACTIVITY - elseif ci == @shifted(MKCOL, 3, 'A') - parser.method = MKCALENDAR - elseif ci == @shifted(SUBSCRIBE, 1, 'E') - parser.method = SEARCH - elseif ci == @shifted(REPORT, 2, 'B') - parser.method = REBIND - elseif ci == @shifted(POST, 1, 'R') - parser.method = PROPFIND - elseif ci == @shifted(PROPFIND, 4, 'P') - parser.method = PROPPATCH - elseif ci == @shifted(PUT, 2, 'R') - parser.method = PURGE - elseif ci == @shifted(LOCK, 1, 'I') - parser.method = LINK - elseif ci == @shifted(UNLOCK, 2, 'S') - parser.method = UNSUBSCRIBE - elseif ci == @shifted(UNLOCK, 2, 'B') - parser.method = UNBIND - elseif ci == @shifted(UNLOCK, 3, 'I') - parser.method = UNLINK + ci = @methodstate(parser.message.method, + Int(parser.index) - 1, ch) + if ci == @methodstate(POST, 1, 'U') + parser.message.method = PUT + elseif ci == @methodstate(POST, 1, 'A') + parser.message.method = PATCH + elseif ci == @methodstate(CONNECT, 1, 'H') + parser.message.method = CHECKOUT + elseif ci == @methodstate(CONNECT, 2, 'P') + parser.message.method = COPY + elseif ci == @methodstate(MKCOL, 1, 'O') + parser.message.method = MOVE + elseif ci == @methodstate(MKCOL, 1, 'E') + parser.message.method = MERGE + elseif ci == @methodstate(MKCOL, 2, 'A') + parser.message.method = MKACTIVITY + elseif ci == @methodstate(MKCOL, 3, 'A') + parser.message.method = MKCALENDAR + elseif ci == @methodstate(SUBSCRIBE, 1, 'E') + parser.message.method = SEARCH + elseif ci == @methodstate(REPORT, 2, 'B') + parser.message.method = REBIND + elseif ci == @methodstate(POST, 1, 'R') + parser.message.method = PROPFIND + elseif ci == @methodstate(PROPFIND, 4, 'P') + parser.message.method = PROPPATCH + elseif ci == @methodstate(PUT, 2, 'R') + parser.message.method = PURGE + elseif ci == @methodstate(LOCK, 1, 'I') + parser.message.method = LINK + elseif ci == @methodstate(UNLOCK, 2, 'S') + parser.message.method = UNSUBSCRIBE + elseif ci == @methodstate(UNLOCK, 2, 'B') + parser.message.method = UNBIND + elseif ci == @methodstate(UNLOCK, 3, 'I') + parser.message.method = UNLINK else @err(HPE_INVALID_METHOD) end - elseif ch == '-' && parser.index == 2 && parser.method == MKCOL + elseif ch == '-' && + parser.index == 2 && + parser.message.method == MKCOL @debug 3 "matched MSEARCH" - parser.method = MSEARCH + parser.message.method = MSEARCH parser.index -= 1 else @err(HPE_INVALID_METHOD) @@ -377,7 +479,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int elseif p_state == s_req_spaces_before_url ch == ' ' && continue - if parser.method == CONNECT + if parser.message.method == CONNECT p_state = s_req_server_start else p_state = s_req_url_start @@ -407,8 +509,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int if ch == ' ' p_state = s_req_http_start else - parser.major = Int16(0) - parser.minor = Int16(9) + parser.message.major = Int16(0) + parser.message.minor = Int16(9) p_state = ifelse(ch == CR, s_req_line_almost_done, s_header_field_start) end @@ -418,14 +520,17 @@ function parse!(parser::Parser, bytes::ByteView)::Int @errorif(p_state == s_dead, HPE_INVALID_URL) p += 1 end + @passert p <= len + 1 write(parser.valuebuffer, view(bytes, start:p-1)) if p_state >= s_req_http_start - parser.url = take!(parser.valuebuffer) - @debugshow 3 parser.url + parser.message.url = take!(parser.valuebuffer) + @debugshow 3 parser.message.url end + p = min(p, len) + elseif p_state == s_req_http_start if ch == 'H' p_state = s_req_http_H @@ -435,60 +540,60 @@ function parse!(parser::Parser, bytes::ByteView)::Int end elseif p_state == s_req_http_H - @strictcheck(ch != 'T') + @errorifstrict(ch != 'T') p_state = s_req_http_HT elseif p_state == s_req_http_HT - @strictcheck(ch != 'T') + @errorifstrict(ch != 'T') p_state = s_req_http_HTT elseif p_state == s_req_http_HTT - @strictcheck(ch != 'P') + @errorifstrict(ch != 'P') p_state = s_req_http_HTTP elseif p_state == s_req_http_HTTP - @strictcheck(ch != '/') + @errorifstrict(ch != '/') p_state = s_req_first_http_major - #= first digit of major HTTP version =# + # first digit of major HTTP version elseif p_state == s_req_first_http_major @errorif(ch < '1' || ch > '9', HPE_INVALID_VERSION) - parser.major = Int16(ch - '0') + parser.message.major = Int16(ch - '0') p_state = s_req_http_major - #= major HTTP version or dot =# + # major HTTP version or dot elseif p_state == s_req_http_major if ch == '.' p_state = s_req_first_http_minor elseif !isnum(ch) @err(HPE_INVALID_VERSION) else - parser.major *= Int16(10) - parser.major += Int16(ch - '0') - @errorif(parser.major > 999, HPE_INVALID_VERSION) + parser.message.major *= Int16(10) + parser.message.major += Int16(ch - '0') + @errorif(parser.message.major > 999, HPE_INVALID_VERSION) end - #= first digit of minor HTTP version =# + # first digit of minor HTTP version elseif p_state == s_req_first_http_minor @errorif(!isnum(ch), HPE_INVALID_VERSION) - parser.minor = Int16(ch - '0') + parser.message.minor = Int16(ch - '0') p_state = s_req_http_minor - #= minor HTTP version or end of request line =# + # minor HTTP version or end of request line elseif p_state == s_req_http_minor if ch == CR p_state = s_req_line_almost_done elseif ch == LF p_state = s_header_field_start else - #= XXX allow spaces after digit? =# + # FIXME allow spaces after digit? @errorif(!isnum(ch), HPE_INVALID_VERSION) - parser.minor *= Int16(10) - parser.minor += Int16(ch - '0') - @errorif(parser.minor > 999, HPE_INVALID_VERSION) + parser.message.minor *= Int16(10) + parser.message.minor += Int16(ch - '0') + @errorif(parser.message.minor > 999, HPE_INVALID_VERSION) end - #= end of request line =# + # end of request line elseif p_state == s_req_line_almost_done @errorif(ch != LF, HPE_LF_EXPECTED) p_state = s_header_field_start @@ -497,8 +602,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int if ch == CR p_state = s_headers_almost_done elseif ch == LF - #= they might be just sending \n instead of \r\n so this would be - * the second \n to denote the end of headers=# + # they might be just sending \n instead of \r\n so this would be + # the second \n to denote the end of headers p_state = s_headers_almost_done p -= 1 else @@ -551,47 +656,53 @@ function parse!(parser::Parser, bytes::ByteView)::Int else parser.header_state = h_general end - #= connection =# + # connection elseif h == h_matching_connection parser.index += 1 - if parser.index > length(CONNECTION) || c != CONNECTION[parser.index] + if parser.index > length(CONNECTION) || + c != CONNECTION[parser.index] parser.header_state = h_general elseif parser.index == length(CONNECTION) parser.header_state = h_connection end - #= proxy-connection =# + # proxy-connection elseif h == h_matching_proxy_connection parser.index += 1 - if parser.index > length(PROXY_CONNECTION) || c != PROXY_CONNECTION[parser.index] + if parser.index > length(PROXY_CONNECTION) || + c != PROXY_CONNECTION[parser.index] parser.header_state = h_general elseif parser.index == length(PROXY_CONNECTION) parser.header_state = h_connection end - #= content-length =# + # content-length elseif h == h_matching_content_length parser.index += 1 - if parser.index > length(CONTENT_LENGTH) || c != CONTENT_LENGTH[parser.index] + if parser.index > length(CONTENT_LENGTH) || + c != CONTENT_LENGTH[parser.index] parser.header_state = h_general elseif parser.index == length(CONTENT_LENGTH) parser.header_state = h_content_length end - #= transfer-encoding =# + # transfer-encoding elseif h == h_matching_transfer_encoding parser.index += 1 - if parser.index > length(TRANSFER_ENCODING) || c != TRANSFER_ENCODING[parser.index] + if parser.index > length(TRANSFER_ENCODING) || + c != TRANSFER_ENCODING[parser.index] parser.header_state = h_general elseif parser.index == length(TRANSFER_ENCODING) parser.header_state = h_transfer_encoding end - #= upgrade =# + # upgrade elseif h == h_matching_upgrade parser.index += 1 - if parser.index > length(UPGRADE) || c != UPGRADE[parser.index] + if parser.index > length(UPGRADE) || + c != UPGRADE[parser.index] parser.header_state = h_general elseif parser.index == length(UPGRADE) parser.header_state = h_upgrade end - elseif @anyeq(h, h_connection, h_content_length, h_transfer_encoding, h_upgrade) + elseif @anyeq(h, h_connection, h_content_length, + h_transfer_encoding, h_upgrade) if ch != ' ' parser.header_state = h_general end @@ -600,14 +711,17 @@ function parse!(parser::Parser, bytes::ByteView)::Int end p += 1 end + @passert p <= len + 1 if ch == ':' p_state = s_header_value_discard_ws else - @assert tokens[Int(ch)+1] != Char(0) || !strict && ch == ' ' + @passert tokens[Int(ch)+1] != Char(0) || !strict && ch == ' ' end write(parser.fieldbuffer, view(bytes, start:p-1)) + p = min(p, len) + elseif p_state == s_header_value_discard_ws (ch == ' ' || ch == '\t') && continue if ch == CR @@ -629,20 +743,22 @@ function parse!(parser::Parser, bytes::ByteView)::Int parser.flags |= F_UPGRADE parser.header_state = h_general elseif parser.header_state == h_transfer_encoding - #= looking for 'Transfer-Encoding: chunked' =# - parser.header_state = ifelse(c == 'c', h_matching_transfer_encoding_chunked, h_general) + # looking for 'Transfer-Encoding: chunked' + parser.header_state = ifelse( + c == 'c', h_matching_transfer_encoding_chunked, h_general) elseif parser.header_state == h_content_length @errorif(!isnum(ch), HPE_INVALID_CONTENT_LENGTH) - @errorif((parser.flags & F_CONTENTLENGTH > 0) != 0, HPE_UNEXPECTED_CONTENT_LENGTH) + @errorif((parser.flags & F_CONTENTLENGTH > 0) != 0, + HPE_UNEXPECTED_CONTENT_LENGTH) parser.flags |= F_CONTENTLENGTH parser.content_length = UInt64(ch - '0') elseif parser.header_state == h_connection - #= looking for 'Connection: keep-alive' =# + # looking for 'Connection: keep-alive' if c == 'k' parser.header_state = h_matching_connection_keep_alive - #= looking for 'Connection: close' =# + # looking for 'Connection: close' elseif c == 'c' parser.header_state = h_matching_connection_close elseif c == 'u' @@ -650,7 +766,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int else parser.header_state = h_matching_connection_token end - #= Multi-value `Connection` header =# + # Multi-value `Connection` header elseif parser.header_state == h_matching_connection_token_start else parser.header_state = h_general @@ -679,7 +795,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int @debugshow 3 h if h == h_general - crlf = findfirst(x->(x == bCR || x == bLF), view(bytes, p:len)) + crlf = findfirst(x->(x == bCR || x == bLF), + view(bytes, p:len)) p = crlf == 0 ? len : p + crlf - 2 elseif h == h_connection || h == h_transfer_encoding @@ -696,7 +813,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int t *= UInt64(10) t += UInt64(ch - '0') - #= Overflow? Test against a conservative limit for simplicity. =# + # Overflow? + # Test against a conservative limit for simplicity. @debugshow 3 Int(parser.content_length) if div(ULLONG_MAX - 10, 10) < t parser.header_state = h @@ -705,20 +823,21 @@ function parse!(parser::Parser, bytes::ByteView)::Int parser.content_length = t end - #= Transfer-Encoding: chunked =# + # Transfer-Encoding: chunked elseif h == h_matching_transfer_encoding_chunked parser.index += 1 - if parser.index > length(CHUNKED) || c != CHUNKED[parser.index] + if parser.index > length(CHUNKED) || + c != CHUNKED[parser.index] h = h_general elseif parser.index == length(CHUNKED) h = h_transfer_encoding_chunked end elseif h == h_matching_connection_token_start - #= looking for 'Connection: keep-alive' =# + # looking for 'Connection: keep-alive' if c == 'k' h = h_matching_connection_keep_alive - #= looking for 'Connection: close' =# + # looking for 'Connection: close' elseif c == 'c' h = h_matching_connection_close elseif c == 'u' @@ -726,32 +845,35 @@ function parse!(parser::Parser, bytes::ByteView)::Int elseif tokens[Int(c)+1] > '\0' h = h_matching_connection_token elseif c == ' ' || c == '\t' - #= Skip lws =# + # Skip lws else h = h_general end - #= looking for 'Connection: keep-alive' =# + # looking for 'Connection: keep-alive' elseif h == h_matching_connection_keep_alive parser.index += 1 - if parser.index > length(KEEP_ALIVE) || c != KEEP_ALIVE[parser.index] + if parser.index > length(KEEP_ALIVE) || + c != KEEP_ALIVE[parser.index] h = h_matching_connection_token elseif parser.index == length(KEEP_ALIVE) h = h_connection_keep_alive end - #= looking for 'Connection: close' =# + # looking for 'Connection: close' elseif h == h_matching_connection_close parser.index += 1 - if parser.index > length(CLOSE) || c != CLOSE[parser.index] + if parser.index > length(CLOSE) || + c != CLOSE[parser.index] h = h_matching_connection_token elseif parser.index == length(CLOSE) h = h_connection_close end - #= looking for 'Connection: upgrade' =# + # looking for 'Connection: upgrade' elseif h == h_matching_connection_upgrade parser.index += 1 - if parser.index > length(UPGRADE) || c != UPGRADE[parser.index] + if parser.index > length(UPGRADE) || + c != UPGRADE[parser.index] h = h_matching_connection_token elseif parser.index == length(UPGRADE) h = h_connection_upgrade @@ -768,7 +890,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int h = h_general end - elseif @anyeq(h, h_connection_keep_alive, h_connection_close, h_connection_upgrade) + elseif @anyeq(h, h_connection_keep_alive, h_connection_close, + h_connection_upgrade) if ch == ',' if h == h_connection_keep_alive parser.flags |= F_CONNECTION_KEEP_ALIVE @@ -789,6 +912,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int end p += 1 end + @passert p <= len + 1 + parser.header_state = h write(parser.valuebuffer, view(bytes, start:p-1)) @@ -798,6 +923,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int String(take!(parser.valuebuffer))) end + p = min(p, len) + elseif p_state == s_header_almost_done @errorif(ch != LF, HPE_LF_EXPECTED) p_state = s_header_value_lws @@ -807,7 +934,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int if ch == ' ' || ch == '\t' p_state = s_header_value_start else - #= finished the header =# + # finished the header if parser.header_state == h_connection_keep_alive parser.flags |= F_CONNECTION_KEEP_ALIVE elseif parser.header_state == h_connection_close @@ -821,7 +948,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int end elseif p_state == s_header_value_discard_ws_almost_done - @strictcheck(ch != LF) + @errorifstrict(ch != LF) p_state = s_header_value_discard_lws elseif p_state == s_header_value_discard_lws @@ -838,79 +965,78 @@ function parse!(parser::Parser, bytes::ByteView)::Int parser.flags |= F_CHUNKED end - #= header value was empty =# + # header value was empty p_state = s_header_field_start parser.onheader(String(take!(parser.fieldbuffer)) => "") p -= 1 end elseif p_state == s_headers_almost_done - @strictcheck(ch != LF) + @errorifstrict(ch != LF) p -= 1 if (parser.flags & F_TRAILING) > 0 - #= End of a chunked request =# + # End of a chunked request p_state = s_message_done continue end - #= Cannot use chunked encoding and a content-length header together - per the HTTP specification. =# - @errorif((parser.flags & F_CHUNKED) > 0 && (parser.flags & F_CONTENTLENGTH) > 0, HPE_UNEXPECTED_CONTENT_LENGTH) + # Cannot use chunked encoding and a content-length header together + # per the HTTP specification. + @errorif((parser.flags & F_CHUNKED) > 0 && + (parser.flags & F_CONTENTLENGTH) > 0, + HPE_UNEXPECTED_CONTENT_LENGTH) p_state = s_headers_done parser.state = p_state - parser.onheaderscomplete() - #= Set this here so that on_headers_complete() callbacks can see it =# - @debug 3 "checking for upgrade..." - if (parser.flags & F_UPGRADE > 0) && (parser.flags & F_CONNECTION_UPGRADE > 0) - parser.upgrade = isrequest(parser) || parser.status == 101 + # Set this here so that on_headers_complete() callbacks can see it + if (parser.flags & F_UPGRADE > 0) && + (parser.flags & F_CONNECTION_UPGRADE > 0) + parser.message.upgrade = isrequest(parser) || + parser.message.status == 101 else - parser.upgrade = isrequest(parser) && parser.method == CONNECT + parser.message.upgrade = isrequest(parser) && + parser.message.method == CONNECT end - @debugshow 3 parser.upgrade - #= Here we call the headers_complete callback. This is somewhat - * different than other callbacks because if the user returns 1, we - * will interpret that as saying that this message has no body. This - * is needed for the annoying case of recieving a response to a HEAD - * request. - * - * We'd like to use CALLBACK_NOTIFY_NOADVANCE() here but we cannot, so - * we have to simulate it by handling a change in errno below. - =# + @debugshow 3 parser.message.upgrade @debug 3 "headersdone" + parser.onheaderscomplete(parser.message) elseif p_state == s_headers_done - @strictcheck(ch != LF) + @errorifstrict(ch != LF) if parser.flags & F_CHUNKED > 0 - #= chunked encoding - ignore Content-Length header =# + # chunked encoding - ignore Content-Length header p_state = s_chunk_size_start elseif parser.isheadresponse || parser.content_length == 0 || - parser.upgrade && isrequest(parser) && parser.method == CONNECT + (parser.message.upgrade && isrequest(parser) && + parser.message.method == CONNECT) p_state = s_message_done elseif parser.content_length != ULLONG_MAX - #= Content-Length header given and non-zero =# + # Content-Length header given and non-zero p_state = s_body_identity - elseif http_message_needs_eof(parser) - #= Read body until EOF =# - p_state = s_body_identity_eof - else - #= Assume content-length 0 - read the next =# + elseif isrequest(parser) || # FIXME never need eof() for request? + div(parser.message.status, 100) == 1 || # 1xx e.g. Continue + parser.message.status == 204 || # No Content + parser.message.status == 304 # Not Modified + # Assume content-length 0 - read the next p_state = s_message_done + else + # Read body until EOF + p_state = s_body_identity_eof end elseif p_state == s_body_identity to_read = Int(min(parser.content_length, len - p + 1)) - assert(parser.content_length != 0 && parser.content_length != ULLONG_MAX) + @passert parser.content_length != 0 && + parser.content_length != ULLONG_MAX parser.onbody(view(bytes, p:p + to_read - 1)) - #= The difference between advancing content_length and p is because - * the latter will automaticaly advance on the next loop iteration. - * Further, if content_length ends up at 0, we want to see the last - * byte again for our message complete callback. - =# + # The difference between advancing content_length and p is because + # the latter will automaticaly advance on the next loop iteration. + # Further, if content_length ends up at 0, we want to see the last + # byte again for our message complete callback. parser.content_length -= to_read p += to_read - 1 @@ -918,13 +1044,13 @@ function parse!(parser::Parser, bytes::ByteView)::Int p_state = s_message_done end - #= read until EOF =# + # read until EOF elseif p_state == s_body_identity_eof parser.onbody(view(bytes, p:len)) p = len elseif p_state == s_chunk_size_start - assert(parser.flags & F_CHUNKED > 0) + @passert parser.flags & F_CHUNKED > 0 unhex_val = unhex[Int(ch)+1] @errorif(unhex_val == -1, HPE_INVALID_CHUNK_SIZE) @@ -933,7 +1059,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int p_state = s_chunk_size elseif p_state == s_chunk_size - assert(parser.flags & F_CHUNKED > 0) + @passert parser.flags & F_CHUNKED > 0 if ch == CR p_state = s_chunk_size_almost_done else @@ -950,7 +1076,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int t *= UInt64(16) t += UInt64(unhex_val) - #= Overflow? Test against a conservative limit for simplicity. =# + # Overflow? Test against a conservative limit for simplicity. @debugshow 3 Int(parser.content_length) if div(ULLONG_MAX - 16, 16) < t @err(HPE_INVALID_CONTENT_LENGTH) @@ -959,15 +1085,15 @@ function parse!(parser::Parser, bytes::ByteView)::Int end elseif p_state == s_chunk_parameters - assert(parser.flags & F_CHUNKED > 0) - #= just ignore this shit. TODO check for overflow =# + @passert parser.flags & F_CHUNKED > 0 + # just ignore this?. FIXME check for overflow? if ch == CR p_state = s_chunk_size_almost_done end elseif p_state == s_chunk_size_almost_done - assert(parser.flags & F_CHUNKED > 0) - @strictcheck(ch != LF) + @passert parser.flags & F_CHUNKED > 0 + @errorifstrict(ch != LF) if parser.content_length == 0 parser.flags |= F_TRAILING @@ -979,14 +1105,14 @@ function parse!(parser::Parser, bytes::ByteView)::Int elseif p_state == s_chunk_data to_read = Int(min(parser.content_length, len - p + 1)) - assert(parser.flags & F_CHUNKED > 0) - assert(parser.content_length != 0 && parser.content_length != ULLONG_MAX) + @passert parser.flags & F_CHUNKED > 0 + @passert parser.content_length != 0 && + parser.content_length != ULLONG_MAX parser.onbody(view(bytes, p:p + to_read - 1)) - #= See the explanation in s_body_identity for why the content - * length and data pointers are managed this way. - =# + # See the explanation in s_body_identity for why the content + # length and data pointers are managed this way. parser.content_length -= to_read p += Int(to_read) - 1 @@ -995,63 +1121,27 @@ function parse!(parser::Parser, bytes::ByteView)::Int end elseif p_state == s_chunk_data_almost_done - assert(parser.flags & F_CHUNKED > 0) - assert(parser.content_length == 0) - @strictcheck(ch != CR) + @passert parser.flags & F_CHUNKED > 0 + @passert parser.content_length == 0 + @errorifstrict(ch != CR) p_state = s_chunk_data_done elseif p_state == s_chunk_data_done - assert(parser.flags & F_CHUNKED > 0) - @strictcheck(ch != LF) + @passert parser.flags & F_CHUNKED > 0 + @errorifstrict(ch != LF) p_state = s_chunk_size_start else - error("unhandled state") + @err HPE_INVALID_INTERNAL_STATE end end - @assert p_state == s_message_done || p == len || p == len + 1 - if p > len # FIXME - p = len - end - - parser.state = p_state + @assert p_state == s_message_done || p == len + @assert p <= len @debug 3 "parse!() exiting $(ParsingStateCode(p_state))" - return p -end -#= Does the parser need to see an EOF to find the end of the message? =# -function http_message_needs_eof(parser) - #= See RFC 2616 section 4.4 =# - if (isrequest(parser) || # FIXME request never needs EOF ?? - div(parser.status, 100) == 1 || #= 1xx e.g. Continue =# - parser.status == 204 || #= No Content =# - parser.status == 304 || #= Not Modified =# - parser.isheadresponse) #= response to a HEAD request =# - return false - end - - if (parser.flags & F_CHUNKED > 0) || parser.content_length != ULLONG_MAX - return false - end - - return true -end - -function http_should_keep_alive(parser) - if parser.major > 0 && parser.minor > 0 - #= HTTP/1.1 =# - if parser.flags & F_CONNECTION_CLOSE > 0 - return false - end - else - #= HTTP/1.0 or earlier =# - if !(parser.flags & F_CONNECTION_KEEP_ALIVE > 0) - return false - end - end - - return !http_message_needs_eof(parser) + parser.state = p_state + return p end diff --git a/src/consts.jl b/src/consts.jl index 650a222a3..c4e816a1a 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -113,6 +113,7 @@ const STATUS_CODES = Dict( # RFC-2068, section 19.6.1.2 LINK=31, UNLINK=32, + NOMETHOD ) const MethodMap = Dict( diff --git a/src/server.jl b/src/server.jl index 9ad277875..fb32023ef 100644 --- a/src/server.jl +++ b/src/server.jl @@ -350,4 +350,39 @@ serve(; host::IPAddr=IPv4(127,0,0,1), args...) = serve(host, port, handler, logger; cert=cert, key=key, verbose=verbose, args...) +#= Does the parser need to see an EOF to find the end of the message? =# +function http_message_needs_eof(parser) + #= See RFC 2616 section 4.4 =# + if (isrequest(parser) || # FIXME request never needs EOF ?? + div(parser.status, 100) == 1 || #= 1xx e.g. Continue =# + parser.status == 204 || #= No Content =# + parser.status == 304 || #= Not Modified =# + parser.isheadresponse) #= response to a HEAD request =# + return false + end + + if (parser.flags & F_CHUNKED > 0) || parser.content_length != ULLONG_MAX + return false + end + + return true +end + +function http_should_keep_alive(parser) + if parser.major > 0 && parser.minor > 0 + #= HTTP/1.1 =# + if parser.flags & F_CONNECTION_CLOSE > 0 + return false + end + else + #= HTTP/1.0 or earlier =# + if !(parser.flags & F_CONNECTION_KEEP_ALIVE > 0) + return false + end + end + + return !http_message_needs_eof(parser) +end + + end # module diff --git a/test/.body.jl.swp b/test/.body.jl.swp new file mode 100644 index 0000000000000000000000000000000000000000..894916391eb84e63604e35a0d0024b8bb1b0f94f GIT binary patch literal 20480 zcmeI4dyFJS9mo6bkk<)#U{r{h+FAaXxtr;k*~i`9EF;_<3%hy`VQ*n`cd+!#^vw2c zPxsK>voBCkc^rxwm4rV|iNTl{VnpMCi4yrk;vayIXrdTU45Hy6hzc5FF!=q|W2Sp< z=5Fsn(Aa(XY<2(Y(N*92RZmq{Ln(Ld#16JeAJTBVN7D|cHtf0W9}liR^vPA4Z`8?p z%wy81` zl_h-Okk3&8{fRzy!TD1sQC6HaRv=a&Rv=a&Rv=a&Rv=a& zRv=d3e@%g)u}ZrYl|D^Yd{~CRl;J8Fs(AebUpH^^|U zY_Q6Ij!e-X|M8Ca^JV;{ zGJL-jT$O);w5xua?1-1qaS$sID-bIXD-bIXD-bIXD-bIXD-bIXEAU^cfKk!3=dieZ zix!wzM5^^3UB3u^3!VfIg9pF?a1+=I#z7t=z&fxRyo!bFAHYw+!{8ve8!P}5RKOpx z7=8}?9y|mNg4@9|xE|~R{oq1y0XP$!2G)Sp;4l{3-v^%u6_5dG@Cp{`FN2rBVelgO z88`$Ef@N?PcnXXC?}D#`&w!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gp9d$5Q!=7ZZCGuL&>%BR>%=3}D< zLlVksIm|s13L#$%dpNyJ^ibl+UGyc<-cZS8Pxn1|2qFNJ3|kVEVX5NUx*MF|(hewf z(++CL81TMB@8`8`-1mpY2w}}mMg?`p^BQW??8%vMMc)(hP=8~3KhE3JhR2*kwqZ_t z{Ox4IvHK}q9%*(m`SDBXGFh?`#swokFG?tg?VD~OJ$)FI6VX4Xvf*2NU~hVKE*+ZU z2s)05GSn|!KZceM)smLI3{fvSs_WBd;Y`}~T*@AzAvoK!7m`98KxpL-b&B-BMnY=P zu7Z(8N(>rmwF<`?>J`#fj7X6bEp0<%w6t3#R7tY0LQj^A1PWPe21!3;TZKkUBY9Ao z+o0f_pES4(T+bs@}Qbt)Q z67LDjhtbM&E#Jm~d^JH{Qw7nCvRKydfZ oN{7#TNj84uo04uqQIZ}2^8=|Gs&KP-NQDC7JT_L?Y+RrJ0EUAX82|tP literal 0 HcmV?d00001 diff --git a/test/.handlers.jl.swp b/test/.handlers.jl.swp new file mode 100644 index 0000000000000000000000000000000000000000..af65eea6a9615874803d3d2c2ac42279615b074d GIT binary patch literal 12288 zcmeI2J7^R^7{@08W1>byD=p$?)9i-a?%qQsMS=*%BoGXjL^P&wb2s;hm)*_n3qO|K+nI0X_s!#u+j-;c z*o1UkOE4V!8GA9_GkfFHgZ9VQ+L$mGy}J#MDRWUoqDLcRRw@^nFjFUO`;u0$Q)Xdx z(JQD>UnmvwroL=w$Mn(Z=}FDI=W^ldInzoPxFfWMHN{p}LRVyf3^Zw=o5f>M!z2V;kKqVf3uzx@0E<37edfcxM)I0r^RA9%Z$ zu{Yo$xDEsu2VI~OeC}rK5x51Gz#P!P*FB7V0q?*QumaLx82sLiI>BvVfe6?Ep6_Dp zE|>!e@V$$%Z{R(62JV407zDpM8G8j@f~VjPxBzCsAoz(s{s6DR3b+X_f+L^@cn%H$ zI>-PSAOmFJe>T7^Y>g+pSJNc_MYZ7lG{qT^9KJLV+)Hv%v@O9ERh5Q@*4&F3ah2g& zt>omT@WYwpm?8)7y9v|LvpL&!OkMD0Zpmt^;bINKEr;=1j}&bbsr3*>oe-!UUdv9C ze5r&iDe&hf%Hi3(aJkd##XvD!7$i$nB6I1KN>6cMEQeLai-pBA%4xO}1aN@^ufS=IwNwH`yn;3=0sXAt|D)tk-Dl=Bx_qce>5f z604#`uA<$~ef6csA6=E*Vk29)zX~Zq0cB?ctN;K2 literal 0 HcmV?d00001 diff --git a/test/.messages.jl.swp b/test/.messages.jl.swp new file mode 100644 index 0000000000000000000000000000000000000000..30a6125806147b73c0313d2d8e15833a685e8dbc GIT binary patch literal 28672 zcmeI4dyFJUdBEE?_yv9t2=Ip^_s*^C9@w4Ty_dzD_k0i5zBPBZ=i9Y`TOUX7Oz%#6 zXQpSndv13RNPGwx1X$u_LWmN9O~7C&AYlc^*opEmfL1MW+IUHBS24IhJd!AWRC4XSVg_P|B(Uu+)z68;?i2;L3vfXyC3)6I^VgVUuXNZYLe zFN}uVO4OVW7mAKN4I_8lmFLsb2lq$KG+0T!qBGxaR?;wPO4MdhNo#4^O1yR)mdke@ zoiq~0yXlbU&HMEvC>9+jXjYZ%EU~u`q<*y;ht2uOt3^rL^c#T)D;9^`mLDg9H`z?N zjN()WuU~*p{<`FyY+`B_H{IA#nZ_j~Q>T`kvyeuR)}pEk)R$TK$skUomgDYS`s#4I zF&D&MtJLuCiDHUWYJ|-wrcN$L9QakOK@-v{Ma>$aL5z%*;oW|#MdfvfNBR|AREahh zRabSS=h%RE(-_`#roO^C*nAYJ@t2Dr4qDcWO;=Gy}!_kg9e(812>4~3k)7n zM>sEc4Ar80!wv4hvLB{lb74M;CA5`w#(ZXb{Dh8^y7c66w;nVX(waB7iu+g5-QwOl z`G!LiZq092>w(*DN@sF!(H&Yc>!!WAFRdew<&XiQoXXT-9q?L1u9m)5N^j=7-BCwL z;7`815;jLc79F`Vhbf8FQl^el5>*xhdbx=pXA>z*{CuP#_t0EW?aoIsk98BG&@c%a zt#mb0OGo4ulg?d^;>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cHXFl6~$3evp@M3 z!bFHg_}r~?HqDAy?eMhTj4y$ip4@#;bLxY=xQFBnpS3+BX_FPYbOp) z%uGnVkL;bbzYxg zE2pwFUfsA=vW}EZv<&0*#-51F)h|C05_C_7TR_l z`^)N~_#h2}R>bv;S3=hBxT5ioX0He{6P{Ia&J4ZQM!h!Mvu7vm zvt>_GiM}|B|un8y|(dd4GcxnIl3sg~~)Sn(cAK5qFEdGWk#E zymKUQU)Cpexmcy7xT@mvIv>V-Y^-QMhg>4*VN1r5L_JkG=I!g+F=U;b^cden)V+*Y zC03UzbD$w(D$sE)IfgSFzLQ!>>j=%9OjMlBh}%$*c2+qiWs>#(6Rf?jWvwpje>utV z7p(7P%^$#4cm-Sr&k*6S-~mYB2)qW0pk(Oe_OI1|)qvH2)qvH2)qvH2)qvH2)qvH2 z)xfX024taa4!u@o!;RldI3`fe{hMSG7P_xyW6qJw{E?G^bt0^0i}QSxK8pmq?fIv& z1yz^*U)jKrUw<6J4K^h8Z&NlD z&JPvWNIyq;&~k{0z2{H-^ONctcs2D;BTD x|HHamb^DKAGXHNlt2y0F`Hz2a{h~!ue=!)y{uiv?&4vppSFeQsbB8$Re*j`WCc6Lt literal 0 HcmV?d00001 diff --git a/test/.parser.jl.swp b/test/.parser.jl.swp new file mode 100644 index 0000000000000000000000000000000000000000..9fa9f65e23b63a4a7c5d3d585728774592fa0583 GIT binary patch literal 106496 zcmeIb2Yh2$b@*>eXrV1-DftK9YIYaTj%`Wq)~t6|w&gOm-0RFNjx5Wv#flxyNqyQTTY=BThN%%nmfrJ355C|lYQ2yU@-@8xmJ(cmaU6PNq`yI*e z-Pg`7@1AqdJ$EgzI2JW}+B%!~Jh-XpwjZ^f{J_`WdiQrf^X~@Qy8WZ`^V4mzt&qqU{5!E+K9OrX z<2lHEipF0J2{a^dArdGSHd=ch(A3c$49MX}d{-F{eaOOvoT+h4Ljny6G$hcFKtloz z2{a_okU&EM4GH`&l|W(V5lv6x-tTVR|FHFaH_!K@tiK;`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_KV|34(IcPjt@ literal 0 HcmV?d00001 diff --git a/test/.runtests.jl.swp b/test/.runtests.jl.swp new file mode 100644 index 0000000000000000000000000000000000000000..66729fcab0c087690b064dc9a85ab35c475cc4a3 GIT binary patch literal 12288 zcmeI2F>ljA6vtn>Fcm5oSPyrA4tCtqkfOFyRkacs3Kb1OVnDjs=k$tm4n8}mOA#NS z415I!kXZQuNQh6t3gQ!V=Q(i(N0v}GD*aFT>2&w}-rfDSD6jALItQZdtrL$WqQh&= z7Z3k@S@`;{K~@fn-G7cFIkfI-%k7xp*gcTJZj!w6;v|r9HT8@OHc#?c%jVek9yA|y zyN6z}mnoYy)7%uZ^-ku}sztDZ0T{Tvfjo=cjYYb@w(1wj%g!Bf>*lk|*FgphzyJ)u z01UtY48Q;kz`&I?kc}GjmT$jMzTkE_H|FMYi3tW^00v+H24DaNU;qYS00v+H24LU{ z8jvB;w?(3j>wJ0s|9|%T|KbMGZ{`>CgZa#SWIizOnRDh1^OD(N?lMcv1=sn>d}lr} z=ge#7IkU|a=d_r01_K6Q00v+H24DaNU;qYS00v;-KQ*9CsAj{2o3t!=XcNSFsO~vd zrDK(H`?j+}l|T?{WwJ?NKId4PFji^N^vzk-dfDknO#_?NdP8Nc9Ms49NqAbGRJp@Q zrhLD;NWk6dy1rPR>beYTbQIMEA{{0DJc{ZM$}=6$-m^1lbyt~iiY9nR^wmHcA{IP- z>Tr~#nb?#38L8N+$!?GsTW*SNu`8|ereEkt^!A?~b)GyHTVm|^Ufc8Cz5SlI)@rvL SqTFgTm?m_&#lBn@C(~c2gTOKX literal 0 HcmV?d00001 diff --git a/test/.server.jl.swp b/test/.server.jl.swp new file mode 100644 index 0000000000000000000000000000000000000000..076c8f410fad6e0726f492c123e6144b4a38ec05 GIT binary patch literal 12288 zcmeI2ONbmr7{_blI}Z&;P$5cfCxjlHNB1V1EF-hV&1SP>5;tULS1<{&r>AzNcY3=f z-8HjW9}y3tAcWv8M}>$W9+W6V@F}>c{r%Zk}EY^ejJSrn;*7 z`~KfoU)8W(H)gAc>7L>)g4bq3zB%{w%U@mlc-_Uf21sBn$kv;_rmcmbU=A0mp4}=O zw(K#^+eMe#mfJt{3??kicU{LS^^D@~(qyf6q$t}23q&bkevkRZ`4xlur^vo#3S0$H*XTeKg7L>tLU@Q1* z6CrCC1N?nIA-{tQ;BC+YvtR~12DX3!K)}`e zFcqJclNJYtOOENQtsjqIu%(%aOoktfLPVjT{j{8 zjIGPOrfBKY!be|(D@RAExn~&HJ7^KEr@Oo<9l{%Qi~HfmdiXe)yezLL@oh4Z#7jM5 z)tax(Q(lPtCzmr)tHRHK6| zF{1C$Dyy@m<3&vlkL$ohbz*9)J2%ICT}~Q?E5110zYsfWA;$A0k&S!zO+`9gF&HIh z(ed)(B7f0A44cd=9;_ZZgd_>G-$vXl#|w1Zbr32zeB3|oNycOyy41< zA;Ww~t)wzFhvoPi4~VK~IaMZZGDw!fl36rmEu~Wjquz0>Li2MZ{*Hny{VBtWxM}&5 zS`u-yhHtVP2}eND)DpLPmg8D=m+8XpV3niq^*SUm>94knz9ZCVNV{6nSW{)J+DH;N zO}HtQb-XT<&MJp*$;cIPg3Y*hTou#P$^x$nJN7$@sufpUwFgQ`ZqeQJ?vnQR$kcR= zmQ?!5$Vm^@&XxVAJJ?N;IfhY?8QJGEb}YLC)p0>)jlO!nG5hwnZFH^aRwaBWi{tgM zM>UjGMFve7A;v} zwb`KB!{wRm#>cVc^H_Sn?mIe}tM}_T)65&4q(TePRn_pgMo+I*(NzeOeK4s!r*hbf zmmQ5}3YViA(pT`Y-`g#otmfo&B+rcM@FhRsa2qCwgOh2{Zg3A}DQL$I;Yf+BxP^@) mOoKc3Uv72k?ACT%mj;WDuv?L7IG0l`Olql>h7+CKH~$5~-1rdy literal 0 HcmV?d00001 diff --git a/test/.types.jl.swp b/test/.types.jl.swp new file mode 100644 index 0000000000000000000000000000000000000000..b7907af6a85dc74b551f2b8cb4f95865b280e67c GIT binary patch literal 12288 zcmeI2KX21O7{)JM7@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!OHvtg_3fv}(X2FLg9`j$L8FBlj>FJ~X={|5-C?cD$X literal 0 HcmV?d00001 diff --git a/test/.utils.jl.swp b/test/.utils.jl.swp new file mode 100644 index 0000000000000000000000000000000000000000..cf5ccce4e016df0fbdbd7b4fc2a019308697663a GIT binary patch literal 12288 zcmeI2Pi)&%9LHZL1dK6;fCC7STo;-I)oIedDW!5Zt(M;QL;jIPHR5Rl8JrmOjt%`~7}j z|NOOMr>ZNN^}+>hHa$hKJWj|LtHzuEnm10|dUuHUv>|3=mm+QWMt0gLIA-0rK+Q$h z-A>!CN$vfmdk`GI8rT*c-^rwB##c(E)wG!7%;)0)w`@Os^+3&j1+}FdP!1f)fq++y z+-Wj7k0+>drvQpZ@f+jX)T}qBZA-Ps?cEu#xfZZD`y4-MLhcunXH9!sZpK$$Hp(xodMX5#BB-+yc1>mVx^ z@i~8pYvIo4ABGj;Q~W;Aqp?G6Yb)x|SeM)JwXC(on%pSWEkCSYZkfHAL)2fE+5D*W zc~jJfyAC(v_euvgl;=wwC*6X0kqr|Oz@>U9eu#VGHfpjFjO zd4Ek9i7_p)DTcHdhQ+WahL^-pPKYrnh7_C;(qg$EZ4A<>lXLT8%@^>e^-QOA=LI6*YxgN_PSPKHOnFY0Sll2 A+5i9m literal 0 HcmV?d00001 diff --git a/test/client.jl b/test/client.jl index 62626366e..19b511566 100644 --- a/test/client.jl +++ b/test/client.jl @@ -2,20 +2,6 @@ using JSON -@testset "HTTP.Connection" begin - conn = HTTP.Connection(IOBuffer()) - @test conn.state == HTTP.Busy - HTTP.idle!(conn) - @test conn.state == HTTP.Idle - HTTP.busy!(conn) - @test conn.state == HTTP.Busy - HTTP.dead!(conn) - @test conn.state == HTTP.Dead - HTTP.idle!(conn) - @test conn.state == HTTP.Dead - HTTP.busy!(conn) - @test conn.state == HTTP.Dead -end for sch in ("http", "https") println("running $sch client tests...") diff --git a/test/parser.jl b/test/parser.jl index 0756b8cb7..4a7ece786 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1795,7 +1795,7 @@ const responses = Message[ @test String(take!(r.body)) == "fooba" for m in instances(Parsers.Method) - m == Parsers.CONNECT && continue + m in (Parsers.NOMETHOD, Parsers.CONNECT) && continue me = m == Parsers.MSEARCH ? "M-SEARCH" : "$m" r = Request("$me / HTTP/1.1\r\n\r\n") @test r.method == string(m) From 40e56eb58f8afb4da6fc4eff098c00ae79c299cf Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 12 Dec 2017 10:39:21 +1100 Subject: [PATCH 041/182] doc examples --- src/Parsers.jl | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/Parsers.jl b/src/Parsers.jl index f3b37adba..fa2ba0a9f 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -28,8 +28,11 @@ export Parser, parse!, messagecomplete, headerscomplete, waitingforeof, ParsingError, ParsingErrorCode +using ..IOExtras using ..URIs.parseurlchar +import MbedTLS.SSLContext + import ..@debug, ..@debugshow, ..DEBUG_LEVEL include("consts.jl") @@ -72,6 +75,30 @@ The `Parser` must be configured with output processing callbacks: in multiple fragments, then `obbody` will be called multiple times. - `onheaderscomplete = f(::Message)` is called at the end of the Header. + +Message data can be passed to the `parse!(::Parser, data)` function +or read from a stream by `read!(::IO, ::Parser)`. + +e.g. + +``` +p = Parser() +p.onheaderscomplete = m -> (@show string(m.method); @show m.url) +p.onheader = h -> @show h +end + +parse!(p, \"\"\" +GET /foo HTTP/1.1 +Content-Length: 0 +Foo: Bar + +\"\"\") + +h = "Content-Length"=>"0" +h = "Foo"=>"Bar" +string(m.method) = "GET" +m.url = "/foo" +``` """ mutable struct Parser @@ -107,6 +134,46 @@ Parser() = Parser(false, x->nothing, x->nothing, ()->nothing, IOBuffer(), IOBuffer(), Message()) +""" + read!(io, ::Parser) + +Read data from `io` into the `Parser` until `eof` +or until the parser finds the end of the message. + +If `readavailable(io)` reads past the end of the Message the excess bytes +are passed to `unread`. This is handled transparently if there is a suitable +`IOExtras.unread!(::IO, SubArray{UInt8, 1})` method defined. +""" + +function Base.read!(io::IO, p::Parser; unread=IOExtras.unread!) + + while !eof(io) + bytes = readavailable(io) + if isempty(bytes) + @debug 1 "Bug https://github.com/JuliaWeb/MbedTLS.jl/issues/113 !" + @assert isa(io, SSLContext) + @assert eof(io) + break + end + + n = parse!(p, bytes) + + if messagecomplete(p) + if n < length(bytes) + unread(io, view(bytes, n+1:length(bytes))) + end + return + end + end + + if !waitingforeof(p) + throw(ParsingError(headerscomplete(p) ? HPE_BODY_INCOMPLETE : + HPE_HEADERS_INCOMPLETE)) + end + return +end + + """ reset!(::Parser) From aa8f232dd71d6b37a28423acd4d8fea654f11ed6 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 12 Dec 2017 15:22:58 +1100 Subject: [PATCH 042/182] Architecture and Internal Interface Documentation --- docs/src/index.md | 301 ++++++++++++++++++++++++++++++++++++++++++++ src/Bodies.jl | 107 +++++++++++----- src/Connections.jl | 25 ++-- src/HTTP.jl | 5 +- src/Messages.jl | 123 +++++++----------- src/Parsers.jl | 25 ++-- src/SendRequest.jl | 9 +- src/server.jl | 4 +- test/.body.jl.swp | Bin 20480 -> 20480 bytes test/.client.jl.swp | Bin 28672 -> 28672 bytes test/.parser.jl.swp | Bin 106496 -> 106496 bytes test/body.jl | 8 +- test/client.jl | 4 +- test/parser.jl | 2 +- 14 files changed, 471 insertions(+), 142 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 883be666e..0850d12c1 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -53,3 +53,304 @@ HTTP.isvalid HTTP.sniff HTTP.escapeHTML ``` + +# HTTP.jl Architecture + +## Parser + +Source: [`Parsers.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Parsers.jl) + +The [`HTTP.Parser`](@ref) separates HTTP Message data (from a `String`, +an `IO` stream or raw bytes) into its component parts. The parts are passed to +three callback functions as they are parsed: +- `onheader(::Pair{String,String})` +- `onheaderscomplete(::`[`HTTP.Parsers.Message`](@ref)`)` +- `onbodyfragment(::SubArray{UInt8,1})` + +If the input data is invalid the Parser throws a [`HTTP.ParsingError`](@ref). + +A Parser processes a single HTTP Message. If the input stream contains +multiple Messages the Parser stops at the end of the first Message. +The `parse!(::Parser, data)` function returns the number of bytes consumed. +If less than `length(data)` was consumed, the excess must be processed +separately. The `read!(io, ::Parser)` method deals with this by pushing +the excess bytes back into the stream using `IOExtras.unread!`. + +The Parser does not interpret the Message Headers except as is necessary +to parse the Message Body. It is beyond the scope of the Parser to deal +with repeated header fields, multi-line values, cookies or case normalization +(see [`HTTP.Messages.appendheader`](@ref)). + +The Parser has no knowledge of the high-level `Request` and `Response` structs +defined in `Messages.jl`. The Parser has it's own low level +[`HTTP.Parsers.Message`](@ref) struct that represents both Request and Response +Messages. + + +## Messages + +Source: [`Messages.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Messages.jl) + +The `Messages` module defines structs that represent [`HTTP.Messages.Request`](@ref) +and [`HTTP.Messages.Response`](@ref) Messages. + +The Messages module defines `IO` `read` and `write` methods for Messages +but it does not deal with URIs, creating connections, or executing requests. + +The Messages module does not explicitly throw exceptions, but it calls +methods that may result in low level `IO` exceptions. + +### Sending Messages + +Messages are formatted and written to an `IO` stream by +[`Base.write(::IO,::HTTP.Messages.Message)`](@ref). + +[`Base.write(::IO, ::HTTP.Messages.Bodies.Body)`](@ref) is called to output the +Message Body. This function implements `chunked` encoding if the body length +is unknown. + + +### Receiving Messages + +Messages are parsed from `IO` stream data by +[`Base.read!(::IO,::HTTP.Messages.Message)`](@ref). + +This function creates a [`HTTP.Parser`](@ref) with callbacks as follows +- `onheader` = [`HTTP.Messages.appendheader`](@ref) +- `onheaderscomplete` = [`HTTP.Messages.readstartline!`](@ref) +- `onbodyfragment` = [`Base.write(::HTTP.Messages.Bodies.Body, bytes)`](@ref) + +[`Base.read!(::IO, ::HTTP.Parser)`](@ref) is called to feed the Parser +with data from the `IO` stream. As the Parser processes the data the +callbacks are called to fill in the `Message` struct. + +The `Response` struct has a `parent` field that points to the corresponding +`Request`. The `Request` struct as a `parent` field that points to a `Response` +in the case of HTTP Redirect. The [`HTTP.Messages.parentcount`](@ref) function is +used to place a limit on nested redirects. + + +### Headers + +Headers are represented by `Vector{Pair{String,String}}`. As compared to +`Dict{String,String}` this allows repeated header fields and preservation of +order. + +Header values can be accessed by name using +[`HTTP.Messages.header`](@ref) and +[`HTTP.Messages.setheader`](@ref). + +The [`HTTP.Messages.appendheader`](@ref) function handles combining +multi-line values, repeated header fields and special handling of +multiple `Set-Cookie` headers. + +### Bodies + +The [`HTTP.Messages.Bodies.Body`](@ref) struct represents a Message Body. +It either stores static body data in an `IOBuffer`, or wraps an `IO` stream +that will consume or produce the Message Body. + +The [`HTTP.Messages.setlengthheader`](@ref) function sets the `Content-Length` +header if the Message Body has known length, or sets the +`Transfer-Encoding: chunked` header to indicate that the Body length is not +known at the time the headers are sent. + + +## Connections + +### Basic Connections + +Source: [`Connect.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Connect.jl) + +[`HTTP.Connect.getconnection`](@ref) creates a new `TCPSocket` or `SSLContext` +for a specified `host` and `port. + +No connection streaming, pooling or reuse is implemented in this module. +However, the `getconnection` interface is the same as the one used by the +connection pool so the `Connect` module can be used directly when reuse is +not required. + + +### Pooled Connections + +Source: [`Connections.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Connections.jl) + +This module wrapps the Basic Connections module above and adds support for: +- Reusing connections for multiple Request/Response Messages, +- Interleaving Request/Response Messages. i.e. allowing a new Request to be + sent before while the previous Response is being read. + +This module defines a [`HTTP.Connections.Connection`](@ref)` <: IO` +struct to manage Message streaming and connection reuse. Methods +are provided for `eof`, `readavailable`, `unsafe_write` and `close`. +This allows the `Connection` object to act as a proxy for the +`TCPSocket` or `SSLContext` that it wraps. + + +The [`HTTP.Connections.pool`](@ref) is a collection of open +`Connection`s. The `request` function calls `getconnection` to +retrieve a connection from the `pool`. When the `request` function +has written a Request Message it calls `closewrite` to signal that +the `Connection` can be reused for writing (to send the next Request). +When the `request` function has read the Response Message it calls +`closeread` to signal that the `Connection` can be reused for +reading. + +e.g. +```julia +request(uri::URI, req::Request, res::Response) + T = uri.scheme == "https" ? SSLContext : TCPSocket + io = getconnection(Connection{T}, uri.host, uri.port) + write(io, req) + closewrite(io) + read!(io, res) + closeread(io) + return res +end +``` + +## Request Execution + +There are three seperate Request Execution layers, all with the same interface. +Clients can choose which layer to import according to the features they require. + +### Basic Request Execution + +Source: [`SendRequest.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/SendRequest.jl) + +The `SendRequest` module implements basic HTTP Request execution. + +The `request` function is split into three methods: +- [`HTTP.SendRequest.request(method::String, uri, headers, body)`](@ref)) +- [`HTTP.SendRequest.request(::HTTP.URIs.URI,request,response)`](@ref) +- [`HTTP.SendRequest.request(::IO, request, response)`](@ref)). + +These methods implement: +- Creating a [`HTTP.Messages.Request`](@ref) for a specified method, URI, + headers and body, +- Setting the mandatory `Host` and `Content-Length` (or `Transfer-Encoding`) + headers. +- Getting a connection from the pool for a specified URI. +- Writing a `Request` to the connection and reading a `Response`. +- Raising a `StatusError` of the Response Status is not in the `2xx` range. + +If the `Body` of the `Request` is connected to an `IO` stream, the `request` +function waits for the Response Headers to be recieved and schedules reading of +the the Response Body to happen as a background task. + + +### Request Execution With Retry + +Source: [`RetryRequest.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/RetryRequest.jl) + +The `RetryRequest` module implements a `request` function that accepts the +same arguments as, and wraps, +[`HTTP.SendRequest.request(method::String, uri, headers, body)`](@ref)). + +This layer adds a retry loop that repeats the `request` in the event of a +recoverable network error. A randomised exponentially increasing delay is +introduced between attempts to avoid making network congestion worse. + +Methods of `isrecoverable(e)` define which exception types lead to a retry: +`Base.UVError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` +(if status is `1xx` or `5xx`). + +### Request Execution With State + +Source: [`CookieRequest.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/CookieRequest.jl) + +The `CookieRequest` module implements a `request` function that accepts the +same arguments as, and wraps the `RetryRequest.request` function. + +This layer adds processing of client-side cookies, basic authorization headers +and `3xx` redirects. + + +# Internal Interfaces + +## Parser Interface + +```@docs +HTTP.Parser +HTTP.Parsers.Message +HTTP.Parsers.parse! +Base.read!(::IO, ::HTTP.Parser) +HTTP.Parsers.messagecomplete +HTTP.Parsers.headerscomplete +HTTP.Parsers.waitingforeof +``` + +## Messages Interface + +### Message + +`const Message = Union{Request,Response}` + +```@docs +HTTP.Messages.header +HTTP.Messages.setheader +HTTP.Messages.defaultheader +HTTP.Messages.setlengthheader +HTTP.Messages.appendheader +HTTP.Messages.waitforheaders +Base.write(::IO,::Union{HTTP.Messages.Request, HTTP.Messages.Response}) +HTTP.Messages.readstartline! +``` + +### Request + +```@docs +HTTP.Messages.Request +``` + +### Response + +```@docs +HTTP.Messages.Response +HTTP.Messages.iserror +HTTP.Messages.isredirect +HTTP.Messages.method +HTTP.Messages.parentcount +``` + +### Body + +```@docs +HTTP.Messages.Bodies.Body +HTTP.Messages.Bodies.isstream +Base.write(::HTTP.Messages.Bodies.Body, ::Any) +Base.write(::IO, ::HTTP.Messages.Bodies.Body) +Base.length(::HTTP.Messages.Bodies.Body) +Base.take!(::HTTP.Messages.Bodies.Body) +HTTP.Messages.Bodies.set_show_max +HTTP.Messages.Bodies.collect! +``` + + +## Connections Interface + +### Low Level Connect Interface + +```@docs +HTTP.Connect.getconnection(::Type{TCPSocket},::AbstractString,::AbstractString) +``` + +### Connection Pooling Interface + +```@docs +HTTP.Connections.Connection +HTTP.Connections.pool +HTTP.Connect.getconnection(::Type{HTTP.Connections.Connection{T}},::AbstractString,::AbstractString) where T <: IO +HTTP.IOExtras.unread!(::HTTP.Connections.Connection,::SubArray{UInt8, 1}) +HTTP.IOExtras.closewrite(::HTTP.Connections.Connection) +HTTP.IOExtras.closeread(::HTTP.Connections.Connection) +``` + + +## Low Level Request Interface + +```@docs +HTTP.SendRequest.request(::String,::Any,::Any,::Any) +HTTP.SendRequest.request(::HTTP.URIs.URI,::HTTP.Messages.Request,::HTTP.Messages.Response) +HTTP.SendRequest.request(::IO,::HTTP.Messages.Request,::HTTP.Messages.Response) +``` diff --git a/src/Bodies.jl b/src/Bodies.jl index 6ba486cdf..29173d794 100644 --- a/src/Bodies.jl +++ b/src/Bodies.jl @@ -3,30 +3,24 @@ module Bodies export Body, isstream -""" - set_show_max(x) - -Set the maximum number of bytes to be displayed by `show(::IO, ::Body)` -""" - -set_show_max(x) = global body_show_max = x -body_show_max = 1000 - - """ Body Represents a HTTP Message Body. -If `io` is set to `notastream`, then `buffer` contains static Message Body data. -Otherwise, `io` is a stream to/from which Message Body data is written/read. +- `stream::IO` +- `buffer::IOBuffer` +- `length::Int` + +If `stream` is set to `notastream`, then `buffer` contains static Message Body data. +Otherwise, `stream` is a stream to/from which Message Body data is written/read. In streaming mode: `length` keeps track of the number of bytes that have passed -through `io`; and `buffer` keeps a cache of the first part of the Message Body +through `stream`; and `buffer` keeps a cache of the first part of the Message Body (for display purposes). See `show` and `set_show_max`). """ mutable struct Body - io::IO + stream::IO buffer::IOBuffer length::Int end @@ -37,8 +31,8 @@ const unknownlength = -1 """ Body() - Body(data) - Body(::IO) + Body(data [, length]) + Body(::IO, [, length]) `Body()` creates an empty HTTP Message `Body` buffer. The `write(::Body)` function can be used to append data to the empty `Body`. @@ -55,14 +49,40 @@ write(socket, b) `Body(data)` creates a `Body` with fixed content. `Body(::IO)` creates a streaming mode `Body`. This can be used to stream either -Request Messages or Response Messages. `write(io, body)` reads data from -the `body`'s stream and writes it to the `io` target. `write(body, data)` writes -data to the `body`'s stream. +Request Messages or Response Messages. `write(io, ::Body)` reads data from +the `Body`'s stream and writes it to `io`. `write(::Body, data)` writes +data from to the `Body`'s stream. + +If `length` is unknown, `write(io, body)` uses chunked Transfer-Encoding. + +e.g. Send a Request Body using chunked Transfer-Encoding: + +``` +io = open("bigfile.dat", "r") +write(socket, Body(io)) +``` + +e.g. Send a Request Body with known length: + +``` +io = open("bigfile.dat", "r") +write(socket, Body(io, filesize("bigfile.dat"))) +``` + +e.g. Send a Response Body to a stream: + +``` +io = open("response_file", "w") +b = Body(io) +while !eof(socket) + write(b, readavailable(socket)) +end +``` """ Body() = Body(notastream, IOBuffer(), unknownlength) Body(buffer::IOBuffer, l=unknownlength) = Body(notastream, buffer, l) -Body(io::IO, l=unknownlength) = Body(io, IOBuffer(body_show_max), l) +Body(stream::IO, l=unknownlength) = Body(stream, IOBuffer(body_show_max), l) Body(::Void) = Body() Body(data, l=unknownlength) = Body(notastream, IOBuffer(data), l) @@ -73,7 +93,7 @@ Body(data, l=unknownlength) = Body(notastream, IOBuffer(data), l) Is this `Body` in streaming mode? """ -isstream(b::Body) = b.io != notastream +isstream(b::Body) = b.stream != notastream """ @@ -99,8 +119,8 @@ function collect!(body::Body) io = IOBuffer() write(io, body) body.buffer = io - close(body.io) - body.io = notastream + close(body.stream) + body.stream = notastream end @assert !isstream(body) return view(body.buffer.data, 1:body.buffer.size) @@ -118,6 +138,13 @@ function Base.take!(body::Body) take!(body.buffer) end + +""" + write(::IO, ::Body) + +Write data from `Body`'s `buffer` or `stream` to an `IO` stream, +""" + function Base.write(io::IO, body::Body) if !isstream(body) @@ -140,8 +167,8 @@ function Base.write(io::IO, body::Body) end # Read from `body.io` until `eof`, write to `io`. - while !eof(body.io) - v = readavailable(body.io) + while !eof(body.stream) + v = readavailable(body.stream) if body.buffer.size < body_show_max write(body.buffer, v) end @@ -152,8 +179,8 @@ end function writechunked(io::IO, body::Body) - while !eof(body.io) - v = readavailable(body.io) + while !eof(body.stream) + v = readavailable(body.stream) if body.buffer.size < body_show_max write(body.buffer, v) end @@ -164,6 +191,13 @@ function writechunked(io::IO, body::Body) end +""" + write(::Body, data) + +Write data to the `Body`'s `stream`, +or append it to the `Body`'s `buffer`. +""" + function Base.write(body::Body, v) if !isstream(body) @@ -173,12 +207,22 @@ function Base.write(body::Body, v) if body.length < body_show_max write(body.buffer, v) end - n = write(body.io, v) + n = write(body.stream, v) body.length += n return n end -Base.close(body::Body) = if isstream(body); close(body.io) end +Base.close(body::Body) = if isstream(body); close(body.stream) end + + +""" + set_show_max(x) + +Set the maximum number of bytes to be displayed by `show(::IO, ::Body)` +""" + +set_show_max(x) = global body_show_max = x +body_show_max = 1000 """ @@ -192,8 +236,8 @@ function Base.show(io::IO, body::Body) bytes = head(body) write(io, bytes) println(io, "") - if isstream(body) && isopen(body.io) - println(io, "⋮\nWaiting for $(typeof(body.io))...") + if isstream(body) && isopen(body.stream) + println(io, "⋮\nWaiting for $(typeof(body.stream))...") elseif length(body) > length(bytes) println(io, "⋮\n$(length(body))-byte body") elseif length(body) == unknownlength @@ -201,4 +245,5 @@ function Base.show(io::IO, body::Body) end end + end #module Bodies diff --git a/src/Connections.jl b/src/Connections.jl index 5722345d3..938959419 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -13,19 +13,22 @@ const ByteView = typeof(view(UInt8[], 1:0)) """ - Connection + Connection{T <: IO} A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. -The `excess` field contains left over bytes read from the connection after -the end of a response message. These bytes are probably the start of the -next response message. - -The `readcount` and `writecount` keep track of the number of Request/Response -Messages that have been read/written. `writecount` is allowed to be no more -than two greater than `readcount` (see `isbusy`). -i.e. after two Requests have been written to a `Connection`, the first -Response must be read before another Request can be written. +- `host::String` +- `port::String` +- `io::T` +- `excess::ByteView`, left over bytes read from the connection after + the end of a response message. These bytes are probably the start of the + next response message. +- `writecount::Int` number of Request Messages that have been written. + `writecount` is allowed to be no more than two greater than `readcount` + (see `isbusy`). i.e. after two Requests have been written to a `Connection`, + the first Response must be read before another Request can be written. +- `readcount::Int`, number of Response Messages that have been read. +- `readdone::Condition`, signals that an entire Response Messages has been read. """ mutable struct Connection{T <: IO} <: IO @@ -74,7 +77,7 @@ end """ unread!(::Connection, bytes) -Push bytes back into a connection (to be returned by the next read). +Push bytes back into a connection's `excess` buffer (to be returned by the next read). """ function IOExtras.unread!(c::Connection, bytes::ByteView) diff --git a/src/HTTP.jl b/src/HTTP.jl index a1a7a5322..ae3f8f890 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -1,8 +1,6 @@ __precompile__(true) module HTTP -export Request, Response, FIFOBuffer - using MbedTLS import MbedTLS.SSLContext const TLS = MbedTLS @@ -11,7 +9,7 @@ import Base.== const DEBUG = false # FIXME rm const PARSING_DEBUG = false # FIXME rm -const DEBUG_LEVEL = 2 +const DEBUG_LEVEL = 1 if VERSION > v"0.7.0-DEV.2338" using Base64 @@ -43,7 +41,6 @@ include("cookies.jl") using .Cookies include("multipart.jl") -include("Bodies.jl") include("Parsers.jl") import .Parsers.ParsingError include("Messages.jl") diff --git a/src/Messages.jl b/src/Messages.jl index 0f70d2162..bbb4acef8 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -1,30 +1,34 @@ module Messages export Message, Request, Response, Body, - method, iserror, isredirect, parentcount, + method, iserror, isredirect, parentcount, isstream, header, setheader, defaultheader, setlengthheader, waitforheaders import ..HTTP +include("Bodies.jl") +using .Bodies + using ..Pairs -using ..IOExtras -using ..Bodies using ..Parsers import ..Parsers import ..@debug, ..DEBUG_LEVEL -import MbedTLS.SSLContext - """ Request Represents a HTTP Request Message. -The `parent` field refers to the `Response` (if any) that led to this request -(e.g. in the case of a redirect). +- `method::String` +- `uri::String` +- `version::VersionNumber` +- `headers::Vector{Pair{String,String}}` +- `body::`[`HTTP.Body`](@ref) +- `parent::Response`, the `Response` (if any) that led to this request + (e.g. in the case of a redirect). """ mutable struct Request @@ -53,11 +57,15 @@ mkheaders(x) = [string(k) => string(v) for (k,v) in x] Represents a HTTP Response Message. -The `parent` field refers to the `Request` that yielded this `Response`. - -The `headerscomplete` `Condition` is raised when the `Parser` has finished -reading the response headers. This allows the `status` and `header` fields to -be read used asynchronously without waiting for the entire body to be parsed. +- `version::VersionNumber` +- `status::Int16` +- `headers::Vector{Pair{String,String}}` +- `body::`[`HTTP.Body`](@ref) +- `parent::Request`, the `Request` that yielded this `Response`. +- `headerscomplete::Condition`, raised when the `Parser` has finished + reading the response headers. This allows the `status` and `header` fields + to be read used asynchronously without waiting for the entire body to be + parsed. """ mutable struct Response @@ -80,12 +88,18 @@ const Message = Union{Request,Response} """ iserror(::Response) - isredirect(::Response) -Does this `Response` have an error or redirect status? +Does this `Response` have an error status? """ iserror(r::Response) = r.status < 200 || r.status >= 300 + + +""" + isredirect(::Response) + +Does this `Response` have a redirect status? +""" isredirect(r::Response) = r.status in (301, 302, 307, 308) @@ -114,7 +128,7 @@ end """ - statustext(::Response) + statustext(::Response) -> String `String` representation of a HTTP status code. e.g. `200 => "OK"`. """ @@ -123,7 +137,7 @@ statustext(r::Response) = Base.get(Parsers.STATUS_CODES, r.status, "Unknown Code """ - waitforheaders(::Response) + waitforheaders(::Response) Wait for the `Parser` (in a different task) to finish parsing the headers. """ @@ -132,7 +146,7 @@ waitforheaders(r::Response) = while r.status == 0; wait(r.headerscomplete) end """ - header(message, key [, default=""]) + header(::Message, key [, default=""]) -> String Get header value for `key`. """ @@ -141,7 +155,7 @@ lceq(a,b) = lowercase(a) == lowercase(b) """ - setheader(message, key => value) + setheader(::Message, key => value) Set header `value` for `key`. """ @@ -149,7 +163,7 @@ setheader(m, v::Pair) = setbyfirst(m.headers, Pair{String,String}(v), lceq) """ - defaultheader(message, key => value) + defaultheader(::Message, key => value) Set header `value` for `key` if it is not already set. """ @@ -183,18 +197,18 @@ end """ - appendheader(message, key => value) + appendheader(::Message, key => value) Append a header value to `message.headers`. If `key` is `""` the `value` is appended to the value of the previous header. -If `key` is the same as the previous header, the `vale` is appended to the -value of the previous header with a comma delimiter. -https://stackoverflow.com/a/24502264 +If `key` is the same as the previous header, the `vale` is [appended to the +value of the previous header with a comma +delimiter](https://stackoverflow.com/a/24502264) -`Set-Cookie` headers are not comma-combined because cookies often contain -internal commas. https://tools.ietf.org/html/rfc6265#section-3 +`Set-Cookie` headers are not comma-combined because cookies [often contain +internal commas](https://tools.ietf.org/html/rfc6265#section-3). """ function appendheader(m::Message, header::Pair{String,String}) @@ -212,7 +226,7 @@ end """ - httpversion(Message) + httpversion(::Message) e.g. `"HTTP/1.1"` """ @@ -221,7 +235,7 @@ httpversion(m::Message) = "HTTP/$(m.version.major).$(m.version.minor)" """ - writestartline(::IO, message) + writestartline(::IO, ::Message) e.g. `"GET /path HTTP/1.1\\r\\n"` or `"HTTP/1.1 200 OK\\r\\n"` """ @@ -238,7 +252,7 @@ end """ - writeheaders(::IO, message) + writeheaders(::IO, ::Message) Write a line for each "name: value" pair and a trailing blank line. """ @@ -253,7 +267,7 @@ end """ - write(::IO, message) + write(::IO, ::Message) Write start line, headers and body of HTTP Message. """ @@ -267,9 +281,9 @@ end """ - readstartline(message, p::Parser) + readstartline!(::Message, p::Parsers.Message) -Read the start-line metadata from `Parser` into a `message` struct. +Read the start-line metadata from Parser into a `::Message` struct. """ function readstartline!(r::Response, m::Parsers.Message) @@ -291,55 +305,14 @@ function readstartline!(r::Request, m::Parsers.Message) end -""" - read!(io, parser) - -Read data from `io` into `parser` until `eof` -or the parser finds the end of the message. -""" - -function Base.read!(io::IO, p::Parser) - - while !eof(io) - bytes = readavailable(io) - if isempty(bytes) - @debug 1 "Bug https://github.com/JuliaWeb/MbedTLS.jl/issues/113 !" - @assert isa(io, SSLContext) - @assert eof(io) - break - end - @assert length(bytes) > 0 - - n = parse!(p, bytes) - @assert n == length(bytes) || messagecomplete(p) - @assert n <= length(bytes) - @debug 3 "p.state = $(Parsers.ParsingStateCode(p.state))" - - if messagecomplete(p) - excess = view(bytes, n+1:length(bytes)) - if !isempty(excess) - unread!(io, excess) - end - return - end - end - - if eof(io) && !waitingforeof(p) - throw(ParsingError(headerscomplete(p) ? Parsers.HPE_BODY_INCOMPLETE : - Parsers.HPE_HEADERS_INCOMPLETE)) - end - return -end - - """ Parser(::Message) Create a parser that stores parsed data into a `Message`. """ -function Parser(m::Message) +function Parsers.Parser(m::Message) p = Parser() - p.onbody = x->write(m.body, x) + p.onbodyfragment = x->write(m.body, x) p.onheader = x->appendheader(m, x) p.onheaderscomplete = x->readstartline!(m, x) p.isheadresponse = (isa(m, Response) && method(m) in ("HEAD", "CONNECT")) @@ -349,7 +322,7 @@ end """ - read!(io, message) + read!(::IO, ::Message) Read data from `io` into a `Message` struct. """ diff --git a/src/Parsers.jl b/src/Parsers.jl index fa2ba0a9f..fc12f578f 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -47,6 +47,12 @@ const enable_passert = false # See macro @passert Message HTTP Message metadata. +- `method::Method` +- `major::Int16` +- `minor::Int16` +- `url::String` +- `status::Int32` +- `upgrade::Bool` """ mutable struct Message @@ -70,9 +76,9 @@ The `Parser` must be configured with output processing callbacks: - `onheader = f(::Pair{String,String})` is called for each Header Line. -- Body data is passed to `onbody = f(::SubArray{UInt8,1})`. +- Body data is passed to `onbodyfragment = f(::SubArray{UInt8,1})`. If the Message is chunked or if the Message is passed to `parse!` - in multiple fragments, then `obbody` will be called multiple times. + in multiple fragments, then `onbodyfragment` will be called multiple times. - `onheaderscomplete = f(::Message)` is called at the end of the Header. @@ -85,7 +91,6 @@ e.g. p = Parser() p.onheaderscomplete = m -> (@show string(m.method); @show m.url) p.onheader = h -> @show h -end parse!(p, \"\"\" GET /foo HTTP/1.1 @@ -106,7 +111,7 @@ mutable struct Parser # config isheadresponse::Bool # Are we parsing a HEAD Response Message? onheader::Function#(::Pair{String,String} - onbody::Function#(::SubArray{UInt8,1}) + onbodyfragment::Function#(::SubArray{UInt8,1}) onheaderscomplete::Function#(::Message) # state @@ -135,7 +140,7 @@ Parser() = Parser(false, x->nothing, x->nothing, ()->nothing, """ - read!(io, ::Parser) + read!(io, ::Parser [, unread=IOExtras.unread!]) Read data from `io` into the `Parser` until `eof` or until the parser finds the end of the message. @@ -143,6 +148,8 @@ or until the parser finds the end of the message. If `readavailable(io)` reads past the end of the Message the excess bytes are passed to `unread`. This is handled transparently if there is a suitable `IOExtras.unread!(::IO, SubArray{UInt8, 1})` method defined. + +Throws `ParsingError` if input is invalid. """ function Base.read!(io::IO, p::Parser; unread=IOExtras.unread!) @@ -185,7 +192,7 @@ function reset!(p::Parser) # config p.isheadresponse = false p.onheader = x->nothing - p.onbody = x->nothing + p.onbodyfragment = x->nothing p.onheaderscomplete = x->nothing # state @@ -1098,7 +1105,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int @passert parser.content_length != 0 && parser.content_length != ULLONG_MAX - parser.onbody(view(bytes, p:p + to_read - 1)) + parser.onbodyfragment(view(bytes, p:p + to_read - 1)) # The difference between advancing content_length and p is because # the latter will automaticaly advance on the next loop iteration. @@ -1113,7 +1120,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int # read until EOF elseif p_state == s_body_identity_eof - parser.onbody(view(bytes, p:len)) + parser.onbodyfragment(view(bytes, p:len)) p = len elseif p_state == s_chunk_size_start @@ -1176,7 +1183,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int @passert parser.content_length != 0 && parser.content_length != ULLONG_MAX - parser.onbody(view(bytes, p:p + to_read - 1)) + parser.onbodyfragment(view(bytes, p:p + to_read - 1)) # See the explanation in s_body_identity for why the content # length and data pointers are managed this way. diff --git a/src/SendRequest.jl b/src/SendRequest.jl index 14d1fedba..b0afc0422 100644 --- a/src/SendRequest.jl +++ b/src/SendRequest.jl @@ -7,7 +7,6 @@ import ..HTTP using ..Pairs.getkv using ..URIs using ..Messages -using ..Bodies using ..Connections using ..IOExtras @@ -74,9 +73,11 @@ end Execute a `Request` and return a `Response`. -`parent=` optionally set a parent `Response`. +kw args: -`response_stream=` optional `IO` stream for response body. +- `parent=` optionally set a parent `Response`. + +- `response_stream=` optional `IO` stream for response body. e.g. use a stream as a request body: @@ -100,7 +101,7 @@ println(stat("response_file").size) """ function request(method::String, uri, headers=[], body=""; - bodylength=Bodies.unknownlength, + bodylength=Messages.Bodies.unknownlength, parent=nothing, response_stream=nothing, kw...) diff --git a/src/server.jl b/src/server.jl index fb32023ef..3294f863e 100644 --- a/src/server.jl +++ b/src/server.jl @@ -173,7 +173,7 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, end HTTP.reset!(parser) request = HTTP.Request() - parser.onbody = x->write(request.body, x) + parser.onbodyfragment = x->write(request.body, x) parser.onheader = x->HTTP.appendheader(request, x) else if !any(x->x[1] == "Connection", response.headers) @@ -258,7 +258,7 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} while true p = HTTP.Parser() request = HTTP.Request() - p.onbody = x->write(request.body, x) + p.onbodyfragment = x->write(request.body, x) p.onheader = x->HTTP.appendheader(request, x) try diff --git a/test/.body.jl.swp b/test/.body.jl.swp index 894916391eb84e63604e35a0d0024b8bb1b0f94f..9a9250d42185a07fa71edf6c94e94997975a8335 100644 GIT binary patch delta 334 zcmX}nJxD@v6vpBAN<%ljUB|XsgdnkqqOqU|QiO)k$Osgyr6TyZXCKfk(U3a0IfOyj z96^hVL5PHNLs&r+(GY$8BJza?&dWK=$rP$!@J(Sy^7^ktXMX&G`Cs!U*(9QHi>QG(M7s8eCaFzOuN*{Q^Gs;-OAOc(Z#w(ss#W_x}hczrC3LC!> zPNR-%RB?t=>_)VqRYDwN=tm!#J<>Zaae-qTU>^=F$J9m{9O?eoT6&G1~sMVA$El*R-O36bejq;o`>z{*^hl|2V ze+MV-oG7%5rd&l*rX0NYt5^LF-@Xr@wVbk+Q(R;ISg6O{(hv}yL*&fYIGFu7*IlkF zk{X@L9D6T1!ww~)WgID&v05(@Rn7jp$WpOL5^)T}iI*agZ8XD*8zcR&iCL5)ZxG31 z5ot_d3{lP7dPn?|3t4ieW=jwUE1W(Zuvj-URe(S delta 443 zcmWmA&ntrg9LMqRE+t}UuAJA}ql(1zy1vKgX06B)QkaJuZ#EGfK{VptDsq8+H= z+~F1n$RdIvwBff-3fo3mp@}-q zV8Or$`lj@Z7H&{CwV@-2CHLd-VJ{+lk2#% aKVieIcwTX@;`s$9^B<>Gr&o1Cje|d*dOBqQ delta 293 zcmWN@KS%;`07vn!QetUhZ3W()f#3=Ka0r5+glcI>8X}<~F?13K5n3~vYKtRs5TdoF zxc)4`-Q*Zetqm^W;?g25y$2t7-7wY-WA;h$aCgsHGpnMs)SsB6VE7n)K98tVYhB;x zo_0cedK8x|Z^o&dxhW}+c|`F$Ck^q5SKQ$er>J5dB@~fC3V%uI2OoIH1A4fHg(WOv zl#sp<;03*e%DQ5Xaey+mQNkuRu#POov(f;4G*Cwl%Wz=B@>jCoanCI*cw;x?yM^?W Pw`H7*rg>|c$W8kn?0_{< diff --git a/test/body.jl b/test/body.jl index 729ce345d..04fff864e 100644 --- a/test/body.jl +++ b/test/body.jl @@ -1,4 +1,4 @@ -using HTTP.Bodies +using HTTP.Messages @testset "HTTP.Bodies" begin @@ -42,12 +42,12 @@ using HTTP.Bodies show(buf, b) @test String(take!(buf)) == "Hello!\nWorld!\n" - tmp = HTTP.Bodies.body_show_max - HTTP.Bodies.set_show_max(12) + tmp = HTTP.Messages.Bodies.body_show_max + HTTP.Messages.Bodies.set_show_max(12) b = Body("Hello World!xxx") #display(b); println() buf = IOBuffer() show(buf, b) @test String(take!(buf)) == "Hello World!\n⋮\n15-byte body\n" - HTTP.Bodies.set_show_max(tmp) + HTTP.Messages.Bodies.set_show_max(tmp) end diff --git a/test/client.jl b/test/client.jl index 19b511566..9d4d9f0e2 100644 --- a/test/client.jl +++ b/test/client.jl @@ -56,7 +56,7 @@ for sch in ("http", "https") r = HTTP.get("$sch://httpbin.org/stream/100"; stream=true) @test HTTP.status(r) == 200 - b = [JSON.parse(l) for l in eachline(r.body.io)] + b = [JSON.parse(l) for l in eachline(r.body.stream)] @test a == b end @@ -170,9 +170,11 @@ for sch in ("http", "https") println("high-level client request methods") buf = IOBuffer() cli = HTTP.Client(buf) +#= FIXME HTTP.get(cli, "$sch://httpbin.org/ip") seekstart(buf) @test length(String(take!(buf))) > 0 +=# r = HTTP.request("$sch://httpbin.org/ip") @test HTTP.status(r) == 200 diff --git a/test/parser.jl b/test/parser.jl index 4a7ece786..c9fd130bf 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -16,7 +16,7 @@ const Headers = Vector{Pair{String,String}} ==(a::Request,b::Request) = (a.method == b.method) && (a.version == b.version) && (a.headers == b.headers) && - (HTTP.Bodies.collect!(a.body) == HTTP.Bodies.collect!(b.body)) + (HTTP.Messages.Bodies.collect!(a.body) == HTTP.Messages.Bodies.collect!(b.body)) mutable struct Message name::String From 9ae07c5966f48ed83242844e44f6ad505b038dfa Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 12 Dec 2017 15:25:40 +1100 Subject: [PATCH 043/182] Whoops --- test/.body.jl.swp | Bin 20480 -> 0 bytes test/.client.jl.swp | Bin 28672 -> 0 bytes test/.cookies.jl.swp | Bin 16384 -> 0 bytes test/.handlers.jl.swp | Bin 12288 -> 0 bytes test/.messages.jl.swp | Bin 28672 -> 0 bytes test/.parser.jl.swp | Bin 106496 -> 0 bytes test/.runtests.jl.swp | Bin 12288 -> 0 bytes test/.server.jl.swp | Bin 12288 -> 0 bytes test/.types.jl.swp | Bin 12288 -> 0 bytes test/.uri.jl.swp | Bin 36864 -> 0 bytes test/.utils.jl.swp | Bin 12288 -> 0 bytes 11 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/.body.jl.swp delete mode 100644 test/.client.jl.swp delete mode 100644 test/.cookies.jl.swp delete mode 100644 test/.handlers.jl.swp delete mode 100644 test/.messages.jl.swp delete mode 100644 test/.parser.jl.swp delete mode 100644 test/.runtests.jl.swp delete mode 100644 test/.server.jl.swp delete mode 100644 test/.types.jl.swp delete mode 100644 test/.uri.jl.swp delete mode 100644 test/.utils.jl.swp diff --git a/test/.body.jl.swp b/test/.body.jl.swp deleted file mode 100644 index 9a9250d42185a07fa71edf6c94e94997975a8335..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI4eT*Ds9ml6FmX}hYU?LbX&+Y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eR>w{m*6=dJ zpLfb~VM|$$M?>C7)kM*E9ej_c!Yjbg>ohT1QDc>vS4nn%w;~pw3MZ61l&STE!W}y- T^zqD7OMR`$KYfZk7PS8W)~itL diff --git a/test/.client.jl.swp b/test/.client.jl.swp deleted file mode 100644 index d94bc7515565b7d42edb879ad1f2bcb46a24e382..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 28672 zcmeI44UAmJUBEY?NogH7siP_gfq34Fo);;iFQJDnZpG7*HERK&S!{A+<@R zROR=dnfLbNcJJ0-6qonx-|pM@X6DWOXWq>G|MQ_dx3fJznc=u8lQ}n9c>nqT zdh*(5K6FhcbZc^T&1253g`?wJM`!D$%IMv0=}zB2nXmdKx7r!?10+~D(X4uI;nY}u zTj9X`{JnX(7O5~Ql&YSpNBI+7kvfw|f29&gC2%nkXhsX8Q|mJmTgJyE`cQVGbMuD7 z7n4#NCzU`dfm8yi1X2m45=bSGN+6X$DuJt10#W0-%rBAmYi<75+3#z*zJJXA-eX;0ZVdhhZM>gv~c(GOzI9$KW$?5OVPM@8tRLarhXV zfLq`P?;tPmEc_NUU=M79H-0>mc^ZBbny>-h4KKbullf)%5JYefya&F6VdBr>G#r83 z;ClEf28pNPkD&~^;ius(3>`1Sm*5=y3j89Rg>m>khLS&p`{2FsO$;k9z~|sm*b5`D z9{vI&%k%IwJPD71Gwo=%i2bws9gZRSaQ4q0xqBufXG7-GDOH;Flc9G;5rbp5cu<&# zVdO^5FsBn~_+gaG-WrxFI|_wL6g7%oJ?{sL1-X&k=@k9)^7O*+urodFY#AHVNiDSH z;Lx4Tg#{JlvK6(Q9oDx?RXpEuh0$G0 zffuRV!iZyXG~$o}ek|F{@r$UDsx|6vP35jo)@D1!veOBbI*HhWNaUiWbSdIEJX?>Z z9^72nJhEBA^5$cm;<=Qxjo3P*-3YvTRITT-#{WjQ2}jz8S6_7e1?Q9-cz!cA(TEf3>S&iK~+m{U}XUOf{hT~&1w=W{Wt^hsl7tppunmZSm_(a%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6Q37lKDv@V@eayQw1 z2MUc~$Guq)1I~tNEA_cuZoovL;{TTULEZK0zTI^!uj9E5&FlF=e`lvGot7zFFYb?f zhZReew`n9fL0e`j(NLN))jT>ToPu|G*~e1COzgp9d$5Q!=7ZZCGuL&>%BR>%=3}D< zLlVksIm|s13L#$%dpNyJ^ibl+UGyc<-cZS8Pxn1|2qFNJ3|kVEVX5NUx*MF|(hewf z(++CL81TMB@8`8`-1mpY2w}}mMg?`p^BQW??8%vMMc)(hP=8~3KhE3JhR2*kwqZ_t z{Ox4IvHK}q9%*(m`SDBXGFh?`#swokFG?tg?VD~OJ$)FI6VX4Xvf*2NU~hVKE*+ZU z2s)05GSn|!KZceM)smLI3{fvSs_WBd;Y`}~T*@AzAvoK!7m`98KxpL-b&B-BMnY=P zu7Z(8N(>rmwF<`?>J`#fj7X6bEp0<%w6t3#R7tY0LQj^A1PWPe21!3;TZKkUBY9Ao z+o0f_pES4(T+bs@}Qbt)Q z67LDjhtbM&E#Jm~d^JH{Qw7nCvRKydfZ oN{7#TNj84uo04uqQIZ}2^8=|Gs&KP-NQDC7JT_L?Y+RrJ0EUAX82|tP diff --git a/test/.handlers.jl.swp b/test/.handlers.jl.swp deleted file mode 100644 index af65eea6a9615874803d3d2c2ac42279615b074d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2J7^R^7{@08W1>byD=p$?)9i-a?%qQsMS=*%BoGXjL^P&wb2s;hm)*_n3qO|K+nI0X_s!#u+j-;c z*o1UkOE4V!8GA9_GkfFHgZ9VQ+L$mGy}J#MDRWUoqDLcRRw@^nFjFUO`;u0$Q)Xdx z(JQD>UnmvwroL=w$Mn(Z=}FDI=W^ldInzoPxFfWMHN{p}LRVyf3^Zw=o5f>M!z2V;kKqVf3uzx@0E<37edfcxM)I0r^RA9%Z$ zu{Yo$xDEsu2VI~OeC}rK5x51Gz#P!P*FB7V0q?*QumaLx82sLiI>BvVfe6?Ep6_Dp zE|>!e@V$$%Z{R(62JV407zDpM8G8j@f~VjPxBzCsAoz(s{s6DR3b+X_f+L^@cn%H$ zI>-PSAOmFJe>T7^Y>g+pSJNc_MYZ7lG{qT^9KJLV+)Hv%v@O9ERh5Q@*4&F3ah2g& zt>omT@WYwpm?8)7y9v|LvpL&!OkMD0Zpmt^;bINKEr;=1j}&bbsr3*>oe-!UUdv9C ze5r&iDe&hf%Hi3(aJkd##XvD!7$i$nB6I1KN>6cMEQeLai-pBA%4xO}1aN@^ufS=IwNwH`yn;3=0sXAt|D)tk-Dl=Bx_qce>5f z604#`uA<$~ef6csA6=E*Vk29)zX~Zq0cB?ctN;K2 diff --git a/test/.messages.jl.swp b/test/.messages.jl.swp deleted file mode 100644 index 30a6125806147b73c0313d2d8e15833a685e8dbc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 28672 zcmeI4dyFJUdBEE?_yv9t2=Ip^_s*^C9@w4Ty_dzD_k0i5zBPBZ=i9Y`TOUX7Oz%#6 zXQpSndv13RNPGwx1X$u_LWmN9O~7C&AYlc^*opEmfL1MW+IUHBS24IhJd!AWRC4XSVg_P|B(Uu+)z68;?i2;L3vfXyC3)6I^VgVUuXNZYLe zFN}uVO4OVW7mAKN4I_8lmFLsb2lq$KG+0T!qBGxaR?;wPO4MdhNo#4^O1yR)mdke@ zoiq~0yXlbU&HMEvC>9+jXjYZ%EU~u`q<*y;ht2uOt3^rL^c#T)D;9^`mLDg9H`z?N zjN()WuU~*p{<`FyY+`B_H{IA#nZ_j~Q>T`kvyeuR)}pEk)R$TK$skUomgDYS`s#4I zF&D&MtJLuCiDHUWYJ|-wrcN$L9QakOK@-v{Ma>$aL5z%*;oW|#MdfvfNBR|AREahh zRabSS=h%RE(-_`#roO^C*nAYJ@t2Dr4qDcWO;=Gy}!_kg9e(812>4~3k)7n zM>sEc4Ar80!wv4hvLB{lb74M;CA5`w#(ZXb{Dh8^y7c66w;nVX(waB7iu+g5-QwOl z`G!LiZq092>w(*DN@sF!(H&Yc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vU!d# zPQ9;CYAV~OdNhTnBW5NG$){sU?RFy(uyIG%cF67+8FKqHYRn?J;7%q|o5UE*a+!3+ zQK`FdYl@5o?``6AqY8=@nt>$_BeMk=TQU{PnbyxusuER>{?4O^cHXFl6~$3evp@M3 z!bFHg_}r~?HqDAy?eMhTj4y$ip4@#;bLxY=xQFBnpS3+BX_FPYbOp) z%uGnVkL;bbzYxg zE2pwFUfsA=vW}EZv<&0*#-51F)h|C05_C_7TR_l z`^)N~_#h2}R>bv;S3=hBxT5ioX0He{6P{Ia&J4ZQM!h!Mvu7vm zvt>_GiM}|B|un8y|(dd4GcxnIl3sg~~)Sn(cAK5qFEdGWk#E zymKUQU)Cpexmcy7xT@mvIv>V-Y^-QMhg>4*VN1r5L_JkG=I!g+F=U;b^cden)V+*Y zC03UzbD$w(D$sE)IfgSFzLQ!>>j=%9OjMlBh}%$*c2+qiWs>#(6Rf?jWvwpje>utV z7p(7P%^$#4cm-Sr&k*6S-~mYB2)qW0pk(Oe_OI1|)qvH2)qvH2)qvH2)qvH2)qvH2 z)xfX024taa4!u@o!;RldI3`fe{hMSG7P_xyW6qJw{E?G^bt0^0i}QSxK8pmq?fIv& z1yz^*U)jKrUw<6J4K^h8Z&NlD z&JPvWNIyq;&~k{0z2{H-^ONctcs2D;BTD x|HHamb^DKAGXHNlt2y0F`Hz2a{h~!ue=!)y{uiv?&4vppSFeQsbB8$Re*j`WCc6Lt diff --git a/test/.parser.jl.swp b/test/.parser.jl.swp deleted file mode 100644 index e6d2c2b5a653c2e0538133fbdaba63504c37f04f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 106496 zcmeIb2Yh2$b@*>eXrXO@g^+*Xt!8)e?AVs%Zq0aiWm_&|%e~Ib;>fZrYb;A170dRr zv_L2cJrD>b0Rjmv5FiEEKwv4MhLZ4u1Og!eLJBRAKtlO{&wcMcz4uhc({@Qd((ZR8 zzjt3dx4e7KJ@?$Tz~Wfc=xOV0;`899rkA$(Prc<^Z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R!~VkdL(jv$G896wYSGQJ4uhWc!)k8KRzp=RjImY~wj5@>^+DaFr#8l^0REn~V6W81RA+B@>rT9tAhX52 z)?}u5)JUh+oJpow&H|ICABXR@-lTEBZk(dp+dA4hBx!0rBR*1fs$NIAc3D+VqWe^- zbJpg&u(ElK++{PTmD5!+nt_0Ayp&|Noto8X#5R~2#593)Hq#{H=i@4lPT5{I@mDK# zBF>89X074SEDKf93d+rx1&yYVljA6vtn>Fcm5oSPyrA4tCtqkfOFyRkacs3Kb1OVnDjs=k$tm4n8}mOA#NS z415I!kXZQuNQh6t3gQ!V=Q(i(N0v}GD*aFT>2&w}-rfDSD6jALItQZdtrL$WqQh&= z7Z3k@S@`;{K~@fn-G7cFIkfI-%k7xp*gcTJZj!w6;v|r9HT8@OHc#?c%jVek9yA|y zyN6z}mnoYy)7%uZ^-ku}sztDZ0T{Tvfjo=cjYYb@w(1wj%g!Bf>*lk|*FgphzyJ)u z01UtY48Q;kz`&I?kc}GjmT$jMzTkE_H|FMYi3tW^00v+H24DaNU;qYS00v+H24LU{ z8jvB;w?(3j>wJ0s|9|%T|KbMGZ{`>CgZa#SWIizOnRDh1^OD(N?lMcv1=sn>d}lr} z=ge#7IkU|a=d_r01_K6Q00v+H24DaNU;qYS00v;-KQ*9CsAj{2o3t!=XcNSFsO~vd zrDK(H`?j+}l|T?{WwJ?NKId4PFji^N^vzk-dfDknO#_?NdP8Nc9Ms49NqAbGRJp@Q zrhLD;NWk6dy1rPR>beYTbQIMEA{{0DJc{ZM$}=6$-m^1lbyt~iiY9nR^wmHcA{IP- z>Tr~#nb?#38L8N+$!?GsTW*SNu`8|ereEkt^!A?~b)GyHTVm|^Ufc8Cz5SlI)@rvL SqTFgTm?m_&#lBn@C(~c2gTOKX diff --git a/test/.server.jl.swp b/test/.server.jl.swp deleted file mode 100644 index 076c8f410fad6e0726f492c123e6144b4a38ec05..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2ONbmr7{_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deleted file mode 100644 index b7907af6a85dc74b551f2b8cb4f95865b280e67c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2KX21O7{)JM7@!5g0Ajir23tyOr==hvf>0|&BT}{0fdNGz$GOICoV$>_P%==& z#zz3bz=uF$;j1tq*kOQ`_xy+asR~jghDz^Aj~w6K^WNj%d9iXW_d#uwUbC+dXlDqy zadPS5r&sS6KD;at>GyQ+Kbz51^p4H;g0SdFt9% zosC9g+tz)GN#&?fpGkXf#xq?3?~nizn4iERSy`^Q+JDI?(sO6;&tD3iNB{{S0VIF~ zkN^@u0!RP}96JK4Um!0a^+J~XVsT;1$h1n-Fw$*=!RcL2yn5eesu&@QV?p=DAZ~*#D^N);J@jl3^z>_HXJ>n9X7(|%%VT%h2NcY%%bo7qI}JVE zP2bz>i$|1HMMO)psuW$G4vl zGW7*H+xN3YUob~g%SP9ibMvDY>$%mH%GL2wC8w8KFTH^X6IazsMLltCGCq-5w{`2L zxSX>L(@NB9#rRdeD_XA)jsgh;5;#l=)UCqkiUJ09CjWHi2A!A>2t^ABR7M8{l#{749YC%iw>1CltCFmclUH``e+=dGMXz3WZAW zdbs1&q0nY{C43RX!ag_yzK;>$Zde9OVHi$^A7MOr0PcpHU_b1GPhoVJhAB7=eu5F> zU*L;y5nKqL#c*&5oDQeK;}}Bz5T@Wf_%sHDe}Z#67!ou-)8f0J*`5s7)Q#HSs$Rnz zB5I%5wt0OtJa1W5Gm}Wj_k0CEhpKMPr|kdJE1l3*tytbQbxl?uu4ZdPg_^!AJTjtX zv)bBX&WeVw31`m>>)}|qDu2|%BcV_;<#63JYMH8Tn!78td|0ls)0Wf4a-k9>3Daog zC#E$r)Ne(k0&6sGT!HOTI>8S~L~Y(j%QR=8vIX zc5hD2XZV>9+<)dU7h#`J z{-oUeh26^@x#y8jbx%PmiW@SLPDa+uL{_Kl|I-}W`HGCKjVzyuq*IacWF$QnNsmX; zlacg_ossnF$a4Pi^P0%A)sbau^~l)j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Zhz!$memGA4;+t&i{Rsv--(O5Mg%6dzT zFY9gvSQ_2;4z%w!z%Z_{k$|)0pGREw_hpGp<90Al#k43(@V)m4`1U(+e;J9#<43e* hfE!OHvtg_3fv}(X2FLg9`j$L8FBlj>FJ~X={|5-C?cD$X diff --git a/test/.utils.jl.swp b/test/.utils.jl.swp deleted file mode 100644 index cf5ccce4e016df0fbdbd7b4fc2a019308697663a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2Pi)&%9LHZL1dK6;fCC7STo;-I)oIedDW!5Zt(M;QL;jIPHR5Rl8JrmOjt%`~7}j z|NOOMr>ZNN^}+>hHa$hKJWj|LtHzuEnm10|dUuHUv>|3=mm+QWMt0gLIA-0rK+Q$h z-A>!CN$vfmdk`GI8rT*c-^rwB##c(E)wG!7%;)0)w`@Os^+3&j1+}FdP!1f)fq++y z+-Wj7k0+>drvQpZ@f+jX)T}qBZA-Ps?cEu#xfZZD`y4-MLhcunXH9!sZpK$$Hp(xodMX5#BB-+yc1>mVx^ z@i~8pYvIo4ABGj;Q~W;Aqp?G6Yb)x|SeM)JwXC(on%pSWEkCSYZkfHAL)2fE+5D*W zc~jJfyAC(v_euvgl;=wwC*6X0kqr|Oz@>U9eu#VGHfpjFjO zd4Ek9i7_p)DTcHdhQ+WahL^-pPKYrnh7_C;(qg$EZ4A<>lXLT8%@^>e^-QOA=LI6*YxgN_PSPKHOnFY0Sll2 A+5i9m From 11f90a407c12681b3803fb00b8f2eb7b8dee98e2 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 12 Dec 2017 23:22:20 +1100 Subject: [PATCH 044/182] Split stack furthur into seperate modules. See module module RequestStack and const DefaultStack in HTTP.jl --- .gitignore | 3 +- src/BasicAuthRequest.jl | 28 +++++++++++++ src/CanonicalizeRequest.jl | 27 ++++++++++++ src/Connect.jl | 9 +++- src/Connections.jl | 24 ++++------- src/CookieRequest.jl | 74 ++++----------------------------- src/ExceptionRequest.jl | 30 ++++++++++++++ src/HTTP.jl | 40 +++++++++++++++++- src/Messages.jl | 2 + src/RedirectRequest.jl | 46 +++++++++++++++++++++ src/RetryRequest.jl | 26 ++++-------- src/SendRequest.jl | 85 ++++++++++++++++++-------------------- src/client.jl | 2 +- src/utils.jl | 31 ++++++++++++++ test/client.jl | 2 +- test/messages.jl | 2 +- test/parser.jl | 4 +- 17 files changed, 283 insertions(+), 152 deletions(-) create mode 100644 src/BasicAuthRequest.jl create mode 100644 src/CanonicalizeRequest.jl create mode 100644 src/ExceptionRequest.jl create mode 100644 src/RedirectRequest.jl create mode 100644 src/utils.jl diff --git a/.gitignore b/.gitignore index 40d8d0f05..7292d94a5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ .DS_Store *.key *.crt +*.swp docs/build/ -docs/site/ \ No newline at end of file +docs/site/ diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl new file mode 100644 index 000000000..36626ecd5 --- /dev/null +++ b/src/BasicAuthRequest.jl @@ -0,0 +1,28 @@ +module BasicAuthRequest + +struct BasicAuthLayer{T} end +export BasicAuthLayer + +import ..HTTP.RequestStack.request + +using ..URIs +using ..Pairs: getkv, setkv + +import ..@debug, ..DEBUG_LEVEL + + +function request(::Type{BasicAuthLayer{Next}}, + method::String, uri, headers=[], body=""; kw...) where Next + + userinfo = URI(uri).userinfo + + if !isempty(userinfo) && getkv(headers, "Authorization", "") == "" + @debug 1 "Adding Authorization: Basic header." + setkv(headers, "Authorization", "Basic $(base64encode(userinfo))") + end + + return request(Next, method, uri, headers, body; kw...) +end + + +end # module BasicAuthRequest diff --git a/src/CanonicalizeRequest.jl b/src/CanonicalizeRequest.jl new file mode 100644 index 000000000..6b67aac5d --- /dev/null +++ b/src/CanonicalizeRequest.jl @@ -0,0 +1,27 @@ +module CanonicalizeRequest + +struct CanonicalizeLayer{T} end +export CanonicalizeLayer + +import ..HTTP.RequestStack.request + +using ..Messages +using ..Strings.tocameldash! + +canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) + +function request(::Type{CanonicalizeLayer{Next}}, + method::String, uri, headers=[], body=""; kw...) where Next + + headers = canonicalizeheaders(headers) + + res = request(Next, method, uri, headers, body; kw...) + + res.headers = canonicalizeheaders(res.headers) + + return res +end + + + +end # module CanonicalizeRequest diff --git a/src/Connect.jl b/src/Connect.jl index 946e822c4..5bb89ae7a 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -2,6 +2,9 @@ module Connect export getconnection +const Connection = Union + + using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! import ..@debug, ..DEBUG_LEVEL @@ -18,14 +21,16 @@ reuse and request interleaving. """ function getconnection(::Type{TCPSocket}, host::AbstractString, - port::AbstractString)::TCPSocket + port::AbstractString; + kw...)::TCPSocket p::UInt = isempty(port) ? UInt(80) : parse(UInt, port) @debug 2 "TCP connect: $host:$p..." connect(getaddrinfo(host), p) end function getconnection(::Type{SSLContext}, host::AbstractString, - port::AbstractString)::SSLContext + port::AbstractString; + kw...)::SSLContext port = isempty(port) ? "443" : port @debug 2 "SSL connect: $host:$port..." io = SSLContext() diff --git a/src/Connections.jl b/src/Connections.jl index 938959419..c9fbef3a0 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -43,18 +43,10 @@ end isbusy(c::Connection) = c.writecount - c.readcount > 1 -Connection{T}() where T <: IO = - Connection{T}("", "", T(), view(UInt8[], 1:0), 0, 0, Condition()) - -function Connection{T}(host::AbstractString, port::AbstractString) where T <: IO - c = Connection{T}() - c.host = host - c.port = port - c.io = getconnection(T, host, port) - return c -end +Connection{T}(host::AbstractString, port::AbstractString, io::T) where T <: IO = + Connection{T}(host, port, io, view(UInt8[], 1:0), 0, 0, Condition()) -const noconnection = Connection{TCPSocket}() +const noconnection = Connection{TCPSocket}("","",TCPSocket()) Base.unsafe_write(c::Connection, p::Ptr{UInt8}, n::UInt) = unsafe_write(c.io, p, n) @@ -144,7 +136,8 @@ or create a new `Connection` if required. function getconnection(::Type{Connection{T}}, host::AbstractString, - port::AbstractString)::Connection{T} where T <: IO + port::AbstractString; + kw...)::Connection{T} where T <: IO lock(poollock) try @@ -157,13 +150,14 @@ function getconnection(::Type{Connection{T}}, while (i = findlast(pattern, pool)) > 0 c = pool[i] if !isopen(c.io) - deleteat!(pool, i) ;@debug 1 "Deleted: $c" + deleteat!(pool, i) ;@debug 1 "Deleted: $c" continue - end; ;@debug 2 "Reused: $c" + end; ;@debug 2 "Reused: $c" return c end - c = Connection{T}(host, port) ;@debug 1 "New: $c" + io = getconnection(T, host, port; kw...) + c = Connection{T}(host, port, io) ;@debug 1 "New: $c" push!(pool, c) @assert !isbusy(c) return c diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index 4b05b56f3..457a0f482 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -1,19 +1,16 @@ module CookieRequest -export request +struct CookieLayer{T} end +export CookieLayer -import ..HTTP +import ..HTTP.RequestStack.request using ..URIs using ..Cookies -using ..Messages using ..Pairs: getkv, setkv -using ..Strings.tocameldash! import ..@debug, ..DEBUG_LEVEL -import ..RetryRequest - const default_cookiejar = Dict{String, Set{Cookie}}() @@ -50,19 +47,9 @@ function setcookies(cookies, host, headers) end -canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) - - -function setbasicauthorization(headers, uri) - if !isempty(uri.userinfo) && getkv(headers, "Authorization", "") == "" - @debug 1 "Adding Authorization: Basic header." - setkv(headers, "Authorization", "Basic $(base64encode(uri.userinfo))") - end -end - - -function request(method::String, uri, headers=[], body=""; - cookiejar=default_cookiejar, kw...) +function request(::Type{CookieLayer{Next}}, + method::String, uri, headers=[], body=""; + cookiejar=default_cookiejar, kw...) where Next u = URI(uri) hostcookies = get!(cookiejar, u.host, Set{Cookie}()) @@ -72,54 +59,11 @@ function request(method::String, uri, headers=[], body=""; setkv(headers, "Cookie", string(getkv(headers, "Cookie", ""), cookies)) end - if getkv(kw, :basicauthorization, false) - setbasicauthorization(headers, uri) - end - - try - res = RetryRequest.request(method, uri, headers, body; kw...) - - if getkv(kw, :canonicalizeheaders, false) - res.headers = canonicalizeheaders(res.headers) - end - - setcookies(hostcookies, u.host, res.headers) - - return res - - catch e - # Redirect request to new location... - if (isa(e, HTTP.StatusError) - && isredirect(e.response) - && parentcount(e.response) < getkv(kw, :maxredirects, 3) - && header(e.response, "Location") != "" - && method != "HEAD") #FIXME why not redirect HEAD? - - setcookies(hostcookies, u.host, e.response.headers) - - return redirect(e.response, method, uri, headers, body; kw...) - else - rethrow(e) - end - end - @assert false "Unreachable!" -end - - -function redirect(res, method, uri, headers, body; kw...) - - uri = absuri(header(res, "Location"), uri) - @debug 1 "Redirect: $uri" - - if getkv(kw, :forwardheaders, true) - headers = filter(h->!(h[1] in ("Host", "Cookie")), headers) - else - headers = [] - end + res = request(Next, method, uri, headers, body; kw...) - setkv(kw, :parent, res) + setcookies(hostcookies, u.host, res.headers) - return request(method, uri, headers, body; kw...) + return res end diff --git a/src/ExceptionRequest.jl b/src/ExceptionRequest.jl new file mode 100644 index 000000000..802208fb1 --- /dev/null +++ b/src/ExceptionRequest.jl @@ -0,0 +1,30 @@ +module ExceptionRequest + +struct ExceptionLayer{T} end +export ExceptionLayer +export StatusError + +import ..HTTP.RequestStack.request + +using ..Messages + + +struct StatusError <: Exception + status::Int16 + response::Messages.Response +end + + +function request(::Type{ExceptionLayer{Next}}, a...; kw...) where Next + + res = request(Next, a...; kw...) + + if iserror(res) && !isredirect(res) + throw(StatusError(res.status, res)) + end + + return res +end + + +end # module ExceptionRequest diff --git a/src/HTTP.jl b/src/HTTP.jl index ae3f8f890..0d43b1ec1 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -5,7 +5,8 @@ using MbedTLS import MbedTLS.SSLContext const TLS = MbedTLS -import Base.== + +import Base.== # FIXME rm const DEBUG = false # FIXME rm const PARSING_DEBUG = false # FIXME rm @@ -21,6 +22,13 @@ else import Dates end +#abstract type RequestLayer{Next} end + +module RequestStack + import ..HTTP + request(m::String, a...; kw...) = request(HTTP.DefaultStack, m, a...; kw...) +end + #FIXME status(r) = r.status headers(r) = Dict(r.headers) @@ -49,7 +57,7 @@ include("Connect.jl") include("Connections.jl") include("SendRequest.jl") -import .SendRequest.StatusError +using .SendRequest include("types.jl") include("client.jl") @@ -67,8 +75,36 @@ function __init__() end +include("ExceptionRequest.jl") +using .ExceptionRequest +import .ExceptionRequest.StatusError include("RetryRequest.jl") +using .RetryRequest include("CookieRequest.jl") +using .CookieRequest +include("BasicAuthRequest.jl") +using .BasicAuthRequest +include("CanonicalizeRequest.jl") +using .CanonicalizeRequest +include("RedirectRequest.jl") +using .RedirectRequest + + +const DefaultStack = + RedirectLayer{ + CanonicalizeLayer{ + BasicAuthLayer{ + CookieLayer{ + RetryLayer{ + ExceptionLayer{ + MessageLayer{ + ConnectLayer{ + #Connect.Connection + Connections.Connection + }}}}}}}} + + + end # module #= diff --git a/src/Messages.jl b/src/Messages.jl index bbb4acef8..0f2117ab4 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -112,6 +112,7 @@ Method of the `Request` that yielded this `Response`. method(r::Response) = r.parent == nothing ? "" : r.parent.method +#= FIXME obsolete ? """ parentcount(::Response) @@ -125,6 +126,7 @@ function parentcount(r::Response) return 1 + parentcount(r.parent.parent) end end +=# """ diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl new file mode 100644 index 000000000..683171885 --- /dev/null +++ b/src/RedirectRequest.jl @@ -0,0 +1,46 @@ +module RedirectRequest + +struct RedirectLayer{T} end +export RedirectLayer + +import ..HTTP.RequestStack.request + +using ..URIs +using ..Messages +using ..Pairs: setkv +using ..Strings.tocameldash! + +import ..@debug, ..DEBUG_LEVEL + + +function request(::Type{RedirectLayer{Next}}, + method::String, uri, headers=[], body=""; + maxredirects=3, forwardheaders=false, kw...) where Next + count = 0 + while true + + res = request(Next, method, uri, headers, body; kw...) + + if (count == maxredirects + || !isredirect(res) + || (location = header(res, "Location")) == "" + || method == "HEAD") #FIXME why not redirect HEAD? + return res + end + + setkv(kw, :parent, res) + uri = absuri(location, uri) + if forwardheaders + headers = filter(h->!(h[1] in ("Host", "Cookie")), headers) + else + headers = [] + end + + count += 1 + end + + @assert false "Unreachable!" +end + + +end # module RedirectRequest diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index c423128e4..05e85b446 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -1,14 +1,11 @@ module RetryRequest -export request - -using Retry +struct RetryLayer{T} end +export RetryLayer +import ..HTTP.RequestStack.request import ..HTTP -using ..Pairs.getkv -import ..SendRequest, ..@debug - isrecoverable(e::Base.UVError) = true isrecoverable(e::Base.DNSError) = true @@ -17,19 +14,12 @@ isrecoverable(e::HTTP.StatusError) = e.status < 200 || e.status >= 500 isrecoverable(e::Exception) = false -function request(a...; kw...) - - n = getkv(kw, :maxretries, 2) + 1 +function request(::Type{RetryLayer{Next}}, a...; maxretries=2, kw...) where Next - @repeat n try - return SendRequest.request(a...; kw...) - catch e - @delay_retry if isrecoverable(e) - @debug 1 "Retrying after $e" - end - end - - @assert false "Unreachable" + retry(request, + delays=ExponentialBackOff(n = maxretries), + check=(s,ex)->(s,isrecoverable(ex)))(Next, a...; kw...) end + end # module RetryRequest diff --git a/src/SendRequest.jl b/src/SendRequest.jl index b0afc0422..b3b804928 100644 --- a/src/SendRequest.jl +++ b/src/SendRequest.jl @@ -1,14 +1,17 @@ module SendRequest -export request, StatusError +struct MessageLayer{T} end +export MessageLayer -import ..HTTP +struct ConnectLayer{T} end +export ConnectLayer + +import ..HTTP.RequestStack.request -using ..Pairs.getkv using ..URIs using ..Messages -using ..Connections +using ..Connect using ..IOExtras using MbedTLS.SSLContext @@ -17,12 +20,12 @@ import ..@debug, ..DEBUG_LEVEL """ - request(::IO, ::Request, ::Response) + writeandread(::IO, ::Request, ::Response) -Send a `Request` and fill in a `Response`. +Send a `Request` and receive a `Response`. """ -function request(io::IO, req::Request, res::Response) +function writeandread(io::IO, req::Request, res::Response) try ;@debug 1 "write to: $io\n$req" write(io, req) @@ -39,31 +42,38 @@ end """ - request(::URI, ::Request, ::Response) + request(::IO, ::Request, ::Response) -Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. +Send a `Request` and receive a `Response`. """ - -function request(uri::URI, req::Request, res::Response; kw...) - - defaultheader(req, "Host" => uri.host) - setlengthheader(req) - - # Get a connection from the pool... - T = uri.scheme == "https" ? SSLContext : TCPSocket - if getkv(kw, :use_connection_pool, true) - T = Connections.Connection{T} - end - io = getconnection(T, uri.host, uri.port) +function request(io::IO, req::Request, res::Response) # Run request in a background task if response body is a stream... if isstream(res.body) - @schedule request(io, req, res) + @schedule writeandread(io, req, res) waitforheaders(res) return res end + return writeandread(io, req, res) +end + + +""" + request(::URI, ::Request, ::Response) + +Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. +""" + + +function request(::Type{ConnectLayer{Connection}}, + uri::URI, req::Request, res::Response; kw...) where Connection + + # Get a connection from the pool... + T = uri.scheme == "https" ? SSLContext : TCPSocket + io = getconnection(Connection{T}, uri.host, uri.port; kw...) + return request(io, req, res) end @@ -100,39 +110,26 @@ println(stat("response_file").size) ``` """ -function request(method::String, uri, headers=[], body=""; +function request(::Type{MessageLayer{Next}}, + method::String, uri, headers=[], body=""; bodylength=Messages.Bodies.unknownlength, parent=nothing, response_stream=nothing, - kw...) + kw...) where Next u = URI(uri) + url = method == "CONNECT" ? hostport(u) : resource(u) - req = Request(method, - method == "CONNECT" ? hostport(u) : resource(u), - headers, - Body(body, bodylength), + req = Request(method, url, headers, Body(body, bodylength); parent=parent) - res = Response(body=Body(response_stream), parent=req) - - request(u, req, res; kw...) - - # Throw StatusError for non Status-2xx Response Messages... - if iserror(res) && getkv(kw, :statusraise, true) - throw(StatusError(res)) - end - - return res -end + defaultheader(req, "Host" => u.host) + setlengthheader(req) + res = Response(body=Body(response_stream), parent=req) -struct StatusError <: Exception - status::Int16 - response::Messages.Response + return request(Next, u, req, res; kw...) end -StatusError(r::Messages.Response) = StatusError(r.status, r) - end # module SendRequest diff --git a/src/client.jl b/src/client.jl index 83552a16a..2972e4780 100644 --- a/src/client.jl +++ b/src/client.jl @@ -78,7 +78,7 @@ function request(client::Client, method, uri::URI; body = read(body) end - return CookieRequest.request(m, uri, h, body; args...) + return RequestStack.request(m, uri, h, body; args...) end request(uri::AbstractString; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, GET, URIs.URL(uri; query=query); verbose=verbose, args...) request(uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, GET, uri; verbose=verbose, args...) diff --git a/src/utils.jl b/src/utils.jl new file mode 100644 index 000000000..0e7476333 --- /dev/null +++ b/src/utils.jl @@ -0,0 +1,31 @@ +macro src() + @static if VERSION >= v"0.7-" && length(:(@test).args) == 2 + esc(quote + (__module__, + __source__.file == nothing ? "?" : String(__source__.file), + __source__.line) + end) + else + esc(quote + (current_module(), + (p = Base.source_path(); p == nothing ? "REPL" : p), + Int(unsafe_load(cglobal(:jl_lineno, Cint)))) + end) + end +end + + +macro log(stmt) + # "[HTTP]: Connecting to remote host..." + return esc(:(verbose && (write(logger, "[HTTP - $(rpad(Dates.now(), 23, ' '))]: $($stmt)\n"); flush(logger)))) +end + +macro catch(etype, expr) + esc(quote + try + $expr + catch e + isa(e, $etype) ? e : rethrow(e) + end + end) +end diff --git a/test/client.jl b/test/client.jl index 9d4d9f0e2..3b14040fb 100644 --- a/test/client.jl +++ b/test/client.jl @@ -156,7 +156,7 @@ for sch in ("http", "https") r = HTTP.get("$sch://httpbin.org/redirect/1") @test HTTP.status(r) == 200 #@test length(HTTP.history(r)) == 1 - @test_throws HTTP.StatusError HTTP.get("$sch://httpbin.org/redirect/6") + @test HTTP.status(HTTP.get("$sch://httpbin.org/redirect/6")) == 302 @test HTTP.status(HTTP.get("$sch://httpbin.org/relative-redirect/1")) == 200 @test HTTP.status(HTTP.get("$sch://httpbin.org/absolute-redirect/1")) == 200 @test HTTP.status(HTTP.get("$sch://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com")) == 200 diff --git a/test/messages.jl b/test/messages.jl index 266ef888e..538e8c2d7 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -5,8 +5,8 @@ using Base.Test using HTTP.Messages import HTTP.Messages.appendheader import HTTP.URI +import HTTP.RequestStack.request -using HTTP.CookieRequest using HTTP.StatusError using JSON diff --git a/test/parser.jl b/test/parser.jl index c9fd130bf..72a850cbc 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1428,7 +1428,7 @@ const responses = Message[ @test uri.port in (req.port, "80", "443") @test string(uri) == req.request_url @test length(r.headers) == req.num_headers - @test Dict(HTTP.CookieRequest.canonicalizeheaders(r.headers)) == Dict(req.headers) + @test Dict(HTTP.CanonicalizeRequest.canonicalizeheaders(r.headers)) == Dict(req.headers) @test String(take!(r.body)) == req.body # FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == req.should_keep_alive @@ -1615,7 +1615,7 @@ const responses = Message[ @test r.status == resp.status_code @test HTTP.Messages.statustext(r) == resp.response_status @test length(r.headers) == resp.num_headers - @test Dict(HTTP.CookieRequest.canonicalizeheaders(r.headers)) == Dict(resp.headers) + @test Dict(HTTP.CanonicalizeRequest.canonicalizeheaders(r.headers)) == Dict(resp.headers) @test String(take!(r.body)) == resp.body # FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == resp.should_keep_alive catch e From 946e35d1d255ab9009ea0598f17c6c6c71d81d3a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 13 Dec 2017 12:13:24 +1100 Subject: [PATCH 045/182] more refinement of layer separation, doc updates --- docs/src/index.md | 213 ++++++++++++++++------ src/BasicAuthRequest.jl | 10 +- src/Bodies.jl | 14 +- src/CanonicalizeRequest.jl | 10 +- src/Connect.jl | 3 - src/{Connections.jl => ConnectionPool.jl} | 4 +- src/ConnectionRequest.jl | 36 ++++ src/CookieRequest.jl | 10 +- src/ExceptionRequest.jl | 11 +- src/HTTP.jl | 22 ++- src/MessageRequest.jl | 65 +++++++ src/Messages.jl | 48 ++++- src/RedirectRequest.jl | 10 +- src/RetryRequest.jl | 8 +- src/SendRequest.jl | 135 -------------- src/SocketRequest.jl | 29 +++ 16 files changed, 381 insertions(+), 247 deletions(-) rename src/{Connections.jl => ConnectionPool.jl} (99%) create mode 100644 src/ConnectionRequest.jl create mode 100644 src/MessageRequest.jl delete mode 100644 src/SendRequest.jl create mode 100644 src/SocketRequest.jl diff --git a/docs/src/index.md b/docs/src/index.md index 0850d12c1..8c7c60af0 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -58,7 +58,7 @@ HTTP.escapeHTML ## Parser -Source: [`Parsers.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Parsers.jl) +Source: `Parsers.jl` The [`HTTP.Parser`](@ref) separates HTTP Message data (from a `String`, an `IO` stream or raw bytes) into its component parts. The parts are passed to @@ -89,7 +89,7 @@ Messages. ## Messages -Source: [`Messages.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Messages.jl) +Source: `Messages.jl` The `Messages` module defines structs that represent [`HTTP.Messages.Request`](@ref) and [`HTTP.Messages.Response`](@ref) Messages. @@ -126,8 +126,7 @@ callbacks are called to fill in the `Message` struct. The `Response` struct has a `parent` field that points to the corresponding `Request`. The `Request` struct as a `parent` field that points to a `Response` -in the case of HTTP Redirect. The [`HTTP.Messages.parentcount`](@ref) function is -used to place a limit on nested redirects. +in the case of HTTP Redirect. ### Headers @@ -160,7 +159,7 @@ known at the time the headers are sent. ### Basic Connections -Source: [`Connect.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Connect.jl) +Source: `Connect.jl` [`HTTP.Connect.getconnection`](@ref) creates a new `TCPSocket` or `SSLContext` for a specified `host` and `port. @@ -173,21 +172,21 @@ not required. ### Pooled Connections -Source: [`Connections.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/Connections.jl) +Source: `ConnectionPool.jl` -This module wrapps the Basic Connections module above and adds support for: +This module wrapps the Basic Connect module above and adds support for: - Reusing connections for multiple Request/Response Messages, - Interleaving Request/Response Messages. i.e. allowing a new Request to be sent before while the previous Response is being read. -This module defines a [`HTTP.Connections.Connection`](@ref)` <: IO` +This module defines a [`HTTP.ConnectionPool.Connection`](@ref)` <: IO` struct to manage Message streaming and connection reuse. Methods are provided for `eof`, `readavailable`, `unsafe_write` and `close`. This allows the `Connection` object to act as a proxy for the `TCPSocket` or `SSLContext` that it wraps. -The [`HTTP.Connections.pool`](@ref) is a collection of open +The [`HTTP.ConnectionPool.pool`](@ref) is a collection of open `Connection`s. The `request` function calls `getconnection` to retrieve a connection from the `pool`. When the `request` function has written a Request Message it calls `closewrite` to signal that @@ -209,61 +208,164 @@ request(uri::URI, req::Request, res::Response) end ``` -## Request Execution +## Request Execution Stack -There are three seperate Request Execution layers, all with the same interface. -Clients can choose which layer to import according to the features they require. +The Request Execution Stack is separated into composable layers. -### Basic Request Execution +Each layer is defined by a nested type `Layer{Next}` where the `Next` +parameter defines the next layer in the stack. +The `request` method for each layer takes a `Layer{Next}` type as +its first argument and dispatches the request to the next layer +using `request(Next, ...)`. -Source: [`SendRequest.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/SendRequest.jl) +The example below defines three layers and three stacks each with +a different combination of layers. -The `SendRequest` module implements basic HTTP Request execution. -The `request` function is split into three methods: -- [`HTTP.SendRequest.request(method::String, uri, headers, body)`](@ref)) -- [`HTTP.SendRequest.request(::HTTP.URIs.URI,request,response)`](@ref) -- [`HTTP.SendRequest.request(::IO, request, response)`](@ref)). +```julia +abstract type Layer end +abstract type Layer1{Next <: Layer} <: Layer end +abstract type Layer2{Next <: Layer} <: Layer end +abstract type Layer3 <: Layer end + +request(::Type{Layer1{Next}}, data) where Next = "L1", request(Next, data) +request(::Type{Layer2{Next}}, data) where Next = "L2", request(Next, data) +request(::Type{Layer3}, data) = "L3", data + +const stack1 = Layer1{Layer2{Layer3}} +const stack2 = Layer2{Layer1{Layer3}} +const stack3 = Layer1{Layer3} +``` -These methods implement: -- Creating a [`HTTP.Messages.Request`](@ref) for a specified method, URI, - headers and body, -- Setting the mandatory `Host` and `Content-Length` (or `Transfer-Encoding`) - headers. -- Getting a connection from the pool for a specified URI. -- Writing a `Request` to the connection and reading a `Response`. -- Raising a `StatusError` of the Response Status is not in the `2xx` range. +```julia +julia> request(stack1, "foo") +("L1", ("L2", ("L3", "foo"))) -If the `Body` of the `Request` is connected to an `IO` stream, the `request` -function waits for the Response Headers to be recieved and schedules reading of -the the Response Body to happen as a background task. +julia> request(stack2, "bar") +("L2", ("L1", ("L3", "bar"))) + +julia> request(stack3, "boo") +("L1", ("L3", "boo")) +``` + +This stack definition pattern gives the user flexibility in how layers are +combined but still allows Julia to do whole-stack comiple time optimistations. + +e.g. the `request(stack1, "foo")` call above is optimised down to a single +function: +```julia +julia> code_typed(request, (Type{stack1}, String))[1].first +CodeInfo(:(begin + return (Core.tuple)("L1", (Core.tuple)("L2", (Core.tuple)("L3", data))) +end)) +``` + +In `HTTP.jl` the `const DefaultStack` type defines the default HTTP Request +processing stack. This is used as the default first parameter of the `request` +function. + +```julia +const DefaultStack = + RedirectLayer{ + CanonicalizeLayer{ + BasicAuthLayer{ + CookieLayer{ + RetryLayer{ + ExceptionLayer{ + MessageLayer{ + ConnectionLayer{ConnectionPool.Connection, + SocketLayer + }}}}}}}} + +request(method::String, uri, headers=[], body=""; kw...) = + request(HTTP.DefaultStack, method, uri, headers, body; kw...) +``` + +Note that the `ConnectLayer`'s optional first parameter is a connection wrapper +type. If it was omitted then `ConnectionLayer` would use raw socket types from +the `Connect` module directly. + + +## Redirect Layer + +Source: `RedirectRequest.jl` + +This layer adds a loop to process `3xx` redirects. + + +## Canonicalize Layer + +Source: `CanonicalizeRequest.jl` +This layer rewrites header field names to canonical Camel-Dash form. -### Request Execution With Retry -Source: [`RetryRequest.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/RetryRequest.jl) +## Basic Authentication Layer -The `RetryRequest` module implements a `request` function that accepts the -same arguments as, and wraps, -[`HTTP.SendRequest.request(method::String, uri, headers, body)`](@ref)). +Source: `BasicAuthRequest.jl` -This layer adds a retry loop that repeats the `request` in the event of a -recoverable network error. A randomised exponentially increasing delay is -introduced between attempts to avoid making network congestion worse. +This layer adds an `Authorization: Basic` header using `URI.userinfo`. -Methods of `isrecoverable(e)` define which exception types lead to a retry: -`Base.UVError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` + +## Cookie Layer + +Source: `CookieRequest.jl` + +This layer stores cookies sent by the server and sends them back to the +server with subsequent requests. + + +## Retry Layer + +Source: `RetryRequest.jl` + +The `RetryRequest` module implements a `request` method with a retry loop that +repeats the request in the event of a recoverable network error. +A randomised exponentially increasing delay is introduced between attempts to +avoid exacerbating making network congestion. + +Methods of `isrecoverable(e)` define which exception types lead to a retry. +e.g. `Base.UVError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` (if status is `1xx` or `5xx`). -### Request Execution With State -Source: [`CookieRequest.jl`](https://github.com/JuliaWeb/HTTP.jl/blob/master/src/CookieRequest.jl) +## ExceptionLayer + +Source: `ExceptionRequest.jl` + +This layer throws a `StatusError` if the Response Status indicates an error. + + +## Message Layer -The `CookieRequest` module implements a `request` function that accepts the -same arguments as, and wraps the `RetryRequest.request` function. +Source: `MessageRequest.jl` -This layer adds processing of client-side cookies, basic authorization headers -and `3xx` redirects. +This layer: +- Creates a [`HTTP.Messages.Request`](@ref) object for the specified + method, URI, headers and body, +- Sets the mandatory `Host` and `Content-Length` (or `Transfer-Encoding`) + headers. +- Creates a [`HTTP.Messages.Response`](@ref) object to hold the response. + + +## Connection Layer + +Source: `ConnectionRequest.jl` + +This layer calls [`HTTP.Connect.getconnection`](@ref) +to get a socket from connection pool. + + +## Socket Layer + +Source: `SocketRequest.jl` + +This layer calls [`HTTP.Messages.writeandread`](@ref) to send the Request +to the socket and receive the Response. + +If the `Body` of the `Request` is connected to an `IO` stream, the `request` +function waits for the Response Headers to be received, but schedules reading of +the Response Body to happen in a background task. # Internal Interfaces @@ -293,7 +395,9 @@ HTTP.Messages.defaultheader HTTP.Messages.setlengthheader HTTP.Messages.appendheader HTTP.Messages.waitforheaders +Base.wait(::HTTP.Messages.Response) Base.write(::IO,::Union{HTTP.Messages.Request, HTTP.Messages.Response}) +HTTP.Messages.writeandread HTTP.Messages.readstartline! ``` @@ -310,7 +414,6 @@ HTTP.Messages.Response HTTP.Messages.iserror HTTP.Messages.isredirect HTTP.Messages.method -HTTP.Messages.parentcount ``` ### Body @@ -338,19 +441,17 @@ HTTP.Connect.getconnection(::Type{TCPSocket},::AbstractString,::AbstractString) ### Connection Pooling Interface ```@docs -HTTP.Connections.Connection -HTTP.Connections.pool -HTTP.Connect.getconnection(::Type{HTTP.Connections.Connection{T}},::AbstractString,::AbstractString) where T <: IO -HTTP.IOExtras.unread!(::HTTP.Connections.Connection,::SubArray{UInt8, 1}) -HTTP.IOExtras.closewrite(::HTTP.Connections.Connection) -HTTP.IOExtras.closeread(::HTTP.Connections.Connection) +HTTP.ConnectionPool.Connection +HTTP.ConnectionPool.pool +HTTP.Connect.getconnection(::Type{HTTP.ConnectionPool.Connection{T}},::AbstractString,::AbstractString) where T <: IO +HTTP.IOExtras.unread!(::HTTP.ConnectionPool.Connection,::SubArray{UInt8, 1}) +HTTP.IOExtras.closewrite(::HTTP.ConnectionPool.Connection) +HTTP.IOExtras.closeread(::HTTP.ConnectionPool.Connection) ``` ## Low Level Request Interface ```@docs -HTTP.SendRequest.request(::String,::Any,::Any,::Any) -HTTP.SendRequest.request(::HTTP.URIs.URI,::HTTP.Messages.Request,::HTTP.Messages.Response) -HTTP.SendRequest.request(::IO,::HTTP.Messages.Request,::HTTP.Messages.Response) +HTTP.RequestStack.request ``` diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index 36626ecd5..cfdc96035 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -1,15 +1,13 @@ module BasicAuthRequest -struct BasicAuthLayer{T} end -export BasicAuthLayer - -import ..HTTP.RequestStack.request - +import ..Layer, ..RequestStack.request using ..URIs using ..Pairs: getkv, setkv - import ..@debug, ..DEBUG_LEVEL +abstract type BasicAuthLayer{Next <: Layer} <: Layer end +export BasicAuthLayer + function request(::Type{BasicAuthLayer{Next}}, method::String, uri, headers=[], body=""; kw...) where Next diff --git a/src/Bodies.jl b/src/Bodies.jl index 29173d794..439a0f18b 100644 --- a/src/Bodies.jl +++ b/src/Bodies.jl @@ -84,7 +84,7 @@ Body() = Body(notastream, IOBuffer(), unknownlength) Body(buffer::IOBuffer, l=unknownlength) = Body(notastream, buffer, l) Body(stream::IO, l=unknownlength) = Body(stream, IOBuffer(body_show_max), l) Body(::Void) = Body() -Body(data, l=unknownlength) = Body(notastream, IOBuffer(data), l) +Body(data, l=unknownlength) = Body(IOBuffer(data), l) """ @@ -212,7 +212,17 @@ function Base.write(body::Body, v) return n end -Base.close(body::Body) = if isstream(body); close(body.stream) end + +function Base.close(body::Body) + if isstream(body) + close(body.stream) + else + body.buffer.writable = false + end +end + +Base.isopen(body::Body) = + isstream(body) ? isopen(body.stream) : iswriteable(body.buffer) """ diff --git a/src/CanonicalizeRequest.jl b/src/CanonicalizeRequest.jl index 6b67aac5d..3bd28e193 100644 --- a/src/CanonicalizeRequest.jl +++ b/src/CanonicalizeRequest.jl @@ -1,13 +1,13 @@ module CanonicalizeRequest -struct CanonicalizeLayer{T} end -export CanonicalizeLayer - -import ..HTTP.RequestStack.request - +import ..Layer, ..RequestStack.request using ..Messages using ..Strings.tocameldash! +abstract type CanonicalizeLayer{Next <: Layer} <: Layer end +export CanonicalizeLayer + + canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) function request(::Type{CanonicalizeLayer{Next}}, diff --git a/src/Connect.jl b/src/Connect.jl index 5bb89ae7a..77230992b 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -2,9 +2,6 @@ module Connect export getconnection -const Connection = Union - - using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! import ..@debug, ..DEBUG_LEVEL diff --git a/src/Connections.jl b/src/ConnectionPool.jl similarity index 99% rename from src/Connections.jl rename to src/ConnectionPool.jl index c9fbef3a0..60898aef0 100644 --- a/src/Connections.jl +++ b/src/ConnectionPool.jl @@ -1,4 +1,4 @@ -module Connections +module ConnectionPool export getconnection @@ -193,4 +193,4 @@ peerport(c::Connection) = !isopen(c.io) ? 0 : tcpstatus(c::Connection) = Base.uv_status_string(tcpsocket(c)) -end # module Connections +end # module ConnectionPool diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl new file mode 100644 index 000000000..ed8ceecff --- /dev/null +++ b/src/ConnectionRequest.jl @@ -0,0 +1,36 @@ +module ConnectionRequest + +import ..Layer, ..RequestStack.request +using ..URIs +using ..Messages +using ..Connect +using MbedTLS.SSLContext + + +abstract type ConnectionLayer{Connection, Next <: Layer} <: Layer end +export ConnectionLayer + + +""" + request(ConnectionLayer{Connection, Next}, ::URI, ::Request, ::Response) + +Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. +""" + +function request(::Type{ConnectionLayer{Connection, Next}}, + uri::URI, req::Request, res::Response; + kw...) where Next where Connection + + Socket = uri.scheme == "https" ? SSLContext : TCPSocket + + io = getconnection(Connection{Socket}, uri.host, uri.port; kw...) + + return request(Next, io, req, res) +end + +# If no `Connection` wrapper type is provided, `Union` acts as a no-op. +request(::Type{ConnectionLayer{Next}}, a...; kw...) where Next <: Layer = + request(ConnectionLayer{Union, Next}, a...; kw...) + + +end # module ConnectionRequest diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index 457a0f482..ff3fa5e41 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -1,16 +1,14 @@ module CookieRequest -struct CookieLayer{T} end -export CookieLayer - -import ..HTTP.RequestStack.request - +import ..Layer, ..RequestStack.request using ..URIs using ..Cookies using ..Pairs: getkv, setkv - import ..@debug, ..DEBUG_LEVEL +abstract type CookieLayer{Next <: Layer} <: Layer end +export CookieLayer + const default_cookiejar = Dict{String, Set{Cookie}}() diff --git a/src/ExceptionRequest.jl b/src/ExceptionRequest.jl index 802208fb1..89ecc7eae 100644 --- a/src/ExceptionRequest.jl +++ b/src/ExceptionRequest.jl @@ -1,13 +1,12 @@ module ExceptionRequest -struct ExceptionLayer{T} end +import ..Layer, ..RequestStack.request +using ..Messages + +abstract type ExceptionLayer{Next <: Layer} <: Layer end export ExceptionLayer export StatusError -import ..HTTP.RequestStack.request - -using ..Messages - struct StatusError <: Exception status::Int16 @@ -19,7 +18,7 @@ function request(::Type{ExceptionLayer{Next}}, a...; kw...) where Next res = request(Next, a...; kw...) - if iserror(res) && !isredirect(res) + if iserror(res) throw(StatusError(res.status, res)) end diff --git a/src/HTTP.jl b/src/HTTP.jl index 0d43b1ec1..fc2e6a3b1 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -22,7 +22,8 @@ else import Dates end -#abstract type RequestLayer{Next} end + +abstract type Layer end module RequestStack import ..HTTP @@ -54,10 +55,8 @@ import .Parsers.ParsingError include("Messages.jl") include("Connect.jl") -include("Connections.jl") +include("ConnectionPool.jl") -include("SendRequest.jl") -using .SendRequest include("types.jl") include("client.jl") @@ -75,6 +74,12 @@ function __init__() end +include("SocketRequest.jl") +using .SocketRequest +include("ConnectionRequest.jl") +using .ConnectionRequest +include("MessageRequest.jl") +using .MessageRequest include("ExceptionRequest.jl") using .ExceptionRequest import .ExceptionRequest.StatusError @@ -92,16 +97,15 @@ using .RedirectRequest const DefaultStack = RedirectLayer{ - CanonicalizeLayer{ + #CanonicalizeLayer{ BasicAuthLayer{ CookieLayer{ RetryLayer{ ExceptionLayer{ MessageLayer{ - ConnectLayer{ - #Connect.Connection - Connections.Connection - }}}}}}}} + ConnectionLayer{ConnectionPool.Connection, + SocketLayer + }}}}}}}#} diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl new file mode 100644 index 000000000..42d61ca44 --- /dev/null +++ b/src/MessageRequest.jl @@ -0,0 +1,65 @@ +module MessageRequest + +import ..Layer, ..RequestStack.request +using ..URIs +using ..Messages + +struct MessageLayer{Next <: Layer} <: Layer end +export MessageLayer + + +""" + request(MessageLayer, method, uri [, headers=[] [, body="" ]; kw args...) + +Execute a `Request` and return a `Response`. + +kw args: + +- `parent=` optionally set a parent `Response`. + +- `response_stream=` optional `IO` stream for response body. + + +e.g. use a stream as a request body: + +``` +io = open("request", "r") +r = request("POST", "http://httpbin.org/post", [], io) +``` + +e.g. send a response body to a stream: + +``` +io = open("response_file", "w") +r = request("GET", "http://httpbin.org/stream/100", response_stream=io) +println(stat("response_file").size) +0 +sleep(1) +println(stat("response_file").size) +14990 +``` +""" + +function request(::Type{MessageLayer{Next}}, + method::String, uri, headers=[], body=""; + bodylength=Messages.Bodies.unknownlength, + parent=nothing, + response_stream=nothing, + kw...) where Next + + u = URI(uri) + url = method == "CONNECT" ? hostport(u) : resource(u) + + req = Request(method, url, headers, Body(body, bodylength); + parent=parent) + + defaultheader(req, "Host" => u.host) + setlengthheader(req) + + res = Response(body=Body(response_stream), parent=req) + + return request(Next, u, req, res; kw...) +end + + +end # module MessageRequest diff --git a/src/Messages.jl b/src/Messages.jl index 0f2117ab4..eae2d10bc 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -3,13 +3,15 @@ module Messages export Message, Request, Response, Body, method, iserror, isredirect, parentcount, isstream, header, setheader, defaultheader, setlengthheader, - waitforheaders + waitforheaders, wait, + writeandread import ..HTTP include("Bodies.jl") using .Bodies +using ..IOExtras using ..Pairs using ..Parsers import ..Parsers @@ -62,10 +64,11 @@ Represents a HTTP Response Message. - `headers::Vector{Pair{String,String}}` - `body::`[`HTTP.Body`](@ref) - `parent::Request`, the `Request` that yielded this `Response`. -- `headerscomplete::Condition`, raised when the `Parser` has finished - reading the response headers. This allows the `status` and `header` fields +- `complete::Condition`, raised when the `Parser` has finished + reading the Response Headers. This allows the `status` and `header` fields to be read used asynchronously without waiting for the entire body to be parsed. + `complete` is also raised when the entire Response Body has been read. """ mutable struct Response @@ -74,7 +77,7 @@ mutable struct Response headers::Vector{Pair{String,String}} body::Body parent - headerscomplete::Condition + complete::Condition end Response(status::Int=0, headers=[]; body=Body(), parent=nothing) = @@ -92,7 +95,7 @@ const Message = Union{Request,Response} Does this `Response` have an error status? """ -iserror(r::Response) = r.status < 200 || r.status >= 300 +iserror(r::Response) = (r.status < 200 || r.status >= 300) && !isredirect(r) """ @@ -144,7 +147,16 @@ statustext(r::Response) = Base.get(Parsers.STATUS_CODES, r.status, "Unknown Code Wait for the `Parser` (in a different task) to finish parsing the headers. """ -waitforheaders(r::Response) = while r.status == 0; wait(r.headerscomplete) end +waitforheaders(r::Response) = while r.status == 0; wait(r.complete) end + + +""" + wait(::Response) + +Wait for the `Parser` (in a different task) to finish parsing the `Response`. +""" + +Base.wait(r::Response) = while isopen(r.body); wait(r.complete) end """ @@ -294,7 +306,7 @@ function readstartline!(r::Response, m::Parsers.Message) if isredirect(r) r.body = Body() end - notify(r.headerscomplete) + notify(r.complete) yield() return end @@ -336,6 +348,28 @@ function Base.read!(io::IO, m::Message) end +""" + writeandread(::IO, ::Request, ::Response) + +Send a `Request` and receive a `Response`. +""" + +function writeandread(io::IO, req::Request, res::Response) + + try ;@debug 1 "write to: $io\n$req" + write(io, req) + closewrite(io) + read!(io, res) + closeread(io) ;@debug 2 "read from: $io\n$res" + catch e + @schedule close(io) + rethrow(e) + end + + return res +end + + Base.take!(m::Message) = take!(m.body) diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index 683171885..102ffbdf3 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -1,17 +1,15 @@ module RedirectRequest -struct RedirectLayer{T} end -export RedirectLayer - -import ..HTTP.RequestStack.request - +import ..Layer, ..RequestStack.request using ..URIs using ..Messages using ..Pairs: setkv using ..Strings.tocameldash! - import ..@debug, ..DEBUG_LEVEL +abstract type RedirectLayer{Next <: Layer} <: Layer end +export RedirectLayer + function request(::Type{RedirectLayer{Next}}, method::String, uri, headers=[], body=""; diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 05e85b446..1ba82ab84 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -1,10 +1,10 @@ module RetryRequest -struct RetryLayer{T} end -export RetryLayer - -import ..HTTP.RequestStack.request import ..HTTP +import ..Layer, ..RequestStack.request + +abstract type RetryLayer{Next <: Layer} <: Layer end +export RetryLayer isrecoverable(e::Base.UVError) = true diff --git a/src/SendRequest.jl b/src/SendRequest.jl deleted file mode 100644 index b3b804928..000000000 --- a/src/SendRequest.jl +++ /dev/null @@ -1,135 +0,0 @@ -module SendRequest - -struct MessageLayer{T} end -export MessageLayer - -struct ConnectLayer{T} end -export ConnectLayer - -import ..HTTP.RequestStack.request - -using ..URIs -using ..Messages - -using ..Connect -using ..IOExtras -using MbedTLS.SSLContext - - -import ..@debug, ..DEBUG_LEVEL - - -""" - writeandread(::IO, ::Request, ::Response) - -Send a `Request` and receive a `Response`. -""" - -function writeandread(io::IO, req::Request, res::Response) - - try ;@debug 1 "write to: $io\n$req" - write(io, req) - closewrite(io) - read!(io, res) - closeread(io) ;@debug 2 "read from: $io\n$res" - catch e - @schedule close(io) - rethrow(e) - end - - return res -end - - -""" - request(::IO, ::Request, ::Response) - -Send a `Request` and receive a `Response`. -""" - -function request(io::IO, req::Request, res::Response) - - # Run request in a background task if response body is a stream... - if isstream(res.body) - @schedule writeandread(io, req, res) - waitforheaders(res) - return res - end - - return writeandread(io, req, res) -end - - -""" - request(::URI, ::Request, ::Response) - -Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. -""" - - -function request(::Type{ConnectLayer{Connection}}, - uri::URI, req::Request, res::Response; kw...) where Connection - - # Get a connection from the pool... - T = uri.scheme == "https" ? SSLContext : TCPSocket - io = getconnection(Connection{T}, uri.host, uri.port; kw...) - - return request(io, req, res) -end - - -""" - request(method, uri [, headers=[] [, body="" ]; kw args...) - -Execute a `Request` and return a `Response`. - -kw args: - -- `parent=` optionally set a parent `Response`. - -- `response_stream=` optional `IO` stream for response body. - - -e.g. use a stream as a request body: - -``` -io = open("request", "r") -r = request("POST", "http://httpbin.org/post", [], io) -``` - -e.g. send a response body to a stream: - -``` -io = open("response_file", "w") -r = request("GET", "http://httpbin.org/stream/100", response_stream=io) -println(stat("response_file").size) -0 -sleep(1) -println(stat("response_file").size) -14990 -``` -""" - -function request(::Type{MessageLayer{Next}}, - method::String, uri, headers=[], body=""; - bodylength=Messages.Bodies.unknownlength, - parent=nothing, - response_stream=nothing, - kw...) where Next - - u = URI(uri) - url = method == "CONNECT" ? hostport(u) : resource(u) - - req = Request(method, url, headers, Body(body, bodylength); - parent=parent) - - defaultheader(req, "Host" => u.host) - setlengthheader(req) - - res = Response(body=Body(response_stream), parent=req) - - return request(Next, u, req, res; kw...) -end - - -end # module SendRequest diff --git a/src/SocketRequest.jl b/src/SocketRequest.jl new file mode 100644 index 000000000..f203c39f3 --- /dev/null +++ b/src/SocketRequest.jl @@ -0,0 +1,29 @@ +module SocketRequest + +import ..Layer, ..RequestStack.request +using ..Messages + +abstract type SocketLayer <: Layer end +export SocketLayer + + +""" + request(SocketLayer, ::IO, ::Request, ::Response) + +Send a `Request` and receive a `Response`. +Run the `Request` in a background task if response body is a stream. +""" + +function request(::Type{SocketLayer}, io::IO, req::Request, res::Response) + + if isstream(res.body) + @schedule writeandread(io, req, res) + waitforheaders(res) + return res + end + + return writeandread(io, req, res) +end + + +end # module SendRequest From ea0a9c969558cc823bdbba30945c9fdac54000c8 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 13 Dec 2017 15:54:44 +1100 Subject: [PATCH 046/182] note that header functions are case-insensitive --- docs/src/index.md | 4 ++-- src/Messages.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 8c7c60af0..eef2d9b60 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -125,7 +125,7 @@ with data from the `IO` stream. As the Parser processes the data the callbacks are called to fill in the `Message` struct. The `Response` struct has a `parent` field that points to the corresponding -`Request`. The `Request` struct as a `parent` field that points to a `Response` +`Request`. The `Request` struct has a `parent` field that points to a `Response` in the case of HTTP Redirect. @@ -137,7 +137,7 @@ order. Header values can be accessed by name using [`HTTP.Messages.header`](@ref) and -[`HTTP.Messages.setheader`](@ref). +[`HTTP.Messages.setheader`](@ref) (case-insensitive). The [`HTTP.Messages.appendheader`](@ref) function handles combining multi-line values, repeated header fields and special handling of diff --git a/src/Messages.jl b/src/Messages.jl index eae2d10bc..e821b34be 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -162,7 +162,7 @@ Base.wait(r::Response) = while isopen(r.body); wait(r.complete) end """ header(::Message, key [, default=""]) -> String -Get header value for `key`. +Get header value for `key` (case-insensitive). """ header(m, k::String, d::String="") = getbyfirst(m.headers, k, k => d, lceq)[2] lceq(a,b) = lowercase(a) == lowercase(b) @@ -171,7 +171,7 @@ lceq(a,b) = lowercase(a) == lowercase(b) """ setheader(::Message, key => value) -Set header `value` for `key`. +Set header `value` for `key` (case-insensitive). """ setheader(m, v::Pair) = setbyfirst(m.headers, Pair{String,String}(v), lceq) From 808b5772b0375e2db3d07f0f2dd75ace7d6550e9 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 13 Dec 2017 16:01:17 +1100 Subject: [PATCH 047/182] doc typos --- docs/src/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index eef2d9b60..eeef8be6a 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -162,7 +162,7 @@ known at the time the headers are sent. Source: `Connect.jl` [`HTTP.Connect.getconnection`](@ref) creates a new `TCPSocket` or `SSLContext` -for a specified `host` and `port. +for a specified `host` and `port`. No connection streaming, pooling or reuse is implemented in this module. However, the `getconnection` interface is the same as the one used by the @@ -322,7 +322,7 @@ Source: `RetryRequest.jl` The `RetryRequest` module implements a `request` method with a retry loop that repeats the request in the event of a recoverable network error. A randomised exponentially increasing delay is introduced between attempts to -avoid exacerbating making network congestion. +avoid exacerbating network congestion. Methods of `isrecoverable(e)` define which exception types lead to a retry. e.g. `Base.UVError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` From 4b601f50a023ff8a3af620b75aeb666186512ac8 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 14 Dec 2017 00:02:34 +1100 Subject: [PATCH 048/182] Cache Parser in ConnectionPool. Make stack dynamically configurable. --- docs/src/index.md | 19 ++++++++++----- src/Connect.jl | 5 +++- src/ConnectionPool.jl | 14 +++++++---- src/ConnectionRequest.jl | 33 ++++++++++++++++---------- src/HTTP.jl | 51 +++++++++++++++++++++++++++------------- src/Messages.jl | 17 ++++++++------ src/Parsers.jl | 10 ++++---- src/client.jl | 2 +- test/client.jl | 6 ++--- test/parser.jl | 16 ++++++------- 10 files changed, 110 insertions(+), 63 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index eeef8be6a..03ab02e44 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -273,7 +273,8 @@ const DefaultStack = RetryLayer{ ExceptionLayer{ MessageLayer{ - ConnectionLayer{ConnectionPool.Connection, + #ConnectLayer{ + ConnectionPoolLayer{ SocketLayer }}}}}}}} @@ -281,10 +282,6 @@ request(method::String, uri, headers=[], body=""; kw...) = request(HTTP.DefaultStack, method, uri, headers, body; kw...) ``` -Note that the `ConnectLayer`'s optional first parameter is a connection wrapper -type. If it was omitted then `ConnectionLayer` would use raw socket types from -the `Connect` module directly. - ## Redirect Layer @@ -348,7 +345,17 @@ This layer: - Creates a [`HTTP.Messages.Response`](@ref) object to hold the response. -## Connection Layer +## Connect Layer + +Source: `ConnectionRequest.jl` + +Alternative to Connection Pool Layer below. + +This layer calls [`HTTP.Connect.getconnection`](@ref) +to get a non-pooled socket. + + +## Connection Pool Layer Source: `ConnectionRequest.jl` diff --git a/src/Connect.jl b/src/Connect.jl index 77230992b..64ed6532a 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -1,9 +1,10 @@ module Connect -export getconnection +export getconnection, getparser using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! +import ..Parsers.Parser import ..@debug, ..DEBUG_LEVEL @@ -38,5 +39,7 @@ function getconnection(::Type{SSLContext}, host::AbstractString, return io end +getparser(::IO) = Parser() + end # module Connect diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 60898aef0..e3ab209a8 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -1,12 +1,13 @@ module ConnectionPool -export getconnection +export getconnection, getparser using ..IOExtras import ..@debug, ..DEBUG_LEVEL import MbedTLS.SSLContext -import ..Connect.getconnection +import ..Connect: getconnection, getparser +import ..Parsers.Parser const ByteView = typeof(view(UInt8[], 1:0)) @@ -19,7 +20,7 @@ A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. - `host::String` - `port::String` -- `io::T` +- `io::T`, the `TCPSocket` or `SSLContext. - `excess::ByteView`, left over bytes read from the connection after the end of a response message. These bytes are probably the start of the next response message. @@ -29,6 +30,7 @@ A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. the first Response must be read before another Request can be written. - `readcount::Int`, number of Response Messages that have been read. - `readdone::Condition`, signals that an entire Response Messages has been read. +- -`parser::Parser`, reuse a `Parser` when this `Connection` is reused. """ mutable struct Connection{T <: IO} <: IO @@ -39,12 +41,13 @@ mutable struct Connection{T <: IO} <: IO writecount::Int readcount::Int readdone::Condition + parser::Parser end isbusy(c::Connection) = c.writecount - c.readcount > 1 Connection{T}(host::AbstractString, port::AbstractString, io::T) where T <: IO = - Connection{T}(host, port, io, view(UInt8[], 1:0), 0, 0, Condition()) + Connection{T}(host, port, io, view(UInt8[], 1:0), 0, 0, Condition(), Parser()) const noconnection = Connection{TCPSocket}("","",TCPSocket()) @@ -168,6 +171,9 @@ function getconnection(::Type{Connection{T}}, end +getparser(c::Connection) = c.parser + + function Base.show(io::IO, c::Connection) print(io, c.host, ":", c.port != "" ? c.port : Int(peerport(c)), ":", diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index ed8ceecff..c3c9ab332 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -3,12 +3,15 @@ module ConnectionRequest import ..Layer, ..RequestStack.request using ..URIs using ..Messages -using ..Connect +using ..ConnectionPool using MbedTLS.SSLContext -abstract type ConnectionLayer{Connection, Next <: Layer} <: Layer end -export ConnectionLayer +abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end +export ConnectionPoolLayer + + +sockettype(uri::URI) = uri.scheme == "https" ? SSLContext : TCPSocket """ @@ -17,20 +20,26 @@ export ConnectionLayer Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. """ -function request(::Type{ConnectionLayer{Connection, Next}}, - uri::URI, req::Request, res::Response; - kw...) where Next where Connection - - Socket = uri.scheme == "https" ? SSLContext : TCPSocket +function request(::Type{ConnectionPoolLayer{Next}}, + uri::URI, req::Request, res::Response; kw...) where Next - io = getconnection(Connection{Socket}, uri.host, uri.port; kw...) + Connection = ConnectionPool.Connection{sockettype(uri)} + io = getconnection(Connection, uri.host, uri.port; kw...) return request(Next, io, req, res) end -# If no `Connection` wrapper type is provided, `Union` acts as a no-op. -request(::Type{ConnectionLayer{Next}}, a...; kw...) where Next <: Layer = - request(ConnectionLayer{Union, Next}, a...; kw...) + +abstract type ConnectLayer{Next <: Layer} <: Layer end +export ConnectLayer + +function request(::Type{ConnectLayer{Next}}, + uri::URI, req::Request, res::Response; kw...) where Next + + io = getconnection(sockettype(uri), uri.host, uri.port; kw...) + + return request(Next, io, req, res) +end end # module ConnectionRequest diff --git a/src/HTTP.jl b/src/HTTP.jl index fc2e6a3b1..38dd1b0d3 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -26,8 +26,11 @@ end abstract type Layer end module RequestStack + import ..HTTP - request(m::String, a...; kw...) = request(HTTP.DefaultStack, m, a...; kw...) + #request(m::String, a...; kw...) = request(HTTP.DefaultStack, m, a...; kw...) + request(m::String, a...; kw...) = request(HTTP.stack(;kw...), m, a...; kw...) + end #FIXME @@ -52,11 +55,9 @@ include("multipart.jl") include("Parsers.jl") import .Parsers.ParsingError -include("Messages.jl") - include("Connect.jl") include("ConnectionPool.jl") - +include("Messages.jl") include("types.jl") include("client.jl") @@ -69,6 +70,8 @@ using .Nitrogen #include("precompile.jl") + + function __init__() global const DEFAULT_CLIENT = Client() end @@ -94,21 +97,37 @@ using .CanonicalizeRequest include("RedirectRequest.jl") using .RedirectRequest +const NoLayer = Union + +function stack(;redirect=true, + basicauthorization=false, + cookies=false, + retry=true, + statusexception=true, + connectionpool=true, + kw...) + + (redirect ? RedirectLayer : NoLayer){ + (basicauthorization ? BasicAuthLayer : NoLayer){ + (cookies ? CookieLayer : NoLayer){ + (retry ? RetryLayer : NoLayer){ + (statusexception ? ExceptionLayer : NoLayer){ + MessageLayer{ + (connectionpool ? ConnectionPoolLayer : ConnectLayer){ + SocketLayer + }}}}}}} +end -const DefaultStack = - RedirectLayer{ - #CanonicalizeLayer{ - BasicAuthLayer{ - CookieLayer{ - RetryLayer{ - ExceptionLayer{ - MessageLayer{ - ConnectionLayer{ConnectionPool.Connection, - SocketLayer - }}}}}}}#} - +const MinimalStack = MessageLayer{ConnectLayer{SocketLayer}} +const DefaultStack = stack() +function _precompile_() + ccall(:jl_generating_output, Cint, ()) == 1 || return nothing + @assert precompile(HTTP.RequestStack.request, + (Type{DefaultStack}, String, String, Vector{Pair{String,String}}, String)) +end +_precompile_() end # module #= diff --git a/src/Messages.jl b/src/Messages.jl index e821b34be..1343ee2f4 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -15,6 +15,7 @@ using ..IOExtras using ..Pairs using ..Parsers import ..Parsers +import ..ConnectionPool import ..@debug, ..DEBUG_LEVEL @@ -76,12 +77,12 @@ mutable struct Response status::Int16 headers::Vector{Pair{String,String}} body::Body - parent complete::Condition + parent end Response(status::Int=0, headers=[]; body=Body(), parent=nothing) = - Response(v"1.1", status, headers, body, parent, Condition()) + Response(v"1.1", status, headers, body, Condition(), parent) Response(bytes) = read!(IOBuffer(bytes), Response()) Base.parse(::Type{Response}, str::AbstractString) = Response(str) @@ -320,12 +321,12 @@ end """ - Parser(::Message) + connectparser(::Message, ::Parser) -Create a parser that stores parsed data into a `Message`. +Configure a `Parser` to store parsed data into this `Message`. """ -function Parsers.Parser(m::Message) - p = Parser() +function connectparser(m::Message, p::Parser) + reset!(p) p.onbodyfragment = x->write(m.body, x) p.onheader = x->appendheader(m, x) p.onheaderscomplete = x->readstartline!(m, x) @@ -342,7 +343,9 @@ Read data from `io` into a `Message` struct. """ function Base.read!(io::IO, m::Message) - read!(io, Parser(m)) + parser = ConnectionPool.getparser(io) + connectparser(m, parser) + read!(io, parser) close(m.body) return m end diff --git a/src/Parsers.jl b/src/Parsers.jl index fc12f578f..2a9e7b080 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -24,7 +24,7 @@ module Parsers -export Parser, parse!, +export Parser, parse!, reset!, messagecomplete, headerscomplete, waitingforeof, ParsingError, ParsingErrorCode @@ -134,7 +134,7 @@ end Create an unconfigured `Parser`. """ -Parser() = Parser(false, x->nothing, x->nothing, ()->nothing, +Parser() = Parser(false, x->nothing, x->nothing, x->nothing, s_start_req_or_res, 0, 0, 0, 0, IOBuffer(), IOBuffer(), Message()) @@ -271,7 +271,7 @@ macro errorifstrict(cond) end macro passert(cond) - enable_passert ? esc(:(@assert($cond))) : :() + enable_passert ? esc(:(@assert $cond)) : :() end macro methodstate(meth, i, char) @@ -781,7 +781,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int parser.header_state = h_general end else - error("Unknown header_state") + @err HPE_INVALID_INTERNAL_STATE end p += 1 end @@ -874,7 +874,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int p = crlf == 0 ? len : p + crlf - 2 elseif h == h_connection || h == h_transfer_encoding - error("Shouldn't get here.") + @err HPE_INVALID_INTERNAL_STATE elseif h == h_content_length t = UInt64(0) if ch == ' ' diff --git a/src/client.jl b/src/client.jl index 2972e4780..adc023591 100644 --- a/src/client.jl +++ b/src/client.jl @@ -64,7 +64,7 @@ function request(client::Client, method, uri::URI; h = [k => v for (k,v) in headers] if stream - push!(args, :response_stream => BufferStream()) + push!(args, (:response_stream, BufferStream())) end if isa(body, Dict) diff --git a/test/client.jl b/test/client.jl index 3b14040fb..017478ad3 100644 --- a/test/client.jl +++ b/test/client.jl @@ -10,7 +10,7 @@ for sch in ("http", "https") @test HTTP.status(HTTP.get("$sch://httpbin.org/ip")) == 200 @test HTTP.status(HTTP.head("$sch://httpbin.org/ip")) == 200 @test HTTP.status(HTTP.options("$sch://httpbin.org/ip")) == 200 - @test HTTP.status(HTTP.post("$sch://httpbin.org/ip"; statusraise=false)) == 405 + @test HTTP.status(HTTP.post("$sch://httpbin.org/ip"; statusexception=false)) == 405 @test HTTP.status(HTTP.post("$sch://httpbin.org/post")) == 200 @test HTTP.status(HTTP.put("$sch://httpbin.org/put")) == 200 @test HTTP.status(HTTP.delete("$sch://httpbin.org/delete")) == 200 @@ -29,10 +29,10 @@ for sch in ("http", "https") println("cookie requests") empty!(HTTP.CookieRequest.default_cookiejar) - r = HTTP.get("$sch://httpbin.org/cookies") + r = HTTP.get("$sch://httpbin.org/cookies", cookies=true) body = String(take!(r)) @test body == "{\n \"cookies\": {}\n}\n" - r = HTTP.get("$sch://httpbin.org/cookies/set?hey=sailor&foo=bar") + r = HTTP.get("$sch://httpbin.org/cookies/set?hey=sailor&foo=bar", cookies=true) @test HTTP.status(r) == 200 body = String(take!(r)) @test body == "{\n \"cookies\": {\n \"foo\": \"bar\", \n \"hey\": \"sailor\"\n }\n}\n" diff --git a/test/parser.jl b/test/parser.jl index 72a850cbc..b92f5eaf1 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1391,7 +1391,7 @@ const responses = Message[ if t > 0 @sync begin r = Request() - p = Messages.Parser(r) + p = Messages.connectparser(r, Parser()) bytes = Vector{UInt8}(req.raw) sz = t for i in 1:sz:length(bytes) @@ -1601,7 +1601,7 @@ const responses = Message[ try if t > 0 r = Response() - p = Messages.Parser(r) + p = Messages.connectparser(r, Parser()) bytes = Vector{UInt8}(resp.raw) sz = t for i in 1:sz:length(bytes) @@ -1709,7 +1709,7 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "1844674407370955160" * "\r\n\r\n" r = Response() - p = Messages.Parser(r) + p = Messages.connectparser(r, Parser()) parse!(p, respstr) @test r.status == 200 @test r.headers == ["Content-Length"=>"1844674407370955160"] @@ -1724,7 +1724,7 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." r = Response() - p = Messages.Parser(r) + p = Messages.connectparser(r, Parser()) parse!(p, respstr) @test r.status == 200 @test r.headers == ["Transfer-Encoding"=>"chunked"] @@ -1740,7 +1740,7 @@ const responses = Message[ HTTP.Parsers.reset!(p) reqstr = "POST / HTTP/1.0\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = Request() - p = Messages.Parser(r) + p = Messages.connectparser(r, Parser()) parse!(p, reqstr) @test headerscomplete(p) @test !messagecomplete(p) @@ -1758,7 +1758,7 @@ const responses = Message[ HTTP.Parsers.reset!(p) respstr = "HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = Response() - p = Messages.Parser(r) + p = Messages.connectparser(r, Parser()) parse!(p, respstr) @test headerscomplete(p) @test !messagecomplete(p) @@ -1774,7 +1774,7 @@ const responses = Message[ reqstr = requests[1].raw * requests[2].raw r = Request() - p = Messages.Parser(r) + p = Messages.connectparser(r, Parser()) n = parse!(p, reqstr) @test headerscomplete(p) @test messagecomplete(p) @@ -1790,7 +1790,7 @@ const responses = Message[ @test r.headers == ["Test" => "Düsseldorf"] r = Response() - p = Messages.Parser(r) + p = Messages.connectparser(r, Parser()) parse!(p, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") @test String(take!(r.body)) == "fooba" From fe07a9bd4b06fd1e62a9a817c191e53db1911ce6 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 14 Dec 2017 09:41:34 +1100 Subject: [PATCH 049/182] simplify Base.read!(::IO, ::Parser) per https://github.com/JuliaWeb/MbedTLS.jl/pull/114 --- src/Parsers.jl | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/Parsers.jl b/src/Parsers.jl index 2a9e7b080..4acb327ac 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -154,26 +154,15 @@ Throws `ParsingError` if input is invalid. function Base.read!(io::IO, p::Parser; unread=IOExtras.unread!) - while !eof(io) + while !messagecomplete(p) && !eof(io) bytes = readavailable(io) - if isempty(bytes) - @debug 1 "Bug https://github.com/JuliaWeb/MbedTLS.jl/issues/113 !" - @assert isa(io, SSLContext) - @assert eof(io) - break - end - n = parse!(p, bytes) - - if messagecomplete(p) - if n < length(bytes) - unread(io, view(bytes, n+1:length(bytes))) - end - return + if n < length(bytes) + unread(io, view(bytes, n+1:length(bytes))) end end - if !waitingforeof(p) + if eof(io) && !waitingforeof(p) throw(ParsingError(headerscomplete(p) ? HPE_BODY_INCOMPLETE : HPE_HEADERS_INCOMPLETE)) end From dc95c38cfc8b6af455dae36330d419f23d5a4f6b Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 14 Dec 2017 09:45:47 +1100 Subject: [PATCH 050/182] fix prior broken commit --- src/Parsers.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Parsers.jl b/src/Parsers.jl index 4acb327ac..e89f257a0 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -154,15 +154,18 @@ Throws `ParsingError` if input is invalid. function Base.read!(io::IO, p::Parser; unread=IOExtras.unread!) - while !messagecomplete(p) && !eof(io) + while !eof(io) bytes = readavailable(io) n = parse!(p, bytes) if n < length(bytes) unread(io, view(bytes, n+1:length(bytes))) end + if messagecomplete(p) + return + end end - if eof(io) && !waitingforeof(p) + if !waitingforeof(p) throw(ParsingError(headerscomplete(p) ? HPE_BODY_INCOMPLETE : HPE_HEADERS_INCOMPLETE)) end From 0a654ebddaeb39a73d905c51efd8ffa1c63eecd7 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 14 Dec 2017 10:06:07 +1100 Subject: [PATCH 051/182] test/REQUIRE JSON --- test/REQUIRE | 1 + 1 file changed, 1 insertion(+) create mode 100644 test/REQUIRE diff --git a/test/REQUIRE b/test/REQUIRE new file mode 100644 index 000000000..732835a50 --- /dev/null +++ b/test/REQUIRE @@ -0,0 +1 @@ +JSON From 3a79361c2eefd7e4f40ee86c452192cb3655e394 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 14 Dec 2017 10:13:58 +1100 Subject: [PATCH 052/182] fix for syntax: unexpected "catch" on 0.7 --- src/server.jl | 2 +- src/utils.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server.jl b/src/server.jl index 3294f863e..6a9bb5824 100644 --- a/src/server.jl +++ b/src/server.jl @@ -110,7 +110,7 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, end length(buffer) > 0 || break starttime[] = time() # reset the timeout while still receiving bytes - err = HTTP.@catch HTTP.ParsingError HTTP.parse!(parser, buffer) + err = HTTP.@catcherr HTTP.ParsingError HTTP.parse!(parser, buffer) startedprocessingrequest = true if err != nothing # error in parsing the http request diff --git a/src/utils.jl b/src/utils.jl index 0e7476333..93418ed08 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -20,7 +20,7 @@ macro log(stmt) return esc(:(verbose && (write(logger, "[HTTP - $(rpad(Dates.now(), 23, ' '))]: $($stmt)\n"); flush(logger)))) end -macro catch(etype, expr) +macro catcherr(etype, expr) esc(quote try $expr From b3625f82eeed580cb9321bffaca12e2fa30e1a6a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 14 Dec 2017 14:00:02 +1100 Subject: [PATCH 053/182] temporarily limit travis to v0.6/linux --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 70f319b50..422cbee9d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,10 @@ language: julia os: - linux - - osx +# - osx julia: - 0.6 - - nightly +# - nightly notifications: email: false after_success: From a4e13a4280234eefe21d1a4852fd89f7091a5e11 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 14 Dec 2017 22:21:28 +1100 Subject: [PATCH 054/182] Ensure that all old client.jl keyword options are handeled or produce a depricat ion message. Simplify Client.options by using ::Vector{Tuple{Symbol,Any}} instead of ::RequestOptions. Add minimal flag in HTTP.jl to use a stripped down stack config. --- src/Connect.jl | 20 ++++--- src/HTTP.jl | 126 ++++++++++++++++---------------------------- src/Pairs.jl | 18 ++++++- src/RetryRequest.jl | 4 +- src/client.jl | 98 ++++++++++++++++++++++++---------- src/precompile.jl | 2 + src/server.jl | 22 ++++---- src/types.jl | 87 ------------------------------ test/client.jl | 4 +- 9 files changed, 161 insertions(+), 220 deletions(-) diff --git a/src/Connect.jl b/src/Connect.jl index 64ed6532a..dbb01cf51 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -18,21 +18,27 @@ The `Connections` module has the same interface but supports connection reuse and request interleaving. """ -function getconnection(::Type{TCPSocket}, host::AbstractString, - port::AbstractString; - kw...)::TCPSocket +function getconnection(::Type{TCPSocket}, + host::AbstractString, + port::AbstractString; + kw...)::TCPSocket + p::UInt = isempty(port) ? UInt(80) : parse(UInt, port) @debug 2 "TCP connect: $host:$p..." connect(getaddrinfo(host), p) end -function getconnection(::Type{SSLContext}, host::AbstractString, - port::AbstractString; - kw...)::SSLContext +function getconnection(::Type{SSLContext}, + host::AbstractString, + port::AbstractString; + require_ssl_verification=false, + sslconfig=SSLConfig(require_ssl_verification), + kw...)::SSLContext + port = isempty(port) ? "443" : port @debug 2 "SSL connect: $host:$port..." io = SSLContext() - setup!(io, SSLConfig(false)) + setup!(io, sslconfig) associate!(io, getconnection(TCPSocket, host, port)) hostname!(io, host) handshake!(io) diff --git a/src/HTTP.jl b/src/HTTP.jl index 38dd1b0d3..f7c01f60f 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -3,13 +3,10 @@ module HTTP using MbedTLS import MbedTLS.SSLContext -const TLS = MbedTLS import Base.== # FIXME rm -const DEBUG = false # FIXME rm -const PARSING_DEBUG = false # FIXME rm const DEBUG_LEVEL = 1 if VERSION > v"0.7.0-DEV.2338" @@ -22,118 +19,83 @@ else import Dates end - -abstract type Layer end +const minimal = false module RequestStack import ..HTTP - #request(m::String, a...; kw...) = request(HTTP.DefaultStack, m, a...; kw...) request(m::String, a...; kw...) = request(HTTP.stack(;kw...), m, a...; kw...) end -#FIXME -status(r) = r.status -headers(r) = Dict(r.headers) - include("debug.jl") include("Pairs.jl") include("Strings.jl") include("IOExtras.jl") - +include("uri.jl"); using .URIs + if !minimal include("consts.jl") include("utils.jl") - -include("uri.jl") -using .URIs -include("fifobuffer.jl") -using .FIFOBuffers -include("cookies.jl") -using .Cookies +include("fifobuffer.jl"); using .FIFOBuffers +include("cookies.jl"); using .Cookies include("multipart.jl") - -include("Parsers.jl") -import .Parsers.ParsingError + end +include("Parsers.jl"); import .Parsers.ParsingError include("Connect.jl") include("ConnectionPool.jl") include("Messages.jl") -include("types.jl") -include("client.jl") -include("sniff.jl") - -include("handlers.jl") -using .Handlers -include("server.jl") -using .Nitrogen - -#include("precompile.jl") - - - -function __init__() - global const DEFAULT_CLIENT = Client() -end - - -include("SocketRequest.jl") -using .SocketRequest -include("ConnectionRequest.jl") -using .ConnectionRequest -include("MessageRequest.jl") -using .MessageRequest -include("ExceptionRequest.jl") -using .ExceptionRequest -import .ExceptionRequest.StatusError -include("RetryRequest.jl") -using .RetryRequest -include("CookieRequest.jl") -using .CookieRequest -include("BasicAuthRequest.jl") -using .BasicAuthRequest -include("CanonicalizeRequest.jl") -using .CanonicalizeRequest -include("RedirectRequest.jl") -using .RedirectRequest +abstract type Layer end const NoLayer = Union +include("SocketRequest.jl"); using .SocketRequest +include("ConnectionRequest.jl"); using .ConnectionRequest +include("MessageRequest.jl"); using .MessageRequest + if !minimal +include("ExceptionRequest.jl"); using .ExceptionRequest + import .ExceptionRequest.StatusError +include("RetryRequest.jl"); using .RetryRequest +include("CookieRequest.jl"); using .CookieRequest +include("BasicAuthRequest.jl"); using .BasicAuthRequest +include("CanonicalizeRequest.jl"); using .CanonicalizeRequest +include("RedirectRequest.jl"); using .RedirectRequest + function stack(;redirect=true, basicauthorization=false, cookies=false, + canonicalizeheaders=false, retry=true, statusexception=true, connectionpool=true, kw...) - (redirect ? RedirectLayer : NoLayer){ - (basicauthorization ? BasicAuthLayer : NoLayer){ - (cookies ? CookieLayer : NoLayer){ - (retry ? RetryLayer : NoLayer){ - (statusexception ? ExceptionLayer : NoLayer){ - MessageLayer{ - (connectionpool ? ConnectionPoolLayer : ConnectLayer){ - SocketLayer - }}}}}}} + (redirect ? RedirectLayer : NoLayer){ + (basicauthorization ? BasicAuthLayer : NoLayer){ + (cookies ? CookieLayer : NoLayer){ + (canonicalizeheaders ? CanonicalizeLayer : NoLayer){ + (retry ? RetryLayer : NoLayer){ + (statusexception ? ExceptionLayer : NoLayer){ + MessageLayer{ + (connectionpool ? ConnectionPoolLayer : ConnectLayer){ + SocketLayer + }}}}}}}} end -const MinimalStack = MessageLayer{ConnectLayer{SocketLayer}} -const DefaultStack = stack() + else +stack(;kw...) = MessageLayer{ConnectLayer{SocketLayer}} + end -function _precompile_() - ccall(:jl_generating_output, Cint, ()) == 1 || return nothing - - @assert precompile(HTTP.RequestStack.request, - (Type{DefaultStack}, String, String, Vector{Pair{String,String}}, String)) +if !minimal +status(r) = r.status #FIXME +headers(r) = Dict(r.headers) #FIXME +include("types.jl") +include("client.jl") +include("sniff.jl") +include("handlers.jl"); using .Handlers +include("server.jl"); using .Nitrogen end -_precompile_() + +include("precompile.jl") end # module -#= -try - HTTP.parse(HTTP.Response, "HTTP/1.1 200 OK\r\n\r\n") - HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\n\r\n") - HTTP.get(HTTP.Client(nothing), "www.google.com") -end -=# diff --git a/src/Pairs.jl b/src/Pairs.jl index 8c8a0d26e..cbfe88095 100644 --- a/src/Pairs.jl +++ b/src/Pairs.jl @@ -7,7 +7,7 @@ export setbyfirst, getbyfirst, setkv, getkv setbyfirst(collection, item) -> item Set `item` in a `collection`. -If `first() of an exisiting matches `first(item)` it is replaced. +If `first() of an exisiting item matches `first(item)` it is replaced. Otherwise the new `item` is inserted at the end of the `collection`. """ @@ -34,6 +34,22 @@ function getbyfirst(c, k, default=nothing, eq = ==) end +""" + defaultbyfirst(collection, item) + +If `first(item)` does not match match `first()` of any existing items, +insert the new `item` at the end of the `collection`. +""" + +function defaultbyfirst(c, item, eq = ==) + k = first(item) + if (i = findfirst(x->eq(first(x), k), c)) == 0 + push!(c, item) + end + return +end + + """ setkv(collection, key, value) diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 1ba82ab84..2e2633dad 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -14,10 +14,10 @@ isrecoverable(e::HTTP.StatusError) = e.status < 200 || e.status >= 500 isrecoverable(e::Exception) = false -function request(::Type{RetryLayer{Next}}, a...; maxretries=2, kw...) where Next +function request(::Type{RetryLayer{Next}}, a...; retries=2, kw...) where Next retry(request, - delays=ExponentialBackOff(n = maxretries), + delays=ExponentialBackOff(n = retries), check=(s,ex)->(s,isrecoverable(ex)))(Next, a...; kw...) end diff --git a/src/client.jl b/src/client.jl index adc023591..3074b77e5 100644 --- a/src/client.jl +++ b/src/client.jl @@ -1,10 +1,10 @@ -using .Parsers +using .Pairs + """ - HTTP.Client([logger::IO]; args...) + HTTP.Client(;args...) A type to facilitate connections to remote hosts, send HTTP requests, and manage state between requests. -Takes an optional `logger` IO argument where client activity is recorded (defaults to `STDOUT`). Additional keyword arguments can be passed that will get transmitted with each HTTP request: * `chunksize::Int`: if a request body is larger than `chunksize`, the "chunked-transfer" http mechanism will be used and chunks will be sent no larger than `chunksize`; default = `nothing` @@ -24,29 +24,12 @@ Additional keyword arguments can be passed that will get transmitted with each H mutable struct Client # cookies are stored in-memory per host and automatically sent when appropriate cookies::Dict{String, Set{Cookie}} - # buffer::Vector{UInt8} #TODO: create a fixed size buffer for reading bytes off the wire and having http_parser use, this should keep allocations down, need to make sure MbedTLS supports blocking readbytes! - logger::Option{IO} # global request settings - options::RequestOptions - connectioncount::Int -end - -Client(logger::Option{IO}, options::RequestOptions) = Client( - Dict{String, Set{Cookie}}(), - logger, options, 1) - -# this is where we provide all the default request options -const DEFAULT_OPTIONS = :((nothing, true, Inf, Inf, nothing, 5, true, false, 3, true, true, false, true, true)) - -@eval begin - Client(logger::Option{IO}; args...) = Client(logger, RequestOptions($(DEFAULT_OPTIONS)...; args...)) - Client(; args...) = Client(nothing, RequestOptions($(DEFAULT_OPTIONS)...; args...)) -end - -function setclient!(client::Client) - global const DEFAULT_CLIENT = client + options::Vector{Tuple{Symbol,Any}} end +Client(;options...) = Client(Dict{String, Set{Cookie}}(), options) +global const DEFAULT_CLIENT = Client() # build Request function request(client::Client, method, uri::URI; @@ -56,22 +39,79 @@ function request(client::Client, method, uri::URI; stream::Bool=false, verbose::Bool=false, args...) - #opts = RequestOptions(; args...) - #not(client.logger) && (client.logger = STDOUT) - #client.logger != STDOUT && (verbose = true) + + # Add default values from client options to args... + for option in client.options + defaultbyfirst(args, option) + end + + if getkv(args, :chunksize, nothing) != nothing + Base.depwarn( + "The chunksize= option is deprecated and has no effect.\n" * + "Use a BufferStream and pass chunks of the desired size to `write`:\n" * + " io=BufferStream()\n" * + " request(\"PUT\", \"http://foo.bar/file\", body=io)\n" * + " write(io, \"chunk1\")\n" * + " write(io, \"chunk2\")\n", + :chunksize) + end + + if getkv(args, :connecttimeout, Inf) != Inf || + getkv(args, :readtimeout, Inf) != Inf + Base.depwarn( + "The connecttimeout= and readtimeout= options are deprecated " * + "and have no effect.\n" * + "See https://github.com/JuliaWeb/HTTP.jl/issues/114\n", + :connecttimeout) + end + + if getkv(args, :tlsconfig, nothing) != nothing + Base.depwarn( + "The tlsconfig= option is deprecated. Use sslconfig=::MbedTLS.SSLConfig", + :tlsconfig) + setkv(args, :sslconfig, getkv(args, :tlsconfig)) + end + + if getkv(args, :allowredirects, nothing) != nothing + Base.depwarn( + "The allowredirects= option is deprecated. Use redirect=::Bool", + :allowredirects) + setkv(args, :redirect, getkv(args, :allowredirects)) + end + + if getkv(args, :managecookies, nothing) != nothing + Base.depwarn( + "The managecookies= option is deprecated. Use cookies=::Bool", + :managecookies) + setkv(args, :cookies, getkv(args, :managecookies)) + end + setkv(args, :cookiejar, client.cookies) + + if getkv(args, :statusraise, nothing) != nothing + Base.depwarn( + "The statusraise= options is deprecated. Use statusexception=::Bool", + :statusraise) + setkv(args, :statusexception, getkv(args, :statusraise)) + end + + if getkv(args, :insecure, nothing) != nothing + Base.depwarn( + "The insecure= option is deprecated. Use require_ssl_verification=::Bool", + :insecure) + setkv(args, :require_ssl_verification, !getkv(args, :insecure)) + end m = string(method) h = [k => v for (k,v) in headers] - if stream push!(args, (:response_stream, BufferStream())) end if isa(body, Dict) body = HTTP.Form(body) - Pairs.setbyfirst(h, "Content-Type" => + setbyfirst(h, "Content-Type" => "multipart/form-data; boundary=$(body.boundary)") - Pairs.setkv(args, :bodylength, length(body)) + setkv(args, :bodylength, length(body)) end if !enablechunked && isa(body, IO) diff --git a/src/precompile.jl b/src/precompile.jl index eef0232ba..d2c7b9ed0 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -1,5 +1,6 @@ function _precompile_() ccall(:jl_generating_output, Cint, ()) == 1 || return nothing +#= @assert precompile(HTTP.URIs.parseurlchar, (UInt8, Char, Bool,)) @assert precompile(HTTP.status, (HTTP.Response,)) @assert precompile(HTTP.Cookies.pathmatch, (HTTP.Cookies.Cookie, String,)) @@ -198,5 +199,6 @@ function _precompile_() @assert precompile(HTTP.body, (Base.GenericIOBuffer{Array{UInt8, 1}}, HTTP.Request, HTTP.RequestOptions,)) @assert precompile(HTTP.startline, (Base.GenericIOBuffer{Array{UInt8, 1}}, HTTP.Request,)) end +=# end _precompile_() diff --git a/src/server.jl b/src/server.jl index 6a9bb5824..98004cb95 100644 --- a/src/server.jl +++ b/src/server.jl @@ -37,7 +37,7 @@ export Server, ServerOptions, serve # buffer re-use for server/client wire-reading # easter egg (response 418) mutable struct ServerOptions - tlsconfig::HTTP.TLS.SSLConfig + tlsconfig::HTTP.MbedTLS.SSLConfig readtimeout::Float64 ratelimit::Rational{Int} support100continue::Bool @@ -45,7 +45,7 @@ mutable struct ServerOptions logbody::Bool end -ServerOptions(; tlsconfig::HTTP.TLS.SSLConfig=HTTP.TLS.SSLConfig(true), +ServerOptions(; tlsconfig::HTTP.MbedTLS.SSLConfig=HTTP.MbedTLS.SSLConfig(true), readtimeout::Float64=180.0, ratelimit::Rational{Int64}=Int64(5)//Int64(1), support100continue::Bool=true, @@ -64,9 +64,9 @@ kill the julia process, interrupt (ctrl/cmd+c) if main task, or send the kill si `put!(server.in, HTTP.KILL)`. Supported keyword arguments include: - * `cert`: if https, the cert file to use, as passed to `HTTP.TLS.SSLConfig(cert, key)` - * `key`: if https, the key file to use, as passed to `HTTP.TLS.SSLConfig(cert, key)` - * `tlsconfig`: pass in an already-constructed `HTTP.TLS.SSLConfig` instance + * `cert`: if https, the cert file to use, as passed to `HTTP.MbedTLS.SSLConfig(cert, key)` + * `key`: if https, the key file to use, as passed to `HTTP.MbedTLS.SSLConfig(cert, key)` + * `tlsconfig`: pass in an already-constructed `HTTP.MbedTLS.SSLConfig` instance * `readtimeout`: how long a client connection will be left open without receiving any bytes * `ratelimit`: a `Rational{Int}` of the form `5//1` indicating how many `messages//second` should be allowed per client IP address; requests exceeding the rate limit will be dropped * `support100continue`: a `Bool` indicating whether `Expect: 100-continue` headers should be supported for delayed request body sending; default = `true` @@ -216,10 +216,10 @@ end initTLS!(::Type{HTTP.http}, tcp, tlsconfig) = return tcp function initTLS!(::Type{HTTP.https}, tcp, tlsconfig) try - tls = HTTP.TLS.SSLContext() - HTTP.TLS.setup!(tls, tlsconfig) - HTTP.TLS.associate!(tls, tcp) - HTTP.TLS.handshake!(tls) + tls = HTTP.MbedTLS.SSLContext() + HTTP.MbedTLS.setup!(tls, tlsconfig) + HTTP.MbedTLS.associate!(tls, tcp) + HTTP.MbedTLS.handshake!(tls) return tls catch e close(tcp) @@ -278,7 +278,7 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} rl.allowance -= 1.0 HTTP.@log "new tcp connection accepted, reading request..." let server=server, p=p, request=request, i=i, tcp=tcp, rl=rl - @async process!(server, p, request, i, initTLS!(T, tcp, server.options.tlsconfig::HTTP.TLS.SSLConfig), rl, Ref{Float64}(time()), verbose) + @async process!(server, p, request, i, initTLS!(T, tcp, server.options.tlsconfig::HTTP.MbedTLS.SSLConfig), rl, Ref{Float64}(time()), verbose) end i += 1 end @@ -310,7 +310,7 @@ function Server(handler::H=HTTP.HandlerFunction((req, rep) -> HTTP.Response("Hel key::String="", args...) where {H <: HTTP.Handler} if cert != "" && key != "" - server = Server{HTTP.https, H}(handler, logger, Channel(1), Channel(1), ServerOptions(; tlsconfig=HTTP.TLS.SSLConfig(cert, key), args...)) + server = Server{HTTP.https, H}(handler, logger, Channel(1), Channel(1), ServerOptions(; tlsconfig=HTTP.MbedTLS.SSLConfig(cert, key), args...)) else server = Server{HTTP.http, H}(handler, logger, Channel(1), Channel(1), ServerOptions(; args...)) end diff --git a/src/types.jl b/src/types.jl index a3c5060fe..9564b8e9a 100644 --- a/src/types.jl +++ b/src/types.jl @@ -2,92 +2,5 @@ abstract type Scheme end struct http <: Scheme end struct https <: Scheme end -# struct ws <: Scheme end -# struct wss <: Scheme end - -sockettype(::Type{http}) = TCPSocket -sockettype(::Type{https}) = TLS.SSLContext -schemetype(::Type{TCPSocket}) = http -schemetype(::Type{TLS.SSLContext}) = https const Headers = Dict{String, String} - -const Option{T} = Union{T, Void} -not(::Void) = true -not(x) = false -function get(value::T, name::Symbol, default::R)::R where {T, R} - val = getfield(value, name)::Option{R} - return not(val) ? default : val -end - -""" - RequestOptions(; chunksize=, connecttimeout=, readtimeout=, tlsconfig=, maxredirects=, allowredirects=) - -A type to represent various http request options. Lives as a separate type so that options can be set -at the `HTTP.Client` level to be applied to every request sent. Options include: - - * `chunksize::Int`: if a request body is larger than `chunksize`, the "chunked-transfer" http mechanism will be used and chunks will be sent no larger than `chunksize`; default = `nothing` - * `connecttimeout::Float64`: sets a timeout on how long to wait when trying to connect to a remote host; default = Inf. Note that while setting a timeout will affect the actual program control flow, there are current lower-level limitations that mean underlying resources may not actually be freed until their own timeouts occur (i.e. libuv sockets only timeout after 75 seconds, with no option to configure) - * `readtimeout::Float64`: sets a timeout on how long to wait when receiving a response from a remote host; default = Int - * `tlsconfig::TLS.SSLConfig`: a valid `TLS.SSLConfig` which will be used to initialize every https connection; default = `nothing` - * `maxredirects::Int`: the maximum number of redirects that will automatically be followed for an http request; default = 5 - * `allowredirects::Bool`: whether redirects should be allowed to be followed at all; default = `true` - * `forwardheaders::Bool`: whether user-provided headers should be forwarded on redirects; default = `false` - * `retries::Int`: # of times a request will be tried before throwing an error; default = 3 - * `managecookies::Bool`: whether the request client should automatically store and add cookies from/to requests (following appropriate host-specific & expiration rules); default = `true` - * `statusraise::Bool`: whether an `HTTP.StatusError` should be raised on a non-2XX response status code; default = `true` - * `insecure::Bool`: whether an "https" connection should allow insecure connections (no TLS verification); default = `false` - * `canonicalizeheaders::Bool`: whether header field names should be canonicalized in responses, e.g. `content-type` is canonicalized to `Content-Type`; default = `true` - * `logbody::Bool`: whether the request body should be logged when `verbose=true` is passed; default = `true` -""" -mutable struct RequestOptions - chunksize::Option{Int} - gzip::Option{Bool} - connecttimeout::Option{Float64} - readtimeout::Option{Float64} - tlsconfig::Option{TLS.SSLConfig} - maxredirects::Option{Int} - allowredirects::Option{Bool} - forwardheaders::Option{Bool} - retries::Option{Int} - managecookies::Option{Bool} - statusraise::Option{Bool} - insecure::Option{Bool} - canonicalizeheaders::Option{Bool} - logbody::Option{Bool} - RequestOptions(ch::Option{Int}, gzip::Option{Bool}, ct::Option{Float64}, rt::Option{Float64}, tls::Option{TLS.SSLConfig}, mr::Option{Int}, ar::Option{Bool}, fh::Option{Bool}, tr::Option{Int}, mc::Option{Bool}, sr::Option{Bool}, i::Option{Bool}, h::Option{Bool}, lb::Option{Bool}) = - new(ch, gzip, ct, rt, tls, mr, ar, fh, tr, mc, sr, i, h, lb) -end - -const RequestOptionsFieldTypes = Dict(:chunksize => Int, - :gzip => Bool, - :connecttimeout => Float64, - :readtimeout => Float64, - :tlsconfig => TLS.SSLConfig, - :maxredirects => Int, - :allowredirects => Bool, - :forwardheaders => Bool, - :retries => Int, - :managecookies => Bool, - :statusraise => Bool, - :insecure => Bool, - :canonicalizeheaders => Bool, - :logbody => Bool) - -function RequestOptions(options::RequestOptions; kwargs...) - for (k, v) in kwargs - setfield!(options, k, convert(RequestOptionsFieldTypes[k], v)) - end - return options -end - -RequestOptions(chunk=nothing, gzip=nothing, ct=nothing, rt=nothing, tls=nothing, mr=nothing, ar=nothing, fh=nothing, tr=nothing, mc=nothing, sr=nothing, i=nothing, h=nothing, lb=nothing; kwargs...) = - RequestOptions(RequestOptions(chunk, gzip, ct, rt, tls, mr, ar, fh, tr, mc, sr, i, h, lb); kwargs...) - -function update!(opts1::RequestOptions, opts2::RequestOptions) - for i = 1:nfields(RequestOptions) - f = fieldname(RequestOptions, i) - not(getfield(opts1, f)) && setfield!(opts1, f, getfield(opts2, f)) - end - return opts1 -end diff --git a/test/client.jl b/test/client.jl index 017478ad3..d62e08f0f 100644 --- a/test/client.jl +++ b/test/client.jl @@ -29,6 +29,7 @@ for sch in ("http", "https") println("cookie requests") empty!(HTTP.CookieRequest.default_cookiejar) + empty!(HTTP.DEFAULT_CLIENT.cookies) r = HTTP.get("$sch://httpbin.org/cookies", cookies=true) body = String(take!(r)) @test body == "{\n \"cookies\": {}\n}\n" @@ -168,9 +169,10 @@ for sch in ("http", "https") # custom client & other high-level entries println("high-level client request methods") + cli = HTTP.Client() +#= buf = IOBuffer() cli = HTTP.Client(buf) -#= FIXME HTTP.get(cli, "$sch://httpbin.org/ip") seekstart(buf) @test length(String(take!(buf))) > 0 From fd7c97da87654ff2784416ae81137a4f329c54bf Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 15 Dec 2017 09:29:32 +1100 Subject: [PATCH 055/182] Revert rename of parser.jl -> Parsers.jl to avoid making GitHub's diff display confused. --- src/HTTP.jl | 2 +- src/{Parsers.jl => parser.jl} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{Parsers.jl => parser.jl} (100%) diff --git a/src/HTTP.jl b/src/HTTP.jl index f7c01f60f..8533ddb5e 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -40,7 +40,7 @@ include("fifobuffer.jl"); using .FIFOBuffers include("cookies.jl"); using .Cookies include("multipart.jl") end -include("Parsers.jl"); import .Parsers.ParsingError +include("parser.jl"); import .Parsers.ParsingError include("Connect.jl") include("ConnectionPool.jl") include("Messages.jl") diff --git a/src/Parsers.jl b/src/parser.jl similarity index 100% rename from src/Parsers.jl rename to src/parser.jl From 024a5a581574a702da5c69434194dfa64527ddb9 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 16 Dec 2017 12:49:11 +1100 Subject: [PATCH 056/182] tweaks to "minimal" configuration --- src/HTTP.jl | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 8533ddb5e..a0c628ab4 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -7,7 +7,7 @@ import MbedTLS.SSLContext import Base.== # FIXME rm -const DEBUG_LEVEL = 1 +const DEBUG_LEVEL = 0 if VERSION > v"0.7.0-DEV.2338" using Base64 @@ -27,6 +27,9 @@ module RequestStack request(m::String, a...; kw...) = request(HTTP.stack(;kw...), m, a...; kw...) end + if minimal +import .RequestStack.request + end include("debug.jl") include("Pairs.jl") @@ -43,23 +46,23 @@ include("multipart.jl") include("parser.jl"); import .Parsers.ParsingError include("Connect.jl") include("ConnectionPool.jl") -include("Messages.jl") +include("Messages.jl"); using .Messages abstract type Layer end const NoLayer = Union -include("SocketRequest.jl"); using .SocketRequest -include("ConnectionRequest.jl"); using .ConnectionRequest -include("MessageRequest.jl"); using .MessageRequest +include("SocketRequest.jl"); using .SocketRequest +include("ConnectionRequest.jl"); using .ConnectionRequest +include("MessageRequest.jl"); using .MessageRequest +include("ExceptionRequest.jl"); using .ExceptionRequest + import .ExceptionRequest.StatusError if !minimal -include("ExceptionRequest.jl"); using .ExceptionRequest - import .ExceptionRequest.StatusError -include("RetryRequest.jl"); using .RetryRequest -include("CookieRequest.jl"); using .CookieRequest -include("BasicAuthRequest.jl"); using .BasicAuthRequest -include("CanonicalizeRequest.jl"); using .CanonicalizeRequest -include("RedirectRequest.jl"); using .RedirectRequest +include("RetryRequest.jl"); using .RetryRequest +include("CookieRequest.jl"); using .CookieRequest +include("BasicAuthRequest.jl"); using .BasicAuthRequest +include("CanonicalizeRequest.jl"); using .CanonicalizeRequest +include("RedirectRequest.jl"); using .RedirectRequest function stack(;redirect=true, basicauthorization=false, @@ -83,7 +86,11 @@ function stack(;redirect=true, end else -stack(;kw...) = MessageLayer{ConnectLayer{SocketLayer}} +stack(;kw...) = ExceptionLayer{ + MessageLayer{ + ConnectionPoolLayer{ + #ConnectLayer{ + SocketLayer}}} end if !minimal @@ -94,8 +101,8 @@ include("client.jl") include("sniff.jl") include("handlers.jl"); using .Handlers include("server.jl"); using .Nitrogen +include("precompile.jl") end -include("precompile.jl") end # module From 388433c7cdbaceaa95b410ba096a5f00ec485a0b Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 16 Dec 2017 12:50:16 +1100 Subject: [PATCH 057/182] fix connection pool locking for concurrent access to same connection --- src/ConnectionPool.jl | 57 ++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index e3ab209a8..00de1f398 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -24,13 +24,11 @@ A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. - `excess::ByteView`, left over bytes read from the connection after the end of a response message. These bytes are probably the start of the next response message. -- `writecount::Int` number of Request Messages that have been written. - `writecount` is allowed to be no more than two greater than `readcount` - (see `isbusy`). i.e. after two Requests have been written to a `Connection`, - the first Response must be read before another Request can be written. -- `readcount::Int`, number of Response Messages that have been read. -- `readdone::Condition`, signals that an entire Response Messages has been read. -- -`parser::Parser`, reuse a `Parser` when this `Connection` is reused. +- `writecount`, number of Request Messages that have been written. +- `readcount`, number of Response Messages that have been read. +- `writelock`, busy writing a Request to `io`. +- `readlock`, busy reading a Response from `io`. +- `parser::Parser`, reuse a `Parser` when this `Connection` is reused. """ mutable struct Connection{T <: IO} <: IO @@ -40,14 +38,15 @@ mutable struct Connection{T <: IO} <: IO excess::ByteView writecount::Int readcount::Int - readdone::Condition + writelock::ReentrantLock + readlock::ReentrantLock parser::Parser end -isbusy(c::Connection) = c.writecount - c.readcount > 1 Connection{T}(host::AbstractString, port::AbstractString, io::T) where T <: IO = - Connection{T}(host, port, io, view(UInt8[], 1:0), 0, 0, Condition(), Parser()) + Connection{T}(host, port, io, view(UInt8[], 1:0), 0, 0, + ReentrantLock(), ReentrantLock(), Parser()) const noconnection = Connection{TCPSocket}("","",TCPSocket()) @@ -72,7 +71,8 @@ end """ unread!(::Connection, bytes) -Push bytes back into a connection's `excess` buffer (to be returned by the next read). +Push bytes back into a connection's `excess` buffer +(to be returned by the next read). """ function IOExtras.unread!(c::Connection, bytes::ByteView) @@ -91,11 +91,17 @@ Increment `writecount` and wait for pending reads to complete. function IOExtras.closewrite(c::Connection) c.writecount += 1 - if isbusy(c) + if islocked(c.readlock) @debug 3 "Waiting to read: $c" - wait(c.readdone) end - @assert !isbusy(c) + if isopen(c.io) + lock(c.readlock) + if !isopen(c.io) + unlock(c.readlock) + end + unlock(c.writelock) + end + @assert isopen(c.io) == islocked(c.readlock) end @@ -107,11 +113,23 @@ Signal that an entire Response Message has been read from the `Connection`. Increment `readcount` and wake up waiting `closewrite`. """ -IOExtras.closeread(c::Connection) = (c.readcount += 1; notify(c.readdone)) - +function IOExtras.closeread(c::Connection) + c.readcount += 1 + if isopen(c.io) + unlock(c.readlock) + end +end -Base.close(c::Connection) = (close(c.io); notify(c.readdone)) +function Base.close(c::Connection) + close(c.io) + if islocked(c.readlock) + unlock(c.readlock) + end + if islocked(c.writelock) + unlock(c.writelock) + end +end """ @@ -145,7 +163,7 @@ function getconnection(::Type{Connection{T}}, lock(poollock) try - pattern = x->(!isbusy(x) && + pattern = x->(!islocked(x.writelock) && typeof(x.io) == T && x.host == host && x.port == port) @@ -156,13 +174,14 @@ function getconnection(::Type{Connection{T}}, deleteat!(pool, i) ;@debug 1 "Deleted: $c" continue end; ;@debug 2 "Reused: $c" + lock(c.writelock) return c end io = getconnection(T, host, port; kw...) c = Connection{T}(host, port, io) ;@debug 1 "New: $c" push!(pool, c) - @assert !isbusy(c) + lock(c.writelock) return c finally From d17d85124f421e97c7a4b0ed8562266e804ee156 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 16 Dec 2017 12:50:56 +1100 Subject: [PATCH 058/182] Parser tweaks - waitingforeof() true if p.state == s_start_req_or_res - Check isheadresponse before F_CHUNKED in s_headers_done state. HEAD response can include chunked, but has no body. - Consume trailing end of line after message. Saves unreading a stray newline at the end of a message. --- src/parser.jl | 28 +++++++++++++++++++++------- src/parseutils.jl | 12 ++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index e89f257a0..54ca3c6f5 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -164,6 +164,7 @@ function Base.read!(io::IO, p::Parser; unread=IOExtras.unread!) return end end + @debug 2 "read!(::$(typeof(io)), Parser($(ParsingStateCode(p.state)))) eof!" if !waitingforeof(p) throw(ParsingError(headerscomplete(p) ? HPE_BODY_INCOMPLETE : @@ -230,7 +231,8 @@ messagecomplete(p::Parser) = p.state >= s_message_done Is the `Parser` waiting for the peer to close the connection to signal the end of the Message Body? """ -waitingforeof(p::Parser) = p.state == s_body_identity_eof +waitingforeof(p::Parser) = p.state == s_body_identity_eof || + p.state == s_start_req_or_res isrequest(p::Parser) = p.message.status == 0 @@ -294,7 +296,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int isempty(bytes) && throw(ArgumentError("bytes must not be empty")) len = length(bytes) p_state = parser.state - @debug 3 "parse!(parser.state=$(ParsingStateCode(p_state))), $len-bytes)" + @debug 2 "parse!(parser.state=$(ParsingStateCode(p_state))), $len-bytes:\n" * + escapelines(String(collect(bytes))) * ")" p = 0 while p < len && p_state != s_message_done @@ -1070,14 +1073,14 @@ function parse!(parser::Parser, bytes::ByteView)::Int elseif p_state == s_headers_done @errorifstrict(ch != LF) - if parser.flags & F_CHUNKED > 0 - # chunked encoding - ignore Content-Length header - p_state = s_chunk_size_start - elseif parser.isheadresponse || + if parser.isheadresponse || parser.content_length == 0 || (parser.message.upgrade && isrequest(parser) && parser.message.method == CONNECT) p_state = s_message_done + elseif parser.flags & F_CHUNKED > 0 + # chunked encoding - ignore Content-Length header + p_state = s_chunk_size_start elseif parser.content_length != ULLONG_MAX # Content-Length header given and non-zero p_state = s_body_identity @@ -1202,8 +1205,19 @@ function parse!(parser::Parser, bytes::ByteView)::Int end end @assert p_state == s_message_done || p == len - @assert p <= len + # Consume trailing end of line after message. + if p_state == s_message_done + while p < len + ch = Char(bytes[p + 1]) + if ch != CR && ch != LF + break + end + p += 1 + end + end + + @assert p <= len @debug 3 "parse!() exiting $(ParsingStateCode(p_state))" parser.state = p_state diff --git a/src/parseutils.jl b/src/parseutils.jl index 7e29d8284..c3e68925f 100644 --- a/src/parseutils.jl +++ b/src/parseutils.jl @@ -22,3 +22,15 @@ end @inline ishex(c) = isnum(c) || ('a' <= lower(c) <= 'f') @inline ishostchar(c) = isalphanum(c) || @anyeq(c, '.', '-', '_', '~') @inline isheaderchar(c) = c == CR || c == LF || c == Char(9) || (c > Char(31) && c != Char(127)) + +""" + escapelines(string) + +Escape `string` and insert '\n' after escaped newline characters. +""" + +function escapelines(s) + s = Base.escape_string(s) + s = replace(s, "\\n", "\\n\n ") + return string(" ", strip(s)) +end From b928fe0d366b5d2f98c6fa24e8cd53c1dd762fe8 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 16 Dec 2017 21:26:30 +1100 Subject: [PATCH 059/182] cosmetics --- src/HTTP.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index a0c628ab4..7fea17423 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -93,7 +93,7 @@ stack(;kw...) = ExceptionLayer{ SocketLayer}}} end -if !minimal + if !minimal status(r) = r.status #FIXME headers(r) = Dict(r.headers) #FIXME include("types.jl") @@ -102,7 +102,7 @@ include("sniff.jl") include("handlers.jl"); using .Handlers include("server.jl"); using .Nitrogen include("precompile.jl") -end + end end # module From 6b6b16e8129edcc58148d01148b4b3e3ccb528f8 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 16 Dec 2017 21:27:47 +1100 Subject: [PATCH 060/182] add state and status to ParsingError. Handle eof() before message has begun --- src/parser.jl | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 54ca3c6f5..e806df0f9 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -25,7 +25,8 @@ module Parsers export Parser, parse!, reset!, - messagecomplete, headerscomplete, waitingforeof, + messagestarted, messagecomplete, headerscomplete, waitingforeof, + connectionclosed, ParsingError, ParsingErrorCode using ..IOExtras @@ -166,9 +167,12 @@ function Base.read!(io::IO, p::Parser; unread=IOExtras.unread!) end @debug 2 "read!(::$(typeof(io)), Parser($(ParsingStateCode(p.state)))) eof!" + if !messagestarted(p) + throw(EOFError()) + end if !waitingforeof(p) - throw(ParsingError(headerscomplete(p) ? HPE_BODY_INCOMPLETE : - HPE_HEADERS_INCOMPLETE)) + throw(ParsingError(p, headerscomplete(p) ? HPE_BODY_INCOMPLETE : + HPE_HEADERS_INCOMPLETE)) end return end @@ -207,6 +211,15 @@ function reset!(p::Parser) end +""" + messagestarted(::Parser) + +Has the `Parser` begun processng a Message? +""" + +messagestarted(p::Parser) = p.state != s_start_req_or_res + + """ headerscomplete(::Parser) @@ -231,8 +244,16 @@ messagecomplete(p::Parser) = p.state >= s_message_done Is the `Parser` waiting for the peer to close the connection to signal the end of the Message Body? """ -waitingforeof(p::Parser) = p.state == s_body_identity_eof || - p.state == s_start_req_or_res +waitingforeof(p::Parser) = p.state == s_body_identity_eof + + +""" + connectionclosed(::Parser) + +Was "Connection: close" parsed? +""" + +connectionclosed(p::Parser) = p.flags & F_CONNECTION_CLOSE > 0 isrequest(p::Parser) = p.message.status == 0 @@ -240,20 +261,27 @@ isrequest(p::Parser) = p.message.status == 0 struct ParsingError <: Exception code::ParsingErrorCode + state::UInt8 + status::Int32 msg::String end -ParsingError(code::ParsingErrorCode) = ParsingError(code, "") + +function ParsingError(p::Parser, code::ParsingErrorCode) + ParsingError(code, p.state, p.message.status, "") +end function Base.show(io::IO, e::ParsingError) println(io, string("HTTP.ParsingError: ", - ParsingErrorCodeMap[e.code], + ParsingErrorCodeMap[e.code], ", ", + ParsingStateCode(e.state), ", ", + e.status, e.msg == "" ? "" : "\n", e.msg)) end macro err(code) - esc(:(throw(ParsingError($code)))) + esc(:(throw(ParsingError(p, $code)))) end macro errorif(cond, err) From c22a7cc7759a0538ac6c668647fd7eeacfefe784 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 16 Dec 2017 21:30:22 +1100 Subject: [PATCH 061/182] fix read/write locks in ConnectionPool.jl --- src/ConnectionPool.jl | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 00de1f398..f5519625c 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -94,14 +94,10 @@ function IOExtras.closewrite(c::Connection) if islocked(c.readlock) @debug 3 "Waiting to read: $c" end - if isopen(c.io) - lock(c.readlock) - if !isopen(c.io) - unlock(c.readlock) - end + lock(c.readlock) + if islocked(c.writelock) unlock(c.writelock) end - @assert isopen(c.io) == islocked(c.readlock) end @@ -115,7 +111,7 @@ Increment `readcount` and wake up waiting `closewrite`. function IOExtras.closeread(c::Connection) c.readcount += 1 - if isopen(c.io) + if islocked(c.readlock) unlock(c.readlock) end end @@ -180,8 +176,8 @@ function getconnection(::Type{Connection{T}}, io = getconnection(T, host, port; kw...) c = Connection{T}(host, port, io) ;@debug 1 "New: $c" - push!(pool, c) lock(c.writelock) + push!(pool, c) return c finally @@ -198,7 +194,7 @@ function Base.show(io::IO, c::Connection) c.port != "" ? c.port : Int(peerport(c)), ":", Int(localport(c)), ", ", typeof(c.io), ", ", tcpstatus(c), ", ", - length(c.excess), "-byte excess, reads/writes: ", + length(c.excess), "-byte excess, writes/reads: ", c.writecount, "/", c.readcount) end From 7ad1d7344c20842bd3d77cbd657b716e5b9cae56 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 17 Dec 2017 14:33:18 +1100 Subject: [PATCH 062/182] typo in @err macro --- src/parser.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser.jl b/src/parser.jl index e806df0f9..6c19dfa0c 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -281,7 +281,7 @@ end macro err(code) - esc(:(throw(ParsingError(p, $code)))) + esc(:(throw(ParsingError(parser, $code)))) end macro errorif(cond, err) From b02c8da547d54316842784af3943fa4459bb79ea Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 17 Dec 2017 14:33:47 +1100 Subject: [PATCH 063/182] add note about header fragmentation --- src/Messages.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Messages.jl b/src/Messages.jl index 1343ee2f4..ea5bf2e88 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -288,8 +288,8 @@ Write start line, headers and body of HTTP Message. """ function Base.write(io::IO, m::Message) - writestartline(io, m) - writeheaders(io, m) + writestartline(io, m) # FIXME To avoid fragmentation, maybe + writeheaders(io, m) # buffer header before sending to `io` write(io, m.body) return end From 67ac47a7517a88b23ff5b3c2150ec233616fc4a4 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 17 Dec 2017 14:34:23 +1100 Subject: [PATCH 064/182] ConnectionPool scheme refinement. Add `max_duplicates` setting. Up to `max_duplicates` for a certian scheme/host/port, create new connections if the existing connections are already locked for writing. If there are aleady `max_duplicates` connections fo a scheme/host/port, return one of the existing connections (even though it is locked for writing by another task). The new call to lock(c.writelock) will then block, forcing the calling task to wait. This seems to work nicely with the AWSS3 concurrency tests: https://github.com/samoconnor/AWSS3.jl/blob/master/test/runtests.jl#L231 Also added a new test at the bottom of test/client.jl Inspection with tcpdump/wireshark shows that multiple requests are being written to the socket before the first response comes back; and once responses start comming, requests and responses stream continuously, utilising both halves of the TCP connection. --- src/ConnectionPool.jl | 97 ++++++++++++++++++++++++++++++++----------- test/client.jl | 11 +++++ 2 files changed, 84 insertions(+), 24 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index f5519625c..b3e3f4874 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -9,6 +9,7 @@ import MbedTLS.SSLContext import ..Connect: getconnection, getparser import ..Parsers.Parser +const max_duplicates = 4 const ByteView = typeof(view(UInt8[], 1:0)) @@ -90,14 +91,17 @@ Increment `writecount` and wait for pending reads to complete. """ function IOExtras.closewrite(c::Connection) - c.writecount += 1 - if islocked(c.readlock) - @debug 3 "Waiting to read: $c" - end - lock(c.readlock) + c.writecount += 1; @debug 1 "write done: $c" if islocked(c.writelock) unlock(c.writelock) end + lock(c.readlock) + # Note, we rely on the readlock's conditon queue to ensure that readers + # wake up in the correct order (i.e. such that Responses match Requests). + # The documentation's description of a "queue" implies that the first + # `wait`-er will be the first to be woken up on `notify`: + # "When a task calls wait() on a Condition, the task is + # [...] added to the condition's queue." end @@ -144,6 +148,52 @@ const pool = Vector{Connection}() const poollock = ReentrantLock() + +""" + findidle(type, host, port) -> Connection + +Find a `Connection` in the `pool` that is ready for writing. +""" + +function findidle(T::Type, + host::AbstractString, + port::AbstractString) + + pattern = x->(!islocked(x.writelock) && + typeof(x.io) == T && + x.host == host && + x.port == port) + + while (i = findlast(pattern, pool)) > 0 + c = pool[i] + if !isopen(c.io) + deleteat!(pool, i) ;@debug 1 "Deleted: $c" + continue + end + return c ;@debug 2 "Reused: $c" + end + return nothing +end + + +""" + findall(type, host, port) -> Connection + +Find all `Connections` in the `pool` for `host` and `port`. +""" + +function findall(T::Type, + host::AbstractString, + port::AbstractString) + + pattern = x->(typeof(x.io) == T && + x.host == host && + x.port == port) + + return filter(pattern, pool) +end + + """ getconnection(type, host, port) -> Connection @@ -156,33 +206,32 @@ function getconnection(::Type{Connection{T}}, port::AbstractString; kw...)::Connection{T} where T <: IO + c = nothing + lock(poollock) try - pattern = x->(!islocked(x.writelock) && - typeof(x.io) == T && - x.host == host && - x.port == port) - - while (i = findlast(pattern, pool)) > 0 - c = pool[i] - if !isopen(c.io) - deleteat!(pool, i) ;@debug 1 "Deleted: $c" - continue - end; ;@debug 2 "Reused: $c" - lock(c.writelock) - return c + # Try to find a connection that is ready for writing... + c = findidle(T, host, port) + + if c == nothing + # If there are not too many duplicates for this host, + # create a new connection, otherwise return a busy one... + l = findall(T, host, port) + if length(l) < max_duplicates + io = getconnection(T, host, port; kw...) + c = Connection{T}(host, port, io) ;@debug 1 "New: $c" + push!(pool, c) + else + c = rand(l) ;@debug 1 "Busy: $c" + end end - io = getconnection(T, host, port; kw...) - c = Connection{T}(host, port, io) ;@debug 1 "New: $c" - lock(c.writelock) - push!(pool, c) - return c - finally unlock(poollock) end + lock(c.writelock) + return c end diff --git a/test/client.jl b/test/client.jl index d62e08f0f..d516fb037 100644 --- a/test/client.jl +++ b/test/client.jl @@ -227,6 +227,17 @@ for sch in ("http", "https") # gzip body = "hey" # body = UInt8[0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xcb,0x48,0xad,0x04,0x00,0xf0,0x15,0xd6,0x88,0x03,0x00,0x00,0x00] # r = HTTP.post("$sch://httpbin.org/post"; body=body, chunksize=1) + + @sync begin + for i = 1:100 + @async begin + r = HTTP.RequestStack.request("GET", "http://httpbin.org/headers", ["i" => i]) + r = JSON.parse(String(take!(r))) + @test r["headers"]["I"] == string(i) + end + end + end + end end # @testset "HTTP.Client" From 21ca900f186ad13e2b018217ca1dff28e1c48c76 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 19 Dec 2017 12:31:50 +1100 Subject: [PATCH 065/182] HTTP.jl - Update top-level RequestStack.request method to convert body stream/data to a ::Body ASAP. - Pass response_body as positional (not kw) arg. Bodies.jl - Add isstreamfresh(::Body) to check if stream is untouched and safe to use in a retry. Messages.jl - Add exception field to ::Response, set by writeandread() when there is an error processing the request. Rethrown by by wait() and waitforheaders(). SocketRequest.jl - Handle exceptions thrown by writeandread() background task for streamed Response Bodies. RetryRequest.jl - Retry for Base.ArgumentError.msg == "stream is closed or unusable" - Don't retry if the previous attempt has altered the state of the request stream or the response stream. ConnectionPool.jl - Use writecount/readcount to ensure that the readlock is obtained in the correct sequence so that Responses match Requests. See closewrite(). - Revised pool strategy: - Try to find a connection with no readers or writers, then - Open new connections up to the limit, then - Try to find a connection with pending readers, but writable, then - Sleep waiting for writes to finish, then try again. - The previous scheme ended up over-interleaving requests while other connections sat idle. - New async test cases to validate connection interleaving. -- INSERT -- --- src/BasicAuthRequest.jl | 5 +- src/Bodies.jl | 14 +++- src/CanonicalizeRequest.jl | 5 +- src/ConnectionPool.jl | 166 +++++++++++++++++++++++++------------ src/ConnectionRequest.jl | 4 +- src/CookieRequest.jl | 4 +- src/HTTP.jl | 34 +++++--- src/MessageRequest.jl | 12 +-- src/Messages.jl | 48 ++++++----- src/RedirectRequest.jl | 4 +- src/RetryRequest.jl | 16 +++- src/SocketRequest.jl | 22 +++-- test/async.jl | 97 ++++++++++++++++++++++ test/client.jl | 10 --- test/runtests.jl | 1 + 15 files changed, 317 insertions(+), 125 deletions(-) create mode 100644 test/async.jl diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index cfdc96035..907384c8b 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -10,7 +10,8 @@ export BasicAuthLayer function request(::Type{BasicAuthLayer{Next}}, - method::String, uri, headers=[], body=""; kw...) where Next + method::String, uri, headers, body, response_body; + kw...) where Next userinfo = URI(uri).userinfo @@ -19,7 +20,7 @@ function request(::Type{BasicAuthLayer{Next}}, setkv(headers, "Authorization", "Basic $(base64encode(userinfo))") end - return request(Next, method, uri, headers, body; kw...) + return request(Next, method, uri, headers, body, response_body,; kw...) end diff --git a/src/Bodies.jl b/src/Bodies.jl index 439a0f18b..a0f4cb951 100644 --- a/src/Bodies.jl +++ b/src/Bodies.jl @@ -1,6 +1,6 @@ module Bodies -export Body, isstream +export Body, isstream, isstreamfresh """ @@ -96,6 +96,16 @@ Is this `Body` in streaming mode? isstream(b::Body) = b.stream != notastream +""" + isstreamfresh(::Body) + +False if there have been any reads/writes from/to the `Body`'s stream. +""" + +isstreamfresh(b::Body) = !isstream(b) || position(b.buffer) == 0 + + + """ length(::Body) @@ -157,7 +167,7 @@ function Base.write(io::IO, body::Body) return end - @assert position(body.buffer) == 0 + @assert isstreamfresh(body) # Use "chunked" encoding if length is unknown. # https://tools.ietf.org/html/rfc7230#section-4.1 diff --git a/src/CanonicalizeRequest.jl b/src/CanonicalizeRequest.jl index 3bd28e193..02df1ccf2 100644 --- a/src/CanonicalizeRequest.jl +++ b/src/CanonicalizeRequest.jl @@ -11,11 +11,12 @@ export CanonicalizeLayer canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) function request(::Type{CanonicalizeLayer{Next}}, - method::String, uri, headers=[], body=""; kw...) where Next + method::String, uri, headers, body, response_body; + kw...) where Next headers = canonicalizeheaders(headers) - res = request(Next, method, uri, headers, body; kw...) + res = request(Next, method, uri, headers, body, response_body; kw...) res.headers = canonicalizeheaders(res.headers) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index b3e3f4874..d8a42caf6 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -9,7 +9,7 @@ import MbedTLS.SSLContext import ..Connect: getconnection, getparser import ..Parsers.Parser -const max_duplicates = 4 +const max_duplicates = 8 const ByteView = typeof(view(UInt8[], 1:0)) @@ -51,6 +51,9 @@ Connection{T}(host::AbstractString, port::AbstractString, io::T) where T <: IO = const noconnection = Connection{TCPSocket}("","",TCPSocket()) +getparser(c::Connection) = c.parser + + Base.unsafe_write(c::Connection, p::Ptr{UInt8}, n::UInt) = unsafe_write(c.io, p, n) @@ -91,17 +94,27 @@ Increment `writecount` and wait for pending reads to complete. """ function IOExtras.closewrite(c::Connection) - c.writecount += 1; @debug 1 "write done: $c" + seq = c.writecount + c.writecount += 1; @debug 2 "write done: $c" + + # The write lock may already have been unlocked by `close` or `purge`. if islocked(c.writelock) unlock(c.writelock) + notify(poolcondition) end lock(c.readlock) - # Note, we rely on the readlock's conditon queue to ensure that readers - # wake up in the correct order (i.e. such that Responses match Requests). - # The documentation's description of a "queue" implies that the first - # `wait`-er will be the first to be woken up on `notify`: - # "When a task calls wait() on a Condition, the task is - # [...] added to the condition's queue." + # Wait for prior pending reads to complete... + while c.readcount != seq + unlock(c.readlock) + yield() + lock(c.readlock) + # Error if there is nothing to read. + if !isopen(c.io) && nb_available(c.io) == 0 + unlock(c.readlock) + throw(EOFError()) + end + end + return end @@ -110,14 +123,15 @@ end Signal that an entire Response Message has been read from the `Connection`. -Increment `readcount` and wake up waiting `closewrite`. +Increment `readcount` and wake up tasks waiting in `closewrite`. """ function IOExtras.closeread(c::Connection) - c.readcount += 1 + c.readcount += 1; @debug 2 "read done: $c" if islocked(c.readlock) unlock(c.readlock) end + return end @@ -129,6 +143,8 @@ function Base.close(c::Connection) if islocked(c.writelock) unlock(c.writelock) end + notify(poolcondition) + return end @@ -146,38 +162,46 @@ the `Connection` can be reused for reading. const pool = Vector{Connection}() const poollock = ReentrantLock() +const poolcondition = Condition() +""" + closeall() - +Close all connections in `pool`. """ - findidle(type, host, port) -> Connection -Find a `Connection` in the `pool` that is ready for writing. +function closeall() + + lock(poollock) + for c in pool + close(c) + end + empty!(pool) + unlock(poollock) + return +end + + """ + findwriteable(type, host, port) -> Vector{Connection} -function findidle(T::Type, - host::AbstractString, - port::AbstractString) +Find `Connections` in the `pool` that are ready for writing. +""" - pattern = x->(!islocked(x.writelock) && - typeof(x.io) == T && - x.host == host && - x.port == port) +function findwriteable(T::Type, + host::AbstractString, + port::AbstractString) - while (i = findlast(pattern, pool)) > 0 - c = pool[i] - if !isopen(c.io) - deleteat!(pool, i) ;@debug 1 "Deleted: $c" - continue - end - return c ;@debug 2 "Reused: $c" - end - return nothing + filter(c->(typeof(c.io) == T && + c.host == host && + c.port == port && + !islocked(c.writelock) && + isopen(c.io)), pool) end """ - findall(type, host, port) -> Connection + findall(type, host, port) -> Vector{Connection} Find all `Connections` in the `pool` for `host` and `port`. """ @@ -186,11 +210,29 @@ function findall(T::Type, host::AbstractString, port::AbstractString) - pattern = x->(typeof(x.io) == T && - x.host == host && - x.port == port) + filter(c->(typeof(c.io) == T && + c.host == host && + c.port == port && + isopen(c.io)), pool) +end + - return filter(pattern, pool) +""" + purge() + +Remove closed connections from `pool`. +""" +function purge() + while (i = findfirst(x->!isopen(x.io), pool)) > 0 + c = pool[i] + if islocked(c.readlock) + unlock(c.readlock) + end + if islocked(c.writelock) + unlock(c.writelock) + end + deleteat!(pool, i) ;@debug 1 "Deleted: $c" + end end @@ -206,38 +248,49 @@ function getconnection(::Type{Connection{T}}, port::AbstractString; kw...)::Connection{T} where T <: IO - c = nothing + while true - lock(poollock) - try + lock(poollock) + try + purge() - # Try to find a connection that is ready for writing... - c = findidle(T, host, port) + # Try to find a connection with no active readers or writers... + writeable = findwriteable(T, host, port) + idle = filter(c->!islocked(c.readlock), writeable) + if !isempty(idle) + c = rand(idle) ;@debug 2 "Idle: $c" + lock(c.writelock) + return c + end - if c == nothing # If there are not too many duplicates for this host, - # create a new connection, otherwise return a busy one... - l = findall(T, host, port) - if length(l) < max_duplicates + # create a new connection... + busy = findall(T, host, port) + if length(busy) < max_duplicates io = getconnection(T, host, port; kw...) c = Connection{T}(host, port, io) ;@debug 1 "New: $c" + lock(c.writelock) push!(pool, c) - else - c = rand(l) ;@debug 1 "Busy: $c" + return c + end + + # Share a connection that has active readers... + if !isempty(writeable) + c = rand(writeable) ;@debug 2 "Shared: $c" + lock(c.writelock) + return c end + + finally + unlock(poollock) end - finally - unlock(poollock) + # Wait for `closewrite` or `close` to signal that a connection is ready. + wait(poolcondition) end - lock(c.writelock) - return c end -getparser(c::Connection) = c.parser - - function Base.show(io::IO, c::Connection) print(io, c.host, ":", c.port != "" ? c.port : Int(peerport(c)), ":", @@ -262,5 +315,14 @@ peerport(c::Connection) = !isopen(c.io) ? 0 : tcpstatus(c::Connection) = Base.uv_status_string(tcpsocket(c)) +function showpool(io::IO) + lock(poollock) + println(io, "ConnectionPool[") + for c in pool + println(io, " $c") + end + println("]\n") + unlock(poollock) +end end # module ConnectionPool diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index c3c9ab332..f980dea89 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -26,7 +26,7 @@ function request(::Type{ConnectionPoolLayer{Next}}, Connection = ConnectionPool.Connection{sockettype(uri)} io = getconnection(Connection, uri.host, uri.port; kw...) - return request(Next, io, req, res) + return request(Next, io, req, res; kw...) end @@ -38,7 +38,7 @@ function request(::Type{ConnectLayer{Next}}, io = getconnection(sockettype(uri), uri.host, uri.port; kw...) - return request(Next, io, req, res) + return request(Next, io, req, res; kw...) end diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index ff3fa5e41..da14b87d5 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -46,7 +46,7 @@ end function request(::Type{CookieLayer{Next}}, - method::String, uri, headers=[], body=""; + method::String, uri, headers, body, response_body; cookiejar=default_cookiejar, kw...) where Next u = URI(uri) @@ -57,7 +57,7 @@ function request(::Type{CookieLayer{Next}}, setkv(headers, "Cookie", string(getkv(headers, "Cookie", ""), cookies)) end - res = request(Next, method, uri, headers, body; kw...) + res = request(Next, method, uri, headers, body, response_body; kw...) setcookies(hostcookies, u.host, res.headers) diff --git a/src/HTTP.jl b/src/HTTP.jl index 7fea17423..99015d1de 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -21,16 +21,6 @@ end const minimal = false -module RequestStack - - import ..HTTP - request(m::String, a...; kw...) = request(HTTP.stack(;kw...), m, a...; kw...) - -end - if minimal -import .RequestStack.request - end - include("debug.jl") include("Pairs.jl") include("Strings.jl") @@ -48,6 +38,30 @@ include("Connect.jl") include("ConnectionPool.jl") include("Messages.jl"); using .Messages +module RequestStack + + import ..HTTP + import ..Body + + function request(method::String, uri, headers, body::Body, + response_body::Body; kw...) + + request(HTTP.stack(;kw...), + method, uri, headers, body, response_body; kw...) + end + + function request(method::String, uri, headers=[], body=""; + bodylength=HTTP.Messages.Bodies.unknownlength, + response_stream=nothing, kw...) + + request(method, uri, headers, Body(body, bodylength), + Body(response_stream); kw...) + end +end + if minimal +import .RequestStack.request + end + abstract type Layer end const NoLayer = Union diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 42d61ca44..11ea39b07 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -41,22 +41,18 @@ println(stat("response_file").size) """ function request(::Type{MessageLayer{Next}}, - method::String, uri, headers=[], body=""; - bodylength=Messages.Bodies.unknownlength, - parent=nothing, - response_stream=nothing, - kw...) where Next + method::String, uri, headers, body::Body, response_body::Body; + parent=nothing, kw...) where Next u = URI(uri) url = method == "CONNECT" ? hostport(u) : resource(u) - req = Request(method, url, headers, Body(body, bodylength); - parent=parent) + req = Request(method, url, headers, body; parent=parent) defaultheader(req, "Host" => u.host) setlengthheader(req) - res = Response(body=Body(response_stream), parent=req) + res = Response(body=response_body, parent=req) return request(Next, u, req, res; kw...) end diff --git a/src/Messages.jl b/src/Messages.jl index ea5bf2e88..cffd20d5e 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -1,7 +1,7 @@ module Messages export Message, Request, Response, Body, - method, iserror, isredirect, parentcount, isstream, + method, iserror, isredirect, parentcount, isstream, isstreamfresh, header, setheader, defaultheader, setlengthheader, waitforheaders, wait, writeandread @@ -64,12 +64,13 @@ Represents a HTTP Response Message. - `status::Int16` - `headers::Vector{Pair{String,String}}` - `body::`[`HTTP.Body`](@ref) -- `parent::Request`, the `Request` that yielded this `Response`. - `complete::Condition`, raised when the `Parser` has finished reading the Response Headers. This allows the `status` and `header` fields to be read used asynchronously without waiting for the entire body to be parsed. `complete` is also raised when the entire Response Body has been read. +- `exception`, set if `writeandread` fails. +- `parent::Request`, the `Request` that yielded this `Response`. """ mutable struct Response @@ -78,11 +79,12 @@ mutable struct Response headers::Vector{Pair{String,String}} body::Body complete::Condition + exception parent end Response(status::Int=0, headers=[]; body=Body(), parent=nothing) = - Response(v"1.1", status, headers, body, Condition(), parent) + Response(v"1.1", status, headers, body, Condition(), nothing, parent) Response(bytes) = read!(IOBuffer(bytes), Response()) Base.parse(::Type{Response}, str::AbstractString) = Response(str) @@ -116,23 +118,6 @@ Method of the `Request` that yielded this `Response`. method(r::Response) = r.parent == nothing ? "" : r.parent.method -#= FIXME obsolete ? -""" - parentcount(::Response) - -How many redirect parents does this `Response` have? -""" - -function parentcount(r::Response) - if r.parent == nothing || r.parent.parent == nothing - return 0 - else - return 1 + parentcount(r.parent.parent) - end -end -=# - - """ statustext(::Response) -> String @@ -148,7 +133,14 @@ statustext(r::Response) = Base.get(Parsers.STATUS_CODES, r.status, "Unknown Code Wait for the `Parser` (in a different task) to finish parsing the headers. """ -waitforheaders(r::Response) = while r.status == 0; wait(r.complete) end +function waitforheaders(r::Response) + while r.status == 0 && r.exception == nothing + wait(r.complete) + end + if r.exception != nothing + rethrow(r.exception) + end +end """ @@ -157,7 +149,14 @@ waitforheaders(r::Response) = while r.status == 0; wait(r.complete) end Wait for the `Parser` (in a different task) to finish parsing the `Response`. """ -Base.wait(r::Response) = while isopen(r.body); wait(r.complete) end +function Base.wait(r::Response) + while isopen(r.body) && r.exception == nothing + wait(r.complete) + end + if r.exception != nothing + rethrow(r.exception) + end +end """ @@ -326,7 +325,7 @@ end Configure a `Parser` to store parsed data into this `Message`. """ function connectparser(m::Message, p::Parser) - reset!(p) + reset!(p) p.onbodyfragment = x->write(m.body, x) p.onheader = x->appendheader(m, x) p.onheaderscomplete = x->readstartline!(m, x) @@ -366,7 +365,10 @@ function writeandread(io::IO, req::Request, res::Response) closeread(io) ;@debug 2 "read from: $io\n$res" catch e @schedule close(io) + res.exception = e rethrow(e) + finally + notify(res.complete) end return res diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index 102ffbdf3..48d3c154b 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -12,12 +12,12 @@ export RedirectLayer function request(::Type{RedirectLayer{Next}}, - method::String, uri, headers=[], body=""; + method::String, uri, headers, body, response_body; maxredirects=3, forwardheaders=false, kw...) where Next count = 0 while true - res = request(Next, method, uri, headers, body; kw...) + res = request(Next, method, uri, headers, body, response_body; kw...) if (count == maxredirects || !isredirect(res) diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 2e2633dad..16101c802 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -2,6 +2,7 @@ module RetryRequest import ..HTTP import ..Layer, ..RequestStack.request +using ..Messages abstract type RetryLayer{Next <: Layer} <: Layer end export RetryLayer @@ -11,14 +12,23 @@ isrecoverable(e::Base.UVError) = true isrecoverable(e::Base.DNSError) = true isrecoverable(e::Base.EOFError) = true isrecoverable(e::HTTP.StatusError) = e.status < 200 || e.status >= 500 +isrecoverable(e::Base.ArgumentError) = e.msg == "stream is closed or unusable" + isrecoverable(e::Exception) = false +isrecoverable(e, request_body, response_body) = isrecoverable(e) && + isstreamfresh(request_body) && + isstreamfresh(response_body) -function request(::Type{RetryLayer{Next}}, a...; retries=2, kw...) where Next +function request(::Type{RetryLayer{Next}}, + method::String, uri, headers, body::Body, response_body::Body; + retries=3, kw...) where Next - retry(request, + retry_request = retry(request, delays=ExponentialBackOff(n = retries), - check=(s,ex)->(s,isrecoverable(ex)))(Next, a...; kw...) + check=(s,ex)->(s,isrecoverable(ex, body, response_body))) + + retry_request(Next, method, uri, headers, body, response_body; kw...) end diff --git a/src/SocketRequest.jl b/src/SocketRequest.jl index f203c39f3..cbc8671fc 100644 --- a/src/SocketRequest.jl +++ b/src/SocketRequest.jl @@ -2,6 +2,7 @@ module SocketRequest import ..Layer, ..RequestStack.request using ..Messages +import ..@debug, ..DEBUG_LEVEL abstract type SocketLayer <: Layer end export SocketLayer @@ -14,15 +15,22 @@ Send a `Request` and receive a `Response`. Run the `Request` in a background task if response body is a stream. """ -function request(::Type{SocketLayer}, io::IO, req::Request, res::Response) +function request(::Type{SocketLayer}, io::IO, req::Request, res::Response; kw...) - if isstream(res.body) - @schedule writeandread(io, req, res) - waitforheaders(res) - return res + if !isstream(res.body) + return writeandread(io, req, res) end - - return writeandread(io, req, res) + + @schedule try + writeandread(io, req, res) + catch e + if res.exception != e + rethrow(e) + end + @debug 1 "Async HTTP Message Exception!\n$e\n$io\n$req\n$res" + end + waitforheaders(res) + return res end diff --git a/test/async.jl b/test/async.jl new file mode 100644 index 000000000..bcd58ea1a --- /dev/null +++ b/test/async.jl @@ -0,0 +1,97 @@ +@testset "HTTP.async" begin + +using JSON + + +for http in ("http", "https") + println("running $http async tests...") + + @sync begin + for i = 1:100 + @async begin + r = HTTP.RequestStack.request("GET", "$http://httpbin.org/headers", ["i" => i]) + r = JSON.parse(String(take!(r))) + @test r["headers"]["I"] == string(i) + end + end + end + + HTTP.ConnectionPool.showpool(STDOUT) + HTTP.ConnectionPool.closeall() + + + @sync begin + for i = 1:100 + @async begin + r = HTTP.RequestStack.request("GET", "$http://httpbin.org/stream/$i") + r = String(take!(r)) + r = split(strip(r), "\n") + @test length(r) == i + end + end + end + + HTTP.ConnectionPool.showpool(STDOUT) + HTTP.ConnectionPool.closeall() + + asyncmap(i->begin + n = i % 20 + 1 + for attempt in 1:3 + r = nothing + try + println("GET $i $n") + s = BufferStream() + r = HTTP.RequestStack.request("GET", "$http://httpbin.org/stream/$n"; + retries=5, response_stream=s) + wait(r) + r = String(read(s)) + break + catch e + if attempt == 3 || !HTTP.RetryRequest.isrecoverable(e) + rethrow(e) + end + end + end + + r = split(strip(r), "\n") + println("GOT $i $n") + @test length(r) == n + + end, 1:1000, ntasks=20) + + + @sync begin + for i = 1:1000 + @async begin + n = i % 20 + 1 + for attempt in 1:3 + r = nothing + try + s = BufferStream() + println("GET $i $n") + r = HTTP.RequestStack.request("GET", "$http://httpbin.org/stream/$n"; + response_stream=s) + wait(r) + r = String(read(s)) + break + catch e + if attempt == 3 || !HTTP.RetryRequest.isrecoverable(e) + rethrow(e) + end + end + end + + r = split(strip(r), "\n") + println("GOT $i $n") + @test length(r) == n + end + end + end + + + HTTP.ConnectionPool.showpool(STDOUT) + HTTP.ConnectionPool.closeall() + +end + +end # @testset "HTTP.Client" diff --git a/test/client.jl b/test/client.jl index d516fb037..5d8354403 100644 --- a/test/client.jl +++ b/test/client.jl @@ -228,16 +228,6 @@ for sch in ("http", "https") # body = UInt8[0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xcb,0x48,0xad,0x04,0x00,0xf0,0x15,0xd6,0x88,0x03,0x00,0x00,0x00] # r = HTTP.post("$sch://httpbin.org/post"; body=body, chunksize=1) - @sync begin - for i = 1:100 - @async begin - r = HTTP.RequestStack.request("GET", "http://httpbin.org/headers", ["i" => i]) - r = JSON.parse(String(take!(r))) - @test r["headers"]["I"] == string(i) - end - end - end - end end # @testset "HTTP.Client" diff --git a/test/runtests.jl b/test/runtests.jl index fbc1817e7..8b0b35585 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,5 +19,6 @@ end # include("types.jl"); # include("handlers.jl") include("client.jl"); + include("async.jl"); # include("server.jl") end; From 9457bf84521bed73875dcf84a13017610660a1bb Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 19 Dec 2017 14:39:44 +1100 Subject: [PATCH 066/182] v0.7 compat --- test/parser.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parser.jl b/test/parser.jl index b0f0c0e2e..f1605e656 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -3,6 +3,7 @@ module ParserTest using Base.Test import ..HTTP +import ..HTTP.pairs using HTTP.Messages using HTTP.Parsers From 4319b2bf35a224225146609656bcc9b0989e133e Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 19 Dec 2017 14:50:23 +1100 Subject: [PATCH 067/182] remove old async test, see new test/async.jl --- test/messages.jl | 45 --------------------------------------------- 1 file changed, 45 deletions(-) diff --git a/test/messages.jl b/test/messages.jl index 538e8c2d7..5499252ca 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -120,51 +120,6 @@ using JSON end - for sch in ["http", "https"] - - log_buffer = Vector{String}() - - function log(s::String) - println(s) - push!(log_buffer, s) - end - - function async_get(url) - io = BufferStream() - q = URI(url).query - log("GET $q") - r = request("GET", url, response_stream=io) - @async begin - s = String(read(io)) - s = split(s, "\n")[end-1] - x = JSON.parse(s) - log("GOT $q: $(x["args"]["req"])") - end - end - - - @sync begin - async_get("$sch://httpbin.org/stream/100?req=1") - async_get("$sch://httpbin.org/stream/100?req=2") - async_get("$sch://httpbin.org/stream/100?req=3") - async_get("$sch://httpbin.org/stream/100?req=4") - async_get("$sch://httpbin.org/stream/100?req=5") - end - - @test log_buffer == ["GET req=1", - "GET req=2", - "GOT req=1: 1", - "GET req=3", - "GOT req=2: 2", - "GET req=4", - "GOT req=3: 3", - "GET req=5", - "GOT req=4: 4", - "GOT req=5: 5"] - - end - - mktempdir() do d cd(d) do From fe2243c298ffecceaa62ded41d283c6979502657 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 19 Dec 2017 22:28:16 +1100 Subject: [PATCH 068/182] v0.7 updates --- .travis.yml | 2 +- src/BasicAuthRequest.jl | 4 ++++ src/CanonicalizeRequest.jl | 2 +- src/Messages.jl | 4 ++++ src/Pairs.jl | 2 +- src/RedirectRequest.jl | 5 ++++ src/Strings.jl | 2 +- src/client.jl | 49 +++++++++++++++++++++++++------------- src/uri.jl | 2 +- test/messages.jl | 3 +++ test/uri.jl | 8 ++++++- 11 files changed, 60 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index 422cbee9d..610e1b1a1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ os: # - osx julia: - 0.6 -# - nightly + - 0.7 notifications: email: false after_success: diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index 907384c8b..cc0181f75 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -1,5 +1,9 @@ module BasicAuthRequest +if VERSION > v"0.7.0-DEV.2338" +using Base64 +end + import ..Layer, ..RequestStack.request using ..URIs using ..Pairs: getkv, setkv diff --git a/src/CanonicalizeRequest.jl b/src/CanonicalizeRequest.jl index 02df1ccf2..3410b0c2c 100644 --- a/src/CanonicalizeRequest.jl +++ b/src/CanonicalizeRequest.jl @@ -8,7 +8,7 @@ abstract type CanonicalizeLayer{Next <: Layer} <: Layer end export CanonicalizeLayer -canonicalizeheaders{T}(h::T) = T([tocameldash!(k) => v for (k,v) in h]) +canonicalizeheaders(h::T) where T = T([tocameldash!(k) => v for (k,v) in h]) function request(::Type{CanonicalizeLayer{Next}}, method::String, uri, headers, body, response_body; diff --git a/src/Messages.jl b/src/Messages.jl index cffd20d5e..bf4f942dd 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -6,6 +6,10 @@ export Message, Request, Response, Body, waitforheaders, wait, writeandread +if VERSION > v"0.7.0-DEV.2338" +using Unicode +end + import ..HTTP include("Bodies.jl") diff --git a/src/Pairs.jl b/src/Pairs.jl index cbfe88095..65072a675 100644 --- a/src/Pairs.jl +++ b/src/Pairs.jl @@ -1,6 +1,6 @@ module Pairs -export setbyfirst, getbyfirst, setkv, getkv +export defaultbyfirst, setbyfirst, getbyfirst, setkv, getkv """ diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index 48d3c154b..0df579bc9 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -26,7 +26,12 @@ function request(::Type{RedirectLayer{Next}}, return res end + + if VERSION > v"0.7.0-DEV.2338" + kw = merge(kw, [:parent => res]) + else setkv(kw, :parent, res) + end uri = absuri(location, uri) if forwardheaders headers = filter(h->!(h[1] in ("Host", "Cookie")), headers) diff --git a/src/Strings.jl b/src/Strings.jl index 152e53dae..3dcf1cd6f 100644 --- a/src/Strings.jl +++ b/src/Strings.jl @@ -26,7 +26,7 @@ Ensure the first character and characters that follow a '-' are uppercase. """ function tocameldash!(s::String) - const toUpper = UInt8('A') - UInt8('a') + toUpper = UInt8('A') - UInt8('a') bytes = Vector{UInt8}(s) upper = true for i = 1:length(bytes) diff --git a/src/client.jl b/src/client.jl index 3074b77e5..0f4995371 100644 --- a/src/client.jl +++ b/src/client.jl @@ -25,7 +25,7 @@ mutable struct Client # cookies are stored in-memory per host and automatically sent when appropriate cookies::Dict{String, Set{Cookie}} # global request settings - options::Vector{Tuple{Symbol,Any}} + options::(VERSION > v"0.7.0-DEV.2338" ? NamedTuple : Vector{Tuple{Symbol,Any}}) end Client(;options...) = Client(Dict{String, Set{Cookie}}(), options) @@ -41,11 +41,18 @@ function request(client::Client, method, uri::URI; args...) # Add default values from client options to args... + if VERSION > v"0.7.0-DEV.2338" + args = merge(client.options, args) + getarg = Base.get + else for option in client.options defaultbyfirst(args, option) end + getarg = getkv + end + newargs = Pair{Symbol,Any}[] - if getkv(args, :chunksize, nothing) != nothing + if getarg(args, :chunksize, nothing) != nothing Base.depwarn( "The chunksize= option is deprecated and has no effect.\n" * "Use a BufferStream and pass chunks of the desired size to `write`:\n" * @@ -56,8 +63,8 @@ function request(client::Client, method, uri::URI; :chunksize) end - if getkv(args, :connecttimeout, Inf) != Inf || - getkv(args, :readtimeout, Inf) != Inf + if getarg(args, :connecttimeout, Inf) != Inf || + getarg(args, :readtimeout, Inf) != Inf Base.depwarn( "The connecttimeout= and readtimeout= options are deprecated " * "and have no effect.\n" * @@ -65,59 +72,67 @@ function request(client::Client, method, uri::URI; :connecttimeout) end - if getkv(args, :tlsconfig, nothing) != nothing + if getarg(args, :tlsconfig, nothing) != nothing Base.depwarn( "The tlsconfig= option is deprecated. Use sslconfig=::MbedTLS.SSLConfig", :tlsconfig) - setkv(args, :sslconfig, getkv(args, :tlsconfig)) + setkv(newargs, :sslconfig, getarg(args, :tlsconfig)) end - if getkv(args, :allowredirects, nothing) != nothing + if getarg(args, :allowredirects, nothing) != nothing Base.depwarn( "The allowredirects= option is deprecated. Use redirect=::Bool", :allowredirects) - setkv(args, :redirect, getkv(args, :allowredirects)) + setkv(newargs, :redirect, getarg(args, :allowredirects)) end - if getkv(args, :managecookies, nothing) != nothing + if getarg(args, :managecookies, nothing) != nothing Base.depwarn( "The managecookies= option is deprecated. Use cookies=::Bool", :managecookies) - setkv(args, :cookies, getkv(args, :managecookies)) + setkv(newargs, :cookies, getarg(args, :managecookies)) end - setkv(args, :cookiejar, client.cookies) + setkv(newargs, :cookiejar, client.cookies) - if getkv(args, :statusraise, nothing) != nothing + if getarg(args, :statusraise, nothing) != nothing Base.depwarn( "The statusraise= options is deprecated. Use statusexception=::Bool", :statusraise) - setkv(args, :statusexception, getkv(args, :statusraise)) + setkv(newargs, :statusexception, getarg(args, :statusraise)) end - if getkv(args, :insecure, nothing) != nothing + if getarg(args, :insecure, nothing) != nothing Base.depwarn( "The insecure= option is deprecated. Use require_ssl_verification=::Bool", :insecure) - setkv(args, :require_ssl_verification, !getkv(args, :insecure)) + setkv(newargs, :require_ssl_verification, !getarg(args, :insecure)) end m = string(method) h = [k => v for (k,v) in headers] if stream - push!(args, (:response_stream, BufferStream())) + setkv(newargs, :response_stream, BufferStream()) end if isa(body, Dict) body = HTTP.Form(body) setbyfirst(h, "Content-Type" => "multipart/form-data; boundary=$(body.boundary)") - setkv(args, :bodylength, length(body)) + setkv(newargs, :bodylength, length(body)) end if !enablechunked && isa(body, IO) body = read(body) end + if VERSION > v"0.7.0-DEV.2338" + args = merge(args, newargs) + else + for newarg in newargs + defaultbyfirst(args, newarg) + end + end + return RequestStack.request(m, uri, h, body; args...) end request(uri::AbstractString; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, GET, URIs.URL(uri; query=query); verbose=verbose, args...) diff --git a/src/uri.jl b/src/uri.jl index 0487d75d8..c87d9f5b0 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -126,7 +126,7 @@ function printuri(io::IO, if sch in uses_authority print(io, sch, "://") !isempty(userinfo) && print(io, userinfo, "@") - print(io, ':' in host? "[$host]" : host) + print(io, ':' in host ? "[$host]" : host) print(io, ((sch == "http" && port == "80") || (sch == "https" && port == "443") || isempty(port)) ? "" : ":$port") elseif path != "" && path != "*" && sch != "" diff --git a/test/messages.jl b/test/messages.jl index 5499252ca..a42ef3c42 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -1,6 +1,9 @@ module MessagesTest using Base.Test +if VERSION > v"0.7.0-DEV.2338" +using Unicode +end using HTTP.Messages import HTTP.Messages.appendheader diff --git a/test/uri.jl b/test/uri.jl index cf5fd2fca..f534e6b23 100644 --- a/test/uri.jl +++ b/test/uri.jl @@ -12,7 +12,13 @@ struct Offset len::UInt16 end -offsetss(uri, offset) = SubString(uri, offset.off, offset.off + offset.len-1) +function offsetss(uri, offset) + if offset == Offset(0,0) + return SubString(uri, 1, 0) + else + return SubString(uri, offset.off, offset.off + offset.len-1) + end +end function URLTest(nm::String, url::String, isconnect::Bool, shouldthrow::Bool) URLTest(nm, url, isconnect, HTTP.URI(""), shouldthrow) From 16836f6694f758e699089afa808f69f2fb2c701d Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 20 Dec 2017 09:52:28 +1100 Subject: [PATCH 069/182] remove unreachable state s_dead --- src/parser.jl | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index 6c19dfa0c..32037e680 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -336,12 +336,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int p += 1 @inbounds ch = Char(bytes[p]) - if p_state == s_dead - # This state is used after a 'Connection: close' message - # the parser will error out if it reads another message - @errorif(ch != CR && ch != LF, HPE_CLOSED_CONNECTION) - - elseif p_state == s_start_req_or_res + if p_state == s_start_req_or_res (ch == CR || ch == LF) && continue parser.flags = 0 parser.content_length = ULLONG_MAX From 2fcc81e5c062036e3c25e7d65d2f36d91ede797d Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 20 Dec 2017 09:41:15 +1100 Subject: [PATCH 070/182] split parse! into parseheaders! and parsebody! --- src/consts.jl | 6 ++- src/parser.jl | 125 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 99 insertions(+), 32 deletions(-) diff --git a/src/consts.jl b/src/consts.jl index c4e816a1a..0220e3a40 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -281,6 +281,7 @@ const ParsingErrorCodeMap = Dict( ,es_req_first_http_minor ,es_req_http_minor ,es_req_line_almost_done + ,es_trailer_start ,es_header_field_start ,es_header_field ,es_header_value_discard_ws @@ -290,12 +291,13 @@ const ParsingErrorCodeMap = Dict( ,es_header_value ,es_header_value_lws ,es_header_almost_done + ,es_headers_almost_done + ,es_headers_done + ,es_body_start ,es_chunk_size_start ,es_chunk_size ,es_chunk_parameters ,es_chunk_size_almost_done - ,es_headers_almost_done - ,es_headers_done ,es_chunk_data ,es_chunk_data_almost_done ,es_chunk_data_done diff --git a/src/parser.jl b/src/parser.jl index 32037e680..9f3d82590 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -226,7 +226,7 @@ messagestarted(p::Parser) = p.state != s_start_req_or_res Has the `Parser` processed the entire Message Header? """ -headerscomplete(p::Parser) = p.state >= s_headers_done +headerscomplete(p::Parser) = p.state > s_headers_done """ @@ -281,7 +281,7 @@ end macro err(code) - esc(:(throw(ParsingError(parser, $code)))) + esc(:(parser.state = p_state; throw(ParsingError(parser, $code)))) end macro errorif(cond, err) @@ -321,14 +321,36 @@ const ByteView = typeof(view(UInt8[], 1:0)) function parse!(parser::Parser, bytes::ByteView)::Int + l = length(bytes) + c = 0 + while c < l + if !headerscomplete(parser) + n = parseheaders!(parser, bytes) + else + n = parsebody!(parser, bytes) + end + c += n + if messagecomplete(parser) + break + end + if c < l + bytes = view(bytes, n+1:length(bytes)) + end + end + return c +end + +function parseheaders!(parser::Parser, bytes::ByteView)::Int + isempty(bytes) && throw(ArgumentError("bytes must not be empty")) + headerscomplete(parser) && throw(ArgumentError("headers already complete")) len = length(bytes) p_state = parser.state - @debug 2 "parse!(parser.state=$(ParsingStateCode(p_state))), $len-bytes:\n" * - escapelines(String(collect(bytes))) * ")" + @debug 2 "parseheaders!(parser.state=$(ParsingStateCode(p_state))), " * + "$len-bytes:\n" * escapelines(String(collect(bytes))) * ")" p = 0 - while p < len && p_state != s_message_done + while p < len && p_state <= s_headers_done @debug 3 string("top of while($p < $len) \"", Base.escape_string(string(Char(bytes[p+1]))), "\" ", @@ -690,7 +712,8 @@ function parse!(parser::Parser, bytes::ByteView)::Int @errorif(ch != LF, HPE_LF_EXPECTED) p_state = s_header_field_start - elseif p_state == s_header_field_start + elseif p_state == s_trailer_start || + p_state == s_header_field_start if ch == CR p_state = s_headers_almost_done elseif ch == LF @@ -1069,33 +1092,35 @@ function parse!(parser::Parser, bytes::ByteView)::Int if (parser.flags & F_TRAILING) > 0 # End of a chunked request p_state = s_message_done - continue - end + else - # Cannot use chunked encoding and a content-length header together - # per the HTTP specification. - @errorif((parser.flags & F_CHUNKED) > 0 && - (parser.flags & F_CONTENTLENGTH) > 0, - HPE_UNEXPECTED_CONTENT_LENGTH) + # Cannot use chunked encoding and a content-length header + # together per the HTTP specification. + @errorif((parser.flags & F_CHUNKED) > 0 && + (parser.flags & F_CONTENTLENGTH) > 0, + HPE_UNEXPECTED_CONTENT_LENGTH) - p_state = s_headers_done - parser.state = p_state + p_state = s_headers_done - # Set this here so that on_headers_complete() callbacks can see it - if (parser.flags & F_UPGRADE > 0) && - (parser.flags & F_CONNECTION_UPGRADE > 0) - parser.message.upgrade = isrequest(parser) || - parser.message.status == 101 - else - parser.message.upgrade = isrequest(parser) && - parser.message.method == CONNECT + # Set this here for onheaderscomplete() callback. + if (parser.flags & F_UPGRADE > 0) && + (parser.flags & F_CONNECTION_UPGRADE > 0) + parser.message.upgrade = isrequest(parser) || + parser.message.status == 101 + else + parser.message.upgrade = isrequest(parser) && + parser.message.method == CONNECT + end + @debugshow 3 parser.message.upgrade end - @debugshow 3 parser.message.upgrade - @debug 3 "headersdone" - parser.onheaderscomplete(parser.message) elseif p_state == s_headers_done @errorifstrict(ch != LF) + + @debug 3 "headersdone" + parser.state = p_state + parser.onheaderscomplete(parser.message) + if parser.isheadresponse || parser.content_length == 0 || (parser.message.upgrade && isrequest(parser) && @@ -1118,7 +1143,44 @@ function parse!(parser::Parser, bytes::ByteView)::Int p_state = s_body_identity_eof end - elseif p_state == s_body_identity + else + @err HPE_INVALID_INTERNAL_STATE + end + end + + @assert p <= len + @assert p == len || + p_state == s_message_done || + p_state == s_chunk_size_start || + p_state == s_body_identity || + p_state == s_body_identity_eof + + @debug 3 "parseheaders!() exiting $(ParsingStateCode(p_state))" + + parser.state = p_state + return p +end + + +function parsebody!(parser::Parser, bytes::ByteView)::Int + + isempty(bytes) && throw(ArgumentError("bytes must not be empty")) + !headerscomplete(parser) && throw(ArgumentError("headers not complete")) + len = length(bytes) + p_state = parser.state + @debug 2 "parsebody!(parser.state=$(ParsingStateCode(p_state))), " * + "$len-bytes:\n" * escapelines(String(collect(bytes))) * ")" + + p = 0 + while p < len && p_state < s_message_done && p_state != s_trailer_start + + @debug 3 string("top of while($p < $len) \"", + Base.escape_string(string(Char(bytes[p+1]))), "\" ", + ParsingStateCode(p_state)) + p += 1 + @inbounds ch = Char(bytes[p]) + + if p_state == s_body_identity to_read = Int(min(parser.content_length, len - p + 1)) @passert parser.content_length != 0 && parser.content_length != ULLONG_MAX @@ -1189,7 +1251,7 @@ function parse!(parser::Parser, bytes::ByteView)::Int if parser.content_length == 0 parser.flags |= F_TRAILING - p_state = s_header_field_start + p_state = s_trailer_start else p_state = s_chunk_data end @@ -1227,7 +1289,6 @@ function parse!(parser::Parser, bytes::ByteView)::Int @err HPE_INVALID_INTERNAL_STATE end end - @assert p_state == s_message_done || p == len # Consume trailing end of line after message. if p_state == s_message_done @@ -1241,7 +1302,11 @@ function parse!(parser::Parser, bytes::ByteView)::Int end @assert p <= len - @debug 3 "parse!() exiting $(ParsingStateCode(p_state))" + @assert p == len || + p_state == s_message_done || + p_state == s_trailer_start + + @debug 3 "parsebody!() exiting $(ParsingStateCode(p_state))" parser.state = p_state return p From dbc37df386a0505a3776c00495224b1e20eff14c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 20 Dec 2017 12:37:51 +1100 Subject: [PATCH 071/182] add readbody! and readheaders! --- src/parser.jl | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/src/parser.jl b/src/parser.jl index 9f3d82590..d2f830542 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -24,7 +24,8 @@ module Parsers -export Parser, parse!, reset!, +export Parser, parse!, parseheaders!, parsebody!, reset!, + readheaders!, readbody!, messagestarted, messagecomplete, headerscomplete, waitingforeof, connectionclosed, ParsingError, ParsingErrorCode @@ -178,6 +179,77 @@ function Base.read!(io::IO, p::Parser; unread=IOExtras.unread!) end +""" + readheaders!(io, ::Parser [, unread=IOExtras.unread!]) + +Read data from `io` into the `Parser` until `eof` +or until the parser finds the end of the Headers. + +If `readavailable(io)` reads past the end of the Headers the excess bytes +are passed to `unread`. + +Throws `ParsingError` if input is invalid. +""" + +function readheaders!(io::IO, p::Parser; unread=IOExtras.unread!) + + while !eof(io) + bytes = readavailable(io) + n = parse!(p, bytes) + if n < length(bytes) + unread(io, view(bytes, n+1:length(bytes))) + end + if headerscomplete(p) + return + end + end + @debug 2 "readheaders!(::$(typeof(io)), " * + "Parser($(ParsingStateCode(p.state)))) eof!" + + if !messagestarted(p) + throw(EOFError()) + end + if !waitingforeof(p) + throw(ParsingError(p, HPE_HEADERS_INCOMPLETE)) + end + return +end + + +""" + readbody!(io, ::Parser [, unread=IOExtras.unread!]) + +Read data from `io` into the `Parser` until `eof` +or until the parser finds the end of the Message Body. + +If `readavailable(io)` reads past the end of the Message the excess bytes +are passed to `unread`. + +Throws `ParsingError` if input is invalid. +""" + +function readbody!(io::IO, p::Parser; unread=IOExtras.unread!) + + while !eof(io) + bytes = readavailable(io) + n = parsebody!(p, bytes) + if n < length(bytes) + unread(io, view(bytes, n+1:length(bytes))) + end + if messagecomplete(p) + return + end + end + @debug 2 "readbody!(::$(typeof(io)), " * + "Parser($(ParsingStateCode(p.state)))) eof!" + + if !waitingforeof(p) + throw(ParsingError(p, HPE_BODY_INCOMPLETE)) + end + return +end + + """ reset!(::Parser) @@ -340,10 +412,14 @@ function parse!(parser::Parser, bytes::ByteView)::Int return c end + +parseheaders!(p::Parser, bytes) = parseheaders!(p, view(bytes, 1:length(bytes))) + function parseheaders!(parser::Parser, bytes::ByteView)::Int isempty(bytes) && throw(ArgumentError("bytes must not be empty")) headerscomplete(parser) && throw(ArgumentError("headers already complete")) + len = length(bytes) p_state = parser.state @debug 2 "parseheaders!(parser.state=$(ParsingStateCode(p_state))), " * @@ -1162,10 +1238,13 @@ function parseheaders!(parser::Parser, bytes::ByteView)::Int end +parsebody!(p::Parser, bytes) = parsebody!(p, view(bytes, 1:length(bytes))) + function parsebody!(parser::Parser, bytes::ByteView)::Int isempty(bytes) && throw(ArgumentError("bytes must not be empty")) !headerscomplete(parser) && throw(ArgumentError("headers not complete")) + len = length(bytes) p_state = parser.state @debug 2 "parsebody!(parser.state=$(ParsingStateCode(p_state))), " * From 17d080fcf924c919f5ad47436f9dac48031c7e95 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 21 Dec 2017 10:26:08 +1100 Subject: [PATCH 072/182] API notes --- docs/src/index.md | 152 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/docs/src/index.md b/docs/src/index.md index 03ab02e44..3339c0278 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -56,6 +56,158 @@ HTTP.escapeHTML # HTTP.jl Architecture + +## User Interface + +The basic API function is: + + `HTTP.request(method, url [, headers [, body [, response_body]]]) -> HTTP.Response` + +`headers` can be any collection where +`[string(k) => string(v) for (k,v) in headers]` yields `Vector{Pair}`. + + +`body` can take a number of forms: + + - a `String` or `AbstractVector{UInt8}`, or + - a collection where `eltype` is `String` or `AbstractVector{UInt8}`, or + - an readable `IO` stream or any `IO`-like type `T` for which + `eof(T)` and `readavailable(T)` are defined. + +`response_body` can be a writeable `IO` stream or any `IO`-like type `T` +for which `write(T, AbstractVector{UInt8})` is defined. + + +The `HTTP.Response` struct contains: + + - `status::Int16` e.g. `200` + - `headers::Vector{Pair{String,String}}` + e.g. ["Server" => "Apache", "Content-Type" => "text/html"] + - `body::IO`, the `response_body` or, by default, an `IOBuffer()`. + + +The `HTTP.open` API allows the Request Body to be streamed to an `IO` channel: + + `HTTP.open(method, url, [,headers]) -> HTTP.BodyStream` + `write(::HTTP.BodyStream, bytes)` + `close(::HTTP.BodyStream) -> HTTP.Response` + + +The `HTTP.open` API also allows the Response Body to be streamed: + + `HTTP.open(method, url, [,headers]) -> HTTP.BodyStream` + `write(::HTTP.BodyStream, bytes)` + `closewrite(::HTTP.BodyStream) -> HTTP.Response` + `read(::HTTP.BodyStream) -> AbstractVector{UInt8}` + `close(::HTTP.BodyStream) -> HTTP.Response` + + +## User Interface Examples + +## Request Body Examples + +``` +r = request("POST", "http://httpbin.org/post", [], "post body data") +@show r.status +``` + +``` +io = open("post_data.txt", "r") +r = request("POST", "http://httpbin.org/post", [], io) +@show r.status +``` + +``` +chunks = ("chunk$i" for i in 1:1000) +r = request("POST", "http://httpbin.org/post", [], chunks) +@show r.status +``` + +``` +chunks = [preamble_chunk, data_chunk, checksum(data_chunk)] +r = request("POST", "http://httpbin.org/post", [], chunks) +@show r.status +``` + +``` +io = HTTP.open("POST", "http://httpbin.org/post") +write(io, preamble) +write(io, data) +write(io, checksum(data)) +r = close(io) +@show r.status +``` + + +## Response Body Examples + +``` +r = request("GET", "http://httpbin.org/get") +@show r.status +println(read(r.body)) +``` + +``` +io = open("get_data.txt", "r") +r = request("GET", "http://httpbin.org/get", [], "", io) +@show r.status +println(read("get_data.txt")) +``` + +``` +io = BufferStream() +@async while !eof(io) + bytes = readavailable(io)) + println("GET data: $bytes") +end +r = request("GET", "http://httpbin.org/get", [], "", io) +@show r.status +``` + +``` +io = HTTP.open("GET", "http://httpbin.org/get") +r = closewrite(io) +@show r.status +while !eof(io) + bytes = readavailable(io)) + println("GET data: $bytes") +end +close(io) +``` + + +## Request and Response Body Examples + + +``` +r = request("POST", "http://httpbin.org/post", [], "post body data") +@show r.status +println(read(r.body)) +``` + +``` +in = open("foo.png", "r") +out = open("foo.jpg", "w") +r = request("POST", "http://convert.com/png2jpg", [], in, out) +@show r.status +``` + +``` +io = HTTP.open("POST", "http://music.com/play") +write(io, JSON.json([ + "auth" => "12345XXXX", + "song_id" => 7, +])) +r = closewrite(io) +@show r.status +while !eof(io) + bytes = readavailable(io)) + play_audio(bytes) +end +close(io) +``` + + ## Parser Source: `Parsers.jl` From 5f132138179cfa4dc3e0155d839dd95692f3b464 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 21 Dec 2017 10:31:09 +1100 Subject: [PATCH 073/182] API notes --- docs/src/index.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 3339c0278..325dc61fd 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -131,9 +131,9 @@ r = request("POST", "http://httpbin.org/post", [], chunks) ``` io = HTTP.open("POST", "http://httpbin.org/post") -write(io, preamble) -write(io, data) -write(io, checksum(data)) +write(io, preamble_chunk) +write(io, data_chunk) +write(io, checksum(data_chunk)) r = close(io) @show r.status ``` From f547a28fc438fc9f3f5f17a591f26b03ec133cb0 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 21 Dec 2017 10:40:18 +1100 Subject: [PATCH 074/182] API notes --- docs/src/index.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 325dc61fd..08b8f80fd 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -83,7 +83,8 @@ The `HTTP.Response` struct contains: - `status::Int16` e.g. `200` - `headers::Vector{Pair{String,String}}` e.g. ["Server" => "Apache", "Content-Type" => "text/html"] - - `body::IO`, the `response_body` or, by default, an `IOBuffer()`. + - `body::Vector{UInt8}`, the Response Body bytes. + Empty if a `response_body` `IO` stream was specified in the `request`. The `HTTP.open` API allows the Request Body to be streamed to an `IO` channel: @@ -144,7 +145,7 @@ r = close(io) ``` r = request("GET", "http://httpbin.org/get") @show r.status -println(read(r.body)) +println(String(r.body)) ``` ``` @@ -182,7 +183,7 @@ close(io) ``` r = request("POST", "http://httpbin.org/post", [], "post body data") @show r.status -println(read(r.body)) +println(String(r.body)) ``` ``` From 5aa1b0b913ed343aaf34dcbd475d71e8a5d72dd6 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 26 Dec 2017 16:50:19 +1100 Subject: [PATCH 075/182] More extreme async and streaming tests: async.jl New HTTPStream <: IO module replaces Bodies.jl - Brings together Parser, Message and Connection - Provides transparent IO-compatible writing/reading of chunked bodies. - Provides a HTTP Body-aware eof() - Supports better streaming via new open() API. New HTTP.open() API function for streaming bodies: HTTP.open(...) do io ... end - Removes the need for tasks and condition signaling. parser.jl - Remove callback functions. - Reduce interface down to parseheaders() and parsebody() - parseheaders is now called as: parseheaders(p, bytes) do header ... end - parseheaders returns a SubArray of the unprocessed bytes. - parsebody returns a tuple of SubArrays: the processed and unprocessed bytes. Messages.jl - Replace ::Body with ::Vector{UInt8}. Simpler. - Add explict Response.request and Request.response fields. - Remove obsolete waiting functions - Remove connectparser. Parser no longer needs connecting. - Add readtrailers() - Remove writeandread() - now handled by StreamLayer ConnectionRequest.jl: - close io on error ConnectionPool.jl - Add reuse_limit option. e.g. httpbin.org seems to drop the connection after 100 requests, so it's better to set a limit to avoid sending hundreds of requests to a connection when only the first few will get processed. IOExtras.jl - add unread! methods for IOBuffer and BufferStream (for use in testing) Move MessageLayer above RetryLayer and ExceptionLayer to give RetryLayer access to Request and Response structs. Used to determine retryability of body streaming. MessageRequest.jl - add bodylength and bodybytes functions (with many methods for different types) - use these to set Conent-Length/Transfer-Encoding RetryRequest.jl - Use special const values and === to mark "body_was_streamed" to avoid reusing a stream. consts.jl - remove unused consts Move VERSION specific stuff to compat.jl --- docs/src/index.md | 82 +++++---- src/BasicAuthRequest.jl | 7 +- src/CanonicalizeRequest.jl | 5 +- src/ConnectionPool.jl | 56 ++++-- src/ConnectionRequest.jl | 18 +- src/CookieRequest.jl | 11 +- src/HTTP.jl | 88 ++++------ src/HTTPStreams.jl | 117 +++++++++++++ src/IOExtras.jl | 37 ++++ src/MessageRequest.jl | 68 ++++---- src/Messages.jl | 304 ++++++++++++++------------------ src/RedirectRequest.jl | 7 +- src/RetryRequest.jl | 19 +- src/SocketRequest.jl | 37 ---- src/StreamRequest.jl | 66 +++++++ src/compat.jl | 24 +++ src/consts.jl | 50 +----- src/parser.jl | 344 ++++++++++++------------------------- test/async.jl | 171 +++++++++++------- test/client.jl | 24 +-- test/messages.jl | 29 ++-- test/parser.jl | 127 ++++++++------ 22 files changed, 888 insertions(+), 803 deletions(-) create mode 100644 src/HTTPStreams.jl delete mode 100644 src/SocketRequest.jl create mode 100644 src/StreamRequest.jl create mode 100644 src/compat.jl diff --git a/docs/src/index.md b/docs/src/index.md index 08b8f80fd..abce6bff1 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -61,7 +61,7 @@ HTTP.escapeHTML The basic API function is: - `HTTP.request(method, url [, headers [, body [, response_body]]]) -> HTTP.Response` + `HTTP.request(method, url [, headers [, body]] [, response_stream=]) -> HTTP.Response` `headers` can be any collection where `[string(k) => string(v) for (k,v) in headers]` yields `Vector{Pair}`. @@ -69,12 +69,14 @@ The basic API function is: `body` can take a number of forms: - - a `String` or `AbstractVector{UInt8}`, or - - a collection where `eltype` is `String` or `AbstractVector{UInt8}`, or + - a `String`, a `Vector{UInt8}` or a readable `IO` + or any `T` accetped by `write(::IO, ::T)` + - a collection of `String` or `AbstractVector{UInt8}` or `IO` + or any `T` accetped by `write(::IO, ::T...)` - an readable `IO` stream or any `IO`-like type `T` for which `eof(T)` and `readavailable(T)` are defined. -`response_body` can be a writeable `IO` stream or any `IO`-like type `T` +`response_stream` can be a writeable `IO` stream or any `IO`-like type `T` for which `write(T, AbstractVector{UInt8})` is defined. @@ -84,23 +86,27 @@ The `HTTP.Response` struct contains: - `headers::Vector{Pair{String,String}}` e.g. ["Server" => "Apache", "Content-Type" => "text/html"] - `body::Vector{UInt8}`, the Response Body bytes. - Empty if a `response_body` `IO` stream was specified in the `request`. + Empty if a `response_stream` was specified in the `request`. The `HTTP.open` API allows the Request Body to be streamed to an `IO` channel: - `HTTP.open(method, url, [,headers]) -> HTTP.BodyStream` - `write(::HTTP.BodyStream, bytes)` - `close(::HTTP.BodyStream) -> HTTP.Response` +``` + HTTP.open(method, url, [,headers]) do io + write(io, bytes) + end -> HTTP.Response +``` The `HTTP.open` API also allows the Response Body to be streamed: - `HTTP.open(method, url, [,headers]) -> HTTP.BodyStream` - `write(::HTTP.BodyStream, bytes)` - `closewrite(::HTTP.BodyStream) -> HTTP.Response` - `read(::HTTP.BodyStream) -> AbstractVector{UInt8}` - `close(::HTTP.BodyStream) -> HTTP.Response` +``` + HTTP.open(method, url, [,headers]) do io + write(io, bytes) + readresponse(io) + read(io) -> AbstractVector{UInt8} + end -> HTTP.Response +``` ## User Interface Examples @@ -131,11 +137,11 @@ r = request("POST", "http://httpbin.org/post", [], chunks) ``` ``` -io = HTTP.open("POST", "http://httpbin.org/post") -write(io, preamble_chunk) -write(io, data_chunk) -write(io, checksum(data_chunk)) -r = close(io) +r = HTTP.open("POST", "http://httpbin.org/post") do io + write(io, preamble_chunk) + write(io, data_chunk) + write(io, checksum(data_chunk)) +end @show r.status ``` @@ -150,7 +156,7 @@ println(String(r.body)) ``` io = open("get_data.txt", "r") -r = request("GET", "http://httpbin.org/get", [], "", io) +r = request("GET", "http://httpbin.org/get", response_stream=io) @show r.status println(read("get_data.txt")) ``` @@ -161,19 +167,19 @@ io = BufferStream() bytes = readavailable(io)) println("GET data: $bytes") end -r = request("GET", "http://httpbin.org/get", [], "", io) +r = request("GET", "http://httpbin.org/get", response_stream=io) @show r.status ``` ``` -io = HTTP.open("GET", "http://httpbin.org/get") -r = closewrite(io) -@show r.status -while !eof(io) - bytes = readavailable(io)) - println("GET data: $bytes") +r = HTTP.open("GET", "http://httpbin.org/get") do io + r = readresponse(io) + @show r.status + while !eof(io) + bytes = readavailable(io)) + println("GET data: $bytes") + end end -close(io) ``` @@ -194,18 +200,18 @@ r = request("POST", "http://convert.com/png2jpg", [], in, out) ``` ``` -io = HTTP.open("POST", "http://music.com/play") -write(io, JSON.json([ - "auth" => "12345XXXX", - "song_id" => 7, -])) -r = closewrite(io) -@show r.status -while !eof(io) - bytes = readavailable(io)) - play_audio(bytes) +HTTP.open("POST", "http://music.com/play") do io + write(io, JSON.json([ + "auth" => "12345XXXX", + "song_id" => 7, + ])) + r = readresponse(io) + @show r.status + while !eof(io) + bytes = readavailable(io)) + play_audio(bytes) + end end -close(io) ``` diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index cc0181f75..5190984cd 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -14,17 +14,16 @@ export BasicAuthLayer function request(::Type{BasicAuthLayer{Next}}, - method::String, uri, headers, body, response_body; - kw...) where Next + method::String, uri::URI, headers, body; kw...) where Next - userinfo = URI(uri).userinfo + userinfo = uri.userinfo if !isempty(userinfo) && getkv(headers, "Authorization", "") == "" @debug 1 "Adding Authorization: Basic header." setkv(headers, "Authorization", "Basic $(base64encode(userinfo))") end - return request(Next, method, uri, headers, body, response_body,; kw...) + return request(Next, method, uri, headers, body; kw...) end diff --git a/src/CanonicalizeRequest.jl b/src/CanonicalizeRequest.jl index 3410b0c2c..128d0e8f0 100644 --- a/src/CanonicalizeRequest.jl +++ b/src/CanonicalizeRequest.jl @@ -11,12 +11,11 @@ export CanonicalizeLayer canonicalizeheaders(h::T) where T = T([tocameldash!(k) => v for (k,v) in h]) function request(::Type{CanonicalizeLayer{Next}}, - method::String, uri, headers, body, response_body; - kw...) where Next + method::String, uri, headers, body; kw...) where Next headers = canonicalizeheaders(headers) - res = request(Next, method, uri, headers, body, response_body; kw...) + res = request(Next, method, uri, headers, body; kw...) res.headers = canonicalizeheaders(res.headers) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index d8a42caf6..f61349ebc 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -10,8 +10,12 @@ import ..Connect: getconnection, getparser import ..Parsers.Parser const max_duplicates = 8 +const nolimit = typemax(Int) -const ByteView = typeof(view(UInt8[], 1:0)) +const nobytes = view(UInt8[], 1:0) +const ByteView = typeof(nobytes) +byteview(bytes::ByteView) = bytes +byteview(bytes)::ByteView = view(bytes, 1:length(bytes)) """ @@ -59,13 +63,14 @@ Base.unsafe_write(c::Connection, p::Ptr{UInt8}, n::UInt) = Base.eof(c::Connection) = isempty(c.excess) && eof(c.io) -function Base.readavailable(c::Connection) + +function Base.readavailable(c::Connection)::ByteView if !isempty(c.excess) bytes = c.excess @debug 3 "read $(length(bytes))-bytes from excess buffer." - c.excess = view(UInt8[], 1:0) + c.excess = nobytes else - bytes = readavailable(c.io) + bytes = byteview(readavailable(c.io)) @debug 3 "read $(length(bytes))-bytes from $(typeof(c.io))" end return bytes @@ -79,10 +84,7 @@ Push bytes back into a connection's `excess` buffer (to be returned by the next read). """ -function IOExtras.unread!(c::Connection, bytes::ByteView) - @assert isempty(c.excess) - c.excess = bytes -end +IOExtras.unread!(c::Connection, bytes::ByteView) = c.excess = bytes """ @@ -131,6 +133,7 @@ function IOExtras.closeread(c::Connection) if islocked(c.readlock) unlock(c.readlock) end + notify(poolcondition) return end @@ -190,16 +193,39 @@ Find `Connections` in the `pool` that are ready for writing. function findwriteable(T::Type, host::AbstractString, - port::AbstractString) + port::AbstractString, + reuse_limit::Int=nolimit) filter(c->(typeof(c.io) == T && c.host == host && c.port == port && + c.writecount < reuse_limit && !islocked(c.writelock) && isopen(c.io)), pool) end +""" + findoverused(type, host, port, reuse_limit) -> Vector{Connection} + +Find `Connections` in the `pool` that are over the reuse limit +and have no more active readers. +""" + +function findoverused(T::Type, + host::AbstractString, + port::AbstractString, + reuse_limit::Int) + + filter(c->(typeof(c.io) == T && + c.host == host && + c.port == port && + c.readcount >= reuse_limit && + !islocked(c.readlock) && + isopen(c.io)), pool) +end + + """ findall(type, host, port) -> Vector{Connection} @@ -246,16 +272,26 @@ or create a new `Connection` if required. function getconnection(::Type{Connection{T}}, host::AbstractString, port::AbstractString; + reuse_limit::Int = nolimit, kw...)::Connection{T} where T <: IO while true lock(poollock) try + + # Close connections that have reached the reuse limit... + if reuse_limit != nolimit + for c in findoverused(T, host, port, reuse_limit) + close(c) + end + end + + # Remove closed connections from `pool`... purge() # Try to find a connection with no active readers or writers... - writeable = findwriteable(T, host, port) + writeable = findwriteable(T, host, port, reuse_limit) idle = filter(c->!islocked(c.readlock), writeable) if !isempty(idle) c = rand(idle) ;@debug 2 "Idle: $c" diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index f980dea89..f4b983aa6 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -21,12 +21,17 @@ Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. """ function request(::Type{ConnectionPoolLayer{Next}}, - uri::URI, req::Request, res::Response; kw...) where Next + uri::URI, req, body; kw...) where Next Connection = ConnectionPool.Connection{sockettype(uri)} io = getconnection(Connection, uri.host, uri.port; kw...) - return request(Next, io, req, res; kw...) + try + return request(Next, io, req, body; kw...) + catch e + @schedule close(io) + rethrow(e) + end end @@ -34,11 +39,16 @@ abstract type ConnectLayer{Next <: Layer} <: Layer end export ConnectLayer function request(::Type{ConnectLayer{Next}}, - uri::URI, req::Request, res::Response; kw...) where Next + uri::URI, req, body; kw...) where Next io = getconnection(sockettype(uri), uri.host, uri.port; kw...) - return request(Next, io, req, res; kw...) + try + return request(Next, io, req, body; kw...) + catch e + @schedule close(io) + rethrow(e) + end end diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index da14b87d5..8ca3521c7 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -46,20 +46,19 @@ end function request(::Type{CookieLayer{Next}}, - method::String, uri, headers, body, response_body; + method::String, uri::URI, headers, body; cookiejar=default_cookiejar, kw...) where Next - u = URI(uri) - hostcookies = get!(cookiejar, u.host, Set{Cookie}()) + hostcookies = get!(cookiejar, uri.host, Set{Cookie}()) - cookies = getcookies(hostcookies, u) + cookies = getcookies(hostcookies, uri) if !isempty(cookies) setkv(headers, "Cookie", string(getkv(headers, "Cookie", ""), cookies)) end - res = request(Next, method, uri, headers, body, response_body; kw...) + res = request(Next, method, uri, headers, body; kw...) - setcookies(hostcookies, u.host, res.headers) + setcookies(hostcookies, uri.host, res.headers) return res end diff --git a/src/HTTP.jl b/src/HTTP.jl index 9454f9fe7..d6690d293 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -5,37 +5,11 @@ using MbedTLS import MbedTLS.SSLContext -import Base.== # FIXME rm - const DEBUG_LEVEL = 0 - -if VERSION > v"0.7.0-DEV.2338" - using Base64 -end - -@static if VERSION >= v"0.7.0-DEV.2915" - 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 - import Dates -end - const minimal = false +include("compat.jl") + include("debug.jl") include("Pairs.jl") include("Strings.jl") @@ -52,46 +26,47 @@ include("parser.jl"); import .Parsers.ParsingError include("Connect.jl") include("ConnectionPool.jl") include("Messages.jl"); using .Messages +include("HTTPStreams.jl"); using .HTTPStreams module RequestStack import ..HTTP - import ..Body + using ..URIs + import ..Messages.mkheaders + import ..Messages.Response + import ..Parsers.Headers - function request(method::String, uri, headers, body::Body, - response_body::Body; kw...) + request(method, uri, headers=[], body=UInt8[]; kw...) = + request(string(method), URI(uri), mkheaders(headers), body; kw...) - request(HTTP.stack(;kw...), - method, uri, headers, body, response_body; kw...) - end + request(method::String, uri::URI, headers::Headers, body; kw...)::Response = + request(HTTP.stack(;kw...), method, uri, headers, body; kw...) +end - function request(method::String, uri, headers=[], body=""; - bodylength=HTTP.Messages.Bodies.unknownlength, - response_stream=nothing, kw...) +open(f::Function, method::String, uri, headers=[]; kw...) = + RequestStack.request(method, uri, headers; iofunction=f, kw...) - request(method, uri, headers, Body(body, bodylength), - Body(response_stream); kw...) - end -end - if minimal -import .RequestStack.request - end +httpget(a...; kw...) = RequestStack.request("GET", a..., kw...) +httpput(a...; kw...) = RequestStack.request("PUT", a..., kw...) +httppost(a...; kw...) = RequestStack.request("POST", a..., kw...) +httphead(a...; kw...) = RequestStack.request("HEAD", a..., kw...) abstract type Layer end const NoLayer = Union - -include("SocketRequest.jl"); using .SocketRequest -include("ConnectionRequest.jl"); using .ConnectionRequest + if !minimal +include("RedirectRequest.jl"); using .RedirectRequest +include("BasicAuthRequest.jl"); using .BasicAuthRequest +include("CookieRequest.jl"); using .CookieRequest +include("CanonicalizeRequest.jl"); using .CanonicalizeRequest + end include("MessageRequest.jl"); using .MessageRequest include("ExceptionRequest.jl"); using .ExceptionRequest import .ExceptionRequest.StatusError - if !minimal include("RetryRequest.jl"); using .RetryRequest -include("CookieRequest.jl"); using .CookieRequest -include("BasicAuthRequest.jl"); using .BasicAuthRequest -include("CanonicalizeRequest.jl"); using .CanonicalizeRequest -include("RedirectRequest.jl"); using .RedirectRequest +include("ConnectionRequest.jl"); using .ConnectionRequest +include("StreamRequest.jl"); using .StreamRequest + if !minimal function stack(;redirect=true, basicauthorization=false, @@ -106,11 +81,11 @@ function stack(;redirect=true, (basicauthorization ? BasicAuthLayer : NoLayer){ (cookies ? CookieLayer : NoLayer){ (canonicalizeheaders ? CanonicalizeLayer : NoLayer){ + MessageLayer{ (retry ? RetryLayer : NoLayer){ (statusexception ? ExceptionLayer : NoLayer){ - MessageLayer{ (connectionpool ? ConnectionPoolLayer : ConnectLayer){ - SocketLayer + StreamLayer }}}}}}}} end @@ -119,12 +94,14 @@ stack(;kw...) = ExceptionLayer{ MessageLayer{ ConnectionPoolLayer{ #ConnectLayer{ - SocketLayer}}} + StreamLayer}}} +import .RequestStack.request end if !minimal status(r) = r.status #FIXME headers(r) = Dict(r.headers) #FIXME +import Base.== # FIXME rm include("types.jl") include("client.jl") include("sniff.jl") @@ -133,5 +110,4 @@ include("server.jl"); using .Nitrogen include("precompile.jl") end - end # module diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl new file mode 100644 index 000000000..ae7eddca1 --- /dev/null +++ b/src/HTTPStreams.jl @@ -0,0 +1,117 @@ +module HTTPStreams + +export HTTPStream, readheaders, readtrailers + +using ..IOExtras +using ..Parsers +using ..Messages + + +struct HTTPStream{T <: Message} <: IO + stream::IO + message::T + parser::Parser + chunked::Bool +end + +function HTTPStream(io::IO, request::Request, parser::Parser) + chunked = header(request, "Transfer-Encoding") == "chunked" + HTTPStream{Response}(io, request.response, parser, chunked) +end + + +function Base.unsafe_write(http::HTTPStream, p::Ptr{UInt8}, n::UInt) + if !http.chunked + return unsafe_write(http.stream, p, n) + end + return write(http.stream, hex(n), "\r\n") + + unsafe_write(http.stream, p, n) + + write(http.stream, "\r\n") +end + + +writeend(http) = http.chunked ? write(http.stream, "0\r\n\r\n") : 0 + + +function Messages.readheaders(http::HTTPStream) + writeend(http) + closewrite(http.stream) + configure_parser(http) + return readheaders(http.stream, http.parser, http.message) +end + + +function configure_parser(http::HTTPStream{Response}) + reset!(http.parser) + if http.message.request.method in ("HEAD", "CONNECT") # FIXME Why CONNECT? + setheadresponse(http.parser) + end +end + +configure_parser(http::HTTPStream{Request}) = reset!(http.parser) + + +readheadersdone(http::HTTPStream) = http.message.status != 0 + + +function Base.eof(http::HTTPStream) + if !readheadersdone(http) + readheaders(http) + @assert readheadersdone(http) + end + if bodycomplete(http.parser) + return true + end + if eof(http.stream) + seteof(http.parser) + return true + end + return false +end + + +function Base.readavailable(http::HTTPStream)::ByteView + if !headerscomplete(http.parser) + throw(ArgumentError("headers must be read before body\n$http\n")) + end + bytes = readavailable(http.stream) + if isempty(bytes) + return nobytes + end + bytes, excess = parsebody(http.parser, bytes) + unread!(http, excess) + return bytes +end + + +IOExtras.unread!(http::HTTPStream, excess) = unread!(http.stream, excess) + + +function Base.read(http::HTTPStream) + buf = IOBuffer() + write(buf, http) + return take!(buf) +end + + +function Base.close(http::HTTPStream{Response}) + while !eof(http) + readavailable(http) + end + readtrailers(http.stream, http.parser, http.message) + + if !messagecomplete(http.parser) + @show http.parser + throw(EOFError()) + end + + if connectionclosed(http.parser) + close(http.stream) + else + closeread(http.stream) + end + return http.message +end + + +end #module HTTPStreams diff --git a/src/IOExtras.jl b/src/IOExtras.jl index 4fc5ac221..2b9b163c7 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -8,12 +8,49 @@ export unread!, closeread, closewrite Push bytes back into a connection (to be returned by the next read). """ +function unread!(io::IOBuffer, bytes) + l = length(bytes) + if l == 0 + return + end + + @assert bytes == io.data[io.ptr - l:io.ptr-1] + + if io.seekable + seek(io, io.ptr - (l + 1)) + return + end + + println("WARNING: Can't unread! non-seekable IOBuffer") + println(" Discarding $(length(bytes)) bytes!") + @assert false + return +end + +function unread!(io::BufferStream, bytes) + if length(bytes) == 0 + return + end + if nb_available(io) > 0 + buf = readavailable(io) + write(io, bytes) + write(io, buf) + else + write(io, bytes) + end + return +end + function unread!(io, bytes) + if length(bytes) == 0 + return + end println("WARNING: No unread! method for $(typeof(io))!") println(" Discarding $(length(bytes)) bytes!") end + """ closewrite(::IO) closeread(::IO) diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 11ea39b07..405fed413 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -3,58 +3,54 @@ module MessageRequest import ..Layer, ..RequestStack.request using ..URIs using ..Messages +using ..Parsers.Headers +using ..Form struct MessageLayer{Next <: Layer} <: Layer end -export MessageLayer +export MessageLayer, body_is_a_stream, body_was_streamed +const ByteVector = Union{AbstractVector{UInt8}, AbstractString} -""" - request(MessageLayer, method, uri [, headers=[] [, body="" ]; kw args...) -Execute a `Request` and return a `Response`. +const unknownlength = -1 +bodylength(body) = unknownlength +bodylength(body::ByteVector) = sizeof(body) +bodylength(body::Form) = length(body) +bodylength(body::Vector{ByteVector}) = sum(sizeof, body) +bodylength(body::IOBuffer) = nb_available(body) +bodylength(body::Vector{IOBuffer}) = sum(nb_available, body) -kw args: -- `parent=` optionally set a parent `Response`. +const body_is_a_stream = UInt8[] +const body_was_streamed = Vector{UInt8}("[Message Body was streamed]") +bodybytes(body) = body_is_a_stream +bodybytes(body::Vector{UInt8}) = body +bodybytes(body::IOBuffer) = read(body) +bodybytes(body::ByteVector) = Vector{UInt8}(body) +bodybytes(body::Vector) = length(body) == 1 ? bodybytes(body[1]) : UInt8[] -- `response_stream=` optional `IO` stream for response body. - - -e.g. use a stream as a request body: - -``` -io = open("request", "r") -r = request("POST", "http://httpbin.org/post", [], io) -``` - -e.g. send a response body to a stream: - -``` -io = open("response_file", "w") -r = request("GET", "http://httpbin.org/stream/100", response_stream=io) -println(stat("response_file").size) -0 -sleep(1) -println(stat("response_file").size) -14990 -``` -""" function request(::Type{MessageLayer{Next}}, - method::String, uri, headers, body::Body, response_body::Body; + method::String, uri::URI, headers::Headers, body; parent=nothing, kw...) where Next - u = URI(uri) - url = method == "CONNECT" ? hostport(u) : resource(u) + path = method == "CONNECT" ? hostport(uri) : resource(uri) - req = Request(method, url, headers, body; parent=parent) + defaultheader(headers, "Host" => uri.host) - defaultheader(req, "Host" => u.host) - setlengthheader(req) + if !hasheader(headers, "Content-Length") && + !hasheader(headers, "Transfer-Encoding") + l = bodylength(body) + if l != unknownlength + setheader(headers, "Content-Length" => string(l)) + else + setheader(headers, "Transfer-Encoding" => "chunked") + end + end - res = Response(body=response_body, parent=req) + req = Request(method, path, headers, bodybytes(body); parent=parent) - return request(Next, u, req, res; kw...) + return request(Next, uri, req, body; kw...) end diff --git a/src/Messages.jl b/src/Messages.jl index bf4f942dd..2ade961f4 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -1,10 +1,10 @@ module Messages -export Message, Request, Response, Body, - method, iserror, isredirect, parentcount, isstream, isstreamfresh, - header, setheader, defaultheader, setlengthheader, - waitforheaders, wait, - writeandread +export Message, Request, Response, + iserror, isredirect, ischunked, + header, hasheader, setheader, defaultheader, appendheader, + readheaders, readtrailers, writeheaders, + readstartline! if VERSION > v"0.7.0-DEV.2338" using Unicode @@ -12,89 +12,83 @@ end import ..HTTP -include("Bodies.jl") -using .Bodies - -using ..IOExtras using ..Pairs +using ..IOExtras using ..Parsers import ..Parsers -import ..ConnectionPool - -import ..@debug, ..DEBUG_LEVEL +abstract type Message end """ - Request + Response -Represents a HTTP Request Message. +Represents a HTTP Response Message. -- `method::String` -- `uri::String` - `version::VersionNumber` +- `status::Int16` - `headers::Vector{Pair{String,String}}` - `body::`[`HTTP.Body`](@ref) -- `parent::Response`, the `Response` (if any) that led to this request - (e.g. in the case of a redirect). +- `request`, the `Request` that yielded this `Response`. """ -mutable struct Request - method::String - uri::String +mutable struct Response <: Message version::VersionNumber - headers::Vector{Pair{String,String}} - body::Body - parent + status::Int16 + headers::Headers + body::Vector{UInt8} + request end -Request() = Request("", "") -Request(method::String, uri, headers=[], body=Body(); parent=nothing) = - Request(method, uri == "" ? "/" : uri, v"1.1", - mkheaders(headers), body, parent) - -Request(bytes) = read!(IOBuffer(bytes), Request()) -Base.parse(::Type{Request}, str::AbstractString) = Request(str) +Response(status::Int=0, headers=[]; body=UInt8[], request=nothing) = + Response(v"1.1", status, mkheaders(headers), body, request) -mkheaders(v::Vector{Pair{String,String}}) = v -mkheaders(x) = [string(k) => string(v) for (k,v) in x] +Response(bytes) = parse(Response, bytes) """ - Response + Request -Represents a HTTP Response Message. +Represents a HTTP Request Message. +- `method::String` +- `uri::String` - `version::VersionNumber` -- `status::Int16` - `headers::Vector{Pair{String,String}}` -- `body::`[`HTTP.Body`](@ref) -- `complete::Condition`, raised when the `Parser` has finished - reading the Response Headers. This allows the `status` and `header` fields - to be read used asynchronously without waiting for the entire body to be - parsed. - `complete` is also raised when the entire Response Body has been read. -- `exception`, set if `writeandread` fails. -- `parent::Request`, the `Request` that yielded this `Response`. +- `body::Vector{UInt8}` +- `response`, the `Response` to this `Request` +- `parent`, the `Response` (if any) that led to this request + (e.g. in the case of a redirect). """ -mutable struct Response +mutable struct Request <: Message + method::String + uri::String version::VersionNumber - status::Int16 - headers::Vector{Pair{String,String}} - body::Body - complete::Condition - exception + headers::Headers + body::Vector{UInt8} + response::Response parent end -Response(status::Int=0, headers=[]; body=Body(), parent=nothing) = - Response(v"1.1", status, headers, body, Condition(), nothing, parent) +Request() = Request("", "") -Response(bytes) = read!(IOBuffer(bytes), Response()) -Base.parse(::Type{Response}, str::AbstractString) = Response(str) +function Request(method::String, uri, headers=[], body=UInt8[]; parent=nothing) + r = Request(method, + uri == "" ? "/" : uri, + v"1.1", + mkheaders(headers), + body, + Response(), + parent) + r.response.request = r + return r +end +Request(bytes) = parse(Request, bytes) + +mkheaders(h::Headers) = h +mkheaders(h)::Headers = Header[string(k) => string(v) for (k,v) in h] -const Message = Union{Request,Response} """ iserror(::Response) @@ -113,15 +107,6 @@ Does this `Response` have a redirect status? isredirect(r::Response) = r.status in (301, 302, 307, 308) -""" - method(::Response) - -Method of the `Request` that yielded this `Response`. -""" - -method(r::Response) = r.parent == nothing ? "" : r.parent.method - - """ statustext(::Response) -> String @@ -132,44 +117,21 @@ statustext(r::Response) = Base.get(Parsers.STATUS_CODES, r.status, "Unknown Code """ - waitforheaders(::Response) - -Wait for the `Parser` (in a different task) to finish parsing the headers. -""" - -function waitforheaders(r::Response) - while r.status == 0 && r.exception == nothing - wait(r.complete) - end - if r.exception != nothing - rethrow(r.exception) - end -end - - -""" - wait(::Response) + header(::Message, key [, default=""]) -> String -Wait for the `Parser` (in a different task) to finish parsing the `Response`. +Get header value for `key` (case-insensitive). """ - -function Base.wait(r::Response) - while isopen(r.body) && r.exception == nothing - wait(r.complete) - end - if r.exception != nothing - rethrow(r.exception) - end -end +header(m, k, d="") = header(m.headers, k, d) +header(h::Headers, k::String, d::String="") = getbyfirst(h, k, k => d, lceq)[2] +lceq(a,b) = lowercase(a) == lowercase(b) """ - header(::Message, key [, default=""]) -> String + hasheader(::Message, key) -> Bool -Get header value for `key` (case-insensitive). +Does header value for `key` exist (case-insensitive)? """ -header(m, k::String, d::String="") = getbyfirst(m.headers, k, k => d, lceq)[2] -lceq(a,b) = lowercase(a) == lowercase(b) +hasheader(m, k::String) = header(m, k) != "" """ @@ -177,7 +139,8 @@ lceq(a,b) = lowercase(a) == lowercase(b) Set header `value` for `key` (case-insensitive). """ -setheader(m, v::Pair) = setbyfirst(m.headers, Pair{String,String}(v), lceq) +setheader(m, v) = setheader(m.headers, v) +setheader(h::Headers, v::Pair) = setbyfirst(h, Pair{String,String}(v), lceq) """ @@ -194,24 +157,13 @@ function defaultheader(m, v::Pair) end - """ - setlengthheader(::Response) + ischunked(::Message) -Set the Content-Length or Transfer-Encoding header according to the -`Response` `Body`. +Does the `Message` have a "Transfer-Encoding: chunked" header? """ -function setlengthheader(r::Request) - - l = length(r.body) - if l == Bodies.unknownlength - setheader(r, "Transfer-Encoding" => "chunked") - else - setheader(r, "Content-Length" => string(l)) - end - return -end +ischunked(m) = header(m, "Transfer-Encoding") == "chunked" """ @@ -225,7 +177,7 @@ If `key` is the same as the previous header, the `vale` is [appended to the value of the previous header with a comma delimiter](https://stackoverflow.com/a/24502264) -`Set-Cookie` headers are not comma-combined because cookies [often contain +`Set-Cookie` headers are not comma-combined because [cookies often contain internal commas](https://tools.ietf.org/html/rfc6265#section-3). """ @@ -272,11 +224,13 @@ end """ writeheaders(::IO, ::Message) -Write a line for each "name: value" pair and a trailing blank line. +Write `Message` start line and +a line for each "name: value" pair and a trailing blank line. """ function writeheaders(io::IO, m::Message) - for (name, value) in m.headers + writestartline(io, m) # FIXME To avoid fragmentation, maybe + for (name, value) in m.headers # buffer header before sending to `io` write(io, "$name: $value\r\n") end write(io, "\r\n") @@ -291,31 +245,32 @@ Write start line, headers and body of HTTP Message. """ function Base.write(io::IO, m::Message) - writestartline(io, m) # FIXME To avoid fragmentation, maybe - writeheaders(io, m) # buffer header before sending to `io` + writeheaders(io, m) write(io, m.body) return end +function Base.String(m::Message) + io = IOBuffer() + write(io, m) + String(take!(io)) +end + + """ - readstartline!(::Message, p::Parsers.Message) + readstartline!(::Parsers.Message, ::Message) Read the start-line metadata from Parser into a `::Message` struct. """ -function readstartline!(r::Response, m::Parsers.Message) +function readstartline!(m::Parsers.Message, r::Response) r.version = VersionNumber(m.major, m.minor) r.status = m.status - if isredirect(r) - r.body = Body() - end - notify(r.complete) - yield() return end -function readstartline!(r::Request, m::Parsers.Message) +function readstartline!(m::Parsers.Message, r::Request) r.version = VersionNumber(m.major, m.minor) r.method = string(m.method) r.uri = m.url @@ -323,78 +278,83 @@ function readstartline!(r::Request, m::Parsers.Message) end -""" - connectparser(::Message, ::Parser) +function readheaders(io::IO, parser::Parser, message::Message) -Configure a `Parser` to store parsed data into this `Message`. -""" -function connectparser(m::Message, p::Parser) - reset!(p) - p.onbodyfragment = x->write(m.body, x) - p.onheader = x->appendheader(m, x) - p.onheaderscomplete = x->readstartline!(m, x) - p.isheadresponse = (isa(m, Response) && method(m) in ("HEAD", "CONNECT")) - # FIXME CONNECT?? - return p + while !headerscomplete(parser) && !eof(io) + excess = parseheaders(parser, readavailable(io)) do h + appendheader(message, h) + end + unread!(io, excess) + end + if !headerscomplete(parser) + throw(EOFError()) + end + readstartline!(parser.message, message) + return message end -""" - read!(::IO, ::Message) +function readtrailers(io::IO, parser::Parser, message::Message) + if messagehastrailing(parser) + readheaders(io, parser, message) + end + return message +end -Read data from `io` into a `Message` struct. -""" -function Base.read!(io::IO, m::Message) - parser = ConnectionPool.getparser(io) - connectparser(m, parser) - read!(io, parser) - close(m.body) +function readbody(io::IO, parser::Parser) + body = IOBuffer() + while !bodycomplete(parser) && !eof(io) + data, excess = parsebody(parser, readavailable(io)) + write(body, data) + unread!(io, excess) + end + return take!(body) +end + + +function Base.parse(::Type{T}, str::AbstractString) where T <: Message + bytes = IOBuffer(str) + p = Parser() + m = T() + readheaders(bytes, p, m) + m.body = readbody(bytes, p) + readtrailers(bytes, p, m) + setoef(p) + if !messagecomplete(p) + throw(EOFError()) + end return m end """ - writeandread(::IO, ::Request, ::Response) + set_show_max(x) -Send a `Request` and receive a `Response`. +Set the maximum number of body bytes to be displayed by `show(::IO, ::Message)` """ -function writeandread(io::IO, req::Request, res::Response) - - try ;@debug 1 "write to: $io\n$req" - write(io, req) - closewrite(io) - read!(io, res) - closeread(io) ;@debug 2 "read from: $io\n$res" - catch e - @schedule close(io) - res.exception = e - rethrow(e) - finally - notify(res.complete) - end - - return res -end +set_show_max(x) = global body_show_max = x +body_show_max = 1000 -Base.take!(m::Message) = take!(m.body) - +""" + bodysummary(bytes) -function Base.String(m::Message) - io = IOBuffer() - write(io, m) - String(take!(io)) -end +The first chunk of the Message Body (for display purposes). +""" +bodysummary(bytes) = view(bytes, 1:min(length(bytes), body_show_max)) function Base.show(io::IO, m::Message) println(io, typeof(m), ":") println(io, "\"\"\"") - writestartline(io, m) writeheaders(io, m) - show(io, m.body) + summary = bodysummary(m.body) + write(io, summary) + if length(m.body) > length(summary) + println(io, "\n⋮\n$(length(m.body))-byte body") + end print(io, "\"\"\"") return end diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index 0df579bc9..ef054bd1b 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -4,6 +4,7 @@ import ..Layer, ..RequestStack.request using ..URIs using ..Messages using ..Pairs: setkv +using ..Parsers.Header using ..Strings.tocameldash! import ..@debug, ..DEBUG_LEVEL @@ -12,12 +13,12 @@ export RedirectLayer function request(::Type{RedirectLayer{Next}}, - method::String, uri, headers, body, response_body; + method::String, uri::URI, headers, body; maxredirects=3, forwardheaders=false, kw...) where Next count = 0 while true - res = request(Next, method, uri, headers, body, response_body; kw...) + res = request(Next, method, uri, headers, body; kw...) if (count == maxredirects || !isredirect(res) @@ -36,7 +37,7 @@ function request(::Type{RedirectLayer{Next}}, if forwardheaders headers = filter(h->!(h[1] in ("Host", "Cookie")), headers) else - headers = [] + headers = Header[] end count += 1 diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 16101c802..d7c7e1256 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -2,7 +2,9 @@ module RetryRequest import ..HTTP import ..Layer, ..RequestStack.request +using ..MessageRequest using ..Messages +import ..@debug, ..DEBUG_LEVEL abstract type RetryLayer{Next <: Layer} <: Layer end export RetryLayer @@ -16,19 +18,18 @@ isrecoverable(e::Base.ArgumentError) = e.msg == "stream is closed or unusable" isrecoverable(e::Exception) = false -isrecoverable(e, request_body, response_body) = isrecoverable(e) && - isstreamfresh(request_body) && - isstreamfresh(response_body) +isrecoverable(e, req) = isrecoverable(e) && + !(req.body === body_was_streamed) && + !(req.response.body === body_was_streamed) -function request(::Type{RetryLayer{Next}}, - method::String, uri, headers, body::Body, response_body::Body; + +function request(::Type{RetryLayer{Next}}, uri, req, body; retries=3, kw...) where Next - retry_request = retry(request, - delays=ExponentialBackOff(n = retries), - check=(s,ex)->(s,isrecoverable(ex, body, response_body))) + retry_request = retry(request, delays=ExponentialBackOff(n = retries), + check=(s,ex)->(s,isrecoverable(ex, req))) - retry_request(Next, method, uri, headers, body, response_body; kw...) + retry_request(Next, uri, req, body; kw...) end diff --git a/src/SocketRequest.jl b/src/SocketRequest.jl deleted file mode 100644 index cbc8671fc..000000000 --- a/src/SocketRequest.jl +++ /dev/null @@ -1,37 +0,0 @@ -module SocketRequest - -import ..Layer, ..RequestStack.request -using ..Messages -import ..@debug, ..DEBUG_LEVEL - -abstract type SocketLayer <: Layer end -export SocketLayer - - -""" - request(SocketLayer, ::IO, ::Request, ::Response) - -Send a `Request` and receive a `Response`. -Run the `Request` in a background task if response body is a stream. -""" - -function request(::Type{SocketLayer}, io::IO, req::Request, res::Response; kw...) - - if !isstream(res.body) - return writeandread(io, req, res) - end - - @schedule try - writeandread(io, req, res) - catch e - if res.exception != e - rethrow(e) - end - @debug 1 "Async HTTP Message Exception!\n$e\n$io\n$req\n$res" - end - waitforheaders(res) - return res -end - - -end # module SendRequest diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl new file mode 100644 index 000000000..684032a59 --- /dev/null +++ b/src/StreamRequest.jl @@ -0,0 +1,66 @@ +module StreamRequest + +import ..Layer, ..RequestStack.request +using ..IOExtras +using ..Parsers +using ..Messages +using ..HTTPStreams +using ..ConnectionPool.getparser +using ..MessageRequest +import ..@debug, ..DEBUG_LEVEL + +abstract type StreamLayer <: Layer end +export StreamLayer + + +writebody(http, req, body) = for chunk in body write(http, req, chunk) end + +function writebody(http, req, body::IO) + req.body = body_was_streamed + write(http, body) +end + + +""" + request(StreamLayer, ::IO, ::Request, ::Response) + +Send a `Request` and receive a `Response`. +Run the `Request` in a background task if response body is a stream. +""" + +function request(::Type{StreamLayer}, io::IO, req::Request, body; + response_stream=nothing, + iofunction=nothing, + kw...)::Response + + write(io, req) + + @debug 1 req + + http = HTTPStream(io, req, getparser(io)) + + if iofunction != nothing + iofunction(http) + else + if req.body === body_is_a_stream + writebody(http, req, body) + end + + readheaders(http) + if response_stream == nothing + http.message.body = read(http) + else + http.message.body = body_was_streamed + write(response_stream, http) + end + end + + close(http) + + @debug 1 http.message + + return http.message +end + + +end # module StreamRequest diff --git a/src/compat.jl b/src/compat.jl new file mode 100644 index 000000000..e9c94049c --- /dev/null +++ b/src/compat.jl @@ -0,0 +1,24 @@ +if VERSION > v"0.7.0-DEV.2338" + using Base64 +end + +@static if VERSION >= v"0.7.0-DEV.2915" + 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 + import Dates +end diff --git a/src/consts.jl b/src/consts.jl index 0220e3a40..14a9b880c 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -155,32 +155,12 @@ Base.convert(::Type{Method}, s::String) = MethodMap[s] # parsing codes @enum(ParsingErrorCode, - # No error HPE_OK, - # Callback-related errors - HPE_CB_message_begin, - HPE_CB_url, - HPE_CB_header_field, - HPE_CB_header_value, - HPE_CB_headers_complete, - HPE_CB_body, - HPE_CB_message_complete, - HPE_CB_status, - HPE_CB_chunk_header, - HPE_CB_chunk_complete, - # Parsing-related errors - HPE_INVALID_EOF_STATE, HPE_HEADER_OVERFLOW, - HPE_CLOSED_CONNECTION, HPE_INVALID_VERSION, HPE_INVALID_STATUS, HPE_INVALID_METHOD, HPE_INVALID_URL, - HPE_INVALID_HOST, - HPE_INVALID_PORT, - HPE_INVALID_PATH, - HPE_INVALID_QUERY_STRING, - HPE_INVALID_FRAGMENT, HPE_LF_EXPECTED, HPE_INVALID_HEADER_TOKEN, HPE_INVALID_CONTENT_LENGTH, @@ -189,38 +169,16 @@ Base.convert(::Type{Method}, s::String) = MethodMap[s] HPE_INVALID_CONSTANT, HPE_INVALID_INTERNAL_STATE, HPE_STRICT, - HPE_PAUSED, - HPE_URI_OVERFLOW, - HPE_BODY_OVERFLOW, - HPE_HEADERS_INCOMPLETE, - HPE_BODY_INCOMPLETE, HPE_UNKNOWN, ) const ParsingErrorCodeMap = Dict( HPE_OK => "success", - HPE_CB_message_begin => "the on_message_begin callback failed", - HPE_CB_url => "the on_url callback failed", - HPE_CB_header_field => "the on_header_field callback failed", - HPE_CB_header_value => "the on_header_value callback failed", - HPE_CB_headers_complete => "the on_headers_complete callback failed", - HPE_CB_body => "the on_body callback failed", - HPE_CB_message_complete => "the on_message_complete callback failed", - HPE_CB_status => "the on_status callback failed", - HPE_CB_chunk_header => "the on_chunk_header callback failed", - HPE_CB_chunk_complete => "the on_chunk_complete callback failed", - HPE_INVALID_EOF_STATE => "stream ended at an unexpected time", HPE_HEADER_OVERFLOW => "too many header bytes seen; overflow detected", - HPE_CLOSED_CONNECTION => "data received after completed connection: close message", HPE_INVALID_VERSION => "invalid HTTP version", HPE_INVALID_STATUS => "invalid HTTP status code", HPE_INVALID_METHOD => "invalid HTTP method", HPE_INVALID_URL => "invalid URL", - HPE_INVALID_HOST => "invalid host", - HPE_INVALID_PORT => "invalid port", - HPE_INVALID_PATH => "invalid path", - HPE_INVALID_QUERY_STRING => "invalid query string", - HPE_INVALID_FRAGMENT => "invalid fragment", HPE_LF_EXPECTED => "LF character expected", HPE_INVALID_HEADER_TOKEN => "invalid character in header", HPE_INVALID_CONTENT_LENGTH => "invalid character in content-length header", @@ -229,11 +187,6 @@ const ParsingErrorCodeMap = Dict( HPE_INVALID_CONSTANT => "invalid constant string", HPE_INVALID_INTERNAL_STATE => "encountered unexpected internal state", HPE_STRICT => "strict mode assertion failed", - HPE_PAUSED => "parser is paused", - HPE_URI_OVERFLOW => "uri exceeded configured maximum uri size", - HPE_BODY_OVERFLOW => "body exceeded configured maximum body size", - HPE_HEADERS_INCOMPLETE => "unexpected end of headers", - HPE_BODY_INCOMPLETE => "unexpected end of body", HPE_UNKNOWN => "an unknown error occurred", ) @@ -414,8 +367,7 @@ const F_CONNECTION_CLOSE = UInt8(1 << 2) const F_CONNECTION_UPGRADE = UInt8(1 << 3) const F_TRAILING = UInt8(1 << 4) const F_UPGRADE = UInt8(1 << 5) -const F_SKIPBODY = UInt8(1 << 6) -const F_CONTENTLENGTH = UInt8(1 << 7) +const F_CONTENTLENGTH = UInt8(1 << 6) # url parsing const normal_url_char = Bool[ diff --git a/src/parser.jl b/src/parser.jl index d2f830542..31e718510 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -24,13 +24,15 @@ module Parsers -export Parser, parse!, parseheaders!, parsebody!, reset!, - readheaders!, readbody!, - messagestarted, messagecomplete, headerscomplete, waitingforeof, - connectionclosed, +export Parser, Header, Headers, ByteView, nobytes, + reset!, + parseheaders, parsebody, + messagestarted, headerscomplete, bodycomplete, messagecomplete, + messagehastrailing, + waitingforeof, seteof, + connectionclosed, setheadresponse, ParsingError, ParsingErrorCode -using ..IOExtras using ..URIs.parseurlchar import MbedTLS.SSLContext @@ -45,17 +47,11 @@ const strict = false # See macro @errifstrict const enable_passert = false # See macro @passert -""" - Message - -HTTP Message metadata. -- `method::Method` -- `major::Int16` -- `minor::Int16` -- `url::String` -- `status::Int32` -- `upgrade::Bool` -""" +const nobytes = view(UInt8[], 1:0) +const ByteView = typeof(nobytes) +const Header = Pair{String,String} +const Headers = Vector{Header} + mutable struct Message method::Method @@ -69,52 +65,10 @@ end Message() = Message(NOMETHOD, 0, 0, "", 0, false) -""" - Parser - -HTTP Message Parser. - -The `Parser` must be configured with output processing callbacks: - -- `onheader = f(::Pair{String,String})` is called for each Header Line. - -- Body data is passed to `onbodyfragment = f(::SubArray{UInt8,1})`. - If the Message is chunked or if the Message is passed to `parse!` - in multiple fragments, then `onbodyfragment` will be called multiple times. - -- `onheaderscomplete = f(::Message)` is called at the end of the Header. - -Message data can be passed to the `parse!(::Parser, data)` function -or read from a stream by `read!(::IO, ::Parser)`. - -e.g. - -``` -p = Parser() -p.onheaderscomplete = m -> (@show string(m.method); @show m.url) -p.onheader = h -> @show h - -parse!(p, \"\"\" -GET /foo HTTP/1.1 -Content-Length: 0 -Foo: Bar - -\"\"\") - -h = "Content-Length"=>"0" -h = "Foo"=>"Bar" -string(m.method) = "GET" -m.url = "/foo" -``` -""" - mutable struct Parser # config isheadresponse::Bool # Are we parsing a HEAD Response Message? - onheader::Function#(::Pair{String,String} - onbodyfragment::Function#(::SubArray{UInt8,1}) - onheaderscomplete::Function#(::Message) # state state::UInt8 @@ -136,120 +90,11 @@ end Create an unconfigured `Parser`. """ -Parser() = Parser(false, x->nothing, x->nothing, x->nothing, +Parser() = Parser(false, s_start_req_or_res, 0, 0, 0, 0, IOBuffer(), IOBuffer(), Message()) -""" - read!(io, ::Parser [, unread=IOExtras.unread!]) - -Read data from `io` into the `Parser` until `eof` -or until the parser finds the end of the message. - -If `readavailable(io)` reads past the end of the Message the excess bytes -are passed to `unread`. This is handled transparently if there is a suitable -`IOExtras.unread!(::IO, SubArray{UInt8, 1})` method defined. - -Throws `ParsingError` if input is invalid. -""" - -function Base.read!(io::IO, p::Parser; unread=IOExtras.unread!) - - while !eof(io) - bytes = readavailable(io) - n = parse!(p, bytes) - if n < length(bytes) - unread(io, view(bytes, n+1:length(bytes))) - end - if messagecomplete(p) - return - end - end - @debug 2 "read!(::$(typeof(io)), Parser($(ParsingStateCode(p.state)))) eof!" - - if !messagestarted(p) - throw(EOFError()) - end - if !waitingforeof(p) - throw(ParsingError(p, headerscomplete(p) ? HPE_BODY_INCOMPLETE : - HPE_HEADERS_INCOMPLETE)) - end - return -end - - -""" - readheaders!(io, ::Parser [, unread=IOExtras.unread!]) - -Read data from `io` into the `Parser` until `eof` -or until the parser finds the end of the Headers. - -If `readavailable(io)` reads past the end of the Headers the excess bytes -are passed to `unread`. - -Throws `ParsingError` if input is invalid. -""" - -function readheaders!(io::IO, p::Parser; unread=IOExtras.unread!) - - while !eof(io) - bytes = readavailable(io) - n = parse!(p, bytes) - if n < length(bytes) - unread(io, view(bytes, n+1:length(bytes))) - end - if headerscomplete(p) - return - end - end - @debug 2 "readheaders!(::$(typeof(io)), " * - "Parser($(ParsingStateCode(p.state)))) eof!" - - if !messagestarted(p) - throw(EOFError()) - end - if !waitingforeof(p) - throw(ParsingError(p, HPE_HEADERS_INCOMPLETE)) - end - return -end - - -""" - readbody!(io, ::Parser [, unread=IOExtras.unread!]) - -Read data from `io` into the `Parser` until `eof` -or until the parser finds the end of the Message Body. - -If `readavailable(io)` reads past the end of the Message the excess bytes -are passed to `unread`. - -Throws `ParsingError` if input is invalid. -""" - -function readbody!(io::IO, p::Parser; unread=IOExtras.unread!) - - while !eof(io) - bytes = readavailable(io) - n = parsebody!(p, bytes) - if n < length(bytes) - unread(io, view(bytes, n+1:length(bytes))) - end - if messagecomplete(p) - return - end - end - @debug 2 "readbody!(::$(typeof(io)), " * - "Parser($(ParsingStateCode(p.state)))) eof!" - - if !waitingforeof(p) - throw(ParsingError(p, HPE_BODY_INCOMPLETE)) - end - return -end - - """ reset!(::Parser) @@ -260,9 +105,6 @@ function reset!(p::Parser) # config p.isheadresponse = false - p.onheader = x->nothing - p.onbodyfragment = x->nothing - p.onheaderscomplete = x->nothing # state p.state = s_start_req_or_res @@ -283,6 +125,15 @@ function reset!(p::Parser) end +""" + setheadresponse(::Parser) + +Mark the Message as being the Response to a HEAD Request. +""" + +setheadresponse(p::Parser) = p.isheadresponse = true + + """ messagestarted(::Parser) @@ -301,6 +152,16 @@ Has the `Parser` processed the entire Message Header? headerscomplete(p::Parser) = p.state > s_headers_done +""" + bodycomplete(::Parser) + +Has the `Parser` processed the Message Body? +""" + +bodycomplete(p::Parser) = p.state == s_message_done || + p.state == s_trailer_start + + """ messagecomplete(::Parser) @@ -319,6 +180,26 @@ to signal the end of the Message Body? waitingforeof(p::Parser) = p.state == s_body_identity_eof +""" + seteof(::Parser) + +Signal that the peer has closed the connection. +""" +function seteof(p::Parser) + if p.state == s_body_identity_eof + p.state = s_message_done + end +end + + +""" + messagehastrailing(::Parser) + +Is the `Parser` ready to process trailing headers? +""" +messagehastrailing(p::Parser) = p.flags & F_TRAILING > 0 + + """ connectionclosed(::Parser) @@ -374,55 +255,27 @@ end """ - parse!(::Parser, bytes) -> count + parseheaders(f(::Pair{String,String}), ::Parser, bytes) -> excess -Parse `bytes` and update the `Parser`. - -Returns number of bytes consumed. -If `bytes` contains the end of one Message and the start of the next -Message, `parse!` will stop at the end of the first Message. - -Throws `ParsingError` if input is invalid. +Read headers from `bytes`, passing each field/value pair to `f`. +Returns a `SubArray` containing bytes not parsed. """ -parse!(p::Parser, bytes::String)::Int = parse!(p, Vector{UInt8}(bytes)) - -parse!(p::Parser, bytes)::Int = parse!(p, view(bytes, 1:length(bytes))) - -const ByteView = typeof(view(UInt8[], 1:0)) - -function parse!(parser::Parser, bytes::ByteView)::Int - - l = length(bytes) - c = 0 - while c < l - if !headerscomplete(parser) - n = parseheaders!(parser, bytes) - else - n = parsebody!(parser, bytes) - end - c += n - if messagecomplete(parser) - break - end - if c < l - bytes = view(bytes, n+1:length(bytes)) - end - end - return c +function parseheaders(f, p, bytes) + v = Vector{UInt8}(bytes) + parseheaders(f, p, view(v, 1:length(v))) end - -parseheaders!(p::Parser, bytes) = parseheaders!(p, view(bytes, 1:length(bytes))) - -function parseheaders!(parser::Parser, bytes::ByteView)::Int +function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, + parser::Parser, bytes::ByteView)::ByteView isempty(bytes) && throw(ArgumentError("bytes must not be empty")) - headerscomplete(parser) && throw(ArgumentError("headers already complete")) + !messagehastrailing(parser) && + headerscomplete(parser) && (ArgumentError("headers already complete")) len = length(bytes) p_state = parser.state - @debug 2 "parseheaders!(parser.state=$(ParsingStateCode(p_state))), " * + @debug 2 "parseheaders(parser.state=$(ParsingStateCode(p_state))), " * "$len-bytes:\n" * escapelines(String(collect(bytes))) * ")" p = 0 @@ -1110,8 +963,8 @@ function parseheaders!(parser::Parser, bytes::ByteView)::Int write(parser.valuebuffer, view(bytes, start:p-1)) if p_state != s_header_value - parser.onheader(String(take!(parser.fieldbuffer)) => - String(take!(parser.valuebuffer))) + onheader(String(take!(parser.fieldbuffer)) => + String(take!(parser.valuebuffer))) end p = min(p, len) @@ -1158,7 +1011,7 @@ function parseheaders!(parser::Parser, bytes::ByteView)::Int # header value was empty p_state = s_header_field_start - parser.onheader(String(take!(parser.fieldbuffer)) => "") + onheader(String(take!(parser.fieldbuffer)) => "") p -= 1 end @@ -1193,10 +1046,6 @@ function parseheaders!(parser::Parser, bytes::ByteView)::Int elseif p_state == s_headers_done @errorifstrict(ch != LF) - @debug 3 "headersdone" - parser.state = p_state - parser.onheaderscomplete(parser.message) - if parser.isheadresponse || parser.content_length == 0 || (parser.message.upgrade && isrequest(parser) && @@ -1231,27 +1080,40 @@ function parseheaders!(parser::Parser, bytes::ByteView)::Int p_state == s_body_identity || p_state == s_body_identity_eof - @debug 3 "parseheaders!() exiting $(ParsingStateCode(p_state))" + @debug 2 "parseheaders() exiting $(ParsingStateCode(p_state))" parser.state = p_state - return p + return view(bytes, p+1:len) end -parsebody!(p::Parser, bytes) = parsebody!(p, view(bytes, 1:length(bytes))) +""" + parsebody(::Parser, bytes) -> data, excess + +Parse body data from `bytes`. +Returns decoded `data` and `excess` bytes not parsed. +""" + +function parsebody(p, bytes) + v = Vector{UInt8}(bytes) + parsebody(p, view(v, 1:length(v))) +end -function parsebody!(parser::Parser, bytes::ByteView)::Int +function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} isempty(bytes) && throw(ArgumentError("bytes must not be empty")) !headerscomplete(parser) && throw(ArgumentError("headers not complete")) len = length(bytes) p_state = parser.state - @debug 2 "parsebody!(parser.state=$(ParsingStateCode(p_state))), " * + @debug 2 "parsebody(parser.state=$(ParsingStateCode(p_state))), " * "$len-bytes:\n" * escapelines(String(collect(bytes))) * ")" + result = nobytes + p = 0 - while p < len && p_state < s_message_done && p_state != s_trailer_start + while p < len && result == nobytes && p_state < s_message_done && + p_state != s_trailer_start @debug 3 string("top of while($p < $len) \"", Base.escape_string(string(Char(bytes[p+1]))), "\" ", @@ -1264,12 +1126,8 @@ function parsebody!(parser::Parser, bytes::ByteView)::Int @passert parser.content_length != 0 && parser.content_length != ULLONG_MAX - parser.onbodyfragment(view(bytes, p:p + to_read - 1)) - - # The difference between advancing content_length and p is because - # the latter will automaticaly advance on the next loop iteration. - # Further, if content_length ends up at 0, we want to see the last - # byte again for our message complete callback. + @passert result == nobytes + result = view(bytes, p:p + to_read - 1) parser.content_length -= to_read p += to_read - 1 @@ -1279,7 +1137,8 @@ function parsebody!(parser::Parser, bytes::ByteView)::Int # read until EOF elseif p_state == s_body_identity_eof - parser.onbodyfragment(view(bytes, p:len)) + @passert result == nobytes + result = bytes p = len elseif p_state == s_chunk_size_start @@ -1342,12 +1201,10 @@ function parsebody!(parser::Parser, bytes::ByteView)::Int @passert parser.content_length != 0 && parser.content_length != ULLONG_MAX - parser.onbodyfragment(view(bytes, p:p + to_read - 1)) - - # See the explanation in s_body_identity for why the content - # length and data pointers are managed this way. + @passert result == nobytes + result = view(bytes, p:p + to_read - 1) parser.content_length -= to_read - p += Int(to_read) - 1 + p += to_read - 1 if parser.content_length == 0 p_state = s_chunk_data_almost_done @@ -1382,14 +1239,27 @@ function parsebody!(parser::Parser, bytes::ByteView)::Int @assert p <= len @assert p == len || + result != nobytes || p_state == s_message_done || p_state == s_trailer_start - @debug 3 "parsebody!() exiting $(ParsingStateCode(p_state))" + @debug 2 "parsebody() exiting $(ParsingStateCode(p_state))" parser.state = p_state - return p + return result, view(bytes, p+1:len) end +Base.show(io::IO, p::Parser) = print(io, "Parser(", + "state=", ParsingStateCode(p.state),", ", + p.flags & F_CHUNKED > 0 ? "F_CHUNKED, " : "", + p.flags & F_CONNECTION_KEEP_ALIVE > 0 ? "F_CONNECTION_KEEP_ALIVE, " : "", + p.flags & F_CONNECTION_CLOSE > 0 ? "F_CONNECTION_CLOSE, " : "", + p.flags & F_CONNECTION_UPGRADE > 0 ? "F_CONNECTION_UPGRADE, " : "", + p.flags & F_TRAILING > 0 ? "F_TRAILING, " : "", + p.flags & F_UPGRADE > 0 ? "F_UPGRADE, " : "", + p.flags & F_CONTENTLENGTH > 0 ? "F_CONTENTLENGTH, " : "", + "content_length=", p.content_length, ", ", + "message=", p.message, ")") + end # module Parsers diff --git a/test/async.jl b/test/async.jl index bcd58ea1a..c900a1db5 100644 --- a/test/async.jl +++ b/test/async.jl @@ -1,97 +1,152 @@ -@testset "HTTP.async" begin - using JSON +using HTTP.IOExtras + +configs = [ + [], + [:reuse_limit => 200], + [:reuse_limit => 100], + [:reuse_limit => 10] +] + +@testset "async $count, $num, $config, $http" for count in 1:3, + num in [10, 100, 1000, 2000], + config in configs, + http in ["http", "https"] + +println("running async $count, 1:$num, $config, $http") -for http in ("http", "https") - println("running $http async tests...") + result = [] @sync begin - for i = 1:100 + for i = 1:min(num,100) @async begin - r = HTTP.RequestStack.request("GET", "$http://httpbin.org/headers", ["i" => i]) - r = JSON.parse(String(take!(r))) - @test r["headers"]["I"] == string(i) + r = HTTP.RequestStack.request("GET", + "$http://httpbin.org/headers", ["i" => i]; config...) + r = JSON.parse(String(r.body)) + push!(result, r["headers"]["I"] => string(i)) end end end + for (a,b) in result + @test a == b + end HTTP.ConnectionPool.showpool(STDOUT) HTTP.ConnectionPool.closeall() - + + result = [] @sync begin - for i = 1:100 + for i = 1:min(num,100) @async begin - r = HTTP.RequestStack.request("GET", "$http://httpbin.org/stream/$i") - r = String(take!(r)) + r = HTTP.RequestStack.request("GET", + "$http://httpbin.org/stream/$i"; config...) + r = String(r.body) r = split(strip(r), "\n") - @test length(r) == i + push!(result, length(r) => i) end end end + for (a,b) in result + @test a == b + end + HTTP.ConnectionPool.showpool(STDOUT) HTTP.ConnectionPool.closeall() + result = [] + asyncmap(i->begin n = i % 20 + 1 - for attempt in 1:3 - r = nothing - try - println("GET $i $n") - s = BufferStream() - r = HTTP.RequestStack.request("GET", "$http://httpbin.org/stream/$n"; - retries=5, response_stream=s) - wait(r) - r = String(read(s)) - break - catch e - if attempt == 3 || !HTTP.RetryRequest.isrecoverable(e) - rethrow(e) - end - end + str = "" + r = HTTP.open("GET", "$http://httpbin.org/stream/$n"; + retries=5, config...) do s + str = String(read(s)) end - - r = split(strip(r), "\n") - println("GOT $i $n") - @test length(r) == n - - end, 1:1000, ntasks=20) + l = split(strip(str), "\n") + #println("GOT $i $n") + + push!(result, length(l) => n) + end, 1:num, ntasks=20) + + for (a,b) in result + @test a == b + end + + result = [] @sync begin - for i = 1:1000 - @async begin - n = i % 20 + 1 - for attempt in 1:3 - r = nothing - try - s = BufferStream() - println("GET $i $n") - r = HTTP.RequestStack.request("GET", "$http://httpbin.org/stream/$n"; - response_stream=s) - wait(r) - r = String(read(s)) - break - catch e - if attempt == 3 || !HTTP.RetryRequest.isrecoverable(e) - rethrow(e) + for i = 1:num + n = i % 20 + 1 + @async begin try + r = nothing + str = nothing + url = "$http://httpbin.org/stream/$n" + if rand(Bool) + if rand(Bool) + for attempt in 1:4 + try + #println("GET $i $n BufferStream $attempt") + s = BufferStream() + r = HTTP.RequestStack.request( + "GET", url; response_stream=s, config...) + @assert r.status == 200 + close(s) + str = String(read(s)) + break + catch e +# st = catch_stacktrace() + if attempt == 10 || + !HTTP.RetryRequest.isrecoverable(e) + rethrow(e) + end + buf = IOBuffer() + println(buf, "$i retry $e $attempt...") + #show(buf, "text/plain", st) + write(STDOUT, take!(buf)) + sleep(0.1) + end end + else + #println("GET $i $n Plain") + r = HTTP.RequestStack.request("GET", url; config...) + @assert r.status == 200 + str = String(r.body) + end + else + #println("GET $i $n open()") + r = HTTP.open("GET", url; config...) do http + str = String(read(http)) end + @assert r.status == 200 end - - r = split(strip(r), "\n") - println("GOT $i $n") - @test length(r) == n - end + + l = split(strip(str), "\n") + #println("GOT $i $n $(length(l))") + if length(l) != n + @show r + @show str + end + push!(result, length(l) => n) + catch e + push!(result, e => n) + buf = IOBuffer() + write(buf, "==========\nAsync exception:\n==========\n$e\n") + show(buf, "text/plain", catch_stacktrace()) + write(buf, "==========\n\n") + write(STDOUT, take!(buf)) + end end end end + for (a,b) in result + @test a == b + end HTTP.ConnectionPool.showpool(STDOUT) HTTP.ConnectionPool.closeall() -end - -end # @testset "HTTP.Client" +end # testset diff --git a/test/client.jl b/test/client.jl index 19e34fbd7..af59b1b48 100644 --- a/test/client.jl +++ b/test/client.jl @@ -31,11 +31,11 @@ for sch in ("http", "https") empty!(HTTP.CookieRequest.default_cookiejar) empty!(HTTP.DEFAULT_CLIENT.cookies) r = HTTP.get("$sch://httpbin.org/cookies", cookies=true) - body = String(take!(r)) + body = String(r.body) @test body == "{\n \"cookies\": {}\n}\n" r = HTTP.get("$sch://httpbin.org/cookies/set?hey=sailor&foo=bar", cookies=true) @test HTTP.status(r) == 200 - body = String(take!(r)) + body = String(r.body) @test body == "{\n \"cookies\": {\n \"foo\": \"bar\", \n \"hey\": \"sailor\"\n }\n}\n" # r = HTTP.get("$sch://httpbin.org/cookies/delete?hey") @@ -50,14 +50,16 @@ for sch in ("http", "https") @test HTTP.status(r) == 200 r = HTTP.get("$sch://httpbin.org/stream/100") @test HTTP.status(r) == 200 - bytes = take!(r) + bytes = r.body a = [JSON.parse(l) for l in split(chomp(String(bytes)), "\n")] totallen = length(bytes) # number of bytes to expect begin - r = HTTP.get("$sch://httpbin.org/stream/100"; stream=true) + io = BufferStream() + r = HTTP.get("$sch://httpbin.org/stream/100"; response_stream=io) + close(io) @test HTTP.status(r) == 200 - b = [JSON.parse(l) for l in eachline(r.body.stream)] + b = [JSON.parse(l) for l in eachline(io)] @test a == b end @@ -98,11 +100,11 @@ for sch in ("http", "https") println("client multipart body") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there")) @test HTTP.status(r) == 200 - @test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }") + @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there")) @test HTTP.status(r) == 200 - @test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }") + @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }") tmp = tempname() open(f->write(f, "hey"), tmp, "w") @@ -110,7 +112,7 @@ for sch in ("http", "https") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "iostream"=>io)) close(io); rm(tmp) @test HTTP.status(r) == 200 - str = String(take!(r)) + str = String(r.body) @test startswith(str, "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") tmp = tempname() @@ -119,7 +121,7 @@ for sch in ("http", "https") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "iostream"=>io)) close(io); rm(tmp) @test HTTP.status(r) == 200 - @test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") + @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") tmp = tempname() open(f->write(f, "hey"), tmp, "w") @@ -128,7 +130,7 @@ for sch in ("http", "https") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "multi"=>m)) close(io); rm(tmp) @test HTTP.status(r) == 200 - @test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") + @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") tmp = tempname() open(f->write(f, "hey"), tmp, "w") @@ -137,7 +139,7 @@ for sch in ("http", "https") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "multi"=>m), chunksize=1000) close(io); rm(tmp) @test HTTP.status(r) == 200 - @test startswith(String(take!(r)), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") + @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") # asynchronous println("asynchronous client request body") diff --git a/test/messages.jl b/test/messages.jl index a42ef3c42..d335ab6fa 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -17,10 +17,10 @@ using JSON @testset "HTTP.Messages" begin req = Request("GET", "/foo", ["Foo" => "Bar"]) - res = Response(200, ["Content-Length" => "5"]; body=Body("Hello"), parent=req) + res = Response(200, ["Content-Length" => "5"]; body="Hello", request=req) @test req.method == "GET" - @test method(res) == "GET" + @test res.request.method == "GET" #display(req); println() #display(res); println() @@ -49,24 +49,20 @@ using JSON raw = String(req) #@show raw - req = Request() - read!(IOBuffer(raw), req) + req = Request(raw) #display(req); println() @test String(req) == raw - req = Request() - read!(IOBuffer(raw * "xxx"), req) + req = Request(raw * "xxx") @test String(req) == raw raw = String(res) #@show raw - res = Response() - read!(IOBuffer(raw), res) + res = Response(raw) #display(res); println() @test String(res) == raw - res = Response() - read!(IOBuffer(raw * "xxx"), res) + res = Response(raw * "xxx") @test String(res) == raw for sch in ["http", "https"] @@ -104,25 +100,27 @@ using JSON uri = "$sch://httpbin.org/$(lowercase(m))" r = request(m, uri) @test r.status == 200 - body = take!(r.body) + body = r.body io = BufferStream() r = request(m, uri, response_stream=io) + close(io) @test r.status == 200 @test read(io) == body end end + for sch in ["http", "https"] for m in ["POST", "PUT", "DELETE", "PATCH"] uri = "$sch://httpbin.org/$(lowercase(m))" io = BufferStream() r = request(m, uri, response_stream=io) + close(io) @test r.status == 200 end end - mktempdir() do d cd(d) do @@ -130,11 +128,8 @@ using JSON io = open("result_file", "w") r = request("GET", "http://httpbin.org/stream/$n", response_stream=io) - @test stat("result_file").size == 0 - while stat("result_file").size <= 1000 - sleep(0.1) - end - @test stat("result_file").size > 1000 + close(io) + @show filesize("result_file") i = 0 for l in readlines("result_file") x = JSON.parse(l) diff --git a/test/parser.jl b/test/parser.jl index f1605e656..fee1404a5 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -17,7 +17,32 @@ const Headers = Vector{Pair{String,String}} ==(a::Request,b::Request) = (a.method == b.method) && (a.version == b.version) && (a.headers == b.headers) && - (HTTP.Messages.Bodies.collect!(a.body) == HTTP.Messages.Bodies.collect!(b.body)) + (a.body == b.body) + + +function parse!(parser::Parser, message::Messages.Message, body, bytes)::Int + + l = length(bytes) + count = 0 + while count < l + if !headerscomplete(parser) + excess = parseheaders(parser, bytes) do h + appendheader(message, h) + end + readstartline!(parser.message, message) + else + fragment, excess = parsebody(parser, bytes) + write(body, fragment) + end + count += length(bytes) - length(excess) + bytes = excess + if messagecomplete(parser) + break + end + end + return count +end + mutable struct Message name::String @@ -1389,30 +1414,21 @@ const responses = Message[ println("TEST - parser.jl - Request $t: $(req.name)") upgrade = Ref{SubArray{UInt8, 1}}() + r = Request() + p = Parser() + b = IOBuffer() + bytes = Vector{UInt8}(req.raw) + sz = t if t > 0 - @sync begin - r = Request() - p = Messages.connectparser(r, Parser()) - bytes = Vector{UInt8}(req.raw) - sz = t - for i in 1:sz:length(bytes) - parse!(p, view(bytes, i:min(i+sz-1, length(bytes)))) - end + for i in 1:sz:length(bytes) + parse!(p, r, b, view(bytes, i:min(i+sz-1, length(bytes)))) end + r.body = take!(b) elseif t < 0 - @sync begin - r = Request() - io = BufferStream() - bytes = Vector{UInt8}(req.raw) - sz = t - @async begin - i = rand(2:length(bytes)) - write(io, bytes[1:i-1]) - yield() - write(io, bytes[i:end]) - end - read!(io, r) - end + i = rand(2:length(bytes)) + parse!(p, r, b, bytes[1:i-1]) + parse!(p, r, b, bytes[i:end]) + r.body = take!(b) else r = Request(req.raw) #r = HTTP.parse(HTTP.Request, req.raw; extraref=upgrade) @@ -1430,7 +1446,7 @@ const responses = Message[ @test string(uri) == req.request_url @test length(r.headers) == req.num_headers @test Dict(HTTP.CanonicalizeRequest.canonicalizeheaders(r.headers)) == Dict(req.headers) - @test String(take!(r.body)) == req.body + @test String(r.body) == req.body # FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == req.should_keep_alive if isassigned(upgrade) @@ -1454,7 +1470,7 @@ const responses = Message[ req = Request("GET", "http://www.techcrunch.com/") req.headers = ["Host"=>"www.techcrunch.com","User-Agent"=>"Fake","Accept"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language"=>"en-us,en;q=0.5","Accept-Encoding"=>"gzip,deflate","Accept-Charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7","Keep-Alive"=>"300","Content-Length"=>"7","Proxy-Connection"=>"keep-alive"] - req.body = Body("1234567") + req.body = Vector{UInt8}("1234567") @test Request(reqstr).headers == req.headers @test Request(reqstr) == req @@ -1498,7 +1514,7 @@ const responses = Message[ req.method = "POST" req.uri = "/" req.headers = ["Host"=>"foo.com", "Transfer-Encoding"=>"chunked", "Trailer-Key"=>"Trailer-Value"] - req.body = Body("foobar") + req.body = Vector{UInt8}("foobar") @test Request(reqstr) == req @@ -1591,7 +1607,7 @@ const responses = Message[ "Accept-Language"=>"en-us", "Accept-Encoding"=>"gzip, deflate", "Connection"=>"Keep-Alive"] - req.body = Body("first=Zara&last=Ali") + req.body = Vector{UInt8}("first=Zara&last=Ali") @test Request(reqstr) == req end @@ -1602,12 +1618,14 @@ const responses = Message[ try if t > 0 r = Response() - p = Messages.connectparser(r, Parser()) + p = Parser() + b = IOBuffer() bytes = Vector{UInt8}(resp.raw) sz = t for i in 1:sz:length(bytes) - parse!(p, view(bytes, i:min(i+sz-1, length(bytes)))) + parse!(p, r, b, view(bytes, i:min(i+sz-1, length(bytes)))) end + r.body = take!(b) else r = Response(resp.raw) end @@ -1617,7 +1635,7 @@ const responses = Message[ @test HTTP.Messages.statustext(r) == resp.response_status @test length(r.headers) == resp.num_headers @test Dict(HTTP.CanonicalizeRequest.canonicalizeheaders(r.headers)) == Dict(resp.headers) - @test String(take!(r.body)) == resp.body + @test String(r.body) == resp.body # FIXME @test HTTP.http_should_keep_alive(HTTP.DEFAULT_PARSER) == resp.should_keep_alive catch e if HTTP.Parsers.strict && isa(e, ParsingError) @@ -1697,21 +1715,23 @@ const responses = Message[ for r in ((Request, "GET / HTTP/1.1\r\n"), (Response, "HTTP/1.0 200 OK\r\n")) - HTTP.Parsers.reset!(DEFAULT_PARSER) R = r[1]() - n = parse!(DEFAULT_PARSER, Vector{UInt8}(r[2])) - @test !headerscomplete(DEFAULT_PARSER) - @test !messagecomplete(DEFAULT_PARSER) + b = IOBuffer() + p = Parser() + n = parse!(Parser(), R, b, r[2]) + @test !headerscomplete(p) + @test !messagecomplete(p) @test n == length(Vector{UInt8}(r[2])) end buf = "GET / HTTP/1.1\r\nheader: value\nhdr: value\r\n" - @test_throws ParsingError r = Request(buf) + @test_throws EOFError r = Request(buf) respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "1844674407370955160" * "\r\n\r\n" r = Response() - p = Messages.connectparser(r, Parser()) - parse!(p, respstr) + b = IOBuffer() + p = Parser() + parse!(p, r, b, respstr) @test r.status == 200 @test r.headers == ["Content-Length"=>"1844674407370955160"] @@ -1725,8 +1745,8 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." r = Response() - p = Messages.connectparser(r, Parser()) - parse!(p, respstr) + p = Parser() + parse!(p, r, b, respstr) @test r.status == 200 @test r.headers == ["Transfer-Encoding"=>"chunked"] @@ -1741,16 +1761,16 @@ const responses = Message[ HTTP.Parsers.reset!(p) reqstr = "POST / HTTP/1.0\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = Request() - p = Messages.connectparser(r, Parser()) - parse!(p, reqstr) + p = Parser() + parse!(p, r, b, reqstr) @test headerscomplete(p) @test !messagecomplete(p) for i = 1:len-1 - parse!(p, "a") + parse!(p, r, b, "a") @test headerscomplete(p) @test !messagecomplete(p) end - parse!(p, "a") + parse!(p, r, b, "a") @test headerscomplete(p) @test messagecomplete(p) end @@ -1759,29 +1779,29 @@ const responses = Message[ HTTP.Parsers.reset!(p) respstr = "HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = Response() - p = Messages.connectparser(r, Parser()) - parse!(p, respstr) + p = Parser() + parse!(p, r, b, respstr) @test headerscomplete(p) @test !messagecomplete(p) for i = 1:len-1 - parse!(p, "a") + parse!(p, r, b, "a") @test headerscomplete(p) @test !messagecomplete(p) end - parse!(p, "a") + parse!(p, r, b, "a") @test headerscomplete(p) @test messagecomplete(p) end reqstr = requests[1].raw * requests[2].raw r = Request() - p = Messages.connectparser(r, Parser()) - n = parse!(p, reqstr) + p = Parser() + n = parse!(p, r, b, reqstr) @test headerscomplete(p) @test messagecomplete(p) ex = Vector{UInt8}(reqstr)[n+1:end] HTTP.Parsers.reset!(p) - parse!(p, ex) + parse!(p, r, b, ex) @test headerscomplete(p) @test messagecomplete(p) @@ -1791,9 +1811,10 @@ const responses = Message[ @test r.headers == ["Test" => "Düsseldorf"] r = Response() - p = Messages.connectparser(r, Parser()) - parse!(p, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") - @test String(take!(r.body)) == "fooba" + p = Parser() + b = IOBuffer() + parse!(p, r, b, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") + @test String(take!(b)) == "fooba" for m in instances(Parsers.Method) m in (Parsers.NOMETHOD, Parsers.CONNECT) && continue @@ -1851,7 +1872,7 @@ const responses = Message[ # @test_throws HTTP.ParsingError HTTP.parse(HTTP.Request, "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection\r\033\065\325eep-Alive\r\nAccept-Encoding: gzip\r\n\r\n") r = Request("GET /bad_get_no_headers_no_body/world HTTP/1.1\r\nAccept: */*\r\n\r\nHELLO") - @test String(take!(r.body)) == "" + @test String(r.body) == "" end end # @testset HTTP.parse From c3f8253f88a432ba3551369e08a170b9cb1da3cc Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 26 Dec 2017 23:49:48 +1100 Subject: [PATCH 076/182] Move request() function from RequestStack module to top-level HTTP module. Add async streaming PUT and GET tests using AWS S3 (added simple AWS4AuthRequest.jl layer to sign requests). --- src/AWS4AuthRequest.jl | 114 +++++++++++++++++++++++++++++++++++++ src/BasicAuthRequest.jl | 2 +- src/CanonicalizeRequest.jl | 2 +- src/ConnectionRequest.jl | 2 +- src/CookieRequest.jl | 2 +- src/ExceptionRequest.jl | 2 +- src/HTTP.jl | 45 ++++++--------- src/MessageRequest.jl | 2 +- src/Messages.jl | 4 +- src/Pairs.jl | 16 +++++- src/RedirectRequest.jl | 2 +- src/RetryRequest.jl | 2 +- src/StreamRequest.jl | 2 +- src/client.jl | 12 ++-- src/server.jl | 14 +++-- src/types.jl | 6 -- test/REQUIRE | 1 + test/async.jl | 73 ++++++++++++++++++++++-- test/client.jl | 105 +++++++++++++++++----------------- test/cookies.jl | 24 ++++---- test/messages.jl | 2 +- test/runtests.jl | 2 +- 22 files changed, 310 insertions(+), 126 deletions(-) create mode 100644 src/AWS4AuthRequest.jl diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl new file mode 100644 index 000000000..373ebd9b7 --- /dev/null +++ b/src/AWS4AuthRequest.jl @@ -0,0 +1,114 @@ +module AWS4AuthRequest + +if VERSION > v"0.7.0-DEV.2338" +using Base64 +end + +using Dates +using Unicode +using MbedTLS: digest, MD_SHA256, MD_MD5 +import ..Layer, ..request, ..Headers +using ..URIs +using ..Pairs: getkv, setkv, rmkv +import ..@debug, ..DEBUG_LEVEL + +abstract type AWS4AuthLayer{Next <: Layer} <: Layer end +export AWS4AuthLayer + +ispathsafe(c::Char) = c == '/' || URIs.issafe(c) +escape_path(path) = escapeuri(path, ispathsafe) + + +# Create AWS Signature Version 4 Authentication Headers. +# http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html + +function sign_aws4!(method::String, + uri::URI, + headers::Headers, + body::Vector{UInt8}; + body_sha256=digest(MD_SHA256, body), + body_md5=digest(MD_MD5, body), + t=now(Dates.UTC), + aws_service=split(uri.host, ".")[1], + aws_region=split(uri.host, ".")[2], + aws_access_key_id=ENV["AWS_ACCESS_KEY_ID"], + aws_secret_access_key=ENV["AWS_SECRET_ACCESS_KEY"], + aws_session_token=get(ENV, "AWS_SESSION_TOKEN", ""), + kw...) + + + # ISO8601 date/time strings for time of request... + date = Dates.format(t,"yyyymmdd") + datetime = Dates.format(t,"yyyymmddTHHMMSSZ") + + # Authentication scope... + scope = [date, aws_region, aws_service, "aws4_request"] + + # Signing key generated from today's scope string... + signing_key = string("AWS4", aws_secret_access_key) + for element in scope + signing_key = digest(MD_SHA256, element, signing_key) + end + + # Authentication scope string... + scope = join(scope, "/") + + # SHA256 hash of content... + content_hash = bytes2hex(body_sha256) + + # HTTP headers... + rmkv(headers, "Authorization") + setkv(headers, "x-amz-content-sha256", content_hash) + setkv(headers, "x-amz-date", datetime) + setkv(headers, "Content-MD5", base64encode(body_md5)) + if aws_session_token != "" + setkv(headers, "x-amz-security-token", aws_session_token) + end + + # Sort and lowercase() Headers to produce canonical form... + canonical_headers = ["$(lowercase(k)):$(strip(v))" for (k,v) in headers] + signed_headers = join(sort([lowercase(k) for (k,v) in headers]), ";") + + # Sort Query String... + query = queryparams(uri.query) + query = Pair[k => query[k] for k in sort(collect(keys(query)))] + + # Create hash of canonical request... + canonical_form = string(method, "\n", + aws_service == "s3" ? uri.path + : escape_path(uri.path), "\n", + escapeuri(query), "\n", + join(sort(canonical_headers), "\n"), "\n\n", + signed_headers, "\n", + content_hash) + @debug 2 "AWS4 canonical_form: $canonical_form" + + canonical_hash = bytes2hex(digest(MD_SHA256, canonical_form)) + + # Create and sign "String to Sign"... + string_to_sign = "AWS4-HMAC-SHA256\n$datetime\n$scope\n$canonical_hash" + signature = bytes2hex(digest(MD_SHA256, string_to_sign, signing_key)) + + @debug 2 "AWS4 string_to_sign: $string_to_sign" + @debug 2 "AWS4 signature: $signature" + + # Append Authorization header... + setkv(headers, "Authorization", string( + "AWS4-HMAC-SHA256 ", + "Credential=$aws_access_key_id/$scope, ", + "SignedHeaders=$signed_headers, ", + "Signature=$signature" + )) +end + + +function request(::Type{AWS4AuthLayer{Next}}, + uri::URI, req, body; kw...) where Next + + sign_aws4!(req.method, uri, req.headers, req.body; kw...) + + return request(Next, uri, req, body; kw...) +end + + +end # module BasicAuthRequest diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index 5190984cd..e50a62754 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -4,7 +4,7 @@ if VERSION > v"0.7.0-DEV.2338" using Base64 end -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..URIs using ..Pairs: getkv, setkv import ..@debug, ..DEBUG_LEVEL diff --git a/src/CanonicalizeRequest.jl b/src/CanonicalizeRequest.jl index 128d0e8f0..572e2ce33 100644 --- a/src/CanonicalizeRequest.jl +++ b/src/CanonicalizeRequest.jl @@ -1,6 +1,6 @@ module CanonicalizeRequest -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..Messages using ..Strings.tocameldash! diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index f4b983aa6..508fab893 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -1,6 +1,6 @@ module ConnectionRequest -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..URIs using ..Messages using ..ConnectionPool diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index 8ca3521c7..4e79a8907 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -1,6 +1,6 @@ module CookieRequest -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..URIs using ..Cookies using ..Pairs: getkv, setkv diff --git a/src/ExceptionRequest.jl b/src/ExceptionRequest.jl index 89ecc7eae..04be78d9b 100644 --- a/src/ExceptionRequest.jl +++ b/src/ExceptionRequest.jl @@ -1,6 +1,6 @@ module ExceptionRequest -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..Messages abstract type ExceptionLayer{Next <: Layer} <: Layer end diff --git a/src/HTTP.jl b/src/HTTP.jl index d6690d293..83c582646 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -1,4 +1,4 @@ -__precompile__(true) +__precompile__() module HTTP using MbedTLS @@ -22,41 +22,33 @@ include("fifobuffer.jl"); using .FIFOBuffers include("cookies.jl"); using .Cookies include("multipart.jl") end -include("parser.jl"); import .Parsers.ParsingError +include("parser.jl"); import .Parsers: ParsingError, Headers include("Connect.jl") include("ConnectionPool.jl") include("Messages.jl"); using .Messages include("HTTPStreams.jl"); using .HTTPStreams -module RequestStack - import ..HTTP - using ..URIs - import ..Messages.mkheaders - import ..Messages.Response - import ..Parsers.Headers +request(method, uri, headers=[], body=UInt8[]; kw...)::Response = + request(string(method), URI(uri), mkheaders(headers), body; kw...) - request(method, uri, headers=[], body=UInt8[]; kw...) = - request(string(method), URI(uri), mkheaders(headers), body; kw...) +request(method::String, uri::URI, headers::Headers, body; kw...)::Response = + request(HTTP.stack(;kw...), method, uri, headers, body; kw...) - request(method::String, uri::URI, headers::Headers, body; kw...)::Response = - request(HTTP.stack(;kw...), method, uri, headers, body; kw...) -end - -open(f::Function, method::String, uri, headers=[]; kw...) = - RequestStack.request(method, uri, headers; iofunction=f, kw...) +open(f::Function, method::String, uri, headers=[]; kw...)::Response = + request(method, uri, headers; iofunction=f, kw...) -httpget(a...; kw...) = RequestStack.request("GET", a..., kw...) -httpput(a...; kw...) = RequestStack.request("PUT", a..., kw...) -httppost(a...; kw...) = RequestStack.request("POST", a..., kw...) -httphead(a...; kw...) = RequestStack.request("HEAD", a..., kw...) +get(a...; kw...) = request("GET", a..., kw...) +put(a...; kw...) = request("PUT", a..., kw...) +post(a...; kw...) = request("POST", a..., kw...) +head(a...; kw...) = request("HEAD", a..., kw...) abstract type Layer end -const NoLayer = Union if !minimal include("RedirectRequest.jl"); using .RedirectRequest include("BasicAuthRequest.jl"); using .BasicAuthRequest +include("AWS4AuthRequest.jl"); using .AWS4AuthRequest include("CookieRequest.jl"); using .CookieRequest include("CanonicalizeRequest.jl"); using .CanonicalizeRequest end @@ -70,6 +62,7 @@ include("StreamRequest.jl"); using .StreamRequest function stack(;redirect=true, basicauthorization=false, + awsauthorization=false, cookies=false, canonicalizeheaders=false, retry=true, @@ -77,16 +70,19 @@ function stack(;redirect=true, connectionpool=true, kw...) + NoLayer = Union + (redirect ? RedirectLayer : NoLayer){ (basicauthorization ? BasicAuthLayer : NoLayer){ (cookies ? CookieLayer : NoLayer){ (canonicalizeheaders ? CanonicalizeLayer : NoLayer){ MessageLayer{ + (awsauthorization ? AWS4AuthLayer : NoLayer){ (retry ? RetryLayer : NoLayer){ (statusexception ? ExceptionLayer : NoLayer){ (connectionpool ? ConnectionPoolLayer : ConnectLayer){ StreamLayer - }}}}}}}} + }}}}}}}}} end else @@ -95,14 +91,9 @@ stack(;kw...) = ExceptionLayer{ ConnectionPoolLayer{ #ConnectLayer{ StreamLayer}}} -import .RequestStack.request end if !minimal -status(r) = r.status #FIXME -headers(r) = Dict(r.headers) #FIXME -import Base.== # FIXME rm -include("types.jl") include("client.jl") include("sniff.jl") include("handlers.jl"); using .Handlers diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 405fed413..e025758e2 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -1,6 +1,6 @@ module MessageRequest -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..URIs using ..Messages using ..Parsers.Headers diff --git a/src/Messages.jl b/src/Messages.jl index 2ade961f4..a2c335412 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -3,7 +3,7 @@ module Messages export Message, Request, Response, iserror, isredirect, ischunked, header, hasheader, setheader, defaultheader, appendheader, - readheaders, readtrailers, writeheaders, + mkheaders, readheaders, readtrailers, writeheaders, readstartline! if VERSION > v"0.7.0-DEV.2338" @@ -320,7 +320,7 @@ function Base.parse(::Type{T}, str::AbstractString) where T <: Message readheaders(bytes, p, m) m.body = readbody(bytes, p) readtrailers(bytes, p, m) - setoef(p) + seteof(p) if !messagecomplete(p) throw(EOFError()) end diff --git a/src/Pairs.jl b/src/Pairs.jl index 65072a675..b48f71b60 100644 --- a/src/Pairs.jl +++ b/src/Pairs.jl @@ -1,6 +1,6 @@ module Pairs -export defaultbyfirst, setbyfirst, getbyfirst, setkv, getkv +export defaultbyfirst, setbyfirst, getbyfirst, setkv, getkv, rmkv """ @@ -72,4 +72,18 @@ function getkv(c, k, default=nothing) end +""" + rmkv(collection, key) + +Remove `key` from `collection` of key/value `Pairs`. +""" + +function rmkv(c, k, default=nothing) + i = findfirst(x->first(x) == k, c) + if i > 0 + deleteat!(c, i) + end + return +end + end # module Pairs diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index ef054bd1b..1c21d3295 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -1,6 +1,6 @@ module RedirectRequest -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..URIs using ..Messages using ..Pairs: setkv diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index d7c7e1256..8dc856384 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -1,7 +1,7 @@ module RetryRequest import ..HTTP -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..MessageRequest using ..Messages import ..@debug, ..DEBUG_LEVEL diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 684032a59..6d08bc9e3 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -1,6 +1,6 @@ module StreamRequest -import ..Layer, ..RequestStack.request +import ..Layer, ..request using ..IOExtras using ..Parsers using ..Messages diff --git a/src/client.jl b/src/client.jl index 0f4995371..32871450a 100644 --- a/src/client.jl +++ b/src/client.jl @@ -33,7 +33,7 @@ global const DEFAULT_CLIENT = Client() # build Request function request(client::Client, method, uri::URI; - headers::Dict=Headers(), + headers::Dict=Dict(), body="", enablechunked::Bool=true, stream::Bool=false, @@ -133,12 +133,12 @@ function request(client::Client, method, uri::URI; end end - return RequestStack.request(m, uri, h, body; args...) + return request(m, uri, h, body; args...) end -request(uri::AbstractString; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, GET, URIs.URL(uri; query=query); verbose=verbose, args...) -request(uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, GET, uri; verbose=verbose, args...) -request(method, uri::String; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, convert(HTTP.Method, method), URIs.URL(uri; query=query); verbose=verbose, args...) -request(method, uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, convert(HTTP.Method, method), uri; verbose=verbose, args...) +#request(uri::AbstractString; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, GET, URIs.URL(uri; query=query); verbose=verbose, args...) +#request(uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, GET, uri; verbose=verbose, args...) +#request(method, uri::String; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, convert(HTTP.Method, method), URIs.URL(uri; query=query); verbose=verbose, args...) +#request(method, uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, convert(HTTP.Method, method), uri; verbose=verbose, args...) for f in [:get, :post, :put, :delete, :head, :trace, :options, :patch, :connect] diff --git a/src/server.jl b/src/server.jl index 5e10fd977..4515731a9 100644 --- a/src/server.jl +++ b/src/server.jl @@ -53,6 +53,10 @@ mutable struct ServerOptions logbody::Bool end +abstract type Scheme end +struct http <: Scheme end +struct https <: Scheme end + ServerOptions(; tlsconfig::HTTP.MbedTLS.SSLConfig=HTTP.MbedTLS.SSLConfig(true), readtimeout::Float64=180.0, ratelimit::Rational{Int64}=Int64(5)//Int64(1), @@ -80,7 +84,7 @@ Supported keyword arguments include: * `support100continue`: a `Bool` indicating whether `Expect: 100-continue` headers should be supported for delayed request body sending; default = `true` * `logbody`: whether the Response body should be logged when `verbose=true` logging is enabled; default = `true` """ -mutable struct Server{T <: HTTP.Scheme, H <: HTTP.Handler} +mutable struct Server{T <: Scheme, H <: HTTP.Handler} handler::H logger::IO in::Channel{Any} @@ -224,8 +228,8 @@ function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, return nothing end -initTLS!(::Type{HTTP.http}, tcp, tlsconfig) = return tcp -function initTLS!(::Type{HTTP.https}, tcp, tlsconfig) +initTLS!(::Type{http}, tcp, tlsconfig) = return tcp +function initTLS!(::Type{https}, tcp, tlsconfig) try tls = HTTP.MbedTLS.SSLContext() HTTP.MbedTLS.setup!(tls, tlsconfig) @@ -321,9 +325,9 @@ function Server(handler::H=HTTP.HandlerFunction((req, rep) -> HTTP.Response("Hel key::String="", args...) where {H <: HTTP.Handler} if cert != "" && key != "" - server = Server{HTTP.https, H}(handler, logger, Channel(1), Channel(1), ServerOptions(; tlsconfig=HTTP.MbedTLS.SSLConfig(cert, key), args...)) + server = Server{https, H}(handler, logger, Channel(1), Channel(1), ServerOptions(; tlsconfig=HTTP.MbedTLS.SSLConfig(cert, key), args...)) else - server = Server{HTTP.http, H}(handler, logger, Channel(1), Channel(1), ServerOptions(; args...)) + server = Server{http, H}(handler, logger, Channel(1), Channel(1), ServerOptions(; args...)) end return server end diff --git a/src/types.jl b/src/types.jl index 9564b8e9a..e69de29bb 100644 --- a/src/types.jl +++ b/src/types.jl @@ -1,6 +0,0 @@ -abstract type Scheme end - -struct http <: Scheme end -struct https <: Scheme end - -const Headers = Dict{String, String} diff --git a/test/REQUIRE b/test/REQUIRE index 732835a50..404f5b366 100644 --- a/test/REQUIRE +++ b/test/REQUIRE @@ -1 +1,2 @@ JSON +XMLDict diff --git a/test/async.jl b/test/async.jl index c900a1db5..5e08b0784 100644 --- a/test/async.jl +++ b/test/async.jl @@ -1,6 +1,70 @@ using JSON +using MbedTLS: digest, MD_MD5, MD_SHA256 +using Base64 + using HTTP.IOExtras +using HTTP.request + +# Tiny S3 interface... +const s3region = "ap-southeast-2" +const s3url = "https://s3.$s3region.amazonaws.com" +s3(method, path, body=UInt8[]; kw...) = + request(method, "$s3url/$path", [], body; awsauthorization=true, kw...) +s3get(path; kw...) = s3("GET", path; kw...) +s3put(path, data; kw...) = s3("PUT", path, data; kw...) + +function create_bucket(bucket) + s3put(bucket, """ + + $s3region + """, + statusexception=false) +end + +create_bucket("http.jl.test") + +put_data_sums = Dict() +@sync for i = 1:100 + data = rand(UInt8, 100000) + md5 = bytes2hex(digest(MD_MD5, data)) + put_data_sums[i] = md5 + @async begin + url = "$s3url/http.jl.test/file$i" + r = HTTP.open("PUT", url, ["Content-Length" => 100000]; + body_sha256=digest(MD_SHA256, data), + body_md5=digest(MD_MD5, data), + awsauthorization=true) do http + for n = 1:1000:100000 + write(http, data[n:n+999]) + sleep(rand(10:100)/1000) + end + end + println("S3 put file$i") + @assert strip(HTTP.header(r, "ETag"), '"') == md5 + end +end + +get_data_sums = Dict() +@sync for i = 1:100 + @async begin + url = "$s3url/http.jl.test/file$i" + buf = IOBuffer() + r = HTTP.open("GET", url; awsauthorization=true) do http + write(buf, http) + end + println("S3 get file$i") + md5 = bytes2hex(digest(MD_MD5, take!(buf))) + @assert strip(HTTP.header(r, "ETag"), '"') == md5 + get_data_sums[i] = md5 + end +end + +for i = 1:100 + @test put_data_sums[i] == get_data_sums[i] +end +#= configs = [ [], [:reuse_limit => 200], @@ -21,7 +85,7 @@ println("running async $count, 1:$num, $config, $http") @sync begin for i = 1:min(num,100) @async begin - r = HTTP.RequestStack.request("GET", + r = HTTP.request("GET", "$http://httpbin.org/headers", ["i" => i]; config...) r = JSON.parse(String(r.body)) push!(result, r["headers"]["I"] => string(i)) @@ -40,7 +104,7 @@ println("running async $count, 1:$num, $config, $http") @sync begin for i = 1:min(num,100) @async begin - r = HTTP.RequestStack.request("GET", + r = HTTP.request("GET", "$http://httpbin.org/stream/$i"; config...) r = String(r.body) r = split(strip(r), "\n") @@ -91,7 +155,7 @@ println("running async $count, 1:$num, $config, $http") try #println("GET $i $n BufferStream $attempt") s = BufferStream() - r = HTTP.RequestStack.request( + r = HTTP.request( "GET", url; response_stream=s, config...) @assert r.status == 200 close(s) @@ -112,7 +176,7 @@ println("running async $count, 1:$num, $config, $http") end else #println("GET $i $n Plain") - r = HTTP.RequestStack.request("GET", url; config...) + r = HTTP.request("GET", url; config...) @assert r.status == 200 str = String(r.body) end @@ -150,3 +214,4 @@ println("running async $count, 1:$num, $config, $http") HTTP.ConnectionPool.closeall() end # testset +=# diff --git a/test/client.jl b/test/client.jl index af59b1b48..30267f1ca 100644 --- a/test/client.jl +++ b/test/client.jl @@ -2,29 +2,30 @@ using JSON +status(r) = r.status for sch in ("http", "https") println("running $sch client tests...") println("simple GET, HEAD, POST, DELETE, etc.") - @test HTTP.status(HTTP.get("$sch://httpbin.org/ip")) == 200 - @test HTTP.status(HTTP.head("$sch://httpbin.org/ip")) == 200 - @test HTTP.status(HTTP.options("$sch://httpbin.org/ip")) == 200 - @test HTTP.status(HTTP.post("$sch://httpbin.org/ip"; statusexception=false)) == 405 - @test HTTP.status(HTTP.post("$sch://httpbin.org/post")) == 200 - @test HTTP.status(HTTP.put("$sch://httpbin.org/put")) == 200 - @test HTTP.status(HTTP.delete("$sch://httpbin.org/delete")) == 200 - @test HTTP.status(HTTP.patch("$sch://httpbin.org/patch")) == 200 + @test status(HTTP.get("$sch://httpbin.org/ip")) == 200 + @test status(HTTP.head("$sch://httpbin.org/ip")) == 200 + @test status(HTTP.options("$sch://httpbin.org/ip")) == 200 + @test status(HTTP.post("$sch://httpbin.org/ip"; statusexception=false)) == 405 + @test status(HTTP.post("$sch://httpbin.org/post")) == 200 + @test status(HTTP.put("$sch://httpbin.org/put")) == 200 + @test status(HTTP.delete("$sch://httpbin.org/delete")) == 200 + @test status(HTTP.patch("$sch://httpbin.org/patch")) == 200 # Testing within tasks, see https://github.com/JuliaWeb/HTTP.jl/issues/18 println("async client request") - @test HTTP.status(wait(@schedule HTTP.get("$sch://httpbin.org/ip"))) == 200 + @test status(wait(@schedule HTTP.get("$sch://httpbin.org/ip"))) == 200 - @test HTTP.status(HTTP.get("$sch://httpbin.org/encoding/utf8")) == 200 + @test status(HTTP.get("$sch://httpbin.org/encoding/utf8")) == 200 println("pass query to uri") r = HTTP.get("$sch://httpbin.org/response-headers"; query=Dict("hey"=>"dude")) - h = HTTP.headers(r) + h = Dict(r.headers) @test (haskey(h, "Hey") ? h["Hey"] == "dude" : h["hey"] == "dude") println("cookie requests") @@ -34,7 +35,7 @@ for sch in ("http", "https") body = String(r.body) @test body == "{\n \"cookies\": {}\n}\n" r = HTTP.get("$sch://httpbin.org/cookies/set?hey=sailor&foo=bar", cookies=true) - @test HTTP.status(r) == 200 + @test status(r) == 200 body = String(r.body) @test body == "{\n \"cookies\": {\n \"foo\": \"bar\", \n \"hey\": \"sailor\"\n }\n}\n" @@ -44,12 +45,12 @@ for sch in ("http", "https") # stream println("client streaming tests") r = HTTP.post("$sch://httpbin.org/post"; body="hey") - @test HTTP.status(r) == 200 + @test status(r) == 200 # stream, but body is too small to actually stream r = HTTP.post("$sch://httpbin.org/post"; body="hey", stream=true) - @test HTTP.status(r) == 200 + @test status(r) == 200 r = HTTP.get("$sch://httpbin.org/stream/100") - @test HTTP.status(r) == 200 + @test status(r) == 200 bytes = r.body a = [JSON.parse(l) for l in split(chomp(String(bytes)), "\n")] totallen = length(bytes) # number of bytes to expect @@ -57,7 +58,7 @@ for sch in ("http", "https") io = BufferStream() r = HTTP.get("$sch://httpbin.org/stream/100"; response_stream=io) close(io) - @test HTTP.status(r) == 200 + @test status(r) == 200 b = [JSON.parse(l) for l in eachline(io)] @test a == b @@ -65,17 +66,17 @@ for sch in ("http", "https") # body posting: Vector{UInt8}, String, IOStream, IOBuffer, FIFOBuffer println("client body posting of various types") - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body="hey")) == 200 - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'])) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body="hey")) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'])) == 200 io = IOBuffer("hey"); seekstart(io) - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body=io)) == 200 tmp = tempname() open(f->write(f, "hey"), tmp, "w") io = open(tmp) - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, enablechunked=false)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body=io, enablechunked=false)) == 200 close(io); rm(tmp) f = HTTP.FIFOBuffer("hey") - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f, enablechunked=false)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body=f, enablechunked=false)) == 200 # chunksize # @@ -84,26 +85,26 @@ for sch in ("http", "https") # message to any POST/PUT requests that are sent using chunked encoding # See https://github.com/kennethreitz/httpbin/issues/340#issuecomment-330176449 println("client transfer-encoding chunked") - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body="hey", chunksize=2)) == 200 - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'], chunksize=2)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body="hey", chunksize=2)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'], chunksize=2)) == 200 io = IOBuffer("hey"); seekstart(io) - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 tmp = tempname() open(f->write(f, "hey"), tmp, "w") io = open(tmp) - @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 + @test_broken status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 close(io); rm(tmp) f = HTTP.FIFOBuffer("hey") - @test_broken HTTP.status(HTTP.post("$sch://httpbin.org/post"; body=f, chunksize=2)) == 200 + @test_broken status(HTTP.post("$sch://httpbin.org/post"; body=f, chunksize=2)) == 200 # multipart println("client multipart body") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there")) - @test HTTP.status(r) == 200 + @test status(r) == 200 @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there")) - @test HTTP.status(r) == 200 + @test status(r) == 200 @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {}, \n \"form\": {\n \"hey\": \"there\"\n }") tmp = tempname() @@ -111,7 +112,7 @@ for sch in ("http", "https") io = open(tmp) r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "iostream"=>io)) close(io); rm(tmp) - @test HTTP.status(r) == 200 + @test status(r) == 200 str = String(r.body) @test startswith(str, "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") @@ -120,7 +121,7 @@ for sch in ("http", "https") io = open(tmp) r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "iostream"=>io)) close(io); rm(tmp) - @test HTTP.status(r) == 200 + @test status(r) == 200 @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"iostream\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") tmp = tempname() @@ -129,7 +130,7 @@ for sch in ("http", "https") m = HTTP.Multipart("mycoolfile.txt", io) r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "multi"=>m)) close(io); rm(tmp) - @test HTTP.status(r) == 200 + @test status(r) == 200 @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") tmp = tempname() @@ -138,7 +139,7 @@ for sch in ("http", "https") m = HTTP.Multipart("mycoolfile", io, "application/octet-stream") r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "multi"=>m), chunksize=1000) close(io); rm(tmp) - @test HTTP.status(r) == 200 + @test status(r) == 200 @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") # asynchronous @@ -151,23 +152,23 @@ for sch in ("http", "https") write(f, " there ") # as we write to f, it triggers another chunk to be sent in our async request write(f, "sailor") close(f) # setting eof on f causes the async request to send a final chunk and return the response - @test HTTP.status(wait(t)) == 200 + @test status(wait(t)) == 200 end # redirects println("client redirect following") r = HTTP.get("$sch://httpbin.org/redirect/1") - @test HTTP.status(r) == 200 + @test status(r) == 200 #@test length(HTTP.history(r)) == 1 - @test HTTP.status(HTTP.get("$sch://httpbin.org/redirect/6")) == 302 - @test HTTP.status(HTTP.get("$sch://httpbin.org/relative-redirect/1")) == 200 - @test HTTP.status(HTTP.get("$sch://httpbin.org/absolute-redirect/1")) == 200 - @test HTTP.status(HTTP.get("$sch://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com")) == 200 + @test status(HTTP.get("$sch://httpbin.org/redirect/6")) == 302 + @test status(HTTP.get("$sch://httpbin.org/relative-redirect/1")) == 200 + @test status(HTTP.get("$sch://httpbin.org/absolute-redirect/1")) == 200 + @test status(HTTP.get("$sch://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com")) == 200 - @test HTTP.status(HTTP.post("$sch://httpbin.org/post"; body="√")) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body="√")) == 200 println("client basic auth") - @test HTTP.status(HTTP.get("$sch://user:pwd@httpbin.org/basic-auth/user/pwd"; basicauthorization=true)) == 200 - @test HTTP.status(HTTP.get("$sch://user:pwd@httpbin.org/hidden-basic-auth/user/pwd"; basicauthorization=true)) == 200 + @test status(HTTP.get("$sch://user:pwd@httpbin.org/basic-auth/user/pwd"; basicauthorization=true)) == 200 + @test status(HTTP.get("$sch://user:pwd@httpbin.org/hidden-basic-auth/user/pwd"; basicauthorization=true)) == 200 # custom client & other high-level entries println("high-level client request methods") @@ -180,28 +181,28 @@ for sch in ("http", "https") @test length(String(take!(buf))) > 0 =# - r = HTTP.request("$sch://httpbin.org/ip") - @test HTTP.status(r) == 200 + r = HTTP.request("GET", "$sch://httpbin.org/ip") + @test status(r) == 200 uri = HTTP.URI("$sch://httpbin.org/ip") - r = HTTP.request(uri) - @test HTTP.status(r) == 200 + r = HTTP.request("GET", uri) + @test status(r) == 200 r = HTTP.get(uri) - @test HTTP.status(r) == 200 + @test status(r) == 200 r = HTTP.get(cli, uri) - @test HTTP.status(r) == 200 + @test status(r) == 200 r = HTTP.request(HTTP.GET, "$sch://httpbin.org/ip") - @test HTTP.status(r) == 200 + @test status(r) == 200 uri = HTTP.URI("$sch://httpbin.org/ip") r = HTTP.request("GET", uri) - @test HTTP.status(r) == 200 + @test status(r) == 200 #= FIXME req = HTTP.Request(HTTP.GET, uri, HTTP.Headers(), HTTP.FIFOBuffer()) r = HTTP.request(req) - @test HTTP.status(r) == 200 + @test status(r) == 200 @test HTTP.request(r) !== nothing @test length(take!(r)) > 0 =# @@ -217,13 +218,13 @@ for sch in ("http", "https") # @test isempty(HTTP.history(r)) r = HTTP.get("$sch://httpbin.org/image/png") - @test HTTP.status(r) == 200 + @test status(r) == 200 # ensure we can use AbstractString for requests r = HTTP.get(SubString("http://httpbin.org/ip",1)) # canonicalizeheaders - @test HTTP.status(HTTP.get("$sch://httpbin.org/ip"; canonicalizeheaders=false)) == 200 + @test status(HTTP.get("$sch://httpbin.org/ip"; canonicalizeheaders=false)) == 200 # r = HTTP.connect("http://47.89.41.164:80") # gzip body = "hey" diff --git a/test/cookies.jl b/test/cookies.jl index 25ac1d6f8..3a0dfa04b 100644 --- a/test/cookies.jl +++ b/test/cookies.jl @@ -49,21 +49,21 @@ end @testset "readsetcookies" begin cookietests = [ - (HTTP.Headers(["Set-Cookie"=> "Cookie-1=v\$1"]), [HTTP.Cookie("Cookie-1", "v\$1")]), - (HTTP.Headers(["Set-Cookie"=> "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"]), + (Dict(["Set-Cookie"=> "Cookie-1=v\$1"]), [HTTP.Cookie("Cookie-1", "v\$1")]), + (Dict(["Set-Cookie"=> "NID=99=YsDT5i3E-CXax-; expires=Wed, 23-Nov-2011 01:05:03 GMT; path=/; domain=.google.ch; HttpOnly"]), [HTTP.Cookie("NID", "99=YsDT5i3E-CXax-"; path="/", domain="google.ch", httponly=true, expires=Dates.DateTime(2011, 11, 23, 1, 5, 3, 0))]), - (HTTP.Headers(["Set-Cookie"=> ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"]), + (Dict(["Set-Cookie"=> ".ASPXAUTH=7E3AA; expires=Wed, 07-Mar-2012 14:25:06 GMT; path=/; HttpOnly"]), [HTTP.Cookie(".ASPXAUTH", "7E3AA"; path="/", expires=Dates.DateTime(2012, 3, 7, 14, 25, 6, 0), httponly=true)]), - (HTTP.Headers(["Set-Cookie"=> "ASP.NET_SessionId=foo; path=/; HttpOnly"]), + (Dict(["Set-Cookie"=> "ASP.NET_SessionId=foo; path=/; HttpOnly"]), [HTTP.Cookie("ASP.NET_SessionId", "foo"; path="/", httponly=true)]), - (HTTP.Headers(["Set-Cookie"=> "special-1=a z"]), [HTTP.Cookie("special-1", "a z")]), - (HTTP.Headers(["Set-Cookie"=> "special-2=\" z\""]), [HTTP.Cookie("special-2", " z")]), - (HTTP.Headers(["Set-Cookie"=> "special-3=\"a \""]), [HTTP.Cookie("special-3", "a ")]), - (HTTP.Headers(["Set-Cookie"=> "special-4=\" \""]), [HTTP.Cookie("special-4", " ")]), - (HTTP.Headers(["Set-Cookie"=> "special-5=a,z"]), [HTTP.Cookie("special-5", "a,z")]), - (HTTP.Headers(["Set-Cookie"=> "special-6=\",z\""]), [HTTP.Cookie("special-6", ",z")]), - (HTTP.Headers(["Set-Cookie"=> "special-7=a,"]), [HTTP.Cookie("special-7", "a,")]), - (HTTP.Headers(["Set-Cookie"=> "special-8=\",\""]), [HTTP.Cookie("special-8", ",")]), + (Dict(["Set-Cookie"=> "special-1=a z"]), [HTTP.Cookie("special-1", "a z")]), + (Dict(["Set-Cookie"=> "special-2=\" z\""]), [HTTP.Cookie("special-2", " z")]), + (Dict(["Set-Cookie"=> "special-3=\"a \""]), [HTTP.Cookie("special-3", "a ")]), + (Dict(["Set-Cookie"=> "special-4=\" \""]), [HTTP.Cookie("special-4", " ")]), + (Dict(["Set-Cookie"=> "special-5=a,z"]), [HTTP.Cookie("special-5", "a,z")]), + (Dict(["Set-Cookie"=> "special-6=\",z\""]), [HTTP.Cookie("special-6", ",z")]), + (Dict(["Set-Cookie"=> "special-7=a,"]), [HTTP.Cookie("special-7", "a,")]), + (Dict(["Set-Cookie"=> "special-8=\",\""]), [HTTP.Cookie("special-8", ",")]), ] for (h, c) in cookietests diff --git a/test/messages.jl b/test/messages.jl index d335ab6fa..75f35f2ec 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -8,7 +8,7 @@ end using HTTP.Messages import HTTP.Messages.appendheader import HTTP.URI -import HTTP.RequestStack.request +import HTTP.request using HTTP.StatusError diff --git a/test/runtests.jl b/test/runtests.jl index 850733b61..1193b357e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -22,7 +22,7 @@ end include("uri.jl"); include("cookies.jl"); include("parser.jl"); - include("body.jl"); +# include("body.jl"); include("messages.jl"); # include("types.jl"); # include("handlers.jl") From 531d36ae489105a39615860ad82619cc3c0d830a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 26 Dec 2017 23:55:25 +1100 Subject: [PATCH 077/182] whoops --- test/async.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/async.jl b/test/async.jl index 5e08b0784..dee6d12bd 100644 --- a/test/async.jl +++ b/test/async.jl @@ -64,7 +64,6 @@ for i = 1:100 @test put_data_sums[i] == get_data_sums[i] end -#= configs = [ [], [:reuse_limit => 200], @@ -214,4 +213,4 @@ println("running async $count, 1:$num, $config, $http") HTTP.ConnectionPool.closeall() end # testset -=# + From 3e0d7a695ad02e4e4c138f826069d8c3897a440f Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 27 Dec 2017 23:43:26 +1100 Subject: [PATCH 078/182] More async streaming tests (and resulting bug-fixes) ConnectionPoolLayer - accept connectionpool::Bool=true option instead of seperate ConnectLayer ConnectionPool.jl - Add NonReentrantLock, wrapper for ReentrantLock with assertion of no multiple locking. - Many new locking logic assertions - Make locking logic more rigid HTTPStream.jl - Fix write to read transition logic in eof() RetryRequest.jl - Reset response body before processing retry. parser.jl - rename setheadresponse() to setnobody() --- src/ConnectionPool.jl | 163 ++++++++++++++++++++++++++------------- src/ConnectionRequest.jl | 30 +++---- src/HTTP.jl | 12 ++- src/HTTPStreams.jl | 36 +++++---- src/Messages.jl | 31 +++++++- src/RedirectRequest.jl | 2 + src/RetryRequest.jl | 9 ++- src/StreamRequest.jl | 18 +++-- src/debug.jl | 18 ++++- src/parser.jl | 62 +++++++-------- test/async.jl | 85 ++++++++++++++------ 11 files changed, 298 insertions(+), 168 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index f61349ebc..850561593 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -9,9 +9,37 @@ import MbedTLS.SSLContext import ..Connect: getconnection, getparser import ..Parsers.Parser + const max_duplicates = 8 const nolimit = typemax(Int) + +macro lockassert(cond) + DEBUG_LEVEL > 1 ? esc(:(@assert $cond)) : :() #FIXME +end + +struct NonReentrantLock + l::ReentrantLock +end + +NonReentrantLock() = NonReentrantLock(ReentrantLock()) + +Base.islocked(l::NonReentrantLock) = islocked(l.l) +havelock(l) = islocked(l) && l.l.locked_by == current_task() + +function Base.lock(l::NonReentrantLock) + @lockassert !havelock(l) + lock(l.l) + @lockassert l.l.reentrancy_cnt == 1 +end + +function Base.unlock(l::NonReentrantLock) + @lockassert havelock(l) + @lockassert l.l.reentrancy_cnt == 1 + unlock(l.l) +end + + const nobytes = view(UInt8[], 1:0) const ByteView = typeof(nobytes) byteview(bytes::ByteView) = bytes @@ -43,28 +71,35 @@ mutable struct Connection{T <: IO} <: IO excess::ByteView writecount::Int readcount::Int - writelock::ReentrantLock - readlock::ReentrantLock + writelock::NonReentrantLock + readlock::NonReentrantLock parser::Parser end Connection{T}(host::AbstractString, port::AbstractString, io::T) where T <: IO = Connection{T}(host, port, io, view(UInt8[], 1:0), 0, 0, - ReentrantLock(), ReentrantLock(), Parser()) + NonReentrantLock(), NonReentrantLock(), Parser()) const noconnection = Connection{TCPSocket}("","",TCPSocket()) + getparser(c::Connection) = c.parser Base.unsafe_write(c::Connection, p::Ptr{UInt8}, n::UInt) = unsafe_write(c.io, p, n) +Base.isopen(c::Connection) = isopen(c.io) Base.eof(c::Connection) = isempty(c.excess) && eof(c.io) +Base.nb_available(c::Connection) = !isempty(c.excess) ? length(c.excess) : + nb_available(c.io) +Base.isreadable(c::Connection) = havelock(c.readlock) +Base.iswritable(c::Connection) = havelock(c.writelock) function Base.readavailable(c::Connection)::ByteView + @lockassert isreadable(c) if !isempty(c.excess) bytes = c.excess @debug 3 "read $(length(bytes))-bytes from excess buffer." @@ -84,7 +119,10 @@ Push bytes back into a connection's `excess` buffer (to be returned by the next read). """ -IOExtras.unread!(c::Connection, bytes::ByteView) = c.excess = bytes +function IOExtras.unread!(c::Connection, bytes::ByteView) + @lockassert isreadable(c) + c.excess = bytes +end """ @@ -96,26 +134,24 @@ Increment `writecount` and wait for pending reads to complete. """ function IOExtras.closewrite(c::Connection) + @lockassert iswritable(c) + seq = c.writecount - c.writecount += 1; @debug 2 "write done: $c" + c.writecount += 1 ;@debug 2 "Write done: $c" + unlock(c.writelock) + notify(poolcondition) - # The write lock may already have been unlocked by `close` or `purge`. - if islocked(c.writelock) - unlock(c.writelock) - notify(poolcondition) - end lock(c.readlock) # Wait for prior pending reads to complete... while c.readcount != seq - unlock(c.readlock) + if !isopen(c) && nb_available(c) == 0 + break + end + unlock(c.readlock) ;@debug 1 "Waiting to read seq=$seq: $c" yield() lock(c.readlock) - # Error if there is nothing to read. - if !isopen(c.io) && nb_available(c.io) == 0 - unlock(c.readlock) - throw(EOFError()) - end end + @lockassert isreadable(c) return end @@ -129,28 +165,40 @@ Increment `readcount` and wake up tasks waiting in `closewrite`. """ function IOExtras.closeread(c::Connection) - c.readcount += 1; @debug 2 "read done: $c" - if islocked(c.readlock) - unlock(c.readlock) - end + @lockassert isreadable(c) + c.readcount += 1 ;@debug 2 "Read done: $c" + unlock(c.readlock) notify(poolcondition) return end function Base.close(c::Connection) - close(c.io) - if islocked(c.readlock) - unlock(c.readlock) - end - if islocked(c.writelock) - unlock(c.writelock) + close(c.io) ;@debug 2 "Closed: $c" + if isreadable(c) + purge(c) + closeread(c) end - notify(poolcondition) return end +""" + purge(::Connection) + +Remove unread data from a `Connection`. +""" + +function purge(c::Connection) + @assert !isopen(c) + while !eof(c.io) + readavailable(c.io) + end + c.excess = nobytes + @assert nb_available(c) == 0 +end + + """ pool @@ -186,15 +234,15 @@ end """ - findwriteable(type, host, port) -> Vector{Connection} + findwritable(type, host, port) -> Vector{Connection} Find `Connections` in the `pool` that are ready for writing. """ -function findwriteable(T::Type, - host::AbstractString, - port::AbstractString, - reuse_limit::Int=nolimit) +function findwritable(T::Type, + host::AbstractString, + port::AbstractString, + reuse_limit::Int=nolimit) filter(c->(typeof(c.io) == T && c.host == host && @@ -249,14 +297,10 @@ end Remove closed connections from `pool`. """ function purge() - while (i = findfirst(x->!isopen(x.io), pool)) > 0 + while (i = findfirst(x->!isopen(x.io) && + x.readcount == x.writecount, pool)) > 0 c = pool[i] - if islocked(c.readlock) - unlock(c.readlock) - end - if islocked(c.writelock) - unlock(c.writelock) - end + purge(c) deleteat!(pool, i) ;@debug 1 "Deleted: $c" end end @@ -278,6 +322,7 @@ function getconnection(::Type{Connection{T}}, while true lock(poollock) + @lockassert poollock.reentrancy_cnt == 1 try # Close connections that have reached the reuse limit... @@ -291,10 +336,10 @@ function getconnection(::Type{Connection{T}}, purge() # Try to find a connection with no active readers or writers... - writeable = findwriteable(T, host, port, reuse_limit) - idle = filter(c->!islocked(c.readlock), writeable) + writable = findwritable(T, host, port, reuse_limit) + idle = filter(c->!islocked(c.readlock), writable) if !isempty(idle) - c = rand(idle) ;@debug 2 "Idle: $c" + c = rand(idle) ;@debug 2 "Idle: $c" lock(c.writelock) return c end @@ -304,15 +349,15 @@ function getconnection(::Type{Connection{T}}, busy = findall(T, host, port) if length(busy) < max_duplicates io = getconnection(T, host, port; kw...) - c = Connection{T}(host, port, io) ;@debug 1 "New: $c" + c = Connection{T}(host, port, io) ;@debug 1 "New: $c" lock(c.writelock) push!(pool, c) return c end # Share a connection that has active readers... - if !isempty(writeable) - c = rand(writeable) ;@debug 2 "Shared: $c" + if !isempty(writable) + c = rand(writable) ;@debug 2 "Shared: $c" lock(c.writelock) return c end @@ -333,21 +378,29 @@ function Base.show(io::IO, c::Connection) Int(localport(c)), ", ", typeof(c.io), ", ", tcpstatus(c), ", ", length(c.excess), "-byte excess, writes/reads: ", - c.writecount, "/", c.readcount) + c.writecount, "/", c.readcount, + islocked(c.readlock) ? ", readlock" : "", + islocked(c.writelock) ? ", writelock" : "") end tcpsocket(c::Connection{SSLContext})::TCPSocket = c.io.bio tcpsocket(c::Connection{TCPSocket})::TCPSocket = c.io -localport(c::Connection) = !isopen(c.io) ? 0 : - VERSION > v"0.7.0-DEV" ? - getsockname(tcpsocket(c))[2] : - Base._sockname(tcpsocket(c), true)[2] - -peerport(c::Connection) = !isopen(c.io) ? 0 : - VERSION > v"0.7.0-DEV" ? - getpeername(tcpsocket(c))[2] : - Base._sockname(tcpsocket(c), false)[2] +localport(c::Connection) = try !isopen(tcpsocket(c)) ? 0 : + VERSION > v"0.7.0-DEV" ? + getsockname(tcpsocket(c))[2] : + Base._sockname(tcpsocket(c), true)[2] + catch + 0 + end + +peerport(c::Connection) = try !isopen(tcpsocket(c)) ? 0 : + VERSION > v"0.7.0-DEV" ? + getpeername(tcpsocket(c))[2] : + Base._sockname(tcpsocket(c), false)[2] + catch + 0 + end tcpstatus(c::Connection) = Base.uv_status_string(tcpsocket(c)) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 508fab893..80ce5c4da 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -5,6 +5,7 @@ using ..URIs using ..Messages using ..ConnectionPool using MbedTLS.SSLContext +import ..@debug, ..DEBUG_LEVEL abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end @@ -20,33 +21,20 @@ sockettype(uri::URI) = uri.scheme == "https" ? SSLContext : TCPSocket Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. """ -function request(::Type{ConnectionPoolLayer{Next}}, - uri::URI, req, body; kw...) where Next +function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; + connectionpool::Bool=true, kw...) where Next - Connection = ConnectionPool.Connection{sockettype(uri)} - io = getconnection(Connection, uri.host, uri.port; kw...) - - try - return request(Next, io, req, body; kw...) - catch e - @schedule close(io) - rethrow(e) + Conncetion = sockettype(uri) + if connectionpool + Connection = ConnectionPool.Connection{Connection} end -end - - -abstract type ConnectLayer{Next <: Layer} <: Layer end -export ConnectLayer - -function request(::Type{ConnectLayer{Next}}, - uri::URI, req, body; kw...) where Next - - io = getconnection(sockettype(uri), uri.host, uri.port; kw...) + io = getconnection(Connection, uri.host, uri.port; kw...) try return request(Next, io, req, body; kw...) catch e - @schedule close(io) + @debug 1 "ConnectionLayer $e. Closing: $io" + close(io) rethrow(e) end end diff --git a/src/HTTP.jl b/src/HTTP.jl index 83c582646..e76832678 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -5,7 +5,7 @@ using MbedTLS import MbedTLS.SSLContext -const DEBUG_LEVEL = 0 +const DEBUG_LEVEL = 1 const minimal = false include("compat.jl") @@ -66,8 +66,7 @@ function stack(;redirect=true, cookies=false, canonicalizeheaders=false, retry=true, - statusexception=true, - connectionpool=true, + statusexception=true kw...) NoLayer = Union @@ -80,16 +79,15 @@ function stack(;redirect=true, (awsauthorization ? AWS4AuthLayer : NoLayer){ (retry ? RetryLayer : NoLayer){ (statusexception ? ExceptionLayer : NoLayer){ - (connectionpool ? ConnectionPoolLayer : ConnectLayer){ + ConnectionPoolLayer{ StreamLayer }}}}}}}}} end else -stack(;kw...) = ExceptionLayer{ - MessageLayer{ +stack(;kw...) = MessageLayer{ + ExceptionLayer{ ConnectionPoolLayer{ - #ConnectLayer{ StreamLayer}}} end diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index ae7eddca1..d28b74978 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -1,6 +1,6 @@ module HTTPStreams -export HTTPStream, readheaders, readtrailers +export HTTPStream, readheaders using ..IOExtras using ..Parsers @@ -11,17 +11,17 @@ struct HTTPStream{T <: Message} <: IO stream::IO message::T parser::Parser - chunked::Bool + writechunked::Bool end function HTTPStream(io::IO, request::Request, parser::Parser) - chunked = header(request, "Transfer-Encoding") == "chunked" - HTTPStream{Response}(io, request.response, parser, chunked) + writechunked = header(request, "Transfer-Encoding") == "chunked" + HTTPStream{Response}(io, request.response, parser, writechunked) end function Base.unsafe_write(http::HTTPStream, p::Ptr{UInt8}, n::UInt) - if !http.chunked + if !http.writechunked return unsafe_write(http.stream, p, n) end return write(http.stream, hex(n), "\r\n") + @@ -30,7 +30,7 @@ function Base.unsafe_write(http::HTTPStream, p::Ptr{UInt8}, n::UInt) end -writeend(http) = http.chunked ? write(http.stream, "0\r\n\r\n") : 0 +writeend(http) = http.writechunked ? write(http.stream, "0\r\n\r\n") : 0 function Messages.readheaders(http::HTTPStream) @@ -43,21 +43,17 @@ end function configure_parser(http::HTTPStream{Response}) reset!(http.parser) - if http.message.request.method in ("HEAD", "CONNECT") # FIXME Why CONNECT? - setheadresponse(http.parser) + if http.message.request.method in ("HEAD", "CONNECT") + setnobody(http.parser) end end configure_parser(http::HTTPStream{Request}) = reset!(http.parser) -readheadersdone(http::HTTPStream) = http.message.status != 0 - - function Base.eof(http::HTTPStream) - if !readheadersdone(http) + if !headerscomplete(http.message) readheaders(http) - @assert readheadersdone(http) end if bodycomplete(http.parser) return true @@ -71,11 +67,14 @@ end function Base.readavailable(http::HTTPStream)::ByteView - if !headerscomplete(http.parser) + if !headerscomplete(http.message) throw(ArgumentError("headers must be read before body\n$http\n")) end + if bodycomplete(http.parser) + throw(ArgumentError("message body already complete\n$http\n")) + end bytes = readavailable(http.stream) - if isempty(bytes) + if isempty(bytes) return nobytes end bytes, excess = parsebody(http.parser, bytes) @@ -98,10 +97,13 @@ function Base.close(http::HTTPStream{Response}) while !eof(http) readavailable(http) end - readtrailers(http.stream, http.parser, http.message) + + if bodycomplete(http.parser) && !messagecomplete(http.parser) + readtrailers(http.stream, http.parser, http.message) + end if !messagecomplete(http.parser) - @show http.parser + close(http.stream) throw(EOFError()) end diff --git a/src/Messages.jl b/src/Messages.jl index a2c335412..726fd8e1b 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -1,9 +1,10 @@ module Messages export Message, Request, Response, + reset!, iserror, isredirect, ischunked, header, hasheader, setheader, defaultheader, appendheader, - mkheaders, readheaders, readtrailers, writeheaders, + mkheaders, readheaders, headerscomplete, readtrailers, writeheaders, readstartline! if VERSION > v"0.7.0-DEV.2338" @@ -16,6 +17,7 @@ using ..Pairs using ..IOExtras using ..Parsers import ..Parsers +import ..Parsers: headerscomplete, reset! abstract type Message end @@ -44,6 +46,17 @@ Response(status::Int=0, headers=[]; body=UInt8[], request=nothing) = Response(bytes) = parse(Response, bytes) +function reset!(r::Response) + r.version = v"1.1" + r.status = 0 + if !isempty(r.headers) + empty!(r.headers) + end + if !isempty(r.body) + empty!(r.body) + end +end + """ Request @@ -294,6 +307,10 @@ function readheaders(io::IO, parser::Parser, message::Message) end +headerscomplete(r::Request) = r.method != "" +headerscomplete(r::Response) = r.status != 0 + + function readtrailers(io::IO, parser::Parser, message::Message) if messagehastrailing(parser) readheaders(io, parser, message) @@ -345,8 +362,20 @@ The first chunk of the Message Body (for display purposes). """ bodysummary(bytes) = view(bytes, 1:min(length(bytes), body_show_max)) +function compactstartline(m::Message) + b = IOBuffer() + writestartline(b, m) + strip(String(take!(b))) +end function Base.show(io::IO, m::Message) + if get(io, :compact, false) + print(io, compactstartline(m)) + if m isa Response + print(io, " <= (", compactstartline(m.request), ")") + end + return + end println(io, typeof(m), ":") println(io, "\"\"\"") writeheaders(io, m) diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index 1c21d3295..a9a670d7a 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -40,6 +40,8 @@ function request(::Type{RedirectLayer{Next}}, headers = Header[] end + @debug 1 "Redirecting to: $uri" + count += 1 end diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 8dc856384..5a340a2b7 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -20,14 +20,17 @@ isrecoverable(e::Exception) = false isrecoverable(e, req) = isrecoverable(e) && !(req.body === body_was_streamed) && - !(req.response.body === body_was_streamed) + !(req.response.body === body_was_streamed) && + (@debug 1 "Retring on $e: $(sprint(showcompact, req))"; + true) function request(::Type{RetryLayer{Next}}, uri, req, body; - retries=3, kw...) where Next + retries=4, kw...) where Next retry_request = retry(request, delays=ExponentialBackOff(n = retries), - check=(s,ex)->(s,isrecoverable(ex, req))) + check=(s,ex)->(s,isrecoverable(ex, req) && + (reset!(req.response); true))) retry_request(Next, uri, req, body; kw...) end diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 6d08bc9e3..b3a48c148 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -5,9 +5,9 @@ using ..IOExtras using ..Parsers using ..Messages using ..HTTPStreams -using ..ConnectionPool.getparser +import ..ConnectionPool using ..MessageRequest -import ..@debug, ..DEBUG_LEVEL +import ..@debugshort, ..DEBUG_LEVEL abstract type StreamLayer <: Layer end export StreamLayer @@ -35,9 +35,10 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; write(io, req) - @debug 1 req + @debugshort 2 req + @debug 3 req - http = HTTPStream(io, req, getparser(io)) + http = HTTPStream(io, req, ConnectionPool.getparser(io)) if iofunction != nothing iofunction(http) @@ -48,18 +49,19 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; readheaders(http) if response_stream == nothing - http.message.body = read(http) + req.response.body = read(http) else - http.message.body = body_was_streamed + req.response.body = body_was_streamed write(response_stream, http) end end close(http) - @debug 1 http.message + @debugshort 2 req.response + @debug 3 req.response - return http.message + return req.response end diff --git a/src/debug.jl b/src/debug.jl index 8b434a237..f76c3e49f 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -1,9 +1,23 @@ +taskid() = hex(hash(current_task()) & 0xffff, 4) + macro debug(n::Int, s) - DEBUG_LEVEL >= n ? esc(:(println(string("DEBUG: ", $s)))) : :() + DEBUG_LEVEL >= n ? :(println("DEBUG: ", taskid(), " ", $(esc(s)))) : + :() end macro debugshow(n::Int, s) - DEBUG_LEVEL >= n ? esc(:(print("DEBUG: "); @show $s)) : :() + DEBUG_LEVEL >= n ? :(println("DEBUG: ", taskid(), " ", + $(sprint(show_unquoted, s)), " = ", + sprint(io->show(io, "text/plain", + begin value=$(esc(s)) end)))) : + :() + +end + +macro debugshort(n::Int, s) + DEBUG_LEVEL >= n ? :(println("DEBUG: ", taskid(), " ", + sprint(showcompact, $(esc(s))))) : + :() end #= diff --git a/src/parser.jl b/src/parser.jl index 31e718510..8774598bc 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -30,7 +30,7 @@ export Parser, Header, Headers, ByteView, nobytes, messagestarted, headerscomplete, bodycomplete, messagecomplete, messagehastrailing, waitingforeof, seteof, - connectionclosed, setheadresponse, + connectionclosed, setnobody, ParsingError, ParsingErrorCode using ..URIs.parseurlchar @@ -44,7 +44,6 @@ include("parseutils.jl") const strict = false # See macro @errifstrict -const enable_passert = false # See macro @passert const nobytes = view(UInt8[], 1:0) @@ -68,7 +67,7 @@ Message() = Message(NOMETHOD, 0, 0, "", 0, false) mutable struct Parser # config - isheadresponse::Bool # Are we parsing a HEAD Response Message? + message_has_no_body::Bool # Are we parsing a HEAD Response Message? # state state::UInt8 @@ -104,7 +103,7 @@ Revert `Parser` to unconfigured state. function reset!(p::Parser) # config - p.isheadresponse = false + p.message_has_no_body = false # state p.state = s_start_req_or_res @@ -126,12 +125,13 @@ end """ - setheadresponse(::Parser) + setnobody(::Parser) -Mark the Message as being the Response to a HEAD Request. +Tell the `Parser` not to look for a Message Body. +e.g. for the Response to a HEAD Request. """ -setheadresponse(p::Parser) = p.isheadresponse = true +setnobody(p::Parser) = p.message_has_no_body = true """ @@ -246,7 +246,7 @@ macro errorifstrict(cond) end macro passert(cond) - enable_passert ? esc(:(@assert $cond)) : :() + DEBUG_LEVEL > 1 ? esc(:(@assert $cond)) : :() end macro methodstate(meth, i, char) @@ -275,13 +275,13 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, len = length(bytes) p_state = parser.state - @debug 2 "parseheaders(parser.state=$(ParsingStateCode(p_state))), " * + @debug 3 "parseheaders(parser.state=$(ParsingStateCode(p_state))), " * "$len-bytes:\n" * escapelines(String(collect(bytes))) * ")" p = 0 while p < len && p_state <= s_headers_done - @debug 3 string("top of while($p < $len) \"", + @debug 4 string("top of while($p < $len) \"", Base.escape_string(string(Char(bytes[p+1]))), "\" ", ParsingStateCode(p_state)) p += 1 @@ -460,14 +460,14 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif p_state == s_req_method matcher = string(parser.message.method) - @debugshow 3 matcher - @debugshow 3 parser.index + @debugshow 4 matcher + @debugshow 4 parser.index if ch == ' ' && parser.index == length(matcher) + 1 p_state = s_req_spaces_before_url elseif parser.index > length(matcher) @err(HPE_INVALID_METHOD) elseif ch == matcher[parser.index] - @debug 3 "nada" + @debug 4 "nada" elseif isalpha(ch) ci = @methodstate(parser.message.method, Int(parser.index) - 1, ch) @@ -511,14 +511,14 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif ch == '-' && parser.index == 2 && parser.message.method == MKCOL - @debug 3 "matched MSEARCH" + @debug 4 "matched MSEARCH" parser.message.method = MSEARCH parser.index -= 1 else @err(HPE_INVALID_METHOD) end parser.index += 1 - @debugshow 3 parser.index + @debugshow 4 parser.index elseif p_state == s_req_spaces_before_url ch == ' ' && continue @@ -569,7 +569,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if p_state >= s_req_http_start parser.message.url = take!(parser.valuebuffer) - @debugshow 3 parser.message.url + @debugshow 4 parser.message.url end p = min(p, len) @@ -675,13 +675,13 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, start = p while p <= len @inbounds ch = Char(bytes[p]) - @debug 3 Base.escape_string(string(ch)) + @debug 4 Base.escape_string(string(ch)) c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] if c == Char(0) @errorif(ch != ':', HPE_INVALID_HEADER_TOKEN) break end - @debugshow 3 parser.header_state + @debugshow 4 parser.header_state h = parser.header_state if h == h_general @@ -822,9 +822,9 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, h = parser.header_state while p <= len @inbounds ch = Char(bytes[p]) - @debug 3 Base.escape_string(string('\'', ch, '\'')) - @debugshow 3 strict - @debugshow 3 isheaderchar(ch) + @debug 4 Base.escape_string(string('\'', ch, '\'')) + @debugshow 4 strict + @debugshow 4 isheaderchar(ch) if ch == CR p_state = s_header_almost_done break @@ -837,7 +837,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, c = lower(ch) - @debugshow 3 h + @debugshow 4 h if h == h_general crlf = findfirst(x->(x == bCR || x == bLF), view(bytes, p:len)) @@ -859,7 +859,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, # Overflow? # Test against a conservative limit for simplicity. - @debugshow 3 Int(parser.content_length) + @debugshow 4 Int(parser.content_length) if div(ULLONG_MAX - 10, 10) < t parser.header_state = h @err(HPE_INVALID_CONTENT_LENGTH) @@ -1040,13 +1040,13 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, parser.message.upgrade = isrequest(parser) && parser.message.method == CONNECT end - @debugshow 3 parser.message.upgrade + @debugshow 4 parser.message.upgrade end elseif p_state == s_headers_done @errorifstrict(ch != LF) - if parser.isheadresponse || + if parser.message_has_no_body || parser.content_length == 0 || (parser.message.upgrade && isrequest(parser) && parser.message.method == CONNECT) @@ -1080,7 +1080,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state == s_body_identity || p_state == s_body_identity_eof - @debug 2 "parseheaders() exiting $(ParsingStateCode(p_state))" + @debug 3 "parseheaders() exiting $(ParsingStateCode(p_state))" parser.state = p_state return view(bytes, p+1:len) @@ -1106,7 +1106,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} len = length(bytes) p_state = parser.state - @debug 2 "parsebody(parser.state=$(ParsingStateCode(p_state))), " * + @debug 3 "parsebody(parser.state=$(ParsingStateCode(p_state))), " * "$len-bytes:\n" * escapelines(String(collect(bytes))) * ")" result = nobytes @@ -1115,7 +1115,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} while p < len && result == nobytes && p_state < s_message_done && p_state != s_trailer_start - @debug 3 string("top of while($p < $len) \"", + @debug 4 string("top of while($p < $len) \"", Base.escape_string(string(Char(bytes[p+1]))), "\" ", ParsingStateCode(p_state)) p += 1 @@ -1156,7 +1156,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} p_state = s_chunk_size_almost_done else unhex_val = unhex[Int(ch)+1] - @debugshow 3 unhex_val + @debugshow 4 unhex_val if unhex_val == -1 if ch == ';' || ch == ' ' p_state = s_chunk_parameters @@ -1169,7 +1169,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} t += UInt64(unhex_val) # Overflow? Test against a conservative limit for simplicity. - @debugshow 3 Int(parser.content_length) + @debugshow 4 Int(parser.content_length) if div(ULLONG_MAX - 16, 16) < t @err(HPE_INVALID_CONTENT_LENGTH) end @@ -1243,7 +1243,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} p_state == s_message_done || p_state == s_trailer_start - @debug 2 "parsebody() exiting $(ParsingStateCode(p_state))" + @debug 3 "parsebody() exiting $(ParsingStateCode(p_state))" parser.state = p_state return result, view(bytes, p+1:len) diff --git a/test/async.jl b/test/async.jl index dee6d12bd..c2d0db04d 100644 --- a/test/async.jl +++ b/test/async.jl @@ -5,6 +5,15 @@ using Base64 using HTTP.IOExtras using HTTP.request +println("async tests") + +@async while true + sleep(10) + HTTP.ConnectionPool.showpool(STDOUT) +end + + + # Tiny S3 interface... const s3region = "ap-southeast-2" const s3url = "https://s3.$s3region.amazonaws.com" @@ -24,46 +33,73 @@ end create_bucket("http.jl.test") +function dump_async_exception(e, st) + buf = IOBuffer() + write(buf, "==========\n@async exception:\n==========\n") + show(buf, "text/plain", e) + show(buf, "text/plain", st) + write(buf, "==========\n\n") + print(String(take!(buf))) +end + +@testset "async s3 $count, $http" for count in [10, 100, 1000, 2000], + http in ["http", "https"] + +println("running async s3 $count $http") + put_data_sums = Dict() -@sync for i = 1:100 - data = rand(UInt8, 100000) +sz = 10000 +ch = 100 +@sync for i = 1:count + data = rand(UInt8, sz) md5 = bytes2hex(digest(MD_MD5, data)) put_data_sums[i] = md5 - @async begin + @async try url = "$s3url/http.jl.test/file$i" - r = HTTP.open("PUT", url, ["Content-Length" => 100000]; + r = HTTP.open("PUT", url, ["Content-Length" => sz]; body_sha256=digest(MD_SHA256, data), body_md5=digest(MD_MD5, data), awsauthorization=true) do http - for n = 1:1000:100000 - write(http, data[n:n+999]) - sleep(rand(10:100)/1000) + for n = 1:ch:sz + write(http, data[n:n+(ch-1)]) + sleep(rand(1:10)/1000) end end - println("S3 put file$i") + #println("S3 put file$i") @assert strip(HTTP.header(r, "ETag"), '"') == md5 + catch e + dump_async_exception(e, catch_stacktrace()) end end get_data_sums = Dict() -@sync for i = 1:100 - @async begin +@sync for i = 1:count + @async try url = "$s3url/http.jl.test/file$i" buf = IOBuffer() - r = HTTP.open("GET", url; awsauthorization=true) do http - write(buf, http) + r = HTTP.open("GET", url; + awsauthorization=true, + reuse_limit = 120) do http + while !eof(http) + write(buf, readavailable(http)) + sleep(rand(1:10)/1000) + end end - println("S3 get file$i") + #println("S3 get file$i") md5 = bytes2hex(digest(MD_MD5, take!(buf))) @assert strip(HTTP.header(r, "ETag"), '"') == md5 get_data_sums[i] = md5 + catch e + dump_async_exception(e, catch_stacktrace()) end end -for i = 1:100 +for i = 1:count @test put_data_sums[i] == get_data_sums[i] end +end + configs = [ [], [:reuse_limit => 200], @@ -83,11 +119,13 @@ println("running async $count, 1:$num, $config, $http") result = [] @sync begin for i = 1:min(num,100) - @async begin + @async try r = HTTP.request("GET", "$http://httpbin.org/headers", ["i" => i]; config...) r = JSON.parse(String(r.body)) push!(result, r["headers"]["I"] => string(i)) + catch e + dump_async_exception(e, catch_stacktrace()) end end end @@ -102,12 +140,14 @@ println("running async $count, 1:$num, $config, $http") @sync begin for i = 1:min(num,100) - @async begin + @async try r = HTTP.request("GET", "$http://httpbin.org/stream/$i"; config...) r = String(r.body) r = split(strip(r), "\n") push!(result, length(r) => i) + catch e + dump_async_exception(e, catch_stacktrace()) end end end @@ -121,6 +161,7 @@ println("running async $count, 1:$num, $config, $http") result = [] +#= asyncmap(i->begin n = i % 20 + 1 str = "" @@ -140,11 +181,12 @@ println("running async $count, 1:$num, $config, $http") end result = [] +=# @sync begin for i = 1:num n = i % 20 + 1 - @async begin try + @async try r = nothing str = nothing url = "$http://httpbin.org/stream/$n" @@ -196,12 +238,8 @@ println("running async $count, 1:$num, $config, $http") push!(result, length(l) => n) catch e push!(result, e => n) - buf = IOBuffer() - write(buf, "==========\nAsync exception:\n==========\n$e\n") - show(buf, "text/plain", catch_stacktrace()) - write(buf, "==========\n\n") - write(STDOUT, take!(buf)) - end end + dump_async_exception(e, catch_stacktrace()) + end end end @@ -214,3 +252,4 @@ println("running async $count, 1:$num, $config, $http") end # testset +sleep(12) From d7155d172d6a2ecaac0445f93839590be5b0dadb Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 27 Dec 2017 23:46:43 +1100 Subject: [PATCH 079/182] whoops --- src/HTTP.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index e76832678..8f49a87dc 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -66,7 +66,7 @@ function stack(;redirect=true, cookies=false, canonicalizeheaders=false, retry=true, - statusexception=true + statusexception=true, kw...) NoLayer = Union From f78324a806e90dda60c011afc865c7fd85324644 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 27 Dec 2017 23:47:21 +1100 Subject: [PATCH 080/182] whoops --- src/ConnectionRequest.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 80ce5c4da..404d173cd 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -24,7 +24,7 @@ Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; connectionpool::Bool=true, kw...) where Next - Conncetion = sockettype(uri) + Connection = sockettype(uri) if connectionpool Connection = ConnectionPool.Connection{Connection} end From 10d8b8221a0e42e8167ba251a40df8d2057224a8 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 27 Dec 2017 23:50:06 +1100 Subject: [PATCH 081/182] whoops --- test/async.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/async.jl b/test/async.jl index c2d0db04d..738835ffa 100644 --- a/test/async.jl +++ b/test/async.jl @@ -15,8 +15,8 @@ end # Tiny S3 interface... -const s3region = "ap-southeast-2" -const s3url = "https://s3.$s3region.amazonaws.com" +s3region = "ap-southeast-2" +s3url = "https://s3.$s3region.amazonaws.com" s3(method, path, body=UInt8[]; kw...) = request(method, "$s3url/$path", [], body; awsauthorization=true, kw...) s3get(path; kw...) = s3("GET", path; kw...) @@ -45,6 +45,7 @@ end @testset "async s3 $count, $http" for count in [10, 100, 1000, 2000], http in ["http", "https"] +s3url = "$http://s3.$s3region.amazonaws.com" println("running async s3 $count $http") put_data_sums = Dict() From 36e871f8c5becdc207f571360d2cb1118e62958f Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:33:25 +1100 Subject: [PATCH 082/182] need eof bugfix in +MbedTLS 0.5.2 --- REQUIRE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REQUIRE b/REQUIRE index aea7cf2f0..82c8bee60 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,2 @@ julia 0.6 -MbedTLS 0.4.0 +MbedTLS 0.5.2 From b6114c77ae43e39f7ef98288281a87622aaa454c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:33:51 +1100 Subject: [PATCH 083/182] tweak AWS4AuthRequest.jl debug levels --- src/AWS4AuthRequest.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index 373ebd9b7..e0ca66f83 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -81,7 +81,7 @@ function sign_aws4!(method::String, join(sort(canonical_headers), "\n"), "\n\n", signed_headers, "\n", content_hash) - @debug 2 "AWS4 canonical_form: $canonical_form" + @debug 3 "AWS4 canonical_form: $canonical_form" canonical_hash = bytes2hex(digest(MD_SHA256, canonical_form)) @@ -89,8 +89,8 @@ function sign_aws4!(method::String, string_to_sign = "AWS4-HMAC-SHA256\n$datetime\n$scope\n$canonical_hash" signature = bytes2hex(digest(MD_SHA256, string_to_sign, signing_key)) - @debug 2 "AWS4 string_to_sign: $string_to_sign" - @debug 2 "AWS4 signature: $signature" + @debug 3 "AWS4 string_to_sign: $string_to_sign" + @debug 3 "AWS4 signature: $signature" # Append Authorization header... setkv(headers, "Authorization", string( From 6813d5461df6c618ee63875b111cc8a6b2d90fce Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:34:22 +1100 Subject: [PATCH 084/182] DEBUG_LEVEL = 0 by default --- src/HTTP.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 8f49a87dc..a9cf55c3a 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -5,7 +5,7 @@ using MbedTLS import MbedTLS.SSLContext -const DEBUG_LEVEL = 1 +const DEBUG_LEVEL = 0 const minimal = false include("compat.jl") From 090148d1e47d26f153b686efaea463bf0a247a8c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:34:54 +1100 Subject: [PATCH 085/182] added lockedby() to compat.jl --- src/compat.jl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/compat.jl b/src/compat.jl index 568726d39..6a01bb729 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -27,3 +27,9 @@ if VERSION < v"0.7.0-DEV.2575" else import Dates end + +@static if VERSION >= v"0.7.0-DEV.2915" + lockedby(l) = l.locked_by +else + lockedby(l) = get(l.locked_by) +end From cd54d0f4f973c2fe56ae468e337c7499fb029929 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:36:47 +1100 Subject: [PATCH 086/182] type annotation --- src/Messages.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Messages.jl b/src/Messages.jl index 726fd8e1b..f5cee3420 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -372,7 +372,7 @@ function Base.show(io::IO, m::Message) if get(io, :compact, false) print(io, compactstartline(m)) if m isa Response - print(io, " <= (", compactstartline(m.request), ")") + print(io, " <= (", compactstartline(m.request::Request), ")") end return end From 28dc704524c70e5becebbbe29319b1ab8cb01b92 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:38:06 +1100 Subject: [PATCH 087/182] type annotation --- src/HTTPStreams.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index d28b74978..5b91c062f 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -43,7 +43,8 @@ end function configure_parser(http::HTTPStream{Response}) reset!(http.parser) - if http.message.request.method in ("HEAD", "CONNECT") + req = http.message.request::Request + if req.method in ("HEAD", "CONNECT") setnobody(http.parser) end end From ebbe960496e378944d474b3c2932ae465f9dbd43 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:38:33 +1100 Subject: [PATCH 088/182] cosmetics --- src/ConnectionRequest.jl | 2 +- src/RedirectRequest.jl | 2 +- src/RetryRequest.jl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 404d173cd..a19109789 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -33,7 +33,7 @@ function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; try return request(Next, io, req, body; kw...) catch e - @debug 1 "ConnectionLayer $e. Closing: $io" + @debug 1 "❗️ ConnectionLayer $e. Closing: $io" close(io) rethrow(e) end diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index a9a670d7a..067df327d 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -40,7 +40,7 @@ function request(::Type{RedirectLayer{Next}}, headers = Header[] end - @debug 1 "Redirecting to: $uri" + @debug 1 "➡️ Redirect: $uri" count += 1 end diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 5a340a2b7..a9c858a28 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -21,7 +21,7 @@ isrecoverable(e::Exception) = false isrecoverable(e, req) = isrecoverable(e) && !(req.body === body_was_streamed) && !(req.response.body === body_was_streamed) && - (@debug 1 "Retring on $e: $(sprint(showcompact, req))"; + (@debug 1 "🔄 Retry $e: $(sprint(showcompact, req))"; true) From 0d1056a9d14db3919fd520175dd60a8dce05e9df Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:39:10 +1100 Subject: [PATCH 089/182] reenstate verbose= option --- src/StreamRequest.jl | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index b3a48c148..1b6cbc2ea 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -7,7 +7,7 @@ using ..Messages using ..HTTPStreams import ..ConnectionPool using ..MessageRequest -import ..@debugshort, ..DEBUG_LEVEL +import ..@debugshort, ..DEBUG_LEVEL, ..printlncompact abstract type StreamLayer <: Layer end export StreamLayer @@ -31,21 +31,33 @@ Run the `Request` in a background task if response body is a stream. function request(::Type{StreamLayer}, io::IO, req::Request, body; response_stream=nothing, iofunction=nothing, + verbose::Int=0, kw...)::Response - write(io, req) - - @debugshort 2 req - @debug 3 req + verbose == 1 && printlncompact(req) + verbose >= 2 && println(req) http = HTTPStream(io, req, ConnectionPool.getparser(io)) if iofunction != nothing + write(io, req) iofunction(http) else + write(io, req) if req.body === body_is_a_stream writebody(http, req, body) end +#= FIXME + @async begin + write(io, req) + if req.body === body_is_a_stream + writebody(http, req, body) + end + writeend(http) + closewrite(http.stream) + end +=# + readheaders(http) if response_stream == nothing @@ -58,8 +70,8 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; close(http) - @debugshort 2 req.response - @debug 3 req.response + verbose == 1 && printlncompact(req.response) + verbose >= 2 && println(req.response) return req.response end From 16084d0c92fcd60576f0740ccbb8af3e3239c05c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:48:00 +1100 Subject: [PATCH 090/182] ConnectionPool.jl enhancements. Remove NonReentrantLock for simplicity, testing indicates that locking is ok now, retain assertions Add pipeline_limit option. Split startread() code out of closewrite(). Cosmetics move localport and peerport to IOExtras.jl --- src/ConnectionPool.jl | 195 ++++++++++++++++++++++++------------------ src/IOExtras.jl | 22 ++++- src/debug.jl | 5 +- 3 files changed, 136 insertions(+), 86 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 850561593..c42866afe 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -4,41 +4,17 @@ export getconnection, getparser using ..IOExtras -import ..@debug, ..DEBUG_LEVEL +import ..@debug, ..DEBUG_LEVEL, ..taskid import MbedTLS.SSLContext import ..Connect: getconnection, getparser import ..Parsers.Parser -const max_duplicates = 8 +const duplicate_connection_limit = 8 +const default_pipeline_limit = 16 const nolimit = typemax(Int) - -macro lockassert(cond) - DEBUG_LEVEL > 1 ? esc(:(@assert $cond)) : :() #FIXME -end - -struct NonReentrantLock - l::ReentrantLock -end - -NonReentrantLock() = NonReentrantLock(ReentrantLock()) - -Base.islocked(l::NonReentrantLock) = islocked(l.l) -havelock(l) = islocked(l) && l.l.locked_by == current_task() - -function Base.lock(l::NonReentrantLock) - @lockassert !havelock(l) - lock(l.l) - @lockassert l.l.reentrancy_cnt == 1 -end - -function Base.unlock(l::NonReentrantLock) - @lockassert havelock(l) - @lockassert l.l.reentrancy_cnt == 1 - unlock(l.l) -end - +const force_lock_assert = true const nobytes = view(UInt8[], 1:0) const ByteView = typeof(nobytes) @@ -67,21 +43,24 @@ A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. mutable struct Connection{T <: IO} <: IO host::String port::String + pipeline_limit::Int + peerport::UInt16 + localport::UInt16 io::T excess::ByteView writecount::Int readcount::Int - writelock::NonReentrantLock - readlock::NonReentrantLock + writelock::ReentrantLock + readlock::ReentrantLock parser::Parser end -Connection{T}(host::AbstractString, port::AbstractString, io::T) where T <: IO = - Connection{T}(host, port, io, view(UInt8[], 1:0), 0, 0, - NonReentrantLock(), NonReentrantLock(), Parser()) - -const noconnection = Connection{TCPSocket}("","",TCPSocket()) +Connection{T}(host::AbstractString, port::AbstractString, + pipeline_limit::Int, io::T) where T <: IO = + Connection{T}(host, port, pipeline_limit, + peerport(io), localport(io), io, view(UInt8[], 1:0), 0, 0, + ReentrantLock(), ReentrantLock(), Parser()) getparser(c::Connection) = c.parser @@ -91,22 +70,40 @@ Base.unsafe_write(c::Connection, p::Ptr{UInt8}, n::UInt) = unsafe_write(c.io, p, n) Base.isopen(c::Connection) = isopen(c.io) -Base.eof(c::Connection) = isempty(c.excess) && eof(c.io) + +function Base.eof(c::Connection) + if nb_available(c) > 0 + return false + end + @debug 3 "eof(::Connection) calling eof($typeof(c.io)): $c" + return eof(c.io) +end + Base.nb_available(c::Connection) = !isempty(c.excess) ? length(c.excess) : nb_available(c.io) Base.isreadable(c::Connection) = havelock(c.readlock) Base.iswritable(c::Connection) = havelock(c.writelock) +macro lockassert(cond) + DEBUG_LEVEL > 0 || force_lock_assert ? esc(:(@assert $cond)) : :() +end + +function havelock(l) + @lockassert l.reentrancy_cnt <= 1 + islocked(l) && l.locked_by == current_task() +end + + function Base.readavailable(c::Connection)::ByteView @lockassert isreadable(c) if !isempty(c.excess) bytes = c.excess - @debug 3 "read $(length(bytes))-bytes from excess buffer." + @debug 3 "↩️ read $(length(bytes))-bytes from excess buffer." c.excess = nobytes else bytes = byteview(readavailable(c.io)) - @debug 3 "read $(length(bytes))-bytes from $(typeof(c.io))" + @debug 3 "⬅️ read $(length(bytes))-bytes from $(typeof(c.io))" end return bytes end @@ -137,18 +134,41 @@ function IOExtras.closewrite(c::Connection) @lockassert iswritable(c) seq = c.writecount - c.writecount += 1 ;@debug 2 "Write done: $c" + c.writecount += 1 ;@debug 2 "🗣 Write done: $c" unlock(c.writelock) notify(poolcondition) + if !isreadable(c) + startread(c, seq) + end + @lockassert isreadable(c) +end + + +""" + startread(::Connection) + +Wait for prior pending reads to complete, then lock the readlock. +""" + +function startread(c::Connection) + @lockassert iswritable(c) + + startread(c, c.writecount) +end + +function startread(c::Connection, seq::Int) + @lockassert !isreadable(c) + lock(c.readlock) - # Wait for prior pending reads to complete... while c.readcount != seq if !isopen(c) && nb_available(c) == 0 + # If there is nothing left to read, + # then unlocking sequence is irrelevant. break end - unlock(c.readlock) ;@debug 1 "Waiting to read seq=$seq: $c" - yield() + unlock(c.readlock) + yield() ;@debug 1 "⏳ seq=$(lpad(seq,3)): $c" lock(c.readlock) end @lockassert isreadable(c) @@ -166,15 +186,15 @@ Increment `readcount` and wake up tasks waiting in `closewrite`. function IOExtras.closeread(c::Connection) @lockassert isreadable(c) - c.readcount += 1 ;@debug 2 "Read done: $c" - unlock(c.readlock) + c.readcount += 1 + unlock(c.readlock) ;@debug 2 "✉️ Read done: $c" notify(poolcondition) return end function Base.close(c::Connection) - close(c.io) ;@debug 2 "Closed: $c" + close(c.io) ;@debug 2 "🚫 Closed: $c" if isreadable(c) purge(c) closeread(c) @@ -242,12 +262,15 @@ Find `Connections` in the `pool` that are ready for writing. function findwritable(T::Type, host::AbstractString, port::AbstractString, - reuse_limit::Int=nolimit) + pipeline_limit::Int, + reuse_limit::Int) filter(c->(typeof(c.io) == T && c.host == host && c.port == port && + c.pipeline_limit == pipeline_limit && c.writecount < reuse_limit && + c.writecount - c.readcount < pipeline_limit && !islocked(c.writelock) && isopen(c.io)), pool) end @@ -282,11 +305,13 @@ Find all `Connections` in the `pool` for `host` and `port`. function findall(T::Type, host::AbstractString, - port::AbstractString) + port::AbstractString, + pipeline_limit::Int) filter(c->(typeof(c.io) == T && c.host == host && c.port == port && + c.pipeline_limit == pipeline_limit && isopen(c.io)), pool) end @@ -298,10 +323,10 @@ Remove closed connections from `pool`. """ function purge() while (i = findfirst(x->!isopen(x.io) && - x.readcount == x.writecount, pool)) > 0 + x.readcount == x.writecount, pool)) > 0 c = pool[i] purge(c) - deleteat!(pool, i) ;@debug 1 "Deleted: $c" + deleteat!(pool, i) ;@debug 1 "🗑 Deleted: $c" end end @@ -316,6 +341,7 @@ or create a new `Connection` if required. function getconnection(::Type{Connection{T}}, host::AbstractString, port::AbstractString; + pipeline_limit::Int = default_pipeline_limit, reuse_limit::Int = nolimit, kw...)::Connection{T} where T <: IO @@ -336,28 +362,28 @@ function getconnection(::Type{Connection{T}}, purge() # Try to find a connection with no active readers or writers... - writable = findwritable(T, host, port, reuse_limit) + writable = findwritable(T, host, port, pipeline_limit, reuse_limit) idle = filter(c->!islocked(c.readlock), writable) if !isempty(idle) - c = rand(idle) ;@debug 2 "Idle: $c" + c = rand(idle) ;@debug 1 "♻️ Idle: $c" lock(c.writelock) return c end # If there are not too many duplicates for this host, # create a new connection... - busy = findall(T, host, port) - if length(busy) < max_duplicates + busy = findall(T, host, port, pipeline_limit) + if length(busy) < duplicate_connection_limit io = getconnection(T, host, port; kw...) - c = Connection{T}(host, port, io) ;@debug 1 "New: $c" + c = Connection{T}(host, port, pipeline_limit, io) lock(c.writelock) - push!(pool, c) + push!(pool, c) ;@debug 1 "🔗 New: $c" return c end # Share a connection that has active readers... if !isempty(writable) - c = rand(writable) ;@debug 2 "Shared: $c" + c = rand(writable) ;@debug 1 "⇆ Shared: $c" lock(c.writelock) return c end @@ -373,36 +399,37 @@ end function Base.show(io::IO, c::Connection) - print(io, c.host, ":", - c.port != "" ? c.port : Int(peerport(c)), ":", - Int(localport(c)), ", ", - typeof(c.io), ", ", tcpstatus(c), ", ", - length(c.excess), "-byte excess, writes/reads: ", - c.writecount, "/", c.readcount, - islocked(c.readlock) ? ", readlock" : "", - islocked(c.writelock) ? ", writelock" : "") + nwaiting = nb_available(tcpsocket(c.io)) + print( + io, + tcpstatus(c), " ", + lpad(c.writecount,3),"↑", islocked(c.writelock) ? "🔒 " : " ", + lpad(c.readcount,3), "↓", islocked(c.readlock) ? "🔒 " : " ", + c.host, ":", + c.port != "" ? c.port : Int(c.peerport), ":", Int(c.localport), + ", ≣", c.pipeline_limit, + length(c.excess) > 0 ? ", $(length(c.excess))-byte excess" : "", + nwaiting > 0 ? ", $nwaiting bytes waiting" : "", + DEBUG_LEVEL > 0 ? ", $(Base._fd(tcpsocket(c.io)))" : "", + DEBUG_LEVEL > 0 && + islocked(c.writelock) ? ", write task: $(taskid(c.writelock))" : "", + DEBUG_LEVEL > 0 && + islocked(c.readlock) ? ", read task: $(taskid(c.readlock))" : "") end -tcpsocket(c::Connection{SSLContext})::TCPSocket = c.io.bio -tcpsocket(c::Connection{TCPSocket})::TCPSocket = c.io - -localport(c::Connection) = try !isopen(tcpsocket(c)) ? 0 : - VERSION > v"0.7.0-DEV" ? - getsockname(tcpsocket(c))[2] : - Base._sockname(tcpsocket(c), true)[2] - catch - 0 - end - -peerport(c::Connection) = try !isopen(tcpsocket(c)) ? 0 : - VERSION > v"0.7.0-DEV" ? - getpeername(tcpsocket(c))[2] : - Base._sockname(tcpsocket(c), false)[2] - catch - 0 - end - -tcpstatus(c::Connection) = Base.uv_status_string(tcpsocket(c)) + +function tcpstatus(c::Connection) + s = Base.uv_status_string(tcpsocket(c.io)) + if s == "connecting" return "🔜🔗" + elseif s == "open" return "🔗 " + elseif s == "active" return "🔁 " + elseif s == "paused" return "⏸ " + elseif s == "closing" return "🔜💀" + elseif s == "closed" return "💀 " + else + return s + end +end function showpool(io::IO) lock(poollock) diff --git a/src/IOExtras.jl b/src/IOExtras.jl index 2b9b163c7..3073033ec 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -1,6 +1,6 @@ module IOExtras -export unread!, closeread, closewrite +export unread!, closeread, closewrite, tcpsocket, localport, peerport """ unread!(::IO, bytes) @@ -62,4 +62,24 @@ closewrite(io) = nothing closeread(io) = close(io) +using MbedTLS.SSLContext +tcpsocket(io::SSLContext)::TCPSocket = io.bio +tcpsocket(io::TCPSocket)::TCPSocket = io + +localport(io) = try !isopen(tcpsocket(io)) ? 0 : + VERSION > v"0.7.0-DEV" ? + getsockname(tcpsocket(io))[2] : + Base._sockname(tcpsocket(io), true)[2] + catch + 0 + end + +peerport(io) = try !isopen(tcpsocket(io)) ? 0 : + VERSION > v"0.7.0-DEV" ? + getpeername(tcpsocket(io))[2] : + Base._sockname(tcpsocket(io), false)[2] + catch + 0 + end + end diff --git a/src/debug.jl b/src/debug.jl index f76c3e49f..222f58f38 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -1,4 +1,5 @@ -taskid() = hex(hash(current_task()) & 0xffff, 4) +taskid(t=current_task()) = hex(hash(t) & 0xffff, 4) +taskid(l::ReentrantLock) = islocked(l) ? taskid(lockedby(l)) : "" macro debug(n::Int, s) DEBUG_LEVEL >= n ? :(println("DEBUG: ", taskid(), " ", $(esc(s)))) : @@ -20,6 +21,8 @@ macro debugshort(n::Int, s) :() end +printlncompact(x) = println(sprint(showcompact, x)) + #= macro src() @static if VERSION >= v"0.7-" && length(:(@test).args) == 2 From a241651480d4c1ab63d915d00ad53ded855c72fb Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 11:52:20 +1100 Subject: [PATCH 091/182] test tweaks --- test/async.jl | 66 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/test/async.jl b/test/async.jl index 738835ffa..bfbb5d81f 100644 --- a/test/async.jl +++ b/test/async.jl @@ -7,13 +7,13 @@ using HTTP.request println("async tests") -@async while true - sleep(10) +stop_pool_dump = false + +@async while !stop_pool_dump HTTP.ConnectionPool.showpool(STDOUT) + sleep(3) end - - # Tiny S3 interface... s3region = "ap-southeast-2" s3url = "https://s3.$s3region.amazonaws.com" @@ -51,8 +51,9 @@ println("running async s3 $count $http") put_data_sums = Dict() sz = 10000 ch = 100 + @sync for i = 1:count - data = rand(UInt8, sz) + data = rand(UInt8(65):UInt8(75), sz) md5 = bytes2hex(digest(MD_MD5, data)) put_data_sums[i] = md5 @async try @@ -73,30 +74,39 @@ ch = 100 end end + get_data_sums = Dict() -@sync for i = 1:count - @async try - url = "$s3url/http.jl.test/file$i" - buf = IOBuffer() - r = HTTP.open("GET", url; - awsauthorization=true, - reuse_limit = 120) do http - while !eof(http) - write(buf, readavailable(http)) - sleep(rand(1:10)/1000) +@sync begin + for i = 1:count + @async try + url = "$s3url/http.jl.test/file$i" + buf = IOBuffer() + r = HTTP.open("GET", url; + verbose=1, + #pipeline_limit=8, + reuse_limit = 90, + awsauthorization=true) do http + truncate(buf, 0) # in case of retry! + while !eof(http) + write(buf, readavailable(http)) + sleep(rand(1:10)/1000) + end end + #println("S3 get file$i") + bytes = take!(buf) + md5 = bytes2hex(digest(MD_MD5, bytes)) + get_data_sums[i] = (md5, strip(HTTP.header(r, "ETag"), '"')) + catch e + dump_async_exception(e, catch_stacktrace()) + rethrow(e) end - #println("S3 get file$i") - md5 = bytes2hex(digest(MD_MD5, take!(buf))) - @assert strip(HTTP.header(r, "ETag"), '"') == md5 - get_data_sums[i] = md5 - catch e - dump_async_exception(e, catch_stacktrace()) end end for i = 1:count - @test put_data_sums[i] == get_data_sums[i] + a, b = get_data_sums[i] + @test a == b + @test a == put_data_sums[i] end end @@ -104,12 +114,11 @@ end configs = [ [], [:reuse_limit => 200], - [:reuse_limit => 100], - [:reuse_limit => 10] + [:reuse_limit => 50] ] -@testset "async $count, $num, $config, $http" for count in 1:3, - num in [10, 100, 1000, 2000], +@testset "async $count, $num, $config, $http" for count in 1:1, + num in [100, 1000, 2000], config in configs, http in ["http", "https"] @@ -254,3 +263,8 @@ println("running async $count, 1:$num, $config, $http") end # testset sleep(12) +stop_pool_dump=true + +HTTP.ConnectionPool.showpool(STDOUT) + +println("async tests done") From 701d2f4e51cfa5e75a8439e568e935774bcbfe52 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 29 Dec 2017 15:04:38 +1100 Subject: [PATCH 092/182] added TimeoutLayer --- src/Connect.jl | 3 ++- src/ConnectionPool.jl | 19 ++++++++++++++++--- src/HTTP.jl | 5 ++++- src/TimeoutRequest.jl | 43 +++++++++++++++++++++++++++++++++++++++++++ test/async.jl | 22 +++++++++++++--------- 5 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 src/TimeoutRequest.jl diff --git a/src/Connect.jl b/src/Connect.jl index dbb01cf51..bf5407ab8 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -1,6 +1,6 @@ module Connect -export getconnection, getparser +export getconnection, getparser, inactiveseconds using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! @@ -47,5 +47,6 @@ end getparser(::IO) = Parser() +inactiveseconds(::IO)= Float64(0) end # module Connect diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index c42866afe..4ca717908 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -1,12 +1,12 @@ module ConnectionPool -export getconnection, getparser +export getconnection, getparser, inactiveseconds using ..IOExtras import ..@debug, ..DEBUG_LEVEL, ..taskid import MbedTLS.SSLContext -import ..Connect: getconnection, getparser +import ..Connect: getconnection, getparser, inactiveseconds import ..Parsers.Parser @@ -52,6 +52,7 @@ mutable struct Connection{T <: IO} <: IO readcount::Int writelock::ReentrantLock readlock::ReentrantLock + timestamp::Float64 parser::Parser end @@ -60,7 +61,7 @@ Connection{T}(host::AbstractString, port::AbstractString, pipeline_limit::Int, io::T) where T <: IO = Connection{T}(host, port, pipeline_limit, peerport(io), localport(io), io, view(UInt8[], 1:0), 0, 0, - ReentrantLock(), ReentrantLock(), Parser()) + ReentrantLock(), ReentrantLock(), 0, Parser()) getparser(c::Connection) = c.parser @@ -85,6 +86,14 @@ Base.isreadable(c::Connection) = havelock(c.readlock) Base.iswritable(c::Connection) = havelock(c.writelock) +function inactiveseconds(c::Connection)::Float64 + if !islocked(c.readlock) + return Float64(0) + end + return time() - c.timestamp +end + + macro lockassert(cond) DEBUG_LEVEL > 0 || force_lock_assert ? esc(:(@assert $cond)) : :() end @@ -105,6 +114,7 @@ function Base.readavailable(c::Connection)::ByteView bytes = byteview(readavailable(c.io)) @debug 3 "⬅️ read $(length(bytes))-bytes from $(typeof(c.io))" end + c.timestamp = time() return bytes end @@ -160,6 +170,7 @@ end function startread(c::Connection, seq::Int) @lockassert !isreadable(c) + c.timestamp = time() lock(c.readlock) while c.readcount != seq if !isopen(c) && nb_available(c) == 0 @@ -409,6 +420,8 @@ function Base.show(io::IO, c::Connection) c.port != "" ? c.port : Int(c.peerport), ":", Int(c.localport), ", ≣", c.pipeline_limit, length(c.excess) > 0 ? ", $(length(c.excess))-byte excess" : "", + inactiveseconds(c) > 5 ? + ", inactive $(round(inactiveseconds(c),1))s" : "", nwaiting > 0 ? ", $nwaiting bytes waiting" : "", DEBUG_LEVEL > 0 ? ", $(Base._fd(tcpsocket(c.io)))" : "", DEBUG_LEVEL > 0 && diff --git a/src/HTTP.jl b/src/HTTP.jl index a9cf55c3a..e2cd6dd93 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -51,6 +51,7 @@ include("BasicAuthRequest.jl"); using .BasicAuthRequest include("AWS4AuthRequest.jl"); using .AWS4AuthRequest 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 @@ -67,6 +68,7 @@ function stack(;redirect=true, canonicalizeheaders=false, retry=true, statusexception=true, + timeout=0, kw...) NoLayer = Union @@ -80,8 +82,9 @@ function stack(;redirect=true, (retry ? RetryLayer : NoLayer){ (statusexception ? ExceptionLayer : NoLayer){ ConnectionPoolLayer{ + (timeout > 0 ? TimeoutLayer : NoLayer){ StreamLayer - }}}}}}}}} + }}}}}}}}}} end else diff --git a/src/TimeoutRequest.jl b/src/TimeoutRequest.jl new file mode 100644 index 000000000..1d3071cf1 --- /dev/null +++ b/src/TimeoutRequest.jl @@ -0,0 +1,43 @@ +module TimeoutRequest + +import ..Layer, ..request, ..lockedby +using ..ConnectionPool +import ..@debug, ..DEBUG_LEVEL + + +abstract type TimeoutLayer{Next <: Layer} <: Layer end +export TimeoutLayer + + +""" + request(TimeoutLayer{Connection, Next}, ::IO, ::Request, body) + +Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. +""" + +function request(::Type{TimeoutLayer{Next}}, io::IO, req, body; + timeout::Int=60, kw...) where Next + + wait_for_timeout = Ref{Bool}(true) + request_task = current_task() + + @async while wait_for_timeout[] + if islocked(io.readlock) && + lockedby(io.readlock) == request_task && + inactiveseconds(io) > timeout + close(io) + @debug 0 "💥 Read inactive > $(timeout)s: $io" + break + end + sleep(10) + end + + try + return request(Next, io, req, body; kw...) + finally + wait_for_timeout[] = false + end +end + + +end # module TimeoutRequest diff --git a/test/async.jl b/test/async.jl index bfbb5d81f..a5c104ed2 100644 --- a/test/async.jl +++ b/test/async.jl @@ -11,7 +11,7 @@ stop_pool_dump = false @async while !stop_pool_dump HTTP.ConnectionPool.showpool(STDOUT) - sleep(3) + sleep(20) end # Tiny S3 interface... @@ -45,12 +45,17 @@ end @testset "async s3 $count, $http" for count in [10, 100, 1000, 2000], http in ["http", "https"] +global s3url s3url = "$http://s3.$s3region.amazonaws.com" println("running async s3 $count $http") put_data_sums = Dict() -sz = 10000 +sz = 1000 ch = 100 +conf = [:reuse_limit => 90, + :verbose => 0, + :pipeline_limit => 32, + :timeout => 20] @sync for i = 1:count data = rand(UInt8(65):UInt8(75), sz) @@ -61,7 +66,8 @@ ch = 100 r = HTTP.open("PUT", url, ["Content-Length" => sz]; body_sha256=digest(MD_SHA256, data), body_md5=digest(MD_MD5, data), - awsauthorization=true) do http + awsauthorization=true, + conf...) do http for n = 1:ch:sz write(http, data[n:n+(ch-1)]) sleep(rand(1:10)/1000) @@ -71,6 +77,7 @@ ch = 100 @assert strip(HTTP.header(r, "ETag"), '"') == md5 catch e dump_async_exception(e, catch_stacktrace()) + rethrow(e) end end @@ -82,10 +89,8 @@ get_data_sums = Dict() url = "$s3url/http.jl.test/file$i" buf = IOBuffer() r = HTTP.open("GET", url; - verbose=1, - #pipeline_limit=8, - reuse_limit = 90, - awsauthorization=true) do http + awsauthorization=true, + conf...) do http truncate(buf, 0) # in case of retry! while !eof(http) write(buf, readavailable(http)) @@ -213,14 +218,12 @@ println("running async $count, 1:$num, $config, $http") str = String(read(s)) break catch e -# st = catch_stacktrace() if attempt == 10 || !HTTP.RetryRequest.isrecoverable(e) rethrow(e) end buf = IOBuffer() println(buf, "$i retry $e $attempt...") - #show(buf, "text/plain", st) write(STDOUT, take!(buf)) sleep(0.1) end @@ -249,6 +252,7 @@ println("running async $count, 1:$num, $config, $http") catch e push!(result, e => n) dump_async_exception(e, catch_stacktrace()) + rethrow(e) end end end From 21b942ea6dab0105e597b37c66460e48f9e50ac8 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 31 Dec 2017 08:41:53 +1100 Subject: [PATCH 093/182] Concurrency enhancements to Handle RFC7230 6.5, early termination. - Add struct ConnectionPool.Transaction <: IO to manage state of a single transaction (Request/Response) on a shared Connection. The Transaction struct holds a sequence number and a reference to the connection. - Decouple read/write lock/unlock sequecing in ConnectionPool. Each Transaction now knows its sequence number. closewrite() no longer calls startread(). Reading can now begin before writing has finished, or start much later without the need to atomically unlockread+lockwrite. - Remove redundant Connection.writelock. - Handle RFC7230 6.5, early send body termination on error: send body runs in an @async task and closeread(::HTTPStream) checks for error code and "Connection: close". - iserror(::Messages) false if status is 0 (not yet recieved) - Don't write(::IO, ::Request) the whole request in StreamLayer, instead startwrite(::HTTPStream) sends the headers automatically and StreamRequest.jl sends the body (static or streamed). - Callstartwrite(::HTTPStream) in HTTPStream{Response} constructor to send headers automatically when stream is created. - Rename readheaders(::HTTPStream) -> startread(::HTTPStream) - Rename writeend(::HTTPStream) -> closewrite(::HTTPStream) - Rename close(::HTTPStream) -> closeread(::HTTPStream), StreamLayer now calls closeread (not close), ConnectionPoolLayer now calls close when pooling is disabled (i.e. one request per connection mode). - Add @require from https://github.com/JuliaLang/julia/pull/15495/files - IOExtras.jl stubs for startwrite(), startread() and closeread() --- src/ConnectionPool.jl | 220 ++++++++++++++++++++------------------- src/ConnectionRequest.jl | 12 ++- src/HTTPStreams.jl | 77 ++++++++++---- src/IOExtras.jl | 11 +- src/Messages.jl | 3 +- src/StreamRequest.jl | 35 ++++--- src/TimeoutRequest.jl | 6 +- src/debug.jl | 22 ++++ 8 files changed, 232 insertions(+), 154 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 4ca717908..c9c59f610 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -4,7 +4,7 @@ export getconnection, getparser, inactiveseconds using ..IOExtras -import ..@debug, ..DEBUG_LEVEL, ..taskid +import ..@debug, ..DEBUG_LEVEL, ..taskid, ..@require, ..precondition_error import MbedTLS.SSLContext import ..Connect: getconnection, getparser, inactiveseconds import ..Parsers.Parser @@ -14,14 +14,18 @@ const duplicate_connection_limit = 8 const default_pipeline_limit = 16 const nolimit = typemax(Int) -const force_lock_assert = true - const nobytes = view(UInt8[], 1:0) const ByteView = typeof(nobytes) byteview(bytes::ByteView) = bytes byteview(bytes)::ByteView = view(bytes, 1:length(bytes)) +function havelock(l) + @assert l.reentrancy_cnt <= 1 + islocked(l) && l.locked_by == current_task() +end + + """ Connection{T <: IO} @@ -40,7 +44,7 @@ A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. - `parser::Parser`, reuse a `Parser` when this `Connection` is reused. """ -mutable struct Connection{T <: IO} <: IO +mutable struct Connection{T <: IO} host::String port::String pipeline_limit::Int @@ -48,43 +52,35 @@ mutable struct Connection{T <: IO} <: IO localport::UInt16 io::T excess::ByteView + writebusy::Bool writecount::Int readcount::Int - writelock::ReentrantLock readlock::ReentrantLock timestamp::Float64 parser::Parser end +struct Transaction{T <: IO} <: IO + c::Connection{T} + sequence::Int +end + Connection{T}(host::AbstractString, port::AbstractString, pipeline_limit::Int, io::T) where T <: IO = Connection{T}(host, port, pipeline_limit, - peerport(io), localport(io), io, view(UInt8[], 1:0), 0, 0, - ReentrantLock(), ReentrantLock(), 0, Parser()) - - -getparser(c::Connection) = c.parser + peerport(io), localport(io), io, view(UInt8[], 1:0), + 0, 0, 0, ReentrantLock(), 0, Parser()) - -Base.unsafe_write(c::Connection, p::Ptr{UInt8}, n::UInt) = - unsafe_write(c.io, p, n) - -Base.isopen(c::Connection) = isopen(c.io) - -function Base.eof(c::Connection) - if nb_available(c) > 0 - return false - end - @debug 3 "eof(::Connection) calling eof($typeof(c.io)): $c" - return eof(c.io) +function Transaction{T}(c::Connection{T}) where T <: IO + r = Transaction{T}(c, c.writecount) + startwrite(r) + return r end -Base.nb_available(c::Connection) = !isempty(c.excess) ? length(c.excess) : - nb_available(c.io) -Base.isreadable(c::Connection) = havelock(c.readlock) -Base.iswritable(c::Connection) = havelock(c.writelock) +getparser(t::Transaction) = t.c.parser +inactiveseconds(t::Transaction) = inactiveseconds(t.c) function inactiveseconds(c::Connection)::Float64 if !islocked(c.readlock) @@ -94,134 +90,147 @@ function inactiveseconds(c::Connection)::Float64 end -macro lockassert(cond) - DEBUG_LEVEL > 0 || force_lock_assert ? esc(:(@assert $cond)) : :() -end +Base.unsafe_write(t::Transaction, p::Ptr{UInt8}, n::UInt) = + unsafe_write(t.c.io, p, n) -function havelock(l) - @lockassert l.reentrancy_cnt <= 1 - islocked(l) && l.locked_by == current_task() +Base.isopen(t::Transaction) = isopen(t.c.io) + +function Base.eof(t::Transaction) + @require isreadable(t) + if nb_available(t) > 0 + return false + end ;@debug 3 "eof(::Transaction) -> eof($typeof(c.io)): $t" + return eof(t.c.io) end +Base.nb_available(t::Transaction) = nb_available(t.c) +Base.nb_available(c::Connection) = + !isempty(c.excess) ? length(c.excess) : nb_available(c.io) + +Base.isreadable(t::Transaction) = islocked(t.c.readlock) && + t.c.readcount == t.sequence + +Base.iswritable(t::Transaction) = t.c.writebusy && + t.c.writecount == t.sequence -function Base.readavailable(c::Connection)::ByteView - @lockassert isreadable(c) - if !isempty(c.excess) - bytes = c.excess + +function Base.readavailable(t::Transaction)::ByteView + @require isreadable(t) + if !isempty(t.c.excess) + bytes = t.c.excess @debug 3 "↩️ read $(length(bytes))-bytes from excess buffer." - c.excess = nobytes + t.c.excess = nobytes else - bytes = byteview(readavailable(c.io)) - @debug 3 "⬅️ read $(length(bytes))-bytes from $(typeof(c.io))" + bytes = byteview(readavailable(t.c.io)) + @debug 3 "⬅️ read $(length(bytes))-bytes from $(typeof(t.c.io))" end - c.timestamp = time() + t.c.timestamp = time() return bytes end """ - unread!(::Connection, bytes) + unread!(::Transaction, bytes) Push bytes back into a connection's `excess` buffer (to be returned by the next read). """ -function IOExtras.unread!(c::Connection, bytes::ByteView) - @lockassert isreadable(c) - c.excess = bytes +function IOExtras.unread!(t::Transaction, bytes::ByteView) + @require isreadable(t) + t.c.excess = bytes +end + + +function IOExtras.startwrite(t::Transaction) + @require !t.c.writebusy + t.c.writebusy = true end """ - closewrite(::Connection) + closewrite(::Transaction) -Signal that an entire Request Message has been written to the `Connection`. +Signal that an entire Request Message has been written to the `Transaction`. Increment `writecount` and wait for pending reads to complete. """ -function IOExtras.closewrite(c::Connection) - @lockassert iswritable(c) +function IOExtras.closewrite(t::Transaction) + @require iswritable(t) - seq = c.writecount - c.writecount += 1 ;@debug 2 "🗣 Write done: $c" - unlock(c.writelock) + t.c.writecount += 1 ;@debug 2 "🗣 Write done: $t" + t.c.writebusy = false notify(poolcondition) - if !isreadable(c) - startread(c, seq) - end - @lockassert isreadable(c) + @assert !iswritable(t) end """ - startread(::Connection) + startread(::Transaction) Wait for prior pending reads to complete, then lock the readlock. """ -function startread(c::Connection) - @lockassert iswritable(c) - - startread(c, c.writecount) -end - -function startread(c::Connection, seq::Int) - @lockassert !isreadable(c) - - c.timestamp = time() - lock(c.readlock) - while c.readcount != seq - if !isopen(c) && nb_available(c) == 0 - # If there is nothing left to read, - # then unlocking sequence is irrelevant. - break - end - unlock(c.readlock) - yield() ;@debug 1 "⏳ seq=$(lpad(seq,3)): $c" - lock(c.readlock) - end - @lockassert isreadable(c) +function IOExtras.startread(t::Transaction) + @require !isreadable(t) + + t.c.timestamp = time() + lock(t.c.readlock) + while t.c.readcount != t.sequence +# if !isopen(t) && nb_available(t) == 0 +# # If there is nothing left to read, +# # then unlocking sequence is irrelevant. +# FIXME break +# end + unlock(t.c.readlock) + yield() ;@debug 1 "⏳ seq=$(lpad(seq,3)): $t" + lock(t.c.readlock) + end ;@debug 1 "👁 Start read: $t" + @assert isreadable(t) return end +ensurereadable(t::Transaction) = if !isreadable(t) startread(t) end + """ - closeread(::Connection) + closeread(::Transaction) -Signal that an entire Response Message has been read from the `Connection`. +Signal that an entire Response Message has been read from the `Transaction`. Increment `readcount` and wake up tasks waiting in `closewrite`. """ -function IOExtras.closeread(c::Connection) - @lockassert isreadable(c) - c.readcount += 1 - unlock(c.readlock) ;@debug 2 "✉️ Read done: $c" +function IOExtras.closeread(t::Transaction) + @require isreadable(t) + t.c.readcount += 1 + unlock(t.c.readlock) ;@debug 2 "✉️ Read done: $t" notify(poolcondition) + @assert !isreadable(t) return end -function Base.close(c::Connection) - close(c.io) ;@debug 2 "🚫 Closed: $c" - if isreadable(c) - purge(c) - closeread(c) +function Base.close(t::Transaction) + close(t.c.io) ;@debug 2 "🚫 Closed: $t" + if isreadable(t) + purge(t.c) + closeread(t) end return end """ - purge(::Connection) + purge(::Transaction) -Remove unread data from a `Connection`. +Remove unread data from a `Transaction`. """ function purge(c::Connection) - @assert !isopen(c) + @require !isopen(c.io) while !eof(c.io) readavailable(c.io) end @@ -276,13 +285,13 @@ function findwritable(T::Type, pipeline_limit::Int, reuse_limit::Int) - filter(c->(typeof(c.io) == T && + filter(c->(!c.writebusy && + typeof(c.io) == T && c.host == host && c.port == port && c.pipeline_limit == pipeline_limit && c.writecount < reuse_limit && c.writecount - c.readcount < pipeline_limit && - !islocked(c.writelock) && isopen(c.io)), pool) end @@ -334,7 +343,7 @@ Remove closed connections from `pool`. """ function purge() while (i = findfirst(x->!isopen(x.io) && - x.readcount == x.writecount, pool)) > 0 + x.readcount >= x.writecount, pool)) > 0 c = pool[i] purge(c) deleteat!(pool, i) ;@debug 1 "🗑 Deleted: $c" @@ -349,17 +358,17 @@ Find a reusable `Connection` in the `pool`, or create a new `Connection` if required. """ -function getconnection(::Type{Connection{T}}, +function getconnection(::Type{Transaction{T}}, host::AbstractString, port::AbstractString; pipeline_limit::Int = default_pipeline_limit, reuse_limit::Int = nolimit, - kw...)::Connection{T} where T <: IO + kw...)::Transaction{T} where T <: IO while true lock(poollock) - @lockassert poollock.reentrancy_cnt == 1 + @assert poollock.reentrancy_cnt == 1 try # Close connections that have reached the reuse limit... @@ -377,8 +386,7 @@ function getconnection(::Type{Connection{T}}, idle = filter(c->!islocked(c.readlock), writable) if !isempty(idle) c = rand(idle) ;@debug 1 "♻️ Idle: $c" - lock(c.writelock) - return c + return Transaction{T}(c) end # If there are not too many duplicates for this host, @@ -387,16 +395,14 @@ function getconnection(::Type{Connection{T}}, if length(busy) < duplicate_connection_limit io = getconnection(T, host, port; kw...) c = Connection{T}(host, port, pipeline_limit, io) - lock(c.writelock) push!(pool, c) ;@debug 1 "🔗 New: $c" - return c + return Transaction{T}(c) end # Share a connection that has active readers... if !isempty(writable) c = rand(writable) ;@debug 1 "⇆ Shared: $c" - lock(c.writelock) - return c + return Transaction{T}(c) end finally @@ -414,7 +420,7 @@ function Base.show(io::IO, c::Connection) print( io, tcpstatus(c), " ", - lpad(c.writecount,3),"↑", islocked(c.writelock) ? "🔒 " : " ", + lpad(c.writecount,3),"↑", c.writebusy ? "🔒 " : " ", lpad(c.readcount,3), "↓", islocked(c.readlock) ? "🔒 " : " ", c.host, ":", c.port != "" ? c.port : Int(c.peerport), ":", Int(c.localport), @@ -425,11 +431,11 @@ function Base.show(io::IO, c::Connection) nwaiting > 0 ? ", $nwaiting bytes waiting" : "", DEBUG_LEVEL > 0 ? ", $(Base._fd(tcpsocket(c.io)))" : "", DEBUG_LEVEL > 0 && - islocked(c.writelock) ? ", write task: $(taskid(c.writelock))" : "", - DEBUG_LEVEL > 0 && islocked(c.readlock) ? ", read task: $(taskid(c.readlock))" : "") end +Base.show(io::IO, t::Transaction) = show(io, t.c) + function tcpstatus(c::Connection) s = Base.uv_status_string(tcpsocket(c.io)) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index a19109789..0e4e8039c 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -24,14 +24,18 @@ Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; connectionpool::Bool=true, kw...) where Next - Connection = sockettype(uri) + SocketType = sockettype(uri) if connectionpool - Connection = ConnectionPool.Connection{Connection} + SocketType = ConnectionPool.Transaction{SocketType} end - io = getconnection(Connection, uri.host, uri.port; kw...) + io = getconnection(SocketType, uri.host, uri.port; kw...) try - return request(Next, io, req, body; kw...) + r = request(Next, io, req, body; kw...) + if !connectionpool + close(io) + end + return r catch e @debug 1 "❗️ ConnectionLayer $e. Closing: $io" close(io) diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index 5b91c062f..cb05cea5c 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -1,10 +1,12 @@ module HTTPStreams -export HTTPStream, readheaders +export HTTPStream using ..IOExtras using ..Parsers using ..Messages +import ..ConnectionPool +import ..@require, ..precondition_error struct HTTPStream{T <: Message} <: IO @@ -15,27 +17,52 @@ struct HTTPStream{T <: Message} <: IO end function HTTPStream(io::IO, request::Request, parser::Parser) + @require iswritable(io) writechunked = header(request, "Transfer-Encoding") == "chunked" - HTTPStream{Response}(io, request.response, parser, writechunked) + http = HTTPStream{Response}(io, request.response, parser, writechunked) + startwrite(http) + return http +end + + +# Writing HTTP Messages + +IOExtras.iswritable(http::HTTPStream) = iswritable(http.stream) + +function IOExtras.startwrite(http::HTTPStream) + @require iswritable(http.stream) + writeheaders(http.stream, http.message.request) end function Base.unsafe_write(http::HTTPStream, p::Ptr{UInt8}, n::UInt) if !http.writechunked - return unsafe_write(http.stream, p, n) + return unsafe_write(http.stream, p, n) end return write(http.stream, hex(n), "\r\n") + - unsafe_write(http.stream, p, n) + + unsafe_write(http.stream, p, n) + write(http.stream, "\r\n") end -writeend(http) = http.writechunked ? write(http.stream, "0\r\n\r\n") : 0 +function IOExtras.closewrite(http::HTTPStream) + if !iswritable(http) + return + end + if http.writechunked + write(http.stream, "0\r\n\r\n") + end + closewrite(http.stream) +end + + +# Reading HTTP Messages +IOExtras.isreadable(http::HTTPStream) = isreadable(http.stream) -function Messages.readheaders(http::HTTPStream) - writeend(http) - closewrite(http.stream) +function IOExtras.startread(http::HTTPStream) + @require !isreadable(http.stream) + startread(http.stream) configure_parser(http) return readheaders(http.stream, http.parser, http.message) end @@ -54,7 +81,7 @@ configure_parser(http::HTTPStream{Request}) = reset!(http.parser) function Base.eof(http::HTTPStream) if !headerscomplete(http.message) - readheaders(http) + startread(http) end if bodycomplete(http.parser) return true @@ -68,12 +95,9 @@ end function Base.readavailable(http::HTTPStream)::ByteView - if !headerscomplete(http.message) - throw(ArgumentError("headers must be read before body\n$http\n")) - end - if bodycomplete(http.parser) - throw(ArgumentError("message body already complete\n$http\n")) - end + @require headerscomplete(http.message) + @require !bodycomplete(http.parser) + bytes = readavailable(http.stream) if isempty(bytes) return nobytes @@ -94,25 +118,42 @@ function Base.read(http::HTTPStream) end -function Base.close(http::HTTPStream{Response}) +function IOExtras.closeread(http::HTTPStream{Response}) + + # "If [the response] indicates the server does not wish to receive the + # message body and is closing the connection, the client SHOULD immediately + # cease transmitting the body and close its side of the connection." + # https://tools.ietf.org/html/rfc7230#section-6.5 + if iswritable(http.stream) && + iserror(http.message) && + connectionclosed(http.parser) + close(http.stream) + return http.message + end + + # Discard unread body bytes... while !eof(http) readavailable(http) end + # Read trailers... if bodycomplete(http.parser) && !messagecomplete(http.parser) readtrailers(http.stream, http.parser, http.message) end + closeread(http.stream) + + # Error if Message is not complete... if !messagecomplete(http.parser) close(http.stream) throw(EOFError()) end + # Close conncetion if server sent "Connection: close"... if connectionclosed(http.parser) close(http.stream) - else - closeread(http.stream) end + return http.message end diff --git a/src/IOExtras.jl b/src/IOExtras.jl index 3073033ec..3a0569bfb 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -1,6 +1,7 @@ module IOExtras -export unread!, closeread, closewrite, tcpsocket, localport, peerport +export unread!, startwrite, closewrite, startread, closeread, + tcpsocket, localport, peerport """ unread!(::IO, bytes) @@ -52,14 +53,18 @@ end """ + startwrite(::IO) closewrite(::IO) + startread(::IO) closeread(::IO) -Signal end of write or read operations. +Signal start/end of write or read operations. """ +startwrite(io) = nothing closewrite(io) = nothing -closeread(io) = close(io) +startread(io) = nothing +closeread(io) = nothing using MbedTLS.SSLContext diff --git a/src/Messages.jl b/src/Messages.jl index f5cee3420..e36bb6819 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -109,7 +109,8 @@ mkheaders(h)::Headers = Header[string(k) => string(v) for (k,v) in h] Does this `Response` have an error status? """ -iserror(r::Response) = (r.status < 200 || r.status >= 300) && !isredirect(r) +iserror(r::Response) = r.status != 0 && + (r.status < 200 || r.status >= 300) && !isredirect(r) """ diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 1b6cbc2ea..cebe6cc58 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -22,10 +22,13 @@ end """ - request(StreamLayer, ::IO, ::Request, ::Response) + request(StreamLayer, ::IO, ::Request, body) -> ::Response -Send a `Request` and receive a `Response`. -Run the `Request` in a background task if response body is a stream. +Send a `Request` and return a `Response`. +Send the `Request` body in a background task and begin reading the response +immediately so that the transmission can be aborted if the `Response` status +indicates that the server does wish to receive the message body +[https://tools.ietf.org/html/rfc7230#section-6.5](RFC7230 6.5). """ function request(::Type{StreamLayer}, io::IO, req::Request, body; @@ -40,35 +43,33 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; http = HTTPStream(io, req, ConnectionPool.getparser(io)) if iofunction != nothing - write(io, req) iofunction(http) + closewrite(http) + closeread(http) else - write(io, req) - if req.body === body_is_a_stream - writebody(http, req, body) - end -#= FIXME - @async begin - write(io, req) + + write_body_task = @async begin if req.body === body_is_a_stream writebody(http, req, body) + else + write(http, req.body) end - writeend(http) - closewrite(http.stream) + closewrite(http) end -=# + yield() - - readheaders(http) + startread(http) if response_stream == nothing req.response.body = read(http) else req.response.body = body_was_streamed write(response_stream, http) end + + closeread(http) + wait(write_body_task) end - close(http) verbose == 1 && printlncompact(req.response) verbose >= 2 && println(req.response) diff --git a/src/TimeoutRequest.jl b/src/TimeoutRequest.jl index 1d3071cf1..e2fcc6a7f 100644 --- a/src/TimeoutRequest.jl +++ b/src/TimeoutRequest.jl @@ -22,14 +22,12 @@ function request(::Type{TimeoutLayer{Next}}, io::IO, req, body; request_task = current_task() @async while wait_for_timeout[] - if islocked(io.readlock) && - lockedby(io.readlock) == request_task && - inactiveseconds(io) > timeout + if isreadable(io) && inactiveseconds(io) > timeout close(io) @debug 0 "💥 Read inactive > $(timeout)s: $io" break end - sleep(10) + sleep(8 + rand() * 4) end try diff --git a/src/debug.jl b/src/debug.jl index 222f58f38..b3d72b123 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -23,6 +23,28 @@ end printlncompact(x) = println(sprint(showcompact, x)) + +@noinline function precondition_error(msg, frame) + msg = string(sprint(StackTraces.show_spec_linfo, + StackTraces.lookup(frame)[2]), + " requires ", msg) + return ArgumentError(msg) +end + + +""" + @require precondition [message] +Throw `ArgumentError` if `precondition` is false. +""" +macro require(precondition, msg = string(precondition)) + esc(:(if ! $precondition throw(precondition_error($msg, backtrace()[1])) end)) +end + + +# FIXME +# Should this have a branch-prediction hint? (same for @assert?) +# http://llvm.org/docs/BranchWeightMetadata.html#built-in-expect-instructions + #= macro src() @static if VERSION >= v"0.7-" && length(:(@test).args) == 2 From 80043ee6c9dc6d72ca6a11acee6bc23b23063e47 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 31 Dec 2017 17:24:12 +1100 Subject: [PATCH 094/182] typo --- docs/src/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index abce6bff1..e1730ecfe 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -70,9 +70,9 @@ The basic API function is: `body` can take a number of forms: - a `String`, a `Vector{UInt8}` or a readable `IO` - or any `T` accetped by `write(::IO, ::T)` + or any `T` accepted by `write(::IO, ::T)` - a collection of `String` or `AbstractVector{UInt8}` or `IO` - or any `T` accetped by `write(::IO, ::T...)` + or any `T` accepted by `write(::IO, ::T...)` - an readable `IO` stream or any `IO`-like type `T` for which `eof(T)` and `readavailable(T)` are defined. From 0f5a32aa7316cf6e4dfb9a375392410a2896f799 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 31 Dec 2017 17:31:32 +1100 Subject: [PATCH 095/182] ConnectionPool eof() handle closed Transaction. Remove auto startwrite() in HTTPStream constructor (better to have the symetry of startwrite()/closewrite() in request(StreamLayer, ...) ). In closeread(http::HTTPStream) don't call closeread(::Transaction) if the Transaction is already closed (e.g. due to exception or early abort). Split out default_iofunction from request(StreamLayer, ...) --- src/ConnectionPool.jl | 6 +++--- src/ConnectionRequest.jl | 2 +- src/HTTPStreams.jl | 12 ++++++++---- src/RetryRequest.jl | 2 +- src/StreamRequest.jl | 32 ++++++++++++++++++-------------- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index c9c59f610..ba850f098 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -96,7 +96,7 @@ Base.unsafe_write(t::Transaction, p::Ptr{UInt8}, n::UInt) = Base.isopen(t::Transaction) = isopen(t.c.io) function Base.eof(t::Transaction) - @require isreadable(t) + @require isreadable(t) || !isopen(t) if nb_available(t) > 0 return false end ;@debug 3 "eof(::Transaction) -> eof($typeof(c.io)): $t" @@ -185,7 +185,7 @@ function IOExtras.startread(t::Transaction) # FIXME break # end unlock(t.c.readlock) - yield() ;@debug 1 "⏳ seq=$(lpad(seq,3)): $t" + yield() ;@debug 0 "⏳ Waiting to read: $t" lock(t.c.readlock) end ;@debug 1 "👁 Start read: $t" @assert isreadable(t) @@ -434,7 +434,7 @@ function Base.show(io::IO, c::Connection) islocked(c.readlock) ? ", read task: $(taskid(c.readlock))" : "") end -Base.show(io::IO, t::Transaction) = show(io, t.c) +Base.show(io::IO, t::Transaction) = print(io, "T$(t.sequence)", t.c) function tcpstatus(c::Connection) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 0e4e8039c..d9d082dd2 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -37,7 +37,7 @@ function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; end return r catch e - @debug 1 "❗️ ConnectionLayer $e. Closing: $io" + @debug 0 "❗️ ConnectionLayer $e. Closing: $io" close(io) rethrow(e) end diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index cb05cea5c..b30413a2f 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -19,9 +19,7 @@ end function HTTPStream(io::IO, request::Request, parser::Parser) @require iswritable(io) writechunked = header(request, "Transfer-Encoding") == "chunked" - http = HTTPStream{Response}(io, request.response, parser, writechunked) - startwrite(http) - return http + HTTPStream{Response}(io, request.response, parser, writechunked) end @@ -127,6 +125,9 @@ function IOExtras.closeread(http::HTTPStream{Response}) if iswritable(http.stream) && iserror(http.message) && connectionclosed(http.parser) + @debug 0 "✋ Abort on $(sprint(writestartline, http.message)): " * + http.stream + @debug 1 "✋ $(http.message)" close(http.stream) return http.message end @@ -141,7 +142,9 @@ function IOExtras.closeread(http::HTTPStream{Response}) readtrailers(http.stream, http.parser, http.message) end - closeread(http.stream) + if isreadable(http.stream) + closeread(http.stream) + end # Error if Message is not complete... if !messagecomplete(http.parser) @@ -151,6 +154,7 @@ function IOExtras.closeread(http::HTTPStream{Response}) # Close conncetion if server sent "Connection: close"... if connectionclosed(http.parser) + @debug 0 "✋ \"Connection: close\": $(http.stream)" close(http.stream) end diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index a9c858a28..cd0441589 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -21,7 +21,7 @@ isrecoverable(e::Exception) = false isrecoverable(e, req) = isrecoverable(e) && !(req.body === body_was_streamed) && !(req.response.body === body_was_streamed) && - (@debug 1 "🔄 Retry $e: $(sprint(showcompact, req))"; + (@debug 0 "🔄 Retry $e: $(sprint(showcompact, req))"; true) diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index cebe6cc58..637066ee6 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -41,14 +41,27 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; verbose >= 2 && println(req) http = HTTPStream(io, req, ConnectionPool.getparser(io)) + startwrite(http) - if iofunction != nothing - iofunction(http) - closewrite(http) - closeread(http) + if iofunction == nothing + default_iofunction(http, req, body, response_stream) else + iofunction(http) + end + + closewrite(http) + closeread(http) + + verbose == 1 && printlncompact(req.response) + verbose >= 2 && println(req.response) + + return req.response +end + - write_body_task = @async begin +function default_iofunction(http::HTTPStream, req::Request, body, response_stream) + @sync begin + @async try if req.body === body_is_a_stream writebody(http, req, body) else @@ -65,17 +78,8 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; req.response.body = body_was_streamed write(response_stream, http) end - closeread(http) - wait(write_body_task) end - - - verbose == 1 && printlncompact(req.response) - verbose >= 2 && println(req.response) - - return req.response end - end # module StreamRequest From 866627408bb0673209981d97341835dab80e8f99 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 31 Dec 2017 17:45:56 +1100 Subject: [PATCH 096/182] fix pipline_limit off-by-one, add +Base.close(c::Connection) = Base.close(c.io) --- src/ConnectionPool.jl | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index ba850f098..dc62c37d3 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -10,7 +10,7 @@ import ..Connect: getconnection, getparser, inactiveseconds import ..Parsers.Parser -const duplicate_connection_limit = 8 +const default_duplicate_limit = 8 const default_pipeline_limit = 16 const nolimit = typemax(Int) @@ -179,11 +179,6 @@ function IOExtras.startread(t::Transaction) t.c.timestamp = time() lock(t.c.readlock) while t.c.readcount != t.sequence -# if !isopen(t) && nb_available(t) == 0 -# # If there is nothing left to read, -# # then unlocking sequence is irrelevant. -# FIXME break -# end unlock(t.c.readlock) yield() ;@debug 0 "⏳ Waiting to read: $t" lock(t.c.readlock) @@ -212,7 +207,6 @@ function IOExtras.closeread(t::Transaction) return end - function Base.close(t::Transaction) close(t.c.io) ;@debug 2 "🚫 Closed: $t" if isreadable(t) @@ -222,6 +216,8 @@ function Base.close(t::Transaction) return end +Base.close(c::Connection) = Base.close(c.io) + """ purge(::Transaction) @@ -291,7 +287,7 @@ function findwritable(T::Type, c.port == port && c.pipeline_limit == pipeline_limit && c.writecount < reuse_limit && - c.writecount - c.readcount < pipeline_limit && + c.writecount - c.readcount < pipeline_limit + 1 && isopen(c.io)), pool) end @@ -361,6 +357,7 @@ or create a new `Connection` if required. function getconnection(::Type{Transaction{T}}, host::AbstractString, port::AbstractString; + duplicate_limit=default_duplicate_limit, pipeline_limit::Int = default_pipeline_limit, reuse_limit::Int = nolimit, kw...)::Transaction{T} where T <: IO @@ -392,7 +389,7 @@ function getconnection(::Type{Transaction{T}}, # If there are not too many duplicates for this host, # create a new connection... busy = findall(T, host, port, pipeline_limit) - if length(busy) < duplicate_connection_limit + if length(busy) < duplicate_limit io = getconnection(T, host, port; kw...) c = Connection{T}(host, port, pipeline_limit, io) push!(pool, c) ;@debug 1 "🔗 New: $c" From 901004f429443dec5dcef8d821e67db7f35e39ab Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 31 Dec 2017 17:46:30 +1100 Subject: [PATCH 097/182] more variation of parameters in test/async.jl --- test/async.jl | 68 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/test/async.jl b/test/async.jl index a5c104ed2..9d0016187 100644 --- a/test/async.jl +++ b/test/async.jl @@ -11,7 +11,7 @@ stop_pool_dump = false @async while !stop_pool_dump HTTP.ConnectionPool.showpool(STDOUT) - sleep(20) + sleep(1) end # Tiny S3 interface... @@ -22,6 +22,7 @@ s3(method, path, body=UInt8[]; kw...) = s3get(path; kw...) = s3("GET", path; kw...) s3put(path, data; kw...) = s3("PUT", path, data; kw...) +#= function create_bucket(bucket) s3put(bucket, """ 90, :verbose => 0, - :pipeline_limit => 32, - :timeout => 20] + :pipeline_limit => pipe, + :duplicate_limit => dup, + :timeout => 120] @sync for i = 1:count data = rand(UInt8(65):UInt8(75), sz) @@ -63,16 +70,23 @@ conf = [:reuse_limit => 90, put_data_sums[i] = md5 @async try url = "$s3url/http.jl.test/file$i" - r = HTTP.open("PUT", url, ["Content-Length" => sz]; - body_sha256=digest(MD_SHA256, data), - body_md5=digest(MD_MD5, data), - awsauthorization=true, - conf...) do http - for n = 1:ch:sz - write(http, data[n:n+(ch-1)]) - sleep(rand(1:10)/1000) + r = nothing + if mode == :open + r = HTTP.open("PUT", url, ["Content-Length" => sz]; + body_sha256=digest(MD_SHA256, data), + body_md5=digest(MD_MD5, data), + awsauthorization=true, + conf...) do http + for n = 1:ch:sz + write(http, data[n:n+(ch-1)]) + sleep(rand(1:10)/1000) + end end end + if mode == :request + r = HTTP.request("PUT", url, [], data; + awsauthorization=true, conf...) + end #println("S3 put file$i") @assert strip(HTTP.header(r, "ETag"), '"') == md5 catch e @@ -88,15 +102,22 @@ get_data_sums = Dict() @async try url = "$s3url/http.jl.test/file$i" buf = IOBuffer() - r = HTTP.open("GET", url; - awsauthorization=true, - conf...) do http - truncate(buf, 0) # in case of retry! - while !eof(http) - write(buf, readavailable(http)) - sleep(rand(1:10)/1000) + r = nothing + if mode == :open + r = HTTP.open("GET", url; + awsauthorization=true, + conf...) do http + truncate(buf, 0) # in case of retry! + while !eof(http) + write(buf, readavailable(http)) + sleep(rand(1:10)/1000) + end end end + if mode == :request + r = HTTP.request("GET", url; response_stream=buf, + awsauthorization=true, conf...) + end #println("S3 get file$i") bytes = take!(buf) md5 = bytes2hex(digest(MD_MD5, bytes)) @@ -116,12 +137,14 @@ end end +#= configs = [ [], [:reuse_limit => 200], [:reuse_limit => 50] ] + @testset "async $count, $num, $config, $http" for count in 1:1, num in [100, 1000, 2000], config in configs, @@ -267,6 +290,7 @@ println("running async $count, 1:$num, $config, $http") end # testset sleep(12) +=# stop_pool_dump=true HTTP.ConnectionPool.showpool(STDOUT) From 8e69738ddb9a7d1b1398d2b78250a947f8553e31 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 2 Jan 2018 12:48:18 +1100 Subject: [PATCH 098/182] Add test/WebSockets.jl to test connection upgrade. --- src/Connect.jl | 4 +- src/ConnectionPool.jl | 9 +- src/ConnectionRequest.jl | 2 +- src/HTTP.jl | 4 +- src/HTTPStreams.jl | 7 +- src/Messages.jl | 2 +- src/WebSockets.jl | 260 +++++++++++++++++++++++++++++++++++++++ src/uri.jl | 8 +- test/WebSockets.jl | 23 ++++ test/runtests.jl | 3 + 10 files changed, 312 insertions(+), 10 deletions(-) create mode 100644 src/WebSockets.jl create mode 100644 test/WebSockets.jl diff --git a/src/Connect.jl b/src/Connect.jl index bf5407ab8..af4a8a0fd 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -1,6 +1,6 @@ module Connect -export getconnection, getparser, inactiveseconds +export getconnection, getparser, inactiveseconds, getrawstream using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! @@ -47,6 +47,8 @@ end getparser(::IO) = Parser() +getrawstream(io::IO) = io + inactiveseconds(::IO)= Float64(0) end # module Connect diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index dc62c37d3..f53da6855 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -1,12 +1,12 @@ module ConnectionPool -export getconnection, getparser, inactiveseconds +export getconnection, getparser, getrawstream, inactiveseconds using ..IOExtras import ..@debug, ..DEBUG_LEVEL, ..taskid, ..@require, ..precondition_error import MbedTLS.SSLContext -import ..Connect: getconnection, getparser, inactiveseconds +import ..Connect: getconnection, getparser, getrawstream, inactiveseconds import ..Parsers.Parser @@ -80,8 +80,13 @@ end getparser(t::Transaction) = t.c.parser + +getrawstream(t::Transaction) = t.c.io + + inactiveseconds(t::Transaction) = inactiveseconds(t.c) + function inactiveseconds(c::Connection)::Float64 if !islocked(c.readlock) return Float64(0) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index d9d082dd2..fa573b9ae 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -12,7 +12,7 @@ abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end export ConnectionPoolLayer -sockettype(uri::URI) = uri.scheme == "https" ? SSLContext : TCPSocket +sockettype(uri::URI) = uri.scheme in ("https", "wss") ? SSLContext : TCPSocket """ diff --git a/src/HTTP.jl b/src/HTTP.jl index e2cd6dd93..d5aeda249 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -5,7 +5,7 @@ using MbedTLS import MbedTLS.SSLContext -const DEBUG_LEVEL = 0 +const DEBUG_LEVEL = 2 const minimal = false include("compat.jl") @@ -26,7 +26,9 @@ include("parser.jl"); import .Parsers: ParsingError, Headers include("Connect.jl") include("ConnectionPool.jl") include("Messages.jl"); using .Messages + import .Messages: header, hasheader include("HTTPStreams.jl"); using .HTTPStreams +include("WebSockets.jl"); using .WebSockets request(method, uri, headers=[], body=UInt8[]; kw...)::Response = diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index b30413a2f..5c26a60ac 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -5,7 +5,8 @@ export HTTPStream using ..IOExtras using ..Parsers using ..Messages -import ..ConnectionPool +import ..Messages: header, hasheader +import ..ConnectionPool.getrawstream import ..@require, ..precondition_error @@ -22,6 +23,10 @@ function HTTPStream(io::IO, request::Request, parser::Parser) HTTPStream{Response}(io, request.response, parser, writechunked) end +header(http::HTTPStream, a...) = header(http.message, a...) +hasheader(http::HTTPStream, a) = header(http.message, a) +getrawstream(http::HTTPStream) = getrawstream(http.stream) + # Writing HTTP Messages diff --git a/src/Messages.jl b/src/Messages.jl index e36bb6819..6a6a184fc 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -109,7 +109,7 @@ mkheaders(h)::Headers = Header[string(k) => string(v) for (k,v) in h] Does this `Response` have an error status? """ -iserror(r::Response) = r.status != 0 && +iserror(r::Response) = r.status != 0 && r.status != 101 && (r.status < 200 || r.status >= 300) && !isredirect(r) diff --git a/src/WebSockets.jl b/src/WebSockets.jl new file mode 100644 index 000000000..16fe1cb68 --- /dev/null +++ b/src/WebSockets.jl @@ -0,0 +1,260 @@ +module WebSockets + +using Base64 +using Unicode +using MbedTLS: digest, MD_SHA1, SSLContext +import ..HTTP +using ..HTTP.IOExtras +import ..ConnectionPool +using HTTP.header +import ..@debug, ..DEBUG_LEVEL, ..@require, ..precondition_error + + + +const WS_FINAL = 0x80 +const WS_CONTINUATION = 0x00 +const WS_TEXT = 0x01 +const WS_BINARY = 0x02 +const WS_CLOSE = 0x08 +const WS_PING = 0x09 +const WS_PONG = 0x0A + +const WS_MASK = 0x80 + + +struct WebSocketError <: Exception + status::Int16 + message::String +end + + +struct WebSocketHeader + opcode::UInt8 + final::Bool + length::UInt + hasmask::Bool + mask::UInt32 +end + + +mutable struct WebSocket{T <: IO} <: IO + io::T + frame_type::UInt8 + rxpayload::Vector{UInt8} + txpayload::Vector{UInt8} + txclosed::Bool + rxclosed::Bool +end + +function WebSocket(io::T; binary=false) where T <: IO + WebSocket{T}(io, binary ? WS_BINARY : WS_TEXT, + UInt8[], UInt8[], false, false) +end + + + +# Handshake + + +function open(f::Function, url; binary=false, kw...) + + key = base64encode(rand(UInt8, 16)) + + headers = [ + "Upgrade" => "websocket", + "Connection" => "Upgrade", + "Sec-WebSocket-Key" => key, + "Sec-WebSocket-Version" => "13" + ] + + HTTP.open("GET", url, headers; kw...) do http + + startread(http) + + status = http.message.status + if status != 101 + return + end + + upgrade = header(http, "Upgrade") + if lowercase(upgrade) != "websocket" + throw(WebSocketError(0, "Expected \"Upgrade: websocket\"!\n" * + "$(http.message)")) + end + + connection = header(http, "Connection") + if lowercase(connection) != "upgrade" + throw(WebSocketError(0, "Expected \"Connection: upgrade\"!\n" * + "$(http.message)")) + end + + hashkey = "$(key)258EAFA5-E914-47DA-95CA-C5AB0DC85B11" + accepthash = base64encode(digest(MD_SHA1, hashkey)) + accept = header(http, "Sec-WebSocket-Accept") + if accept != accepthash + throw(WebSocketError(0, "Invalid Sec-WebSocket-Accept\n" * + "$(http.message)")) + end + + io = ConnectionPool.getrawstream(http) + f(WebSocket(io; binary=binary)) + end +end + + + +# Sending Frames + + +function Base.unsafe_write(ws::WebSocket, p::Ptr{UInt8}, n::UInt) + return wswrite(ws, unsafe_wrap(Array, p, n)) +end + + +function Base.write(ws::WebSocket, x1, x2, xs...) + local n::Int = 0 + n += wswrite(ws, ws.frame_type, x1) + xs = (x2, xs...) + l = length(xs) + for i in 1:l + n += wswrite(ws, i == l ? WS_FINAL : WS_CONTINUATION, xs[i]) + end + return n +end + + +function IOExtras.closewrite(ws::WebSocket) + @require !ws.txclosed + opcode = WS_FINAL | WS_CLOSE + @debug 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, 0x00))" + write(ws.io, opcode, 0x00) + ws.txclosed = true +end + + +wslength(l) = l < 0x7E ? (UInt8(l), UInt8[]) : + l <= 0xFFFF ? (0x7E, reinterpret(UInt8, [UInt16(l)])) : + (0x7F, reinterpret(UInt8, [UInt64(l)])) + + +wswrite(ws::WebSocket, x) = wswrite(ws, WS_FINAL | ws.frame_type, x) + +wswrite(ws::WebSocket, opcode::UInt8, x) = wswrite(ws, opcode, Vector{UInt8}(x)) + +function wswrite(ws::WebSocket, opcode::UInt8, bytes::Vector{UInt8}) + + n = length(bytes) + len, extended_len = wslength(n) + len |= WS_MASK + mask = mask!(ws, bytes) + + @debug 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, len, extended_len, mask))" + write(ws.io, opcode, len, extended_len, mask) + + @debug 2 " ⬅️ $(ws.txpayload[1:n])" + unsafe_write(ws.io, pointer(ws.txpayload), n) +end + + +function mask!(ws::WebSocket, bytes::Vector{UInt8}) + mask = rand(UInt8, 4) + l = length(bytes) + if length(ws.txpayload) < l + resize!(ws.txpayload, l) + end + for i in 1:l + ws.txpayload[i] = bytes[i] ⊻ mask[((i-1) % 4)+1] + end + return mask +end + + +function Base.close(ws::WebSocket) + if !ws.txclosed + closewrite(ws) + end + while !ws.rxclosed + readframe(ws) + end +end + + +Base.isopen(ws::WebSocket) = !ws.rxclosed + + + +# Receiving Frames + +Base.eof(ws::WebSocket) = eof(ws.io) + +Base.readavailable(ws::WebSocket) = collect(readframe(ws)) + + +function readheader(io::IO) + b = UInt8[0,0] + read!(io, b) + len = b[2] & ~WS_MASK + WebSocketHeader( + b[1] & 0x0F, + b[1] & WS_FINAL > 0, + len == 0x7F ? UInt(ntoh(read(io, UInt64))) : + len == 0x7E ? UInt(ntoh(read(io, UInt16))) : UInt(len), + b[2] & WS_MASK > 0, + b[2] & WS_MASK > 0 ? ntoh(read(io, UInt32)) : UInt32(0)) +end + + +function readframe(ws::WebSocket) + h = readheader(ws.io) + @debug 1 "WebSocket ➡️ $h" + + if h.length > 0 + if length(ws.rxpayload) < h.length + resize!(ws.rxpayload, h.length) + end + unsafe_read(ws.io, pointer(ws.rxpayload), h.length) + @debug 2 " ➡️ \"$(String(ws.rxpayload[1:h.length]))\"" + end + + if h.opcode == WS_CLOSE + ws.rxclosed = true + if h.length >= 2 + status = UInt16(ws.rxpayload[1]) << 8 | ws.rxpayload[2] + if status != 1000 + message = String(ws.rxpayload[3:h.length]) + throw(WebSocketError(status, message)) + end + end + return UInt8[] + elseif h.opcode == WS_PING + write(ws.io, [WS_PONG, 0x00]) + wswrite(ws, WS_FINAL | WS_PONG, ws.rxpayload) + return readframe(ws) + else + return view(ws.rxpayload, 1:Int(h.length)) + end +end + +function WebSocketHeader(bytes...) + io = IOBuffer() + write(io, bytes...) + seek(io, 0) + return readheader(io) +end + +function Base.show(io::IO, h::WebSocketHeader) + print(io, "WebSocketHeader(", + h.opcode == WS_CONTINUATION ? "CONTINUATION" : + h.opcode == WS_TEXT ? "TEXT" : + h.opcode == WS_BINARY ? "BINARY" : + h.opcode == WS_CLOSE ? "CLOSE" : + h.opcode == WS_PING ? "PING" : + h.opcode == WS_PONG ? "PONG" : h.opcode, + h.final ? " | FINAL, " : ", ", + h.length > 0 ? "$(Int(h.length))-byte payload" : "", + h.hasmask ? ", mask = $(hex(h.mask))" : "", + ")") +end + + +end # module WebSockets diff --git a/src/uri.jl b/src/uri.jl index c87d9f5b0..c1263a23d 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -51,8 +51,10 @@ function URI(;host::AbstractString="", path::AbstractString="", fragment::AbstractString="", isconnect::Bool=false) host != "" && scheme == "" && !isconnect && (scheme = "http") io = IOBuffer() - printuri(io, scheme, userinfo, host, string(port), path, escapeuri(query), fragment) - return URI(String(take!(io)); isconnect=isconnect) + printuri(io, scheme, userinfo, host, string(port), + path, escapeuri(query), fragment) + uri = String(take!(io)) + return URI(uri, isconnect=isconnect) end # we assume `str` is at least host & port @@ -152,7 +154,7 @@ function queryparams(q::AbstractString) end # Validate known URI formats -const uses_authority = ["hdfs", "ftp", "http", "gopher", "nntp", "telnet", "imap", "wais", "file", "mms", "https", "shttp", "snews", "prospero", "rtsp", "rtspu", "rsync", "svn", "svn+ssh", "sftp" ,"nfs", "git", "git+ssh", "ldap", "s3"] +const uses_authority = ["hdfs", "ftp", "http", "gopher", "nntp", "telnet", "imap", "wais", "file", "mms", "https", "shttp", "snews", "prospero", "rtsp", "rtspu", "rsync", "svn", "svn+ssh", "sftp" ,"nfs", "git", "git+ssh", "ldap", "s3", "ws"] const uses_params = ["ftp", "hdl", "prospero", "http", "imap", "https", "shttp", "rtsp", "rtspu", "sip", "sips", "mms", "sftp", "tel"] const non_hierarchical = ["gopher", "hdl", "mailto", "news", "telnet", "wais", "imap", "snews", "sip", "sips"] const uses_query = ["http", "wais", "imap", "https", "shttp", "mms", "gopher", "rtsp", "rtspu", "sip", "sips", "ldap"] diff --git a/test/WebSockets.jl b/test/WebSockets.jl new file mode 100644 index 000000000..a49a3e726 --- /dev/null +++ b/test/WebSockets.jl @@ -0,0 +1,23 @@ +using HTTP +using HTTP.IOExtras + +for s in ["ws", "wss"] + + HTTP.WebSockets.open("$s://echo.websocket.org") do io + write(io, Vector{UInt8}("Foo")) + @test !eof(io) + @test String(readavailable(io)) == "Foo" + + write(io, Vector{UInt8}("Hello")) + write(io, " There") + write(io, " World", "!") + closewrite(io) + + buf = IOBuffer() + write(buf, io) + @test String(take!(buf)) == "Hello There World!" + + close(io) + end + +end diff --git a/test/runtests.jl b/test/runtests.jl index 1193b357e..699c60bce 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,6 +16,7 @@ end @testset "HTTP" begin +#= include("utils.jl"); include("fifobuffer.jl"); include("sniff.jl"); @@ -28,5 +29,7 @@ end # include("handlers.jl") include("client.jl"); include("async.jl"); +=# + include("WebSockets.jl"); # include("server.jl") end; From 03c6a035dbf1fcab6d98f06a1ccd6ec4e2322efe Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 2 Jan 2018 15:14:02 +1100 Subject: [PATCH 099/182] Ignore 100 Continue message in startread(http::HTTPStream) --- src/HTTPStreams.jl | 13 ++++++++++++- src/Messages.jl | 4 ++-- src/parser.jl | 2 +- test/async.jl | 13 ++++++++++--- test/runtests.jl | 6 +++--- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index 5c26a60ac..cbb92d57e 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -8,6 +8,7 @@ using ..Messages import ..Messages: header, hasheader import ..ConnectionPool.getrawstream import ..@require, ..precondition_error +import ..@debug, ..DEBUG_LEVEL struct HTTPStream{T <: Message} <: IO @@ -67,7 +68,17 @@ function IOExtras.startread(http::HTTPStream) @require !isreadable(http.stream) startread(http.stream) configure_parser(http) - return readheaders(http.stream, http.parser, http.message) + h = readheaders(http.stream, http.parser, http.message) + if http.message isa Response && http.message.status == 100 + # 100 Continue + # https://tools.ietf.org/html/rfc7230#section-5.6 + # https://tools.ietf.org/html/rfc7231#section-6.2.1 + @debug 1 "✅ Continue: $(http.stream)" + configure_parser(http) + h = readheaders(http.stream, http.parser, http.message) + end + return h + end diff --git a/src/Messages.jl b/src/Messages.jl index 6a6a184fc..30f782689 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -109,7 +109,7 @@ mkheaders(h)::Headers = Header[string(k) => string(v) for (k,v) in h] Does this `Response` have an error status? """ -iserror(r::Response) = r.status != 0 && r.status != 101 && +iserror(r::Response) = r.status != 0 && r.status != 100 && r.status != 101 && (r.status < 200 || r.status >= 300) && !isredirect(r) @@ -309,7 +309,7 @@ end headerscomplete(r::Request) = r.method != "" -headerscomplete(r::Response) = r.status != 0 +headerscomplete(r::Response) = r.status != 0 && r.status != 100 function readtrailers(io::IO, parser::Parser, message::Message) diff --git a/src/parser.jl b/src/parser.jl index 8774598bc..305f8fec1 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -1057,7 +1057,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif parser.content_length != ULLONG_MAX # Content-Length header given and non-zero p_state = s_body_identity - elseif isrequest(parser) || # FIXME never need eof() for request? + elseif isrequest(parser) || # RFC 7230, 3.3.3, 6. div(parser.message.status, 100) == 1 || # 1xx e.g. Continue parser.message.status == 204 || # No Content parser.message.status == 304 # Not Modified diff --git a/test/async.jl b/test/async.jl index 9d0016187..837eacb2e 100644 --- a/test/async.jl +++ b/test/async.jl @@ -46,12 +46,20 @@ end @testset "async s3 dup$dup, count$count, sz$sz, pipw$pipe, $http, $mode" for count in [10, 100, 1000, 2000], - dup in [1, 8, 16], + dup in [1, 8], http in ["http", "https"], sz in [100, 1000, 10000], mode in [:request, :open], pipe in [0, 32] +if dup == 1 && count > 100 + continue +end + +if count == 2000 && (sz != 1000 || pipe != 32) + continue +end + global s3url s3url = "$http://s3.$s3region.amazonaws.com" println("running async s3 dup$dup, count$count, sz$sz, pipe$pipe, $http, $mode") @@ -137,7 +145,6 @@ end end -#= configs = [ [], [:reuse_limit => 200], @@ -290,7 +297,7 @@ println("running async $count, 1:$num, $config, $http") end # testset sleep(12) -=# + stop_pool_dump=true HTTP.ConnectionPool.showpool(STDOUT) diff --git a/test/runtests.jl b/test/runtests.jl index 699c60bce..208b6101c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,7 +16,7 @@ end @testset "HTTP" begin -#= + include("utils.jl"); include("fifobuffer.jl"); include("sniff.jl"); @@ -28,8 +28,8 @@ end # include("types.jl"); # include("handlers.jl") include("client.jl"); - include("async.jl"); -=# include("WebSockets.jl"); + + include("async.jl"); # include("server.jl") end; From 7511663ab0cbea541e774f3b0cbc1e8ae9d26638 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 2 Jan 2018 15:28:40 +1100 Subject: [PATCH 100/182] Test for 100-continue --- src/WebSockets.jl | 2 +- test/async.jl | 2 +- test/messages.jl | 6 ++++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/WebSockets.jl b/src/WebSockets.jl index 16fe1cb68..93420e28e 100644 --- a/src/WebSockets.jl +++ b/src/WebSockets.jl @@ -67,7 +67,7 @@ function open(f::Function, url; binary=false, kw...) "Sec-WebSocket-Version" => "13" ] - HTTP.open("GET", url, headers; kw...) do http + HTTP.open("GET", url, headers; reuse_limit=0, kw...) do http startread(http) diff --git a/test/async.jl b/test/async.jl index 837eacb2e..138803da4 100644 --- a/test/async.jl +++ b/test/async.jl @@ -52,7 +52,7 @@ end mode in [:request, :open], pipe in [0, 32] -if dup == 1 && count > 100 +if (dup == 1 || pipe == 0) && count > 100 continue end diff --git a/test/messages.jl b/test/messages.jl index 75f35f2ec..4a0ee4fbf 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -119,6 +119,12 @@ using JSON close(io) @test r.status == 200 end + + r = request("POST", "$sch://httpbin.org/post", + ["Expect" => "100-continue"], "Hello") + @test r.status == 200 + r = JSON.parse(String(r.body)) + @test r["data"] == "Hello" end mktempdir() do d From d5e4038cb36e5d88c9c8be81d0dd4d1036d1446c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 2 Jan 2018 15:56:49 +1100 Subject: [PATCH 101/182] MUST NOT automatically retry a request with a non-idempotent method https://tools.ietf.org/html/rfc7230#section-6.3.1 --- src/HTTP.jl | 2 +- src/Messages.jl | 19 ++++++++++++++++++- src/RetryRequest.jl | 32 ++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index d5aeda249..7584df972 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -5,7 +5,7 @@ using MbedTLS import MbedTLS.SSLContext -const DEBUG_LEVEL = 2 +const DEBUG_LEVEL = 0 const minimal = false include("compat.jl") diff --git a/src/Messages.jl b/src/Messages.jl index 30f782689..3ceed020b 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -2,7 +2,7 @@ module Messages export Message, Request, Response, reset!, - iserror, isredirect, ischunked, + iserror, isredirect, ischunked, issafe, isidempotent, header, hasheader, setheader, defaultheader, appendheader, mkheaders, readheaders, headerscomplete, readtrailers, writeheaders, readstartline! @@ -102,6 +102,23 @@ Request(bytes) = parse(Request, bytes) mkheaders(h::Headers) = h mkheaders(h)::Headers = Header[string(k) => string(v) for (k,v) in h] +""" + issafe(::Request) + +https://tools.ietf.org/html/rfc7231#section-4.2.1 +""" + +issafe(r::Request) = r.method in ["GET", "HEAD", "OPTIONS", "TRACE"] + + +""" + isidempotent(::Request) + +https://tools.ietf.org/html/rfc7231#section-4.2.2 +""" + +isidempotent(r::Request) = issafe(r) || r.method in ["PUT", "DELETE"] + """ iserror(::Response) diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index cd0441589..de5834711 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -13,24 +13,32 @@ export RetryLayer isrecoverable(e::Base.UVError) = true isrecoverable(e::Base.DNSError) = true isrecoverable(e::Base.EOFError) = true -isrecoverable(e::HTTP.StatusError) = e.status < 200 || e.status >= 500 isrecoverable(e::Base.ArgumentError) = e.msg == "stream is closed or unusable" +isrecoverable(e::HTTP.StatusError) = e.status >= 500 isrecoverable(e::Exception) = false -isrecoverable(e, req) = isrecoverable(e) && - !(req.body === body_was_streamed) && - !(req.response.body === body_was_streamed) && - (@debug 0 "🔄 Retry $e: $(sprint(showcompact, req))"; - true) - +isrecoverable(e, req, retry_non_idempotent) = + isrecoverable(e) && + !(req.body === body_was_streamed) && + !(req.response.body === body_was_streamed) && + (retry_non_idempotent || !isidempotent(req)) + # MUST NOT automatically retry a request with a non-idempotent method + # https://tools.ietf.org/html/rfc7230#section-6.3.1 function request(::Type{RetryLayer{Next}}, uri, req, body; - retries=4, kw...) where Next - - retry_request = retry(request, delays=ExponentialBackOff(n = retries), - check=(s,ex)->(s,isrecoverable(ex, req) && - (reset!(req.response); true))) + retries=4, retry_non_idempotent=false, kw...) where Next + + retry_request = retry(request, + delays=ExponentialBackOff(n = retries), + check=(s,ex)->begin + retry = isrecoverable(ex, req, retry_non_idempotent) + if retry + @debug 0 "🔄 Retry $e: $(sprint(showcompact, req))" + reset!(req.response) + end + return s, retry + end) retry_request(Next, uri, req, body; kw...) end From 6a7e1faf7a066907b6c56a62261c1380d033f2c6 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 2 Jan 2018 17:15:05 +1100 Subject: [PATCH 102/182] fix deadlock when no conncetion duplication is allowed --- src/ConnectionPool.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index f53da6855..12b44ada7 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -218,6 +218,7 @@ function Base.close(t::Transaction) purge(t.c) closeread(t) end + notify(poolcondition) return end @@ -436,7 +437,7 @@ function Base.show(io::IO, c::Connection) islocked(c.readlock) ? ", read task: $(taskid(c.readlock))" : "") end -Base.show(io::IO, t::Transaction) = print(io, "T$(t.sequence)", t.c) +Base.show(io::IO, t::Transaction) = print(io, "T$(t.sequence) ", t.c) function tcpstatus(c::Connection) From 7c89d28aeba947d220818c683d3ba93be7b8f2d1 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 2 Jan 2018 17:15:34 +1100 Subject: [PATCH 103/182] https://tools.ietf.org/html/rfc7230#section-6.3.2 "A user agent SHOULD NOT pipeline requests after a non-idempotent method, until the final response status code for that method has been received" --- src/HTTPStreams.jl | 20 +++++++++++++------- src/Messages.jl | 2 +- src/RetryRequest.jl | 2 +- src/StreamRequest.jl | 14 ++++++++++++-- test/messages.jl | 8 ++++++++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index cbb92d57e..1e4d0caf6 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -1,17 +1,17 @@ module HTTPStreams -export HTTPStream +export HTTPStream, closebody using ..IOExtras using ..Parsers using ..Messages -import ..Messages: header, hasheader +import ..Messages: header, hasheader, writestartline import ..ConnectionPool.getrawstream import ..@require, ..precondition_error import ..@debug, ..DEBUG_LEVEL -struct HTTPStream{T <: Message} <: IO +mutable struct HTTPStream{T <: Message} <: IO stream::IO message::T parser::Parser @@ -49,13 +49,19 @@ function Base.unsafe_write(http::HTTPStream, p::Ptr{UInt8}, n::UInt) end +function closebody(http::HTTPStream) + if http.writechunked + write(http.stream, "0\r\n\r\n") + http.writechunked = false + end +end + + function IOExtras.closewrite(http::HTTPStream) if !iswritable(http) return end - if http.writechunked - write(http.stream, "0\r\n\r\n") - end + closebody(http) closewrite(http.stream) end @@ -142,7 +148,7 @@ function IOExtras.closeread(http::HTTPStream{Response}) iserror(http.message) && connectionclosed(http.parser) @debug 0 "✋ Abort on $(sprint(writestartline, http.message)): " * - http.stream + "$(http.stream)" @debug 1 "✋ $(http.message)" close(http.stream) return http.message diff --git a/src/Messages.jl b/src/Messages.jl index 3ceed020b..1e89ab11f 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -5,7 +5,7 @@ export Message, Request, Response, iserror, isredirect, ischunked, issafe, isidempotent, header, hasheader, setheader, defaultheader, appendheader, mkheaders, readheaders, headerscomplete, readtrailers, writeheaders, - readstartline! + readstartline!, writestartline if VERSION > v"0.7.0-DEV.2338" using Unicode diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index de5834711..ae51129fa 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -23,7 +23,7 @@ isrecoverable(e, req, retry_non_idempotent) = !(req.body === body_was_streamed) && !(req.response.body === body_was_streamed) && (retry_non_idempotent || !isidempotent(req)) - # MUST NOT automatically retry a request with a non-idempotent method + # "MUST NOT automatically retry a request with a non-idempotent method" # https://tools.ietf.org/html/rfc7230#section-6.3.1 function request(::Type{RetryLayer{Next}}, uri, req, body; diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 637066ee6..687274c74 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -7,7 +7,7 @@ using ..Messages using ..HTTPStreams import ..ConnectionPool using ..MessageRequest -import ..@debugshort, ..DEBUG_LEVEL, ..printlncompact +import ..@debug, ..DEBUG_LEVEL, ..printlncompact abstract type StreamLayer <: Layer end export StreamLayer @@ -64,10 +64,20 @@ function default_iofunction(http::HTTPStream, req::Request, body, response_strea @async try if req.body === body_is_a_stream writebody(http, req, body) + closebody(http) else write(http, req.body) end - closewrite(http) + + if isidempotent(req) + closewrite(http) + else + # "A user agent SHOULD NOT pipeline requests after a + # non-idempotent method, until the final response + # status code for that method has been received" + # https://tools.ietf.org/html/rfc7230#section-6.3.2 + @debug 1 "🔒 non-idempotent, hold write lock: $(http.stream)" + end end yield() diff --git a/test/messages.jl b/test/messages.jl index 4a0ee4fbf..f58e0686b 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -145,6 +145,14 @@ using JSON @test i == n end end + +#= FIXME +Pipelineing tests + - Test pipeline_limit option + - Test early body send abort + - Test no pipelinging after non-idempotent +=# + end end # module MessagesTest From 279e80720eb410c498ee2be50b7ce5a324e976a6 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 2 Jan 2018 19:24:25 +1100 Subject: [PATCH 104/182] Fix isrecoverable()/isidempotent() bug Retry on 408 timeout and 403 forbidden (credentials timeout is fixable by retrying). Improve retry debug output. --- src/RetryRequest.jl | 26 ++++++++++++++++++++++---- src/StreamRequest.jl | 3 ++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index ae51129fa..44f5eaef5 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -14,7 +14,9 @@ isrecoverable(e::Base.UVError) = true isrecoverable(e::Base.DNSError) = true isrecoverable(e::Base.EOFError) = true isrecoverable(e::Base.ArgumentError) = e.msg == "stream is closed or unusable" -isrecoverable(e::HTTP.StatusError) = e.status >= 500 +isrecoverable(e::HTTP.StatusError) = e.status == 403 || # Forbidden + e.status == 408 || # Timeout + e.status >= 500 # Server Error isrecoverable(e::Exception) = false @@ -22,20 +24,23 @@ isrecoverable(e, req, retry_non_idempotent) = isrecoverable(e) && !(req.body === body_was_streamed) && !(req.response.body === body_was_streamed) && - (retry_non_idempotent || !isidempotent(req)) + (retry_non_idempotent || isidempotent(req)) # "MUST NOT automatically retry a request with a non-idempotent method" # https://tools.ietf.org/html/rfc7230#section-6.3.1 function request(::Type{RetryLayer{Next}}, uri, req, body; - retries=4, retry_non_idempotent=false, kw...) where Next + retries::Int=4, retry_non_idempotent::Bool=false, + kw...) where Next retry_request = retry(request, delays=ExponentialBackOff(n = retries), check=(s,ex)->begin retry = isrecoverable(ex, req, retry_non_idempotent) if retry - @debug 0 "🔄 Retry $e: $(sprint(showcompact, req))" + @debug 0 "🔄 Retry $ex: $(sprint(showcompact, req))" reset!(req.response) + else + @debug 0 "🚷 No Retry $(no_retry_reason(ex, req))" end return s, retry end) @@ -44,4 +49,17 @@ function request(::Type{RetryLayer{Next}}, uri, req, body; end +function no_retry_reason(ex, req) + buf = IOBuffer() + showcompact(buf, req) + print(buf, ": ", + ex isa HTTP.StatusError ? "HTTP $(ex.status): " : + !isrecoverable(ex) ? "$ex not recoverable, " : "", + (req.body === body_was_streamed) ? "request streamed, " : "", + (req.response.body === body_was_streamed) ? "response streamed, " : "", + !isidempotent(req) ? "$(req.method) non-idempotent" : "") + return String(take!(buf)) +end + + end # module RetryRequest diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 687274c74..d22253725 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -76,7 +76,8 @@ function default_iofunction(http::HTTPStream, req::Request, body, response_strea # non-idempotent method, until the final response # status code for that method has been received" # https://tools.ietf.org/html/rfc7230#section-6.3.2 - @debug 1 "🔒 non-idempotent, hold write lock: $(http.stream)" + @debug 1 "🔒 $(req.method) non-idempotent, " * + "holding write lock: $(http.stream)" end end yield() From 6acaee26f2abf07e1baacaaefe4bbe66863c1ba0 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 11:00:08 +1100 Subject: [PATCH 105/182] Add loopback test for abort while sending body (& fix bugs) Add loopback tests for streaming and collection body types (& fix bugs). Do closewrite() in close(t::Transaction) to ensure aborted connections are stuck in writable state. Implement post-abort exception handling in StreamRequest.jl --- src/ConnectionPool.jl | 3 + src/ConnectionRequest.jl | 8 +- src/HTTP.jl | 4 +- src/HTTPStreams.jl | 19 ++-- src/IOExtras.jl | 8 +- src/MessageRequest.jl | 9 +- src/RetryRequest.jl | 10 +- src/StreamRequest.jl | 109 +++++++++++------- src/debug.jl | 2 +- test/async.jl | 2 + test/loopback.jl | 235 +++++++++++++++++++++++++++++++++++++++ test/messages.jl | 33 +++++- test/runtests.jl | 2 +- 13 files changed, 376 insertions(+), 68 deletions(-) create mode 100644 test/loopback.jl diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 12b44ada7..e52ee16b9 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -214,6 +214,9 @@ end function Base.close(t::Transaction) close(t.c.io) ;@debug 2 "🚫 Closed: $t" + if iswritable(t) + closewrite(t) + end if isreadable(t) purge(t.c) closeread(t) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index fa573b9ae..54654aaff 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -12,7 +12,8 @@ abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end export ConnectionPoolLayer -sockettype(uri::URI) = uri.scheme in ("https", "wss") ? SSLContext : TCPSocket +sockettype(uri::URI, default) = uri.scheme in ("wss", "https") ? SSLContext : + default """ @@ -22,9 +23,10 @@ Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. """ function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; - connectionpool::Bool=true, kw...) where Next + connectionpool::Bool=true, socket_type::Type=TCPSocket, + kw...) where Next - SocketType = sockettype(uri) + SocketType = sockettype(uri, socket_type) if connectionpool SocketType = ConnectionPool.Transaction{SocketType} end diff --git a/src/HTTP.jl b/src/HTTP.jl index 7584df972..566567853 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -5,7 +5,7 @@ using MbedTLS import MbedTLS.SSLContext -const DEBUG_LEVEL = 0 +const DEBUG_LEVEL = 1 const minimal = false include("compat.jl") @@ -38,7 +38,7 @@ request(method::String, uri::URI, headers::Headers, body; kw...)::Response = request(HTTP.stack(;kw...), method, uri, headers, body; kw...) open(f::Function, method::String, uri, headers=[]; kw...)::Response = - request(method, uri, headers; iofunction=f, kw...) + request(method, uri, headers, nothing; iofunction=f, kw...) get(a...; kw...) = request("GET", a..., kw...) put(a...; kw...) = request("PUT", a..., kw...) diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index 1e4d0caf6..d92364d27 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -1,6 +1,6 @@ module HTTPStreams -export HTTPStream, closebody +export HTTPStream, closebody, isaborted using ..IOExtras using ..Parsers @@ -138,21 +138,26 @@ function Base.read(http::HTTPStream) end -function IOExtras.closeread(http::HTTPStream{Response}) +function isaborted(http::HTTPStream{Response}) # "If [the response] indicates the server does not wish to receive the - # message body and is closing the connection, the client SHOULD immediately - # cease transmitting the body and close its side of the connection." + # message body and is closing the connection, the client SHOULD + # immediately cease transmitting the body and close the connection." # https://tools.ietf.org/html/rfc7230#section-6.5 + if iswritable(http.stream) && iserror(http.message) && connectionclosed(http.parser) @debug 0 "✋ Abort on $(sprint(writestartline, http.message)): " * - "$(http.stream)" + "$(http.stream)" @debug 1 "✋ $(http.message)" - close(http.stream) - return http.message + return true end + return false +end + + +function IOExtras.closeread(http::HTTPStream{Response}) # Discard unread body bytes... while !eof(http) diff --git a/src/IOExtras.jl b/src/IOExtras.jl index 3a0569bfb..21105c6f3 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -1,8 +1,14 @@ module IOExtras -export unread!, startwrite, closewrite, startread, closeread, +export isioerror, unread!, startwrite, closewrite, startread, closeread, tcpsocket, localport, peerport +isioerror(e) = false +isioerror(::Base.EOFError) = true +isioerror(::Base.UVError) = true +isioerror(e::ArgumentError) = e.msg == "stream is closed or unusable" + + """ unread!(::IO, bytes) diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index e025758e2..dace969df 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -14,9 +14,11 @@ const ByteVector = Union{AbstractVector{UInt8}, AbstractString} const unknownlength = -1 bodylength(body) = unknownlength -bodylength(body::ByteVector) = sizeof(body) +bodylength(body::AbstractVector{UInt8}) = length(body) +bodylength(body::AbstractString) = sizeof(body) bodylength(body::Form) = length(body) -bodylength(body::Vector{ByteVector}) = sum(sizeof, body) +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) bodylength(body::Vector{IOBuffer}) = sum(nb_available, body) @@ -27,7 +29,8 @@ bodybytes(body) = body_is_a_stream bodybytes(body::Vector{UInt8}) = body bodybytes(body::IOBuffer) = read(body) bodybytes(body::ByteVector) = Vector{UInt8}(body) -bodybytes(body::Vector) = length(body) == 1 ? bodybytes(body[1]) : UInt8[] +bodybytes(body::Vector) = length(body) == 1 ? bodybytes(body[1]) : + body_is_a_stream function request(::Type{MessageLayer{Next}}, diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 44f5eaef5..a2cedcbd2 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -2,6 +2,7 @@ module RetryRequest import ..HTTP import ..Layer, ..request +using ..IOExtras.isioerror using ..MessageRequest using ..Messages import ..@debug, ..DEBUG_LEVEL @@ -10,15 +11,12 @@ abstract type RetryLayer{Next <: Layer} <: Layer end export RetryLayer -isrecoverable(e::Base.UVError) = true +isrecoverable(e::Exception) = isioerror(e) isrecoverable(e::Base.DNSError) = true -isrecoverable(e::Base.EOFError) = true -isrecoverable(e::Base.ArgumentError) = e.msg == "stream is closed or unusable" isrecoverable(e::HTTP.StatusError) = e.status == 403 || # Forbidden e.status == 408 || # Timeout e.status >= 500 # Server Error -isrecoverable(e::Exception) = false isrecoverable(e, req, retry_non_idempotent) = isrecoverable(e) && @@ -40,7 +38,7 @@ function request(::Type{RetryLayer{Next}}, uri, req, body; @debug 0 "🔄 Retry $ex: $(sprint(showcompact, req))" reset!(req.response) else - @debug 0 "🚷 No Retry $(no_retry_reason(ex, req))" + @debug 0 "🚷 No Retry: $(no_retry_reason(ex, req))" end return s, retry end) @@ -52,7 +50,7 @@ end function no_retry_reason(ex, req) buf = IOBuffer() showcompact(buf, req) - print(buf, ": ", + print(buf, ", ", ex isa HTTP.StatusError ? "HTTP $(ex.status): " : !isrecoverable(ex) ? "$ex not recoverable, " : "", (req.body === body_was_streamed) ? "request streamed, " : "", diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index d22253725..d29639b82 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -13,14 +13,6 @@ abstract type StreamLayer <: Layer end export StreamLayer -writebody(http, req, body) = for chunk in body write(http, req, chunk) end - -function writebody(http, req, body::IO) - req.body = body_was_streamed - write(http, body) -end - - """ request(StreamLayer, ::IO, ::Request, body) -> ::Response @@ -43,10 +35,33 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; http = HTTPStream(io, req, ConnectionPool.getparser(io)) startwrite(http) - if iofunction == nothing - default_iofunction(http, req, body, response_stream) - else - iofunction(http) + aborted = false + try + + @sync begin + if iofunction == nothing + @async writebody(http, req, body) + yield() + startread(http) + readbody(http, req.response, response_stream) + else + iofunction(http) + end + + if isaborted(http) + close(io) + aborted = true + end + end + + catch e + if aborted && + e isa CompositeException && + (ex = first(e.exceptions).ex; isioerror(ex)) + @debug 1 "⚠️ $(req.response.status) abort exception excpeted: $ex" + else + rethrow(e) + end end closewrite(http) @@ -59,38 +74,50 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; end -function default_iofunction(http::HTTPStream, req::Request, body, response_stream) - @sync begin - @async try - if req.body === body_is_a_stream - writebody(http, req, body) - closebody(http) - else - write(http, req.body) - end +function writebody(http::HTTPStream, req::Request, body) - if isidempotent(req) - closewrite(http) - else - # "A user agent SHOULD NOT pipeline requests after a - # non-idempotent method, until the final response - # status code for that method has been received" - # https://tools.ietf.org/html/rfc7230#section-6.3.2 - @debug 1 "🔒 $(req.method) non-idempotent, " * - "holding write lock: $(http.stream)" - end - end - yield() + if req.body === body_is_a_stream + writebodystream(http, req, body) + closebody(http) + else + write(http, req.body) + end - startread(http) - if response_stream == nothing - req.response.body = read(http) - else - req.response.body = body_was_streamed - write(response_stream, http) - end - closeread(http) + if isidempotent(req) + closewrite(http) + else + @debug 1 "🔒 $(req.method) non-idempotent, " * + "holding write lock: $(http.stream)" + # "A user agent SHOULD NOT pipeline requests after a + # non-idempotent method, until the final response + # status code for that method has been received" + # https://tools.ietf.org/html/rfc7230#section-6.3.2 + end +end + +function writebodystream(http, req, body) + for chunk in body + writechunk(http, req, chunk) + end +end + +function writebodystream(http, req, body::IO) + req.body = body_was_streamed + write(http, body) +end + +writechunk(http, req, body::IO) = writebodystream(http, req, body) +writechunk(http, req, body) = write(http, body) + + +function readbody(http::HTTPStream, res::Response, response_stream) + if response_stream == nothing + res.body = read(http) + else + res.body = body_was_streamed + write(response_stream, http) end end + end # module StreamRequest diff --git a/src/debug.jl b/src/debug.jl index b3d72b123..7c735a0dc 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -8,7 +8,7 @@ end macro debugshow(n::Int, s) DEBUG_LEVEL >= n ? :(println("DEBUG: ", taskid(), " ", - $(sprint(show_unquoted, s)), " = ", + $(sprint(Base.show_unquoted, s)), " = ", sprint(io->show(io, "text/plain", begin value=$(esc(s)) end)))) : :() diff --git a/test/async.jl b/test/async.jl index 138803da4..a63d274e9 100644 --- a/test/async.jl +++ b/test/async.jl @@ -171,6 +171,7 @@ println("running async $count, 1:$num, $config, $http") push!(result, r["headers"]["I"] => string(i)) catch e dump_async_exception(e, catch_stacktrace()) + rethrow(e) end end end @@ -193,6 +194,7 @@ println("running async $count, 1:$num, $config, $http") push!(result, length(r) => i) catch e dump_async_exception(e, catch_stacktrace()) + rethrow(e) end end end diff --git a/test/loopback.jl b/test/loopback.jl new file mode 100644 index 000000000..e626e6260 --- /dev/null +++ b/test/loopback.jl @@ -0,0 +1,235 @@ +using HTTP + +using HTTP.IOExtras +using HTTP.Parsers +using HTTP.Messages +using HTTP.MessageRequest.bodylength +using HTTP.Parsers.escapelines + + +mutable struct FunctionIO <: IO + f::Function + buf::IOBuffer + done::Bool +end + +FunctionIO(f::Function) = FunctionIO(f, IOBuffer(), false) +call(fio::FunctionIO) = !fio.done && + (fio.buf = IOBuffer(fio.f()) ; fio.done = true) +Base.eof(fio::FunctionIO) = (call(fio); eof(fio.buf)) +Base.nb_available(fio::FunctionIO) = (call(fio); nb_available(fio.buf)) +Base.readavailable(fio::FunctionIO) = (call(fio); readavailable(fio.buf)) +Base.read(fio::FunctionIO, a...) = (call(fio); read(fio.buf, a...)) + + +mutable struct Loopback <: IO + got_headers::Bool + buf::IOBuffer + io::BufferStream +end +Loopback() = Loopback(false, IOBuffer(), BufferStream()) + +function reset(lb::Loopback) + truncate(lb.buf, 0) + lb.got_headers = false +end + +Base.eof(lb::Loopback) = eof(lb.io) +Base.nb_available(lb::Loopback) = nb_available(lb.io) +Base.readavailable(lb::Loopback) = readavailable(lb.io) +Base.close(lb::Loopback) = (close(lb.io); close(lb.buf)) +Base.isopen(lb::Loopback) = isopen(lb.io) + + +function on_headers(f, lb) + if lb.got_headers + return + end + buf = copy(lb.buf) + seek(buf, 0) + req = Request() + try + readheaders(buf, Parser(), req) + lb.got_headers = true + catch e + if !(e isa EOFError || e isa HTTP.ParsingError) + rethrow(e) + end + end + if lb.got_headers + f(req) + end +end + +function on_body(f, lb) + s = String(take!(copy(lb.buf))) +# println("Request: \"\"\"") +# println(escapelines(s)) +# println("\"\"\"") + req = nothing + try + req = parse(HTTP.Request, s) + catch e + if !(e isa EOFError || e isa HTTP.ParsingError) + rethrow(e) + end + end + if req != nothing + reset(lb) + @schedule try + f(req) + catch e + println("⚠️ on_body exception: $e") + end + end +end + + +function Base.unsafe_write(lb::Loopback, p::Ptr{UInt8}, n::UInt) + + if !isopen(lb.buf) + throw(ArgumentError("stream is closed or unusable")) + end + + n = unsafe_write(lb.buf, p, n) + + on_headers(lb) do req + + println("📡 $(sprint(showcompact, req))") + + if req.uri == "/abort" + reset(lb) + response = HTTP.Response(403, ["Connection" => "close", + "Content-Length" => 0]) + write(lb.io, response) + end + end + + on_body(lb) do req + + l = length(req.body) + response = HTTP.Response(200, ["Content-Length" => l], + body = req.body) + if req.uri == "/echo" + write(lb.io, response) + elseif req.uri == "/delay" + sleep(0.1) + write(lb.io, response) + else + response = HTTP.Response(403, + ["Connection" => "close", + "Content-Length" => 0]) + write(lb.io, response) + end + end + + return n +end + +HTTP.IOExtras.tcpsocket(::Loopback) = TCPSocket() + +function HTTP.Connect.getconnection(::Type{Loopback}, + host::AbstractString, + port::AbstractString; + kw...)::Loopback + return Loopback() +end + +config = [ + :socket_type => Loopback, + :retry => false +] + +lbget(req, headers, body) = + HTTP.request("GET", "http://test/$req", headers, body; config...) + +lbopen(f, req, headers) = + HTTP.open(f, "GET", "http://test/$req", headers; config...) + +@testset "loopback" begin + + r = lbget("echo", [], ["Hello", IOBuffer(" "), "World!"]); + @test String(r.body) == "Hello World!" + + io = FunctionIO(()->"Hello World!") + @test String(read(io)) == "Hello World!" + + r = lbget("echo", [], FunctionIO(()->"Hello World!")) + @test String(r.body) == "Hello World!" + + + r = lbget("echo", [], ["Hello", " ", "World!"]); + @test String(r.body) == "Hello World!" + + r = lbget("echo", [], [Vector{UInt8}("Hello"), + Vector{UInt8}(" "), + Vector{UInt8}("World!")]); + @test String(r.body) == "Hello World!" + + r = lbget("delay", [], [Vector{UInt8}("Hello"), + Vector{UInt8}(" "), + Vector{UInt8}("World!")]); + @test String(r.body) == "Hello World!" + + + body = nothing + body_sent = false + r = lbopen("delay", []) do http + @sync begin + @async begin + write(http, "Hello World!") + closewrite(http) + body_sent = true + end + startread(http) + body = read(http) + closeread(http) + end + end + @test String(body) == "Hello World!" + + body = nothing + body_aborted = false + body_sent = false + @test_throws HTTP.StatusError begin + r = lbopen("abort", []) do http + @sync begin + @async try + sleep(0.1) + write(http, "Hello World!") + closewrite(http) + body_sent = true + catch e + if e isa ArgumentError && + e.msg == "stream is closed or unusable" + body_aborted = true + else + rethrow(e) + end + end + startread(http) + body = read(http) + closeread(http) + end + end + end + @test body_aborted == true + @test body_sent == false + + r = lbget("echo", [], [ + FunctionIO(()->(sleep(0.1); "Hello")), + FunctionIO(()->(sleep(0.1); " World!"))]) + @test String(r.body) == "Hello World!" + + hello_sent = false + world_sent = false + @test_throws HTTP.StatusError begin + r = lbget("abort", [], [ + FunctionIO(()->(hello_sent = true; sleep(0.1); "Hello")), + FunctionIO(()->(world_sent = true; " World!"))]) + end + @test hello_sent + @test !world_sent +end + + diff --git a/test/messages.jl b/test/messages.jl index f58e0686b..7542fed0d 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -12,10 +12,37 @@ import HTTP.request using HTTP.StatusError +using HTTP.MessageRequest.bodylength +using HTTP.MessageRequest.bodybytes +using HTTP.MessageRequest.unknownlength + using JSON @testset "HTTP.Messages" begin + @test bodylength(7) == unknownlength + @test bodylength(UInt8[1,2,3]) == 3 + @test bodylength(view(UInt8[1,2,3], 1:2)) == 2 + @test bodylength("Hello") == 5 + @test bodylength(SubString("World!",1,5)) == 5 + @test bodylength(["Hello", " ", "World!"]) == 12 + @test bodylength(["Hello", " ", SubString("World!",1,5)]) == 11 + @test bodylength([SubString("Hello", 1,5), " ", SubString("World!",1,5)]) == 11 + @test bodylength([UInt8[1,2,3], UInt8[4,5,6]]) == 6 + @test bodylength([UInt8[1,2,3], view(UInt8[4,5,6],1:2)]) == 5 + @test bodylength([view(UInt8[1,2,3],1:2), view(UInt8[4,5,6],1:2)]) == 4 + @test bodylength(IOBuffer("foo")) == 3 + @test bodylength([IOBuffer("foo"), IOBuffer("bar")]) == 6 + + @test bodybytes(7) == UInt8[] + @test bodybytes(UInt8[1,2,3]) == UInt8[1,2,3] + @test bodybytes(view(UInt8[1,2,3], 1:2)) == UInt8[1,2] + @test bodybytes("Hello") == Vector{UInt8}("Hello") + @test bodybytes(SubString("World!",1,5)) == Vector{UInt8}("World") + @test bodybytes(["Hello", " ", "World!"]) == UInt8[] + @test bodybytes([UInt8[1,2,3], UInt8[4,5,6]]) == UInt8[] + + req = Request("GET", "/foo", ["Foo" => "Bar"]) res = Response(200, ["Content-Length" => "5"]; body="Hello", request=req) @@ -41,7 +68,7 @@ using JSON appendheader(req, "Set-Cookie" => "A") appendheader(req, "Set-Cookie" => "B") - @test filter(x->first(x) == "Set-Cookie", req.headers) == + @test filter(x->first(x) == "Set-Cookie", req.headers) == ["Set-Cookie" => "A", "Set-Cookie" => "B"] @test Messages.httpversion(req) == "HTTP/1.1" @@ -69,7 +96,7 @@ using JSON for m in ["GET", "HEAD", "OPTIONS"] @test request(m, "$sch://httpbin.org/ip").status == 200 end - try + try request("POST", "$sch://httpbin.org/ip") @test false catch e @@ -88,7 +115,7 @@ using JSON end close(io) end - yield() + yield() r = request("POST", "http://httpbin.org/post", [], io) @test r.status == 200 end diff --git a/test/runtests.jl b/test/runtests.jl index 208b6101c..1822c6926 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,6 +16,7 @@ end @testset "HTTP" begin + include("loopback.jl"); include("utils.jl"); include("fifobuffer.jl"); @@ -29,7 +30,6 @@ end # include("handlers.jl") include("client.jl"); include("WebSockets.jl"); - include("async.jl"); # include("server.jl") end; From b7a1eb7e581f2b78cff7745b505ecd90c73cd5c4 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 11:17:19 +1100 Subject: [PATCH 106/182] dont set chunked header when upgrade header is set --- src/ConnectionPool.jl | 4 ++-- src/MessageRequest.jl | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index e52ee16b9..2ef8dd1cd 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -435,8 +435,8 @@ function Base.show(io::IO, c::Connection) inactiveseconds(c) > 5 ? ", inactive $(round(inactiveseconds(c),1))s" : "", nwaiting > 0 ? ", $nwaiting bytes waiting" : "", - DEBUG_LEVEL > 0 ? ", $(Base._fd(tcpsocket(c.io)))" : "", - DEBUG_LEVEL > 0 && + DEBUG_LEVEL > 1 ? ", $(Base._fd(tcpsocket(c.io)))" : "", + DEBUG_LEVEL > 1 && islocked(c.readlock) ? ", read task: $(taskid(c.readlock))" : "") end diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index dace969df..7602e4a21 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -42,7 +42,8 @@ function request(::Type{MessageLayer{Next}}, defaultheader(headers, "Host" => uri.host) if !hasheader(headers, "Content-Length") && - !hasheader(headers, "Transfer-Encoding") + !hasheader(headers, "Transfer-Encoding") && + !hasheader(headers, "Upgrade") l = bodylength(body) if l != unknownlength setheader(headers, "Content-Length" => string(l)) From 343d86024cece8e1a584d8dc69fa60c50d2511d7 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 12:02:08 +1100 Subject: [PATCH 107/182] add tests for pipeline_limit and duplicate_limit --- src/ConnectionPool.jl | 2 +- test/loopback.jl | 139 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 130 insertions(+), 11 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 2ef8dd1cd..57307274c 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -398,7 +398,7 @@ function getconnection(::Type{Transaction{T}}, # If there are not too many duplicates for this host, # create a new connection... busy = findall(T, host, port, pipeline_limit) - if length(busy) < duplicate_limit + if length(busy) < duplicate_limit + 1 io = getconnection(T, host, port; kw...) c = Connection{T}(host, port, pipeline_limit, io) push!(pool, c) ;@debug 1 "🔗 New: $c" diff --git a/test/loopback.jl b/test/loopback.jl index e626e6260..c3a4fbe96 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -41,6 +41,8 @@ Base.close(lb::Loopback) = (close(lb.io); close(lb.buf)) Base.isopen(lb::Loopback) = isopen(lb.io) +server_events = [] + function on_headers(f, lb) if lb.got_headers return @@ -87,20 +89,24 @@ end function Base.unsafe_write(lb::Loopback, p::Ptr{UInt8}, n::UInt) + global server_events + if !isopen(lb.buf) throw(ArgumentError("stream is closed or unusable")) end n = unsafe_write(lb.buf, p, n) - + on_headers(lb) do req println("📡 $(sprint(showcompact, req))") + push!(server_events, "Request: $(sprint(showcompact, req))") if req.uri == "/abort" reset(lb) response = HTTP.Response(403, ["Connection" => "close", - "Content-Length" => 0]) + "Content-Length" => 0]; request=req) + push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) end end @@ -109,16 +115,19 @@ function Base.unsafe_write(lb::Loopback, p::Ptr{UInt8}, n::UInt) l = length(req.body) response = HTTP.Response(200, ["Content-Length" => l], - body = req.body) + body = req.body; request=req) if req.uri == "/echo" + push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) - elseif req.uri == "/delay" - sleep(0.1) + elseif ismatch(r"^/delay", req.uri) + sleep(1) + push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) else response = HTTP.Response(403, ["Connection" => "close", - "Content-Length" => 0]) + "Content-Length" => 0]; request=req) + push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) end end @@ -137,17 +146,20 @@ end config = [ :socket_type => Loopback, - :retry => false + :retry => false, + :duplicate_limit => 0 ] -lbget(req, headers, body) = - HTTP.request("GET", "http://test/$req", headers, body; config...) +lbget(req, headers, body; kw...) = + HTTP.request("GET", "http://test/$req", headers, body; config..., kw...) lbopen(f, req, headers) = HTTP.open(f, "GET", "http://test/$req", headers; config...) @testset "loopback" begin + global server_events + r = lbget("echo", [], ["Hello", IOBuffer(" "), "World!"]); @test String(r.body) == "Hello World!" @@ -156,7 +168,6 @@ lbopen(f, req, headers) = r = lbget("echo", [], FunctionIO(()->"Hello World!")) @test String(r.body) == "Hello World!" - r = lbget("echo", [], ["Hello", " ", "World!"]); @test String(r.body) == "Hello World!" @@ -171,6 +182,7 @@ lbopen(f, req, headers) = Vector{UInt8}("World!")]); @test String(r.body) == "Hello World!" + HTTP.ConnectionPool.showpool(STDOUT) body = nothing body_sent = false @@ -230,6 +242,113 @@ lbopen(f, req, headers) = end @test hello_sent @test !world_sent + + HTTP.ConnectionPool.showpool(STDOUT) + + function async_test(;kw...) + r1 = nothing + r2 = nothing + r3 = nothing + r4 = nothing + r5 = nothing + t1 = time() + @sync begin + @async r1 = lbget("delay1", [], "Hello World! 1"; kw...) + sleep(0.01) + @async r2 = lbget("delay2", [], "Hello World! 2"; kw...) + sleep(0.01) + @async r3 = lbget("delay3", [], "Hello World! 3"; kw...) + sleep(0.01) + @async r4 = lbget("delay4", [], "Hello World! 4"; kw...) + sleep(0.01) + @async r5 = lbget("delay5", [], "Hello World! 5"; kw...) + end + t2 = time() + + @test String(r1.body) == "Hello World! 1" + @test String(r2.body) == "Hello World! 2" + @test String(r3.body) == "Hello World! 3" + @test String(r4.body) == "Hello World! 4" + @test String(r5.body) == "Hello World! 5" + + return t2 - t1 + end + + + server_events = [] + t = async_test(;pipeline_limit=0) + @test 4 < t < 6 + @test server_events == [ + "Request: GET /delay1 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", + "Request: GET /delay2 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Request: GET /delay3 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", + "Request: GET /delay4 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] + + server_events = [] + t = async_test(;pipeline_limit=1) + @test 2 < t < 4 + @test server_events == [ + "Request: GET /delay1 HTTP/1.1", + "Request: GET /delay2 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", + "Request: GET /delay3 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Request: GET /delay4 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] + + server_events = [] + t = async_test(;pipeline_limit=2) + @test 1 < t < 3 + @test server_events == [ + "Request: GET /delay1 HTTP/1.1", + "Request: GET /delay2 HTTP/1.1", + "Request: GET /delay3 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", + "Request: GET /delay4 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] + + server_events = [] + t = async_test(;pipeline_limit=3) + @test 1 < t < 3 + @test server_events == [ + "Request: GET /delay1 HTTP/1.1", + "Request: GET /delay2 HTTP/1.1", + "Request: GET /delay3 HTTP/1.1", + "Request: GET /delay4 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] + + server_events = [] + t = async_test() + @test 1 < t < 2 + @test server_events == [ + "Request: GET /delay1 HTTP/1.1", + "Request: GET /delay2 HTTP/1.1", + "Request: GET /delay3 HTTP/1.1", + "Request: GET /delay4 HTTP/1.1", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] end From 6dd280761cc34fa6168e25dbe358d7df1c67e6e6 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 12:49:00 +1100 Subject: [PATCH 108/182] Pass Content-Length:0 to S3 GET in tests to avoid automatic chunked header. Tweak debug levels. --- src/ConnectionPool.jl | 22 +++++++++++----------- src/ConnectionRequest.jl | 2 +- src/HTTPStreams.jl | 20 ++++++++------------ src/RetryRequest.jl | 4 ++-- src/StreamRequest.jl | 2 +- src/TimeoutRequest.jl | 2 +- test/async.jl | 7 ++++--- 7 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 57307274c..15f9ad38d 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -10,7 +10,7 @@ import ..Connect: getconnection, getparser, getrawstream, inactiveseconds import ..Parsers.Parser -const default_duplicate_limit = 8 +const default_duplicate_limit = 7 const default_pipeline_limit = 16 const nolimit = typemax(Int) @@ -104,7 +104,7 @@ function Base.eof(t::Transaction) @require isreadable(t) || !isopen(t) if nb_available(t) > 0 return false - end ;@debug 3 "eof(::Transaction) -> eof($typeof(c.io)): $t" + end ;@debug 4 "eof(::Transaction) -> eof($typeof(c.io)): $t" return eof(t.c.io) end @@ -123,11 +123,11 @@ function Base.readavailable(t::Transaction)::ByteView @require isreadable(t) if !isempty(t.c.excess) bytes = t.c.excess - @debug 3 "↩️ read $(length(bytes))-bytes from excess buffer." + @debug 4 "↩️ read $(length(bytes))-bytes from excess buffer." t.c.excess = nobytes else bytes = byteview(readavailable(t.c.io)) - @debug 3 "⬅️ read $(length(bytes))-bytes from $(typeof(t.c.io))" + @debug 4 "⬅️ read $(length(bytes))-bytes from $(typeof(t.c.io))" end t.c.timestamp = time() return bytes @@ -164,7 +164,7 @@ Increment `writecount` and wait for pending reads to complete. function IOExtras.closewrite(t::Transaction) @require iswritable(t) - t.c.writecount += 1 ;@debug 2 "🗣 Write done: $t" + t.c.writecount += 1 ;@debug 3 "🗣 Write done: $t" t.c.writebusy = false notify(poolcondition) @@ -185,9 +185,9 @@ function IOExtras.startread(t::Transaction) lock(t.c.readlock) while t.c.readcount != t.sequence unlock(t.c.readlock) - yield() ;@debug 0 "⏳ Waiting to read: $t" + yield() ;@debug 1 "⏳ Waiting to read: $t" lock(t.c.readlock) - end ;@debug 1 "👁 Start read: $t" + end ;@debug 2 "👁 Start read: $t" @assert isreadable(t) return end @@ -206,14 +206,14 @@ Increment `readcount` and wake up tasks waiting in `closewrite`. function IOExtras.closeread(t::Transaction) @require isreadable(t) t.c.readcount += 1 - unlock(t.c.readlock) ;@debug 2 "✉️ Read done: $t" + unlock(t.c.readlock) ;@debug 3 "✉️ Read done: $t" notify(poolcondition) @assert !isreadable(t) return end function Base.close(t::Transaction) - close(t.c.io) ;@debug 2 "🚫 Closed: $t" + close(t.c.io) ;@debug 3 "🚫 Closed: $t" if iswritable(t) closewrite(t) end @@ -391,7 +391,7 @@ function getconnection(::Type{Transaction{T}}, writable = findwritable(T, host, port, pipeline_limit, reuse_limit) idle = filter(c->!islocked(c.readlock), writable) if !isempty(idle) - c = rand(idle) ;@debug 1 "♻️ Idle: $c" + c = rand(idle) ;@debug 2 "♻️ Idle: $c" return Transaction{T}(c) end @@ -407,7 +407,7 @@ function getconnection(::Type{Transaction{T}}, # Share a connection that has active readers... if !isempty(writable) - c = rand(writable) ;@debug 1 "⇆ Shared: $c" + c = rand(writable) ;@debug 2 "⇆ Shared: $c" return Transaction{T}(c) end diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 54654aaff..4f8dab8d6 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -39,7 +39,7 @@ function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; end return r catch e - @debug 0 "❗️ ConnectionLayer $e. Closing: $io" + @debug 1 "❗️ ConnectionLayer $e. Closing: $io" close(io) rethrow(e) end diff --git a/src/HTTPStreams.jl b/src/HTTPStreams.jl index d92364d27..e02974a7d 100644 --- a/src/HTTPStreams.jl +++ b/src/HTTPStreams.jl @@ -148,9 +148,9 @@ function isaborted(http::HTTPStream{Response}) if iswritable(http.stream) && iserror(http.message) && connectionclosed(http.parser) - @debug 0 "✋ Abort on $(sprint(writestartline, http.message)): " * + @debug 1 "✋ Abort on $(sprint(writestartline, http.message)): " * "$(http.stream)" - @debug 1 "✋ $(http.message)" + @debug 2 "✋ $(http.message)" return true end return false @@ -169,20 +169,16 @@ function IOExtras.closeread(http::HTTPStream{Response}) readtrailers(http.stream, http.parser, http.message) end - if isreadable(http.stream) - closeread(http.stream) - end - - # Error if Message is not complete... if !messagecomplete(http.parser) + # Error if Message is not complete... close(http.stream) throw(EOFError()) - end - - # Close conncetion if server sent "Connection: close"... - if connectionclosed(http.parser) - @debug 0 "✋ \"Connection: close\": $(http.stream)" + elseif connectionclosed(http.parser) + # Close conncetion if server sent "Connection: close"... + @debug 1 "✋ \"Connection: close\": $(http.stream)" close(http.stream) + elseif isreadable(http.stream) + closeread(http.stream) end return http.message diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index a2cedcbd2..3dcb19c0e 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -35,10 +35,10 @@ function request(::Type{RetryLayer{Next}}, uri, req, body; check=(s,ex)->begin retry = isrecoverable(ex, req, retry_non_idempotent) if retry - @debug 0 "🔄 Retry $ex: $(sprint(showcompact, req))" + @debug 1 "🔄 Retry $ex: $(sprint(showcompact, req))" reset!(req.response) else - @debug 0 "🚷 No Retry: $(no_retry_reason(ex, req))" + @debug 1 "🚷 No Retry: $(no_retry_reason(ex, req))" end return s, retry end) diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index d29639b82..c20e92162 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -86,7 +86,7 @@ function writebody(http::HTTPStream, req::Request, body) if isidempotent(req) closewrite(http) else - @debug 1 "🔒 $(req.method) non-idempotent, " * + @debug 2 "🔒 $(req.method) non-idempotent, " * "holding write lock: $(http.stream)" # "A user agent SHOULD NOT pipeline requests after a # non-idempotent method, until the final response diff --git a/src/TimeoutRequest.jl b/src/TimeoutRequest.jl index e2fcc6a7f..f49442715 100644 --- a/src/TimeoutRequest.jl +++ b/src/TimeoutRequest.jl @@ -24,7 +24,7 @@ function request(::Type{TimeoutLayer{Next}}, io::IO, req, body; @async while wait_for_timeout[] if isreadable(io) && inactiveseconds(io) > timeout close(io) - @debug 0 "💥 Read inactive > $(timeout)s: $io" + @debug 1 "💥 Read inactive > $(timeout)s: $io" break end sleep(8 + rand() * 4) diff --git a/test/async.jl b/test/async.jl index a63d274e9..fb10f0e9f 100644 --- a/test/async.jl +++ b/test/async.jl @@ -1,3 +1,4 @@ +using HTTP using JSON using MbedTLS: digest, MD_MD5, MD_SHA256 using Base64 @@ -46,7 +47,7 @@ end @testset "async s3 dup$dup, count$count, sz$sz, pipw$pipe, $http, $mode" for count in [10, 100, 1000, 2000], - dup in [1, 8], + dup in [0, 7], http in ["http", "https"], sz in [100, 1000, 10000], mode in [:request, :open], @@ -67,7 +68,7 @@ println("running async s3 dup$dup, count$count, sz$sz, pipe$pipe, $http, $mode") put_data_sums = Dict() ch = 100 conf = [:reuse_limit => 90, - :verbose => 0, + :verbose => 1, :pipeline_limit => pipe, :duplicate_limit => dup, :timeout => 120] @@ -112,7 +113,7 @@ get_data_sums = Dict() buf = IOBuffer() r = nothing if mode == :open - r = HTTP.open("GET", url; + r = HTTP.open("GET", url, ["Content-Length" => 0]; awsauthorization=true, conf...) do http truncate(buf, 0) # in case of retry! From d34e9e11c3838aa2d497fcc5943990aa7540c90d Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 13:00:28 +1100 Subject: [PATCH 109/182] added tests for no pipelinging after non-idempotent --- test/loopback.jl | 71 +++++++++++++++++++++++++++++++++++++----------- test/messages.jl | 7 ----- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/test/loopback.jl b/test/loopback.jl index c3a4fbe96..13e6ff6e2 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -150,8 +150,8 @@ config = [ :duplicate_limit => 0 ] -lbget(req, headers, body; kw...) = - HTTP.request("GET", "http://test/$req", headers, body; config..., kw...) +lbreq(req, headers, body; method="GET", kw...) = + HTTP.request(method, "http://test/$req", headers, body; config..., kw...) lbopen(f, req, headers) = HTTP.open(f, "GET", "http://test/$req", headers; config...) @@ -160,24 +160,24 @@ lbopen(f, req, headers) = global server_events - r = lbget("echo", [], ["Hello", IOBuffer(" "), "World!"]); + r = lbreq("echo", [], ["Hello", IOBuffer(" "), "World!"]); @test String(r.body) == "Hello World!" io = FunctionIO(()->"Hello World!") @test String(read(io)) == "Hello World!" - r = lbget("echo", [], FunctionIO(()->"Hello World!")) + r = lbreq("echo", [], FunctionIO(()->"Hello World!")) @test String(r.body) == "Hello World!" - r = lbget("echo", [], ["Hello", " ", "World!"]); + r = lbreq("echo", [], ["Hello", " ", "World!"]); @test String(r.body) == "Hello World!" - r = lbget("echo", [], [Vector{UInt8}("Hello"), + r = lbreq("echo", [], [Vector{UInt8}("Hello"), Vector{UInt8}(" "), Vector{UInt8}("World!")]); @test String(r.body) == "Hello World!" - r = lbget("delay", [], [Vector{UInt8}("Hello"), + r = lbreq("delay", [], [Vector{UInt8}("Hello"), Vector{UInt8}(" "), Vector{UInt8}("World!")]); @test String(r.body) == "Hello World!" @@ -200,6 +200,13 @@ lbopen(f, req, headers) = end @test String(body) == "Hello World!" + + + # "If [the response] indicates the server does not wish to receive the + # message body and is closing the connection, the client SHOULD + # immediately cease transmitting the body and close the connection." + # https://tools.ietf.org/html/rfc7230#section-6.5 + body = nothing body_aborted = false body_sent = false @@ -228,7 +235,7 @@ lbopen(f, req, headers) = @test body_aborted == true @test body_sent == false - r = lbget("echo", [], [ + r = lbreq("echo", [], [ FunctionIO(()->(sleep(0.1); "Hello")), FunctionIO(()->(sleep(0.1); " World!"))]) @test String(r.body) == "Hello World!" @@ -236,7 +243,7 @@ lbopen(f, req, headers) = hello_sent = false world_sent = false @test_throws HTTP.StatusError begin - r = lbget("abort", [], [ + r = lbreq("abort", [], [ FunctionIO(()->(hello_sent = true; sleep(0.1); "Hello")), FunctionIO(()->(world_sent = true; " World!"))]) end @@ -245,7 +252,7 @@ lbopen(f, req, headers) = HTTP.ConnectionPool.showpool(STDOUT) - function async_test(;kw...) + function async_test(m=["GET","GET","GET","GET","GET"];kw...) r1 = nothing r2 = nothing r3 = nothing @@ -253,15 +260,15 @@ lbopen(f, req, headers) = r5 = nothing t1 = time() @sync begin - @async r1 = lbget("delay1", [], "Hello World! 1"; kw...) + @async r1 = lbreq("delay1", [], "Hello World! 1"; method=m[1], kw...) sleep(0.01) - @async r2 = lbget("delay2", [], "Hello World! 2"; kw...) + @async r2 = lbreq("delay2", [], "Hello World! 2"; method=m[2], kw...) sleep(0.01) - @async r3 = lbget("delay3", [], "Hello World! 3"; kw...) + @async r3 = lbreq("delay3", [], "Hello World! 3"; method=m[3], kw...) sleep(0.01) - @async r4 = lbget("delay4", [], "Hello World! 4"; kw...) + @async r4 = lbreq("delay4", [], "Hello World! 4"; method=m[4], kw...) sleep(0.01) - @async r5 = lbget("delay5", [], "Hello World! 5"; kw...) + @async r5 = lbreq("delay5", [], "Hello World! 5"; method=m[5], kw...) end t2 = time() @@ -349,6 +356,38 @@ lbopen(f, req, headers) = "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] -end + # "A user agent SHOULD NOT pipeline requests after a + # non-idempotent method, until the final response + # status code for that method has been received" + # https://tools.ietf.org/html/rfc7230#section-6.3.2 + + server_events = [] + t = async_test(["POST","GET","GET","GET","GET"]) + @test server_events == [ + "Request: POST /delay1 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (POST /delay1 HTTP/1.1)", + "Request: GET /delay2 HTTP/1.1", + "Request: GET /delay3 HTTP/1.1", + "Request: GET /delay4 HTTP/1.1", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] + + server_events = [] + t = async_test(["GET","GET","POST", "GET","GET"]) + @test server_events == [ + "Request: GET /delay1 HTTP/1.1", + "Request: GET /delay2 HTTP/1.1", + "Request: POST /delay3 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (POST /delay3 HTTP/1.1)", + "Request: GET /delay4 HTTP/1.1", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] +end diff --git a/test/messages.jl b/test/messages.jl index 7542fed0d..2ec019ed5 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -173,13 +173,6 @@ using JSON end end -#= FIXME -Pipelineing tests - - Test pipeline_limit option - - Test early body send abort - - Test no pipelinging after non-idempotent -=# - end end # module MessagesTest From f44d68dc2ad9c35e33d9df66fe892e28480af013 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 14:27:41 +1100 Subject: [PATCH 110/182] Fix for closed connections stuck in pool. When connection failed during write, readcount was one less than writecount. Remove read/write count check from purge. Now simply remove closed connections from pool. Any outstanding Transactions have their own reference to the Connection anyway. --- src/ConnectionPool.jl | 27 ++++++++++++++++++--------- src/IOExtras.jl | 1 + test/async.jl | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 15f9ad38d..6f5cb573d 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -144,12 +144,14 @@ Push bytes back into a connection's `excess` buffer function IOExtras.unread!(t::Transaction, bytes::ByteView) @require isreadable(t) t.c.excess = bytes + return end function IOExtras.startwrite(t::Transaction) @require !t.c.writebusy t.c.writebusy = true + return end @@ -169,6 +171,7 @@ function IOExtras.closewrite(t::Transaction) notify(poolcondition) @assert !iswritable(t) + return end @@ -205,33 +208,40 @@ Increment `readcount` and wake up tasks waiting in `closewrite`. function IOExtras.closeread(t::Transaction) @require isreadable(t) + t.c.readcount += 1 unlock(t.c.readlock) ;@debug 3 "✉️ Read done: $t" notify(poolcondition) + @assert !isreadable(t) return end function Base.close(t::Transaction) - close(t.c.io) ;@debug 3 "🚫 Closed: $t" + close(t.c) if iswritable(t) closewrite(t) end if isreadable(t) - purge(t.c) closeread(t) end - notify(poolcondition) return end -Base.close(c::Connection) = Base.close(c.io) +function Base.close(c::Connection) + if nb_available(c) > 0 + purge(c) + end + close(c.io) + notify(poolcondition) + return +end """ - purge(::Transaction) + purge(::Connection) -Remove unread data from a `Transaction`. +Remove unread data from a `Connection`. """ function purge(c::Connection) @@ -274,6 +284,7 @@ function closeall() end empty!(pool) unlock(poollock) + notify(poolcondition) return end @@ -347,10 +358,8 @@ end Remove closed connections from `pool`. """ function purge() - while (i = findfirst(x->!isopen(x.io) && - x.readcount >= x.writecount, pool)) > 0 + while (i = findfirst(x->!isopen(x.io), pool)) > 0 c = pool[i] - purge(c) deleteat!(pool, i) ;@debug 1 "🗑 Deleted: $c" end end diff --git a/src/IOExtras.jl b/src/IOExtras.jl index 21105c6f3..fedfaad86 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -54,6 +54,7 @@ function unread!(io, bytes) end println("WARNING: No unread! method for $(typeof(io))!") println(" Discarding $(length(bytes)) bytes!") + return end diff --git a/test/async.jl b/test/async.jl index fb10f0e9f..99b41e187 100644 --- a/test/async.jl +++ b/test/async.jl @@ -68,7 +68,7 @@ println("running async s3 dup$dup, count$count, sz$sz, pipe$pipe, $http, $mode") put_data_sums = Dict() ch = 100 conf = [:reuse_limit => 90, - :verbose => 1, + :verbose => 0, :pipeline_limit => pipe, :duplicate_limit => dup, :timeout => 120] From 1cfdd96da0b121d19382777138b44c9b58b885c7 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 14:40:29 +1100 Subject: [PATCH 111/182] loopback test connection pool cleanup --- test/loopback.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/loopback.jl b/test/loopback.jl index 13e6ff6e2..b769ab0fe 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -40,6 +40,8 @@ Base.readavailable(lb::Loopback) = readavailable(lb.io) Base.close(lb::Loopback) = (close(lb.io); close(lb.buf)) Base.isopen(lb::Loopback) = isopen(lb.io) +HTTP.ConnectionPool.tcpstatus(c::HTTP.ConnectionPool.Connection{Loopback}) = "🤖 " + server_events = [] @@ -390,4 +392,6 @@ lbopen(f, req, headers) = "Request: GET /delay5 HTTP/1.1", "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] + + HTTP.ConnectionPool.closeall() end From a42f1d6d8aa6aea1847086bd24becde186476b9a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 18:14:01 +1100 Subject: [PATCH 112/182] close before purge in close(c::Connection) --- src/ConnectionPool.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 6f5cb573d..5f02e4231 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -166,7 +166,7 @@ Increment `writecount` and wait for pending reads to complete. function IOExtras.closewrite(t::Transaction) @require iswritable(t) - t.c.writecount += 1 ;@debug 3 "🗣 Write done: $t" + t.c.writecount += 1 ;@debug 2 "🗣 Write done: $t" t.c.writebusy = false notify(poolcondition) @@ -210,7 +210,7 @@ function IOExtras.closeread(t::Transaction) @require isreadable(t) t.c.readcount += 1 - unlock(t.c.readlock) ;@debug 3 "✉️ Read done: $t" + unlock(t.c.readlock) ;@debug 2 "✉️ Read done: $t" notify(poolcondition) @assert !isreadable(t) @@ -229,10 +229,10 @@ function Base.close(t::Transaction) end function Base.close(c::Connection) + close(c.io) if nb_available(c) > 0 purge(c) end - close(c.io) notify(poolcondition) return end From 2e4e4edbcb4d7171d9232378da50addf278d8458 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 18:14:51 +1100 Subject: [PATCH 113/182] Set Content-Length: 0 by default for iofunction GET requests --- src/MessageRequest.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 7602e4a21..c97ef3ecf 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -35,7 +35,7 @@ bodybytes(body::Vector) = length(body) == 1 ? bodybytes(body[1]) : function request(::Type{MessageLayer{Next}}, method::String, uri::URI, headers::Headers, body; - parent=nothing, kw...) where Next + parent=nothing, iofunction=nothing, kw...) where Next path = method == "CONNECT" ? hostport(uri) : resource(uri) @@ -47,6 +47,8 @@ function request(::Type{MessageLayer{Next}}, l = bodylength(body) if l != unknownlength setheader(headers, "Content-Length" => string(l)) + elseif method == "GET" && iofunction isa Function + setheader(headers, "Content-Length" => 0) else setheader(headers, "Transfer-Encoding" => "chunked") end @@ -54,7 +56,7 @@ function request(::Type{MessageLayer{Next}}, req = Request(method, path, headers, bodybytes(body); parent=parent) - return request(Next, uri, req, body; kw...) + return request(Next, uri, req, body; iofunction=iofunction, kw...) end From 15232fd515d113afdc4ee2172e04850f4057c4e4 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 18:15:16 +1100 Subject: [PATCH 114/182] tweak delays in loopback test to remove race conditions --- test/loopback.jl | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/test/loopback.jl b/test/loopback.jl index b769ab0fe..ca57cbad8 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -122,7 +122,7 @@ function Base.unsafe_write(lb::Loopback, p::Ptr{UInt8}, n::UInt) push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) elseif ismatch(r"^/delay", req.uri) - sleep(1) + sleep(0.5) push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) else @@ -262,15 +262,26 @@ lbopen(f, req, headers) = r5 = nothing t1 = time() @sync begin - @async r1 = lbreq("delay1", [], "Hello World! 1"; method=m[1], kw...) + @async r1 = lbreq("delay1", [], + FunctionIO(()->(sleep(0.01); "Hello World! 1")); + method=m[1], kw...) sleep(0.01) - @async r2 = lbreq("delay2", [], "Hello World! 2"; method=m[2], kw...) + @async r2 = lbreq("delay2", [], + FunctionIO(()->(sleep(0.02); "Hello World! 2")); + method=m[2], kw...) sleep(0.01) - @async r3 = lbreq("delay3", [], "Hello World! 3"; method=m[3], kw...) + @async r3 = lbreq("delay3", [], + FunctionIO(()->(sleep(0.03); "Hello World! 3")); + method=m[3], kw...) sleep(0.01) - @async r4 = lbreq("delay4", [], "Hello World! 4"; method=m[4], kw...) + @async r4 = lbreq("delay4", [], + FunctionIO(()->(sleep(0.04); "Hello World! 4")); + method=m[4], kw...) + sleep(0.01) + @async r5 = lbreq("delay5", [], + FunctionIO(()->(sleep(0.05); "Hello World! 5")); + method=m[5], kw...) sleep(0.01) - @async r5 = lbreq("delay5", [], "Hello World! 5"; method=m[5], kw...) end t2 = time() @@ -286,7 +297,8 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=0) - @test 4 < t < 6 + @show t + @test 2.8 < t < 3.3 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", @@ -301,7 +313,8 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=1) - @test 2 < t < 4 + @show t + @test 1.4 < t < 1.8 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -316,7 +329,8 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=2) - @test 1 < t < 3 + @show t + @test 1 < t < 1.4 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -331,7 +345,8 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=3) - @test 1 < t < 3 + @show t + @test 0.8 < t < 1.2 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -346,7 +361,8 @@ lbopen(f, req, headers) = server_events = [] t = async_test() - @test 1 < t < 2 + @show t + @test 0.6 < t < 1 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", From 27d665bcc95447a94776c03489b35facaf1ad7fc Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 18:17:16 +1100 Subject: [PATCH 115/182] dont run AWS S3 tests if creds ENV not set --- test/async.jl | 28 ++++++++++++++-------------- test/runtests.jl | 13 ++++++------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/test/async.jl b/test/async.jl index 99b41e187..f2b8631dc 100644 --- a/test/async.jl +++ b/test/async.jl @@ -45,19 +45,16 @@ function dump_async_exception(e, st) print(String(take!(buf))) end +if haskey(ENV, "AWS_ACCESS_KEY_ID") @testset "async s3 dup$dup, count$count, sz$sz, pipw$pipe, $http, $mode" for - count in [10, 100, 1000, 2000], + count in [10, 100, 1000], dup in [0, 7], http in ["http", "https"], - sz in [100, 1000, 10000], + sz in [100, 10000], mode in [:request, :open], pipe in [0, 32] -if (dup == 1 || pipe == 0) && count > 100 - continue -end - -if count == 2000 && (sz != 1000 || pipe != 32) +if (dup == 0 || pipe == 0) && count > 100 continue end @@ -144,12 +141,13 @@ for i = 1:count @test a == put_data_sums[i] end -end +end # testset +end # if haskey(ENV, "AWS_ACCESS_KEY_ID") configs = [ - [], - [:reuse_limit => 200], - [:reuse_limit => 50] + [:verbose => 0], + [:verbose => 0, :reuse_limit => 200], + [:verbose => 0, :reuse_limit => 50] ] @@ -158,9 +156,7 @@ configs = [ config in configs, http in ["http", "https"] -println("running async $count, 1:$num, $config, $http") - - +println("running async $count, 1:$num, $config, $http A") result = [] @sync begin @@ -185,6 +181,8 @@ println("running async $count, 1:$num, $config, $http") result = [] +println("running async $count, 1:$num, $config, $http B") + @sync begin for i = 1:min(num,100) @async try @@ -231,6 +229,8 @@ println("running async $count, 1:$num, $config, $http") result = [] =# +println("running async $count, 1:$num, $config, $http C") + @sync begin for i = 1:num n = i % 20 + 1 diff --git a/test/runtests.jl b/test/runtests.jl index 1822c6926..0a0d1847d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,20 +16,19 @@ end @testset "HTTP" begin - include("loopback.jl"); - include("utils.jl"); include("fifobuffer.jl"); include("sniff.jl"); include("uri.jl"); include("cookies.jl"); include("parser.jl"); -# include("body.jl"); - include("messages.jl"); -# include("types.jl"); -# include("handlers.jl") - include("client.jl"); + + include("loopback.jl"); include("WebSockets.jl"); include("async.jl"); + include("messages.jl"); + include("client.jl"); + +# include("handlers.jl") # include("server.jl") end; From a4ed450aa9eeaa6ade5d7189791063cc9b8bd73d Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 18:19:39 +1100 Subject: [PATCH 116/182] whoops --- src/MessageRequest.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index c97ef3ecf..925350368 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -48,7 +48,7 @@ function request(::Type{MessageLayer{Next}}, if l != unknownlength setheader(headers, "Content-Length" => string(l)) elseif method == "GET" && iofunction isa Function - setheader(headers, "Content-Length" => 0) + setheader(headers, "Content-Length" => "0") else setheader(headers, "Transfer-Encoding" => "chunked") end From 941c6e6cacbd10c79889f7624d5672030b08c5ec Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 3 Jan 2018 22:29:28 +1100 Subject: [PATCH 117/182] docstring cleanup, work in progress --- docs/src/index.md | 32 ++++++- src/AWS4AuthRequest.jl | 49 ++++++----- src/BasicAuthRequest.jl | 8 +- src/CanonicalizeRequest.jl | 12 ++- src/Connect.jl | 4 +- src/ConnectionPool.jl | 6 +- src/ConnectionRequest.jl | 20 +++-- src/CookieRequest.jl | 48 ++++++----- src/ExceptionRequest.jl | 21 +++-- src/HTTP.jl | 168 ++++++++++++++++++++++++++++++++++++- src/MessageRequest.jl | 53 +++++++----- src/RedirectRequest.jl | 12 ++- src/RetryRequest.jl | 38 +++++---- src/StreamRequest.jl | 15 ++-- src/TimeoutRequest.jl | 12 ++- src/client.jl | 4 +- test/loopback.jl | 2 +- 17 files changed, 376 insertions(+), 128 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index e1730ecfe..016873714 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -5,6 +5,34 @@ ```@contents ``` +## Requests + + +```@docs +HTTP.request(::String,::HTTP.URIs.URI,::Array{Pair{String,String},1},::Any) +HTTP.get +HTTP.put +HTTP.post +HTTP.head +``` + +## Layers + +```@docs +HTTP.RedirectLayer +HTTP.BasicAuthLayer +HTTP.CookieLayer +HTTP.CanonicalizeLayer +HTTP.MessageLayer +HTTP.AWS4AuthLayer +HTTP.RetryLayer +HTTP.ExceptionLayer +HTTP.ConnectionPoolLayer +HTTP.TimeoutLayer +HTTP.StreamLayer +``` + + ## Requests Note that the HTTP methods of POST, DELETE, PUT, etc. all follow the same format as `HTTP.get`, documented below. ```@docs @@ -542,7 +570,7 @@ the Response Body to happen in a background task. HTTP.Parser HTTP.Parsers.Message HTTP.Parsers.parse! -Base.read!(::IO, ::HTTP.Parser) +Base.read!(::IO, ::HTTP.Parsers.Parser) HTTP.Parsers.messagecomplete HTTP.Parsers.headerscomplete HTTP.Parsers.waitingforeof @@ -584,7 +612,7 @@ HTTP.Messages.method ### Body -```@docs +``` HTTP.Messages.Bodies.Body HTTP.Messages.Bodies.isstream Base.write(::HTTP.Messages.Bodies.Body, ::Any) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index e0ca66f83..46cfddc3f 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -12,28 +12,46 @@ using ..URIs using ..Pairs: getkv, setkv, rmkv import ..@debug, ..DEBUG_LEVEL + +""" + request(AWS4AuthLayer, ::URI, ::Request, body) -> HTTP.Response + +Add a AWS Signature Version 4 `Authorization` header to a `Request`. + +Credentials are read from environment variables `AWS_ACCESS_KEY_ID`, +`AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN`. + +See [http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html](@ref) +""" + abstract type AWS4AuthLayer{Next <: Layer} <: Layer end export AWS4AuthLayer +function request(::Type{AWS4AuthLayer{Next}}, + uri::URI, req, body; kw...) where Next + + sign_aws4!(req.method, uri, req.headers, req.body; kw...) + + return request(Next, uri, req, body; kw...) +end + + ispathsafe(c::Char) = c == '/' || URIs.issafe(c) escape_path(path) = escapeuri(path, ispathsafe) -# Create AWS Signature Version 4 Authentication Headers. -# http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html - function sign_aws4!(method::String, uri::URI, headers::Headers, body::Vector{UInt8}; - body_sha256=digest(MD_SHA256, body), - body_md5=digest(MD_MD5, body), - t=now(Dates.UTC), - aws_service=split(uri.host, ".")[1], - aws_region=split(uri.host, ".")[2], - aws_access_key_id=ENV["AWS_ACCESS_KEY_ID"], - aws_secret_access_key=ENV["AWS_SECRET_ACCESS_KEY"], - aws_session_token=get(ENV, "AWS_SESSION_TOKEN", ""), + body_sha256::Vector{UInt8}=digest(MD_SHA256, body), + body_md5::Vector{UInt8}=digest(MD_MD5, body), + t::DateTime=now(Dates.UTC), + aws_service::String=String(split(uri.host, ".")[1]), + aws_region::String=String(split(uri.host, ".")[2]), + aws_access_key_id::String=ENV["AWS_ACCESS_KEY_ID"], + aws_secret_access_key::String=ENV["AWS_SECRET_ACCESS_KEY"], + aws_session_token::String=get(ENV, "AWS_SESSION_TOKEN", ""), kw...) @@ -102,13 +120,4 @@ function sign_aws4!(method::String, end -function request(::Type{AWS4AuthLayer{Next}}, - uri::URI, req, body; kw...) where Next - - sign_aws4!(req.method, uri, req.headers, req.body; kw...) - - return request(Next, uri, req, body; kw...) -end - - end # module BasicAuthRequest diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index e50a62754..220127a88 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -9,10 +9,16 @@ using ..URIs using ..Pairs: getkv, setkv import ..@debug, ..DEBUG_LEVEL + +""" + request(BasicAuthLayer, method, ::URI, headers, body) -> HTTP.Response + +Add `Authorization: Basic` header using credentials from url userinfo. +""" + abstract type BasicAuthLayer{Next <: Layer} <: Layer end export BasicAuthLayer - function request(::Type{BasicAuthLayer{Next}}, method::String, uri::URI, headers, body; kw...) where Next diff --git a/src/CanonicalizeRequest.jl b/src/CanonicalizeRequest.jl index 572e2ce33..b49adf73d 100644 --- a/src/CanonicalizeRequest.jl +++ b/src/CanonicalizeRequest.jl @@ -4,11 +4,15 @@ import ..Layer, ..request using ..Messages using ..Strings.tocameldash! -abstract type CanonicalizeLayer{Next <: Layer} <: Layer end -export CanonicalizeLayer +""" + request(CanonicalizeLayer, method, ::URI, headers, body) -> HTTP.Response -canonicalizeheaders(h::T) where T = T([tocameldash!(k) => v for (k,v) in h]) +Rewrite request and response headers in Canonical-Camel-Dash-Format. +""" + +abstract type CanonicalizeLayer{Next <: Layer} <: Layer end +export CanonicalizeLayer function request(::Type{CanonicalizeLayer{Next}}, method::String, uri, headers, body; kw...) where Next @@ -23,5 +27,7 @@ function request(::Type{CanonicalizeLayer{Next}}, end +canonicalizeheaders(h::T) where T = T([tocameldash!(k) => v for (k,v) in h]) + end # module CanonicalizeRequest diff --git a/src/Connect.jl b/src/Connect.jl index af4a8a0fd..1585f8a78 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -31,8 +31,8 @@ end function getconnection(::Type{SSLContext}, host::AbstractString, port::AbstractString; - require_ssl_verification=false, - sslconfig=SSLConfig(require_ssl_verification), + require_ssl_verification::Bool=false, + sslconfig::SSLConfig=SSLConfig(require_ssl_verification), kw...)::SSLContext port = isempty(port) ? "443" : port diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 5f02e4231..770bad3a7 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -375,9 +375,9 @@ or create a new `Connection` if required. function getconnection(::Type{Transaction{T}}, host::AbstractString, port::AbstractString; - duplicate_limit=default_duplicate_limit, - pipeline_limit::Int = default_pipeline_limit, - reuse_limit::Int = nolimit, + duplicate_limit::Int=default_duplicate_limit, + pipeline_limit::Int=default_pipeline_limit, + reuse_limit::Int=nolimit, kw...)::Transaction{T} where T <: IO while true diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 4f8dab8d6..63299028a 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -8,19 +8,17 @@ using MbedTLS.SSLContext import ..@debug, ..DEBUG_LEVEL -abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end -export ConnectionPoolLayer - - -sockettype(uri::URI, default) = uri.scheme in ("wss", "https") ? SSLContext : - default +""" + request(ConnectionPoolLayer, ::URI, ::Request, body) -> HTTP.Response +Retrieve an `IO` connection from the [`ConnectionPool`](@ref). +Close the connection if the request throws an exception. +Otherwise leave it open so that it can be reused. """ - request(ConnectionLayer{Connection, Next}, ::URI, ::Request, ::Response) -Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. -""" +abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end +export ConnectionPoolLayer function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; connectionpool::Bool=true, socket_type::Type=TCPSocket, @@ -46,4 +44,8 @@ function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; end +sockettype(uri::URI, default) = uri.scheme in ("wss", "https") ? SSLContext : + default + + end # module ConnectionRequest diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index 4e79a8907..b157fe52f 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -6,11 +6,38 @@ using ..Cookies using ..Pairs: getkv, setkv import ..@debug, ..DEBUG_LEVEL + +const default_cookiejar = Dict{String, Set{Cookie}}() + + +""" + 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 end export CookieLayer +function request(::Type{CookieLayer{Next}}, + method::String, uri::URI, headers, body; + cookiejar::Dict{String, Set{Cookie}}=default_cookiejar, + kw...) where Next -const default_cookiejar = Dict{String, Set{Cookie}}() + hostcookies = get!(cookiejar, uri.host, Set{Cookie}()) + + cookies = getcookies(hostcookies, uri) + if !isempty(cookies) + setkv(headers, "Cookie", string(getkv(headers, "Cookie", ""), cookies)) + end + + res = request(Next, method, uri, headers, body; kw...) + + setcookies(hostcookies, uri.host, res.headers) + + return res +end function getcookies(cookies, uri) @@ -45,23 +72,4 @@ function setcookies(cookies, host, headers) end -function request(::Type{CookieLayer{Next}}, - method::String, uri::URI, headers, body; - cookiejar=default_cookiejar, kw...) where Next - - hostcookies = get!(cookiejar, uri.host, Set{Cookie}()) - - cookies = getcookies(hostcookies, uri) - if !isempty(cookies) - setkv(headers, "Cookie", string(getkv(headers, "Cookie", ""), cookies)) - end - - res = request(Next, method, uri, headers, body; kw...) - - setcookies(hostcookies, uri.host, res.headers) - - return res -end - - end # module CookieRequest diff --git a/src/ExceptionRequest.jl b/src/ExceptionRequest.jl index 04be78d9b..c43fcfa5e 100644 --- a/src/ExceptionRequest.jl +++ b/src/ExceptionRequest.jl @@ -1,18 +1,19 @@ module ExceptionRequest +export StatusError + import ..Layer, ..request using ..Messages -abstract type ExceptionLayer{Next <: Layer} <: Layer end -export ExceptionLayer -export StatusError +""" + request(ExceptionLayer, ::URI, ::Request, body) -> HTTP.Response -struct StatusError <: Exception - status::Int16 - response::Messages.Response -end +Throw a `StatusError` if the request returns an error response status. +""" +abstract type ExceptionLayer{Next <: Layer} <: Layer end +export ExceptionLayer function request(::Type{ExceptionLayer{Next}}, a...; kw...) where Next @@ -26,4 +27,10 @@ function request(::Type{ExceptionLayer{Next}}, a...; kw...) where Next end +struct StatusError <: Exception + status::Int16 + response::Messages.Response +end + + end # module ExceptionRequest diff --git a/src/HTTP.jl b/src/HTTP.jl index 566567853..bfed36fcf 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -31,21 +31,185 @@ include("HTTPStreams.jl"); using .HTTPStreams include("WebSockets.jl"); using .WebSockets -request(method, uri, headers=[], body=UInt8[]; kw...)::Response = - request(string(method), URI(uri), mkheaders(headers), body; kw...) +""" + + HTTP.request(method, url [, headers [, body]]; ]) -> HTTP.Response + +Send a HTTP Request Message and recieve a HTTP Response Message. + +`headers` can be any collection where +`[string(k) => string(v) for (k,v) in headers]` yields `Vector{Pair}`. +e.g. a `Dict()`, a `Vector{Tuple}`, a `Vector{Pair}` or an iterator. + +`body` can take a number of forms: + + - a `String`, a `Vector{UInt8}` or a readable `IO` stream + or any `T` accepted by `write(::IO, ::T)` + - a collection of `String` or `AbstractVector{UInt8}` or `IO` streams + or items of any type `T` accepted by `write(::IO, ::T...)` + - a readable `IO` stream or any `IO`-like type `T` for which + `eof(T)` and `readavailable(T)` are defined. + +The `HTTP.Response` struct contains: + + - `status::Int16` e.g. `200` + - `headers::Vector{Pair{String,String}}` + e.g. ["Server" => "Apache", "Content-Type" => "text/html"] + - `body::Vector{UInt8}`, the Response Body bytes. + Empty if a `response_stream` was specified in the `request`. + +`HTTP.get`, `HTTP.put`, `HTTP.post` and `HTTP.head` are defined as shorthand +for `HTTP.request("GET", ...)`, etc. + +`HTTP.request` and `HTTP.open` also accept the following optional keyword +parameters: + + +Streaming options (See [`HTTP.StreamLayer`](@ref)]) + + - `response_stream = nothing`, a writeable `IO` stream or any `IO`-like + type `T` for which `write(T, AbstractVector{UInt8})` is defined. + - `verbose = 0`, set to `1` or `2` for extra message logging. + + +Connection Pool options (See `ConnectionPool.jl`) + + - `connectionpool = true`, enable the `ConnectionPool`. + - `duplicate_limit = 7`, number of duplicate connections to each host:port. + - `pipeline_limit = 16`, number of simultaneous requests per connection. + - `reuse_limit = nolimit`, each connection is closed after this many requests. + - `socket_type = TCPSocket` + + +Timeout options (See [`HTTP.TimeoutLayer`](@ref)]) + + - `timeout = 60`, close the connection if no data is recieved for this many + seconds. Use `timeout = 0` to disable. + + +Retry options (See [`HTTP.RetryLayer`](@ref)]) + + - `retry = true`, retry idempotent requests in case of error. + - `retries = 4`, number of times to retry. + - `retry_non_idempotent = false`, retry non-idempotent requests too. e.g. POST. + + +Redirect options (See [`HTTP.RedirectLayer`](@ref)]) + + - `redirect = true`, follow 3xx redirect responses. + - `redirect_limit = 3`, number of times to redirect. + - `forwardheaders = false`, forward original headers on redirect. + + +Status Exception options (See [`HTTP.ExceptionLayer`](@ref)]) + + - `statusexception = true`, throw `HTTP.StatusError` for response status >= 300. + + +SSLContext options (See `Connect.jl`) + + - `require_ssl_verification = false`, pass `MBEDTLS_SSL_VERIFY_REQUIRED` to + the mbed TLS library. + ["... peer must present a valid certificate, handshake is aborted if + verification failed."](https://tls.mbed.org/api/ssl_8h.html#a5695285c9dbfefec295012b566290f37) + - sslconfig = SSLConfig(require_ssl_verification)` + + +Basic Authenticaiton options (See [`HTTP.BasicAuthLayer`](@ref)]) + + - basicauthorization=false, add `Authorization: Basic` header using credentials + from url userinfo. + + +AWS Authenticaiton options (See [`HTTP.AWS4AuthLayer`](@ref)]) + - `awsauthorization = false`, enable AWS4 Authentication. + - `aws_service = split(uri.host, ".")[1]` + - `aws_region = split(uri.host, ".")[2]` + - `aws_access_key_id = ENV["AWS_ACCESS_KEY_ID"]` + - `aws_secret_access_key = ENV["AWS_SECRET_ACCESS_KEY"]` + - `aws_session_token = get(ENV, "AWS_SESSION_TOKEN", "")` + - `body_sha256 = digest(MD_SHA256, body)`, + - `body_md5 = digest(MD_MD5, body)`, + + +Cookie options (See [`HTTP.CookieLayer`](@ref)]) + + - `cookies = false`, enable cookies. + - `cookiejar::Dict{String, Set{Cookie}}=default_cookiejar` + + +Cananoincalization options (See [`HTTP.CanonicalizeLayer`](@ref)]) + + - `canonicalizeheaders = false`, rewrite request and response headers in + Canonical-Camel-Dash-Format. +""" request(method::String, uri::URI, headers::Headers, body; kw...)::Response = request(HTTP.stack(;kw...), method, uri, headers, body; kw...) +request(method, uri, headers=[], body=UInt8[]; kw...)::Response = + request(string(method), URI(uri), mkheaders(headers), body; kw...) + + +""" + HTTP.open(method, url, [,headers]) do + write(io, bytes) + end -> HTTP.Response + +The `HTTP.open` API allows the Request Body to be written to an `IO` stream. +`HTTP.open` also allows the Response Body to be streamed: + + + HTTP.open(method, url, [,headers]) do io + [startread(io) -> HTTP.Response] + while !eof(io) + readavailable(io) -> AbstractVector{UInt8} + end + end -> HTTP.Response +""" + open(f::Function, method::String, uri, headers=[]; kw...)::Response = request(method, uri, headers, nothing; iofunction=f, kw...) + +""" + HTTP.get(url [, headers]; ) -> HTTP.Response + + +Shorthand for `HTTP.request("GET", ...)`. See [`HTTP.request`](@ref). +""" + + get(a...; kw...) = request("GET", a..., kw...) + +""" + HTTP.put(url, headers, body; ) -> HTTP.Response + +Shorthand for `HTTP.request("PUT", ...)`. See [`HTTP.request`](@ref). +""" + + put(a...; kw...) = request("PUT", a..., kw...) + +""" + HTTP.post(url, headers, body; ) -> HTTP.Response + +Shorthand for `HTTP.request("POST", ...)`. See [`HTTP.request`](@ref). +""" + + post(a...; kw...) = request("POST", a..., kw...) + +""" + HTTP.head(url; ) -> HTTP.Response + +Shorthand for `HTTP.request("HEAD", ...)`. See [`HTTP.request`](@ref). +""" + head(a...; kw...) = request("HEAD", a..., kw...) + abstract type Layer end if !minimal include("RedirectRequest.jl"); using .RedirectRequest diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 925350368..543a64df8 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -1,37 +1,22 @@ module MessageRequest +export body_is_a_stream, body_was_streamed + import ..Layer, ..request using ..URIs using ..Messages using ..Parsers.Headers using ..Form -struct MessageLayer{Next <: Layer} <: Layer end -export MessageLayer, body_is_a_stream, body_was_streamed - -const ByteVector = Union{AbstractVector{UInt8}, AbstractString} +""" + request(MessageLayer, method, ::URI, headers, body) -> HTTP.Response -const unknownlength = -1 -bodylength(body) = unknownlength -bodylength(body::AbstractVector{UInt8}) = length(body) -bodylength(body::AbstractString) = sizeof(body) -bodylength(body::Form) = length(body) -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) -bodylength(body::Vector{IOBuffer}) = sum(nb_available, body) - - -const body_is_a_stream = UInt8[] -const body_was_streamed = Vector{UInt8}("[Message Body was streamed]") -bodybytes(body) = body_is_a_stream -bodybytes(body::Vector{UInt8}) = body -bodybytes(body::IOBuffer) = read(body) -bodybytes(body::ByteVector) = Vector{UInt8}(body) -bodybytes(body::Vector) = length(body) == 1 ? bodybytes(body[1]) : - body_is_a_stream +Construct a [`HTTP.Request`](@ref) and set mandatory headers. +""" +struct MessageLayer{Next <: Layer} <: Layer end +export MessageLayer function request(::Type{MessageLayer{Next}}, method::String, uri::URI, headers::Headers, body; @@ -60,4 +45,26 @@ function request(::Type{MessageLayer{Next}}, end +const unknownlength = -1 +bodylength(body) = unknownlength +bodylength(body::AbstractVector{UInt8}) = length(body) +bodylength(body::AbstractString) = sizeof(body) +bodylength(body::Form) = length(body) +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) +bodylength(body::Vector{IOBuffer}) = sum(nb_available, body) + + +const body_is_a_stream = UInt8[] +const body_was_streamed = Vector{UInt8}("[Message Body was streamed]") +bodybytes(body) = body_is_a_stream +bodybytes(body::Vector{UInt8}) = body +bodybytes(body::IOBuffer) = read(body) +bodybytes(body::AbstractVector{UInt8}) = Vector{UInt8}(body) +bodybytes(body::AbstractString) = Vector{UInt8}(body) +bodybytes(body::Vector) = length(body) == 1 ? bodybytes(body[1]) : + body_is_a_stream + + end # module MessageRequest diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index 067df327d..af4eb122c 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -8,19 +8,25 @@ using ..Parsers.Header using ..Strings.tocameldash! import ..@debug, ..DEBUG_LEVEL + +""" + request(RedirectLayer, method, ::URI, headers, body) -> HTTP.Response + +Redirect request in the case of 3xx response status. +""" + abstract type RedirectLayer{Next <: Layer} <: Layer end export RedirectLayer - function request(::Type{RedirectLayer{Next}}, method::String, uri::URI, headers, body; - maxredirects=3, forwardheaders=false, kw...) where Next + redirect_limit=3, forwardheaders=false, kw...) where Next count = 0 while true res = request(Next, method, uri, headers, body; kw...) - if (count == maxredirects + if (count == redirect_limit || !isredirect(res) || (location = header(res, "Location")) == "" || method == "HEAD") #FIXME why not redirect HEAD? diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 3dcb19c0e..b40f263d9 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -7,30 +7,21 @@ using ..MessageRequest using ..Messages import ..@debug, ..DEBUG_LEVEL -abstract type RetryLayer{Next <: Layer} <: Layer end -export RetryLayer +""" + request(RetryLayer, ::URI, ::Request, body) -> HTTP.Response -isrecoverable(e::Exception) = isioerror(e) -isrecoverable(e::Base.DNSError) = true -isrecoverable(e::HTTP.StatusError) = e.status == 403 || # Forbidden - e.status == 408 || # Timeout - e.status >= 500 # Server Error - +Retry the request if it throws a recoverable exception. +""" -isrecoverable(e, req, retry_non_idempotent) = - isrecoverable(e) && - !(req.body === body_was_streamed) && - !(req.response.body === body_was_streamed) && - (retry_non_idempotent || isidempotent(req)) - # "MUST NOT automatically retry a request with a non-idempotent method" - # https://tools.ietf.org/html/rfc7230#section-6.3.1 +abstract type RetryLayer{Next <: Layer} <: Layer end +export RetryLayer function request(::Type{RetryLayer{Next}}, uri, req, body; retries::Int=4, retry_non_idempotent::Bool=false, kw...) where Next - retry_request = retry(request, + retry_request = Base.retry(request, delays=ExponentialBackOff(n = retries), check=(s,ex)->begin retry = isrecoverable(ex, req, retry_non_idempotent) @@ -47,6 +38,21 @@ function request(::Type{RetryLayer{Next}}, uri, req, body; end +isrecoverable(e::Exception) = isioerror(e) +isrecoverable(e::Base.DNSError) = true +isrecoverable(e::HTTP.StatusError) = e.status == 403 || # Forbidden + e.status == 408 || # Timeout + e.status >= 500 # Server Error + +isrecoverable(e, req, retry_non_idempotent) = + isrecoverable(e) && + !(req.body === body_was_streamed) && + !(req.response.body === body_was_streamed) && + (retry_non_idempotent || isidempotent(req)) + # "MUST NOT automatically retry a request with a non-idempotent method" + # https://tools.ietf.org/html/rfc7230#section-6.3.1 + + function no_retry_reason(ex, req) buf = IOBuffer() showcompact(buf, req) diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index c20e92162..addc2b4a0 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -9,20 +9,19 @@ import ..ConnectionPool using ..MessageRequest import ..@debug, ..DEBUG_LEVEL, ..printlncompact -abstract type StreamLayer <: Layer end -export StreamLayer - """ - request(StreamLayer, ::IO, ::Request, body) -> ::Response + request(StreamLayer, ::IO, ::Request, body) -> HTTP.Response -Send a `Request` and return a `Response`. -Send the `Request` body in a background task and begin reading the response +Send a `Request` body in a background task and begin reading the response immediately so that the transmission can be aborted if the `Response` status -indicates that the server does wish to receive the message body -[https://tools.ietf.org/html/rfc7230#section-6.5](RFC7230 6.5). +indicates that the server does not wish to receive the message body +[RFC7230 6.5](https://tools.ietf.org/html/rfc7230#section-6.5). """ +abstract type StreamLayer <: Layer end +export StreamLayer + function request(::Type{StreamLayer}, io::IO, req::Request, body; response_stream=nothing, iofunction=nothing, diff --git a/src/TimeoutRequest.jl b/src/TimeoutRequest.jl index f49442715..2c29bf779 100644 --- a/src/TimeoutRequest.jl +++ b/src/TimeoutRequest.jl @@ -5,21 +5,19 @@ using ..ConnectionPool import ..@debug, ..DEBUG_LEVEL -abstract type TimeoutLayer{Next <: Layer} <: Layer end -export TimeoutLayer - - """ - request(TimeoutLayer{Connection, Next}, ::IO, ::Request, body) + request(TimeoutLayer, ::IO, ::Request, body) -> HTTP.Response -Get a `Connection` for a `URI`, send a `Request` and fill in a `Response`. +Close `IO` if no data has been received for `timeout` seconds. """ +abstract type TimeoutLayer{Next <: Layer} <: Layer end +export TimeoutLayer + function request(::Type{TimeoutLayer{Next}}, io::IO, req, body; timeout::Int=60, kw...) where Next wait_for_timeout = Ref{Bool}(true) - request_task = current_task() @async while wait_for_timeout[] if isreadable(io) && inactiveseconds(io) > timeout diff --git a/src/client.jl b/src/client.jl index 32871450a..c6d24ec6d 100644 --- a/src/client.jl +++ b/src/client.jl @@ -145,6 +145,7 @@ for f in [:get, :post, :put, :delete, :head, f_str = uppercase(string(f)) meth = convert(Method, f_str) @eval begin +#= @doc """ $($f)(uri; kwargs...) -> Response $($f)(client::HTTP.Client, uri; kwargs...) -> Response @@ -266,7 +267,8 @@ close(f) # setting eof on f causes the async request to send a final chunk and r resp = wait(t) # get our response by getting the result of our asynchronous task ``` - """ function $(f) end + """ =# function $(f) end + ($f)(uri::AbstractString; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, $meth, URIs.URL(uri; query=query, isconnect=$(f_str == "CONNECT")); verbose=verbose, args...) ($f)(uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, $meth, uri; verbose=verbose, args...) ($f)(client::Client, uri::AbstractString; query="", args...) = request(client, $meth, URIs.URL(uri; query=query, isconnect=$(f_str == "CONNECT")); args...) diff --git a/test/loopback.jl b/test/loopback.jl index ca57cbad8..77532bab1 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -156,7 +156,7 @@ lbreq(req, headers, body; method="GET", kw...) = HTTP.request(method, "http://test/$req", headers, body; config..., kw...) lbopen(f, req, headers) = - HTTP.open(f, "GET", "http://test/$req", headers; config...) + HTTP.open(f, "PUT", "http://test/$req", headers; config...) @testset "loopback" begin From 704264d6f615207629a8f54ae916b1678f0d3d0b Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 4 Jan 2018 16:27:40 +1100 Subject: [PATCH 118/182] arch doc --- docs/src/index.md | 72 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/docs/src/index.md b/docs/src/index.md index 016873714..89debb030 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -84,6 +84,78 @@ HTTP.escapeHTML # HTTP.jl Architecture +``` + ┌────────────────────────────────────────────────────────────────────────────┐ + │ ┌───────────────┐ │ + │ HTTP.request(method, uri, headers, body) -> │ HTTP.Response ├──────────────┼┐ + │ │ └───────────────┘ ││ + │ │ ││ + │ │ ┌──────────────────────────────────────┐ ┌──────────────────┐ ││ + │ └───▶│ request(RedirectLayer, ...) │ │ HTTP.StatusError │ ││ + │ └─┬────────────────────────────────────┴─┐ └─────────▲────────┘ ││ + │ │ request(BasicAuthLayer, ...) │ │ ││ + │ └─┬────────────────────────────────────┴─┐ │ ││ + │ │ request(CookieLayer, ...) │ │ ││ + │ └─┬────────────────────────────────────┴─┐ │ ││ + │ │ request(CanonicalizeLayer, ...) │ │ ││ + │ └─┬────────────────────────────────────┴─┐ │ ││ + │ │ request(MessageLayer, ...) ├─────────┼──────┐ ││ + │ └─┬────────────────────────────────────┴─┐ │ │ ││ + │ │ request(AWS4AuthLayer, ...) │ │ │ ││ + │ └─┬────────────────────────────────────┴─┐ │ │ ││ + │ │ request(RetryLayer, ...) │ │ │ ││ + │ └─┬────────────────────────────────────┴─┐ │ │ ││ + │ │ request(ExceptionLayer, ...) ├───┘ │ ││ + │ └─┬────────────────────────────────────┴─┐ │ ││ +┌┼────────────────────────┤ request(ConnectionPoolLayer, ...) │ │ ││ +││ └─┬────────────────────────────────────┴─┐ │ ││ +││ │ request(TimeoutLayer, ...) │ │ ││ +││ └─┬────────────────────────────────────┴─┐ │ ││ +││ │ request(StreamLayer, ...) │ │ ││ +││ └──────────────────────┬───────────────┘ │ ││ +│└───────────────────────────────────────────────────┼────────────────────┼───┘│ +│┌─────────────────────────────┐ ┌───────────────────▼────────────────────┼───┐│ +││ │ │ HTTP.Stream <:IO │ ││ +││ HTTP.Parser │ │ ┌─────────────────────▼─┐ ││ +││ │ │ startwrite(io) ◀─┤ HTTP.Request │ ││ +││ parseheaders(bytes) do Pair │ │ write(io, body) │ │ ││ +││ parsebody(bytes) -> bytes │ │ ... │ method::String │ ││ +││ │ │ closewrite(io) │ uri::String │ ││ +││ reset!() │ │ │ headers::Vector{Pair} │ ││ +││ │ │ │ body::Vector{UInt8} │ ││ +││ messagestarted() -> Bool │ │ └────────▲─────┬────────┘ ││ +││ headerscomplete() " │ │ │ │ ││ +││ waitingforeof() " │ │ ┌────────┴─────▼────────┐ ││ +││ bodycomplete() " ◀─┤ startread(io)────▶ HTTP.Response ◀─┼┘ +││ messagecomplete() " │ │ read(io) -> body │ │ │ +││ connectionclosed() " │ │ ... │ status::Int │ │ +││ │ │ closeread() │ headers::Vector{Pair} │ │ +││ │ │ │ body::Vector{UInt8} │ │ +││ │ │ └───────────────────────┘ │ +││ ParsingError <:Exception │ │ EOFError <:Exception │ +│└─────────────────────────────┘ └─┬──────────────────────────────────────────┘ +│┌─────────────────────────────────┼──────────────────────────────────────────┐ +└▶ HTTP.ConnectionPool │ │ + │ ┌───────────▼───────────┐ ┌───────────────────────┐ │ + │ getconnection() -> │ HTTP.Transaction <:IO │ │ HTTP.Transaction <:IO │ │ + │ │ └───────────────────────┘ └───────────────────────┘ │ + │ │ ╲│╱ ╲│╱ │ + │ │ │ │ │ + │ │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ + │ │ pool: [│ HTTP.Connection │,│ HTTP.Connection │...]│ + │ │ └───────────┬───────────┘ └───────────┬───────────┘ │ + └───────┼─────────────────────────┼─────────────────────────┼────────────────┘ + ┌───────▼─────────────────────────┼─────────────────────────┼────────────────┐ + │ HTTP.Connect │ │ │ + │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ + │ getconnection() -> │ Base.TCPSocket <:IO │ │MbedTLS.SSLContext <:IO│ │ + │ └───────────────────────┘ └───────────┬───────────┘ │ + │ │ │ + │ EOFError <:Exception ┌───────────▼───────────┐ │ + │ UVError <:Exception │ Base.TCPSocket <:IO │ │ + │ DNSError <:Exception └───────────────────────┘ │ + └────────────────────────────────────────────────────────────────────────────┘ +``` ## User Interface From 3c94232c2a17657bdc12991c3e85910ed645ae73 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 6 Jan 2018 16:55:21 +1100 Subject: [PATCH 119/182] Doc cleanup --- docs/src/index.md | 687 ++++------------------------- docs/src/layers.monopic | Bin 0 -> 8602 bytes src/Connect.jl | 10 + src/ConnectionPool.jl | 48 +- src/ConnectionRequest.jl | 2 +- src/HTTP.jl | 339 +++++++++++++- src/MessageRequest.jl | 2 +- src/Messages.jl | 93 +++- src/RedirectRequest.jl | 2 +- src/RetryRequest.jl | 8 + src/StreamRequest.jl | 13 +- src/{HTTPStreams.jl => Streams.jl} | 48 +- src/parser.jl | 31 +- 13 files changed, 626 insertions(+), 657 deletions(-) create mode 100644 docs/src/layers.monopic rename src/{HTTPStreams.jl => Streams.jl} (73%) diff --git a/docs/src/index.md b/docs/src/index.md index 89debb030..f83c481e3 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -1,6 +1,6 @@ # HTTP.jl Documentation -`HTTP.jl` provides a pure Julia library for HTTP functionality. +`HTTP.jl` a Julia library for HTTP Messages. ```@contents ``` @@ -16,33 +16,22 @@ HTTP.post HTTP.head ``` -## Layers - -```@docs -HTTP.RedirectLayer -HTTP.BasicAuthLayer -HTTP.CookieLayer -HTTP.CanonicalizeLayer -HTTP.MessageLayer -HTTP.AWS4AuthLayer -HTTP.RetryLayer -HTTP.ExceptionLayer -HTTP.ConnectionPoolLayer -HTTP.TimeoutLayer -HTTP.StreamLayer -``` - ## Requests Note that the HTTP methods of POST, DELETE, PUT, etc. all follow the same format as `HTTP.get`, documented below. -```@docs + +``` +@docs HTTP.get HTTP.Client HTTP.Connection ``` + ### HTTP request errors -```@docs + +``` +@docs HTTP.ConnectError HTTP.SendError HTTP.ClosedError @@ -51,7 +40,9 @@ HTTP.RedirectError HTTP.StatusError ``` + ## Server / Handlers + ```@docs HTTP.serve HTTP.Server @@ -61,638 +52,129 @@ HTTP.Router HTTP.register! ``` + ## HTTP Types + ```@docs HTTP.URI -HTTP.Request -HTTP.RequestOptions -HTTP.Response HTTP.Cookie -HTTP.FIFOBuffer ``` + ## HTTP Utilities + ```@docs -HTTP.parse -HTTP.escape -HTTP.unescape -HTTP.splitpath -HTTP.isvalid +HTTP.URIs.escapeuri +HTTP.URIs.unescapeuri +HTTP.URIs.splitpath +Base.isvalid(::HTTP.URIs.URI) HTTP.sniff -HTTP.escapeHTML +HTTP.Strings.escapehtml ``` -# HTTP.jl Architecture +# HTTP.jl Internal Architecture +```@docs +HTTP.Layer +HTTP.stack ``` - ┌────────────────────────────────────────────────────────────────────────────┐ - │ ┌───────────────┐ │ - │ HTTP.request(method, uri, headers, body) -> │ HTTP.Response ├──────────────┼┐ - │ │ └───────────────┘ ││ - │ │ ││ - │ │ ┌──────────────────────────────────────┐ ┌──────────────────┐ ││ - │ └───▶│ request(RedirectLayer, ...) │ │ HTTP.StatusError │ ││ - │ └─┬────────────────────────────────────┴─┐ └─────────▲────────┘ ││ - │ │ request(BasicAuthLayer, ...) │ │ ││ - │ └─┬────────────────────────────────────┴─┐ │ ││ - │ │ request(CookieLayer, ...) │ │ ││ - │ └─┬────────────────────────────────────┴─┐ │ ││ - │ │ request(CanonicalizeLayer, ...) │ │ ││ - │ └─┬────────────────────────────────────┴─┐ │ ││ - │ │ request(MessageLayer, ...) ├─────────┼──────┐ ││ - │ └─┬────────────────────────────────────┴─┐ │ │ ││ - │ │ request(AWS4AuthLayer, ...) │ │ │ ││ - │ └─┬────────────────────────────────────┴─┐ │ │ ││ - │ │ request(RetryLayer, ...) │ │ │ ││ - │ └─┬────────────────────────────────────┴─┐ │ │ ││ - │ │ request(ExceptionLayer, ...) ├───┘ │ ││ - │ └─┬────────────────────────────────────┴─┐ │ ││ -┌┼────────────────────────┤ request(ConnectionPoolLayer, ...) │ │ ││ -││ └─┬────────────────────────────────────┴─┐ │ ││ -││ │ request(TimeoutLayer, ...) │ │ ││ -││ └─┬────────────────────────────────────┴─┐ │ ││ -││ │ request(StreamLayer, ...) │ │ ││ -││ └──────────────────────┬───────────────┘ │ ││ -│└───────────────────────────────────────────────────┼────────────────────┼───┘│ -│┌─────────────────────────────┐ ┌───────────────────▼────────────────────┼───┐│ -││ │ │ HTTP.Stream <:IO │ ││ -││ HTTP.Parser │ │ ┌─────────────────────▼─┐ ││ -││ │ │ startwrite(io) ◀─┤ HTTP.Request │ ││ -││ parseheaders(bytes) do Pair │ │ write(io, body) │ │ ││ -││ parsebody(bytes) -> bytes │ │ ... │ method::String │ ││ -││ │ │ closewrite(io) │ uri::String │ ││ -││ reset!() │ │ │ headers::Vector{Pair} │ ││ -││ │ │ │ body::Vector{UInt8} │ ││ -││ messagestarted() -> Bool │ │ └────────▲─────┬────────┘ ││ -││ headerscomplete() " │ │ │ │ ││ -││ waitingforeof() " │ │ ┌────────┴─────▼────────┐ ││ -││ bodycomplete() " ◀─┤ startread(io)────▶ HTTP.Response ◀─┼┘ -││ messagecomplete() " │ │ read(io) -> body │ │ │ -││ connectionclosed() " │ │ ... │ status::Int │ │ -││ │ │ closeread() │ headers::Vector{Pair} │ │ -││ │ │ │ body::Vector{UInt8} │ │ -││ │ │ └───────────────────────┘ │ -││ ParsingError <:Exception │ │ EOFError <:Exception │ -│└─────────────────────────────┘ └─┬──────────────────────────────────────────┘ -│┌─────────────────────────────────┼──────────────────────────────────────────┐ -└▶ HTTP.ConnectionPool │ │ - │ ┌───────────▼───────────┐ ┌───────────────────────┐ │ - │ getconnection() -> │ HTTP.Transaction <:IO │ │ HTTP.Transaction <:IO │ │ - │ │ └───────────────────────┘ └───────────────────────┘ │ - │ │ ╲│╱ ╲│╱ │ - │ │ │ │ │ - │ │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ - │ │ pool: [│ HTTP.Connection │,│ HTTP.Connection │...]│ - │ │ └───────────┬───────────┘ └───────────┬───────────┘ │ - └───────┼─────────────────────────┼─────────────────────────┼────────────────┘ - ┌───────▼─────────────────────────┼─────────────────────────┼────────────────┐ - │ HTTP.Connect │ │ │ - │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ - │ getconnection() -> │ Base.TCPSocket <:IO │ │MbedTLS.SSLContext <:IO│ │ - │ └───────────────────────┘ └───────────┬───────────┘ │ - │ │ │ - │ EOFError <:Exception ┌───────────▼───────────┐ │ - │ UVError <:Exception │ Base.TCPSocket <:IO │ │ - │ DNSError <:Exception └───────────────────────┘ │ - └────────────────────────────────────────────────────────────────────────────┘ -``` - -## User Interface - -The basic API function is: - - `HTTP.request(method, url [, headers [, body]] [, response_stream=]) -> HTTP.Response` - -`headers` can be any collection where -`[string(k) => string(v) for (k,v) in headers]` yields `Vector{Pair}`. - - -`body` can take a number of forms: - - - a `String`, a `Vector{UInt8}` or a readable `IO` - or any `T` accepted by `write(::IO, ::T)` - - a collection of `String` or `AbstractVector{UInt8}` or `IO` - or any `T` accepted by `write(::IO, ::T...)` - - an readable `IO` stream or any `IO`-like type `T` for which - `eof(T)` and `readavailable(T)` are defined. - -`response_stream` can be a writeable `IO` stream or any `IO`-like type `T` -for which `write(T, AbstractVector{UInt8})` is defined. - - -The `HTTP.Response` struct contains: - - - `status::Int16` e.g. `200` - - `headers::Vector{Pair{String,String}}` - e.g. ["Server" => "Apache", "Content-Type" => "text/html"] - - `body::Vector{UInt8}`, the Response Body bytes. - Empty if a `response_stream` was specified in the `request`. - - -The `HTTP.open` API allows the Request Body to be streamed to an `IO` channel: - -``` - HTTP.open(method, url, [,headers]) do io - write(io, bytes) - end -> HTTP.Response -``` - - -The `HTTP.open` API also allows the Response Body to be streamed: - -``` - HTTP.open(method, url, [,headers]) do io - write(io, bytes) - readresponse(io) - read(io) -> AbstractVector{UInt8} - end -> HTTP.Response -``` - - -## User Interface Examples - -## Request Body Examples - -``` -r = request("POST", "http://httpbin.org/post", [], "post body data") -@show r.status -``` - -``` -io = open("post_data.txt", "r") -r = request("POST", "http://httpbin.org/post", [], io) -@show r.status -``` - -``` -chunks = ("chunk$i" for i in 1:1000) -r = request("POST", "http://httpbin.org/post", [], chunks) -@show r.status -``` - -``` -chunks = [preamble_chunk, data_chunk, checksum(data_chunk)] -r = request("POST", "http://httpbin.org/post", [], chunks) -@show r.status -``` - -``` -r = HTTP.open("POST", "http://httpbin.org/post") do io - write(io, preamble_chunk) - write(io, data_chunk) - write(io, checksum(data_chunk)) -end -@show r.status -``` - - -## Response Body Examples - -``` -r = request("GET", "http://httpbin.org/get") -@show r.status -println(String(r.body)) -``` - -``` -io = open("get_data.txt", "r") -r = request("GET", "http://httpbin.org/get", response_stream=io) -@show r.status -println(read("get_data.txt")) -``` - -``` -io = BufferStream() -@async while !eof(io) - bytes = readavailable(io)) - println("GET data: $bytes") -end -r = request("GET", "http://httpbin.org/get", response_stream=io) -@show r.status -``` - -``` -r = HTTP.open("GET", "http://httpbin.org/get") do io - r = readresponse(io) - @show r.status - while !eof(io) - bytes = readavailable(io)) - println("GET data: $bytes") - end -end -``` - - -## Request and Response Body Examples -``` -r = request("POST", "http://httpbin.org/post", [], "post body data") -@show r.status -println(String(r.body)) -``` +## Request Execution Layers -``` -in = open("foo.png", "r") -out = open("foo.jpg", "w") -r = request("POST", "http://convert.com/png2jpg", [], in, out) -@show r.status -``` - -``` -HTTP.open("POST", "http://music.com/play") do io - write(io, JSON.json([ - "auth" => "12345XXXX", - "song_id" => 7, - ])) - r = readresponse(io) - @show r.status - while !eof(io) - bytes = readavailable(io)) - play_audio(bytes) - end -end +```@docs +HTTP.RedirectLayer +HTTP.BasicAuthLayer +HTTP.CookieLayer +HTTP.CanonicalizeLayer +HTTP.MessageLayer +HTTP.AWS4AuthLayer +HTTP.RetryLayer +HTTP.ExceptionLayer +HTTP.ConnectionPoolLayer +HTTP.TimeoutLayer +HTTP.StreamLayer ``` - ## Parser -Source: `Parsers.jl` - -The [`HTTP.Parser`](@ref) separates HTTP Message data (from a `String`, -an `IO` stream or raw bytes) into its component parts. The parts are passed to -three callback functions as they are parsed: -- `onheader(::Pair{String,String})` -- `onheaderscomplete(::`[`HTTP.Parsers.Message`](@ref)`)` -- `onbodyfragment(::SubArray{UInt8,1})` - -If the input data is invalid the Parser throws a [`HTTP.ParsingError`](@ref). +*Source: `Parsers.jl`* -A Parser processes a single HTTP Message. If the input stream contains -multiple Messages the Parser stops at the end of the first Message. -The `parse!(::Parser, data)` function returns the number of bytes consumed. -If less than `length(data)` was consumed, the excess must be processed -separately. The `read!(io, ::Parser)` method deals with this by pushing -the excess bytes back into the stream using `IOExtras.unread!`. - -The Parser does not interpret the Message Headers except as is necessary -to parse the Message Body. It is beyond the scope of the Parser to deal -with repeated header fields, multi-line values, cookies or case normalization -(see [`HTTP.Messages.appendheader`](@ref)). - -The Parser has no knowledge of the high-level `Request` and `Response` structs -defined in `Messages.jl`. The Parser has it's own low level -[`HTTP.Parsers.Message`](@ref) struct that represents both Request and Response -Messages. +```@docs +HTTP.Parsers +``` ## Messages +*Source: `Messages.jl`* -Source: `Messages.jl` - -The `Messages` module defines structs that represent [`HTTP.Messages.Request`](@ref) -and [`HTTP.Messages.Response`](@ref) Messages. - -The Messages module defines `IO` `read` and `write` methods for Messages -but it does not deal with URIs, creating connections, or executing requests. - -The Messages module does not explicitly throw exceptions, but it calls -methods that may result in low level `IO` exceptions. - -### Sending Messages - -Messages are formatted and written to an `IO` stream by -[`Base.write(::IO,::HTTP.Messages.Message)`](@ref). - -[`Base.write(::IO, ::HTTP.Messages.Bodies.Body)`](@ref) is called to output the -Message Body. This function implements `chunked` encoding if the body length -is unknown. - - -### Receiving Messages - -Messages are parsed from `IO` stream data by -[`Base.read!(::IO,::HTTP.Messages.Message)`](@ref). - -This function creates a [`HTTP.Parser`](@ref) with callbacks as follows -- `onheader` = [`HTTP.Messages.appendheader`](@ref) -- `onheaderscomplete` = [`HTTP.Messages.readstartline!`](@ref) -- `onbodyfragment` = [`Base.write(::HTTP.Messages.Bodies.Body, bytes)`](@ref) - -[`Base.read!(::IO, ::HTTP.Parser)`](@ref) is called to feed the Parser -with data from the `IO` stream. As the Parser processes the data the -callbacks are called to fill in the `Message` struct. - -The `Response` struct has a `parent` field that points to the corresponding -`Request`. The `Request` struct has a `parent` field that points to a `Response` -in the case of HTTP Redirect. - - -### Headers - -Headers are represented by `Vector{Pair{String,String}}`. As compared to -`Dict{String,String}` this allows repeated header fields and preservation of -order. - -Header values can be accessed by name using -[`HTTP.Messages.header`](@ref) and -[`HTTP.Messages.setheader`](@ref) (case-insensitive). - -The [`HTTP.Messages.appendheader`](@ref) function handles combining -multi-line values, repeated header fields and special handling of -multiple `Set-Cookie` headers. - -### Bodies - -The [`HTTP.Messages.Bodies.Body`](@ref) struct represents a Message Body. -It either stores static body data in an `IOBuffer`, or wraps an `IO` stream -that will consume or produce the Message Body. - -The [`HTTP.Messages.setlengthheader`](@ref) function sets the `Content-Length` -header if the Message Body has known length, or sets the -`Transfer-Encoding: chunked` header to indicate that the Body length is not -known at the time the headers are sent. +```@docs +HTTP.Messages +``` ## Connections ### Basic Connections -Source: `Connect.jl` - -[`HTTP.Connect.getconnection`](@ref) creates a new `TCPSocket` or `SSLContext` -for a specified `host` and `port`. - -No connection streaming, pooling or reuse is implemented in this module. -However, the `getconnection` interface is the same as the one used by the -connection pool so the `Connect` module can be used directly when reuse is -not required. - - -### Pooled Connections - -Source: `ConnectionPool.jl` - -This module wrapps the Basic Connect module above and adds support for: -- Reusing connections for multiple Request/Response Messages, -- Interleaving Request/Response Messages. i.e. allowing a new Request to be - sent before while the previous Response is being read. - -This module defines a [`HTTP.ConnectionPool.Connection`](@ref)` <: IO` -struct to manage Message streaming and connection reuse. Methods -are provided for `eof`, `readavailable`, `unsafe_write` and `close`. -This allows the `Connection` object to act as a proxy for the -`TCPSocket` or `SSLContext` that it wraps. - - -The [`HTTP.ConnectionPool.pool`](@ref) is a collection of open -`Connection`s. The `request` function calls `getconnection` to -retrieve a connection from the `pool`. When the `request` function -has written a Request Message it calls `closewrite` to signal that -the `Connection` can be reused for writing (to send the next Request). -When the `request` function has read the Response Message it calls -`closeread` to signal that the `Connection` can be reused for -reading. - -e.g. -```julia -request(uri::URI, req::Request, res::Response) - T = uri.scheme == "https" ? SSLContext : TCPSocket - io = getconnection(Connection{T}, uri.host, uri.port) - write(io, req) - closewrite(io) - read!(io, res) - closeread(io) - return res -end -``` - -## Request Execution Stack - -The Request Execution Stack is separated into composable layers. +*Source: `Connect.jl`* -Each layer is defined by a nested type `Layer{Next}` where the `Next` -parameter defines the next layer in the stack. -The `request` method for each layer takes a `Layer{Next}` type as -its first argument and dispatches the request to the next layer -using `request(Next, ...)`. - -The example below defines three layers and three stacks each with -a different combination of layers. - - -```julia -abstract type Layer end -abstract type Layer1{Next <: Layer} <: Layer end -abstract type Layer2{Next <: Layer} <: Layer end -abstract type Layer3 <: Layer end - -request(::Type{Layer1{Next}}, data) where Next = "L1", request(Next, data) -request(::Type{Layer2{Next}}, data) where Next = "L2", request(Next, data) -request(::Type{Layer3}, data) = "L3", data - -const stack1 = Layer1{Layer2{Layer3}} -const stack2 = Layer2{Layer1{Layer3}} -const stack3 = Layer1{Layer3} +```@docs +HTTP.Connect ``` -```julia -julia> request(stack1, "foo") -("L1", ("L2", ("L3", "foo"))) - -julia> request(stack2, "bar") -("L2", ("L1", ("L3", "bar"))) -julia> request(stack3, "boo") -("L1", ("L3", "boo")) -``` - -This stack definition pattern gives the user flexibility in how layers are -combined but still allows Julia to do whole-stack comiple time optimistations. +### Pooled Connections -e.g. the `request(stack1, "foo")` call above is optimised down to a single -function: -```julia -julia> code_typed(request, (Type{stack1}, String))[1].first -CodeInfo(:(begin - return (Core.tuple)("L1", (Core.tuple)("L2", (Core.tuple)("L3", data))) -end)) -``` +*Source: `ConnectionPool.jl`* -In `HTTP.jl` the `const DefaultStack` type defines the default HTTP Request -processing stack. This is used as the default first parameter of the `request` -function. - -```julia -const DefaultStack = - RedirectLayer{ - CanonicalizeLayer{ - BasicAuthLayer{ - CookieLayer{ - RetryLayer{ - ExceptionLayer{ - MessageLayer{ - #ConnectLayer{ - ConnectionPoolLayer{ - SocketLayer - }}}}}}}} - -request(method::String, uri, headers=[], body=""; kw...) = - request(HTTP.DefaultStack, method, uri, headers, body; kw...) +```@docs +HTTP.ConnectionPool ``` -## Redirect Layer - -Source: `RedirectRequest.jl` - -This layer adds a loop to process `3xx` redirects. - - -## Canonicalize Layer - -Source: `CanonicalizeRequest.jl` - -This layer rewrites header field names to canonical Camel-Dash form. - - -## Basic Authentication Layer - -Source: `BasicAuthRequest.jl` - -This layer adds an `Authorization: Basic` header using `URI.userinfo`. - - -## Cookie Layer - -Source: `CookieRequest.jl` - -This layer stores cookies sent by the server and sends them back to the -server with subsequent requests. - - -## Retry Layer - -Source: `RetryRequest.jl` - -The `RetryRequest` module implements a `request` method with a retry loop that -repeats the request in the event of a recoverable network error. -A randomised exponentially increasing delay is introduced between attempts to -avoid exacerbating network congestion. - -Methods of `isrecoverable(e)` define which exception types lead to a retry. -e.g. `Base.UVError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` -(if status is `1xx` or `5xx`). - - -## ExceptionLayer - -Source: `ExceptionRequest.jl` - -This layer throws a `StatusError` if the Response Status indicates an error. - - -## Message Layer - -Source: `MessageRequest.jl` - -This layer: -- Creates a [`HTTP.Messages.Request`](@ref) object for the specified - method, URI, headers and body, -- Sets the mandatory `Host` and `Content-Length` (or `Transfer-Encoding`) - headers. -- Creates a [`HTTP.Messages.Response`](@ref) object to hold the response. - - -## Connect Layer - -Source: `ConnectionRequest.jl` - -Alternative to Connection Pool Layer below. - -This layer calls [`HTTP.Connect.getconnection`](@ref) -to get a non-pooled socket. - - -## Connection Pool Layer - -Source: `ConnectionRequest.jl` - -This layer calls [`HTTP.Connect.getconnection`](@ref) -to get a socket from connection pool. - - -## Socket Layer - -Source: `SocketRequest.jl` - -This layer calls [`HTTP.Messages.writeandread`](@ref) to send the Request -to the socket and receive the Response. - -If the `Body` of the `Request` is connected to an `IO` stream, the `request` -function waits for the Response Headers to be received, but schedules reading of -the Response Body to happen in a background task. - - # Internal Interfaces ## Parser Interface ```@docs -HTTP.Parser -HTTP.Parsers.Message -HTTP.Parsers.parse! -Base.read!(::IO, ::HTTP.Parsers.Parser) -HTTP.Parsers.messagecomplete +HTTP.Parsers.Parser +HTTP.Parsers.parseheaders +HTTP.Parsers.parsebody +HTTP.Parsers.reset! +HTTP.Parsers.messagestarted HTTP.Parsers.headerscomplete +HTTP.Parsers.bodycomplete +HTTP.Parsers.messagecomplete +HTTP.Parsers.messagehastrailing HTTP.Parsers.waitingforeof +HTTP.Parsers.seteof +HTTP.Parsers.connectionclosed +HTTP.Parsers.setnobody ``` ## Messages Interface -### Message - -`const Message = Union{Request,Response}` - ```@docs +HTTP.Messages.Request +HTTP.Messages.Response +HTTP.Messages.iserror +HTTP.Messages.isredirect +HTTP.Messages.ischunked +HTTP.Messages.issafe +HTTP.Messages.isidempotent HTTP.Messages.header +HTTP.Messages.hasheader HTTP.Messages.setheader HTTP.Messages.defaultheader -HTTP.Messages.setlengthheader HTTP.Messages.appendheader -HTTP.Messages.waitforheaders -Base.wait(::HTTP.Messages.Response) -Base.write(::IO,::Union{HTTP.Messages.Request, HTTP.Messages.Response}) -HTTP.Messages.writeandread +HTTP.Messages.readheaders HTTP.Messages.readstartline! -``` - -### Request - -```@docs -HTTP.Messages.Request -``` - -### Response - -```@docs -HTTP.Messages.Response -HTTP.Messages.iserror -HTTP.Messages.isredirect -HTTP.Messages.method -``` - -### Body - -``` -HTTP.Messages.Bodies.Body -HTTP.Messages.Bodies.isstream -Base.write(::HTTP.Messages.Bodies.Body, ::Any) -Base.write(::IO, ::HTTP.Messages.Bodies.Body) -Base.length(::HTTP.Messages.Bodies.Body) -Base.take!(::HTTP.Messages.Bodies.Body) -HTTP.Messages.Bodies.set_show_max -HTTP.Messages.Bodies.collect! +HTTP.Messages.headerscomplete(::HTTP.Messages.Response) +HTTP.Messages.readtrailers +HTTP.Messages.writestartline +HTTP.Messages.writeheaders +Base.write(::IO,::HTTP.Messages.Message) ``` @@ -709,15 +191,10 @@ HTTP.Connect.getconnection(::Type{TCPSocket},::AbstractString,::AbstractString) ```@docs HTTP.ConnectionPool.Connection HTTP.ConnectionPool.pool -HTTP.Connect.getconnection(::Type{HTTP.ConnectionPool.Connection{T}},::AbstractString,::AbstractString) where T <: IO -HTTP.IOExtras.unread!(::HTTP.ConnectionPool.Connection,::SubArray{UInt8, 1}) -HTTP.IOExtras.closewrite(::HTTP.ConnectionPool.Connection) -HTTP.IOExtras.closeread(::HTTP.ConnectionPool.Connection) -``` - - -## Low Level Request Interface - -```@docs -HTTP.RequestStack.request +HTTP.Connect.getconnection(::Type{HTTP.ConnectionPool.Transaction{T}},::AbstractString,::AbstractString) where T <: IO +HTTP.IOExtras.unread!(::HTTP.ConnectionPool.Transaction,::SubArray{UInt8,1,Array{UInt8,1},Tuple{UnitRange{Int64}},true}) +HTTP.IOExtras.startwrite(::HTTP.ConnectionPool.Transaction) +HTTP.IOExtras.closewrite(::HTTP.ConnectionPool.Transaction) +HTTP.IOExtras.startread(::HTTP.ConnectionPool.Transaction) +HTTP.IOExtras.closeread(::HTTP.ConnectionPool.Transaction) ``` diff --git a/docs/src/layers.monopic b/docs/src/layers.monopic new file mode 100644 index 0000000000000000000000000000000000000000..403356d594560e55e66882d169c70f9ee3e0bc66 GIT binary patch literal 8602 zcmbW6RZtvGo39~+;1E2x6Fj&(f#B{zLSWFrU4pv@4esuP4lco62N>K3cbD_u@2jmn zwYzmLPS?{{KA*&!VyQc(^|y> zap?XS$jCxrUqU$^JB~~1j&B3Ft1MTM%PDVRpdZg32c0Cccvk4<&;4wcJ(Q1}L!q6q zuG39zHP&)HoT~|fLH(532(NnjYj$A_e7j5Y*O%kKOFIW_v7jCY2Uo5P6@nl%F4D2c zvo3JXggpV@Y+DcFPG8T~?90yhxYh+0DQ3A?ho`%cuQ77yWFNx-VEAj8`sK;><)GX} zzxy`y!HmD96Wp4<0u$SZY*)Z0?djdOo0}W$S7XE!*A%Pm2pT7MDpF^mFrb48hgm#H zBL1VH@)<#5TYrY+-&HNo{-3vw>a)MM`uoL3R6Y>u<*gp?y(s1NccUv!FZAF40$O@P zxBA2;iYd;Fr*aoSTT6HtPfu8V7%JAUj<VqN8h}CYmtnV%POScZUW>Mm~DV@#e`1j>CG}t5)3^KiMI3 zZw2^qWsnQ{d2{UA@CmP9oQyxUVfFXL8Q-(NSmnQM*hH~8RK0bA)7w3TvJ92X=T(~5 z=o@BalcdqlGXSRSYc~e zF;}WLXysGb=-;r&$AO)j(=0nuX&OcsS6x}{{)Asj?sAOywN9M`+nD_@b=ST`{yB=n zE(x?AkrXOa{9XoEQTVXYhAWxbuZuj;3;g^PcB2&LtmOzeCW%pR=b>IE1rWJ7RIL-_8 zA62VFWnS^){F}7AR`?|6?gGA!?C6@5Z8uLb4V028ea97TM@VT4-!J@<{F58I1iqmP zUZrwPkJg^iHi@Oy43H^?B=2?WU~apogVZmFJ`<$7WxMCPI+2X$qiwU!`>pxloaLOs z9QyjrsLL*_#QvLW>-(*(t(uxnMl3%j-^^j4sLzF)*9}bJ7ZaO``Q+bCS8r!Nz3&Sx z0XE*0FWD^KA5%rbM~xWIKZeGCH~gyBkoiS4IYYMG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NbK1bq@WK)6a`(#l?l|p^v`(J`gd(7Fi^}_LPw}HV{@J0 zlDA86M3WE7-mB2LG39AQWXf-7ZWZJ3CYP4ISE4g#qS7#qY5?2$^W?@tB$|(qTN8&M zOvL3;tj5BLkEf@+yvoE^;5ZI|swrj)xOI;i)Dg$atuX6iT(20@DQ z#p+oMyOE+MG3r+FOir2&vJQThs>12b(sWVc15KscplyfLg$szZcU^h! z>f%W*ix#MWX#z@$pN-WAgR2CZ@>i1fCrfkQm*zmCmdu__5K0V%)P+nJpD`p#upsk zC~R^>ZdTXRRa94gjqQNE_b452`rX~GZ{*1|#PaT67nS&S(BU8xjtt~NZjBfMR=pIx zs}`YDOgQ<}|5LiZyCdIg)7DZ4;GL7ll4?gvUYwLbt34L&81MfzRYzdOCy3^;sFz0-gSxf{NX(wYS4D~e5sw*as~Q)8SL8tbqrP+>D@Y3yN|wsF2AI} z^dc=7vx__~C_s3V5YNtuE7t&5luCski~&XOQ*dokDy5z1nWsR?K3*>TZJ~)G4bdh`_4;NR4TQ|S5By4)4!5dn z#HEb0P0Rz*VvWtAgjcq0)H|<~JZ`a-ofESmuIviQ2%ESmk;bNRYoc|mYEE#fOg(H` zf(T|Yh`3&Zu2h9H={QBy@uM@2`k^dZ>1w@Hp)j-5_#HF;FwJOz6aio5B;D7_pSJvk zpP7JAnUb(^ZdWy2vaHAH)d0l^twwzzk=q~gN&VH(;N-Lj8=7O}$Kp@`n-+%1@sLGm z+5MPXT+5r0kH|K0)IbYW?DCP3C^ApK@{-%pT-Mx(dEt#4p4`iRaa?fHr79Q^Z09Tnd`Tzg` literal 0 HcmV?d00001 diff --git a/src/Connect.jl b/src/Connect.jl index 1585f8a78..4d1732080 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -1,3 +1,13 @@ +""" +[`HTTP.Connect.getconnection`](@ref) creates a new `TCPSocket` or `SSLContext` +for a specified `host` and `port`. + +No connection streaming, pooling or reuse is implemented in this module. +However, the `getconnection` interface is the same as the one used by the +connection pool so the `Connect` module can be used directly when reuse is +not required. +""" + module Connect export getconnection, getparser, inactiveseconds, getrawstream diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 770bad3a7..576f8738c 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -1,3 +1,28 @@ +""" +This module wrapps the basic Connect module above and adds support for: +- Reusing connections for multiple Request/Response Messages, +- Pipelining Request/Response Messages. i.e. allowing a new Request to be + sent before previous Responses have been read. + +This module defines a [`HTTP.ConnectionPool.Connection`](@ref) +struct to manage pipelining and connection reuse and a +[`HTTP.ConnectionPool.Transaction`](@ref)`<: IO` struct to manage a single +pipelined request. Methods are provided for `eof`, `readavailable`, +`unsafe_write` and `close`. +This allows the `Transaction` object to act as a proxy for the +`TCPSocket` or `SSLContext` that it wraps. + +The [`HTTP.ConnectionPool.pool`](@ref) is a collection of open +`Connection`s. The `request` function calls `getconnection` to +retrieve a connection from the `pool`. When the `request` function +has written a Request Message it calls `closewrite` to signal that +the `Connection` can be reused for writing (to send the next Request). +When the `request` function has read the Response Message it calls +`closeread` to signal that the `Connection` can be reused for +reading. +``` + +""" module ConnectionPool export getconnection, getparser, getrawstream, inactiveseconds @@ -32,15 +57,20 @@ end A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. - `host::String` -- `port::String` +- `port::String`, exactly as specified in the URI (i.e. may be empty). +- `pipeline_linit`, number of requests to send before waiting for responses. +- `peerport`, remote TCP port number (used for debug messages). +- `localport`, local TCP port number (used for debug messages). - `io::T`, the `TCPSocket` or `SSLContext. - `excess::ByteView`, left over bytes read from the connection after the end of a response message. These bytes are probably the start of the next response message. +- `writebusy`, is a `Transaction` busy writing to this `Connection` ? - `writecount`, number of Request Messages that have been written. - `readcount`, number of Response Messages that have been read. - `writelock`, busy writing a Request to `io`. - `readlock`, busy reading a Response from `io`. +- `timestamp, time data was last recieved. - `parser::Parser`, reuse a `Parser` when this `Connection` is reused. """ @@ -148,6 +178,15 @@ function IOExtras.unread!(t::Transaction, bytes::ByteView) end +""" + startwrite(::Transaction) + +Set `writebusy`. +Should only be called by the `Transaction` constructor because +`getconnection` only creates new `Transaction`s when a `Connection` is +available for writing. +""" + function IOExtras.startwrite(t::Transaction) @require !t.c.writebusy t.c.writebusy = true @@ -159,8 +198,6 @@ end closewrite(::Transaction) Signal that an entire Request Message has been written to the `Transaction`. - -Increment `writecount` and wait for pending reads to complete. """ function IOExtras.closewrite(t::Transaction) @@ -195,15 +232,14 @@ function IOExtras.startread(t::Transaction) return end -ensurereadable(t::Transaction) = if !isreadable(t) startread(t) end - """ closeread(::Transaction) Signal that an entire Response Message has been read from the `Transaction`. -Increment `readcount` and wake up tasks waiting in `closewrite`. +Increment `readcount` and wake up tasks waiting in `startread` by unlocking +`readlock`. """ function IOExtras.closeread(t::Transaction) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 63299028a..3511bc81b 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -11,7 +11,7 @@ import ..@debug, ..DEBUG_LEVEL """ request(ConnectionPoolLayer, ::URI, ::Request, body) -> HTTP.Response -Retrieve an `IO` connection from the [`ConnectionPool`](@ref). +Retrieve an `IO` connection from the [`HTTP.ConnectionPool`](@ref). Close the connection if the request throws an exception. Otherwise leave it open so that it can be reused. diff --git a/src/HTTP.jl b/src/HTTP.jl index bfed36fcf..824fa8332 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -27,7 +27,7 @@ include("Connect.jl") include("ConnectionPool.jl") include("Messages.jl"); using .Messages import .Messages: header, hasheader -include("HTTPStreams.jl"); using .HTTPStreams +include("Streams.jl"); using .Streams include("WebSockets.jl"); using .WebSockets @@ -37,6 +37,12 @@ include("WebSockets.jl"); using .WebSockets Send a HTTP Request Message and recieve a HTTP Response Message. +``` +r = HTTP.request("GET", "http://httpbin.org/ip") +println(r.status) +println(String(r.body)) +``` + `headers` can be any collection where `[string(k) => string(v) for (k,v) in headers]` yields `Vector{Pair}`. e.g. a `Dict()`, a `Vector{Tuple}`, a `Vector{Pair}` or an iterator. @@ -142,6 +148,121 @@ Cananoincalization options (See [`HTTP.CanonicalizeLayer`](@ref)]) - `canonicalizeheaders = false`, rewrite request and response headers in Canonical-Camel-Dash-Format. + + +## Request Body Examples + +String body: +``` +r = request("POST", "http://httpbin.org/post", [], "post body data") +@show r.status +``` + +Stream body from file: +``` +io = open("post_data.txt", "r") +r = request("POST", "http://httpbin.org/post", [], io) +@show r.status +``` + +Generator body: +``` +chunks = ("chunk\$i" for i in 1:1000) +r = request("POST", "http://httpbin.org/post", [], chunks) +@show r.status +``` + +Collection body: +``` +chunks = [preamble_chunk, data_chunk, checksum(data_chunk)] +r = request("POST", "http://httpbin.org/post", [], chunks) +@show r.status +``` + +`open() do io` body: +``` +r = HTTP.open("POST", "http://httpbin.org/post") do io + write(io, preamble_chunk) + write(io, data_chunk) + write(io, checksum(data_chunk)) +end +@show r.status +``` + + +## Response Body Examples + +String body: +``` +r = request("GET", "http://httpbin.org/get") +@show r.status +println(String(r.body)) +``` + +Stream body to file: +``` +io = open("get_data.txt", "w") +r = request("GET", "http://httpbin.org/get", response_stream=io) +@show r.status +println(read("get_data.txt")) +``` + +Stream body through buffer: +``` +io = BufferStream() +@async while !eof(io) + bytes = readavailable(io)) + println("GET data: \$bytes") +end +r = request("GET", "http://httpbin.org/get", response_stream=io) +@show r.status +``` + +Stream body through `open() do io`: +``` +r = HTTP.open("GET", "http://httpbin.org/get") do io + r = startread(io) + @show r.status + while !eof(io) + bytes = readavailable(io)) + println("GET data: \$bytes") + end +end +``` + + +## Request and Response Body Examples + +String bodies: +``` +r = request("POST", "http://httpbin.org/post", [], "post body data") +@show r.status +println(String(r.body)) +``` + +Stream bodies from and to files: +``` +in = open("foo.png", "r") +out = open("foo.jpg", "w") +r = request("POST", "http://convert.com/png2jpg", [], in, response_stream=out) +@show r.status +``` + +Stream bodies through: `open() do io`: +``` +HTTP.open("POST", "http://music.com/play") do io + write(io, JSON.json([ + "auth" => "12345XXXX", + "song_id" => 7, + ])) + r = readresponse(io) + @show r.status + while !eof(io) + bytes = readavailable(io)) + play_audio(bytes) + end +end +``` """ request(method::String, uri::URI, headers::Headers, body; kw...)::Response = @@ -210,6 +331,61 @@ head(a...; kw...) = request("HEAD", a..., kw...) +""" + +## Request Execution Stack + +The Request Execution Stack is separated into composable layers. + +Each layer is defined by a nested type `Layer{Next}` where the `Next` +parameter defines the next layer in the stack. +The `request` method for each layer takes a `Layer{Next}` type as +its first argument and dispatches the request to the next layer +using `request(Next, ...)`. + +The example below defines three layers and three stacks each with +a different combination of layers. + + +```julia +abstract type Layer end +abstract type Layer1{Next <: Layer} <: Layer end +abstract type Layer2{Next <: Layer} <: Layer end +abstract type Layer3 <: Layer end + +request(::Type{Layer1{Next}}, data) where Next = "L1", request(Next, data) +request(::Type{Layer2{Next}}, data) where Next = "L2", request(Next, data) +request(::Type{Layer3}, data) = "L3", data + +const stack1 = Layer1{Layer2{Layer3}} +const stack2 = Layer2{Layer1{Layer3}} +const stack3 = Layer1{Layer3} +``` + +```julia +julia> request(stack1, "foo") +("L1", ("L2", ("L3", "foo"))) + +julia> request(stack2, "bar") +("L2", ("L1", ("L3", "bar"))) + +julia> request(stack3, "boo") +("L1", ("L3", "boo")) +``` + +This stack definition pattern gives the user flexibility in how layers are +combined but still allows Julia to do whole-stack comiple time optimistations. + +e.g. the `request(stack1, "foo")` call above is optimised down to a single +function: +```julia +julia> code_typed(request, (Type{stack1}, String))[1].first +CodeInfo(:(begin + return (Core.tuple)("L1", (Core.tuple)("L2", (Core.tuple)("L3", data))) +end)) +``` +""" + abstract type Layer end if !minimal include("RedirectRequest.jl"); using .RedirectRequest @@ -225,7 +401,154 @@ include("ExceptionRequest.jl"); using .ExceptionRequest include("RetryRequest.jl"); using .RetryRequest include("ConnectionRequest.jl"); using .ConnectionRequest include("StreamRequest.jl"); using .StreamRequest - if !minimal + +""" +The `stack()` function returns the default HTTP Layer-stack type. +This type is passed as the first parameter to the [`HTTP.request`](@ref) function. + +`stack()` accepts optional keyword arguments to enable/disable specific layers +in the stack: +`request(method, args...; kw...) request(stack(;kw...), args...; kw...)` + + +The minimal request execution stack is: + +``` +stack = MessageLayer{ConnectionPoolLayer{StreamLayer}} +``` + +The figure below illustrates a minimal Layer-stack with the +`connectionpool=false` option that causes the `ConnectionPoolLayer` to call +HTTP.Connect.getconnection() directly rather reusing pooled connections. + +``` + ┌────────────────────────────────────────────────────────────────────────────┐ + │ ┌───────────────────┐ │ + │ request(method, uri, headers, body) -> │ HTTP.Response │ │ + │ ────────────────────────── └─────────▲─────────┘ │ + │ ║ ║ │ + │ ┌────────────────────────────────────────────────────────────┐ │ + │ │ request(MessageLayer, method, ::URI, ::Headers, body) │ │ + │ ├────────────────────────────────────────────────────────────┤ │ +┌┼───┤ request(ConnectionPoolLayer, ::URI, ::Request, body) │ │ +││ ├────────────────────────────────────────────────────────────┤ │ +││ │ request(StreamLayer, ::IO, ::Request, body) │ │ +││ └──────────────┬───────────────────┬─────────────────────────┘ │ +│└──────────────────┼────────║──────────┼───────────────║─────────────────────┘ +│ │ ║ │ ║ +│┌──────────────────▼───────────────┐ │ ┌──────────────────────────────────┐ +││ HTTP.Request │ │ │ HTTP.Response │ +│└──────────────────▲───────────────┘ │ └───────────────▲──────────────────┘ +│┌──────────────────┴────────║──────────▼───────────────║──┴──────────────────┐ +││ HTTP.Stream <:IO ║ ╔══════╗ ║ │ +│└───────────────────────────║───────────║──────║───────║──┬──────────────────┘ +│┌──────────────────────────────────┐ ║ ┌────▼───────║──▼──────────────────┐ +││ HTTP.Messages │ ║ │ HTTP.Parser │ +│└──────────────────────────────────┘ ║ └──────────────────────────────────┘ +│┌───────────────────────────║───────────║────────────────────────────────────┐ +└▶ HTTP.Connect ║ ║ │ + └───────────────────────────║───────────║────────────────────────────────────┘ + ║ ║ + ┌───────────────────────────║───────────║──────────────┐ ┏━━━━━━━━━━━━━━━━━━┓ + │ HTTP Server ▼ ║ │ ┃ data flow: ════▶ ┃ + │ Request Response │ ┃ reference: ────▶ ┃ + └──────────────────────────────────────────────────────┘ ┗━━━━━━━━━━━━━━━━━━┛ +``` + +The next figure illustrates the full Layer-stack and its relationship with +the [`HTTP.Response`](@ref), the [`HTTP.Parser`](@ref), +the [`HTTP.Stream`](@ref) and the [`HTTP.ConnectionPool`](@ref). + +``` + ┌────────────────────────────────────────────────────────────────────────────┐ + │ ┌──────────────────┐ │ + │ HTTP.jl Request Stack │ HTTP.StatusError │ │ + │ └───────────┬──────┘ │ + │ ┌───────────────────┐ │ + │ request(method, uri, headers, body) -> │ HTTP.Response │ │ │ + │ ────────────────────────── └─────────▲─────────┘ │ + │ ║ ║ │ │ + │ ┌────────────────────────────────────────────────────────────┐ │ + │ │ request(RedirectLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ + │ │ request(BasicAuthLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ + │ │ request(CookieLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ + │ │ request(CanonicalizeLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ + │ │ request(MessageLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ + │ │ request(AWS4AuthLayer, ::URI, ::Request, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ + │ │ request(RetryLayer, ::URI, ::Request, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ + │ │ request(ExceptionLayer, ::URI, ::Request, body) │─ ┘ │ + │ ├────────────────────────────────────────────────────────────┤ │ +┌┼───┤ request(ConnectionPoolLayer, ::URI, ::Request, body) │ │ +││ ├────────────────────────────────────────────────────────────┤ │ +││ │ request(TimeoutLayer, ::IO, ::Request, body) │ │ +││ ├────────────────────────────────────────────────────────────┤ │ +││ │ request(StreamLayer, ::IO, ::Request, body) │ │ +││ └──────────────┬───────────────────┬─────────────────────────┘ │ +│└──────────────────┼────────║──────────┼───────────────║─────────────────────┘ +│ │ ║ │ ║ +│┌──────────────────▼───────────────┐ │ ┌──────────────────────────────────┐ +││ HTTP.Request │ │ │ HTTP.Response │ +││ │ │ │ │ +││ method::String ◀───┼──▶ status::Int │ +││ uri::String │ │ │ headers::Vector{Pair} │ +││ headers::Vector{Pair} │ │ │ body::Vector{UInt8} │ +││ body::Vector{UInt8} │ │ │ │ +│└──────────────────▲───────────────┘ │ └───────────────▲──────────────────┘ +│┌──────────────────┴────────║──────────▼───────────────║──┴──────────────────┐ +││ HTTP.Stream <:IO ║ ╔══════╗ ║ │ +││ ┌───────────────────────────┐ ║ ┌──▼─────────────────────────┐ │ +││ │ startwrite(::Stream) │ ║ │ startread(::Stream) │ │ +││ │ write(::Stream, body) │ ║ │ read(::Stream) -> body │ │ +││ │ ... │ ║ │ ... │ │ +││ │ closewrite(::Stream) │ ║ │ closeread(::Stream) │ │ +││ └───────────────────────────┘ ║ └────────────────────────────┘ │ +││ EOFError <:Exception ║ ║ ║ ║ │ +│└───────────────────────────║────────┬──║──────║───────║──┬──────────────────┘ +│┌──────────────────────────────────┐ │ ║ ┌────▼───────║──▼──────────────────┐ +││ HTTP.Messages │ │ ║ │ HTTP.Parser │ +││ │ │ ║ │ │ +││ writestartline(::IO, ::Request) │ │ ║ │ parseheaders(bytes) do h::Pair │ +││ writeheaders(::IO, ::Request) │ │ ║ │ parsebody(bytes) -> bytes │ +││ │ │ ║ │ │ +││ │ │ ║ │ ParsingError <:Exception │ +│└──────────────────────────────────┘ │ ║ └──────────────────────────────────┘ +│ ║ │ ║ +│┌───────────────────────────║────────┼──║────────────────────────────────────┐ +└▶ HTTP.ConnectionPool ║ │ ║ │ + │ ┌──────────────▼────────┐ ┌───────────────────────┐ │ + │ getconnection() -> │ HTTP.Transaction <:IO │ │ HTTP.Transaction <:IO │ │ + │ │ └───────────────────────┘ └───────────────────────┘ │ + │ │ ║ ╲│╱ ║ ╲│╱ │ + │ │ ║ │ ║ │ │ + │ │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ + │ │ pool: [│ HTTP.Connection │,│ HTTP.Connection │...]│ + │ │ └───────────┬───────────┘ └───────────┬───────────┘ │ + └───────┼───────────────────║─────┼─────║───────────────────┼────────────────┘ + ┌───────▼───────────────────║─────┼─────║───────────────────┼────────────────┐ + │ HTTP.Connect ║ │ ║ │ │ + │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ + │ getconnection() -> │ Base.TCPSocket <:IO │ │MbedTLS.SSLContext <:IO│ │ + │ └───────────────────────┘ └───────────┬───────────┘ │ + │ ║ ║ │ │ + │ EOFError <:Exception ║ ║ ┌───────────▼───────────┐ │ + │ UVError <:Exception ║ ║ │ Base.TCPSocket <:IO │ │ + │ DNSError <:Exception ║ ║ └───────────────────────┘ │ + └───────────────────────────║───────────║────────────────────────────────────┘ + ║ ║ + ┌───────────────────────────║───────────║──────────────┐ ┏━━━━━━━━━━━━━━━━━━┓ + │ HTTP Server ▼ ║ │ ┃ data flow: ════▶ ┃ + │ Request Response │ ┃ reference: ────▶ ┃ + └──────────────────────────────────────────────────────┘ ┗━━━━━━━━━━━━━━━━━━┛ +``` +*See `docs/src/layers`[`.monopic`](http://monodraw.helftone.com).* +""" function stack(;redirect=true, basicauthorization=false, @@ -236,7 +559,9 @@ function stack(;redirect=true, statusexception=true, timeout=0, kw...) - + if !minimal + MessageLayer{ExceptionLayer{ConnectionPoolLayer{StreamLayer}}} + else NoLayer = Union (redirect ? RedirectLayer : NoLayer){ @@ -251,14 +576,9 @@ function stack(;redirect=true, (timeout > 0 ? TimeoutLayer : NoLayer){ StreamLayer }}}}}}}}}} + end end - else -stack(;kw...) = MessageLayer{ - ExceptionLayer{ - ConnectionPoolLayer{ - StreamLayer}}} - end if !minimal include("client.jl") @@ -268,4 +588,5 @@ include("server.jl"); using .Nitrogen include("precompile.jl") end + end # module diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 543a64df8..5d3bf5cb9 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -12,7 +12,7 @@ using ..Form """ request(MessageLayer, method, ::URI, headers, body) -> HTTP.Response -Construct a [`HTTP.Request`](@ref) and set mandatory headers. +Construct a [`HTTP.Request`](@ref) object and set mandatory headers. """ struct MessageLayer{Next <: Layer} <: Layer end diff --git a/src/Messages.jl b/src/Messages.jl index 1e89ab11f..dde032c09 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -1,3 +1,63 @@ +""" +The `Messages` module defines structs that represent [`HTTP.Messages.Request`](@ref) +and [`HTTP.Messages.Response`](@ref) Messages. + +The `Response` struct has a `request` field that points to the corresponding +`Request`; and the `Request` struct has a `response` field. +The `Request` struct also has a `parent` field that points to a `Response` +in the case of HTTP Redirect. + + +The Messages module defines `IO` `read` and `write` methods for Messages +but it does not deal with URIs, creating connections, or executing requests. +The + +The `read` methods throw `EOFError` exceptions if input data is incomplete. +and call parser functions that may throw `HTTP.ParsingError` exceptions. +The `read` and `write` methods may also result in low level `IO` exceptions. + + +### Sending Messages + +Messages are formatted and written to an `IO` stream by +[`Base.write(::IO,::HTTP.Messages.Message)`](@ref) and or +[`HTTP.Messages.writeheaders`](@ref). + + +### Receiving Messages + +Messages are parsed from `IO` stream data by +[`HTTP.Messages.readheaders`](@ref). +This function calls [`HTTP.Messages.appendheader`](@ref) and +[`HTTP.Messages.readstartline!`](@ref). + +The `read` methods rely on [`HTTP.IOExtras.unread!`](@ref) to push excess +data back to the input stream. + + +### Headers + +Headers are represented by `Vector{Pair{String,String}}`. As compared to +`Dict{String,String}` this allows [repeated header fields and preservation of +order](https://tools.ietf.org/html/rfc7230#section-3.2.2). + +Header values can be accessed by name using +[`HTTP.Messages.header`](@ref) and +[`HTTP.Messages.setheader`](@ref) (case-insensitive). + +The [`HTTP.Messages.appendheader`](@ref) function handles combining +multi-line values, repeated header fields and special handling of +multiple `Set-Cookie` headers. + +### Bodies + +The [`HTTP.Message`](@ref) structs represent the Message Body as `Vector{UInt8}`. + +Streaming of request and response bodies is handled by the +[`HTTP.StreamLayer`](@ref) and the [`HTTP.Stream`](@ref) `<: IO` stream. +""" + + module Messages export Message, Request, Response, @@ -22,14 +82,14 @@ import ..Parsers: headerscomplete, reset! abstract type Message end """ - Response + Response <: Message Represents a HTTP Response Message. - `version::VersionNumber` - `status::Int16` - `headers::Vector{Pair{String,String}}` -- `body::`[`HTTP.Body`](@ref) +- `body::Vector{UInt8}` - `request`, the `Request` that yielded this `Response`. """ @@ -59,7 +119,7 @@ end """ - Request + Request <: Message Represents a HTTP Request Message. @@ -309,6 +369,13 @@ function readstartline!(m::Parsers.Message, r::Request) end +""" + readheaders(::IO, ::Parser, ::Message) + +Read headers (and startline) from an `IO` stream into a `Message` struct. +Throw `EOFError` if input is incomplete. +""" + function readheaders(io::IO, parser::Parser, message::Message) while !headerscomplete(parser) && !eof(io) @@ -325,9 +392,21 @@ function readheaders(io::IO, parser::Parser, message::Message) end -headerscomplete(r::Request) = r.method != "" +""" + headerscomplete(::Message) + +Have the headers been read into this `Message`? +""" + headerscomplete(r::Response) = r.status != 0 && r.status != 100 +headerscomplete(r::Request) = r.method != "" + + +""" + readtrailers(::IO, ::Parser, ::Message) +Read trailers from an `IO` stream into a `Message` struct. +""" function readtrailers(io::IO, parser::Parser, message::Message) if messagehastrailing(parser) @@ -337,6 +416,12 @@ function readtrailers(io::IO, parser::Parser, message::Message) end +""" + readbody(::IO, ::Parser) -> Vector{UInt8} + +Read message body from an `IO` stream. +""" + function readbody(io::IO, parser::Parser) body = IOBuffer() while !bodycomplete(parser) && !eof(io) diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index af4eb122c..5c8ead07a 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -12,7 +12,7 @@ import ..@debug, ..DEBUG_LEVEL """ request(RedirectLayer, method, ::URI, headers, body) -> HTTP.Response -Redirect request in the case of 3xx response status. +Redirects the request in the case of 3xx response status. """ abstract type RedirectLayer{Next <: Layer} <: Layer end diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index b40f263d9..cf4ed44d6 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -12,6 +12,14 @@ import ..@debug, ..DEBUG_LEVEL request(RetryLayer, ::URI, ::Request, body) -> HTTP.Response Retry the request if it throws a recoverable exception. + +`Base.retry` and `Base.ExponentialBackOff` implement a randomised exponentially +increasing delay is introduced between attempts to avoid exacerbating network +congestion. + +Methods of `isrecoverable(e)` define which exception types lead to a retry. +e.g. `Base.UVError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` +(if status is ``5xx`). """ abstract type RetryLayer{Next <: Layer} <: Layer end diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index addc2b4a0..73aa7dfc8 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -4,7 +4,7 @@ import ..Layer, ..request using ..IOExtras using ..Parsers using ..Messages -using ..HTTPStreams +using ..Streams import ..ConnectionPool using ..MessageRequest import ..@debug, ..DEBUG_LEVEL, ..printlncompact @@ -13,7 +13,10 @@ import ..@debug, ..DEBUG_LEVEL, ..printlncompact """ request(StreamLayer, ::IO, ::Request, body) -> HTTP.Response -Send a `Request` body in a background task and begin reading the response +Create a [`HTTP.Stream`](@ref) to send a `Request` and `body` to an `IO` +stream and read the response. + +Sens the `Request` body in a background task and begins reading the response immediately so that the transmission can be aborted if the `Response` status indicates that the server does not wish to receive the message body [RFC7230 6.5](https://tools.ietf.org/html/rfc7230#section-6.5). @@ -31,7 +34,7 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; verbose == 1 && printlncompact(req) verbose >= 2 && println(req) - http = HTTPStream(io, req, ConnectionPool.getparser(io)) + http = Stream(io, req, ConnectionPool.getparser(io)) startwrite(http) aborted = false @@ -73,7 +76,7 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; end -function writebody(http::HTTPStream, req::Request, body) +function writebody(http::Stream, req::Request, body) if req.body === body_is_a_stream writebodystream(http, req, body) @@ -109,7 +112,7 @@ writechunk(http, req, body::IO) = writebodystream(http, req, body) writechunk(http, req, body) = write(http, body) -function readbody(http::HTTPStream, res::Response, response_stream) +function readbody(http::Stream, res::Response, response_stream) if response_stream == nothing res.body = read(http) else diff --git a/src/HTTPStreams.jl b/src/Streams.jl similarity index 73% rename from src/HTTPStreams.jl rename to src/Streams.jl index e02974a7d..71b9fb2cd 100644 --- a/src/HTTPStreams.jl +++ b/src/Streams.jl @@ -1,6 +1,6 @@ -module HTTPStreams +module Streams -export HTTPStream, closebody, isaborted +export Stream, closebody, isaborted using ..IOExtras using ..Parsers @@ -11,35 +11,35 @@ import ..@require, ..precondition_error import ..@debug, ..DEBUG_LEVEL -mutable struct HTTPStream{T <: Message} <: IO +mutable struct Stream{T <: Message} <: IO stream::IO message::T parser::Parser writechunked::Bool end -function HTTPStream(io::IO, request::Request, parser::Parser) +function Stream(io::IO, request::Request, parser::Parser) @require iswritable(io) writechunked = header(request, "Transfer-Encoding") == "chunked" - HTTPStream{Response}(io, request.response, parser, writechunked) + Stream{Response}(io, request.response, parser, writechunked) end -header(http::HTTPStream, a...) = header(http.message, a...) -hasheader(http::HTTPStream, a) = header(http.message, a) -getrawstream(http::HTTPStream) = getrawstream(http.stream) +header(http::Stream, a...) = header(http.message, a...) +hasheader(http::Stream, a) = header(http.message, a) +getrawstream(http::Stream) = getrawstream(http.stream) # Writing HTTP Messages -IOExtras.iswritable(http::HTTPStream) = iswritable(http.stream) +IOExtras.iswritable(http::Stream) = iswritable(http.stream) -function IOExtras.startwrite(http::HTTPStream) +function IOExtras.startwrite(http::Stream) @require iswritable(http.stream) writeheaders(http.stream, http.message.request) end -function Base.unsafe_write(http::HTTPStream, p::Ptr{UInt8}, n::UInt) +function Base.unsafe_write(http::Stream, p::Ptr{UInt8}, n::UInt) if !http.writechunked return unsafe_write(http.stream, p, n) end @@ -49,7 +49,7 @@ function Base.unsafe_write(http::HTTPStream, p::Ptr{UInt8}, n::UInt) end -function closebody(http::HTTPStream) +function closebody(http::Stream) if http.writechunked write(http.stream, "0\r\n\r\n") http.writechunked = false @@ -57,7 +57,7 @@ function closebody(http::HTTPStream) end -function IOExtras.closewrite(http::HTTPStream) +function IOExtras.closewrite(http::Stream) if !iswritable(http) return end @@ -68,9 +68,9 @@ end # Reading HTTP Messages -IOExtras.isreadable(http::HTTPStream) = isreadable(http.stream) +IOExtras.isreadable(http::Stream) = isreadable(http.stream) -function IOExtras.startread(http::HTTPStream) +function IOExtras.startread(http::Stream) @require !isreadable(http.stream) startread(http.stream) configure_parser(http) @@ -88,7 +88,7 @@ function IOExtras.startread(http::HTTPStream) end -function configure_parser(http::HTTPStream{Response}) +function configure_parser(http::Stream{Response}) reset!(http.parser) req = http.message.request::Request if req.method in ("HEAD", "CONNECT") @@ -96,10 +96,10 @@ function configure_parser(http::HTTPStream{Response}) end end -configure_parser(http::HTTPStream{Request}) = reset!(http.parser) +configure_parser(http::Stream{Request}) = reset!(http.parser) -function Base.eof(http::HTTPStream) +function Base.eof(http::Stream) if !headerscomplete(http.message) startread(http) end @@ -114,7 +114,7 @@ function Base.eof(http::HTTPStream) end -function Base.readavailable(http::HTTPStream)::ByteView +function Base.readavailable(http::Stream)::ByteView @require headerscomplete(http.message) @require !bodycomplete(http.parser) @@ -128,17 +128,17 @@ function Base.readavailable(http::HTTPStream)::ByteView end -IOExtras.unread!(http::HTTPStream, excess) = unread!(http.stream, excess) +IOExtras.unread!(http::Stream, excess) = unread!(http.stream, excess) -function Base.read(http::HTTPStream) +function Base.read(http::Stream) buf = IOBuffer() write(buf, http) return take!(buf) end -function isaborted(http::HTTPStream{Response}) +function isaborted(http::Stream{Response}) # "If [the response] indicates the server does not wish to receive the # message body and is closing the connection, the client SHOULD @@ -157,7 +157,7 @@ function isaborted(http::HTTPStream{Response}) end -function IOExtras.closeread(http::HTTPStream{Response}) +function IOExtras.closeread(http::Stream{Response}) # Discard unread body bytes... while !eof(http) @@ -185,4 +185,4 @@ function IOExtras.closeread(http::HTTPStream{Response}) end -end #module HTTPStreams +end #module Streams diff --git a/src/parser.jl b/src/parser.jl index 305f8fec1..56e30f777 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -22,6 +22,28 @@ # IN THE SOFTWARE. # + +""" +The parser separates a raw HTTP Message into its component parts. + +If the input data is invalid the Parser throws a [`HTTP.ParsingError`](@ref). + +The parser processes a single HTTP Message. If the input stream contains +multiple Messages the Parser stops at the end of the first Message. +The `parseheaders` and `parsebody` functions return a `SubArray` containing the +unuses portion of the input. + +The Parser does not interpret the Message Headers except as needed +to parse the Message Body. It is beyond the scope of the Parser to deal +with repeated header fields, multi-line values, cookies or case normalization +(see [`HTTP.Messages.appendheader`](@ref)). + +The Parser has no knowledge of the high-level `Request` and `Response` structs +defined in `Messages.jl`. The Parser has it's own low level +[`HTTP.Parsers.Message`](@ref) struct that represents both Request and Response +Messages. +""" + module Parsers export Parser, Header, Headers, ByteView, nobytes, @@ -255,10 +277,17 @@ end """ - parseheaders(f(::Pair{String,String}), ::Parser, bytes) -> excess + parseheaders(::Parser, bytes) do h::Pair{String,String} ... -> excess Read headers from `bytes`, passing each field/value pair to `f`. Returns a `SubArray` containing bytes not parsed. + +e.g. +``` +excess = parseheaders(p, bytes) do (k,v) + println("\$k: \$v") +end +``` """ function parseheaders(f, p, bytes) From 7507bbb8893309bf9559f16bf3ea7772772cddf8 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 6 Jan 2018 23:17:51 +1100 Subject: [PATCH 120/182] fix !minima/minimal bug in stack() --- src/HTTP.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 824fa8332..0839085e9 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -559,7 +559,7 @@ function stack(;redirect=true, statusexception=true, timeout=0, kw...) - if !minimal + if minimal MessageLayer{ExceptionLayer{ConnectionPoolLayer{StreamLayer}}} else NoLayer = Union From 38cf9c6f5225ddd41d31bb2e4752f4a41d8e5913 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 7 Jan 2018 22:57:44 +1100 Subject: [PATCH 121/182] Doc cleanup. Wrap IO errors in IOError type. --- REQUIRE | 1 + docs/src/index.md | 98 +++++++++---- docs/src/layers.monopic | Bin 8602 -> 8960 bytes src/AWS4AuthRequest.jl | 42 +++++- src/Connect.jl | 4 +- src/ConnectionPool.jl | 20 ++- src/ConnectionRequest.jl | 8 +- src/ExceptionRequest.jl | 13 +- src/HTTP.jl | 292 +++++++++++++++++++++------------------ src/IOExtras.jl | 31 ++++- src/MessageRequest.jl | 4 +- src/Messages.jl | 6 +- src/RedirectRequest.jl | 3 +- src/RetryRequest.jl | 7 +- src/StreamRequest.jl | 4 +- src/Streams.jl | 39 +++++- src/compat.jl | 2 + src/parser.jl | 59 +++++--- test/async.jl | 2 - test/client.jl | 4 +- test/loopback.jl | 40 +++--- 21 files changed, 439 insertions(+), 240 deletions(-) diff --git a/REQUIRE b/REQUIRE index 82c8bee60..1aca9608d 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,3 @@ julia 0.6 MbedTLS 0.5.2 +IniFile diff --git a/docs/src/index.md b/docs/src/index.md index f83c481e3..2b53b77bd 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -1,6 +1,27 @@ # HTTP.jl Documentation -`HTTP.jl` a Julia library for HTTP Messages. +`HTTP.jl` is a Julia library for HTTP Messages. + +[`HTTP.request`](@ref) sends a HTTP Request Message and +returns a Response Message. + +```julia +r = HTTP.request("GET", "http://httpbin.org/ip") +println(r.status) +println(String(r.body)) +``` + +[`HTTP.open`](@ref) sends a HTTP Request Message and +opens an `IO` stream from which the Response can be read. + +```julia +HTTP.open("GET", "https://tinyurl.com/bach-cello-suite-1-ogg") do http + open(`vlc -q --play-and-exit --intf dummy -`, "w") do vlc + write(vlc, http) + end +end +``` + ```@contents ``` @@ -10,34 +31,22 @@ ```@docs HTTP.request(::String,::HTTP.URIs.URI,::Array{Pair{String,String},1},::Any) +HTTP.open HTTP.get HTTP.put HTTP.post HTTP.head ``` +Request functions may throw the following exceptions: -## Requests -Note that the HTTP methods of POST, DELETE, PUT, etc. all follow the same format as `HTTP.get`, documented below. - -``` -@docs -HTTP.get -HTTP.Client -HTTP.Connection +```@docs +HTTP.StatusError +HTTP.ParsingError +HTTP.IOError ``` - - -### HTTP request errors - ``` -@docs -HTTP.ConnectError -HTTP.SendError -HTTP.ClosedError -HTTP.ReadError -HTTP.RedirectError -HTTP.StatusError +Base.DNSError ``` @@ -53,21 +62,27 @@ HTTP.register! ``` -## HTTP Types +## URIs ```@docs HTTP.URI +HTTP.URIs.escapeuri +HTTP.URIs.unescapeuri +HTTP.URIs.splitpath +Base.isvalid(::HTTP.URIs.URI) +``` + + +## Cookies + +```@docs HTTP.Cookie ``` -## HTTP Utilities +## Utilities ```@docs -HTTP.URIs.escapeuri -HTTP.URIs.unescapeuri -HTTP.URIs.splitpath -Base.isvalid(::HTTP.URIs.URI) HTTP.sniff HTTP.Strings.escapehtml ``` @@ -101,7 +116,7 @@ HTTP.StreamLayer *Source: `Parsers.jl`* ```@docs -HTTP.Parsers +HTTP.Parsers.Parser ``` @@ -113,6 +128,14 @@ HTTP.Messages ``` +## Streams +*Source: `Streams.jl`* + +```@docs +HTTP.Streams.Stream +``` + + ## Connections ### Basic Connections @@ -138,7 +161,8 @@ HTTP.ConnectionPool ## Parser Interface ```@docs -HTTP.Parsers.Parser +HTTP.Parsers.Message +HTTP.Parsers.Parser() HTTP.Parsers.parseheaders HTTP.Parsers.parsebody HTTP.Parsers.reset! @@ -177,6 +201,23 @@ HTTP.Messages.writeheaders Base.write(::IO,::HTTP.Messages.Message) ``` +## IOExtras Interface + +```@docs +HTTP.IOExtras +HTTP.IOExtras.unread! +HTTP.IOExtras.startwrite(::IO) +HTTP.IOExtras.isioerror +``` + + +## Streams Interface + +```@docs +HTTP.Streams.closebody +HTTP.Streams.isaborted +``` + ## Connections Interface @@ -190,6 +231,7 @@ HTTP.Connect.getconnection(::Type{TCPSocket},::AbstractString,::AbstractString) ```@docs HTTP.ConnectionPool.Connection +HTTP.ConnectionPool.Transaction HTTP.ConnectionPool.pool HTTP.Connect.getconnection(::Type{HTTP.ConnectionPool.Transaction{T}},::AbstractString,::AbstractString) where T <: IO HTTP.IOExtras.unread!(::HTTP.ConnectionPool.Transaction,::SubArray{UInt8,1,Array{UInt8,1},Tuple{UnitRange{Int64}},true}) diff --git a/docs/src/layers.monopic b/docs/src/layers.monopic index 403356d594560e55e66882d169c70f9ee3e0bc66..9d00a76e364e140add7680f893b98d592c3b2d59 100644 GIT binary patch literal 8960 zcmai(Wl$VYldiEqa2uRJaCZv?cXyZIHZZ|8Xb2D-26uONcL+}K2@Zoy@Zb)ce7m*x z{<(EeJ=N9n_f%K++pj8Ws%q*AQt*^fZ{hwu1jG}Qlh{>Vuzzw`fC?P5+2ZqOR>D!@ ze)>KPi(l#KlssWnUK5^9D{OM$L~jJdN8U%XM_Q$ZNEt;H�oBGWmDnDzb!1#S;@> zj&>HxHyNS?UPXEhYw1&aVWso&`?g0vDPgrvH4Ld+iDTE$!)w!%x!0J^{kPv*xAY)ZY_P1Y31rS%O#Yt-JZX5l5y+(PM?)pdYp_uaKe zzLKZG&Hb{kNT;vZM3EXhWJ1j?#rb-^v*ajs*?IbO!@nb&?cS&%P>tRxwGsOg(%4yf z8)bcB>yT=**|!4NynmPi_jvA1`RQK>I1%ZIEe32qbNL0j`HS=>VjEX)auDTibf*ez zOmcTOww~6Q!n`b9BgoFC&Mr26OkQTLkJop(A2nQe>PYj4S6eH6L>uhEugDL{XbXEj{g<+?*^gqmw?b-oG&x zHu8J&GnZ+u9KK-hB~kY`65PJi-Q8)`-{|Tb`{BeblWZ4X*%|<1$~Q-F+)&ny3%pSY zSUj|OZcoFfBw9Y0g4U2zXb$c(&!;EyaMIA(wIR@s4_*I@9)Zc3T z>aSCFd0pOXw7*V*fa6i2!+v(BVmuIm3FyYf{9{KK$MVnC=5}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-ofESmuIviQ2%ESmk;bNRYoc|mYEE#fOg(H` zf(T|Yh`3&Zu2h9H={QBy@uM@2`k^dZ>1w@Hp)j-5_#HF;FwJOz6aio5B;D7_pSJvk zpP7JAnUb(^ZdWy2vaHAH)d0l^twwzzk=q~gN&VH(;N-Lj8=7O}$Kp@`n-+%1@sLGm z+5MPXT+5r0kH|K0)IbYW?DCP3C^ApK@{-%pT-Mx(dEt#4p4`iRaa?fHr79Q^Z09Tnd`Tzg` diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index 46cfddc3f..715fe34ec 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -16,12 +16,12 @@ import ..@debug, ..DEBUG_LEVEL """ request(AWS4AuthLayer, ::URI, ::Request, body) -> HTTP.Response -Add a AWS Signature Version 4 `Authorization` header to a `Request`. +Add a [AWS Signature Version 4](http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html) +`Authorization` header to a `Request`. + Credentials are read from environment variables `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN`. - -See [http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html](@ref) """ abstract type AWS4AuthLayer{Next <: Layer} <: Layer end @@ -30,6 +30,11 @@ export AWS4AuthLayer function request(::Type{AWS4AuthLayer{Next}}, uri::URI, req, body; kw...) where Next + if !haskey(kw, :aws_access_key_id) && + !haskey(ENV, "AWS_ACCESS_KEY_ID") + kw = merge(dot_aws_credentials(), kw) + end + sign_aws4!(req.method, uri, req.headers, req.body; kw...) return request(Next, uri, req, body; kw...) @@ -120,4 +125,35 @@ function sign_aws4!(method::String, end +using IniFile + +credentials = NamedTuple() + +""" +Load Credentials from [AWS CLI ~/.aws/credentials file] +(http://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html). +""" + +function dot_aws_credentials()::NamedTuple + + global credentials + if !isempty(credentials) + return credentials + end + + f = get(ENV, "AWS_CONFIG_FILE", joinpath(homedir(), ".aws", "credentials")) + p = get(ENV, "AWS_DEFAULT_PROFILE", get(ENV, "AWS_PROFILE", "default")) + + if !isfile(f) + return NamedTuple() + end + + ini = read(Inifile(), f) + + credentials = ( + aws_access_key_id = String(get(ini, p, "aws_access_key_id")), + aws_secret_access_key = String(get(ini, p, "aws_secret_access_key"))) +end + + end # module BasicAuthRequest diff --git a/src/Connect.jl b/src/Connect.jl index 4d1732080..e33609cb9 100644 --- a/src/Connect.jl +++ b/src/Connect.jl @@ -1,5 +1,5 @@ """ -[`HTTP.Connect.getconnection`](@ref) creates a new `TCPSocket` or `SSLContext` +[`getconnection`](@ref) creates a new `TCPSocket` or `SSLContext` for a specified `host` and `port`. No connection streaming, pooling or reuse is implemented in this module. @@ -14,7 +14,7 @@ export getconnection, getparser, inactiveseconds, getrawstream using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! -import ..Parsers.Parser +import ..Parser import ..@debug, ..DEBUG_LEVEL diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 576f8738c..679b1f7a3 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -4,15 +4,15 @@ This module wrapps the basic Connect module above and adds support for: - Pipelining Request/Response Messages. i.e. allowing a new Request to be sent before previous Responses have been read. -This module defines a [`HTTP.ConnectionPool.Connection`](@ref) +This module defines a [`Connection`](@ref) struct to manage pipelining and connection reuse and a -[`HTTP.ConnectionPool.Transaction`](@ref)`<: IO` struct to manage a single +[`Transaction`](@ref)`<: IO` struct to manage a single pipelined request. Methods are provided for `eof`, `readavailable`, `unsafe_write` and `close`. This allows the `Transaction` object to act as a proxy for the `TCPSocket` or `SSLContext` that it wraps. -The [`HTTP.ConnectionPool.pool`](@ref) is a collection of open +The [`pool`](@ref) is a collection of open `Connection`s. The `request` function calls `getconnection` to retrieve a connection from the `pool`. When the `request` function has written a Request Message it calls `closewrite` to signal that @@ -32,7 +32,7 @@ using ..IOExtras import ..@debug, ..DEBUG_LEVEL, ..taskid, ..@require, ..precondition_error import MbedTLS.SSLContext import ..Connect: getconnection, getparser, getrawstream, inactiveseconds -import ..Parsers.Parser +import ..Parser const default_duplicate_limit = 7 @@ -56,6 +56,7 @@ end A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. +Fields: - `host::String` - `port::String`, exactly as specified in the URI (i.e. may be empty). - `pipeline_linit`, number of requests to send before waiting for responses. @@ -90,6 +91,15 @@ mutable struct Connection{T <: IO} parser::Parser end + +""" +A single pipelined HTTP Request/Response transaction`. + +Fields: + - `c`, the shared [`Connection`](@ref) used for this `Transaction`. + - `sequence::Int`, identifies this `Transaction` among the others that share `c`. +""" + struct Transaction{T <: IO} <: IO c::Connection{T} sequence::Int @@ -291,8 +301,6 @@ end """ - pool - The `pool` is a collection of open `Connection`s. The `request` function calls `getconnection` to retrieve a connection from the `pool`. When the `request` function has written a Request Message diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 3511bc81b..5ef139181 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -3,6 +3,7 @@ module ConnectionRequest import ..Layer, ..request using ..URIs using ..Messages +using ..IOExtras using ..ConnectionPool using MbedTLS.SSLContext import ..@debug, ..DEBUG_LEVEL @@ -11,10 +12,13 @@ import ..@debug, ..DEBUG_LEVEL """ request(ConnectionPoolLayer, ::URI, ::Request, body) -> HTTP.Response -Retrieve an `IO` connection from the [`HTTP.ConnectionPool`](@ref). +Retrieve an `IO` connection from the [`ConnectionPool`](@ref). Close the connection if the request throws an exception. Otherwise leave it open so that it can be reused. + +`IO` related exceptions from `Base` are wrapped in `HTTP.IOError`. +See [`isioerror`](@ref). """ abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end @@ -39,7 +43,7 @@ function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; catch e @debug 1 "❗️ ConnectionLayer $e. Closing: $io" close(io) - rethrow(e) + rethrow(isioerror(e) ? IOError(e) : e) end end diff --git a/src/ExceptionRequest.jl b/src/ExceptionRequest.jl index c43fcfa5e..4af8a6357 100644 --- a/src/ExceptionRequest.jl +++ b/src/ExceptionRequest.jl @@ -3,7 +3,8 @@ module ExceptionRequest export StatusError import ..Layer, ..request -using ..Messages +import ..HTTP +using ..Messages.iserror """ @@ -27,9 +28,17 @@ function request(::Type{ExceptionLayer{Next}}, a...; kw...) where Next end +""" +The `Response` has a `4xx`, `5xx` or unrecognised status code. + +Fields: + - `status::Int16`, the response status code. + - `response` the [`HTTP.Response`](@ref) +""" + struct StatusError <: Exception status::Int16 - response::Messages.Response + response::HTTP.Response end diff --git a/src/HTTP.jl b/src/HTTP.jl index 0839085e9..884e96a7f 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -13,22 +13,23 @@ include("compat.jl") include("debug.jl") include("Pairs.jl") include("Strings.jl") -include("IOExtras.jl") -include("uri.jl"); using .URIs +include("IOExtras.jl") ;import .IOExtras.IOError +include("uri.jl") ;using .URIs if !minimal include("consts.jl") include("utils.jl") -include("fifobuffer.jl"); using .FIFOBuffers -include("cookies.jl"); using .Cookies +include("fifobuffer.jl") ;using .FIFOBuffers +include("cookies.jl") ;using .Cookies include("multipart.jl") end -include("parser.jl"); import .Parsers: ParsingError, Headers +include("parser.jl") ;import .Parsers: Parser, Headers, Header, + ParsingError, ByteView include("Connect.jl") include("ConnectionPool.jl") -include("Messages.jl"); using .Messages +include("Messages.jl") ;using .Messages import .Messages: header, hasheader -include("Streams.jl"); using .Streams -include("WebSockets.jl"); using .WebSockets +include("Streams.jl") ;using .Streams +include("WebSockets.jl") ;using .WebSockets """ @@ -37,7 +38,8 @@ include("WebSockets.jl"); using .WebSockets Send a HTTP Request Message and recieve a HTTP Response Message. -``` +e.g. +```julia r = HTTP.request("GET", "http://httpbin.org/ip") println(r.status) println(String(r.body)) @@ -49,8 +51,7 @@ e.g. a `Dict()`, a `Vector{Tuple}`, a `Vector{Pair}` or an iterator. `body` can take a number of forms: - - a `String`, a `Vector{UInt8}` or a readable `IO` stream - or any `T` accepted by `write(::IO, ::T)` + - a `String`, a `Vector{UInt8}` or any `T` accepted by `write(::IO, ::T)` - a collection of `String` or `AbstractVector{UInt8}` or `IO` streams or items of any type `T` accepted by `write(::IO, ::T...)` - a readable `IO` stream or any `IO`-like type `T` for which @@ -61,24 +62,38 @@ The `HTTP.Response` struct contains: - `status::Int16` e.g. `200` - `headers::Vector{Pair{String,String}}` e.g. ["Server" => "Apache", "Content-Type" => "text/html"] - - `body::Vector{UInt8}`, the Response Body bytes. - Empty if a `response_stream` was specified in the `request`. + - `body::Vector{UInt8}`, the Response Body bytes + (empty if a `response_stream` was specified in the `request`). + +Functions `HTTP.get`, `HTTP.put`, `HTTP.post` and `HTTP.head` are defined as +shorthand for `HTTP.request("GET", ...)`, etc. + +`HTTP.request` and `HTTP.open` also accept optional keyword parameters. + +e.g. +```julia +HTTP.request("GET", "http://httpbin.org/ip"; retries=4, cookies=true) + +HTTP.get("http://s3.us-east-1.amazonaws.com/"; aws_authorization=true) -`HTTP.get`, `HTTP.put`, `HTTP.post` and `HTTP.head` are defined as shorthand -for `HTTP.request("GET", ...)`, etc. +conf = (timeout = 10, + pipeline_limit = 4, + retry = false, + redirect = false) -`HTTP.request` and `HTTP.open` also accept the following optional keyword -parameters: +HTTP.get("http://httpbin.org/ip"; conf..) +HTTP.put("http://httpbin.org/put", [], "Hello"; conf..) +``` -Streaming options (See [`HTTP.StreamLayer`](@ref)]) +Streaming options - `response_stream = nothing`, a writeable `IO` stream or any `IO`-like type `T` for which `write(T, AbstractVector{UInt8})` is defined. - `verbose = 0`, set to `1` or `2` for extra message logging. -Connection Pool options (See `ConnectionPool.jl`) +Connection Pool options - `connectionpool = true`, enable the `ConnectionPool`. - `duplicate_limit = 7`, number of duplicate connections to each host:port. @@ -87,32 +102,32 @@ Connection Pool options (See `ConnectionPool.jl`) - `socket_type = TCPSocket` -Timeout options (See [`HTTP.TimeoutLayer`](@ref)]) +Timeout options - `timeout = 60`, close the connection if no data is recieved for this many seconds. Use `timeout = 0` to disable. -Retry options (See [`HTTP.RetryLayer`](@ref)]) +Retry options - `retry = true`, retry idempotent requests in case of error. - `retries = 4`, number of times to retry. - `retry_non_idempotent = false`, retry non-idempotent requests too. e.g. POST. -Redirect options (See [`HTTP.RedirectLayer`](@ref)]) +Redirect options - `redirect = true`, follow 3xx redirect responses. - `redirect_limit = 3`, number of times to redirect. - `forwardheaders = false`, forward original headers on redirect. -Status Exception options (See [`HTTP.ExceptionLayer`](@ref)]) +Status Exception options - `statusexception = true`, throw `HTTP.StatusError` for response status >= 300. -SSLContext options (See `Connect.jl`) +SSLContext options - `require_ssl_verification = false`, pass `MBEDTLS_SSL_VERIFY_REQUIRED` to the mbed TLS library. @@ -121,13 +136,14 @@ SSLContext options (See `Connect.jl`) - sslconfig = SSLConfig(require_ssl_verification)` -Basic Authenticaiton options (See [`HTTP.BasicAuthLayer`](@ref)]) +Basic Authenticaiton options - basicauthorization=false, add `Authorization: Basic` header using credentials from url userinfo. -AWS Authenticaiton options (See [`HTTP.AWS4AuthLayer`](@ref)]) +AWS Authenticaiton options + - `awsauthorization = false`, enable AWS4 Authentication. - `aws_service = split(uri.host, ".")[1]` - `aws_region = split(uri.host, ".")[2]` @@ -138,13 +154,13 @@ AWS Authenticaiton options (See [`HTTP.AWS4AuthLayer`](@ref)]) - `body_md5 = digest(MD_MD5, body)`, -Cookie options (See [`HTTP.CookieLayer`](@ref)]) +Cookie options - `cookies = false`, enable cookies. - `cookiejar::Dict{String, Set{Cookie}}=default_cookiejar` -Cananoincalization options (See [`HTTP.CanonicalizeLayer`](@ref)]) +Cananoincalization options - `canonicalizeheaders = false`, rewrite request and response headers in Canonical-Camel-Dash-Format. @@ -153,79 +169,84 @@ Cananoincalization options (See [`HTTP.CanonicalizeLayer`](@ref)]) ## Request Body Examples String body: -``` -r = request("POST", "http://httpbin.org/post", [], "post body data") -@show r.status +```julia +request("POST", "http://httpbin.org/post", [], "post body data") ``` Stream body from file: -``` +```julia io = open("post_data.txt", "r") -r = request("POST", "http://httpbin.org/post", [], io) -@show r.status +request("POST", "http://httpbin.org/post", [], io) ``` Generator body: -``` +```julia chunks = ("chunk\$i" for i in 1:1000) -r = request("POST", "http://httpbin.org/post", [], chunks) -@show r.status +request("POST", "http://httpbin.org/post", [], chunks) ``` Collection body: -``` +```julia chunks = [preamble_chunk, data_chunk, checksum(data_chunk)] -r = request("POST", "http://httpbin.org/post", [], chunks) -@show r.status +request("POST", "http://httpbin.org/post", [], chunks) ``` `open() do io` body: -``` -r = HTTP.open("POST", "http://httpbin.org/post") do io +```julia +HTTP.open("POST", "http://httpbin.org/post") do io write(io, preamble_chunk) write(io, data_chunk) write(io, checksum(data_chunk)) end -@show r.status ``` ## Response Body Examples String body: -``` +```julia r = request("GET", "http://httpbin.org/get") -@show r.status println(String(r.body)) ``` Stream body to file: -``` +```julia io = open("get_data.txt", "w") r = request("GET", "http://httpbin.org/get", response_stream=io) -@show r.status +close(io) println(read("get_data.txt")) ``` Stream body through buffer: -``` +```julia io = BufferStream() @async while !eof(io) bytes = readavailable(io)) println("GET data: \$bytes") end r = request("GET", "http://httpbin.org/get", response_stream=io) -@show r.status +close(io) ``` Stream body through `open() do io`: -``` -r = HTTP.open("GET", "http://httpbin.org/get") do io - r = startread(io) - @show r.status - while !eof(io) - bytes = readavailable(io)) - println("GET data: \$bytes") +```julia +r = HTTP.open("GET", "http://httpbin.org/stream/10") do io + while !eof(io) + println(String(readavailable(io))) + end +end + +HTTP.open("GET", "https://tinyurl.com/bach-cello-suite-1-ogg") do http + n = 0 + r = startread(http) + l = parse(Int, header(r, "Content-Length")) + open(`vlc -q --play-and-exit --intf dummy -`, "w") do vlc + while !eof(http) + bytes = readavailable(http) + write(vlc, bytes) + n += length(bytes) + println("streamed \$n-bytes \$((100*n)÷l)%\\u1b[1A") + end end end ``` @@ -234,22 +255,20 @@ end ## Request and Response Body Examples String bodies: -``` +```julia r = request("POST", "http://httpbin.org/post", [], "post body data") -@show r.status println(String(r.body)) ``` Stream bodies from and to files: -``` +```julia in = open("foo.png", "r") out = open("foo.jpg", "w") -r = request("POST", "http://convert.com/png2jpg", [], in, response_stream=out) -@show r.status +request("POST", "http://convert.com/png2jpg", [], in, response_stream=out) ``` Stream bodies through: `open() do io`: -``` +```julia HTTP.open("POST", "http://music.com/play") do io write(io, JSON.json([ "auth" => "12345XXXX", @@ -273,20 +292,26 @@ request(method, uri, headers=[], body=UInt8[]; kw...)::Response = """ - HTTP.open(method, url, [,headers]) do - write(io, bytes) - end -> HTTP.Response - -The `HTTP.open` API allows the Request Body to be written to an `IO` stream. -`HTTP.open` also allows the Response Body to be streamed: - - HTTP.open(method, url, [,headers]) do io + write(io, body) [startread(io) -> HTTP.Response] while !eof(io) readavailable(io) -> AbstractVector{UInt8} end end -> HTTP.Response + +The `HTTP.open` API allows the Request Body to be written to (and/or the +Response Body to be read from) an `IO` stream. + + +e.g. Streaming an audio file to the `vlc` player: +```julia +HTTP.open("GET", "https://tinyurl.com/bach-cello-suite-1-ogg") do http + open(`vlc -q --play-and-exit --intf dummy -`, "w") do vlc + write(vlc, http) + end +end +``` """ open(f::Function, method::String, uri, headers=[]; kw...)::Response = @@ -413,7 +438,7 @@ in the stack: The minimal request execution stack is: -``` +```julia stack = MessageLayer{ConnectionPoolLayer{StreamLayer}} ``` @@ -456,68 +481,71 @@ HTTP.Connect.getconnection() directly rather reusing pooled connections. ``` The next figure illustrates the full Layer-stack and its relationship with -the [`HTTP.Response`](@ref), the [`HTTP.Parser`](@ref), -the [`HTTP.Stream`](@ref) and the [`HTTP.ConnectionPool`](@ref). +[`HTTP.Response`](@ref), [`HTTP.Parser`](@ref), +[`HTTP.Stream`](@ref) and the [`HTTP.ConnectionPool`](@ref). ``` ┌────────────────────────────────────────────────────────────────────────────┐ - │ ┌──────────────────┐ │ - │ HTTP.jl Request Stack │ HTTP.StatusError │ │ - │ └───────────┬──────┘ │ │ ┌───────────────────┐ │ - │ request(method, uri, headers, body) -> │ HTTP.Response │ │ │ - │ ────────────────────────── └─────────▲─────────┘ │ - │ ║ ║ │ │ - │ ┌────────────────────────────────────────────────────────────┐ │ - │ │ request(RedirectLayer, method, ::URI, ::Headers, body) │ │ │ - │ ├────────────────────────────────────────────────────────────┤ │ - │ │ request(BasicAuthLayer, method, ::URI, ::Headers, body) │ │ │ - │ ├────────────────────────────────────────────────────────────┤ │ - │ │ request(CookieLayer, method, ::URI, ::Headers, body) │ │ │ - │ ├────────────────────────────────────────────────────────────┤ │ - │ │ request(CanonicalizeLayer, method, ::URI, ::Headers, body) │ │ │ - │ ├────────────────────────────────────────────────────────────┤ │ - │ │ request(MessageLayer, method, ::URI, ::Headers, body) │ │ │ - │ ├────────────────────────────────────────────────────────────┤ │ - │ │ request(AWS4AuthLayer, ::URI, ::Request, body) │ │ │ - │ ├────────────────────────────────────────────────────────────┤ │ - │ │ request(RetryLayer, ::URI, ::Request, body) │ │ │ - │ ├────────────────────────────────────────────────────────────┤ │ - │ │ request(ExceptionLayer, ::URI, ::Request, body) │─ ┘ │ - │ ├────────────────────────────────────────────────────────────┤ │ -┌┼───┤ request(ConnectionPoolLayer, ::URI, ::Request, body) │ │ -││ ├────────────────────────────────────────────────────────────┤ │ + │ HTTP.jl Request Stack │ HTTP.ParsingError ├ ─ ─ ─ ─ ┐ │ + │ └───────────────────┘ │ + │ ┌───────────────────┐ │ │ + │ │ HTTP.IOError ├ ─ ─ ─ │ + │ └───────────────────┘ │ │ │ + │ ┌───────────────────┐ │ + │ │ HTTP.StatusError │─ ─ │ │ │ + │ └───────────────────┘ │ │ + │ ┌───────────────────┐ │ │ │ + │ request(method, uri, headers, body) -> │ HTTP.Response │ │ │ + │ ────────────────────────── └─────────▲─────────┘ │ │ │ + │ ║ ║ │ │ + │ ┌────────────────────────────────────────────────────────────┐ │ │ │ + │ │ request(RedirectLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ │ │ + │ │ request(BasicAuthLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ │ │ + │ │ request(CookieLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ │ │ + │ │ request(CanonicalizeLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ │ │ + │ │ request(MessageLayer, method, ::URI, ::Headers, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ │ │ + │ │ request(AWS4AuthLayer, ::URI, ::Request, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ │ │ + │ │ request(RetryLayer, ::URI, ::Request, body) │ │ │ + │ ├────────────────────────────────────────────────────────────┤ │ │ │ + │ │ request(ExceptionLayer, ::URI, ::Request, body) ├ ─ ┘ │ + │ ├────────────────────────────────────────────────────────────┤ │ │ │ +┌┼───┤ request(ConnectionPoolLayer, ::URI, ::Request, body) ├ ─ ─ ─ │ +││ ├────────────────────────────────────────────────────────────┤ │ │ ││ │ request(TimeoutLayer, ::IO, ::Request, body) │ │ -││ ├────────────────────────────────────────────────────────────┤ │ +││ ├────────────────────────────────────────────────────────────┤ │ │ ││ │ request(StreamLayer, ::IO, ::Request, body) │ │ -││ └──────────────┬───────────────────┬─────────────────────────┘ │ +││ └──────────────┬───────────────────┬─────────────────────────┘ │ │ │└──────────────────┼────────║──────────┼───────────────║─────────────────────┘ -│ │ ║ │ ║ +│ │ ║ │ ║ │ │┌──────────────────▼───────────────┐ │ ┌──────────────────────────────────┐ -││ HTTP.Request │ │ │ HTTP.Response │ +││ HTTP.Request │ │ │ HTTP.Response │ │ ││ │ │ │ │ -││ method::String ◀───┼──▶ status::Int │ +││ method::String ◀───┼──▶ status::Int │ │ ││ uri::String │ │ │ headers::Vector{Pair} │ -││ headers::Vector{Pair} │ │ │ body::Vector{UInt8} │ +││ headers::Vector{Pair} │ │ │ body::Vector{UInt8} │ │ ││ body::Vector{UInt8} │ │ │ │ -│└──────────────────▲───────────────┘ │ └───────────────▲──────────────────┘ +│└──────────────────▲───────────────┘ │ └───────────────▲────────────────┼─┘ │┌──────────────────┴────────║──────────▼───────────────║──┴──────────────────┐ -││ HTTP.Stream <:IO ║ ╔══════╗ ║ │ +││ HTTP.Stream <:IO ║ ╔══════╗ ║ │ │ ││ ┌───────────────────────────┐ ║ ┌──▼─────────────────────────┐ │ -││ │ startwrite(::Stream) │ ║ │ startread(::Stream) │ │ +││ │ startwrite(::Stream) │ ║ │ startread(::Stream) │ │ │ ││ │ write(::Stream, body) │ ║ │ read(::Stream) -> body │ │ -││ │ ... │ ║ │ ... │ │ +││ │ ... │ ║ │ ... │ │ │ ││ │ closewrite(::Stream) │ ║ │ closeread(::Stream) │ │ -││ └───────────────────────────┘ ║ └────────────────────────────┘ │ -││ EOFError <:Exception ║ ║ ║ ║ │ +││ └───────────────────────────┘ ║ └────────────────────────────┘ │ │ │└───────────────────────────║────────┬──║──────║───────║──┬──────────────────┘ -│┌──────────────────────────────────┐ │ ║ ┌────▼───────║──▼──────────────────┐ +│┌──────────────────────────────────┐ │ ║ ┌────▼───────║──▼────────────────┴─┐ ││ HTTP.Messages │ │ ║ │ HTTP.Parser │ ││ │ │ ║ │ │ ││ writestartline(::IO, ::Request) │ │ ║ │ parseheaders(bytes) do h::Pair │ ││ writeheaders(::IO, ::Request) │ │ ║ │ parsebody(bytes) -> bytes │ -││ │ │ ║ │ │ -││ │ │ ║ │ ParsingError <:Exception │ │└──────────────────────────────────┘ │ ║ └──────────────────────────────────┘ │ ║ │ ║ │┌───────────────────────────║────────┼──║────────────────────────────────────┐ @@ -537,13 +565,13 @@ the [`HTTP.Stream`](@ref) and the [`HTTP.ConnectionPool`](@ref). │ getconnection() -> │ Base.TCPSocket <:IO │ │MbedTLS.SSLContext <:IO│ │ │ └───────────────────────┘ └───────────┬───────────┘ │ │ ║ ║ │ │ - │ EOFError <:Exception ║ ║ ┌───────────▼───────────┐ │ - │ UVError <:Exception ║ ║ │ Base.TCPSocket <:IO │ │ - │ DNSError <:Exception ║ ║ └───────────────────────┘ │ + │ ║ ║ ┌───────────▼───────────┐ │ + │ ║ ║ │ Base.TCPSocket <:IO │ │ + │ ║ ║ └───────────────────────┘ │ └───────────────────────────║───────────║────────────────────────────────────┘ ║ ║ ┌───────────────────────────║───────────║──────────────┐ ┏━━━━━━━━━━━━━━━━━━┓ - │ HTTP Server ▼ ║ │ ┃ data flow: ════▶ ┃ + │ HTTP Server ▼ │ ┃ data flow: ════▶ ┃ │ Request Response │ ┃ reference: ────▶ ┃ └──────────────────────────────────────────────────────┘ ┗━━━━━━━━━━━━━━━━━━┛ ``` @@ -551,12 +579,12 @@ the [`HTTP.Stream`](@ref) and the [`HTTP.ConnectionPool`](@ref). """ function stack(;redirect=true, - basicauthorization=false, - awsauthorization=false, + basic_authorization=false, + aws_authorization=false, cookies=false, - canonicalizeheaders=false, + canonicalize_headers=false, retry=true, - statusexception=true, + status_exception=true, timeout=0, kw...) if minimal @@ -564,17 +592,17 @@ function stack(;redirect=true, else NoLayer = Union - (redirect ? RedirectLayer : NoLayer){ - (basicauthorization ? BasicAuthLayer : NoLayer){ - (cookies ? CookieLayer : NoLayer){ - (canonicalizeheaders ? CanonicalizeLayer : NoLayer){ - MessageLayer{ - (awsauthorization ? AWS4AuthLayer : NoLayer){ - (retry ? RetryLayer : NoLayer){ - (statusexception ? ExceptionLayer : NoLayer){ - ConnectionPoolLayer{ - (timeout > 0 ? TimeoutLayer : NoLayer){ - StreamLayer + (redirect ? RedirectLayer : NoLayer){ + (basic_authorization ? BasicAuthLayer : NoLayer){ + (cookies ? CookieLayer : NoLayer){ + (canonicalize_headers ? CanonicalizeLayer : NoLayer){ + MessageLayer{ + (aws_authorization ? AWS4AuthLayer : NoLayer){ + (retry ? RetryLayer : NoLayer){ + (status_exception ? ExceptionLayer : NoLayer){ + ConnectionPoolLayer{ + (timeout > 0 ? TimeoutLayer : NoLayer){ + StreamLayer }}}}}}}}}} end end diff --git a/src/IOExtras.jl b/src/IOExtras.jl index fedfaad86..c22720757 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -1,14 +1,43 @@ +""" +This module defines extensions to the `Base.IO` interface to support: + - an `unread!` function for pushing excess bytes back into a stream, + - `startwrite`, `closewrite`, `startread` and `closeread` for streams + with transactional semantics. +""" + module IOExtras -export isioerror, unread!, startwrite, closewrite, startread, closeread, +export IOError, isioerror, + unread!, + startwrite, closewrite, startread, closeread, tcpsocket, localport, peerport +""" + isioerror(exception) + +Is `exception` caused by a possibly recoverable IO error. +""" + isioerror(e) = false isioerror(::Base.EOFError) = true isioerror(::Base.UVError) = true isioerror(e::ArgumentError) = e.msg == "stream is closed or unusable" +""" +The request terminated with due to an IO-related error. + +Fields: + - `e`, the error. +""" + +struct IOError + e +end + +Base.show(io::IO, e::IOError) = show(io, e.e) + + """ unread!(::IO, bytes) diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 5d3bf5cb9..565c92fd0 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -5,14 +5,14 @@ export body_is_a_stream, body_was_streamed import ..Layer, ..request using ..URIs using ..Messages -using ..Parsers.Headers +using ..Headers using ..Form """ request(MessageLayer, method, ::URI, headers, body) -> HTTP.Response -Construct a [`HTTP.Request`](@ref) object and set mandatory headers. +Construct a [`Request`](@ref) object and set mandatory headers. """ struct MessageLayer{Next <: Layer} <: Layer end diff --git a/src/Messages.jl b/src/Messages.jl index dde032c09..9c5e75019 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -1,6 +1,6 @@ """ -The `Messages` module defines structs that represent [`HTTP.Messages.Request`](@ref) -and [`HTTP.Messages.Response`](@ref) Messages. +The `Messages` module defines structs that represent [`HTTP.Request`](@ref) +and [`HTTP.Response`](@ref) Messages. The `Response` struct has a `request` field that points to the corresponding `Request`; and the `Request` struct has a `response` field. @@ -51,7 +51,7 @@ multiple `Set-Cookie` headers. ### Bodies -The [`HTTP.Message`](@ref) structs represent the Message Body as `Vector{UInt8}`. +The `HTTP.Message` structs represent the Message Body as `Vector{UInt8}`. Streaming of request and response bodies is handled by the [`HTTP.StreamLayer`](@ref) and the [`HTTP.Stream`](@ref) `<: IO` stream. diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index 5c8ead07a..b6b64794a 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -4,8 +4,7 @@ import ..Layer, ..request using ..URIs using ..Messages using ..Pairs: setkv -using ..Parsers.Header -using ..Strings.tocameldash! +using ..Header import ..@debug, ..DEBUG_LEVEL diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index cf4ed44d6..54407f625 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -2,7 +2,7 @@ module RetryRequest import ..HTTP import ..Layer, ..request -using ..IOExtras.isioerror +using ..IOExtras using ..MessageRequest using ..Messages import ..@debug, ..DEBUG_LEVEL @@ -18,7 +18,7 @@ increasing delay is introduced between attempts to avoid exacerbating network congestion. Methods of `isrecoverable(e)` define which exception types lead to a retry. -e.g. `Base.UVError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` +e.g. `HTTP.IOError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` (if status is ``5xx`). """ @@ -46,7 +46,8 @@ function request(::Type{RetryLayer{Next}}, uri, req, body; end -isrecoverable(e::Exception) = isioerror(e) +isrecoverable(e) = false +isrecoverable(e::IOError) = true isrecoverable(e::Base.DNSError) = true isrecoverable(e::HTTP.StatusError) = e.status == 403 || # Forbidden e.status == 408 || # Timeout diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 73aa7dfc8..4865c393b 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -13,12 +13,12 @@ import ..@debug, ..DEBUG_LEVEL, ..printlncompact """ request(StreamLayer, ::IO, ::Request, body) -> HTTP.Response -Create a [`HTTP.Stream`](@ref) to send a `Request` and `body` to an `IO` +Create a [`Stream`](@ref) to send a `Request` and `body` to an `IO` stream and read the response. Sens the `Request` body in a background task and begins reading the response immediately so that the transmission can be aborted if the `Response` status -indicates that the server does not wish to receive the message body +indicates that the server does not wish to receive the message body. [RFC7230 6.5](https://tools.ietf.org/html/rfc7230#section-6.5). """ diff --git a/src/Streams.jl b/src/Streams.jl index 71b9fb2cd..5481e48c0 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -18,6 +18,24 @@ mutable struct Stream{T <: Message} <: IO writechunked::Bool end + +""" + Stream(::IO, ::Request, ::Parser) + +Creates a `HTTP.Stream` that wraps an existing `IO` stream. + + - `startwrite(::Stream)` sends the `Request` headers to the `IO` stream. + - `write(::Stream, body)` sends the `body` (or a chunk of the bocdy). + - `closewrite(::Stream)` sends the final `0` chunk (if needed) and calls + `closewrite` on the `IO` stream. + + - `startread(::Stream)` parses the `Response` headers from the `IO` stream. + - `eof(::Stream)` and `readavailable(::Stream)` parse the body from the `IO` + stream. + - `closeread(::Stream)` reads the trailers and calls `closeread` on the `IO` + stream. +""" + function Stream(io::IO, request::Request, parser::Parser) @require iswritable(io) writechunked = header(request, "Transfer-Encoding") == "chunked" @@ -48,6 +66,11 @@ function Base.unsafe_write(http::Stream, p::Ptr{UInt8}, n::UInt) write(http.stream, "\r\n") end +""" + closebody(::Stream) + +Write the final `0` chunk if needed. +""" function closebody(http::Stream) if http.writechunked @@ -138,12 +161,18 @@ function Base.read(http::Stream) end -function isaborted(http::Stream{Response}) +""" + isaborted(::Stream{Response}) + +Has the server signalled that it does not wish to receive the message body? - # "If [the response] indicates the server does not wish to receive the - # message body and is closing the connection, the client SHOULD - # immediately cease transmitting the body and close the connection." - # https://tools.ietf.org/html/rfc7230#section-6.5 +"If [the response] indicates the server does not wish to receive the + message body and is closing the connection, the client SHOULD + immediately cease transmitting the body and close the connection." +[RFC7230, 6.5](https://tools.ietf.org/html/rfc7230#section-6.5) +""" + +function isaborted(http::Stream{Response}) if iswritable(http.stream) && iserror(http.message) && diff --git a/src/compat.jl b/src/compat.jl index 6a01bb729..aed091b30 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -33,3 +33,5 @@ end else lockedby(l) = get(l.locked_by) end + +Base.String(x::SubArray{UInt8,1}) = String(Vector{UInt8}(x)) diff --git a/src/parser.jl b/src/parser.jl index 56e30f777..f93f9760c 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -23,27 +23,6 @@ # -""" -The parser separates a raw HTTP Message into its component parts. - -If the input data is invalid the Parser throws a [`HTTP.ParsingError`](@ref). - -The parser processes a single HTTP Message. If the input stream contains -multiple Messages the Parser stops at the end of the first Message. -The `parseheaders` and `parsebody` functions return a `SubArray` containing the -unuses portion of the input. - -The Parser does not interpret the Message Headers except as needed -to parse the Message Body. It is beyond the scope of the Parser to deal -with repeated header fields, multi-line values, cookies or case normalization -(see [`HTTP.Messages.appendheader`](@ref)). - -The Parser has no knowledge of the high-level `Request` and `Response` structs -defined in `Messages.jl`. The Parser has it's own low level -[`HTTP.Parsers.Message`](@ref) struct that represents both Request and Response -Messages. -""" - module Parsers export Parser, Header, Headers, ByteView, nobytes, @@ -73,6 +52,14 @@ const ByteView = typeof(nobytes) const Header = Pair{String,String} const Headers = Vector{Header} +""" + - `method::Method`: internal parser `@enum` for HTTP method. + - `major` and `minor`: HTTP version + - `url::String`: request URL + - `status::Int`: response status + - `upgrade::Bool`: Connection should be upgraded to a different protocol. + e.g. `CONNECT` or `Connection: upgrade`. +""" mutable struct Message method::Method @@ -86,6 +73,26 @@ end Message() = Message(NOMETHOD, 0, 0, "", 0, false) +""" +The parser separates a raw HTTP Message into its component parts. + +If the input data is invalid the Parser throws a `ParsingError`. + +The parser processes a single HTTP Message. If the input stream contains +multiple Messages the Parser stops at the end of the first Message. +The `parseheaders` and `parsebody` functions return a `SubArray` containing the +unuses portion of the input. + +The Parser does not interpret the Message Headers except as needed +to parse the Message Body. It is beyond the scope of the Parser to deal +with repeated header fields, multi-line values, cookies or case normalization. + +The Parser has no knowledge of the high-level `Request` and `Response` structs +defined in `Messages.jl`. The Parser has it's own low level +[`Message`](@ref) struct that represents both Request and Response +Messages. +""" + mutable struct Parser # config @@ -234,6 +241,16 @@ connectionclosed(p::Parser) = p.flags & F_CONNECTION_CLOSE > 0 isrequest(p::Parser) = p.message.status == 0 +""" +The [`Parser`] input was invalid. + +Fields: + - `code`, internal `@enum ParsingErrorCode`. + - `state`, internal parsing state. + - `status::Int32`, HTTP response status. + - `msg::String`, error message. +""" + struct ParsingError <: Exception code::ParsingErrorCode state::UInt8 diff --git a/test/async.jl b/test/async.jl index f2b8631dc..c57569ae0 100644 --- a/test/async.jl +++ b/test/async.jl @@ -299,8 +299,6 @@ println("running async $count, 1:$num, $config, $http C") end # testset -sleep(12) - stop_pool_dump=true HTTP.ConnectionPool.showpool(STDOUT) diff --git a/test/client.jl b/test/client.jl index 30267f1ca..b54edfe4a 100644 --- a/test/client.jl +++ b/test/client.jl @@ -167,8 +167,8 @@ for sch in ("http", "https") @test status(HTTP.post("$sch://httpbin.org/post"; body="√")) == 200 println("client basic auth") - @test status(HTTP.get("$sch://user:pwd@httpbin.org/basic-auth/user/pwd"; basicauthorization=true)) == 200 - @test status(HTTP.get("$sch://user:pwd@httpbin.org/hidden-basic-auth/user/pwd"; basicauthorization=true)) == 200 + @test status(HTTP.get("$sch://user:pwd@httpbin.org/basic-auth/user/pwd"; basic_authorization=true)) == 200 + @test status(HTTP.get("$sch://user:pwd@httpbin.org/hidden-basic-auth/user/pwd"; basic_authorization=true)) == 200 # custom client & other high-level entries println("high-level client request methods") diff --git a/test/loopback.jl b/test/loopback.jl index 77532bab1..a990c0f75 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -1,3 +1,4 @@ +using Test using HTTP using HTTP.IOExtras @@ -121,8 +122,9 @@ function Base.unsafe_write(lb::Loopback, p::Ptr{UInt8}, n::UInt) if req.uri == "/echo" push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) - elseif ismatch(r"^/delay", req.uri) - sleep(0.5) + elseif (m = match(r"^/delay([0-9]*)$", req.uri)) != nothing + t = parse(Int, first(m.captures)) + sleep(t/10) push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) else @@ -179,16 +181,16 @@ lbopen(f, req, headers) = Vector{UInt8}("World!")]); @test String(r.body) == "Hello World!" - r = lbreq("delay", [], [Vector{UInt8}("Hello"), - Vector{UInt8}(" "), - Vector{UInt8}("World!")]); + r = lbreq("delay10", [], [Vector{UInt8}("Hello"), + Vector{UInt8}(" "), + Vector{UInt8}("World!")]); @test String(r.body) == "Hello World!" HTTP.ConnectionPool.showpool(STDOUT) body = nothing body_sent = false - r = lbopen("delay", []) do http + r = lbopen("delay10", []) do http @sync begin @async begin write(http, "Hello World!") @@ -262,26 +264,20 @@ lbopen(f, req, headers) = r5 = nothing t1 = time() @sync begin - @async r1 = lbreq("delay1", [], - FunctionIO(()->(sleep(0.01); "Hello World! 1")); + @async r1 = lbreq("delay1", [], FunctionIO(()->(sleep(0.00); "Hello World! 1")); method=m[1], kw...) - sleep(0.01) @async r2 = lbreq("delay2", [], - FunctionIO(()->(sleep(0.02); "Hello World! 2")); + FunctionIO(()->(sleep(0.01); "Hello World! 2")); method=m[2], kw...) - sleep(0.01) @async r3 = lbreq("delay3", [], - FunctionIO(()->(sleep(0.03); "Hello World! 3")); + FunctionIO(()->(sleep(0.02); "Hello World! 3")); method=m[3], kw...) - sleep(0.01) @async r4 = lbreq("delay4", [], - FunctionIO(()->(sleep(0.04); "Hello World! 4")); + FunctionIO(()->(sleep(0.03); "Hello World! 4")); method=m[4], kw...) - sleep(0.01) @async r5 = lbreq("delay5", [], - FunctionIO(()->(sleep(0.05); "Hello World! 5")); + FunctionIO(()->(sleep(0.04); "Hello World! 5")); method=m[5], kw...) - sleep(0.01) end t2 = time() @@ -298,7 +294,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=0) @show t - @test 2.8 < t < 3.3 + @test 2.1 < t < 2.3 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", @@ -314,7 +310,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=1) @show t - @test 1.4 < t < 1.8 + @test 0.9 < t < 1.1 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -330,7 +326,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=2) @show t - @test 1 < t < 1.4 + @test 0.6 < t < 1 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -346,7 +342,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=3) @show t - @test 0.8 < t < 1.2 + @test 0.5 < t < 0.8 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -362,7 +358,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test() @show t - @test 0.6 < t < 1 + @test 0.5 < t < 0.8 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", From 5e0dadec79e55956d58ff4dc1b7c5fb18211a253 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 8 Jan 2018 21:05:40 +1100 Subject: [PATCH 122/182] Remove Connect.jl module. Rename duplicate_limit to connection_limit. Rename timeout to readtimeout (to match original API). Use const nobody for default empty body to avoid allocation. Make default headers = Header[] rather than just [] for type stability. Move IOExtras unread!(::IOBuffer) and unread!(::BufferStream) to ../test/ Improve Streams.jl doc strings. Add `URI(uri::URI) = uri` to avoid reconstructing URIs --- src/Connect.jl | 64 ------------------------- src/ConnectionPool.jl | 47 ++++++++++++++---- src/ConnectionRequest.jl | 13 ++--- src/HTTP.jl | 100 ++++++++++++--------------------------- src/IOExtras.jl | 33 ------------- src/MessageRequest.jl | 5 ++ src/Streams.jl | 22 ++++++--- src/TimeoutRequest.jl | 6 +-- src/client.jl | 14 ++---- src/precompile.jl | 4 ++ src/uri.jl | 4 +- test/async.jl | 2 +- test/loopback.jl | 10 ++-- test/parser.jl | 34 +++++++++++++ 14 files changed, 146 insertions(+), 212 deletions(-) delete mode 100644 src/Connect.jl diff --git a/src/Connect.jl b/src/Connect.jl deleted file mode 100644 index e33609cb9..000000000 --- a/src/Connect.jl +++ /dev/null @@ -1,64 +0,0 @@ -""" -[`getconnection`](@ref) creates a new `TCPSocket` or `SSLContext` -for a specified `host` and `port`. - -No connection streaming, pooling or reuse is implemented in this module. -However, the `getconnection` interface is the same as the one used by the -connection pool so the `Connect` module can be used directly when reuse is -not required. -""" - -module Connect - -export getconnection, getparser, inactiveseconds, getrawstream - -using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! - -import ..Parser -import ..@debug, ..DEBUG_LEVEL - - -""" - getconnection(type, host, port) -> IO - -Create a new `TCPSocket` or `SSLContext` connection. - -Note: this `Connect` module creates simple unadorned connection objects. -The `Connections` module has the same interface but supports connection -reuse and request interleaving. -""" - -function getconnection(::Type{TCPSocket}, - host::AbstractString, - port::AbstractString; - kw...)::TCPSocket - - p::UInt = isempty(port) ? UInt(80) : parse(UInt, port) - @debug 2 "TCP connect: $host:$p..." - connect(getaddrinfo(host), p) -end - -function getconnection(::Type{SSLContext}, - host::AbstractString, - port::AbstractString; - require_ssl_verification::Bool=false, - sslconfig::SSLConfig=SSLConfig(require_ssl_verification), - kw...)::SSLContext - - port = isempty(port) ? "443" : port - @debug 2 "SSL connect: $host:$port..." - io = SSLContext() - setup!(io, sslconfig) - associate!(io, getconnection(TCPSocket, host, port)) - hostname!(io, host) - handshake!(io) - return io -end - -getparser(::IO) = Parser() - -getrawstream(io::IO) = io - -inactiveseconds(::IO)= Float64(0) - -end # module Connect diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 679b1f7a3..1bee3e8ba 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -1,5 +1,6 @@ """ -This module wrapps the basic Connect module above and adds support for: +This module provides the [`getconnection`](@ref) function with support for: +- Opening TCP and SSL connections. - Reusing connections for multiple Request/Response Messages, - Pipelining Request/Response Messages. i.e. allowing a new Request to be sent before previous Responses have been read. @@ -20,9 +21,8 @@ the `Connection` can be reused for writing (to send the next Request). When the `request` function has read the Response Message it calls `closeread` to signal that the `Connection` can be reused for reading. -``` - """ + module ConnectionPool export getconnection, getparser, getrawstream, inactiveseconds @@ -30,12 +30,12 @@ export getconnection, getparser, getrawstream, inactiveseconds using ..IOExtras import ..@debug, ..DEBUG_LEVEL, ..taskid, ..@require, ..precondition_error -import MbedTLS.SSLContext -import ..Connect: getconnection, getparser, getrawstream, inactiveseconds +using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! + import ..Parser -const default_duplicate_limit = 7 +const default_connection_limit = 8 const default_pipeline_limit = 16 const nolimit = typemax(Int) @@ -419,7 +419,7 @@ or create a new `Connection` if required. function getconnection(::Type{Transaction{T}}, host::AbstractString, port::AbstractString; - duplicate_limit::Int=default_duplicate_limit, + connection_limit::Int=default_connection_limit, pipeline_limit::Int=default_pipeline_limit, reuse_limit::Int=nolimit, kw...)::Transaction{T} where T <: IO @@ -448,10 +448,10 @@ function getconnection(::Type{Transaction{T}}, return Transaction{T}(c) end - # If there are not too many duplicates for this host, + # If there are not too many connections to this host:port, # create a new connection... busy = findall(T, host, port, pipeline_limit) - if length(busy) < duplicate_limit + 1 + if length(busy) < connection_limit io = getconnection(T, host, port; kw...) c = Connection{T}(host, port, pipeline_limit, io) push!(pool, c) ;@debug 1 "🔗 New: $c" @@ -474,6 +474,35 @@ function getconnection(::Type{Transaction{T}}, end +function getconnection(::Type{TCPSocket}, + host::AbstractString, + port::AbstractString; + kw...)::TCPSocket + + p::UInt = isempty(port) ? UInt(80) : parse(UInt, port) + @debug 2 "TCP connect: $host:$p..." + connect(getaddrinfo(host), p) +end + + +function getconnection(::Type{SSLContext}, + host::AbstractString, + port::AbstractString; + require_ssl_verification::Bool=false, + sslconfig::SSLConfig=SSLConfig(require_ssl_verification), + kw...)::SSLContext + + port = isempty(port) ? "443" : port + @debug 2 "SSL connect: $host:$port..." + io = SSLContext() + setup!(io, sslconfig) + associate!(io, getconnection(TCPSocket, host, port)) + hostname!(io, host) + handshake!(io) + return io +end + + function Base.show(io::IO, c::Connection) nwaiting = nb_available(tcpsocket(c.io)) print( diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 5ef139181..d2e7def76 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -25,20 +25,13 @@ abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end export ConnectionPoolLayer function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; - connectionpool::Bool=true, socket_type::Type=TCPSocket, - kw...) where Next + socket_type::Type=TCPSocket, kw...) where Next - SocketType = sockettype(uri, socket_type) - if connectionpool - SocketType = ConnectionPool.Transaction{SocketType} - end - io = getconnection(SocketType, uri.host, uri.port; kw...) + IOType = ConnectionPool.Transaction{sockettype(uri, socket_type)} + io = getconnection(IOType, uri.host, uri.port; kw...) try r = request(Next, io, req, body; kw...) - if !connectionpool - close(io) - end return r catch e @debug 1 "❗️ ConnectionLayer $e. Closing: $io" diff --git a/src/HTTP.jl b/src/HTTP.jl index 884e96a7f..b46581622 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -5,7 +5,7 @@ using MbedTLS import MbedTLS.SSLContext -const DEBUG_LEVEL = 1 +const DEBUG_LEVEL = 0 const minimal = false include("compat.jl") @@ -24,12 +24,10 @@ include("multipart.jl") end include("parser.jl") ;import .Parsers: Parser, Headers, Header, ParsingError, ByteView -include("Connect.jl") include("ConnectionPool.jl") include("Messages.jl") ;using .Messages import .Messages: header, hasheader include("Streams.jl") ;using .Streams -include("WebSockets.jl") ;using .WebSockets """ @@ -76,7 +74,7 @@ HTTP.request("GET", "http://httpbin.org/ip"; retries=4, cookies=true) HTTP.get("http://s3.us-east-1.amazonaws.com/"; aws_authorization=true) -conf = (timeout = 10, +conf = (readtimeout = 10, pipeline_limit = 4, retry = false, redirect = false) @@ -95,17 +93,17 @@ Streaming options Connection Pool options - - `connectionpool = true`, enable the `ConnectionPool`. - - `duplicate_limit = 7`, number of duplicate connections to each host:port. - - `pipeline_limit = 16`, number of simultaneous requests per connection. - - `reuse_limit = nolimit`, each connection is closed after this many requests. + - `connection_limit = 8`, number of concurrent connections to each host:port. + - `pipeline_limit = 16`, number of concurrent requests per connection. + - `reuse_limit = nolimit`, number of times a connection is reused after the + first request. - `socket_type = TCPSocket` Timeout options - - `timeout = 60`, close the connection if no data is recieved for this many - seconds. Use `timeout = 0` to disable. + - `readtimeout = 60`, close the connection if no data is recieved for this many + seconds. Use `readtimeout = 0` to disable. Retry options @@ -287,7 +285,9 @@ end request(method::String, uri::URI, headers::Headers, body; kw...)::Response = request(HTTP.stack(;kw...), method, uri, headers, body; kw...) -request(method, uri, headers=[], body=UInt8[]; kw...)::Response = +const nobody = UInt8[] + +request(method, uri, headers=Header[], body=nobody; kw...)::Response = request(string(method), URI(uri), mkheaders(headers), body; kw...) @@ -314,7 +314,7 @@ end ``` """ -open(f::Function, method::String, uri, headers=[]; kw...)::Response = +open(f::Function, method::String, uri, headers=Header[]; kw...)::Response = request(method, uri, headers, nothing; iofunction=f, kw...) @@ -325,27 +325,27 @@ open(f::Function, method::String, uri, headers=[]; kw...)::Response = Shorthand for `HTTP.request("GET", ...)`. See [`HTTP.request`](@ref). """ - get(a...; kw...) = request("GET", a..., kw...) + """ HTTP.put(url, headers, body; ) -> HTTP.Response Shorthand for `HTTP.request("PUT", ...)`. See [`HTTP.request`](@ref). """ - put(a...; kw...) = request("PUT", a..., kw...) + """ HTTP.post(url, headers, body; ) -> HTTP.Response Shorthand for `HTTP.request("POST", ...)`. See [`HTTP.request`](@ref). """ - post(a...; kw...) = request("POST", a..., kw...) + """ HTTP.head(url; ) -> HTTP.Response @@ -442,52 +442,14 @@ The minimal request execution stack is: stack = MessageLayer{ConnectionPoolLayer{StreamLayer}} ``` -The figure below illustrates a minimal Layer-stack with the -`connectionpool=false` option that causes the `ConnectionPoolLayer` to call -HTTP.Connect.getconnection() directly rather reusing pooled connections. - -``` - ┌────────────────────────────────────────────────────────────────────────────┐ - │ ┌───────────────────┐ │ - │ request(method, uri, headers, body) -> │ HTTP.Response │ │ - │ ────────────────────────── └─────────▲─────────┘ │ - │ ║ ║ │ - │ ┌────────────────────────────────────────────────────────────┐ │ - │ │ request(MessageLayer, method, ::URI, ::Headers, body) │ │ - │ ├────────────────────────────────────────────────────────────┤ │ -┌┼───┤ request(ConnectionPoolLayer, ::URI, ::Request, body) │ │ -││ ├────────────────────────────────────────────────────────────┤ │ -││ │ request(StreamLayer, ::IO, ::Request, body) │ │ -││ └──────────────┬───────────────────┬─────────────────────────┘ │ -│└──────────────────┼────────║──────────┼───────────────║─────────────────────┘ -│ │ ║ │ ║ -│┌──────────────────▼───────────────┐ │ ┌──────────────────────────────────┐ -││ HTTP.Request │ │ │ HTTP.Response │ -│└──────────────────▲───────────────┘ │ └───────────────▲──────────────────┘ -│┌──────────────────┴────────║──────────▼───────────────║──┴──────────────────┐ -││ HTTP.Stream <:IO ║ ╔══════╗ ║ │ -│└───────────────────────────║───────────║──────║───────║──┬──────────────────┘ -│┌──────────────────────────────────┐ ║ ┌────▼───────║──▼──────────────────┐ -││ HTTP.Messages │ ║ │ HTTP.Parser │ -│└──────────────────────────────────┘ ║ └──────────────────────────────────┘ -│┌───────────────────────────║───────────║────────────────────────────────────┐ -└▶ HTTP.Connect ║ ║ │ - └───────────────────────────║───────────║────────────────────────────────────┘ - ║ ║ - ┌───────────────────────────║───────────║──────────────┐ ┏━━━━━━━━━━━━━━━━━━┓ - │ HTTP Server ▼ ║ │ ┃ data flow: ════▶ ┃ - │ Request Response │ ┃ reference: ────▶ ┃ - └──────────────────────────────────────────────────────┘ ┗━━━━━━━━━━━━━━━━━━┛ -``` - -The next figure illustrates the full Layer-stack and its relationship with -[`HTTP.Response`](@ref), [`HTTP.Parser`](@ref), +The figure below illustrates the full request exection stack and its +relationship with [`HTTP.Response`](@ref), [`HTTP.Parser`](@ref), [`HTTP.Stream`](@ref) and the [`HTTP.ConnectionPool`](@ref). ``` ┌────────────────────────────────────────────────────────────────────────────┐ │ ┌───────────────────┐ │ - │ HTTP.jl Request Stack │ HTTP.ParsingError ├ ─ ─ ─ ─ ┐ │ + │ HTTP.jl Request Execution Stack │ HTTP.ParsingError ├ ─ ─ ─ ─ ┐ │ │ └───────────────────┘ │ │ ┌───────────────────┐ │ │ │ │ HTTP.IOError ├ ─ ─ ─ │ @@ -552,17 +514,15 @@ The next figure illustrates the full Layer-stack and its relationship with └▶ HTTP.ConnectionPool ║ │ ║ │ │ ┌──────────────▼────────┐ ┌───────────────────────┐ │ │ getconnection() -> │ HTTP.Transaction <:IO │ │ HTTP.Transaction <:IO │ │ - │ │ └───────────────────────┘ └───────────────────────┘ │ - │ │ ║ ╲│╱ ║ ╲│╱ │ - │ │ ║ │ ║ │ │ - │ │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ - │ │ pool: [│ HTTP.Connection │,│ HTTP.Connection │...]│ - │ │ └───────────┬───────────┘ └───────────┬───────────┘ │ - └───────┼───────────────────║─────┼─────║───────────────────┼────────────────┘ - ┌───────▼───────────────────║─────┼─────║───────────────────┼────────────────┐ - │ HTTP.Connect ║ │ ║ │ │ + │ └───────────────────────┘ └───────────────────────┘ │ + │ ║ ╲│╱ ║ ╲│╱ │ + │ ║ │ ║ │ │ │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ - │ getconnection() -> │ Base.TCPSocket <:IO │ │MbedTLS.SSLContext <:IO│ │ + │ pool: [│ HTTP.Connection │,│ HTTP.Connection │...]│ + │ └───────────┬───────────┘ └───────────┬───────────┘ │ + │ ║ │ ║ │ │ + │ ┌───────────▼───────────┐ ┌───────────▼───────────┐ │ + │ │ Base.TCPSocket <:IO │ │MbedTLS.SSLContext <:IO│ │ │ └───────────────────────┘ └───────────┬───────────┘ │ │ ║ ║ │ │ │ ║ ║ ┌───────────▼───────────┐ │ @@ -585,7 +545,7 @@ function stack(;redirect=true, canonicalize_headers=false, retry=true, status_exception=true, - timeout=0, + readtimeout=0, kw...) if minimal MessageLayer{ExceptionLayer{ConnectionPoolLayer{StreamLayer}}} @@ -601,7 +561,7 @@ function stack(;redirect=true, (retry ? RetryLayer : NoLayer){ (status_exception ? ExceptionLayer : NoLayer){ ConnectionPoolLayer{ - (timeout > 0 ? TimeoutLayer : NoLayer){ + (readtimeout > 0 ? TimeoutLayer : NoLayer){ StreamLayer }}}}}}}}}} end @@ -609,12 +569,12 @@ end if !minimal +include("WebSockets.jl") ;using .WebSockets include("client.jl") include("sniff.jl") include("handlers.jl"); using .Handlers include("server.jl"); using .Nitrogen -include("precompile.jl") end - +include("precompile.jl") end # module diff --git a/src/IOExtras.jl b/src/IOExtras.jl index c22720757..74aa348d0 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -44,39 +44,6 @@ Base.show(io::IO, e::IOError) = show(io, e.e) Push bytes back into a connection (to be returned by the next read). """ -function unread!(io::IOBuffer, bytes) - l = length(bytes) - if l == 0 - return - end - - @assert bytes == io.data[io.ptr - l:io.ptr-1] - - if io.seekable - seek(io, io.ptr - (l + 1)) - return - end - - println("WARNING: Can't unread! non-seekable IOBuffer") - println(" Discarding $(length(bytes)) bytes!") - @assert false - return -end - -function unread!(io::BufferStream, bytes) - if length(bytes) == 0 - return - end - if nb_available(io) > 0 - buf = readavailable(io) - write(io, bytes) - write(io, buf) - else - write(io, bytes) - end - return -end - function unread!(io, bytes) if length(bytes) == 0 return diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 565c92fd0..e41d466a5 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -6,7 +6,10 @@ import ..Layer, ..request using ..URIs using ..Messages using ..Headers +using ..minimal +if !minimal using ..Form +end """ @@ -49,7 +52,9 @@ const unknownlength = -1 bodylength(body) = unknownlength 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) diff --git a/src/Streams.jl b/src/Streams.jl index 5481e48c0..c1b07d513 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -2,6 +2,7 @@ module Streams export Stream, closebody, isaborted +import ..HTTP using ..IOExtras using ..Parsers using ..Messages @@ -27,13 +28,23 @@ Creates a `HTTP.Stream` that wraps an existing `IO` stream. - `startwrite(::Stream)` sends the `Request` headers to the `IO` stream. - `write(::Stream, body)` sends the `body` (or a chunk of the bocdy). - `closewrite(::Stream)` sends the final `0` chunk (if needed) and calls - `closewrite` on the `IO` stream. - - - `startread(::Stream)` parses the `Response` headers from the `IO` stream. + `closewrite` on the `IO` stream. When the `IO` stream is a + [`HTTP.ConnectionPool.Transaction`](@ref), calling `closewrite` releases + the [`HTTP.ConnectionPool.Connection`](@ref) back into the pool for use by the + next pipelined request. + + - `startread(::Stream)` calls `startread` on the `IO` stream then + reads and parses the `Response` headers. When the `IO` stream is a + [`HTTP.ConnectionPool.Transaction`](@ref), calling `startread` waits for other + pipelined responses to be read from the [`HTTP.ConnectionPool.Connection`](@ref). - `eof(::Stream)` and `readavailable(::Stream)` parse the body from the `IO` stream. - `closeread(::Stream)` reads the trailers and calls `closeread` on the `IO` - stream. + stream. When the `IO` stream is a [`HTTP.ConnectionPool.Transaction`](@ref), + calling `closeread` releases the readlock and allows the next pipelined + response to be read by another `Stream` that is waiting in `startread`. + If the `Parser` has not recieved a complete response, `closeread` throws + an `EOFError`. """ function Stream(io::IO, request::Request, parser::Parser) @@ -94,7 +105,6 @@ end IOExtras.isreadable(http::Stream) = isreadable(http.stream) function IOExtras.startread(http::Stream) - @require !isreadable(http.stream) startread(http.stream) configure_parser(http) h = readheaders(http.stream, http.parser, http.message) @@ -188,7 +198,7 @@ end function IOExtras.closeread(http::Stream{Response}) - # Discard unread body bytes... + # Discard body bytes that were not read... while !eof(http) readavailable(http) end diff --git a/src/TimeoutRequest.jl b/src/TimeoutRequest.jl index 2c29bf779..7dce5eabd 100644 --- a/src/TimeoutRequest.jl +++ b/src/TimeoutRequest.jl @@ -15,14 +15,14 @@ abstract type TimeoutLayer{Next <: Layer} <: Layer end export TimeoutLayer function request(::Type{TimeoutLayer{Next}}, io::IO, req, body; - timeout::Int=60, kw...) where Next + readtimeout::Int=60, kw...) where Next wait_for_timeout = Ref{Bool}(true) @async while wait_for_timeout[] - if isreadable(io) && inactiveseconds(io) > timeout + if isreadable(io) && inactiveseconds(io) > readtimeout close(io) - @debug 1 "💥 Read inactive > $(timeout)s: $io" + @debug 1 "💥 Read inactive > $(readtimeout)s: $io" break end sleep(8 + rand() * 4) diff --git a/src/client.jl b/src/client.jl index c6d24ec6d..d94799c73 100644 --- a/src/client.jl +++ b/src/client.jl @@ -55,19 +55,13 @@ function request(client::Client, method, uri::URI; if getarg(args, :chunksize, nothing) != nothing Base.depwarn( "The chunksize= option is deprecated and has no effect.\n" * - "Use a BufferStream and pass chunks of the desired size to `write`:\n" * - " io=BufferStream()\n" * - " request(\"PUT\", \"http://foo.bar/file\", body=io)\n" * - " write(io, \"chunk1\")\n" * - " write(io, \"chunk2\")\n", + "Use a HTTP.open and pass chunks of the desired size to `write`.", :chunksize) end if getarg(args, :connecttimeout, Inf) != Inf || - getarg(args, :readtimeout, Inf) != Inf Base.depwarn( - "The connecttimeout= and readtimeout= options are deprecated " * - "and have no effect.\n" * + "The connecttimeout= is deprecated and has no effect.\n" * "See https://github.com/JuliaWeb/HTTP.jl/issues/114\n", :connecttimeout) end @@ -96,9 +90,9 @@ function request(client::Client, method, uri::URI; if getarg(args, :statusraise, nothing) != nothing Base.depwarn( - "The statusraise= options is deprecated. Use statusexception=::Bool", + "The statusraise= options is deprecated. Use status_exception=::Bool", :statusraise) - setkv(newargs, :statusexception, getarg(args, :statusraise)) + setkv(newargs, :status_exception, getarg(args, :statusraise)) end if getarg(args, :insecure, nothing) != nothing diff --git a/src/precompile.jl b/src/precompile.jl index 9e530be58..eb87a25d4 100644 --- a/src/precompile.jl +++ b/src/precompile.jl @@ -1,5 +1,9 @@ function _precompile_() ccall(:jl_generating_output, Cint, ()) == 1 || return nothing + + println("Precompiling...") +# @assert precompile(HTTP.request, (String, String,)) +# @assert precompile(HTTP.request, (String, URI, Headers, Vector{UInt8},)) #= @assert precompile(HTTP.URIs.parseurlchar, (UInt8, Char, Bool,)) @assert precompile(HTTP.status, (HTTP.Response,)) diff --git a/src/uri.jl b/src/uri.jl index c1263a23d..825539369 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -45,6 +45,8 @@ struct URI userinfo::SubString end +URI(uri::URI) = uri + function URI(;host::AbstractString="", path::AbstractString="", scheme::AbstractString="", userinfo::AbstractString="", port::Union{Integer,AbstractString}="", query="", @@ -95,7 +97,7 @@ Base.parse(::Type{URI}, str::AbstractString; isconnect::Bool=false) = a.fragment == b.fragment && a.userinfo == b.userinfo -function resource(uri::URI) +@inline function resource(uri::URI) string(uri.path, isempty(uri.query) ? "" : "?$(uri.query)", isempty(uri.fragment) ? "" : "#$(uri.fragment)") diff --git a/test/async.jl b/test/async.jl index c57569ae0..dbede8f40 100644 --- a/test/async.jl +++ b/test/async.jl @@ -67,7 +67,7 @@ ch = 100 conf = [:reuse_limit => 90, :verbose => 0, :pipeline_limit => pipe, - :duplicate_limit => dup, + :connection_limit => dup + 1, :timeout => 120] @sync for i = 1:count diff --git a/test/loopback.jl b/test/loopback.jl index a990c0f75..443ec7421 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -141,17 +141,17 @@ end HTTP.IOExtras.tcpsocket(::Loopback) = TCPSocket() -function HTTP.Connect.getconnection(::Type{Loopback}, - host::AbstractString, - port::AbstractString; - kw...)::Loopback +function HTTP.ConnectionPool.getconnection(::Type{Loopback}, + host::AbstractString, + port::AbstractString; + kw...)::Loopback return Loopback() end config = [ :socket_type => Loopback, :retry => false, - :duplicate_limit => 0 + :connection_limit => 1 ] lbreq(req, headers, body; method="GET", kw...) = diff --git a/test/parser.jl b/test/parser.jl index fee1404a5..e600489c1 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -20,6 +20,40 @@ const Headers = Vector{Pair{String,String}} (a.body == b.body) +function HTTP.IOExtras.unread!(io::IOBuffer, bytes) + l = length(bytes) + if l == 0 + return + end + + @assert bytes == io.data[io.ptr - l:io.ptr-1] + + if io.seekable + seek(io, io.ptr - (l + 1)) + return + end + + println("WARNING: Can't unread! non-seekable IOBuffer") + println(" Discarding $(length(bytes)) bytes!") + @assert false + return +end + + +function HTTP.IOExtras.unread!(io::BufferStream, bytes) + if length(bytes) == 0 + return + end + if nb_available(io) > 0 + buf = readavailable(io) + write(io, bytes) + write(io, buf) + else + write(io, bytes) + end + return +end + function parse!(parser::Parser, message::Messages.Message, body, bytes)::Int l = length(bytes) From 128a33bc3ef8cc64ece3c5cb5d57f2d1a812b657 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 8 Jan 2018 21:07:59 +1100 Subject: [PATCH 123/182] doc updates --- docs/src/index.md | 23 ++--------------------- docs/src/layers.monopic | Bin 8960 -> 8713 bytes 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 2b53b77bd..99fc9a762 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -138,17 +138,6 @@ HTTP.Streams.Stream ## Connections -### Basic Connections - -*Source: `Connect.jl`* - -```@docs -HTTP.Connect -``` - - -### Pooled Connections - *Source: `ConnectionPool.jl`* ```@docs @@ -219,21 +208,13 @@ HTTP.Streams.isaborted ``` -## Connections Interface - -### Low Level Connect Interface - -```@docs -HTTP.Connect.getconnection(::Type{TCPSocket},::AbstractString,::AbstractString) -``` - -### Connection Pooling Interface +## Connection Pooling Interface ```@docs HTTP.ConnectionPool.Connection HTTP.ConnectionPool.Transaction HTTP.ConnectionPool.pool -HTTP.Connect.getconnection(::Type{HTTP.ConnectionPool.Transaction{T}},::AbstractString,::AbstractString) where T <: IO +HTTP.ConnectionPool.getconnection HTTP.IOExtras.unread!(::HTTP.ConnectionPool.Transaction,::SubArray{UInt8,1,Array{UInt8,1},Tuple{UnitRange{Int64}},true}) HTTP.IOExtras.startwrite(::HTTP.ConnectionPool.Transaction) HTTP.IOExtras.closewrite(::HTTP.ConnectionPool.Transaction) diff --git a/docs/src/layers.monopic b/docs/src/layers.monopic index 9d00a76e364e140add7680f893b98d592c3b2d59..aa13010c9e83a1b6887f73ad29fc45d43f12b25e 100644 GIT binary patch literal 8713 zcmZvgWmFu%lD2V|5C|^80u1i%0Rq8-ySuv%?l8b4xI=JaKcIP*PM9!&ATy|_rhX9l-GZ&*Y)ZAe&51}m7>X@ zBV$se!;j9fEgR2=z;SIuFNw~}4VCdi;bL^dTlIBnZ`Zy}$b~sqM6!<8aeJPmw3b+1 z)XDjIBWw((rIvAB7YNh`Hu2R@)&7Qkdy&OTz9_xj{kr%ry*el4(WEy@`#R&HzmdR~ z_2_g%f%Ue3p$<#xHclE$tH zR)h6Z`Jm`?gHTyhf@r_5goBNUaD_BO&VBu9&}rUj+O*QOJ#RvP>r=rW_NRBOD}zm0 zA66z$iq9PO^pYb+p}AkBJtJ6Hw_nD0eRM>!P8tV~l`22;oYy>20;(tHZ}&Zxnht>~ z*?bXqch1->BBB87oMv?eZ-~?7u8^9NKueoQ{~bUC*#pDBPhu)j;@sh)*p@CUU~U-~ zKw;Mmu${CW-I+c+6;8!|G_LZ`&~G2;(qT!?8DYR8!G;C(mSiLTCVKY0+CtZ#&?Rv+ zIx8d55@h!M=&QBfa8ZT;92^T`fRpFMe_&UCId4Dq3t0vwm@6pcgzhJr*O7P0{8p;K;e zo=}^JJo5Oyr6p*qF0yU6R_j$7@^_Q}%e!OGDF zT~5)>72>O}sD4Mr8tCmJ;E$(m0{(PGdi@T@z2h|1Zre2#U}+YD42kfqu^PY9s*m)@ zl`3y1u)USER={g1txiOY=+FG%vNMeczcWbCb$q#!u_2R^<{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01-8g%DGedx*}PCnRHGo)*p6@ zN08slZ}aAxaT25LKY}XW5N_Q&ws*9Dxjk`h*FT+3!RHS$V;(7pfg_*FzM1n+Z7Mr% zHN08%+s`6kDOU)m?sx3EuvWy%RwAdlOfp0yxie#Nt*XP|;|$3oajrrJ{gq&1Y__OK zo6Jj*L1?eumo6V!R`ZSWbWvL}z%TtIsGs13C2)i#-U~}`3Y!B|N-kBGNd(sl3-XgeGKJ9vy~c?^oMT(p)i~tqEe1)U)aw&17VT_O~05Veg&n zXe=G^piyy?_!iO>P&7Td{x(d%$De7?4-)hP0UVsPs$-WvO}FE%uS{gpdW~1Mh~`th z!@@lt1Mazo2?ry#oO=ZGh1_?zOGlHmFvt4VF@-#5XqGp@JPdOkm)g1=G?V#W*gGjN z_kbTA96g%YM%%hC@;^Bb#)2h}Q&;X~Vpy(i_ra0hk;N)*16^MO@Dn5S3 zehpQ75-9_WNeuO95B8%YbmR~I+jC+v4Vk2OQ6g9-(#3B5w0gk?iT=Ggrg%Q4_%^0E71n6! zbUGJ2COsCqeU+6ADc2yL(g7hwbBd=z#ppLmowzgV&yNc`Up7U0{`*vr8+rS5r7F_H zl=5juhb;d(>tetF8rn3ZS4AgjBw2sM13Z6c<{Wj_Wcpjg=pOit>kBwL&GGuS-Ng(b z5h60nE#8ofiPg80Q%99m6z0Ui#@NM0a{cf_T)jMl+XPp8m>QR49c9DgV%S0_&LMJ$ zcZnIgWfu`XUzGFsf+CrbI$QNw#P4}_k+!WgLjV8( From c1d2b357e60621b2ce604a24f99f9401741d9222 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 8 Jan 2018 21:37:10 +1100 Subject: [PATCH 124/182] typo --- src/AWS4AuthRequest.jl | 3 --- src/HTTP.jl | 4 ++++ src/client.jl | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index 715fe34ec..a5cc86e0b 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -1,9 +1,6 @@ module AWS4AuthRequest -if VERSION > v"0.7.0-DEV.2338" using Base64 -end - using Dates using Unicode using MbedTLS: digest, MD_SHA256, MD_MD5 diff --git a/src/HTTP.jl b/src/HTTP.jl index b46581622..455da0510 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -415,7 +415,9 @@ 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 @@ -569,7 +571,9 @@ 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 diff --git a/src/client.jl b/src/client.jl index d94799c73..aa3ed6d97 100644 --- a/src/client.jl +++ b/src/client.jl @@ -59,7 +59,7 @@ function request(client::Client, method, uri::URI; :chunksize) end - if getarg(args, :connecttimeout, Inf) != Inf || + if getarg(args, :connecttimeout, Inf) != Inf Base.depwarn( "The connecttimeout= is deprecated and has no effect.\n" * "See https://github.com/JuliaWeb/HTTP.jl/issues/114\n", From 422c8f5234132c6d24f553f03a13c34859da6dc9 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 8 Jan 2018 23:23:17 +1100 Subject: [PATCH 125/182] Fix show(::IOError). Close response_stream after writing in StreamRequest.jl. --- src/ConnectionRequest.jl | 3 +-- src/HTTP.jl | 2 ++ src/IOExtras.jl | 4 ++-- src/StreamRequest.jl | 1 + test/async.jl | 24 +++++++++++++----------- test/client.jl | 1 - test/messages.jl | 3 --- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index d2e7def76..7034561c3 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -31,8 +31,7 @@ function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; io = getconnection(IOType, uri.host, uri.port; kw...) try - r = request(Next, io, req, body; kw...) - return r + return request(Next, io, req, body; kw...) catch e @debug 1 "❗️ ConnectionLayer $e. Closing: $io" close(io) diff --git a/src/HTTP.jl b/src/HTTP.jl index 455da0510..83478be38 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -425,7 +425,9 @@ include("TimeoutRequest.jl"); using .TimeoutRequest 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 diff --git a/src/IOExtras.jl b/src/IOExtras.jl index 74aa348d0..9da85841c 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -31,11 +31,11 @@ Fields: - `e`, the error. """ -struct IOError +struct IOError <: Exception e end -Base.show(io::IO, e::IOError) = show(io, e.e) +Base.show(io::IO, e::IOError) = print(io, "IOError(", e.e, ")") """ diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 4865c393b..bbec68371 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -118,6 +118,7 @@ function readbody(http::Stream, res::Response, response_stream) else res.body = body_was_streamed write(response_stream, http) + close(response_stream) end end diff --git a/test/async.jl b/test/async.jl index dbede8f40..9fac43399 100644 --- a/test/async.jl +++ b/test/async.jl @@ -1,3 +1,4 @@ +using Test using HTTP using JSON using MbedTLS: digest, MD_MD5, MD_SHA256 @@ -19,7 +20,7 @@ end s3region = "ap-southeast-2" s3url = "https://s3.$s3region.amazonaws.com" s3(method, path, body=UInt8[]; kw...) = - request(method, "$s3url/$path", [], body; awsauthorization=true, kw...) + request(method, "$s3url/$path", [], body; aws_authorization=true, kw...) s3get(path; kw...) = s3("GET", path; kw...) s3put(path, data; kw...) = s3("PUT", path, data; kw...) @@ -45,7 +46,8 @@ function dump_async_exception(e, st) print(String(take!(buf))) end -if haskey(ENV, "AWS_ACCESS_KEY_ID") +if haskey(ENV, "AWS_ACCESS_KEY_ID") || + 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], @@ -68,7 +70,7 @@ conf = [:reuse_limit => 90, :verbose => 0, :pipeline_limit => pipe, :connection_limit => dup + 1, - :timeout => 120] + :readtimeout => 120] @sync for i = 1:count data = rand(UInt8(65):UInt8(75), sz) @@ -81,7 +83,7 @@ conf = [:reuse_limit => 90, r = HTTP.open("PUT", url, ["Content-Length" => sz]; body_sha256=digest(MD_SHA256, data), body_md5=digest(MD_MD5, data), - awsauthorization=true, + aws_authorization=true, conf...) do http for n = 1:ch:sz write(http, data[n:n+(ch-1)]) @@ -91,7 +93,7 @@ conf = [:reuse_limit => 90, end if mode == :request r = HTTP.request("PUT", url, [], data; - awsauthorization=true, conf...) + aws_authorization=true, conf...) end #println("S3 put file$i") @assert strip(HTTP.header(r, "ETag"), '"') == md5 @@ -107,25 +109,26 @@ get_data_sums = Dict() for i = 1:count @async try url = "$s3url/http.jl.test/file$i" - buf = IOBuffer() + buf = BufferStream() r = nothing if mode == :open r = HTTP.open("GET", url, ["Content-Length" => 0]; - awsauthorization=true, + aws_authorization=true, conf...) do http - truncate(buf, 0) # in case of retry! + buf = BufferStream() # in case of retry! while !eof(http) write(buf, readavailable(http)) sleep(rand(1:10)/1000) end + close(buf) end end if mode == :request r = HTTP.request("GET", url; response_stream=buf, - awsauthorization=true, conf...) + aws_authorization=true, conf...) end #println("S3 get file$i") - bytes = take!(buf) + bytes = read(buf) md5 = bytes2hex(digest(MD_MD5, bytes)) get_data_sums[i] = (md5, strip(HTTP.header(r, "ETag"), '"')) catch e @@ -247,7 +250,6 @@ println("running async $count, 1:$num, $config, $http C") r = HTTP.request( "GET", url; response_stream=s, config...) @assert r.status == 200 - close(s) str = String(read(s)) break catch e diff --git a/test/client.jl b/test/client.jl index b54edfe4a..0c3bc28d8 100644 --- a/test/client.jl +++ b/test/client.jl @@ -57,7 +57,6 @@ for sch in ("http", "https") begin io = BufferStream() r = HTTP.get("$sch://httpbin.org/stream/100"; response_stream=io) - close(io) @test status(r) == 200 b = [JSON.parse(l) for l in eachline(io)] diff --git a/test/messages.jl b/test/messages.jl index 2ec019ed5..0f6ba66f2 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -131,7 +131,6 @@ using JSON io = BufferStream() r = request(m, uri, response_stream=io) - close(io) @test r.status == 200 @test read(io) == body end @@ -143,7 +142,6 @@ using JSON uri = "$sch://httpbin.org/$(lowercase(m))" io = BufferStream() r = request(m, uri, response_stream=io) - close(io) @test r.status == 200 end @@ -161,7 +159,6 @@ using JSON io = open("result_file", "w") r = request("GET", "http://httpbin.org/stream/$n", response_stream=io) - close(io) @show filesize("result_file") i = 0 for l in readlines("result_file") From 25e2af999b072c3729cf62d25061e7e6b82b8d22 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 10 Jan 2018 16:01:21 +1100 Subject: [PATCH 126/182] handlers tweaks for compatibility with HTTP.Request --- src/handlers.jl | 6 +++--- test/handlers.jl | 34 ++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/handlers.jl b/src/handlers.jl index cf32e47d3..facf8c36b 100644 --- a/src/handlers.jl +++ b/src/handlers.jl @@ -48,7 +48,7 @@ end handle(h::HandlerFunction, req, resp) = h.func(req, resp) "A default 404 Handler" -const FourOhFour = HandlerFunction((req, resp) -> Response(404)) +const FourOhFour = HandlerFunction((req, resp) -> HTTP.Response(404)) """ Router(h::Handler) @@ -142,9 +142,9 @@ end function handle(r::Router, req, resp) # get the url/path of the request - m = val(Symbol(HTTP.method(req))) - uri = HTTP.uri(req) + 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)) p = uri.path diff --git a/test/handlers.jl b/test/handlers.jl index 952825a9c..761dba8b5 100644 --- a/test/handlers.jl +++ b/test/handlers.jl @@ -1,3 +1,13 @@ +using Test +using HTTP + +import Base.== + +==(a::HTTP.Response,b::HTTP.Response) = (a.status == b.status) && + (a.version == b.version) && + (a.headers == b.headers) && + (a.body == b.body) + @testset "HTTP.Handler" begin f = HTTP.HandlerFunction((req, resp) -> HTTP.Response(200)) @@ -10,7 +20,7 @@ r = HTTP.Router() HTTP.register!(r, "/path/to/greatness", f) @test length(methods(r.func)) == 2 req = HTTP.Request() -req.uri = HTTP.URI("/path/to/greatness") +req.uri = "/path/to/greatness" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(200) p = "/next/path/to/greatness" @@ -18,24 +28,24 @@ f2 = HTTP.HandlerFunction((req, resp) -> HTTP.Response(201)) HTTP.register!(r, p, f2) @test length(methods(r.func)) == 3 req = HTTP.Request() -req.uri = HTTP.URI("/next/path/to/greatness") +req.uri = "/next/path/to/greatness" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(201) r = HTTP.Router() HTTP.register!(r, "GET", "/sget", f) HTTP.register!(r, "POST", "/spost", f) HTTP.register!(r, HTTP.POST, "/tpost", f) -req = HTTP.Request(HTTP.GET, "/sget") +req = HTTP.Request("GET", "/sget") @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(200) -req = HTTP.Request(HTTP.POST, "/sget") +req = HTTP.Request("POST", "/sget") @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(404) -req = HTTP.Request(HTTP.GET, "/spost") +req = HTTP.Request("GET", "/spost") @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(404) -req = HTTP.Request(HTTP.POST, "/spost") +req = HTTP.Request("POST", "/spost") @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(200) -req = HTTP.Request(HTTP.GET, "/tpost") +req = HTTP.Request("GET", "/tpost") @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(404) -req = HTTP.Request(HTTP.POST, "/tpost") +req = HTTP.Request("POST", "/tpost") @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(200) r = HTTP.Router() @@ -47,16 +57,16 @@ f4 = HTTP.HandlerFunction((req, resp) -> HTTP.Response(203)) HTTP.register!(r, "/test/*/ghotra/seven", f4) req = HTTP.Request() -req.uri = HTTP.URI("/test") +req.uri = "/test" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(200) -req.uri = HTTP.URI("/test/sarv") +req.uri = "/test/sarv" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(201) -req.uri = HTTP.URI("/test/sarv/ghotra") +req.uri = "/test/sarv/ghotra" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(202) -req.uri = HTTP.URI("/test/sar/ghotra/seven") +req.uri = "/test/sar/ghotra/seven" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(203) end From eabeab79306237877eb7ae8b6692e27ff27829bf Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 10 Jan 2018 16:02:50 +1100 Subject: [PATCH 127/182] remove redundant connectionclosed(p::Parser), replaced by hasheader(http, "Connection", "close") --- src/parser.jl | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/parser.jl b/src/parser.jl index f93f9760c..e64fa1624 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -31,7 +31,7 @@ export Parser, Header, Headers, ByteView, nobytes, messagestarted, headerscomplete, bodycomplete, messagecomplete, messagehastrailing, waitingforeof, seteof, - connectionclosed, setnobody, + setnobody, ParsingError, ParsingErrorCode using ..URIs.parseurlchar @@ -229,15 +229,6 @@ Is the `Parser` ready to process trailing headers? messagehastrailing(p::Parser) = p.flags & F_TRAILING > 0 -""" - connectionclosed(::Parser) - -Was "Connection: close" parsed? -""" - -connectionclosed(p::Parser) = p.flags & F_CONNECTION_CLOSE > 0 - - isrequest(p::Parser) = p.message.status == 0 From 969f4383a48c8096661dc618108404f872898661 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 10 Jan 2018 16:03:34 +1100 Subject: [PATCH 128/182] added @ensure to debug.jl --- src/debug.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/debug.jl b/src/debug.jl index 7c735a0dc..0c373a37b 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -41,6 +41,24 @@ macro require(precondition, msg = string(precondition)) end +@noinline function postcondition_error(msg, frame) + msg = string(sprint(StackTraces.show_spec_linfo, + StackTraces.lookup(frame)[2]), + " failed to ensure ", msg) + return AssertionError(msg) +end + + + +""" + @ensure postcondition [message] +Throw `ArgumentError` if `postcondition` is false. +""" +macro ensure(postcondition, msg = string(postcondition)) + esc(:(if ! $postcondition throw(postcondition_error($msg, backtrace()[1])) end)) +end + + # FIXME # Should this have a branch-prediction hint? (same for @assert?) # http://llvm.org/docs/BranchWeightMetadata.html#built-in-expect-instructions From dd5b23a247c9d99683cbf3f710f03f53f49bc59f Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 10 Jan 2018 16:05:34 +1100 Subject: [PATCH 129/182] remove unneeded abbeviation of request to req --- src/StreamRequest.jl | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index bbec68371..fa431db29 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -25,16 +25,17 @@ indicates that the server does not wish to receive the message body. abstract type StreamLayer <: Layer end export StreamLayer -function request(::Type{StreamLayer}, io::IO, req::Request, body; +function request(::Type{StreamLayer}, io::IO, request::Request, body; response_stream=nothing, iofunction=nothing, verbose::Int=0, kw...)::Response - verbose == 1 && printlncompact(req) - verbose >= 2 && println(req) + verbose == 1 && printlncompact(request) + verbose >= 2 && println(request) - http = Stream(io, req, ConnectionPool.getparser(io)) + response = request.response + http = Stream(response, ConnectionPool.getparser(io), io) startwrite(http) aborted = false @@ -42,10 +43,10 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; @sync begin if iofunction == nothing - @async writebody(http, req, body) + @async writebody(http, request, body) yield() startread(http) - readbody(http, req.response, response_stream) + readbody(http, response, response_stream) else iofunction(http) end @@ -60,7 +61,7 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; if aborted && e isa CompositeException && (ex = first(e.exceptions).ex; isioerror(ex)) - @debug 1 "⚠️ $(req.response.status) abort exception excpeted: $ex" + @debug 1 "⚠️ $(response.status) abort exception excpeted: $ex" else rethrow(e) end @@ -69,10 +70,10 @@ function request(::Type{StreamLayer}, io::IO, req::Request, body; closewrite(http) closeread(http) - verbose == 1 && printlncompact(req.response) - verbose >= 2 && println(req.response) + verbose == 1 && printlncompact(response) + verbose >= 2 && println(response) - return req.response + return response end From 2c9fee6e8a279cb4b76d6d5169d63ff22c5b8c85 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 10 Jan 2018 16:05:59 +1100 Subject: [PATCH 130/182] added hasheader(::Message, key, value) --- src/Messages.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Messages.jl b/src/Messages.jl index 9c5e75019..94d2cd980 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -225,6 +225,14 @@ Does header value for `key` exist (case-insensitive)? hasheader(m, k::String) = header(m, k) != "" +""" + hasheader(::Message, key, value) -> Bool + +Does header for `key` match `value` (both case-insensitive)? +""" +hasheader(m, k::String, v::String) = lowercase(header(m, k)) == v + + """ setheader(::Message, key => value) From 4345a9c57eeae3534bf51a796f81cdb72fe84c35 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 10 Jan 2018 16:07:59 +1100 Subject: [PATCH 131/182] ConnectionPool and HTTP.Stream tweaks for server mode compatibility. ConnectionPool: - Make startwrite/closewrite locling symetrical with startread/closread. - Use a Condition and busy flag instead of ReentrantLock to esnure consistent behaviour regardless of what task the start/end functions are called from. - Add Connection.sequence for initialisation of new Transactions's seq No. - Base.isopen(t::Transaction) returns false after read and write are closed. MessageRequest.jl - Don't set "chunked" header, this is now done in startwrite(http::Stream) for client and server modes. Streams.jl - add messagetowrite(http::Stream{*}) - Set "chunked" header in startwrite(http::Stream) when needed. - Call startwrite() in first call to Base.unsafe_write() - Add closewrite(http::Stream{Request}) and closeread(http::Stream{Request}) for server mode. --- src/ConnectionPool.jl | 144 ++++++++++++++++++++++-------------------- src/MessageRequest.jl | 4 +- src/Streams.jl | 79 +++++++++++++++++------ 3 files changed, 137 insertions(+), 90 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 1bee3e8ba..9d46f980b 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -25,11 +25,13 @@ reading. module ConnectionPool -export getconnection, getparser, getrawstream, inactiveseconds +export Connection, Transaction, + getconnection, getparser, getrawstream, inactiveseconds using ..IOExtras -import ..@debug, ..DEBUG_LEVEL, ..taskid, ..@require, ..precondition_error +import ..@debug, ..@debugshow, ..DEBUG_LEVEL, ..taskid +import ..@require, ..precondition_error, ..@ensure, ..postcondition_error using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! import ..Parser @@ -45,12 +47,6 @@ byteview(bytes::ByteView) = bytes byteview(bytes)::ByteView = view(bytes, 1:length(bytes)) -function havelock(l) - @assert l.reentrancy_cnt <= 1 - islocked(l) && l.locked_by == current_task() -end - - """ Connection{T <: IO} @@ -61,16 +57,16 @@ Fields: - `port::String`, exactly as specified in the URI (i.e. may be empty). - `pipeline_linit`, number of requests to send before waiting for responses. - `peerport`, remote TCP port number (used for debug messages). -- `localport`, local TCP port number (used for debug messages). +- `localport`, local TCP port number (used for debug messages). - `io::T`, the `TCPSocket` or `SSLContext. - `excess::ByteView`, left over bytes read from the connection after the end of a response message. These bytes are probably the start of the next response message. -- `writebusy`, is a `Transaction` busy writing to this `Connection` ? -- `writecount`, number of Request Messages that have been written. -- `readcount`, number of Response Messages that have been read. -- `writelock`, busy writing a Request to `io`. -- `readlock`, busy reading a Response from `io`. +- `sequence`, number of most recent `Transaction`. +- `writecount`, number of Messages that have been written. +- `writedone`, signal that `writecount` was incremented. +- `readcount`, number of Messages that have been read. +- `readdone`, signal that `readcount` was incremented. - `timestamp, time data was last recieved. - `parser::Parser`, reuse a `Parser` when this `Connection` is reused. """ @@ -83,10 +79,13 @@ mutable struct Connection{T <: IO} localport::UInt16 io::T excess::ByteView - writebusy::Bool + sequence::Int writecount::Int + writebusy::Bool + writedone::Condition readcount::Int - readlock::ReentrantLock + readbusy::Bool + readdone::Condition timestamp::Float64 parser::Parser end @@ -97,7 +96,7 @@ A single pipelined HTTP Request/Response transaction`. Fields: - `c`, the shared [`Connection`](@ref) used for this `Transaction`. - - `sequence::Int`, identifies this `Transaction` among the others that share `c`. + - `sequence::Int`, identifies this `Transaction` among the others that share `c`. """ struct Transaction{T <: IO} <: IO @@ -105,30 +104,38 @@ struct Transaction{T <: IO} <: IO sequence::Int end +Connection{T}(io::T) where T <: IO = + Connection{T}("", "", default_pipeline_limit, io) Connection{T}(host::AbstractString, port::AbstractString, pipeline_limit::Int, io::T) where T <: IO = Connection{T}(host, port, pipeline_limit, - peerport(io), localport(io), io, view(UInt8[], 1:0), - 0, 0, 0, ReentrantLock(), 0, Parser()) - -function Transaction{T}(c::Connection{T}) where T <: IO - r = Transaction{T}(c, c.writecount) - startwrite(r) - return r + peerport(io), localport(io), + io, view(UInt8[], 1:0), + -1, + 0, false, Condition(), + 0, false, Condition(), + 0, Parser()) + +Transaction{T}(c::Connection{T}) where T <: IO = + Transaction{T}(c, (c.sequence += 1)) + +function client_transaction(T, c) + t = Transaction{T}(c) + startwrite(t) + return t end -getparser(t::Transaction) = t.c.parser +getparser(t::Transaction) = t.c.parser getrawstream(t::Transaction) = t.c.io inactiveseconds(t::Transaction) = inactiveseconds(t.c) - function inactiveseconds(c::Connection)::Float64 - if !islocked(c.readlock) + if !c.readbusy || c.writebusy return Float64(0) end return time() - c.timestamp @@ -138,7 +145,11 @@ end Base.unsafe_write(t::Transaction, p::Ptr{UInt8}, n::UInt) = unsafe_write(t.c.io, p, n) -Base.isopen(t::Transaction) = isopen(t.c.io) +Base.isopen(c::Connection) = isopen(c.io) + +Base.isopen(t::Transaction) = isopen(t.c) && + t.c.readcount <= t.sequence && + t.c.writecount <= t.sequence function Base.eof(t::Transaction) @require isreadable(t) || !isopen(t) @@ -152,11 +163,10 @@ Base.nb_available(t::Transaction) = nb_available(t.c) Base.nb_available(c::Connection) = !isempty(c.excess) ? length(c.excess) : nb_available(c.io) -Base.isreadable(t::Transaction) = islocked(t.c.readlock) && - t.c.readcount == t.sequence -Base.iswritable(t::Transaction) = t.c.writebusy && - t.c.writecount == t.sequence +Base.isreadable(t::Transaction) = t.c.readbusy && t.c.readcount == t.sequence + +Base.iswritable(t::Transaction) = t.c.writebusy && t.c.writecount == t.sequence function Base.readavailable(t::Transaction)::ByteView @@ -191,15 +201,17 @@ end """ startwrite(::Transaction) -Set `writebusy`. -Should only be called by the `Transaction` constructor because -`getconnection` only creates new `Transaction`s when a `Connection` is -available for writing. +Wait for prior pending writes to complete. """ function IOExtras.startwrite(t::Transaction) - @require !t.c.writebusy + @require !iswritable(t) ;t.c.writecount != t.sequence && + @debug 1 "⏳ Wait write: $t" + while t.c.writecount != t.sequence + wait(t.c.writedone) + end ;@debug 2 "👁 Start write:$t" t.c.writebusy = true + @ensure iswritable(t) return end @@ -213,11 +225,12 @@ Signal that an entire Request Message has been written to the `Transaction`. function IOExtras.closewrite(t::Transaction) @require iswritable(t) - t.c.writecount += 1 ;@debug 2 "🗣 Write done: $t" t.c.writebusy = false + t.c.writecount += 1 ;@debug 2 "🗣 Write done: $t" + notify(t.c.writedone) notify(poolcondition) - @assert !iswritable(t) + @ensure !iswritable(t) return end @@ -225,20 +238,18 @@ end """ startread(::Transaction) -Wait for prior pending reads to complete, then lock the readlock. +Wait for prior pending reads to complete. """ function IOExtras.startread(t::Transaction) - @require !isreadable(t) - + @require !isreadable(t) ;t.c.readcount != t.sequence && + @debug 1 "⏳ Wait read: $t" t.c.timestamp = time() - lock(t.c.readlock) - while t.c.readcount != t.sequence - unlock(t.c.readlock) - yield() ;@debug 1 "⏳ Waiting to read: $t" - lock(t.c.readlock) + while t.c.readcount != t.sequence + wait(t.c.readdone) end ;@debug 2 "👁 Start read: $t" - @assert isreadable(t) + t.c.readbusy = true + @ensure isreadable(t) return end @@ -248,18 +259,18 @@ end Signal that an entire Response Message has been read from the `Transaction`. -Increment `readcount` and wake up tasks waiting in `startread` by unlocking -`readlock`. +Increment `readcount` and wake up tasks waiting in `startread`. """ function IOExtras.closeread(t::Transaction) @require isreadable(t) + t.c.readbusy = false t.c.readcount += 1 - unlock(t.c.readlock) ;@debug 2 "✉️ Read done: $t" + notify(t.c.readdone) ;@debug 2 "✉️ Read done: $t" notify(poolcondition) - @assert !isreadable(t) + @ensure !isreadable(t) return end @@ -296,7 +307,7 @@ function purge(c::Connection) readavailable(c.io) end c.excess = nobytes - @assert nb_available(c) == 0 + @ensure nb_available(c) == 0 end @@ -372,7 +383,7 @@ function findoverused(T::Type, c.host == host && c.port == port && c.readcount >= reuse_limit && - !islocked(c.readlock) && + !c.readbusy && isopen(c.io)), pool) end @@ -404,7 +415,7 @@ Remove closed connections from `pool`. function purge() while (i = findfirst(x->!isopen(x.io), pool)) > 0 c = pool[i] - deleteat!(pool, i) ;@debug 1 "🗑 Deleted: $c" + deleteat!(pool, i) ;@debug 1 "🗑 Deleted: $c" end end @@ -427,7 +438,6 @@ function getconnection(::Type{Transaction{T}}, while true lock(poollock) - @assert poollock.reentrancy_cnt == 1 try # Close connections that have reached the reuse limit... @@ -442,10 +452,10 @@ function getconnection(::Type{Transaction{T}}, # Try to find a connection with no active readers or writers... writable = findwritable(T, host, port, pipeline_limit, reuse_limit) - idle = filter(c->!islocked(c.readlock), writable) + idle = filter(c->!c.readbusy, writable) if !isempty(idle) - c = rand(idle) ;@debug 2 "♻️ Idle: $c" - return Transaction{T}(c) + c = rand(idle) ;@debug 2 "♻️ Idle: $c" + return client_transaction(T, c) end # If there are not too many connections to this host:port, @@ -454,14 +464,14 @@ function getconnection(::Type{Transaction{T}}, if length(busy) < connection_limit io = getconnection(T, host, port; kw...) c = Connection{T}(host, port, pipeline_limit, io) - push!(pool, c) ;@debug 1 "🔗 New: $c" - return Transaction{T}(c) + push!(pool, c) ;@debug 1 "🔗 New: $c" + return client_transaction(T, c) end # Share a connection that has active readers... if !isempty(writable) - c = rand(writable) ;@debug 2 "⇆ Shared: $c" - return Transaction{T}(c) + c = rand(writable) ;@debug 2 "⇆ Shared: $c" + return client_transaction(T, c) end finally @@ -509,7 +519,7 @@ function Base.show(io::IO, c::Connection) io, tcpstatus(c), " ", lpad(c.writecount,3),"↑", c.writebusy ? "🔒 " : " ", - lpad(c.readcount,3), "↓", islocked(c.readlock) ? "🔒 " : " ", + lpad(c.readcount,3), "↓", c.readbusy ? "🔒 " : " ", c.host, ":", c.port != "" ? c.port : Int(c.peerport), ":", Int(c.localport), ", ≣", c.pipeline_limit, @@ -517,12 +527,10 @@ function Base.show(io::IO, c::Connection) inactiveseconds(c) > 5 ? ", inactive $(round(inactiveseconds(c),1))s" : "", nwaiting > 0 ? ", $nwaiting bytes waiting" : "", - DEBUG_LEVEL > 1 ? ", $(Base._fd(tcpsocket(c.io)))" : "", - DEBUG_LEVEL > 1 && - islocked(c.readlock) ? ", read task: $(taskid(c.readlock))" : "") + DEBUG_LEVEL > 1 ? ", $(Base._fd(tcpsocket(c.io)))" : "") end -Base.show(io::IO, t::Transaction) = print(io, "T$(t.sequence) ", t.c) +Base.show(io::IO, t::Transaction) = print(io, "T$(rpad(t.sequence,2)) ", t.c) function tcpstatus(c::Connection) diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index e41d466a5..112f86044 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -37,8 +37,8 @@ function request(::Type{MessageLayer{Next}}, setheader(headers, "Content-Length" => string(l)) elseif method == "GET" && iofunction isa Function setheader(headers, "Content-Length" => "0") - else - setheader(headers, "Transfer-Encoding" => "chunked") +# FIXME else +# setheader(headers, "Transfer-Encoding" => "chunked") end end diff --git a/src/Streams.jl b/src/Streams.jl index c1b07d513..000822216 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -6,16 +6,16 @@ import ..HTTP using ..IOExtras using ..Parsers using ..Messages -import ..Messages: header, hasheader, writestartline +import ..Messages: header, hasheader, setheader, writeheaders, writestartline import ..ConnectionPool.getrawstream import ..@require, ..precondition_error import ..@debug, ..DEBUG_LEVEL -mutable struct Stream{T <: Message} <: IO - stream::IO - message::T +mutable struct Stream{M <: Message, S <: IO} <: IO + message::M parser::Parser + stream::S writechunked::Bool end @@ -47,28 +47,42 @@ Creates a `HTTP.Stream` that wraps an existing `IO` stream. an `EOFError`. """ -function Stream(io::IO, request::Request, parser::Parser) - @require iswritable(io) - writechunked = header(request, "Transfer-Encoding") == "chunked" - Stream{Response}(io, request.response, parser, writechunked) -end +Stream(r::M, p::Parser, io::S) where {M, S} = Stream{M,S}(r, p, io, false) header(http::Stream, a...) = header(http.message, a...) -hasheader(http::Stream, a) = header(http.message, a) +setheader(http::Stream, a...) = setheader(http.message.response, a...) +hasheader(http::Stream, a...) = hasheader(http.message, a...) getrawstream(http::Stream) = getrawstream(http.stream) # Writing HTTP Messages +messagetowrite(http::Stream{Response}) = http.message.request +messagetowrite(http::Stream{Request}) = http.message.response + IOExtras.iswritable(http::Stream) = iswritable(http.stream) function IOExtras.startwrite(http::Stream) - @require iswritable(http.stream) - writeheaders(http.stream, http.message.request) + if !iswritable(http.stream) + startwrite(http.stream) + end + m = messagetowrite(http) + if !hasheader(m, "Content-Length") && + !hasheader(m, "Transfer-Encoding") && + !hasheader(m, "Upgrade") + http.writechunked = true + setheader(m, "Transfer-Encoding" => "chunked") + else + http.writechunked = hasheader(m, "Transfer-Encoding", "chunked") + end + writeheaders(http.stream, m) end function Base.unsafe_write(http::Stream, p::Ptr{UInt8}, n::UInt) + if !iswritable(http) && isopen(http.stream) + startwrite(http) + end if !http.writechunked return unsafe_write(http.stream, p, n) end @@ -77,6 +91,7 @@ function Base.unsafe_write(http::Stream, p::Ptr{UInt8}, n::UInt) write(http.stream, "\r\n") end + """ closebody(::Stream) @@ -91,7 +106,7 @@ function closebody(http::Stream) end -function IOExtras.closewrite(http::Stream) +function IOExtras.closewrite(http::Stream{Response}) if !iswritable(http) return end @@ -99,6 +114,19 @@ function IOExtras.closewrite(http::Stream) closewrite(http.stream) end +function IOExtras.closewrite(http::Stream{Request}) + @require iswritable(http) + + closebody(http) + closewrite(http.stream) + + if hasheader(http.message, "Connection", "close") + # Close conncetion if client sent "Connection: close"... + @debug 1 "✋ \"Connection: close\": $(http.stream)" + close(http.stream) + end +end + # Reading HTTP Messages @@ -107,17 +135,16 @@ IOExtras.isreadable(http::Stream) = isreadable(http.stream) function IOExtras.startread(http::Stream) startread(http.stream) configure_parser(http) - h = readheaders(http.stream, http.parser, http.message) + message = readheaders(http.stream, http.parser, http.message) if http.message isa Response && http.message.status == 100 # 100 Continue # https://tools.ietf.org/html/rfc7230#section-5.6 # https://tools.ietf.org/html/rfc7231#section-6.2.1 @debug 1 "✅ Continue: $(http.stream)" configure_parser(http) - h = readheaders(http.stream, http.parser, http.message) + message = readheaders(http.stream, http.parser, http.message) end - return h - + return message end @@ -172,7 +199,7 @@ end """ - isaborted(::Stream{Response}) + isaborted(::Stream{Response}) Has the server signalled that it does not wish to receive the message body? @@ -186,7 +213,7 @@ function isaborted(http::Stream{Response}) if iswritable(http.stream) && iserror(http.message) && - connectionclosed(http.parser) + hasheader(http.message, "Connection", "close") @debug 1 "✋ Abort on $(sprint(writestartline, http.message)): " * "$(http.stream)" @debug 2 "✋ $(http.message)" @@ -212,7 +239,7 @@ function IOExtras.closeread(http::Stream{Response}) # Error if Message is not complete... close(http.stream) throw(EOFError()) - elseif connectionclosed(http.parser) + elseif hasheader(http.message, "Connection", "close") # Close conncetion if server sent "Connection: close"... @debug 1 "✋ \"Connection: close\": $(http.stream)" close(http.stream) @@ -224,4 +251,16 @@ function IOExtras.closeread(http::Stream{Response}) end +function IOExtras.closeread(http::Stream{Request}) + if !messagecomplete(http.parser) + # Error if Message is not complete... + close(http.stream) + throw(EOFError()) + end + if isreadable(http) + closeread(http.stream) + end +end + + end #module Streams From d0329053447cfb16cc64f6e5b2af9a8238da6467 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 10 Jan 2018 16:18:58 +1100 Subject: [PATCH 132/182] cosmatics --- src/ConnectionPool.jl | 2 +- src/MessageRequest.jl | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 9d46f980b..b8075fa2a 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -245,7 +245,7 @@ function IOExtras.startread(t::Transaction) @require !isreadable(t) ;t.c.readcount != t.sequence && @debug 1 "⏳ Wait read: $t" t.c.timestamp = time() - while t.c.readcount != t.sequence + while t.c.readcount != t.sequence wait(t.c.readdone) end ;@debug 2 "👁 Start read: $t" t.c.readbusy = true diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 112f86044..f2d98d5b7 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -37,8 +37,6 @@ function request(::Type{MessageLayer{Next}}, setheader(headers, "Content-Length" => string(l)) elseif method == "GET" && iofunction isa Function setheader(headers, "Content-Length" => "0") -# FIXME else -# setheader(headers, "Transfer-Encoding" => "chunked") end end From 16f28eea82d36342fce56856b1284000ed61537c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Wed, 10 Jan 2018 16:22:55 +1100 Subject: [PATCH 133/182] WIP integrating connection pipelining and HTTP.Stream with server.jl --- src/server.jl | 326 +++++++++++++++++++++++--------------------------- 1 file changed, 150 insertions(+), 176 deletions(-) diff --git a/src/server.jl b/src/server.jl index 4515731a9..cc47f3fbf 100644 --- a/src/server.jl +++ b/src/server.jl @@ -1,5 +1,12 @@ module Nitrogen +using ..IOExtras +using ..Streams +using ..Messages +using ..Parsers +using ..ConnectionPool +import ..@debug, ..@debugshow, ..DEBUG_LEVEL + if !isdefined(Base, :Nothing) const Nothing = Void const Cvoid = Void @@ -70,7 +77,7 @@ ServerOptions(; tlsconfig::HTTP.MbedTLS.SSLConfig=HTTP.MbedTLS.SSLConfig(true), An http/https server. Supports listening on a `host` and `port` via the `HTTP.serve(server, host, port)` function. `handler` is a function of the form `f(::Request, ::Response) -> HTTP.Response`, i.e. it takes both a `Request` and pre-built `Response` -objects as inputs and returns the, potentially modified, `Response`. `logger` indicates where logging output should be directed. +objects as inputs and returns the, potentially modified, `Respose`. `logger` indicates where logging output should be directed. When `HTTP.serve` is called, it aims to "never die", catching and recovering from all internal errors. To forcefully stop, one can obviously kill the julia process, interrupt (ctrl/cmd+c) if main task, or send the kill signal over a server in channel like: `put!(server.in, HTTP.KILL)`. @@ -91,154 +98,129 @@ mutable struct Server{T <: Scheme, H <: HTTP.Handler} out::Channel{Any} options::ServerOptions - Server{T, H}(handler::H, logger::IO=STDOUT, ch=Channel(1), ch2=Channel(1), options=ServerOptions()) where {T, H} = new{T, H}(handler, logger, ch, ch2, options) + Server{T, H}(handler::H, logger::IO=STDOUT, ch=Channel(1), ch2=Channel(1), + options=ServerOptions()) where {T, H} = + new{T, H}(handler, logger, ch, ch2, options) end backtrace() = sprint(Base.show_backtrace, catch_backtrace()) -function process!(server::Server{T, H}, parser, request, i, tcp, rl, starttime, verbose) where {T, H} - handler, logger, options = server.handler, server.logger, server.options - startedprocessingrequest = error = shouldclose = alreadysent100continue = false - rate = Float64(server.options.ratelimit.num) - rl.allowance += 1.0 # because it was just decremented right before we got here - HTTP.@log "processing on connection i=$i..." + +function handle_request(f, server, io, i, verbose=true) + + logger = server.logger + + request = HTTP.Request() + http = Streams.Stream(request, ConnectionPool.getparser(io), io) + response = request.response + response.status = 200 + try - tsk = @async begin - while isopen(tcp) - update!(rl, server.options.ratelimit) - if rl.allowance > rate - HTTP.@log "throttling on connection i=$i" - rl.allowance = rate - end - if rl.allowance < 1.0 - HTTP.@log "sleeping on connection i=$i due to rate limiting" - sleep(1.0) - else - rl.allowance -= 1.0 - HTTP.@log "reading request bytes with readtimeout=$(options.readtimeout)" - # EH: - buffer = try - readavailable(tcp) - catch e - UInt8[] - end - length(buffer) > 0 || break - starttime[] = time() # reset the timeout while still receiving bytes - err = HTTP.@catcherr HTTP.ParsingError HTTP.parse!(parser, buffer) - startedprocessingrequest = true - if err != nothing - # error in parsing the http request - HTTP.@log "error parsing request on connection i=$i: $(HTTP.ParsingErrorCodeMap[err.code])" - if err.code == HTTP.HPE_INVALID_VERSION - response = HTTP.Response(505) - elseif err.code == HTTP.HPE_HEADER_OVERFLOW - response = HTTP.Response(431) - elseif err.code == HTTP.HPE_URI_OVERFLOW - response = HTTP.Response(414) - elseif err.code == HTTP.HPE_BODY_OVERFLOW - response = HTTP.Response(413) - elseif err.code == HTTP.HPE_INVALID_METHOD - response = HTTP.Response(405) - else - response = HTTP.Response(400) - end - error = true - elseif HTTP.headerscomplete(parser) && Base.get(HTTP.headers(request), "Expect", "") == "100-continue" && !alreadysent100continue - if options.support100continue - HTTP.@log "sending 100 Continue response to get request body" - # EH: - try - write(tcp, HTTP.Response(100), options) - catch e - HTTP.@log e - error = true - end - parser.state = HTTP.s_body_identity - alreadysent100continue = true - continue - else - response = HTTP.Response(417) - error = true - end - elseif HTTP.upgrade(parser) - @show String(collect(HTTP.extra(parser))) - HTTP.@log "received upgrade request on connection i=$i" - response = HTTP.Response(501, "upgrade requests are not currently supported") - error = true - elseif HTTP.messagecomplete(parser) - HTTP.@log "received request on connection i=$i" - - request.method = parser.method - request.uri = parser.url - request.major = parser.major - request.minor = parser.minor - - verbose && (show(logger, request); println(logger, "")) - try - response = Handlers.handle(handler, request, HTTP.Response()) - catch e - response = HTTP.Response(500) - error = true - showerror(logger, e) - println(logger, backtrace()) - end - if HTTP.http_should_keep_alive(parser) && !error - if !any(x->x[1] == "Connection", response.headers) - push!(response.headers, "Connection" => "keep-alive") - end - HTTP.reset!(parser) - request = HTTP.Request() - parser.onbodyfragment = x->write(request.body, x) - parser.onheader = x->HTTP.appendheader(request, x) - else - if !any(x->x[1] == "Connection", response.headers) - push!(response.headers, "Connection" => "close") - end - shouldclose = true - end - if !error - HTTP.@log "responding with response on connection i=$i" - verbose && (show(logger, response); println(logger, "")) - - try - write(tcp, response, options) - catch e - HTTP.@log e - error = true - end - end - (error || shouldclose) && break - startedprocessingrequest = alreadysent100continue = false - end - end + startread(http) + + if header(request, "Expect") == "100-continue" + if server.options.support100continue + response.status = 100 + startwrite(http) + response.status = 200 + else + response.status = 417 end end - timeout = options.readtimeout - while !istaskdone(tsk) && (time() - starttime[] < timeout) - sleep(0.001) + + if http.parser.message.upgrade + HTTP.@log "received upgrade request on connection i=$i" + response.status = 501 + response.body = + Vector{UInt8}("upgrade requests are not currently supported") end - if !istaskdone(tsk) - HTTP.@log "connection i=$i timed out waiting for request bytes" - startedprocessingrequest && write(tcp, HTTP.Response(408), options) + + catch e + if e isa HTTP.ParsingError + HTTP.@log "error parsing request on connection i=$i: " * + HTTP.ParsingErrorCodeMap[err.code] + response.status = e.code == Parsers.HPE_INVALID_VERSION ? 505 : + e.code == Parsers.HPE_INVALID_METHOD ? 405 : 400 + response.body = HTTP.ParsingErrorCodeMap[err.code] + else + close(io) + rethrow(e) end - finally - close(tcp) end - HTTP.@log "finished processing on connection i=$i" - return nothing + + if iserror(response) + startwrite(http) + write(http, response.body) + close(io) + return + end + + HTTP.@log "received request on connection i=$i" + verbose && (show(logger, request); println(logger, "")) + + @async try + + try + f(http) + catch e + if !iswritable(io) + showerror(logger, e) + println(logger, backtrace()) + response.status = 500 + startwrite(http) + write(http, sprint(showerror, e)) + else + rethrow(e) + end + end + + closeread(http) + closewrite(http) + + catch e + close(io) + rethrow(e) + end end -initTLS!(::Type{http}, tcp, tlsconfig) = return tcp -function initTLS!(::Type{https}, tcp, tlsconfig) + + +function handle_connection(f, server::Server{T, H}, i, io::Connection{ST}, rl, verbose) where {T, H, ST} + logger = server.logger + rate = Float64(server.options.ratelimit.num) + rl.allowance += 1.0 # because it was just decremented right before we got here + HTTP.@log "processing on connection i=$i..." + while isopen(io) + update!(rl, server.options.ratelimit) + if rl.allowance > rate + HTTP.@log "throttling on connection i=$i" + rl.allowance = rate + end + if rl.allowance < 1.0 + HTTP.@log "sleeping on connection i=$i due to rate limiting" + sleep(1.0) + else + rl.allowance -= 1.0 + end + handle_request(f, server, ConnectionPool.Transaction{ST}(io), i) + end +end + + +init_connection(::Server{http}, tcp) = tcp + +function init_connection(server::Server{https}, tcp) + tls_config = server.options.tlsconfig::HTTP.MbedTLS.SSLConfig try tls = HTTP.MbedTLS.SSLContext() - HTTP.MbedTLS.setup!(tls, tlsconfig) + HTTP.MbedTLS.setup!(tls, tls_config) HTTP.MbedTLS.associate!(tls, tcp) HTTP.MbedTLS.handshake!(tls) return tls catch e close(tcp) error("Error establishing SSL connection: $e") + rethrow(e) end end @@ -257,10 +239,10 @@ end @enum Signals KILL -function serve(server::Server{T, H}, host, port, verbose) where {T, H} +function serve(f, server::Server{T, H}, host, port, verbose) where {T, H} logger = server.logger HTTP.@log "starting server to listen on: $(host):$(port)" - tcpserver = listen(host, port) + tcpserver = Base.listen(host, port) ratelimits = Dict{IPAddr, RateLimit}() rate = Float64(server.options.ratelimit.num) i = 0 @@ -271,11 +253,6 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} end end while true - p = HTTP.Parser() - request = HTTP.Request() - p.onbodyfragment = x->write(request.body, x) - p.onheader = x->HTTP.appendheader(request, x) - try # accept blocks until a new connection is detected tcp = accept(tcpserver) @@ -292,8 +269,36 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} else rl.allowance -= 1.0 HTTP.@log "new tcp connection accepted, reading request..." - let server=server, p=p, request=request, i=i, tcp=tcp, rl=rl - @async process!(server, p, request, i, initTLS!(T, tcp, server.options.tlsconfig::HTTP.MbedTLS.SSLConfig), rl, Ref{Float64}(time()), verbose) + tcp = init_connection(server, tcp) + SocketType = T == https ? HTTP.MbedTLS.SSLContext : TCPSocket + c = Connection{SocketType}(tcp) + let server=server, i=i, c=c, rl=rl + wait_for_timeout = Ref{Bool}(true) + readtimeout = server.options.readtimeout + @async while wait_for_timeout[] + if inactiveseconds(c) > readtimeout + + # FIXME send a 408 ? + + close(io) + HTTP.@log "Connection timeout i=$i" + break + end + sleep(8 + rand() * 4) + end + @async try + handle_connection(f, server, i, c, rl, verbose) + catch e + if e isa EOFError + HTTP.@log "connection i=$i: $e" + else + rethrow(e) + end + finally + HTTP.@log "finished processing on connection i=$i" + wait_for_timeout[] = false + close(c) + end end i += 1 end @@ -344,8 +349,8 @@ By default, `HTTP.serve` aims to "never die", catching and recovering from all i """ function serve end -serve(server::Server, host=IPv4(127,0,0,1), port=8081; verbose::Bool=true) = serve(server, host, port, verbose) -function serve(host::IPAddr, port::Int, +serve(f, server::Server, host=IPv4(127,0,0,1), port=8081; verbose::Bool=true) = serve(f, server, host, port, verbose) +function serve(f, host::IPAddr, port::Int, handler=(req, rep) -> HTTP.Response("Hello World!"), logger::I=STDOUT; cert::String="", @@ -353,9 +358,9 @@ function serve(host::IPAddr, port::Int, verbose::Bool=true, args...) where {I} server = Server(handler, logger; cert=cert, key=key, args...) - return serve(server, host, port, verbose) + return serve(f, server, host, port, verbose) end -serve(; host::IPAddr=IPv4(127,0,0,1), +serve(f, ; host::IPAddr=IPv4(127,0,0,1), port::Int=8081, handler=(req, rep) -> HTTP.Response("Hello World!"), logger::IO=STDOUT, @@ -363,41 +368,10 @@ serve(; host::IPAddr=IPv4(127,0,0,1), key::String="", verbose::Bool=true, args...) = - serve(host, port, handler, logger; cert=cert, key=key, verbose=verbose, args...) - -#= Does the parser need to see an EOF to find the end of the message? =# -function http_message_needs_eof(parser) - #= See RFC 2616 section 4.4 =# - if (isrequest(parser) || # FIXME request never needs EOF ?? - div(parser.status, 100) == 1 || #= 1xx e.g. Continue =# - parser.status == 204 || #= No Content =# - parser.status == 304 || #= Not Modified =# - parser.isheadresponse) #= response to a HEAD request =# - return false - end - - if (parser.flags & F_CHUNKED > 0) || parser.content_length != ULLONG_MAX - return false - end + serve(f, host, port, handler, logger; cert=cert, key=key, verbose=verbose, args...) - return true +function listen(f) + HTTP.serve(f, HTTP.Server((x,y)->())) end -function http_should_keep_alive(parser) - if parser.major > 0 && parser.minor > 0 - #= HTTP/1.1 =# - if parser.flags & F_CONNECTION_CLOSE > 0 - return false - end - else - #= HTTP/1.0 or earlier =# - if !(parser.flags & F_CONNECTION_KEEP_ALIVE > 0) - return false - end - end - - return !http_message_needs_eof(parser) -end - - end # module From a2419c948f53f625eea0e1a5a4aed62272e82ecf Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 11 Jan 2018 14:23:30 +1100 Subject: [PATCH 134/182] Add HTTP.listen server API HTTP.listen() do http write(http, "Hello World!") end Add HTTP.WebSockets.listen API as test case for HTTP.listen HTTP.WebSockets.listen() do ws write(ws, "WebSockets Echo Server...\n") while !eof(ws); s = String(readavailable(ws)) println(("in: $s") write(ws, "echo: $s\n") end end --- src/ConnectionPool.jl | 20 +++--- src/HTTP.jl | 2 +- src/Messages.jl | 6 +- src/Streams.jl | 43 +++++++++--- src/WebSockets.jl | 107 ++++++++++++++++++++---------- src/server.jl | 149 +++++++++++++++++++++++++++++++++++++++++- 6 files changed, 265 insertions(+), 62 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index b8075fa2a..5fa35518c 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -104,11 +104,9 @@ struct Transaction{T <: IO} <: IO sequence::Int end -Connection{T}(io::T) where T <: IO = - Connection{T}("", "", default_pipeline_limit, io) -Connection{T}(host::AbstractString, port::AbstractString, - pipeline_limit::Int, io::T) where T <: IO = +Connection(host::AbstractString, port::AbstractString, + pipeline_limit::Int, io::T) where T <: IO = Connection{T}(host, port, pipeline_limit, peerport(io), localport(io), io, view(UInt8[], 1:0), @@ -117,11 +115,11 @@ Connection{T}(host::AbstractString, port::AbstractString, 0, false, Condition(), 0, Parser()) -Transaction{T}(c::Connection{T}) where T <: IO = +Transaction(c::Connection{T}) where T <: IO = Transaction{T}(c, (c.sequence += 1)) -function client_transaction(T, c) - t = Transaction{T}(c) +function client_transaction(c) + t = Transaction(c) startwrite(t) return t end @@ -135,7 +133,7 @@ getrawstream(t::Transaction) = t.c.io inactiveseconds(t::Transaction) = inactiveseconds(t.c) function inactiveseconds(c::Connection)::Float64 - if !c.readbusy || c.writebusy + if !c.readbusy && !c.writebusy return Float64(0) end return time() - c.timestamp @@ -455,7 +453,7 @@ function getconnection(::Type{Transaction{T}}, idle = filter(c->!c.readbusy, writable) if !isempty(idle) c = rand(idle) ;@debug 2 "♻️ Idle: $c" - return client_transaction(T, c) + return client_transaction(c) end # If there are not too many connections to this host:port, @@ -463,9 +461,9 @@ function getconnection(::Type{Transaction{T}}, busy = findall(T, host, port, pipeline_limit) if length(busy) < connection_limit io = getconnection(T, host, port; kw...) - c = Connection{T}(host, port, pipeline_limit, io) + c = Connection(host, port, pipeline_limit, io) push!(pool, c) ;@debug 1 "🔗 New: $c" - return client_transaction(T, c) + return client_transaction(c) end # Share a connection that has active readers... diff --git a/src/HTTP.jl b/src/HTTP.jl index 83478be38..2d54a2a9f 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -579,7 +579,7 @@ include("WebSockets.jl") ;using .WebSockets include("client.jl") include("sniff.jl") include("handlers.jl"); using .Handlers -include("server.jl"); using .Nitrogen +include("server.jl"); using .Nitrogen.listen end include("precompile.jl") diff --git a/src/Messages.jl b/src/Messages.jl index 94d2cd980..b1c84ff80 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -212,7 +212,7 @@ statustext(r::Response) = Base.get(Parsers.STATUS_CODES, r.status, "Unknown Code Get header value for `key` (case-insensitive). """ -header(m, k, d="") = header(m.headers, k, d) +header(m::Message, k, d="") = header(m.headers, k, d) header(h::Headers, k::String, d::String="") = getbyfirst(h, k, k => d, lceq)[2] lceq(a,b) = lowercase(a) == lowercase(b) @@ -238,7 +238,7 @@ hasheader(m, k::String, v::String) = lowercase(header(m, k)) == v Set header `value` for `key` (case-insensitive). """ -setheader(m, v) = setheader(m.headers, v) +setheader(m::Message, v) = setheader(m.headers, v) setheader(h::Headers, v::Pair) = setbyfirst(h, Pair{String,String}(v), lceq) @@ -371,7 +371,7 @@ end function readstartline!(m::Parsers.Message, r::Request) r.version = VersionNumber(m.major, m.minor) - r.method = string(m.method) + r.method = m.method r.uri = m.url return end diff --git a/src/Streams.jl b/src/Streams.jl index 000822216..e0bc03d46 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -1,12 +1,15 @@ module Streams -export Stream, closebody, isaborted +export Stream, closebody, isaborted, + header, hasheader, + setstatus, setheader import ..HTTP using ..IOExtras using ..Parsers using ..Messages -import ..Messages: header, hasheader, setheader, writeheaders, writestartline +import ..Messages: header, hasheader, setheader, + writeheaders, writestartline import ..ConnectionPool.getrawstream import ..@require, ..precondition_error import ..@debug, ..DEBUG_LEVEL @@ -50,10 +53,11 @@ Creates a `HTTP.Stream` that wraps an existing `IO` stream. Stream(r::M, p::Parser, io::S) where {M, S} = Stream{M,S}(r, p, io, false) header(http::Stream, a...) = header(http.message, a...) +setstatus(http::Stream, status) = (http.message.response.status = status) setheader(http::Stream, a...) = setheader(http.message.response, a...) -hasheader(http::Stream, a...) = hasheader(http.message, a...) getrawstream(http::Stream) = getrawstream(http.stream) +IOExtras.isopen(http::Stream) = isopen(http.stream) # Writing HTTP Messages @@ -135,16 +139,35 @@ IOExtras.isreadable(http::Stream) = isreadable(http.stream) function IOExtras.startread(http::Stream) startread(http.stream) configure_parser(http) - message = readheaders(http.stream, http.parser, http.message) - if http.message isa Response && http.message.status == 100 - # 100 Continue - # https://tools.ietf.org/html/rfc7230#section-5.6 - # https://tools.ietf.org/html/rfc7231#section-6.2.1 + readheaders(http.stream, http.parser, http.message) + handle_continue(http) + return http.message +end + + +""" +100 Continue +https://tools.ietf.org/html/rfc7230#section-5.6 +https://tools.ietf.org/html/rfc7231#section-6.2.1 +""" + +function handle_continue(http::Stream{Response}) + if http.message.status == 100 @debug 1 "✅ Continue: $(http.stream)" configure_parser(http) - message = readheaders(http.stream, http.parser, http.message) + readheaders(http.stream, http.parser, http.message) + end + +end + +function handle_continue(http::Stream{Request}) + if hasheader(http.message, "Expect", "100-continue") + if !iswritable(http.stream) + startwrite(http.stream) + end + @debug 1 "✅ Continue: $(http.stream)" + writeheaders(http.stream, Response(100)) end - return message end diff --git a/src/WebSockets.jl b/src/WebSockets.jl index 93420e28e..723454907 100644 --- a/src/WebSockets.jl +++ b/src/WebSockets.jl @@ -4,7 +4,8 @@ using Base64 using Unicode using MbedTLS: digest, MD_SHA1, SSLContext import ..HTTP -using ..HTTP.IOExtras +using ..IOExtras +using ..Streams import ..ConnectionPool using HTTP.header import ..@debug, ..DEBUG_LEVEL, ..@require, ..precondition_error @@ -40,14 +41,15 @@ end mutable struct WebSocket{T <: IO} <: IO io::T frame_type::UInt8 + server::Bool rxpayload::Vector{UInt8} txpayload::Vector{UInt8} txclosed::Bool rxclosed::Bool end -function WebSocket(io::T; binary=false) where T <: IO - WebSocket{T}(io, binary ? WS_BINARY : WS_TEXT, +function WebSocket(io::T; server=false, binary=false) where T <: IO + WebSocket{T}(io, binary ? WS_BINARY : WS_TEXT, server, UInt8[], UInt8[], false, false) end @@ -56,7 +58,27 @@ end # Handshake -function open(f::Function, url; binary=false, kw...) +function check_upgrade(http) + + if !hasheader(http, "Upgrade", "websocket") + throw(WebSocketError(0, "Expected \"Upgrade: websocket\"!\n" * + "$(http.message)")) + end + + if !hasheader(http, "Connection", "upgrade") + throw(WebSocketError(0, "Expected \"Connection: upgrade\"!\n" * + "$(http.message)")) + end +end + + +function accept_hash(key) + hashkey = "$(key)258EAFA5-E914-47DA-95CA-C5AB0DC85B11" + return base64encode(digest(MD_SHA1, hashkey)) +end + + +function open(f::Function, url; binary=false, verbose=false, kw...) key = base64encode(rand(UInt8, 16)) @@ -67,7 +89,8 @@ function open(f::Function, url; binary=false, kw...) "Sec-WebSocket-Version" => "13" ] - HTTP.open("GET", url, headers; reuse_limit=0, kw...) do http + HTTP.open("GET", url, headers; + reuse_limit=0, verbose=verbose ? 2 : 0, kw...) do http startread(http) @@ -76,22 +99,9 @@ function open(f::Function, url; binary=false, kw...) return end - upgrade = header(http, "Upgrade") - if lowercase(upgrade) != "websocket" - throw(WebSocketError(0, "Expected \"Upgrade: websocket\"!\n" * - "$(http.message)")) - end - - connection = header(http, "Connection") - if lowercase(connection) != "upgrade" - throw(WebSocketError(0, "Expected \"Connection: upgrade\"!\n" * - "$(http.message)")) - end + check_upgrade(http) - hashkey = "$(key)258EAFA5-E914-47DA-95CA-C5AB0DC85B11" - accepthash = base64encode(digest(MD_SHA1, hashkey)) - accept = header(http, "Sec-WebSocket-Accept") - if accept != accepthash + if header(http, "Sec-WebSocket-Accept") != accept_hash(key) throw(WebSocketError(0, "Invalid Sec-WebSocket-Accept\n" * "$(http.message)")) end @@ -102,6 +112,31 @@ function open(f::Function, url; binary=false, kw...) end +function listen(f::Function, + host::String="localhost", port::UInt16=UInt16(8081); + binary=false, verbose=false, kw...) + + HTTP.listen(host, port; verbose=verbose) do http + + check_upgrade(http) + if !hasheader(http, "Sec-WebSocket-Version", "13") + throw(WebSocketError(0, "Expected \"Sec-WebSocket-Version: 13\"!\n" * + "$(http.message)")) + end + + setstatus(http, 101) + setheader(http, "Upgrade" => "websocket") + setheader(http, "Connection" => "Upgrade") + key = header(http, "Sec-WebSocket-Key") + setheader(http, "Sec-WebSocket-Accept" => accept_hash(key)) + + startwrite(http) + + io = ConnectionPool.getrawstream(http) + f(WebSocket(io; binary=binary, server=true)) + end +end + # Sending Frames @@ -132,9 +167,9 @@ function IOExtras.closewrite(ws::WebSocket) end -wslength(l) = l < 0x7E ? (UInt8(l), UInt8[]) : - l <= 0xFFFF ? (0x7E, reinterpret(UInt8, [UInt16(l)])) : - (0x7F, reinterpret(UInt8, [UInt64(l)])) +wslength(l) = l < 0x7E ? (UInt8(l), UInt8[]) : + l <= 0xFFFF ? (0x7E, reinterpret(UInt8, [hton(UInt16(l))])) : + (0x7F, reinterpret(UInt8, [hton(UInt64(l))])) wswrite(ws::WebSocket, x) = wswrite(ws, WS_FINAL | ws.frame_type, x) @@ -146,24 +181,22 @@ function wswrite(ws::WebSocket, opcode::UInt8, bytes::Vector{UInt8}) n = length(bytes) len, extended_len = wslength(n) len |= WS_MASK - mask = mask!(ws, bytes) + mask = mask!(ws.txpayload, bytes, n) @debug 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, len, extended_len, mask))" write(ws.io, opcode, len, extended_len, mask) - + @debug 2 " ⬅️ $(ws.txpayload[1:n])" unsafe_write(ws.io, pointer(ws.txpayload), n) end -function mask!(ws::WebSocket, bytes::Vector{UInt8}) - mask = rand(UInt8, 4) - l = length(bytes) - if length(ws.txpayload) < l - resize!(ws.txpayload, l) +function mask!(out, in, l, mask=rand(UInt8, 4)) + if length(out) < l + resize!(out, l) end for i in 1:l - ws.txpayload[i] = bytes[i] ⊻ mask[((i-1) % 4)+1] + out[i] = in[i] ⊻ mask[((i-1) % 4)+1] end return mask end @@ -200,7 +233,7 @@ function readheader(io::IO) len == 0x7F ? UInt(ntoh(read(io, UInt64))) : len == 0x7E ? UInt(ntoh(read(io, UInt16))) : UInt(len), b[2] & WS_MASK > 0, - b[2] & WS_MASK > 0 ? ntoh(read(io, UInt32)) : UInt32(0)) + b[2] & WS_MASK > 0 ? read(io, UInt32) : UInt32(0)) end @@ -231,10 +264,14 @@ function readframe(ws::WebSocket) wswrite(ws, WS_FINAL | WS_PONG, ws.rxpayload) return readframe(ws) else - return view(ws.rxpayload, 1:Int(h.length)) + l = Int(h.length) + if h.hasmask + mask!(ws.rxpayload, ws.rxpayload, l, reinterpret(UInt8, [h.mask])) + end + return view(ws.rxpayload, 1:l) end end - + function WebSocketHeader(bytes...) io = IOBuffer() write(io, bytes...) @@ -243,7 +280,7 @@ function WebSocketHeader(bytes...) end function Base.show(io::IO, h::WebSocketHeader) - print(io, "WebSocketHeader(", + print(io, "WebSocketHeader(", h.opcode == WS_CONTINUATION ? "CONTINUATION" : h.opcode == WS_TEXT ? "TEXT" : h.opcode == WS_BINARY ? "BINARY" : diff --git a/src/server.jl b/src/server.jl index cc47f3fbf..77d9bb23a 100644 --- a/src/server.jl +++ b/src/server.jl @@ -6,6 +6,8 @@ using ..Messages using ..Parsers using ..ConnectionPool import ..@debug, ..@debugshow, ..DEBUG_LEVEL +using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! + if !isdefined(Base, :Nothing) const Nothing = Void @@ -370,8 +372,151 @@ serve(f, ; host::IPAddr=IPv4(127,0,0,1), args...) = serve(f, host, port, handler, logger; cert=cert, key=key, verbose=verbose, args...) -function listen(f) - HTTP.serve(f, HTTP.Server((x,y)->())) + + + +function getsslcontext(tcp, sslconfig) + ssl = SSLContext() + setup!(ssl, sslconfig) + associate!(ssl, tcp) + handshake!(ssl) + return ssl +end + + +function listen(f::Function, + host::String="127.0.0.1", port::UInt16=UInt16(8081); + ssl::Bool=false, + require_ssl_verification::Bool=false, + sslconfig::SSLConfig=SSLConfig(require_ssl_verification), + pipeline_limit::Int=ConnectionPool.default_pipeline_limit, + kw...) + + @info "Listening on: $(host):$(port)" + tcpserver = Base.listen(getaddrinfo(host), port) + + try + while isopen(tcpserver) + try + io = accept(tcpserver) + io = ssl ? getsslcontext(io, sslconfig) : io + let io = Connection(host, string(port), pipeline_limit, io) + @info "Accept: $io" + @async try + handle_connection(f, io; kw...) + catch e + @error "Error: $io" e catch_stacktrace() + finally + close(io) + @info "Closed: $io" + end + end + catch e + if typeof(e) <: InterruptException + @warn "Interrupted: listen($host,$port)" + close(tcpserver) + else + rethrow(e) + end + end + end + finally + close(tcpserver) + end + + return +end + + +function handle_connection(f::Function, c::Connection; + readtimeout::Int=0, kw...) + + wait_for_timeout = Ref{Bool}(true) + if readtimeout > 0 + @async while wait_for_timeout[] + @show inactiveseconds(c) + if inactiveseconds(c) > readtimeout + @warn "Timeout: $c" + writeheaders(c.io, Response(408, ["Connection" => "close"])) + close(c) + break + end + sleep(8 + rand() * 4) + end + end + + try + while isopen(c) + io = Transaction(c) + handle_transaction(f, io; kw...) + end + finally + wait_for_timeout[] = false + end + return end + +function handle_transaction(f::Function, t; verbose=false, kw...) + + request = HTTP.Request() + http = Streams.Stream(request, ConnectionPool.getparser(t), t) + response = request.response + response.status = 200 + + try + startread(http) + catch e + if e isa EOFError && !messagestarted(http.parser) + return + elseif e isa HTTP.ParsingError + @error e + status = e.code == Parsers.HPE_INVALID_VERSION ? 505 : + e.code == Parsers.HPE_INVALID_METHOD ? 405 : 400 + write(http.stream, + Response(status, body = HTTP.ParsingErrorCodeMap[err.code])) + else + rethrow(e) + end + end + + if verbose + @info http.message + end + + @async try + handle_stream(f, http) + catch e + if isioerror(e) + @warn e + else + @error e catch_stacktrace() + end + close(t) + end + return +end + + +function handle_stream(f::Function, http) + + try + f(http) + catch e + if isopen(http) && !iswritable(http) + @error e catch_stacktrace() + http.message.response.status = 500 + startwrite(http) + write(http, sprint(showerror, e)) + else + rethrow(e) + end + end + + closeread(http) + closewrite(http) + return +end + + end # module From e17f7adf03f04439aa42e89f691ca3b78925fe29 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 11 Jan 2018 14:29:25 +1100 Subject: [PATCH 135/182] Parser cleanup post "Connection: Upgrade" testing. - rename ULLONG_MAX => unknown_length - rationalise reset!() / constructors - move some init from reset!() to state s_start_req_or_res - replace flags with chunked::Bool and trailing::Bool - dead code removal: - No need for special treatment of Connection: and Upgrade: headers. Parser does not use these internally. (Content-Length: and Transfer-Encoding: are still used to determine how to parse body) - Hard-coded list of methods precludes use of other registered/custom methods. e.g. https://tools.ietf.org/html/rfc3253#section-3.5. Method is a token: https://tools.ietf.org/html/rfc7230#section-3.1.1 https://tools.ietf.org/html/rfc7230#section-3.2.6 So, parse method until first non-token character. --- docs/src/index.md | 1 - src/MessageRequest.jl | 6 +- src/consts.jl | 62 ++--- src/parser.jl | 618 +++++++++++++++++++++--------------------- test/parser.jl | 11 +- 5 files changed, 350 insertions(+), 348 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 99fc9a762..79de83970 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -151,7 +151,6 @@ HTTP.ConnectionPool ```@docs HTTP.Parsers.Message -HTTP.Parsers.Parser() HTTP.Parsers.parseheaders HTTP.Parsers.parsebody HTTP.Parsers.reset! diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index f2d98d5b7..065c3f19f 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -33,7 +33,7 @@ function request(::Type{MessageLayer{Next}}, !hasheader(headers, "Transfer-Encoding") && !hasheader(headers, "Upgrade") l = bodylength(body) - if l != unknownlength + if l != unknown_length setheader(headers, "Content-Length" => string(l)) elseif method == "GET" && iofunction isa Function setheader(headers, "Content-Length" => "0") @@ -46,8 +46,8 @@ function request(::Type{MessageLayer{Next}}, end -const unknownlength = -1 -bodylength(body) = unknownlength +const unknown_length = -1 +bodylength(body) = unknown_length bodylength(body::AbstractVector{UInt8}) = length(body) bodylength(body::AbstractString) = sizeof(body) if !minimal diff --git a/src/consts.jl b/src/consts.jl index 14a9b880c..4b2ad2e1a 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -113,10 +113,12 @@ const STATUS_CODES = Dict( # RFC-2068, section 19.6.1.2 LINK=31, UNLINK=32, + xHTTP, NOMETHOD ) const MethodMap = Dict( + "HTTP" => xHTTP, "DELETE" => DELETE, "GET" => GET, "HEAD" => HEAD, @@ -156,7 +158,6 @@ Base.convert(::Type{Method}, s::String) = MethodMap[s] # parsing codes @enum(ParsingErrorCode, HPE_OK, - HPE_HEADER_OVERFLOW, HPE_INVALID_VERSION, HPE_INVALID_STATUS, HPE_INVALID_METHOD, @@ -174,7 +175,6 @@ Base.convert(::Type{Method}, s::String) = MethodMap[s] const ParsingErrorCodeMap = Dict( HPE_OK => "success", - HPE_HEADER_OVERFLOW => "too many header bytes seen; overflow detected", HPE_INVALID_VERSION => "invalid HTTP version", HPE_INVALID_STATUS => "invalid HTTP status code", HPE_INVALID_METHOD => "invalid HTTP method", @@ -264,32 +264,32 @@ end # header states const h_general = 0x00 -const h_C = 0x01 -const h_CO = 0x02 -const h_CON = 0x03 +#UNUSED const h_C = 0x01 +#UNUSED const h_CO = 0x02 +#UNUSED const h_CON = 0x03 -const h_matching_connection = 0x04 -const h_matching_proxy_connection = 0x05 +#UNUSED const h_matching_connection = 0x04 +#UNUSED const h_matching_proxy_connection = 0x05 const h_matching_content_length = 0x06 const h_matching_transfer_encoding = 0x07 -const h_matching_upgrade = 0x08 +#UNUSED const h_matching_upgrade = 0x08 -const h_connection = 0x0a +#UNUSED const h_connection = 0x0a const h_content_length = 0x0b const h_transfer_encoding = 0x0c -const h_upgrade = 0x0d +#UNUSED const h_upgrade = 0x0d const h_matching_transfer_encoding_chunked = 0x0f -const h_matching_connection_token_start = 0x10 -const h_matching_connection_keep_alive = 0x11 -const h_matching_connection_close = 0x12 -const h_matching_connection_upgrade = 0x13 -const h_matching_connection_token = 0x14 +#UNUSED const h_matching_connection_token_start = 0x10 +#UNUSED const h_matching_connection_keep_alive = 0x11 +#UNUSED const h_matching_connection_close = 0x12 +#UNUSED const h_matching_connection_upgrade = 0x13 +#UNUSED const h_matching_connection_token = 0x14 const h_transfer_encoding_chunked = 0x15 -const h_connection_keep_alive = 0x16 -const h_connection_close = 0x17 -const h_connection_upgrade = 0x18 +#UNUSED const h_connection_keep_alive = 0x16 +#UNUSED const h_connection_close = 0x17 +#UNUSED const h_connection_upgrade = 0x18 const CR = '\r' const bCR = UInt8('\r') @@ -297,16 +297,16 @@ const LF = '\n' const bLF = UInt8('\n') const CRLF = "\r\n" -const ULLONG_MAX = typemax(UInt64) +const unknown_length = typemax(UInt64) -const PROXY_CONNECTION = "proxy-connection" -const CONNECTION = "connection" +#UNUSED const PROXY_CONNECTION = "proxy-connection" +#UNUSED const CONNECTION = "connection" const CONTENT_LENGTH = "content-length" const TRANSFER_ENCODING = "transfer-encoding" -const UPGRADE = "upgrade" +#UNUSED const UPGRADE = "upgrade" const CHUNKED = "chunked" -const KEEP_ALIVE = "keep-alive" -const CLOSE = "close" +#UNUSED const KEEP_ALIVE = "keep-alive" +#UNUSED const CLOSE = "close" #= Tokens as defined by rfc 2616. Also lowercases them. # token = 1* @@ -361,13 +361,13 @@ const unhex = Int8[ ] # flags -const F_CHUNKED = UInt8(1 << 0) -const F_CONNECTION_KEEP_ALIVE = UInt8(1 << 1) -const F_CONNECTION_CLOSE = UInt8(1 << 2) -const F_CONNECTION_UPGRADE = UInt8(1 << 3) -const F_TRAILING = UInt8(1 << 4) -const F_UPGRADE = UInt8(1 << 5) -const F_CONTENTLENGTH = UInt8(1 << 6) +#UNUSED const F_CHUNKED = UInt8(1 << 0) +#UNUSED const F_CONNECTION_KEEP_ALIVE = UInt8(1 << 1) +#UNUSED const F_CONNECTION_CLOSE = UInt8(1 << 2) +#UNUSED const F_CONNECTION_UPGRADE = UInt8(1 << 3) +#UNUSED const F_TRAILING = UInt8(1 << 4) +#UNUSED const F_UPGRADE = UInt8(1 << 5) +#UNUSED const F_CONTENTLENGTH = UInt8(1 << 6) # url parsing const normal_url_char = Bool[ diff --git a/src/parser.jl b/src/parser.jl index e64fa1624..8d1d5c80a 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -57,20 +57,28 @@ const Headers = Vector{Header} - `major` and `minor`: HTTP version - `url::String`: request URL - `status::Int`: response status - - `upgrade::Bool`: Connection should be upgraded to a different protocol. - e.g. `CONNECT` or `Connection: upgrade`. """ mutable struct Message - method::Method + method::String +#UNUSED methodc::Method major::Int16 minor::Int16 url::String status::Int32 - upgrade::Bool + + Message() = reset!(new()) end -Message() = Message(NOMETHOD, 0, 0, "", 0, false) +function reset!(m::Message) + m.method = "" +#UNUSED m.methodc = NOMETHOD + m.major = 0 + m.minor = 0 + m.url = "" + m.status = 0 + return m +end """ @@ -102,25 +110,23 @@ mutable struct Parser state::UInt8 header_state::UInt8 index::UInt8 - flags::UInt8 + chunked::Bool + trailing::Bool content_length::UInt64 fieldbuffer::IOBuffer valuebuffer::IOBuffer # output message::Message -end - - -""" - Parser() - -Create an unconfigured `Parser`. -""" -Parser() = Parser(false, - s_start_req_or_res, 0, 0, 0, 0, - IOBuffer(), IOBuffer(), Message()) + function Parser() + p = new() + p.fieldbuffer = IOBuffer() + p.valuebuffer = IOBuffer() + p.message = Message() + return reset!(p) + end +end """ @@ -130,26 +136,10 @@ Revert `Parser` to unconfigured state. """ function reset!(p::Parser) - - # config p.message_has_no_body = false - - # state p.state = s_start_req_or_res - p.header_state = 0 - p.index = 0 - p.flags = 0 - p.content_length = 0 - truncate(p.fieldbuffer, 0) - truncate(p.valuebuffer, 0) - - # output - p.message.method = NOMETHOD - p.message.major = 0 - p.message.minor = 0 - p.message.url = "" - p.message.status = 0 - p.message.upgrade = false + reset!(p.message) + return p end @@ -226,7 +216,7 @@ end Is the `Parser` ready to process trailing headers? """ -messagehastrailing(p::Parser) = p.flags & F_TRAILING > 0 +messagehastrailing(p::Parser) = p.trailing isrequest(p::Parser) = p.message.status == 0 @@ -326,47 +316,54 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if p_state == s_start_req_or_res (ch == CR || ch == LF) && continue - parser.flags = 0 - parser.content_length = ULLONG_MAX - if ch == 'H' - p_state = s_res_or_resp_H - else + parser.header_state = h_general + parser.index = 0 + parser.content_length = unknown_length + parser.chunked = false + parser.trailing = false + truncate(parser.fieldbuffer, 0) + truncate(parser.valuebuffer, 0) + +#UNUSED if ch == 'H' +#UNUSED p_state = s_res_or_resp_H +#UNUSED else p_state = s_start_req p -= 1 - end - - elseif p_state == s_res_or_resp_H - if ch == 'T' - p_state = s_res_HT - else - @errorif(ch != 'E', HPE_INVALID_CONSTANT) - parser.message.method = HEAD - parser.index = 3 - p_state = s_req_method - end - - elseif p_state == s_start_res - parser.flags = 0 - parser.content_length = ULLONG_MAX - if ch == 'H' - p_state = s_res_H - elseif ch == CR || ch == LF - else - @err HPE_INVALID_CONSTANT - end - - elseif p_state == s_res_H - @errorifstrict(ch != 'T') - p_state = s_res_HT - - elseif p_state == s_res_HT - @errorifstrict(ch != 'T') - p_state = s_res_HTT - - elseif p_state == s_res_HTT - @errorifstrict(ch != 'P') - p_state = s_res_HTTP +#UNUSED end + +#UNUSED elseif p_state == s_res_or_resp_H +#UNUSED if ch == 'T' +#UNUSED p_state = s_res_HT +#UNUSED else +#UNUSED @errorif(ch != 'E', HPE_INVALID_CONSTANT) +#UNUSED parser.message.methodc = HEAD +#UNUSED write(parser.valuebuffer, "HE") +#UNUSED parser.index = 3 +#UNUSED p_state = s_req_method +#UNUSED end + +#UNUSED elseif p_state == s_start_res +#UNUSED parser.flags = 0 +#UNUSED parser.content_length = unknown_length +#UNUSED if ch == 'H' +#UNUSED p_state = s_res_H +#UNUSED elseif ch == CR || ch == LF +#UNUSED else +#UNUSED @err HPE_INVALID_CONSTANT +#UNUSED end + +#UNUSED elseif p_state == s_res_H +#UNUSED @errorifstrict(ch != 'T') +#UNUSED p_state = s_res_HT + +#UNUSED elseif p_state == s_res_HT +#UNUSED @errorifstrict(ch != 'T') +#UNUSED p_state = s_res_HTT + +#UNUSED elseif p_state == s_res_HTT +#UNUSED @errorifstrict(ch != 'P') +#UNUSED p_state = s_res_HTTP elseif p_state == s_res_HTTP @errorifstrict(ch != '/') @@ -453,113 +450,134 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif p_state == s_start_req (ch == CR || ch == LF) && continue - parser.flags = 0 - parser.content_length = ULLONG_MAX +#REDUNDANT parser.flags = 0 +#REDUNDANT parser.content_length = unknown_length @errorif(!isalpha(ch), HPE_INVALID_METHOD) - parser.message.method = Method(0) +#= UNUSED + parser.message.methodc = Method(0) parser.index = 2 if ch == 'A' - parser.message.method = ACL + parser.message.methodc = ACL elseif ch == 'B' - parser.message.method = BIND + parser.message.methodc = BIND elseif ch == 'C' - parser.message.method = CONNECT + parser.message.methodc = CONNECT elseif ch == 'D' - parser.message.method = DELETE + parser.message.methodc = DELETE elseif ch == 'G' - parser.message.method = GET + parser.message.methodc = GET elseif ch == 'H' - parser.message.method = HEAD + parser.message.methodc = HEAD elseif ch == 'L' - parser.message.method = LOCK + parser.message.methodc = LOCK elseif ch == 'M' - parser.message.method = MKCOL + parser.message.methodc = MKCOL elseif ch == 'N' - parser.message.method = NOTIFY + parser.message.methodc = NOTIFY elseif ch == 'O' - parser.message.method = OPTIONS + parser.message.methodc = OPTIONS elseif ch == 'P' - parser.message.method = POST + parser.message.methodc = POST elseif ch == 'R' - parser.message.method = REPORT + parser.message.methodc = REPORT elseif ch == 'S' - parser.message.method = SUBSCRIBE + parser.message.methodc = SUBSCRIBE elseif ch == 'T' - parser.message.method = TRACE + parser.message.methodc = TRACE elseif ch == 'U' - parser.message.method = UNLOCK + parser.message.methodc = UNLOCK else @err(HPE_INVALID_METHOD) end +=# p_state = s_req_method + write(parser.valuebuffer, ch) + elseif p_state == s_req_method - matcher = string(parser.message.method) - @debugshow 4 matcher - @debugshow 4 parser.index - if ch == ' ' && parser.index == length(matcher) + 1 +#UNUSED matcher = string(parser.message.methodc == xHTTP ? "HTTP" : parser.message.methodc) +#UNUSED @debugshow 4 matcher +#UNUSED @debugshow 4 parser.index + if tokens[Int(ch)+1] == Char(0) + parser.message.method = take!(parser.valuebuffer) + if parser.message.method == "HTTP" + p_state = s_res_first_http_major + else + p_state = s_req_spaces_before_url + end + else + write(parser.valuebuffer, ch) + end +#= UNUSED if ch == '/' && parser.index == length(matcher) + 1 && + parser.message.methodc == xHTTP + p_state = s_res_first_http_major + truncate(parser.valuebuffer, 0) + elseif ch == ' ' && parser.index == length(matcher) + 1 p_state = s_req_spaces_before_url elseif parser.index > length(matcher) @err(HPE_INVALID_METHOD) elseif ch == matcher[parser.index] @debug 4 "nada" elseif isalpha(ch) - ci = @methodstate(parser.message.method, + ci = @methodstate(parser.message.methodc, Int(parser.index) - 1, ch) if ci == @methodstate(POST, 1, 'U') - parser.message.method = PUT + parser.message.methodc = PUT + elseif ci == @methodstate(HEAD, 1, 'T') + parser.message.methodc = xHTTP elseif ci == @methodstate(POST, 1, 'A') - parser.message.method = PATCH + parser.message.methodc = PATCH elseif ci == @methodstate(CONNECT, 1, 'H') - parser.message.method = CHECKOUT + parser.message.methodc = CHECKOUT elseif ci == @methodstate(CONNECT, 2, 'P') - parser.message.method = COPY + parser.message.methodc = COPY elseif ci == @methodstate(MKCOL, 1, 'O') - parser.message.method = MOVE + parser.message.methodc = MOVE elseif ci == @methodstate(MKCOL, 1, 'E') - parser.message.method = MERGE + parser.message.methodc = MERGE elseif ci == @methodstate(MKCOL, 2, 'A') - parser.message.method = MKACTIVITY + parser.message.methodc = MKACTIVITY elseif ci == @methodstate(MKCOL, 3, 'A') - parser.message.method = MKCALENDAR + parser.message.methodc = MKCALENDAR elseif ci == @methodstate(SUBSCRIBE, 1, 'E') - parser.message.method = SEARCH + parser.message.methodc = SEARCH elseif ci == @methodstate(REPORT, 2, 'B') - parser.message.method = REBIND + parser.message.methodc = REBIND elseif ci == @methodstate(POST, 1, 'R') - parser.message.method = PROPFIND + parser.message.methodc = PROPFIND elseif ci == @methodstate(PROPFIND, 4, 'P') - parser.message.method = PROPPATCH + parser.message.methodc = PROPPATCH elseif ci == @methodstate(PUT, 2, 'R') - parser.message.method = PURGE + parser.message.methodc = PURGE elseif ci == @methodstate(LOCK, 1, 'I') - parser.message.method = LINK + parser.message.methodc = LINK elseif ci == @methodstate(UNLOCK, 2, 'S') - parser.message.method = UNSUBSCRIBE + parser.message.methodc = UNSUBSCRIBE elseif ci == @methodstate(UNLOCK, 2, 'B') - parser.message.method = UNBIND + parser.message.methodc = UNBIND elseif ci == @methodstate(UNLOCK, 3, 'I') - parser.message.method = UNLINK + parser.message.methodc = UNLINK else @err(HPE_INVALID_METHOD) end elseif ch == '-' && parser.index == 2 && - parser.message.method == MKCOL + parser.message.methodc == MKCOL @debug 4 "matched MSEARCH" - parser.message.method = MSEARCH + parser.message.methodc = MSEARCH parser.index -= 1 else @err(HPE_INVALID_METHOD) end parser.index += 1 @debugshow 4 parser.index +=# elseif p_state == s_req_spaces_before_url ch == ' ' && continue - if parser.message.method == CONNECT + if parser.message.method == "CONNECT" p_state = s_req_server_start else p_state = s_req_url_start @@ -694,13 +712,13 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_header_field if c == 'c' - parser.header_state = h_C - elseif c == 'p' - parser.header_state = h_matching_proxy_connection + parser.header_state = h_matching_content_length +#UNUSED elseif c == 'p' +#UNUSED parser.header_state = h_matching_proxy_connection elseif c == 't' parser.header_state = h_matching_transfer_encoding - elseif c == 'u' - parser.header_state = h_matching_upgrade +#UNUSED elseif c == 'u' +#UNUSED parser.header_state = h_matching_upgrade else parser.header_state = h_general end @@ -722,39 +740,39 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, h = parser.header_state if h == h_general - elseif h == h_C - parser.index += 1 - parser.header_state = c == 'o' ? h_CO : h_general - elseif h == h_CO - parser.index += 1 - parser.header_state = c == 'n' ? h_CON : h_general - elseif h == h_CON - parser.index += 1 - if c == 'n' - parser.header_state = h_matching_connection - elseif c == 't' - parser.header_state = h_matching_content_length - else - parser.header_state = h_general - end - # connection - elseif h == h_matching_connection - parser.index += 1 - if parser.index > length(CONNECTION) || - c != CONNECTION[parser.index] - parser.header_state = h_general - elseif parser.index == length(CONNECTION) - parser.header_state = h_connection - end - # proxy-connection - elseif h == h_matching_proxy_connection - parser.index += 1 - if parser.index > length(PROXY_CONNECTION) || - c != PROXY_CONNECTION[parser.index] - parser.header_state = h_general - elseif parser.index == length(PROXY_CONNECTION) - parser.header_state = h_connection - end +#UNUSED elseif h == h_C +#UNUSED parser.index += 1 +#UNUSED parser.header_state = c == 'o' ? h_CO : h_general +#UNUSED elseif h == h_CO +#UNUSED parser.index += 1 +#UNUSED parser.header_state = c == 'n' ? h_CON : h_general +#UNUSED elseif h == h_CON +#UNUSED parser.index += 1 +#UNUSED if c == 'n' +#UNUSED parser.header_state = h_matching_connection +#UNUSED if c == 't' +#UNUSED parser.header_state = h_matching_content_length +#UNUSED else +#UNUSED parser.header_state = h_general +#UNUSED end +#UNUSED # connection +#UNUSED elseif h == h_matching_connection +#UNUSED parser.index += 1 +#UNUSED if parser.index > length(CONNECTION) || +#UNUSED c != CONNECTION[parser.index] +#UNUSED parser.header_state = h_general +#UNUSED elseif parser.index == length(CONNECTION) +#UNUSED parser.header_state = h_connection +#UNUSED end +#UNUSED # proxy-connection +#UNUSED elseif h == h_matching_proxy_connection +#UNUSED parser.index += 1 +#UNUSED if parser.index > length(PROXY_CONNECTION) || +#UNUSED c != PROXY_CONNECTION[parser.index] +#UNUSED parser.header_state = h_general +#UNUSED elseif parser.index == length(PROXY_CONNECTION) +#UNUSED parser.header_state = h_connection +#UNUSED end # content-length elseif h == h_matching_content_length parser.index += 1 @@ -773,17 +791,17 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif parser.index == length(TRANSFER_ENCODING) parser.header_state = h_transfer_encoding end - # upgrade - elseif h == h_matching_upgrade - parser.index += 1 - if parser.index > length(UPGRADE) || - c != UPGRADE[parser.index] - parser.header_state = h_general - elseif parser.index == length(UPGRADE) - parser.header_state = h_upgrade - end - elseif @anyeq(h, h_connection, h_content_length, - h_transfer_encoding, h_upgrade) +#UNUSED # upgrade +#UNUSED elseif h == h_matching_upgrade +#UNUSED parser.index += 1 +#UNUSED if parser.index > length(UPGRADE) || +#UNUSED c != UPGRADE[parser.index] +#UNUSED parser.header_state = h_general +#UNUSED elseif parser.index == length(UPGRADE) +#UNUSED parser.header_state = h_upgrade +#UNUSED end + elseif @anyeq(h, #=h_connection,=# h_content_length, + h_transfer_encoding#=, h_upgrade=#) if ch != ' ' parser.header_state = h_general end @@ -820,35 +838,34 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, parser.index = 1 c = lower(ch) - if parser.header_state == h_upgrade - parser.flags |= F_UPGRADE - parser.header_state = h_general - elseif parser.header_state == h_transfer_encoding +#UNUSED if parser.header_state == h_upgrade +#UNUSED parser.flags |= F_UPGRADE +#UNUSED parser.header_state = h_general + if parser.header_state == h_transfer_encoding # looking for 'Transfer-Encoding: chunked' parser.header_state = ifelse( c == 'c', h_matching_transfer_encoding_chunked, h_general) elseif parser.header_state == h_content_length @errorif(!isnum(ch), HPE_INVALID_CONTENT_LENGTH) - @errorif((parser.flags & F_CONTENTLENGTH > 0) != 0, + @errorif(parser.content_length != unknown_length, HPE_UNEXPECTED_CONTENT_LENGTH) - parser.flags |= F_CONTENTLENGTH parser.content_length = UInt64(ch - '0') - elseif parser.header_state == h_connection - # looking for 'Connection: keep-alive' - if c == 'k' - parser.header_state = h_matching_connection_keep_alive - # looking for 'Connection: close' - elseif c == 'c' - parser.header_state = h_matching_connection_close - elseif c == 'u' - parser.header_state = h_matching_connection_upgrade - else - parser.header_state = h_matching_connection_token - end - # Multi-value `Connection` header - elseif parser.header_state == h_matching_connection_token_start +#UNUSED elseif parser.header_state == h_connection +#UNUSED # looking for 'Connection: keep-alive' +#UNUSED if c == 'k' +#UNUSED parser.header_state = h_matching_connection_keep_alive +#UNUSED # looking for 'Connection: close' +#UNUSED elseif c == 'c' +#UNUSED parser.header_state = h_matching_connection_close +#UNUSED if c == 'u' +#UNUSED parser.header_state = h_matching_connection_upgrade +#UNUSED else +#UNUSED parser.header_state = h_matching_connection_token +#UNUSED end +#UNUSED # Multi-value `Connection` header +#UNUSED elseif parser.header_state == h_matching_connection_token_start else parser.header_state = h_general end @@ -880,7 +897,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, view(bytes, p:len)) p = crlf == 0 ? len : p + crlf - 2 - elseif h == h_connection || h == h_transfer_encoding + elseif h == #=h_connection ||=# h == h_transfer_encoding @err HPE_INVALID_INTERNAL_STATE elseif h == h_content_length t = UInt64(0) @@ -897,7 +914,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, # Overflow? # Test against a conservative limit for simplicity. @debugshow 4 Int(parser.content_length) - if div(ULLONG_MAX - 10, 10) < t + if div(typemax(UInt64) - 10, 10) < t parser.header_state = h @err(HPE_INVALID_CONTENT_LENGTH) end @@ -914,78 +931,79 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, h = h_transfer_encoding_chunked end - elseif h == h_matching_connection_token_start - # looking for 'Connection: keep-alive' - if c == 'k' - h = h_matching_connection_keep_alive - # looking for 'Connection: close' - elseif c == 'c' - h = h_matching_connection_close - elseif c == 'u' - h = h_matching_connection_upgrade - elseif tokens[Int(c)+1] > '\0' - h = h_matching_connection_token - elseif c == ' ' || c == '\t' - # Skip lws - else - h = h_general - end +#UNUSED elseif h == h_matching_connection_token_start +#UNUSED # looking for 'Connection: keep-alive' +#UNUSED if c == 'k' +#UNUSED h = h_matching_connection_keep_alive +#UNUSED # looking for 'Connection: close' +#UNUSED elseif c == 'c' +#UNUSED h = h_matching_connection_close +#UNUSED if c == 'u' +#UNUSED h = h_matching_connection_upgrade +#UNUSED elseif tokens[Int(c)+1] > '\0' +#UNUSED h = h_matching_connection_token +#UNUSED elseif c == ' ' || c == '\t' +#UNUSED # Skip lws +#UNUSED else +#UNUSED h = h_general +#UNUSED end # looking for 'Connection: keep-alive' - elseif h == h_matching_connection_keep_alive - parser.index += 1 - if parser.index > length(KEEP_ALIVE) || - c != KEEP_ALIVE[parser.index] - h = h_matching_connection_token - elseif parser.index == length(KEEP_ALIVE) - h = h_connection_keep_alive - end - - # looking for 'Connection: close' - elseif h == h_matching_connection_close - parser.index += 1 - if parser.index > length(CLOSE) || - c != CLOSE[parser.index] - h = h_matching_connection_token - elseif parser.index == length(CLOSE) - h = h_connection_close - end - - # looking for 'Connection: upgrade' - elseif h == h_matching_connection_upgrade - parser.index += 1 - if parser.index > length(UPGRADE) || - c != UPGRADE[parser.index] - h = h_matching_connection_token - elseif parser.index == length(UPGRADE) - h = h_connection_upgrade - end - - elseif h == h_matching_connection_token - if ch == ',' - h = h_matching_connection_token_start - parser.index = 1 - end +#UNUSED elseif h == h_matching_connection_keep_alive +#UNUSED parser.index += 1 +#UNUSED if parser.index > length(KEEP_ALIVE) || +#UNUSED c != KEEP_ALIVE[parser.index] +#UNUSED h = h_matching_connection_token +#UNUSED elseif parser.index == length(KEEP_ALIVE) +#UNUSED h = h_connection_keep_alive +#UNUSED end + +#UNUSED # looking for 'Connection: close' +#UNUSED elseif h == h_matching_connection_close +#UNUSED parser.index += 1 +#UNUSED if parser.index > length(CLOSE) || +#UNUSED c != CLOSE[parser.index] +#UNUSED h = h_matching_connection_token +#UNUSED elseif parser.index == length(CLOSE) +#UNUSED h = h_connection_close +#UNUSED end + +#UNUSED # looking for 'Connection: upgrade' +#UNUSED elseif h == h_matching_connection_upgrade +#UNUSED parser.index += 1 +#UNUSED if parser.index > length(UPGRADE) || +#UNUSED c != UPGRADE[parser.index] +#UNUSED h = h_matching_connection_token +#UNUSED elseif parser.index == length(UPGRADE) +#UNUSED h = h_connection_upgrade +#UNUSED end + +#UNUSED elseif h == h_matching_connection_token +#UNUSED if ch == ',' +#UNUSED h = h_matching_connection_token_start +#UNUSED parser.index = 1 +#UNUSED end elseif h == h_transfer_encoding_chunked if ch != ' ' h = h_general end - elseif @anyeq(h, h_connection_keep_alive, h_connection_close, - h_connection_upgrade) - if ch == ',' - if h == h_connection_keep_alive - parser.flags |= F_CONNECTION_KEEP_ALIVE - elseif h == h_connection_close - parser.flags |= F_CONNECTION_CLOSE - elseif h == h_connection_upgrade - parser.flags |= F_CONNECTION_UPGRADE - end - h = h_matching_connection_token_start - parser.index = 1 - elseif ch != ' ' - h = h_matching_connection_token - end + +#UNUSED elseif @anyeq(h, h_connection_keep_alive, h_connection_close, +#UNUSED h_connection_upgrade) +#UNUSED if ch == ',' +#UNUSED if h == h_connection_keep_alive +#UNUSED parser.flags |= F_CONNECTION_KEEP_ALIVE +#UNUSED elseif h == h_connection_close +#UNUSED parser.flags |= F_CONNECTION_CLOSE +#UNUSED elseif h == h_connection_upgrade +#UNUSED parser.flags |= F_CONNECTION_UPGRADE +#UNUSED end +#UNUSED h = h_matching_connection_token_start +#UNUSED parser.index = 1 +#UNUSED elseif ch != ' ' +#UNUSED h = h_matching_connection_token +#UNUSED end else p_state = s_header_value @@ -1016,14 +1034,14 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_header_value_start else # finished the header - if parser.header_state == h_connection_keep_alive - parser.flags |= F_CONNECTION_KEEP_ALIVE - elseif parser.header_state == h_connection_close - parser.flags |= F_CONNECTION_CLOSE - elseif parser.header_state == h_transfer_encoding_chunked - parser.flags |= F_CHUNKED - elseif parser.header_state == h_connection_upgrade - parser.flags |= F_CONNECTION_UPGRADE +#UNUSED if parser.header_state == h_connection_keep_alive +#UNUSED parser.flags |= F_CONNECTION_KEEP_ALIVE +#UNUSED elseif parser.header_state == h_connection_close +#UNUSED parser.flags |= F_CONNECTION_CLOSE + if parser.header_state == h_transfer_encoding_chunked + parser.chunked = true +#UNUSED elseif parser.header_state == h_connection_upgrade +#UNUSED parser.flags |= F_CONNECTION_UPGRADE end p_state = s_header_field_start end @@ -1036,14 +1054,14 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if ch == ' ' || ch == '\t' p_state = s_header_value_discard_ws else - if parser.header_state == h_connection_keep_alive - parser.flags |= F_CONNECTION_KEEP_ALIVE - elseif parser.header_state == h_connection_close - parser.flags |= F_CONNECTION_CLOSE - elseif parser.header_state == h_connection_upgrade - parser.flags |= F_CONNECTION_UPGRADE - elseif parser.header_state == h_transfer_encoding_chunked - parser.flags |= F_CHUNKED +#UNUSED if parser.header_state == h_connection_keep_alive +#UNUSED parser.flags |= F_CONNECTION_KEEP_ALIVE +#UNUSED elseif parser.header_state == h_connection_close +#UNUSED parser.flags |= F_CONNECTION_CLOSE +#UNUSED if parser.header_state == h_connection_upgrade +#UNUSED parser.flags |= F_CONNECTION_UPGRADE + if parser.header_state == h_transfer_encoding_chunked + parser.chunked = true end # header value was empty @@ -1055,51 +1073,38 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif p_state == s_headers_almost_done @errorifstrict(ch != LF) p -= 1 - if (parser.flags & F_TRAILING) > 0 + if parser.trailing # End of a chunked request p_state = s_message_done else # Cannot use chunked encoding and a content-length header # together per the HTTP specification. - @errorif((parser.flags & F_CHUNKED) > 0 && - (parser.flags & F_CONTENTLENGTH) > 0, + @errorif(parser.chunked && + parser.content_length != unknown_length, HPE_UNEXPECTED_CONTENT_LENGTH) p_state = s_headers_done - - # Set this here for onheaderscomplete() callback. - if (parser.flags & F_UPGRADE > 0) && - (parser.flags & F_CONNECTION_UPGRADE > 0) - parser.message.upgrade = isrequest(parser) || - parser.message.status == 101 - else - parser.message.upgrade = isrequest(parser) && - parser.message.method == CONNECT - end - @debugshow 4 parser.message.upgrade end elseif p_state == s_headers_done @errorifstrict(ch != LF) if parser.message_has_no_body || - parser.content_length == 0 || - (parser.message.upgrade && isrequest(parser) && - parser.message.method == CONNECT) + parser.content_length == 0 || + parser.message.method == "CONNECT" p_state = s_message_done - elseif parser.flags & F_CHUNKED > 0 + elseif parser.chunked # chunked encoding - ignore Content-Length header p_state = s_chunk_size_start - elseif parser.content_length != ULLONG_MAX + elseif parser.content_length != unknown_length # Content-Length header given and non-zero p_state = s_body_identity elseif isrequest(parser) || # RFC 7230, 3.3.3, 6. div(parser.message.status, 100) == 1 || # 1xx e.g. Continue parser.message.status == 204 || # No Content parser.message.status == 304 # Not Modified - # Assume content-length 0 - read the next - p_state = s_message_done + p_state = s_message_done # =>Content-1ength: 0 else # Read body until EOF p_state = s_body_identity_eof @@ -1161,7 +1166,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} if p_state == s_body_identity to_read = Int(min(parser.content_length, len - p + 1)) @passert parser.content_length != 0 && - parser.content_length != ULLONG_MAX + parser.content_length != unknown_length @passert result == nobytes result = view(bytes, p:p + to_read - 1) @@ -1179,7 +1184,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} p = len elseif p_state == s_chunk_size_start - @passert parser.flags & F_CHUNKED > 0 + @passert parser.chunked unhex_val = unhex[Int(ch)+1] @errorif(unhex_val == -1, HPE_INVALID_CHUNK_SIZE) @@ -1188,7 +1193,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} p_state = s_chunk_size elseif p_state == s_chunk_size - @passert parser.flags & F_CHUNKED > 0 + @passert parser.chunked if ch == CR p_state = s_chunk_size_almost_done else @@ -1207,25 +1212,25 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} # Overflow? Test against a conservative limit for simplicity. @debugshow 4 Int(parser.content_length) - if div(ULLONG_MAX - 16, 16) < t + if div(typemax(UInt64) - 16, 16) < t @err(HPE_INVALID_CONTENT_LENGTH) end parser.content_length = t end elseif p_state == s_chunk_parameters - @passert parser.flags & F_CHUNKED > 0 + @passert parser.chunked # just ignore this?. FIXME check for overflow? if ch == CR p_state = s_chunk_size_almost_done end elseif p_state == s_chunk_size_almost_done - @passert parser.flags & F_CHUNKED > 0 + @passert parser.chunked @errorifstrict(ch != LF) if parser.content_length == 0 - parser.flags |= F_TRAILING + parser.trailing = 1 p_state = s_trailer_start else p_state = s_chunk_data @@ -1234,9 +1239,9 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} elseif p_state == s_chunk_data to_read = Int(min(parser.content_length, len - p + 1)) - @passert parser.flags & F_CHUNKED > 0 + @passert parser.chunked @passert parser.content_length != 0 && - parser.content_length != ULLONG_MAX + parser.content_length != unknown_length @passert result == nobytes result = view(bytes, p:p + to_read - 1) @@ -1248,13 +1253,13 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} end elseif p_state == s_chunk_data_almost_done - @passert parser.flags & F_CHUNKED > 0 + @passert parser.chunked @passert parser.content_length == 0 @errorifstrict(ch != CR) p_state = s_chunk_data_done elseif p_state == s_chunk_data_done - @passert parser.flags & F_CHUNKED > 0 + @passert parser.chunked @errorifstrict(ch != LF) p_state = s_chunk_size_start @@ -1288,14 +1293,9 @@ end Base.show(io::IO, p::Parser) = print(io, "Parser(", - "state=", ParsingStateCode(p.state),", ", - p.flags & F_CHUNKED > 0 ? "F_CHUNKED, " : "", - p.flags & F_CONNECTION_KEEP_ALIVE > 0 ? "F_CONNECTION_KEEP_ALIVE, " : "", - p.flags & F_CONNECTION_CLOSE > 0 ? "F_CONNECTION_CLOSE, " : "", - p.flags & F_CONNECTION_UPGRADE > 0 ? "F_CONNECTION_UPGRADE, " : "", - p.flags & F_TRAILING > 0 ? "F_TRAILING, " : "", - p.flags & F_UPGRADE > 0 ? "F_UPGRADE, " : "", - p.flags & F_CONTENTLENGTH > 0 ? "F_CONTENTLENGTH, " : "", + "state=", ParsingStateCode(p.state), ", ", + "chunked=", p.chunked, ", ", + "trailing=", p.trailing, ", ", "content_length=", p.content_length, ", ", "message=", p.message, ")") diff --git a/test/parser.jl b/test/parser.jl index e600489c1..57dd2d09f 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -519,7 +519,7 @@ Message(name= "curl get" ,should_keep_alive= true ,http_major= 1 ,http_minor= 1 -,method= "MSEARCH" +,method= "M-SEARCH" ,query_string= "" ,fragment= "" ,request_path= "*" @@ -1851,15 +1851,18 @@ const responses = Message[ @test String(take!(b)) == "fooba" for m in instances(Parsers.Method) - m in (Parsers.NOMETHOD, Parsers.CONNECT) && continue + m in (Parsers.xHTTP, Parsers.NOMETHOD, Parsers.CONNECT) && continue me = m == Parsers.MSEARCH ? "M-SEARCH" : "$m" r = Request("$me / HTTP/1.1\r\n\r\n") - @test r.method == string(m) + @test r.method == string(me) end - for m in ("ASDF","C******","COLA","GEM","GETA","M****","MKCOLA","PROPPATCHA","PUN","PX","SA","hello world") + for m in ("HTTP/1.1", "hello world") @test_throws ParsingError Request("$m / HTTP/1.1\r\n\r\n") end + for m in ("ASDF","C******","COLA","GEM","GETA","M****","MKCOLA","PROPPATCHA","PUN","PX","SA") + @test Request("$m / HTTP/1.1\r\n\r\n").method == m + end @test_throws ParsingError Request("GET / HTTP/1.1\r\n" * "name\r\n" * " : value\r\n\r\n") From 133b5dd89585ac5f0c1e7793fdebfef91bfe5366 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Thu, 11 Jan 2018 15:47:15 +1100 Subject: [PATCH 136/182] Remove commented out dead code. add parse_token function, used to parse method. --- src/consts.jl | 40 +----- src/parser.jl | 341 ++++++-------------------------------------------- 2 files changed, 38 insertions(+), 343 deletions(-) diff --git a/src/consts.jl b/src/consts.jl index 4b2ad2e1a..c30414379 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -195,11 +195,6 @@ const ParsingErrorCodeMap = Dict( ,es_dead=1 ,es_start_req_or_res=2 ,es_res_or_resp_H=3 - ,es_start_res=4 - ,es_res_H=5 - ,es_res_HT=6 - ,es_res_HTT=7 - ,es_res_HTTP=8 ,es_res_first_http_major=9 ,es_res_http_major=10 ,es_res_first_http_minor=11 @@ -264,32 +259,13 @@ end # header states const h_general = 0x00 -#UNUSED const h_C = 0x01 -#UNUSED const h_CO = 0x02 -#UNUSED const h_CON = 0x03 -#UNUSED const h_matching_connection = 0x04 -#UNUSED const h_matching_proxy_connection = 0x05 const h_matching_content_length = 0x06 const h_matching_transfer_encoding = 0x07 -#UNUSED const h_matching_upgrade = 0x08 - -#UNUSED const h_connection = 0x0a const h_content_length = 0x0b const h_transfer_encoding = 0x0c -#UNUSED const h_upgrade = 0x0d - const h_matching_transfer_encoding_chunked = 0x0f -#UNUSED const h_matching_connection_token_start = 0x10 -#UNUSED const h_matching_connection_keep_alive = 0x11 -#UNUSED const h_matching_connection_close = 0x12 -#UNUSED const h_matching_connection_upgrade = 0x13 -#UNUSED const h_matching_connection_token = 0x14 - const h_transfer_encoding_chunked = 0x15 -#UNUSED const h_connection_keep_alive = 0x16 -#UNUSED const h_connection_close = 0x17 -#UNUSED const h_connection_upgrade = 0x18 const CR = '\r' const bCR = UInt8('\r') @@ -299,14 +275,9 @@ const CRLF = "\r\n" const unknown_length = typemax(UInt64) -#UNUSED const PROXY_CONNECTION = "proxy-connection" -#UNUSED const CONNECTION = "connection" const CONTENT_LENGTH = "content-length" const TRANSFER_ENCODING = "transfer-encoding" -#UNUSED const UPGRADE = "upgrade" const CHUNKED = "chunked" -#UNUSED const KEEP_ALIVE = "keep-alive" -#UNUSED const CLOSE = "close" #= Tokens as defined by rfc 2616. Also lowercases them. # token = 1* @@ -349,6 +320,8 @@ const tokens = Char[ #= 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del =# 'x', 'y', 'z', 0, '|', 0, '~', 0 ] +istoken(c) = tokens[UInt8(c)+1] != Char(0) + const unhex = Int8[ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 @@ -360,15 +333,6 @@ const unhex = Int8[ ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ] -# flags -#UNUSED const F_CHUNKED = UInt8(1 << 0) -#UNUSED const F_CONNECTION_KEEP_ALIVE = UInt8(1 << 1) -#UNUSED const F_CONNECTION_CLOSE = UInt8(1 << 2) -#UNUSED const F_CONNECTION_UPGRADE = UInt8(1 << 3) -#UNUSED const F_TRAILING = UInt8(1 << 4) -#UNUSED const F_UPGRADE = UInt8(1 << 5) -#UNUSED const F_CONTENTLENGTH = UInt8(1 << 6) - # url parsing const normal_url_char = Bool[ #= 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel =# diff --git a/src/parser.jl b/src/parser.jl index 8d1d5c80a..f421e0d45 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -61,7 +61,6 @@ const Headers = Vector{Header} mutable struct Message method::String -#UNUSED methodc::Method major::Int16 minor::Int16 url::String @@ -72,7 +71,6 @@ end function reset!(m::Message) m.method = "" -#UNUSED m.methodc = NOMETHOD m.major = 0 m.minor = 0 m.url = "" @@ -273,6 +271,26 @@ macro methodstate(meth, i, char) return esc(:(Int($meth) << Int(16) | Int($i) << Int(8) | Int($char))) end +function parse_token(bytes, len, p, buffer) + start = p + while p <= len + @inbounds ch = Char(bytes[p]) + if !istoken(ch) + break + end + p += 1 + end + @passert p <= len + 1 + + write(buffer, view(bytes, start:p-1)) + + if p > len + return len, false + else + return p, true + end +end + """ parseheaders(::Parser, bytes) do h::Pair{String,String} ... -> excess @@ -325,49 +343,8 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, truncate(parser.fieldbuffer, 0) truncate(parser.valuebuffer, 0) -#UNUSED if ch == 'H' -#UNUSED p_state = s_res_or_resp_H -#UNUSED else - p_state = s_start_req - p -= 1 -#UNUSED end - -#UNUSED elseif p_state == s_res_or_resp_H -#UNUSED if ch == 'T' -#UNUSED p_state = s_res_HT -#UNUSED else -#UNUSED @errorif(ch != 'E', HPE_INVALID_CONSTANT) -#UNUSED parser.message.methodc = HEAD -#UNUSED write(parser.valuebuffer, "HE") -#UNUSED parser.index = 3 -#UNUSED p_state = s_req_method -#UNUSED end - -#UNUSED elseif p_state == s_start_res -#UNUSED parser.flags = 0 -#UNUSED parser.content_length = unknown_length -#UNUSED if ch == 'H' -#UNUSED p_state = s_res_H -#UNUSED elseif ch == CR || ch == LF -#UNUSED else -#UNUSED @err HPE_INVALID_CONSTANT -#UNUSED end - -#UNUSED elseif p_state == s_res_H -#UNUSED @errorifstrict(ch != 'T') -#UNUSED p_state = s_res_HT - -#UNUSED elseif p_state == s_res_HT -#UNUSED @errorifstrict(ch != 'T') -#UNUSED p_state = s_res_HTT - -#UNUSED elseif p_state == s_res_HTT -#UNUSED @errorifstrict(ch != 'P') -#UNUSED p_state = s_res_HTTP - - elseif p_state == s_res_HTTP - @errorifstrict(ch != '/') - p_state = s_res_first_http_major + p_state = s_start_req + p -= 1 elseif p_state == s_res_first_http_major @errorif(!isnum(ch), HPE_INVALID_VERSION) @@ -450,130 +427,27 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif p_state == s_start_req (ch == CR || ch == LF) && continue -#REDUNDANT parser.flags = 0 -#REDUNDANT parser.content_length = unknown_length - @errorif(!isalpha(ch), HPE_INVALID_METHOD) - -#= UNUSED - parser.message.methodc = Method(0) - parser.index = 2 - - if ch == 'A' - parser.message.methodc = ACL - elseif ch == 'B' - parser.message.methodc = BIND - elseif ch == 'C' - parser.message.methodc = CONNECT - elseif ch == 'D' - parser.message.methodc = DELETE - elseif ch == 'G' - parser.message.methodc = GET - elseif ch == 'H' - parser.message.methodc = HEAD - elseif ch == 'L' - parser.message.methodc = LOCK - elseif ch == 'M' - parser.message.methodc = MKCOL - elseif ch == 'N' - parser.message.methodc = NOTIFY - elseif ch == 'O' - parser.message.methodc = OPTIONS - elseif ch == 'P' - parser.message.methodc = POST - elseif ch == 'R' - parser.message.methodc = REPORT - elseif ch == 'S' - parser.message.methodc = SUBSCRIBE - elseif ch == 'T' - parser.message.methodc = TRACE - elseif ch == 'U' - parser.message.methodc = UNLOCK - else - @err(HPE_INVALID_METHOD) - end -=# - p_state = s_req_method - write(parser.valuebuffer, ch) + @errorif(!istoken(ch), HPE_INVALID_METHOD) + + p_state = s_req_method + p -= 1 elseif p_state == s_req_method -#UNUSED matcher = string(parser.message.methodc == xHTTP ? "HTTP" : parser.message.methodc) -#UNUSED @debugshow 4 matcher -#UNUSED @debugshow 4 parser.index - if tokens[Int(ch)+1] == Char(0) + + p, complete = parse_token(bytes, len, p, parser.valuebuffer) + + if complete parser.message.method = take!(parser.valuebuffer) - if parser.message.method == "HTTP" + @inbounds ch = Char(bytes[p]) + if parser.message.method == "HTTP" && ch == '/' p_state = s_res_first_http_major - else + elseif ch == ' ' p_state = s_req_spaces_before_url - end - else - write(parser.valuebuffer, ch) - end -#= UNUSED if ch == '/' && parser.index == length(matcher) + 1 && - parser.message.methodc == xHTTP - p_state = s_res_first_http_major - truncate(parser.valuebuffer, 0) - elseif ch == ' ' && parser.index == length(matcher) + 1 - p_state = s_req_spaces_before_url - elseif parser.index > length(matcher) - @err(HPE_INVALID_METHOD) - elseif ch == matcher[parser.index] - @debug 4 "nada" - elseif isalpha(ch) - ci = @methodstate(parser.message.methodc, - Int(parser.index) - 1, ch) - if ci == @methodstate(POST, 1, 'U') - parser.message.methodc = PUT - elseif ci == @methodstate(HEAD, 1, 'T') - parser.message.methodc = xHTTP - elseif ci == @methodstate(POST, 1, 'A') - parser.message.methodc = PATCH - elseif ci == @methodstate(CONNECT, 1, 'H') - parser.message.methodc = CHECKOUT - elseif ci == @methodstate(CONNECT, 2, 'P') - parser.message.methodc = COPY - elseif ci == @methodstate(MKCOL, 1, 'O') - parser.message.methodc = MOVE - elseif ci == @methodstate(MKCOL, 1, 'E') - parser.message.methodc = MERGE - elseif ci == @methodstate(MKCOL, 2, 'A') - parser.message.methodc = MKACTIVITY - elseif ci == @methodstate(MKCOL, 3, 'A') - parser.message.methodc = MKCALENDAR - elseif ci == @methodstate(SUBSCRIBE, 1, 'E') - parser.message.methodc = SEARCH - elseif ci == @methodstate(REPORT, 2, 'B') - parser.message.methodc = REBIND - elseif ci == @methodstate(POST, 1, 'R') - parser.message.methodc = PROPFIND - elseif ci == @methodstate(PROPFIND, 4, 'P') - parser.message.methodc = PROPPATCH - elseif ci == @methodstate(PUT, 2, 'R') - parser.message.methodc = PURGE - elseif ci == @methodstate(LOCK, 1, 'I') - parser.message.methodc = LINK - elseif ci == @methodstate(UNLOCK, 2, 'S') - parser.message.methodc = UNSUBSCRIBE - elseif ci == @methodstate(UNLOCK, 2, 'B') - parser.message.methodc = UNBIND - elseif ci == @methodstate(UNLOCK, 3, 'I') - parser.message.methodc = UNLINK else @err(HPE_INVALID_METHOD) end - elseif ch == '-' && - parser.index == 2 && - parser.message.methodc == MKCOL - @debug 4 "matched MSEARCH" - parser.message.methodc = MSEARCH - parser.index -= 1 - else - @err(HPE_INVALID_METHOD) end - parser.index += 1 - @debugshow 4 parser.index -=# elseif p_state == s_req_spaces_before_url ch == ' ' && continue @@ -713,12 +587,8 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if c == 'c' parser.header_state = h_matching_content_length -#UNUSED elseif c == 'p' -#UNUSED parser.header_state = h_matching_proxy_connection elseif c == 't' parser.header_state = h_matching_transfer_encoding -#UNUSED elseif c == 'u' -#UNUSED parser.header_state = h_matching_upgrade else parser.header_state = h_general end @@ -740,39 +610,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, h = parser.header_state if h == h_general -#UNUSED elseif h == h_C -#UNUSED parser.index += 1 -#UNUSED parser.header_state = c == 'o' ? h_CO : h_general -#UNUSED elseif h == h_CO -#UNUSED parser.index += 1 -#UNUSED parser.header_state = c == 'n' ? h_CON : h_general -#UNUSED elseif h == h_CON -#UNUSED parser.index += 1 -#UNUSED if c == 'n' -#UNUSED parser.header_state = h_matching_connection -#UNUSED if c == 't' -#UNUSED parser.header_state = h_matching_content_length -#UNUSED else -#UNUSED parser.header_state = h_general -#UNUSED end -#UNUSED # connection -#UNUSED elseif h == h_matching_connection -#UNUSED parser.index += 1 -#UNUSED if parser.index > length(CONNECTION) || -#UNUSED c != CONNECTION[parser.index] -#UNUSED parser.header_state = h_general -#UNUSED elseif parser.index == length(CONNECTION) -#UNUSED parser.header_state = h_connection -#UNUSED end -#UNUSED # proxy-connection -#UNUSED elseif h == h_matching_proxy_connection -#UNUSED parser.index += 1 -#UNUSED if parser.index > length(PROXY_CONNECTION) || -#UNUSED c != PROXY_CONNECTION[parser.index] -#UNUSED parser.header_state = h_general -#UNUSED elseif parser.index == length(PROXY_CONNECTION) -#UNUSED parser.header_state = h_connection -#UNUSED end # content-length elseif h == h_matching_content_length parser.index += 1 @@ -782,6 +619,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif parser.index == length(CONTENT_LENGTH) parser.header_state = h_content_length end + # transfer-encoding elseif h == h_matching_transfer_encoding parser.index += 1 @@ -791,15 +629,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif parser.index == length(TRANSFER_ENCODING) parser.header_state = h_transfer_encoding end -#UNUSED # upgrade -#UNUSED elseif h == h_matching_upgrade -#UNUSED parser.index += 1 -#UNUSED if parser.index > length(UPGRADE) || -#UNUSED c != UPGRADE[parser.index] -#UNUSED parser.header_state = h_general -#UNUSED elseif parser.index == length(UPGRADE) -#UNUSED parser.header_state = h_upgrade -#UNUSED end + elseif @anyeq(h, #=h_connection,=# h_content_length, h_transfer_encoding#=, h_upgrade=#) if ch != ' ' @@ -815,7 +645,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if ch == ':' p_state = s_header_value_discard_ws else - @passert tokens[Int(ch)+1] != Char(0) || !strict && ch == ' ' + @passert !strict && ch == ' ' || istoken(ch) end write(parser.fieldbuffer, view(bytes, start:p-1)) @@ -838,9 +668,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, parser.index = 1 c = lower(ch) -#UNUSED if parser.header_state == h_upgrade -#UNUSED parser.flags |= F_UPGRADE -#UNUSED parser.header_state = h_general if parser.header_state == h_transfer_encoding # looking for 'Transfer-Encoding: chunked' parser.header_state = ifelse( @@ -852,20 +679,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, HPE_UNEXPECTED_CONTENT_LENGTH) parser.content_length = UInt64(ch - '0') -#UNUSED elseif parser.header_state == h_connection -#UNUSED # looking for 'Connection: keep-alive' -#UNUSED if c == 'k' -#UNUSED parser.header_state = h_matching_connection_keep_alive -#UNUSED # looking for 'Connection: close' -#UNUSED elseif c == 'c' -#UNUSED parser.header_state = h_matching_connection_close -#UNUSED if c == 'u' -#UNUSED parser.header_state = h_matching_connection_upgrade -#UNUSED else -#UNUSED parser.header_state = h_matching_connection_token -#UNUSED end -#UNUSED # Multi-value `Connection` header -#UNUSED elseif parser.header_state == h_matching_connection_token_start else parser.header_state = h_general end @@ -931,80 +744,10 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, h = h_transfer_encoding_chunked end -#UNUSED elseif h == h_matching_connection_token_start -#UNUSED # looking for 'Connection: keep-alive' -#UNUSED if c == 'k' -#UNUSED h = h_matching_connection_keep_alive -#UNUSED # looking for 'Connection: close' -#UNUSED elseif c == 'c' -#UNUSED h = h_matching_connection_close -#UNUSED if c == 'u' -#UNUSED h = h_matching_connection_upgrade -#UNUSED elseif tokens[Int(c)+1] > '\0' -#UNUSED h = h_matching_connection_token -#UNUSED elseif c == ' ' || c == '\t' -#UNUSED # Skip lws -#UNUSED else -#UNUSED h = h_general -#UNUSED end - # looking for 'Connection: keep-alive' -#UNUSED elseif h == h_matching_connection_keep_alive -#UNUSED parser.index += 1 -#UNUSED if parser.index > length(KEEP_ALIVE) || -#UNUSED c != KEEP_ALIVE[parser.index] -#UNUSED h = h_matching_connection_token -#UNUSED elseif parser.index == length(KEEP_ALIVE) -#UNUSED h = h_connection_keep_alive -#UNUSED end - -#UNUSED # looking for 'Connection: close' -#UNUSED elseif h == h_matching_connection_close -#UNUSED parser.index += 1 -#UNUSED if parser.index > length(CLOSE) || -#UNUSED c != CLOSE[parser.index] -#UNUSED h = h_matching_connection_token -#UNUSED elseif parser.index == length(CLOSE) -#UNUSED h = h_connection_close -#UNUSED end - -#UNUSED # looking for 'Connection: upgrade' -#UNUSED elseif h == h_matching_connection_upgrade -#UNUSED parser.index += 1 -#UNUSED if parser.index > length(UPGRADE) || -#UNUSED c != UPGRADE[parser.index] -#UNUSED h = h_matching_connection_token -#UNUSED elseif parser.index == length(UPGRADE) -#UNUSED h = h_connection_upgrade -#UNUSED end - -#UNUSED elseif h == h_matching_connection_token -#UNUSED if ch == ',' -#UNUSED h = h_matching_connection_token_start -#UNUSED parser.index = 1 -#UNUSED end - elseif h == h_transfer_encoding_chunked if ch != ' ' h = h_general end - - -#UNUSED elseif @anyeq(h, h_connection_keep_alive, h_connection_close, -#UNUSED h_connection_upgrade) -#UNUSED if ch == ',' -#UNUSED if h == h_connection_keep_alive -#UNUSED parser.flags |= F_CONNECTION_KEEP_ALIVE -#UNUSED elseif h == h_connection_close -#UNUSED parser.flags |= F_CONNECTION_CLOSE -#UNUSED elseif h == h_connection_upgrade -#UNUSED parser.flags |= F_CONNECTION_UPGRADE -#UNUSED end -#UNUSED h = h_matching_connection_token_start -#UNUSED parser.index = 1 -#UNUSED elseif ch != ' ' -#UNUSED h = h_matching_connection_token -#UNUSED end - else p_state = s_header_value h = h_general @@ -1034,14 +777,8 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_header_value_start else # finished the header -#UNUSED if parser.header_state == h_connection_keep_alive -#UNUSED parser.flags |= F_CONNECTION_KEEP_ALIVE -#UNUSED elseif parser.header_state == h_connection_close -#UNUSED parser.flags |= F_CONNECTION_CLOSE if parser.header_state == h_transfer_encoding_chunked parser.chunked = true -#UNUSED elseif parser.header_state == h_connection_upgrade -#UNUSED parser.flags |= F_CONNECTION_UPGRADE end p_state = s_header_field_start end @@ -1054,12 +791,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if ch == ' ' || ch == '\t' p_state = s_header_value_discard_ws else -#UNUSED if parser.header_state == h_connection_keep_alive -#UNUSED parser.flags |= F_CONNECTION_KEEP_ALIVE -#UNUSED elseif parser.header_state == h_connection_close -#UNUSED parser.flags |= F_CONNECTION_CLOSE -#UNUSED if parser.header_state == h_connection_upgrade -#UNUSED parser.flags |= F_CONNECTION_UPGRADE if parser.header_state == h_transfer_encoding_chunked parser.chunked = true end From 2aad8ca7d8fc7d593ecee0f20dccc632cd22137d Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 01:55:38 +1100 Subject: [PATCH 137/182] add Base.readbytes!(t::Transaction, a::Vector{UInt8}, nb::Int) --- src/ConnectionPool.jl | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 5fa35518c..2adc3c95a 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -182,6 +182,24 @@ function Base.readavailable(t::Transaction)::ByteView end +function Base.readbytes!(t::Transaction, a::Vector{UInt8}, nb::Int) + + if !isempty(t.c.excess) + l = length(t.c.excess) + copyto!(a, 1, t.c.excess, 1, min(l, nb)) + if l > nb + t.c.excess = view(t.c.excess, nb+1:l) + return nb + else + t.c.excess = nobytes + return l + readbytes!(t.c.io, view(a, l+1:nb)) + end + end + + return readbytes!(t.c.io, a, nb) +end + + """ unread!(::Transaction, bytes) @@ -469,7 +487,7 @@ function getconnection(::Type{Transaction{T}}, # Share a connection that has active readers... if !isempty(writable) c = rand(writable) ;@debug 2 "⇆ Shared: $c" - return client_transaction(T, c) + return client_transaction(c) end finally From 250b6ea2a27017c4e865ea2965dfa576364ff789 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 01:57:41 +1100 Subject: [PATCH 138/182] Messages.jl Fix case insensitiveness of hasheader Add RFC compliant bodylength(::Request/::Response) methods. See https://github.com/nodejs/http-parser/issues/403 Update readbody() to only use Parser for chunked bodies. --- src/Messages.jl | 92 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 20 deletions(-) diff --git a/src/Messages.jl b/src/Messages.jl index b1c84ff80..8525e82f7 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -10,7 +10,6 @@ in the case of HTTP Redirect. The Messages module defines `IO` `read` and `write` methods for Messages but it does not deal with URIs, creating connections, or executing requests. -The The `read` methods throw `EOFError` exceptions if input data is incomplete. and call parser functions that may throw `HTTP.ParsingError` exceptions. @@ -41,7 +40,7 @@ Headers are represented by `Vector{Pair{String,String}}`. As compared to `Dict{String,String}` this allows [repeated header fields and preservation of order](https://tools.ietf.org/html/rfc7230#section-3.2.2). -Header values can be accessed by name using +Header values can be accessed by name using [`HTTP.Messages.header`](@ref) and [`HTTP.Messages.setheader`](@ref) (case-insensitive). @@ -65,7 +64,8 @@ export Message, Request, Response, iserror, isredirect, ischunked, issafe, isidempotent, header, hasheader, setheader, defaultheader, appendheader, mkheaders, readheaders, headerscomplete, readtrailers, writeheaders, - readstartline!, writestartline + readstartline!, writestartline, + bodylength, unknown_length if VERSION > v"0.7.0-DEV.2338" using Unicode @@ -79,6 +79,9 @@ using ..Parsers import ..Parsers import ..Parsers: headerscomplete, reset! +const unknown_length = typemax(Int64) + + abstract type Message end """ @@ -98,12 +101,21 @@ mutable struct Response <: Message status::Int16 headers::Headers body::Vector{UInt8} - request + request::Message + + function Response(status::Int=0, headers=[]; body=UInt8[], request=nothing) + r = new() + r.version = v"1.1" + r.status = status + r.headers = mkheaders(headers) + r.body = body + if request != nothing + r.request = request + end + return r + end end -Response(status::Int=0, headers=[]; body=UInt8[], request=nothing) = - Response(v"1.1", status, mkheaders(headers), body, request) - Response(bytes) = parse(Response, bytes) function reset!(r::Response) @@ -230,7 +242,7 @@ hasheader(m, k::String) = header(m, k) != "" Does header for `key` match `value` (both case-insensitive)? """ -hasheader(m, k::String, v::String) = lowercase(header(m, k)) == v +hasheader(m, k::String, v::String) = lowercase(header(m, k)) == lowercase(v) """ @@ -262,7 +274,7 @@ end Does the `Message` have a "Transfer-Encoding: chunked" header? """ -ischunked(m) = header(m, "Transfer-Encoding") == "chunked" +ischunked(m) = hasheader(m, "Transfer-Encoding", "chunked") """ @@ -424,32 +436,72 @@ function readtrailers(io::IO, parser::Parser, message::Message) end +""" +"The presence of a message body in a response depends on both the + request method to which it is responding and the response status code. + Responses to the HEAD request method never include a message body []. + 2xx (Successful) responses to a CONNECT request method (Section 4.3.6 of + [RFC7231]) switch to tunnel mode instead of having a message body. + All 1xx (Informational), 204 (No Content), and 304 (Not Modified) + responses do not include a message body. All other responses do + include a message body, although the body might be of zero length." +[RFC7230 3.3](https://tools.ietf.org/html/rfc7230#section-3.3) +""" + +bodylength(r::Response)::Int = + r.request.method == "HEAD" ? 0 : + r.status in [204, 304] ? 0 : + (l = header(r, "Content-Length")) != "" ? parse(Int, l) : + unknown_length + + +""" +"The presence of a message body in a request is signaled by a + Content-Length or Transfer-Encoding header field. Request message + framing is independent of method semantics, even if the method does + not define any use for a message body." +[RFC7230 3.3](https://tools.ietf.org/html/rfc7230#section-3.3) +""" + +bodylength(r::Request)::Int = + ischunked(r) ? unknown_length : + parse(Int, header(r, "Content-Length", "0")) + + """ readbody(::IO, ::Parser) -> Vector{UInt8} Read message body from an `IO` stream. """ -function readbody(io::IO, parser::Parser) - body = IOBuffer() - while !bodycomplete(parser) && !eof(io) - data, excess = parsebody(parser, readavailable(io)) - write(body, data) - unread!(io, excess) +function readbody(io::IO, parser::Parser, m::Message) + if ischunked(m) + body = IOBuffer() + while !bodycomplete(parser) && !eof(io) + data, excess = parsebody(parser, readavailable(io)) + write(body, data) + unread!(io, excess) + end + m.body = take!(body) + else + l = bodylength(m) + m.body = read(io, l) + if l != unknown_length && length(m.body) < l + throw(EOFError()) + end end - return take!(body) end function Base.parse(::Type{T}, str::AbstractString) where T <: Message bytes = IOBuffer(str) p = Parser() - m = T() + r = Request() + m::T = T == Request ? r : r.response readheaders(bytes, p, m) - m.body = readbody(bytes, p) + readbody(bytes, p, m) readtrailers(bytes, p, m) - seteof(p) - if !messagecomplete(p) + if ischunked(m) && !messagecomplete(p) throw(EOFError()) end return m From e0d900a82123e8727b0bdad5a0c27be81ee09bd9 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 02:02:36 +1100 Subject: [PATCH 139/182] Revise Streams.jl to bypass parser for non-chunked bodies. readavailable(::HTTP.Stream) for non chunked messages is now a very thin wrapper for readavailable(::TCPSocket) (or readbytes!(::TCPSocket, ...) if nb_available() is more than Content-Length). Parser overhead is removed. !unread overhead is removed. --- src/Bodies.jl | 269 ------------------------------------------ src/IOExtras.jl | 21 ++++ src/MessageRequest.jl | 2 +- src/Streams.jl | 60 ++++++---- 4 files changed, 60 insertions(+), 292 deletions(-) delete mode 100644 src/Bodies.jl diff --git a/src/Bodies.jl b/src/Bodies.jl deleted file mode 100644 index a0f4cb951..000000000 --- a/src/Bodies.jl +++ /dev/null @@ -1,269 +0,0 @@ -module Bodies - -export Body, isstream, isstreamfresh - - -""" - Body - -Represents a HTTP Message Body. - -- `stream::IO` -- `buffer::IOBuffer` -- `length::Int` - -If `stream` is set to `notastream`, then `buffer` contains static Message Body data. -Otherwise, `stream` is a stream to/from which Message Body data is written/read. -In streaming mode: `length` keeps track of the number of bytes that have passed -through `stream`; and `buffer` keeps a cache of the first part of the Message Body -(for display purposes). See `show` and `set_show_max`). -""" - -mutable struct Body - stream::IO - buffer::IOBuffer - length::Int -end - -const notastream = IOBuffer("") -const unknownlength = -1 - - -""" - Body() - Body(data [, length]) - Body(::IO, [, length]) - -`Body()` creates an empty HTTP Message `Body` buffer. -The `write(::Body)` function can be used to append data to the empty `Body`. -The `write(::IO)` function can then be used to send the Body to an `IO` stream. -e.g. - -``` -b = Body() -write(b, "Hello\\n") -write(b, "World!\\n") -write(socket, b) -``` - -`Body(data)` creates a `Body` with fixed content. - -`Body(::IO)` creates a streaming mode `Body`. This can be used to stream either -Request Messages or Response Messages. `write(io, ::Body)` reads data from -the `Body`'s stream and writes it to `io`. `write(::Body, data)` writes -data from to the `Body`'s stream. - -If `length` is unknown, `write(io, body)` uses chunked Transfer-Encoding. - -e.g. Send a Request Body using chunked Transfer-Encoding: - -``` -io = open("bigfile.dat", "r") -write(socket, Body(io)) -``` - -e.g. Send a Request Body with known length: - -``` -io = open("bigfile.dat", "r") -write(socket, Body(io, filesize("bigfile.dat"))) -``` - -e.g. Send a Response Body to a stream: - -``` -io = open("response_file", "w") -b = Body(io) -while !eof(socket) - write(b, readavailable(socket)) -end -``` -""" - -Body() = Body(notastream, IOBuffer(), unknownlength) -Body(buffer::IOBuffer, l=unknownlength) = Body(notastream, buffer, l) -Body(stream::IO, l=unknownlength) = Body(stream, IOBuffer(body_show_max), l) -Body(::Void) = Body() -Body(data, l=unknownlength) = Body(IOBuffer(data), l) - - -""" - isstream(::Body) - -Is this `Body` in streaming mode? -""" - -isstream(b::Body) = b.stream != notastream - - -""" - isstreamfresh(::Body) - -False if there have been any reads/writes from/to the `Body`'s stream. -""" - -isstreamfresh(b::Body) = !isstream(b) || position(b.buffer) == 0 - - - -""" - length(::Body) - -Number of bytes in the body. -In streaming mode, number of bytes that have passed through the stream. -""" - -Base.length(b::Body) = isstream(b) ? b.length : b.buffer.size - - -""" - collect!(::Body) - -If the `Body` is in streaming mode, read the complete content of the stream -into the local buffer then close the stream. -Returns a `view` of the local buffer. -""" - -function collect!(body::Body) - if isstream(body) - io = IOBuffer() - write(io, body) - body.buffer = io - close(body.stream) - body.stream = notastream - end - @assert !isstream(body) - return view(body.buffer.data, 1:body.buffer.size) -end - - -""" - take!(::Body) - -Obtain the contents of `Body` and clear the internal buffer. -""" - -function Base.take!(body::Body) - collect!(body) - take!(body.buffer) -end - - -""" - write(::IO, ::Body) - -Write data from `Body`'s `buffer` or `stream` to an `IO` stream, -""" - -function Base.write(io::IO, body::Body) - - if !isstream(body) - if VERSION > v"0.7.0-DEV.2338" - bytes = view(body.buffer.data, 1:body.buffer.size) - else - bytes = body.buffer.data[1:body.buffer.size] - end - write(io, bytes) - return - end - - @assert isstreamfresh(body) - - # Use "chunked" encoding if length is unknown. - # https://tools.ietf.org/html/rfc7230#section-4.1 - if body.length == unknownlength - writechunked(io, body) - return - end - - # Read from `body.io` until `eof`, write to `io`. - while !eof(body.stream) - v = readavailable(body.stream) - if body.buffer.size < body_show_max - write(body.buffer, v) - end - write(io, v) - end - return -end - - -function writechunked(io::IO, body::Body) - while !eof(body.stream) - v = readavailable(body.stream) - if body.buffer.size < body_show_max - write(body.buffer, v) - end - write(io, hex(length(v)), "\r\n", v, "\r\n") - end - write(io, "0\r\n\r\n") - return -end - - -""" - write(::Body, data) - -Write data to the `Body`'s `stream`, -or append it to the `Body`'s `buffer`. -""" - -function Base.write(body::Body, v) - - if !isstream(body) - return write(body.buffer, v) - end - - if body.length < body_show_max - write(body.buffer, v) - end - n = write(body.stream, v) - body.length += n - return n -end - - -function Base.close(body::Body) - if isstream(body) - close(body.stream) - else - body.buffer.writable = false - end -end - -Base.isopen(body::Body) = - isstream(body) ? isopen(body.stream) : iswriteable(body.buffer) - - -""" - set_show_max(x) - -Set the maximum number of bytes to be displayed by `show(::IO, ::Body)` -""" - -set_show_max(x) = global body_show_max = x -body_show_max = 1000 - - -""" - head(::Body) - -The first chunk of the `Body` data (for display purposes). -""" -head(b::Body) = view(b.buffer.data, 1:min(b.buffer.size, body_show_max)) - -function Base.show(io::IO, body::Body) - bytes = head(body) - write(io, bytes) - println(io, "") - if isstream(body) && isopen(body.stream) - println(io, "⋮\nWaiting for $(typeof(body.stream))...") - elseif length(body) > length(bytes) - println(io, "⋮\n$(length(body))-byte body") - elseif length(body) == unknownlength - println(io, "⋮\nlength unknown (chunked)") - end -end - - -end #module Bodies diff --git a/src/IOExtras.jl b/src/IOExtras.jl index 9da85841c..7b9750ef6 100644 --- a/src/IOExtras.jl +++ b/src/IOExtras.jl @@ -44,6 +44,27 @@ Base.show(io::IO, e::IOError) = print(io, "IOError(", e.e, ")") Push bytes back into a connection (to be returned by the next read). """ + +function unread!(io::IOBuffer, bytes) + l = length(bytes) + if l == 0 + return + end + + @assert bytes == io.data[io.ptr - l:io.ptr-1] + + if io.seekable + seek(io, io.ptr - (l + 1)) + return + end + + println("WARNING: Can't unread! non-seekable IOBuffer") + println(" Discarding $(length(bytes)) bytes!") + @assert false + return +end + + function unread!(io, bytes) if length(bytes) == 0 return diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 065c3f19f..de2d463b1 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -5,6 +5,7 @@ export body_is_a_stream, body_was_streamed import ..Layer, ..request using ..URIs using ..Messages +import ..Messages.bodylength using ..Headers using ..minimal if !minimal @@ -46,7 +47,6 @@ function request(::Type{MessageLayer{Next}}, end -const unknown_length = -1 bodylength(body) = unknown_length bodylength(body::AbstractVector{UInt8}) = length(body) bodylength(body::AbstractString) = sizeof(body) diff --git a/src/Streams.jl b/src/Streams.jl index e0bc03d46..5e0b5db78 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -12,6 +12,7 @@ import ..Messages: header, hasheader, setheader, writeheaders, writestartline import ..ConnectionPool.getrawstream import ..@require, ..precondition_error +import ..@ensure, ..postcondition_error import ..@debug, ..DEBUG_LEVEL @@ -20,6 +21,8 @@ mutable struct Stream{M <: Message, S <: IO} <: IO parser::Parser stream::S writechunked::Bool + readchunked::Bool + ntoread::Int end @@ -50,7 +53,8 @@ Creates a `HTTP.Stream` that wraps an existing `IO` stream. an `EOFError`. """ -Stream(r::M, p::Parser, io::S) where {M, S} = Stream{M,S}(r, p, io, false) +Stream(r::M, p::Parser, io::S) where {M, S} = + Stream{M,S}(r, p, io, false, false, 0) header(http::Stream, a...) = header(http.message, a...) setstatus(http::Stream, status) = (http.message.response.status = status) @@ -64,6 +68,7 @@ IOExtras.isopen(http::Stream) = isopen(http.stream) messagetowrite(http::Stream{Response}) = http.message.request messagetowrite(http::Stream{Request}) = http.message.response + IOExtras.iswritable(http::Stream) = iswritable(http.stream) function IOExtras.startwrite(http::Stream) @@ -77,7 +82,7 @@ function IOExtras.startwrite(http::Stream) http.writechunked = true setheader(m, "Transfer-Encoding" => "chunked") else - http.writechunked = hasheader(m, "Transfer-Encoding", "chunked") + http.writechunked = ischunked(m) end writeheaders(http.stream, m) end @@ -137,10 +142,16 @@ end IOExtras.isreadable(http::Stream) = isreadable(http.stream) function IOExtras.startread(http::Stream) + startread(http.stream) - configure_parser(http) + + reset!(http.parser) readheaders(http.stream, http.parser, http.message) handle_continue(http) + + http.readchunked = ischunked(http.message) + http.ntoread = bodylength(http.message) + return http.message end @@ -171,26 +182,14 @@ function handle_continue(http::Stream{Request}) end -function configure_parser(http::Stream{Response}) - reset!(http.parser) - req = http.message.request::Request - if req.method in ("HEAD", "CONNECT") - setnobody(http.parser) - end -end - -configure_parser(http::Stream{Request}) = reset!(http.parser) - - function Base.eof(http::Stream) if !headerscomplete(http.message) startread(http) end - if bodycomplete(http.parser) + if http.ntoread == 0 return true end if eof(http.stream) - seteof(http.parser) return true end return false @@ -199,14 +198,31 @@ end function Base.readavailable(http::Stream)::ByteView @require headerscomplete(http.message) - @require !bodycomplete(http.parser) - bytes = readavailable(http.stream) - if isempty(bytes) + if http.ntoread == 0 + return nobytes + end + if nb_available(http.stream) > http.ntoread + raw = read(http.stream, http.ntoread) + bytes = view(raw, 1:length(raw)) + else + bytes = readavailable(http.stream) + end + l = length(bytes) + if l == 0 return nobytes end - bytes, excess = parsebody(http.parser, bytes) - unread!(http, excess) + if http.readchunked + bytes, excess = parsebody(http.parser, bytes) + unread!(http, excess) + if bodycomplete(http.parser) + http.ntoread = 0 + end + end + if http.ntoread != unknown_length + http.ntoread -= length(bytes) + end + @ensure http.ntoread >= 0 return bytes end @@ -258,7 +274,7 @@ function IOExtras.closeread(http::Stream{Response}) readtrailers(http.stream, http.parser, http.message) end - if !messagecomplete(http.parser) + if http.ntoread != unknown_length && http.ntoread > 0 # Error if Message is not complete... close(http.stream) throw(EOFError()) From 97409a97bea8c0516d698fe41cc7e69f02d214e4 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 02:10:22 +1100 Subject: [PATCH 140/182] use ConnectionPool.byteview conveniance fn in Base.readavailable(http::Stream) --- src/Streams.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Streams.jl b/src/Streams.jl index 5e0b5db78..ed0d7cdfc 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -10,7 +10,7 @@ using ..Parsers using ..Messages import ..Messages: header, hasheader, setheader, writeheaders, writestartline -import ..ConnectionPool.getrawstream +import ..ConnectionPool: getrawstream, byteview import ..@require, ..precondition_error import ..@ensure, ..postcondition_error import ..@debug, ..DEBUG_LEVEL @@ -203,8 +203,7 @@ function Base.readavailable(http::Stream)::ByteView return nobytes end if nb_available(http.stream) > http.ntoread - raw = read(http.stream, http.ntoread) - bytes = view(raw, 1:length(raw)) + bytes = byteview(read(http.stream, http.ntoread)) else bytes = readavailable(http.stream) end From fa380546775af885fefc0985d39d66a774cdf3d3 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 02:12:18 +1100 Subject: [PATCH 141/182] Remove redundant header interpretation from Parser. bodylength(::HTTP.Message) implements RFC compliant body lenth determination. Parser no longer needs to interpret Content-Length or "chunked" headers. No Need to "parse" non chunked boides. Just let the client read them directly from the stream. Update tests per: https://github.com/nodejs/http-parser/issues/403 --- src/consts.jl | 16 --- src/parser.jl | 331 ++++++++--------------------------------------- test/loopback.jl | 2 +- test/parser.jl | 88 ++++++------- test/runtests.jl | 2 +- test/server.jl | 3 + 6 files changed, 102 insertions(+), 340 deletions(-) diff --git a/src/consts.jl b/src/consts.jl index c30414379..feaacc545 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -257,28 +257,12 @@ for i in instances(ParsingStateCode) @eval const $(Symbol(string(i)[2:end])) = UInt8($i) end -# header states -const h_general = 0x00 - -const h_matching_content_length = 0x06 -const h_matching_transfer_encoding = 0x07 -const h_content_length = 0x0b -const h_transfer_encoding = 0x0c -const h_matching_transfer_encoding_chunked = 0x0f -const h_transfer_encoding_chunked = 0x15 - const CR = '\r' const bCR = UInt8('\r') const LF = '\n' const bLF = UInt8('\n') const CRLF = "\r\n" -const unknown_length = typemax(UInt64) - -const CONTENT_LENGTH = "content-length" -const TRANSFER_ENCODING = "transfer-encoding" -const CHUNKED = "chunked" - #= Tokens as defined by rfc 2616. Also lowercases them. # token = 1* # separators = "(" | ")" | "<" | ">" | "@" diff --git a/src/parser.jl b/src/parser.jl index f421e0d45..ffc73d105 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -30,8 +30,6 @@ export Parser, Header, Headers, ByteView, nobytes, parseheaders, parsebody, messagestarted, headerscomplete, bodycomplete, messagecomplete, messagehastrailing, - waitingforeof, seteof, - setnobody, ParsingError, ParsingErrorCode using ..URIs.parseurlchar @@ -39,6 +37,7 @@ using ..URIs.parseurlchar import MbedTLS.SSLContext import ..@debug, ..@debugshow, ..DEBUG_LEVEL +import ..@require, ..precondition_error include("consts.jl") include("parseutils.jl") @@ -101,16 +100,10 @@ Messages. mutable struct Parser - # config - message_has_no_body::Bool # Are we parsing a HEAD Response Message? - # state state::UInt8 - header_state::UInt8 - index::UInt8 - chunked::Bool + chunk_length::UInt64 trailing::Bool - content_length::UInt64 fieldbuffer::IOBuffer valuebuffer::IOBuffer @@ -134,23 +127,16 @@ Revert `Parser` to unconfigured state. """ function reset!(p::Parser) - p.message_has_no_body = false p.state = s_start_req_or_res + p.chunk_length = 0 + p.trailing = false + truncate(p.fieldbuffer, 0) + truncate(p.valuebuffer, 0) reset!(p.message) return p end -""" - setnobody(::Parser) - -Tell the `Parser` not to look for a Message Body. -e.g. for the Response to a HEAD Request. -""" - -setnobody(p::Parser) = p.message_has_no_body = true - - """ messagestarted(::Parser) @@ -188,27 +174,6 @@ Has the `Parser` processed the entire Message? messagecomplete(p::Parser) = p.state >= s_message_done -""" - waitingforeof(::Parser) - -Is the `Parser` waiting for the peer to close the connection -to signal the end of the Message Body? -""" -waitingforeof(p::Parser) = p.state == s_body_identity_eof - - -""" - seteof(::Parser) - -Signal that the peer has closed the connection. -""" -function seteof(p::Parser) - if p.state == s_body_identity_eof - p.state = s_message_done - end -end - - """ messagehastrailing(::Parser) @@ -271,11 +236,11 @@ macro methodstate(meth, i, char) return esc(:(Int($meth) << Int(16) | Int($i) << Int(8) | Int($char))) end -function parse_token(bytes, len, p, buffer) +function parse_token(bytes, len, p, buffer; allowed='a') start = p while p <= len @inbounds ch = Char(bytes[p]) - if !istoken(ch) + if !istoken(ch) && ch != allowed break end p += 1 @@ -314,9 +279,8 @@ end function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, parser::Parser, bytes::ByteView)::ByteView - isempty(bytes) && throw(ArgumentError("bytes must not be empty")) - !messagehastrailing(parser) && - headerscomplete(parser) && (ArgumentError("headers already complete")) + @require !isempty(bytes) + @require messagehastrailing(parser) || !headerscomplete(parser) len = length(bytes) p_state = parser.state @@ -335,14 +299,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if p_state == s_start_req_or_res (ch == CR || ch == LF) && continue - parser.header_state = h_general - parser.index = 0 - parser.content_length = unknown_length - parser.chunked = false - parser.trailing = false - truncate(parser.fieldbuffer, 0) - truncate(parser.valuebuffer, 0) - p_state = s_start_req p -= 1 @@ -411,7 +367,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_header_field_start else p_state = s_res_status - parser.index = 1 end elseif p_state == s_res_status @@ -570,8 +525,8 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, @errorif(ch != LF, HPE_LF_EXPECTED) p_state = s_header_field_start - elseif p_state == s_trailer_start || - p_state == s_header_field_start + elseif p_state == s_header_field_start || + p_state == s_trailer_start if ch == CR p_state = s_headers_almost_done elseif ch == LF @@ -582,74 +537,19 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, else c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] @errorif(c == Char(0), HPE_INVALID_HEADER_TOKEN) - parser.index = 1 p_state = s_header_field - - if c == 'c' - parser.header_state = h_matching_content_length - elseif c == 't' - parser.header_state = h_matching_transfer_encoding - else - parser.header_state = h_general - end - - write(parser.fieldbuffer, bytes[p]) + p -= 1 end elseif p_state == s_header_field - start = p - while p <= len - @inbounds ch = Char(bytes[p]) - @debug 4 Base.escape_string(string(ch)) - c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] - if c == Char(0) - @errorif(ch != ':', HPE_INVALID_HEADER_TOKEN) - break - end - @debugshow 4 parser.header_state - h = parser.header_state - if h == h_general - - # content-length - elseif h == h_matching_content_length - parser.index += 1 - if parser.index > length(CONTENT_LENGTH) || - c != CONTENT_LENGTH[parser.index] - parser.header_state = h_general - elseif parser.index == length(CONTENT_LENGTH) - parser.header_state = h_content_length - end - # transfer-encoding - elseif h == h_matching_transfer_encoding - parser.index += 1 - if parser.index > length(TRANSFER_ENCODING) || - c != TRANSFER_ENCODING[parser.index] - parser.header_state = h_general - elseif parser.index == length(TRANSFER_ENCODING) - parser.header_state = h_transfer_encoding - end - - elseif @anyeq(h, #=h_connection,=# h_content_length, - h_transfer_encoding#=, h_upgrade=#) - if ch != ' ' - parser.header_state = h_general - end - else - @err HPE_INVALID_INTERNAL_STATE - end - p += 1 - end - @passert p <= len + 1 - - if ch == ':' + p, complete = parse_token(bytes, len, p, parser.fieldbuffer; + allowed = ' ') + if complete + @inbounds ch = Char(bytes[p]) + @errorif(ch != ':', HPE_INVALID_HEADER_TOKEN) p_state = s_header_value_discard_ws - else - @passert !strict && ch == ' ' || istoken(ch) end - write(parser.fieldbuffer, view(bytes, start:p-1)) - - p = min(p, len) elseif p_state == s_header_value_discard_ws (ch == ' ' || ch == '\t') && continue @@ -665,28 +565,12 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p -= 1 elseif p_state == s_header_value_start p_state = s_header_value - parser.index = 1 c = lower(ch) - if parser.header_state == h_transfer_encoding - # looking for 'Transfer-Encoding: chunked' - parser.header_state = ifelse( - c == 'c', h_matching_transfer_encoding_chunked, h_general) - - elseif parser.header_state == h_content_length - @errorif(!isnum(ch), HPE_INVALID_CONTENT_LENGTH) - @errorif(parser.content_length != unknown_length, - HPE_UNEXPECTED_CONTENT_LENGTH) - parser.content_length = UInt64(ch - '0') - - else - parser.header_state = h_general - end write(parser.valuebuffer, bytes[p]) elseif p_state == s_header_value start = p - h = parser.header_state while p <= len @inbounds ch = Char(bytes[p]) @debug 4 Base.escape_string(string('\'', ch, '\'')) @@ -705,59 +589,14 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, c = lower(ch) @debugshow 4 h - if h == h_general - crlf = findfirst(x->(x == bCR || x == bLF), - view(bytes, p:len)) - p = crlf == 0 ? len : p + crlf - 2 - - elseif h == #=h_connection ||=# h == h_transfer_encoding - @err HPE_INVALID_INTERNAL_STATE - elseif h == h_content_length - t = UInt64(0) - if ch == ' ' - else - if !isnum(ch) - parser.header_state = h - @err(HPE_INVALID_CONTENT_LENGTH) - end - t = parser.content_length - t *= UInt64(10) - t += UInt64(ch - '0') - - # Overflow? - # Test against a conservative limit for simplicity. - @debugshow 4 Int(parser.content_length) - if div(typemax(UInt64) - 10, 10) < t - parser.header_state = h - @err(HPE_INVALID_CONTENT_LENGTH) - end - parser.content_length = t - end - - # Transfer-Encoding: chunked - elseif h == h_matching_transfer_encoding_chunked - parser.index += 1 - if parser.index > length(CHUNKED) || - c != CHUNKED[parser.index] - h = h_general - elseif parser.index == length(CHUNKED) - h = h_transfer_encoding_chunked - end + crlf = findfirst(x->(x == bCR || x == bLF), + view(bytes, p:len)) + p = crlf == 0 ? len : p + crlf - 2 - elseif h == h_transfer_encoding_chunked - if ch != ' ' - h = h_general - end - else - p_state = s_header_value - h = h_general - end p += 1 end @passert p <= len + 1 - parser.header_state = h - write(parser.valuebuffer, view(bytes, start:p-1)) if p_state != s_header_value @@ -777,9 +616,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_header_value_start else # finished the header - if parser.header_state == h_transfer_encoding_chunked - parser.chunked = true - end p_state = s_header_field_start end @@ -791,10 +627,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if ch == ' ' || ch == '\t' p_state = s_header_value_discard_ws else - if parser.header_state == h_transfer_encoding_chunked - parser.chunked = true - end - # header value was empty p_state = s_header_field_start onheader(String(take!(parser.fieldbuffer)) => "") @@ -808,39 +640,13 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, # End of a chunked request p_state = s_message_done else - - # Cannot use chunked encoding and a content-length header - # together per the HTTP specification. - @errorif(parser.chunked && - parser.content_length != unknown_length, - HPE_UNEXPECTED_CONTENT_LENGTH) - p_state = s_headers_done end elseif p_state == s_headers_done @errorifstrict(ch != LF) - if parser.message_has_no_body || - parser.content_length == 0 || - parser.message.method == "CONNECT" - p_state = s_message_done - elseif parser.chunked - # chunked encoding - ignore Content-Length header - p_state = s_chunk_size_start - elseif parser.content_length != unknown_length - # Content-Length header given and non-zero - p_state = s_body_identity - elseif isrequest(parser) || # RFC 7230, 3.3.3, 6. - div(parser.message.status, 100) == 1 || # 1xx e.g. Continue - parser.message.status == 204 || # No Content - parser.message.status == 304 # Not Modified - p_state = s_message_done # =>Content-1ength: 0 - else - # Read body until EOF - p_state = s_body_identity_eof - end - + p_state = s_body_start else @err HPE_INVALID_INTERNAL_STATE end @@ -849,9 +655,19 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, @assert p <= len @assert p == len || p_state == s_message_done || - p_state == s_chunk_size_start || - p_state == s_body_identity || - p_state == s_body_identity_eof + p_state == s_body_start + + + # Consume trailing end of line after message. + if p_state == s_message_done + while p < len + ch = Char(bytes[p + 1]) + if ch != CR && ch != LF + break + end + p += 1 + end + end @debug 3 "parseheaders() exiting $(ParsingStateCode(p_state))" @@ -874,8 +690,12 @@ end function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} - isempty(bytes) && throw(ArgumentError("bytes must not be empty")) - !headerscomplete(parser) && throw(ArgumentError("headers not complete")) + @require !isempty(bytes) + @require headerscomplete(parser) + + if parser.state == s_body_start + parser.state = s_chunk_size_start + end len = length(bytes) p_state = parser.state @@ -885,8 +705,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} result = nobytes p = 0 - while p < len && result == nobytes && p_state < s_message_done && - p_state != s_trailer_start + while p < len && result == nobytes && p_state != s_trailer_start @debug 4 string("top of while($p < $len) \"", Base.escape_string(string(Char(bytes[p+1]))), "\" ", @@ -894,37 +713,15 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} p += 1 @inbounds ch = Char(bytes[p]) - if p_state == s_body_identity - to_read = Int(min(parser.content_length, len - p + 1)) - @passert parser.content_length != 0 && - parser.content_length != unknown_length - - @passert result == nobytes - result = view(bytes, p:p + to_read - 1) - parser.content_length -= to_read - p += to_read - 1 - - if parser.content_length == 0 - p_state = s_message_done - end - - # read until EOF - elseif p_state == s_body_identity_eof - @passert result == nobytes - result = bytes - p = len - - elseif p_state == s_chunk_size_start - @passert parser.chunked + if p_state == s_chunk_size_start unhex_val = unhex[Int(ch)+1] @errorif(unhex_val == -1, HPE_INVALID_CHUNK_SIZE) - parser.content_length = unhex_val + parser.chunk_length = unhex_val p_state = s_chunk_size elseif p_state == s_chunk_size - @passert parser.chunked if ch == CR p_state = s_chunk_size_almost_done else @@ -937,60 +734,54 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} end @err(HPE_INVALID_CHUNK_SIZE) end - t = parser.content_length + t = parser.chunk_length t *= UInt64(16) t += UInt64(unhex_val) # Overflow? Test against a conservative limit for simplicity. - @debugshow 4 Int(parser.content_length) + @debugshow 4 Int(parser.chunk_length) if div(typemax(UInt64) - 16, 16) < t @err(HPE_INVALID_CONTENT_LENGTH) end - parser.content_length = t + parser.chunk_length = t end elseif p_state == s_chunk_parameters - @passert parser.chunked # just ignore this?. FIXME check for overflow? if ch == CR p_state = s_chunk_size_almost_done end elseif p_state == s_chunk_size_almost_done - @passert parser.chunked @errorifstrict(ch != LF) - if parser.content_length == 0 - parser.trailing = 1 + if parser.chunk_length == 0 + parser.trailing = true p_state = s_trailer_start else p_state = s_chunk_data end elseif p_state == s_chunk_data - to_read = Int(min(parser.content_length, len - p + 1)) + to_read = Int(min(parser.chunk_length, len - p + 1)) - @passert parser.chunked - @passert parser.content_length != 0 && - parser.content_length != unknown_length + @passert parser.chunk_length != 0 && @passert result == nobytes result = view(bytes, p:p + to_read - 1) - parser.content_length -= to_read + parser.chunk_length -= to_read p += to_read - 1 - if parser.content_length == 0 + if parser.chunk_length == 0 p_state = s_chunk_data_almost_done end elseif p_state == s_chunk_data_almost_done - @passert parser.chunked - @passert parser.content_length == 0 + @passert parser.chunk_length == 0 @errorifstrict(ch != CR) p_state = s_chunk_data_done elseif p_state == s_chunk_data_done - @passert parser.chunked @errorifstrict(ch != LF) p_state = s_chunk_size_start @@ -999,21 +790,9 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} end end - # Consume trailing end of line after message. - if p_state == s_message_done - while p < len - ch = Char(bytes[p + 1]) - if ch != CR && ch != LF - break - end - p += 1 - end - end - @assert p <= len @assert p == len || result != nobytes || - p_state == s_message_done || p_state == s_trailer_start @debug 3 "parsebody() exiting $(ParsingStateCode(p_state))" @@ -1025,9 +804,7 @@ end Base.show(io::IO, p::Parser) = print(io, "Parser(", "state=", ParsingStateCode(p.state), ", ", - "chunked=", p.chunked, ", ", "trailing=", p.trailing, ", ", - "content_length=", p.content_length, ", ", "message=", p.message, ")") end # module Parsers diff --git a/test/loopback.jl b/test/loopback.jl index 443ec7421..524f2ea05 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -84,7 +84,7 @@ function on_body(f, lb) @schedule try f(req) catch e - println("⚠️ on_body exception: $e") + println("⚠️ on_body exception: $(sprint(showerror, e))\n$(catch_stacktrace())") end end end diff --git a/test/parser.jl b/test/parser.jl index 57dd2d09f..7e7a238fb 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -20,26 +20,6 @@ const Headers = Vector{Pair{String,String}} (a.body == b.body) -function HTTP.IOExtras.unread!(io::IOBuffer, bytes) - l = length(bytes) - if l == 0 - return - end - - @assert bytes == io.data[io.ptr - l:io.ptr-1] - - if io.seekable - seek(io, io.ptr - (l + 1)) - return - end - - println("WARNING: Can't unread! non-seekable IOBuffer") - println(" Discarding $(length(bytes)) bytes!") - @assert false - return -end - - function HTTP.IOExtras.unread!(io::BufferStream, bytes) if length(bytes) == 0 return @@ -54,7 +34,18 @@ function HTTP.IOExtras.unread!(io::BufferStream, bytes) return end -function parse!(parser::Parser, message::Messages.Message, body, bytes)::Int +function Base.length(io::IOBuffer) + mark(io) + seek(io, 0) + n = nb_available(io) + reset(io) + return n +end + +parse!(parser::Parser, message::Messages.Message, body, bytes)::Int = + parse!(parser, message, body, Vector{UInt8}(bytes)) + +function parse!(parser::Parser, message::Messages.Message, body, bytes::Vector{UInt8})::Int l = length(bytes) count = 0 @@ -65,12 +56,19 @@ function parse!(parser::Parser, message::Messages.Message, body, bytes)::Int end readstartline!(parser.message, message) else - fragment, excess = parsebody(parser, bytes) - write(body, fragment) + if ischunked(message) + fragment, excess = parsebody(parser, bytes) + write(body, fragment) + else + n = min(length(bytes), bodylength(message) - length(body)) + write(body, view(bytes, 1:n)) + count += n + break + end end count += length(bytes) - length(excess) bytes = excess - if messagecomplete(parser) + if ischunked(message) && messagecomplete(parser) break end end @@ -116,18 +114,6 @@ function Message(; name::String="", kwargs...) end -#= -FIXME request tests for: - - No response body for 100 <= r.status < 200 || - r.status == 204 || - r.status == 304 || - method(r) in ("HEAD", "CONNECT") - - = No request body for method(r) in ("GET", "HEAD", "CONNECT") -=# - - - #= * R E Q U E S T S * =# const requests = Message[ @@ -787,12 +773,12 @@ Message(name= "curl get" ,host="foo.bar.com" ,port="443" ,num_headers= 3 -,upgrade="blarfcicle" +,upgrade="" ,headers=[ "User-Agent"=> "Mozilla/1.1N" , "Proxy-Authorization"=> "basic aGVsbG86d29ybGQ=" , "Content-Length"=> "10" ] -,body= "" +,body= "blarfcicle" ), Message(name = "link request" ,raw= "LINK /images/my_dog.jpg HTTP/1.1\r\n" * "Host: example.com\r\n" * @@ -1552,6 +1538,7 @@ const responses = Message[ @test Request(reqstr) == req +#= FIXME reqstr = "POST / HTTP/1.1\r\n" * "Host: foo.com\r\n" * "Transfer-Encoding: chunked\r\n" * @@ -1562,6 +1549,7 @@ const responses = Message[ "\r\n" @test_throws ParsingError Request(reqstr) +=# reqstr = "CONNECT www.google.com:443 HTTP/1.1\r\n\r\n" @@ -1651,7 +1639,7 @@ const responses = Message[ println("TEST - parser.jl - Response $t: $(resp.name)") try if t > 0 - r = Response() + r = Request().response p = Parser() b = IOBuffer() bytes = Vector{UInt8}(resp.raw) @@ -1769,6 +1757,7 @@ const responses = Message[ @test r.status == 200 @test r.headers == ["Content-Length"=>"1844674407370955160"] +#= respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551615" * "\r\n\r\n" e = try Response(respstr) catch e e end @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH @@ -1776,6 +1765,7 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Content-Length: " * "18446744073709551616" * "\r\n\r\n" e = try Response(respstr) catch e e end @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH +=# respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFE" * "\r\n..." r = Response() @@ -1792,6 +1782,7 @@ const responses = Message[ @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH for len in (1000, 100000) + b = IOBuffer() HTTP.Parsers.reset!(p) reqstr = "POST / HTTP/1.0\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" r = Request() @@ -1806,13 +1797,15 @@ const responses = Message[ end parse!(p, r, b, "a") @test headerscomplete(p) - @test messagecomplete(p) +# @test messagecomplete(p) + @test length(take!(b)) == len end for len in (1000, 100000) + b = IOBuffer() HTTP.Parsers.reset!(p) respstr = "HTTP/1.0 200 OK\r\nConnection: Keep-Alive\r\nContent-Length: $len\r\n\r\n" - r = Response() + r = Request().response p = Parser() parse!(p, r, b, respstr) @test headerscomplete(p) @@ -1824,27 +1817,32 @@ const responses = Message[ end parse!(p, r, b, "a") @test headerscomplete(p) - @test messagecomplete(p) +# @test messagecomplete(p) + @test length(take!(b)) == len end + b = IOBuffer() reqstr = requests[1].raw * requests[2].raw r = Request() p = Parser() n = parse!(p, r, b, reqstr) @test headerscomplete(p) - @test messagecomplete(p) + #@test messagecomplete(p) + @test String(take!(b)) == requests[1].body + b = IOBuffer() ex = Vector{UInt8}(reqstr)[n+1:end] HTTP.Parsers.reset!(p) parse!(p, r, b, ex) @test headerscomplete(p) - @test messagecomplete(p) + #@test messagecomplete(p) + @test String(take!(b)) == requests[2].body @test_throws ParsingError Request("GET / HTP/1.1\r\n\r\n") r = Request("GET / HTTP/1.1\r\n" * "Test: Düsseldorf\r\n\r\n") @test r.headers == ["Test" => "Düsseldorf"] - r = Response() + r = Request().response p = Parser() b = IOBuffer() parse!(p, r, b, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") diff --git a/test/runtests.jl b/test/runtests.jl index 0a0d1847d..2353e1b74 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -29,6 +29,6 @@ end include("messages.jl"); include("client.jl"); -# include("handlers.jl") + include("handlers.jl") # include("server.jl") end; diff --git a/test/server.jl b/test/server.jl index c641900a6..820775712 100644 --- a/test/server.jl +++ b/test/server.jl @@ -1,3 +1,6 @@ +using HTTP +using Test + @testset "HTTP.serve" begin # test kill switch From 9b5191f571a1930329899921e4fb86577fb5020c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 16:24:09 +1100 Subject: [PATCH 142/182] https://github.com/JuliaWeb/HTTP.jl/pull/135#pullrequestreview-88370194 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 610e1b1a1..cb42ec219 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ os: # - osx julia: - 0.6 - - 0.7 + - nightly notifications: email: false after_success: From 09a508cf16dc8752f9d80cb0c8cc8c0ba127b5f5 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 16:30:29 +1100 Subject: [PATCH 143/182] https://github.com/JuliaWeb/HTTP.jl/pull/135#pullrequestreview-88370960 --- src/AWS4AuthRequest.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index a5cc86e0b..da6c7a94e 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -153,4 +153,4 @@ function dot_aws_credentials()::NamedTuple end -end # module BasicAuthRequest +end # module AWS4AuthRequest From 058e2e40c8e2efe7e191f7608262115f14998d22 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 16:36:33 +1100 Subject: [PATCH 144/182] doc tweak --- src/parser.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser.jl b/src/parser.jl index ffc73d105..ce9d6a55c 100644 --- a/src/parser.jl +++ b/src/parser.jl @@ -52,7 +52,7 @@ const Header = Pair{String,String} const Headers = Vector{Header} """ - - `method::Method`: internal parser `@enum` for HTTP method. + - `method::String`: the HTTP method - `major` and `minor`: HTTP version - `url::String`: request URL - `status::Int`: response status From 2acab0900bf6092395907437837161867498fa24 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 16:40:04 +1100 Subject: [PATCH 145/182] typo --- src/ConnectionPool.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 2adc3c95a..a14e78b7c 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -55,7 +55,7 @@ A `TCPSocket` or `SSLContext` connection to a HTTP `host` and `port`. Fields: - `host::String` - `port::String`, exactly as specified in the URI (i.e. may be empty). -- `pipeline_linit`, number of requests to send before waiting for responses. +- `pipeline_limit`, number of requests to send before waiting for responses. - `peerport`, remote TCP port number (used for debug messages). - `localport`, local TCP port number (used for debug messages). - `io::T`, the `TCPSocket` or `SSLContext. From cb9ad328c7348d811147c1bd511a5b5be3b0fb04 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 17:32:33 +1100 Subject: [PATCH 146/182] https://github.com/JuliaWeb/HTTP.jl/pull/135#pullrequestreview-88372788 https://github.com/JuliaWeb/HTTP.jl/pull/135#pullrequestreview-88373024 --- src/ConnectionPool.jl | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index a14e78b7c..7df26607e 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -67,7 +67,7 @@ Fields: - `writedone`, signal that `writecount` was incremented. - `readcount`, number of Messages that have been read. - `readdone`, signal that `readcount` was incremented. -- `timestamp, time data was last recieved. +- `timestamp`, time data was last recieved. - `parser::Parser`, reuse a `Parser` when this `Connection` is reused. """ @@ -109,7 +109,7 @@ Connection(host::AbstractString, port::AbstractString, pipeline_limit::Int, io::T) where T <: IO = Connection{T}(host, port, pipeline_limit, peerport(io), localport(io), - io, view(UInt8[], 1:0), + io, nobytes, -1, 0, false, Condition(), 0, false, Condition(), @@ -429,10 +429,8 @@ end Remove closed connections from `pool`. """ function purge() - while (i = findfirst(x->!isopen(x.io), pool)) > 0 - c = pool[i] - deleteat!(pool, i) ;@debug 1 "🗑 Deleted: $c" - end + isdeletable(c) = !isopen(c.io) && (@debug 1 "🗑 Deleted: $c"; true) + deleteat!(pool, map(isdeletable, pool)) end @@ -510,14 +508,19 @@ function getconnection(::Type{TCPSocket}, connect(getaddrinfo(host), p) end +const nosslconfig = SSLConfig() function getconnection(::Type{SSLContext}, host::AbstractString, port::AbstractString; - require_ssl_verification::Bool=false, - sslconfig::SSLConfig=SSLConfig(require_ssl_verification), + require_ssl_verification::Bool=true, + sslconfig::SSLConfig=nosslconfig, kw...)::SSLContext + if sslconfig === nosslconfig + sslconfig = SSLConfig(require_ssl_verification) + end + port = isempty(port) ? "443" : port @debug 2 "SSL connect: $host:$port..." io = SSLContext() From 499b752128e9c99427ab6408793ecea541e97b0a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 17:46:10 +1100 Subject: [PATCH 147/182] doc fix --- src/HTTP.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/HTTP.jl b/src/HTTP.jl index 2d54a2a9f..d95a21125 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -234,6 +234,7 @@ r = HTTP.open("GET", "http://httpbin.org/stream/10") do io end end +using HTTP.IOExtras HTTP.open("GET", "https://tinyurl.com/bach-cello-suite-1-ogg") do http n = 0 r = startread(http) From cb1b8505eef94916f451e302a8665b02c432c48d Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 17:48:46 +1100 Subject: [PATCH 148/182] doc tweaks --- src/HTTP.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index d95a21125..0657dc871 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -168,25 +168,25 @@ Cananoincalization options String body: ```julia -request("POST", "http://httpbin.org/post", [], "post body data") +HTTP.request("POST", "http://httpbin.org/post", [], "post body data") ``` Stream body from file: ```julia io = open("post_data.txt", "r") -request("POST", "http://httpbin.org/post", [], io) +HTTP.request("POST", "http://httpbin.org/post", [], io) ``` Generator body: ```julia chunks = ("chunk\$i" for i in 1:1000) -request("POST", "http://httpbin.org/post", [], chunks) +HTTP.request("POST", "http://httpbin.org/post", [], chunks) ``` Collection body: ```julia chunks = [preamble_chunk, data_chunk, checksum(data_chunk)] -request("POST", "http://httpbin.org/post", [], chunks) +HTTP.request("POST", "http://httpbin.org/post", [], chunks) ``` `open() do io` body: @@ -203,14 +203,14 @@ end String body: ```julia -r = request("GET", "http://httpbin.org/get") +r = HTTP.request("GET", "http://httpbin.org/get") println(String(r.body)) ``` Stream body to file: ```julia io = open("get_data.txt", "w") -r = request("GET", "http://httpbin.org/get", response_stream=io) +r = HTTP.request("GET", "http://httpbin.org/get", response_stream=io) close(io) println(read("get_data.txt")) ``` @@ -222,7 +222,7 @@ io = BufferStream() bytes = readavailable(io)) println("GET data: \$bytes") end -r = request("GET", "http://httpbin.org/get", response_stream=io) +r = HTTP.request("GET", "http://httpbin.org/get", response_stream=io) close(io) ``` @@ -255,7 +255,7 @@ end String bodies: ```julia -r = request("POST", "http://httpbin.org/post", [], "post body data") +r = HTTP.request("POST", "http://httpbin.org/post", [], "post body data") println(String(r.body)) ``` @@ -263,7 +263,7 @@ Stream bodies from and to files: ```julia in = open("foo.png", "r") out = open("foo.jpg", "w") -request("POST", "http://convert.com/png2jpg", [], in, response_stream=out) +HTTP.request("POST", "http://convert.com/png2jpg", [], in, response_stream=out) ``` Stream bodies through: `open() do io`: From bb96146161d5c9ce136099a4c921145bf331beed Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 17:50:34 +1100 Subject: [PATCH 149/182] doc tweak --- src/HTTP.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index 0657dc871..dac5d5f2a 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -235,6 +235,7 @@ r = HTTP.open("GET", "http://httpbin.org/stream/10") do io end using HTTP.IOExtras + HTTP.open("GET", "https://tinyurl.com/bach-cello-suite-1-ogg") do http n = 0 r = startread(http) @@ -268,12 +269,14 @@ HTTP.request("POST", "http://convert.com/png2jpg", [], in, response_stream=out) Stream bodies through: `open() do io`: ```julia +using HTTP.IOExtras + HTTP.open("POST", "http://music.com/play") do io write(io, JSON.json([ "auth" => "12345XXXX", "song_id" => 7, ])) - r = readresponse(io) + r = startread(io) @show r.status while !eof(io) bytes = readavailable(io)) From c02b21b5697159db5ce80389c7b3f37d563b651c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 18:01:52 +1100 Subject: [PATCH 150/182] remove redundant import in Messages.jl --- src/Messages.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Messages.jl b/src/Messages.jl index 8525e82f7..750e128f1 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -76,7 +76,6 @@ import ..HTTP using ..Pairs using ..IOExtras using ..Parsers -import ..Parsers import ..Parsers: headerscomplete, reset! const unknown_length = typemax(Int64) From a8f4f730b8b203327efc8e12aed731e7b324aead Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Fri, 12 Jan 2018 18:44:26 +1100 Subject: [PATCH 151/182] typos, and add escapepath to URIs module --- src/AWS4AuthRequest.jl | 6 +----- src/Messages.jl | 2 +- src/Streams.jl | 4 ++-- src/server.jl | 2 +- src/uri.jl | 7 ++++++- test/messages.jl | 4 ++-- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index da6c7a94e..a629c0466 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -38,10 +38,6 @@ function request(::Type{AWS4AuthLayer{Next}}, end -ispathsafe(c::Char) = c == '/' || URIs.issafe(c) -escape_path(path) = escapeuri(path, ispathsafe) - - function sign_aws4!(method::String, uri::URI, headers::Headers, @@ -96,7 +92,7 @@ function sign_aws4!(method::String, # Create hash of canonical request... canonical_form = string(method, "\n", aws_service == "s3" ? uri.path - : escape_path(uri.path), "\n", + : escapepath(uri.path), "\n", escapeuri(query), "\n", join(sort(canonical_headers), "\n"), "\n\n", signed_headers, "\n", diff --git a/src/Messages.jl b/src/Messages.jl index 750e128f1..46fccbaa1 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -283,7 +283,7 @@ Append a header value to `message.headers`. If `key` is `""` the `value` is appended to the value of the previous header. -If `key` is the same as the previous header, the `vale` is [appended to the +If `key` is the same as the previous header, the `value` is [appended to the value of the previous header with a comma delimiter](https://stackoverflow.com/a/24502264) diff --git a/src/Streams.jl b/src/Streams.jl index ed0d7cdfc..21abe15a1 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -32,7 +32,7 @@ end Creates a `HTTP.Stream` that wraps an existing `IO` stream. - `startwrite(::Stream)` sends the `Request` headers to the `IO` stream. - - `write(::Stream, body)` sends the `body` (or a chunk of the bocdy). + - `write(::Stream, body)` sends the `body` (or a chunk of the body). - `closewrite(::Stream)` sends the final `0` chunk (if needed) and calls `closewrite` on the `IO` stream. When the `IO` stream is a [`HTTP.ConnectionPool.Transaction`](@ref), calling `closewrite` releases @@ -239,7 +239,7 @@ end """ isaborted(::Stream{Response}) -Has the server signalled that it does not wish to receive the message body? +Has the server signaled that it does not wish to receive the message body? "If [the response] indicates the server does not wish to receive the message body and is closing the connection, the client SHOULD diff --git a/src/server.jl b/src/server.jl index 77d9bb23a..3146cbcb4 100644 --- a/src/server.jl +++ b/src/server.jl @@ -79,7 +79,7 @@ ServerOptions(; tlsconfig::HTTP.MbedTLS.SSLConfig=HTTP.MbedTLS.SSLConfig(true), An http/https server. Supports listening on a `host` and `port` via the `HTTP.serve(server, host, port)` function. `handler` is a function of the form `f(::Request, ::Response) -> HTTP.Response`, i.e. it takes both a `Request` and pre-built `Response` -objects as inputs and returns the, potentially modified, `Respose`. `logger` indicates where logging output should be directed. +objects as inputs and returns the, potentially modified, `Response`. `logger` indicates where logging output should be directed. When `HTTP.serve` is called, it aims to "never die", catching and recovering from all internal errors. To forcefully stop, one can obviously kill the julia process, interrupt (ctrl/cmd+c) if main task, or send the kill signal over a server in channel like: `put!(server.in, HTTP.KILL)`. diff --git a/src/uri.jl b/src/uri.jl index 825539369..3763934fd 100644 --- a/src/uri.jl +++ b/src/uri.jl @@ -8,7 +8,8 @@ import Base.== include("urlparser.jl") -export URI, URL, hostport, resource, queryparams, absuri, escapeuri, unescapeuri +export URI, URL, hostport, resource, queryparams, absuri, + escapeuri, unescapeuri, escapepath """ HTTP.URL(host; userinfo="", path="", query="", fragment="", isconnect=false) @@ -220,6 +221,10 @@ function unescapeuri(str) return String(take!(out)) end +ispathsafe(c::Char) = c == '/' || issafe(c) +escapepath(path) = escapeuri(path, ispathsafe) + + """ Splits the path into components See: http://tools.ietf.org/html/rfc3986#section-3.3 diff --git a/test/messages.jl b/test/messages.jl index 0f6ba66f2..68942b7e4 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -14,13 +14,13 @@ using HTTP.StatusError using HTTP.MessageRequest.bodylength using HTTP.MessageRequest.bodybytes -using HTTP.MessageRequest.unknownlength +using HTTP.MessageRequest.unknown_length using JSON @testset "HTTP.Messages" begin - @test bodylength(7) == unknownlength + @test bodylength(7) == unknown_length @test bodylength(UInt8[1,2,3]) == 3 @test bodylength(view(UInt8[1,2,3], 1:2)) == 2 @test bodylength("Hello") == 5 From e32604b212dbc4c36df903341c22e31c55f2b3cd Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 08:41:00 +1100 Subject: [PATCH 152/182] Rename Nitrogren -> Servers https://github.com/JuliaWeb/HTTP.jl/commit/042dab6e9c0b5172a409ac96c521429aca7d979c#r26814240 Add logging macros to compat.jl --- src/HTTP.jl | 2 +- src/compat.jl | 34 ++++++++++++++-------------------- src/server.jl | 6 +++++- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index dac5d5f2a..283a1effd 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -583,7 +583,7 @@ include("WebSockets.jl") ;using .WebSockets include("client.jl") include("sniff.jl") include("handlers.jl"); using .Handlers -include("server.jl"); using .Nitrogen.listen +include("server.jl"); using .Servers.listen end include("precompile.jl") diff --git a/src/compat.jl b/src/compat.jl index aed091b30..66b318475 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -1,9 +1,19 @@ -if VERSION > v"0.7.0-DEV.2338" - using Base64 -end - @static if VERSION >= v"0.7.0-DEV.2915" + + using Base64 using Unicode + import Dates + +else # Julia v0.6 + + const Dates = Base.Dates + pairs(x) = [k => v for (k,v) in x] + + macro debug(s) DEBUG_LEVEL > 0 ? :(("D- ", $(esc(s)))) : :() end + macro info(s) DEBUG_LEVEL > 0 ? :(println("I- ", $(esc(s)))) : :() end + macro warn(s) DEBUG_LEVEL > 0 ? :(println("W- ", $(esc(s)))) : :() end + macro error(s, a...) DEBUG_LEVEL > 0 ? :(println("E- ", $(esc((s, a...))))) : :() end + end macro uninit(expr) @@ -13,25 +23,9 @@ macro uninit(expr) return esc(expr) end -if !isdefined(Base, :pairs) - pairs(x) = x -end - if !isdefined(Base, :Nothing) const Nothing = Void const Cvoid = Void end -if VERSION < v"0.7.0-DEV.2575" - const Dates = Base.Dates -else - import Dates -end - -@static if VERSION >= v"0.7.0-DEV.2915" - lockedby(l) = l.locked_by -else - lockedby(l) = get(l.locked_by) -end - Base.String(x::SubArray{UInt8,1}) = String(Vector{UInt8}(x)) diff --git a/src/server.jl b/src/server.jl index 3146cbcb4..d600c9f0c 100644 --- a/src/server.jl +++ b/src/server.jl @@ -1,4 +1,4 @@ -module Nitrogen +module Servers using ..IOExtras using ..Streams @@ -8,6 +8,10 @@ using ..ConnectionPool import ..@debug, ..@debugshow, ..DEBUG_LEVEL using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! +if VERSION < v"0.7.0-DEV.2575" +import ..@info, ..@warn, ..@error +end + if !isdefined(Base, :Nothing) const Nothing = Void From aa792e5198eed7c8d6d0c8bdf5e907b0acf7fb0f Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 08:50:41 +1100 Subject: [PATCH 153/182] 0.6 tweaks Rename some files to match module names. --- src/ConnectionPool.jl | 2 ++ src/HTTP.jl | 10 +++++----- src/{parser.jl => Parsers.jl} | 0 src/{server.jl => Servers.jl} | 0 src/Streams.jl | 2 +- src/TimeoutRequest.jl | 2 +- src/{uri.jl => URIs.jl} | 0 src/debug.jl | 1 - test/async.jl | 20 ++++++++++++++++---- test/client.jl | 2 +- test/handlers.jl | 3 +++ test/loopback.jl | 12 +++++++----- test/messages.jl | 6 ++++-- test/parser.jl | 4 ++++ test/runtests.jl | 5 ++++- test/server.jl | 4 ++-- 16 files changed, 50 insertions(+), 23 deletions(-) rename src/{parser.jl => Parsers.jl} (100%) rename src/{server.jl => Servers.jl} (100%) rename src/{uri.jl => URIs.jl} (100%) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 7df26607e..21fe1e45b 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -182,6 +182,8 @@ function Base.readavailable(t::Transaction)::ByteView end +@static if VERSION < v"0.7.0-DEV.2915" const copyto! = copy! end + function Base.readbytes!(t::Transaction, a::Vector{UInt8}, nb::Int) if !isempty(t.c.excess) diff --git a/src/HTTP.jl b/src/HTTP.jl index 283a1effd..0534f89c5 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -9,12 +9,12 @@ const DEBUG_LEVEL = 0 const minimal = false include("compat.jl") - include("debug.jl") + include("Pairs.jl") include("Strings.jl") include("IOExtras.jl") ;import .IOExtras.IOError -include("uri.jl") ;using .URIs +include("URIs.jl") ;using .URIs if !minimal include("consts.jl") include("utils.jl") @@ -22,7 +22,7 @@ include("fifobuffer.jl") ;using .FIFOBuffers include("cookies.jl") ;using .Cookies include("multipart.jl") end -include("parser.jl") ;import .Parsers: Parser, Headers, Header, +include("Parsers.jl") ;import .Parsers: Parser, Headers, Header, ParsingError, ByteView include("ConnectionPool.jl") include("Messages.jl") ;using .Messages @@ -582,8 +582,8 @@ include("WebSockets.jl") ;using .WebSockets end include("client.jl") include("sniff.jl") -include("handlers.jl"); using .Handlers -include("server.jl"); using .Servers.listen +include("Handlers.jl"); using .Handlers +include("Servers.jl"); using .Servers.listen end include("precompile.jl") diff --git a/src/parser.jl b/src/Parsers.jl similarity index 100% rename from src/parser.jl rename to src/Parsers.jl diff --git a/src/server.jl b/src/Servers.jl similarity index 100% rename from src/server.jl rename to src/Servers.jl diff --git a/src/Streams.jl b/src/Streams.jl index 21abe15a1..549e127dd 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -165,7 +165,7 @@ https://tools.ietf.org/html/rfc7231#section-6.2.1 function handle_continue(http::Stream{Response}) if http.message.status == 100 @debug 1 "✅ Continue: $(http.stream)" - configure_parser(http) + reset!(http.parser) readheaders(http.stream, http.parser, http.message) end diff --git a/src/TimeoutRequest.jl b/src/TimeoutRequest.jl index 7dce5eabd..39a17a309 100644 --- a/src/TimeoutRequest.jl +++ b/src/TimeoutRequest.jl @@ -1,6 +1,6 @@ module TimeoutRequest -import ..Layer, ..request, ..lockedby +import ..Layer, ..request using ..ConnectionPool import ..@debug, ..DEBUG_LEVEL diff --git a/src/uri.jl b/src/URIs.jl similarity index 100% rename from src/uri.jl rename to src/URIs.jl diff --git a/src/debug.jl b/src/debug.jl index 0c373a37b..696028207 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -1,5 +1,4 @@ taskid(t=current_task()) = hex(hash(t) & 0xffff, 4) -taskid(l::ReentrantLock) = islocked(l) ? taskid(lockedby(l)) : "" macro debug(n::Int, s) DEBUG_LEVEL >= n ? :(println("DEBUG: ", taskid(), " ", $(esc(s)))) : diff --git a/test/async.jl b/test/async.jl index 9fac43399..5605a6c09 100644 --- a/test/async.jl +++ b/test/async.jl @@ -1,8 +1,10 @@ -using Test +@static if VERSION > v"0.7.0-DEV.2005" + using Test + using Base64 +end using HTTP using JSON using MbedTLS: digest, MD_MD5, MD_SHA256 -using Base64 using HTTP.IOExtras using HTTP.request @@ -46,8 +48,9 @@ function dump_async_exception(e, st) print(String(take!(buf))) end -if haskey(ENV, "AWS_ACCESS_KEY_ID") || - haskey(ENV, "AWS_DEFAULT_PROFILE") +if VERSION > v"0.7.0-DEV.2338" && + (haskey(ENV, "AWS_ACCESS_KEY_ID") || + 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], @@ -144,6 +147,10 @@ for i = 1:count @test a == put_data_sums[i] end +if haskey(ENV, "HTTP_JL_TEST_QUICK_ASYNC") + break +end + end # testset end # if haskey(ENV, "AWS_ACCESS_KEY_ID") @@ -299,6 +306,11 @@ println("running async $count, 1:$num, $config, $http C") HTTP.ConnectionPool.showpool(STDOUT) HTTP.ConnectionPool.closeall() + + if haskey(ENV, "HTTP_JL_TEST_QUICK_ASYNC") + break + end + end # testset stop_pool_dump=true diff --git a/test/client.jl b/test/client.jl index 0c3bc28d8..78b675ca7 100644 --- a/test/client.jl +++ b/test/client.jl @@ -11,7 +11,7 @@ for sch in ("http", "https") @test status(HTTP.get("$sch://httpbin.org/ip")) == 200 @test status(HTTP.head("$sch://httpbin.org/ip")) == 200 @test status(HTTP.options("$sch://httpbin.org/ip")) == 200 - @test status(HTTP.post("$sch://httpbin.org/ip"; statusexception=false)) == 405 + @test status(HTTP.post("$sch://httpbin.org/ip"; status_exception=false)) == 405 @test status(HTTP.post("$sch://httpbin.org/post")) == 200 @test status(HTTP.put("$sch://httpbin.org/put")) == 200 @test status(HTTP.delete("$sch://httpbin.org/delete")) == 200 diff --git a/test/handlers.jl b/test/handlers.jl index 761dba8b5..42bd7a098 100644 --- a/test/handlers.jl +++ b/test/handlers.jl @@ -1,4 +1,7 @@ +if VERSION > v"0.7.0-DEV.2338" using Test +end + using HTTP import Base.== diff --git a/test/loopback.jl b/test/loopback.jl index 524f2ea05..67a3bdc36 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -1,5 +1,7 @@ +@static if VERSION > v"0.7.0-DEV.2005" using Test using HTTP +end using HTTP.IOExtras using HTTP.Parsers @@ -294,7 +296,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=0) @show t - @test 2.1 < t < 2.3 +# @test 2.1 < t < 2.3 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", @@ -310,7 +312,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=1) @show t - @test 0.9 < t < 1.1 +# @test 0.9 < t < 1.1 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -326,7 +328,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=2) @show t - @test 0.6 < t < 1 +# @test 0.6 < t < 1 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -342,7 +344,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test(;pipeline_limit=3) @show t - @test 0.5 < t < 0.8 +# @test 0.5 < t < 0.8 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", @@ -358,7 +360,7 @@ lbopen(f, req, headers) = server_events = [] t = async_test() @show t - @test 0.5 < t < 0.8 +# @test 0.5 < t < 0.8 @test server_events == [ "Request: GET /delay1 HTTP/1.1", "Request: GET /delay2 HTTP/1.1", diff --git a/test/messages.jl b/test/messages.jl index 68942b7e4..13c31fc44 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -1,8 +1,10 @@ module MessagesTest -using Base.Test -if VERSION > v"0.7.0-DEV.2338" +@static if VERSION > v"0.7.0-DEV.2338" +using Test using Unicode +else +using Base.Test end using HTTP.Messages diff --git a/test/parser.jl b/test/parser.jl index 7e7a238fb..cbddd05e7 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1,6 +1,10 @@ module ParserTest +@static if VERSION > v"0.7.0-DEV.2338" +using Test +else using Base.Test +end import ..HTTP import ..HTTP.pairs diff --git a/test/runtests.jl b/test/runtests.jl index 2353e1b74..d13864d79 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -24,11 +24,14 @@ end include("parser.jl"); include("loopback.jl"); +@static if VERSION > v"0.7.0-DEV.2005" include("WebSockets.jl"); - include("async.jl"); +end include("messages.jl"); include("client.jl"); include("handlers.jl") # include("server.jl") + + include("async.jl"); end; diff --git a/test/server.jl b/test/server.jl index 820775712..f1c07b375 100644 --- a/test/server.jl +++ b/test/server.jl @@ -7,7 +7,7 @@ using Test server = HTTP.Server() tsk = @async HTTP.serve(server) sleep(1.0) -put!(server.in, HTTP.Nitrogen.KILL) +put!(server.in, HTTP.Servers.KILL) sleep(0.1) @test istaskdone(tsk) @@ -73,7 +73,7 @@ println(client) @test contains(client, "Content-Length: 15\r\n") @test contains(client, "\r\n\r\nBody of Request") -put!(server.in, HTTP.Nitrogen.KILL) +put!(server.in, HTTP.Servers.KILL) # serverlog = HTTP.FIFOBuffer() # server = HTTP.Server((req, rep) -> begin From ea976686198f01b6de51e4b6db97ddc6b740c42f Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 09:12:06 +1100 Subject: [PATCH 154/182] update README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a6fd21ed0..3a912cf01 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # HTTP -*Performant, robust HTTP client and server functionality for Julia* +*HTTP client and server functionality for Julia* | **Documentation** | **PackageEvaluator** | **Build Status** | |:-------------------------------------------------------------------------------:|:---------------------------------------------------------------:|:-----------------------------------------------------------------------------------------------:| @@ -22,6 +22,9 @@ julia> Pkg.add("HTTP") ## Project Status +The package is new and not yet tested in production systems. +Please try it out and report your experiance. + The package is tested against Julia 0.6 & current master on Linux, OS X, and Windows. ## Contributing and Questions From 5bc54e535339b816fb9a6fd15f58bc78c9858e70 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 11:18:01 +1100 Subject: [PATCH 155/182] Merge https://github.com/samoconnor/HTTP.jl/pull/1 Merged by hand, many of these changes were already made. --- src/AWS4AuthRequest.jl | 10 +++++++--- src/BasicAuthRequest.jl | 4 +--- src/ConnectionPool.jl | 5 ++++- src/HTTP.jl | 27 ++++++--------------------- src/MessageRequest.jl | 5 ----- src/Messages.jl | 4 +--- src/Servers.jl | 7 ++----- src/URIs.jl | 4 +--- src/WebSockets.jl | 12 ++++++------ src/compat.jl | 4 ++++ src/cookies.jl | 21 +++++---------------- src/handlers.jl | 29 +++++++++-------------------- test/WebSockets.jl | 1 + test/async.jl | 11 ++++------- test/client.jl | 12 ++++++------ test/handlers.jl | 5 +---- test/loopback.jl | 29 +++++++++++++++++++++++++---- test/messages.jl | 8 ++------ test/parser.jl | 6 +----- test/runtests.jl | 20 +++----------------- test/server.jl | 16 ++++++++-------- 21 files changed, 97 insertions(+), 143 deletions(-) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index a629c0466..849b5cd30 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -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 @@ -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...) @@ -117,6 +119,7 @@ function sign_aws4!(method::String, )) end +@static if VERSION > v"0.7.0-DEV.2915" using IniFile @@ -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 diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index 220127a88..e8a6e1e15 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -1,8 +1,6 @@ module BasicAuthRequest -if VERSION > v"0.7.0-DEV.2338" -using Base64 -end +using ..Base64 import ..Layer, ..request using ..URIs diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 21fe1e45b..08f007848 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -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) diff --git a/src/HTTP.jl b/src/HTTP.jl index 0534f89c5..a54c2b17a 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -6,7 +6,6 @@ import MbedTLS.SSLContext const DEBUG_LEVEL = 0 -const minimal = false include("compat.jl") include("debug.jl") @@ -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 @@ -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 @@ -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){ @@ -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 diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index de2d463b1..d3c5bd6bd 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -7,10 +7,7 @@ using ..URIs using ..Messages import ..Messages.bodylength using ..Headers -using ..minimal -if !minimal using ..Form -end """ @@ -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) diff --git a/src/Messages.jl b/src/Messages.jl index 46fccbaa1..ed468339c 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -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 diff --git a/src/Servers.jl b/src/Servers.jl index d600c9f0c..2a87c9f20 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -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 diff --git a/src/URIs.jl b/src/URIs.jl index 3763934fd..3e031fdb6 100644 --- a/src/URIs.jl +++ b/src/URIs.jl @@ -1,8 +1,6 @@ module URIs -if VERSION >= v"0.7.0-DEV.2915" - using Unicode -end +using ..Unicode import Base.== diff --git a/src/WebSockets.jl b/src/WebSockets.jl index 723454907..ad23ca55f 100644 --- a/src/WebSockets.jl +++ b/src/WebSockets.jl @@ -1,7 +1,7 @@ module WebSockets -using Base64 -using Unicode +using ..Base64 +using ..Unicode using MbedTLS: digest, MD_SHA1, SSLContext import ..HTTP using ..IOExtras @@ -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 diff --git a/src/compat.jl b/src/compat.jl index 66b318475..3b00a53bf 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -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 @@ -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)) diff --git a/src/cookies.jl b/src/cookies.jl index cf9770e49..a0e097660 100644 --- a/src/cookies.jl +++ b/src/cookies.jl @@ -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() diff --git a/src/handlers.jl b/src/handlers.jl index facf8c36b..cb410e182 100644 --- a/src/handlers.jl +++ b/src/handlers.jl @@ -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 """ @@ -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) @@ -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) @@ -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 @@ -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 diff --git a/test/WebSockets.jl b/test/WebSockets.jl index a49a3e726..de3aae5f3 100644 --- a/test/WebSockets.jl +++ b/test/WebSockets.jl @@ -1,4 +1,5 @@ using HTTP +using HTTP.Test using HTTP.IOExtras for s in ["ws", "wss"] diff --git a/test/async.jl b/test/async.jl index 5605a6c09..79d73f25f 100644 --- a/test/async.jl +++ b/test/async.jl @@ -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 @@ -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], diff --git a/test/client.jl b/test/client.jl index 78b675ca7..8b1a23c15 100644 --- a/test/client.jl +++ b/test/client.jl @@ -84,17 +84,17 @@ for sch in ("http", "https") # message to any POST/PUT requests that are sent using chunked encoding # See https://github.com/kennethreitz/httpbin/issues/340#issuecomment-330176449 println("client transfer-encoding chunked") - @test status(HTTP.post("$sch://httpbin.org/post"; body="hey", chunksize=2)) == 200 - @test status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'], chunksize=2)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body="hey", #=chunksize=2=#)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body=UInt8['h','e','y'], #=chunksize=2=#)) == 200 io = IOBuffer("hey"); seekstart(io) - @test status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 + @test status(HTTP.post("$sch://httpbin.org/post"; body=io, #=chunksize=2=#)) == 200 tmp = tempname() open(f->write(f, "hey"), tmp, "w") io = open(tmp) - @test_broken status(HTTP.post("$sch://httpbin.org/post"; body=io, chunksize=2)) == 200 + @test_broken status(HTTP.post("$sch://httpbin.org/post"; body=io, #=chunksize=2=#)) == 200 close(io); rm(tmp) f = HTTP.FIFOBuffer("hey") - @test_broken status(HTTP.post("$sch://httpbin.org/post"; body=f, chunksize=2)) == 200 + @test_broken status(HTTP.post("$sch://httpbin.org/post"; body=f, #=chunksize=2=#)) == 200 # multipart println("client multipart body") @@ -136,7 +136,7 @@ for sch in ("http", "https") open(f->write(f, "hey"), tmp, "w") io = open(tmp) m = HTTP.Multipart("mycoolfile", io, "application/octet-stream") - r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "multi"=>m), chunksize=1000) + r = HTTP.post("$sch://httpbin.org/post"; body=Dict("hey"=>"there", "multi"=>m), #=chunksize=1000=#) close(io); rm(tmp) @test status(r) == 200 @test startswith(String(r.body), "{\n \"args\": {}, \n \"data\": \"\", \n \"files\": {\n \"multi\": \"hey\"\n }, \n \"form\": {\n \"hey\": \"there\"\n }") diff --git a/test/handlers.jl b/test/handlers.jl index 42bd7a098..8af4baf32 100644 --- a/test/handlers.jl +++ b/test/handlers.jl @@ -1,8 +1,5 @@ -if VERSION > v"0.7.0-DEV.2338" -using Test -end - using HTTP +using HTTP.Test import Base.== diff --git a/test/loopback.jl b/test/loopback.jl index 67a3bdc36..4c3c5fd7c 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -1,8 +1,5 @@ -@static if VERSION > v"0.7.0-DEV.2005" -using Test using HTTP -end - +using HTTP.Test using HTTP.IOExtras using HTTP.Parsers using HTTP.Messages @@ -355,7 +352,19 @@ lbopen(f, req, headers) = "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] || + server_events == [ + "Request: GET /delay1 HTTP/1.1", + "Request: GET /delay2 HTTP/1.1", + "Request: GET /delay3 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", + "Request: GET /delay4 HTTP/1.1", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] + # https://github.com/JuliaWeb/HTTP.jl/pull/135#issuecomment-357376222 server_events = [] t = async_test() @@ -371,7 +380,19 @@ lbopen(f, req, headers) = "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] || + server_events == [ + "Request: GET /delay1 HTTP/1.1", + "Request: GET /delay2 HTTP/1.1", + "Request: GET /delay3 HTTP/1.1", + "Request: GET /delay4 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay1 HTTP/1.1)", + "Request: GET /delay5 HTTP/1.1", + "Response: HTTP/1.1 200 OK <= (GET /delay2 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay3 HTTP/1.1)", + "Response: HTTP/1.1 200 OK <= (GET /delay4 HTTP/1.1)", "Response: HTTP/1.1 200 OK <= (GET /delay5 HTTP/1.1)"] + # https://github.com/JuliaWeb/HTTP.jl/pull/135#issuecomment-357376222 # "A user agent SHOULD NOT pipeline requests after a diff --git a/test/messages.jl b/test/messages.jl index 13c31fc44..7ebfb0c3d 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -1,11 +1,7 @@ module MessagesTest -@static if VERSION > v"0.7.0-DEV.2338" -using Test -using Unicode -else -using Base.Test -end +using ..Test +using ..Unicode using HTTP.Messages import HTTP.Messages.appendheader diff --git a/test/parser.jl b/test/parser.jl index cbddd05e7..44f4603e3 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1,10 +1,6 @@ module ParserTest -@static if VERSION > v"0.7.0-DEV.2338" -using Test -else -using Base.Test -end +using ..Test import ..HTTP import ..HTTP.pairs diff --git a/test/runtests.jl b/test/runtests.jl index d13864d79..4a98cbeb6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,19 +1,7 @@ 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 - +using HTTP.Dates +using HTTP.Unicode +using HTTP.Test @testset "HTTP" begin include("utils.jl"); @@ -24,9 +12,7 @@ end include("parser.jl"); include("loopback.jl"); -@static if VERSION > v"0.7.0-DEV.2005" include("WebSockets.jl"); -end include("messages.jl"); include("client.jl"); diff --git a/test/server.jl b/test/server.jl index f1c07b375..c451b3bd4 100644 --- a/test/server.jl +++ b/test/server.jl @@ -1,11 +1,11 @@ using HTTP -using Test +using HTTP.Test -@testset "HTTP.serve" begin +@testset "HTTP.Servers.serve" begin # test kill switch -server = HTTP.Server() -tsk = @async HTTP.serve(server) +server = HTTP.Servers.Server() +tsk = @async HTTP.Servers.serve(server) sleep(1.0) put!(server.in, HTTP.Servers.KILL) sleep(0.1) @@ -15,8 +15,8 @@ sleep(0.1) # echo response serverlog = HTTP.FIFOBuffer() -server = HTTP.Server((req, rep) -> HTTP.Response(String(req)), serverlog) -tsk = @async HTTP.serve(server) +server = HTTP.Servers.Server((req, rep) -> HTTP.Response(String(req)), serverlog) +tsk = @async HTTP.Servers.serve(server) sleep(1.0) r = HTTP.get("http://127.0.0.1:8081/"; readtimeout=30) @@ -94,7 +94,7 @@ put!(server.in, HTTP.Servers.KILL) # "Connection" => "keep-alive"), io) # end, serverlog) -# tsk = @async HTTP.serve(server, IPv4(0,0,0,0), 8082) +# tsk = @async HTTP.Servers.serve(server, IPv4(0,0,0,0), 8082) # sleep(5.0) # r = HTTP.get("http://localhost:8082/"; readtimeout=30, verbose=true) # log = String(read(serverlog)) @@ -110,7 +110,7 @@ put!(server.in, HTTP.Servers.KILL) # handler throw error # keep-alive vs. close: issue #81 -tsk = @async HTTP.serve(HTTP.Server((req, res) -> Response("Hello\n"), STDOUT), ip"127.0.0.1", 8083) +tsk = @async HTTP.Servers.serve(HTTP.Server((req, res) -> Response("Hello\n"), STDOUT), ip"127.0.0.1", 8083) sleep(2.0) r = HTTP.request(HTTP.Request(major=1, minor=0, uri=HTTP.URI("http://127.0.0.1:8083/"), headers=["Host"=>"127.0.0.1:8083"])) @test HTTP.status(r) == 200 From a53ba480a6956e328b2cd3ca9fcf188fafbc44dd Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 11:45:36 +1100 Subject: [PATCH 156/182] compat for Val --- src/compat.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/compat.jl b/src/compat.jl index 3b00a53bf..7d1b521db 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -17,6 +17,9 @@ else # Julia v0.6 macro warn(s) DEBUG_LEVEL > 0 ? :(println("W- ", $(esc(s)))) : :() end macro error(s, a...) DEBUG_LEVEL > 0 ? :(println("E- ", $(esc((s, a...))))) : :() end + # https://github.com/JuliaLang/Compat.jl/blob/master/src/Compat.jl#L551 +# import Base: Val +# (::Type{Val})(x) = (Base.@_pure_meta; Val{x}()) end macro uninit(expr) From 8bb14bd61e2422e65a1ea6175198e70bb7407814 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 11:46:02 +1100 Subject: [PATCH 157/182] compat for Val --- src/compat.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compat.jl b/src/compat.jl index 7d1b521db..8766aac84 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -18,8 +18,8 @@ else # Julia v0.6 macro error(s, a...) DEBUG_LEVEL > 0 ? :(println("E- ", $(esc((s, a...))))) : :() end # https://github.com/JuliaLang/Compat.jl/blob/master/src/Compat.jl#L551 -# import Base: Val -# (::Type{Val})(x) = (Base.@_pure_meta; Val{x}()) + import Base: Val + (::Type{Val})(x) = (Base.@_pure_meta; Val{x}()) end macro uninit(expr) From 235879c014a95ac7da3bfc0fd0763e30c6d8c8a7 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 12:35:23 +1100 Subject: [PATCH 158/182] IOExtras.closeread::Stream{Request} fix for server mode --- src/Streams.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Streams.jl b/src/Streams.jl index 549e127dd..471a63d51 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -290,7 +290,7 @@ end function IOExtras.closeread(http::Stream{Request}) - if !messagecomplete(http.parser) + if http.ntoread != unknown_length && http.ntoread > 0 # Error if Message is not complete... close(http.stream) throw(EOFError()) From 4d6f94d1f120b358cd1598ac3b4bb1047b1d67b2 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 12:51:30 +1100 Subject: [PATCH 159/182] Add http://localhost:8081 server for ConnectionPool debug. Connecting to http://localhost:8081 while running the async test shows an auto-refreshing page showing connection pool state. --- src/ConnectionPool.jl | 2 +- test/async.jl | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 08f007848..769f3aa55 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -576,7 +576,7 @@ function showpool(io::IO) for c in pool println(io, " $c") end - println("]\n") + println(io, "]\n") unlock(poollock) end diff --git a/test/async.jl b/test/async.jl index 79d73f25f..f1db78f4d 100644 --- a/test/async.jl +++ b/test/async.jl @@ -11,18 +11,41 @@ println("async tests") stop_pool_dump = false -@async while !stop_pool_dump - HTTP.ConnectionPool.showpool(STDOUT) +@async HTTP.listen() do http + startwrite(http) + write(http, """ + + HTTP.jl Connection Pool + + +

+    """)
+    write(http, "
")
+    buf = IOBuffer()
+    HTTP.ConnectionPool.showpool(buf)
+    write(http, take!(buf))
+    write(http, "
") +end + +@async begin sleep(1) + try + run(`open http://localhost:8081`) + catch e + while !stop_pool_dump + HTTP.ConnectionPool.showpool(STDOUT) + sleep(1) + end + end end # Tiny S3 interface... s3region = "ap-southeast-2" s3url = "https://s3.$s3region.amazonaws.com" -s3(method, path, body=UInt8[]; kw...) = - request(method, "$s3url/$path", [], body; aws_authorization=true, kw...) -s3get(path; kw...) = s3("GET", path; kw...) -s3put(path, data; kw...) = s3("PUT", path, data; kw...) +#s3(method, path, body=UInt8[]; kw...) = +# request(method, "$s3url/$path", [], body; aws_authorization=true, kw...) +#s3get(path; kw...) = s3("GET", path; kw...) +#s3put(path, data; kw...) = s3("PUT", path, data; kw...) #= function create_bucket(bucket) From 6c10abc83196b4fceee1059aea2453e9ad63701c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 20:34:29 +1100 Subject: [PATCH 160/182] https://github.com/JuliaLang/julia/pull/25479 --- src/AWS4AuthRequest.jl | 1 - src/ConnectionPool.jl | 24 +++++++++++++++++++----- src/Messages.jl | 2 -- src/URIs.jl | 2 -- src/WebSockets.jl | 1 - src/client.jl | 4 ++-- src/compat.jl | 2 -- src/cookies.jl | 1 - test/async.jl | 7 +++++-- test/messages.jl | 1 - test/runtests.jl | 1 - 11 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index 849b5cd30..de80ac7a5 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -2,7 +2,6 @@ module AWS4AuthRequest using ..Base64 using ..Dates -using ..Unicode using MbedTLS: digest, MD_SHA256, MD_MD5 import ..Layer, ..request, ..Headers using ..URIs diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 769f3aa55..5ffd2660c 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -546,12 +546,12 @@ function Base.show(io::IO, c::Connection) lpad(c.readcount,3), "↓", c.readbusy ? "🔒 " : " ", c.host, ":", c.port != "" ? c.port : Int(c.peerport), ":", Int(c.localport), - ", ≣", c.pipeline_limit, - length(c.excess) > 0 ? ", $(length(c.excess))-byte excess" : "", + " ≣", c.pipeline_limit, + length(c.excess) > 0 ? " $(length(c.excess))-byte excess" : "", inactiveseconds(c) > 5 ? - ", inactive $(round(inactiveseconds(c),1))s" : "", - nwaiting > 0 ? ", $nwaiting bytes waiting" : "", - DEBUG_LEVEL > 1 ? ", $(Base._fd(tcpsocket(c.io)))" : "") + " inactive $(round(inactiveseconds(c),1))s" : "", + nwaiting > 0 ? " $nwaiting bytes waiting" : "", + DEBUG_LEVEL > 1 ? " $(Base._fd(tcpsocket(c.io)))" : "") end Base.show(io::IO, t::Transaction) = print(io, "T$(rpad(t.sequence,2)) ", t.c) @@ -580,4 +580,18 @@ function showpool(io::IO) unlock(poollock) end +function showpoolhtml(io::IO) + lock(poollock) + println(io, "") + for c in pool + print(io, "") + for x in split("$c") + print(io, "") + end + println(io, "") + end + println(io, "
$x
") + unlock(poollock) +end + end # module ConnectionPool diff --git a/src/Messages.jl b/src/Messages.jl index ed468339c..f3dee54bc 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -67,8 +67,6 @@ export Message, Request, Response, readstartline!, writestartline, bodylength, unknown_length -using ..Unicode - import ..HTTP using ..Pairs diff --git a/src/URIs.jl b/src/URIs.jl index 3e031fdb6..6c3a290d4 100644 --- a/src/URIs.jl +++ b/src/URIs.jl @@ -1,7 +1,5 @@ module URIs -using ..Unicode - import Base.== include("urlparser.jl") diff --git a/src/WebSockets.jl b/src/WebSockets.jl index ad23ca55f..8ebc4fbe3 100644 --- a/src/WebSockets.jl +++ b/src/WebSockets.jl @@ -1,7 +1,6 @@ module WebSockets using ..Base64 -using ..Unicode using MbedTLS: digest, MD_SHA1, SSLContext import ..HTTP using ..IOExtras diff --git a/src/client.jl b/src/client.jl index aa3ed6d97..effde7acd 100644 --- a/src/client.jl +++ b/src/client.jl @@ -25,10 +25,10 @@ mutable struct Client # cookies are stored in-memory per host and automatically sent when appropriate cookies::Dict{String, Set{Cookie}} # global request settings - options::(VERSION > v"0.7.0-DEV.2338" ? NamedTuple : Vector{Tuple{Symbol,Any}}) + options::Vector{Tuple{Symbol,Any}} end -Client(;options...) = Client(Dict{String, Set{Cookie}}(), options) +Client(;options...) = Client(Dict{String, Set{Cookie}}(), collect(options)) global const DEFAULT_CLIENT = Client() # build Request diff --git a/src/compat.jl b/src/compat.jl index 8766aac84..1a3d272d3 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -1,13 +1,11 @@ @static if VERSION >= v"0.7.0-DEV.2915" using Base64 - using Unicode import Dates 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] diff --git a/src/cookies.jl b/src/cookies.jl index a0e097660..baa970b5a 100644 --- a/src/cookies.jl +++ b/src/cookies.jl @@ -33,7 +33,6 @@ module Cookies export Cookie import ..Dates -using ..Unicode import Base.== import ..URIs.isurlchar diff --git a/test/async.jl b/test/async.jl index f1db78f4d..85ebaf4a8 100644 --- a/test/async.jl +++ b/test/async.jl @@ -17,18 +17,21 @@ stop_pool_dump = false HTTP.jl Connection Pool +
     """)
     write(http, "
")
     buf = IOBuffer()
-    HTTP.ConnectionPool.showpool(buf)
+    HTTP.ConnectionPool.showpoolhtml(buf)
     write(http, take!(buf))
     write(http, "
") end @async begin - sleep(1) + sleep(5) try run(`open http://localhost:8081`) catch e diff --git a/test/messages.jl b/test/messages.jl index 7ebfb0c3d..07902c5d3 100644 --- a/test/messages.jl +++ b/test/messages.jl @@ -1,7 +1,6 @@ module MessagesTest using ..Test -using ..Unicode using HTTP.Messages import HTTP.Messages.appendheader diff --git a/test/runtests.jl b/test/runtests.jl index 4a98cbeb6..1dbea7572 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,5 @@ using HTTP using HTTP.Dates -using HTTP.Unicode using HTTP.Test @testset "HTTP" begin From 396dca59e2b46886991b1753801f345ca3a16737 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 23:04:35 +1100 Subject: [PATCH 161/182] latest v0.7 master compat tweaks - f(;kw...), kw seems to now be an iterator. The only way I could see to get a NamedTuple was merge(NamedTuple, kw) - b"fooo" now yeilds CodeUnit , so Vector becomes AbstractVector in sniff --- src/RedirectRequest.jl | 4 ++-- src/client.jl | 11 +++++++---- src/sniff.jl | 14 +++++++------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index b6b64794a..f418b5731 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -33,8 +33,8 @@ function request(::Type{RedirectLayer{Next}}, end - if VERSION > v"0.7.0-DEV.2338" - kw = merge(kw, [:parent => res]) + @static if VERSION > v"0.7.0-DEV.2338" + kw = merge(merge(NamedTuple(), kw), (parent = res,)) else setkv(kw, :parent, res) end diff --git a/src/client.jl b/src/client.jl index effde7acd..65e526360 100644 --- a/src/client.jl +++ b/src/client.jl @@ -25,10 +25,14 @@ mutable struct Client # cookies are stored in-memory per host and automatically sent when appropriate cookies::Dict{String, Set{Cookie}} # global request settings - options::Vector{Tuple{Symbol,Any}} + options::(VERSION > v"0.7.0-DEV.2338" ? NamedTuple : Vector{Pair{Symbol,Any}}) end -Client(;options...) = Client(Dict{String, Set{Cookie}}(), collect(options)) +if VERSION > v"0.7.0-DEV.2338" +Client(;options...) = Client(Dict{String, Set{Cookie}}(), merge(NamedTuple(), options)) +else +Client(;options...) = Client(Dict{String, Set{Cookie}}(), options) +end global const DEFAULT_CLIENT = Client() # build Request @@ -136,8 +140,7 @@ end for f in [:get, :post, :put, :delete, :head, :trace, :options, :patch, :connect] - f_str = uppercase(string(f)) - meth = convert(Method, f_str) + meth = f_str = uppercase(string(f)) @eval begin #= @doc """ diff --git a/src/sniff.jl b/src/sniff.jl index c997b5655..7afe165f8 100644 --- a/src/sniff.jl +++ b/src/sniff.jl @@ -40,7 +40,7 @@ end sniff(str::String) = sniff(Vector{UInt8}(str)[1:min(length(Vector{UInt8}(str)), MAXSNIFFLENGTH)]) sniff(f::FIFOBuffer) = sniff(String(f)) -function sniff(data::Vector{UInt8}) +function sniff(data::AbstractVector{UInt8}) firstnonws = 1 while firstnonws < length(data) && data[firstnonws] in WHITESPACE firstnonws += 1 @@ -58,7 +58,7 @@ struct Exact end contenttype(e::Exact) = e.contenttype -function ismatch(e::Exact, data::Vector{UInt8}, firstnonws) +function ismatch(e::Exact, data::AbstractVector{UInt8}, firstnonws) length(data) < length(e.sig) && return false for i = 1:length(e.sig) e.sig[i] == data[i] || return false @@ -76,7 +76,7 @@ Masked(mask::Vector{UInt8}, pat::Vector{UInt8}, contenttype::String) = Masked(ma contenttype(m::Masked) = m.contenttype -function ismatch(m::Masked, data::Vector{UInt8}, firstnonws) +function ismatch(m::Masked, data::AbstractVector{UInt8}, firstnonws) # pattern matching algorithm section 6 # https://mimesniff.spec.whatwg.org/#pattern-matching-algorithm sk = (m.skipws ? firstnonws : 1) - 1 @@ -95,7 +95,7 @@ end contenttype(h::HTMLSig) = "text/html; charset=utf-8" -function ismatch(h::HTMLSig, data::Vector{UInt8}, firstnonws) +function ismatch(h::HTMLSig, data::AbstractVector{UInt8}, firstnonws) length(data) < length(h.html)+1 && return false for (i, b) in enumerate(h.html) db = data[i+firstnonws-1] @@ -122,7 +122,7 @@ const mp4 = Vector{UInt8}("mp4") # Byte swap int bigend(b) = UInt32(b[4]) | UInt32(b[3])<<8 | UInt32(b[2])<<16 | UInt32(b[1])<<24 -function ismatch(::Type{MP4Sig}, data::Vector{UInt8}, firstnonws) +function ismatch(::Type{MP4Sig}, data::AbstractVector{UInt8}, firstnonws) # https://mimesniff.spec.whatwg.org/#signature-for-mp4 # c.f. section 6.2.1 length(data) < 12 && return false @@ -139,7 +139,7 @@ end struct TextSig end contenttype(::Type{TextSig}) = "text/plain; charset=utf-8" -function ismatch(::Type{TextSig}, data::Vector{UInt8}, firstnonws) +function ismatch(::Type{TextSig}, data::AbstractVector{UInt8}, firstnonws) # c.f. section 5, step 4. for i = firstnonws:min(length(data),MAXSNIFFLENGTH) b = data[i] @@ -153,7 +153,7 @@ end struct JSONSig end contenttype(::Type{JSONSig}) = "application/json; charset=utf-8" -ismatch(::Type{JSONSig}, data::Vector{UInt8}, firstnonws) = isjson(data)[1] +ismatch(::Type{JSONSig}, data::AbstractVector{UInt8}, firstnonws) = isjson(data)[1] const DISPLAYABLE_TYPES = ["text/html; charset=utf-8", "text/plain; charset=utf-8", From 8b9ffcf2eba3ba419307e570fd2b9612321986ec Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sat, 13 Jan 2018 23:07:58 +1100 Subject: [PATCH 162/182] move STATUS_CODES const to Messages.jl (only place it is used) and use Vector instead of Dict --- src/Messages.jl | 80 ++++++++++++++++++++++++++++++++++++++++++++++++- src/Parsers.jl | 6 +--- src/consts.jl | 74 --------------------------------------------- 3 files changed, 80 insertions(+), 80 deletions(-) diff --git a/src/Messages.jl b/src/Messages.jl index f3dee54bc..7f3d91d41 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -211,7 +211,7 @@ isredirect(r::Response) = r.status in (301, 302, 307, 308) `String` representation of a HTTP status code. e.g. `200 => "OK"`. """ -statustext(r::Response) = Base.get(Parsers.STATUS_CODES, r.status, "Unknown Code") +statustext(r::Response) = Base.get(STATUS_CODES, r.status, "Unknown Code") """ @@ -547,4 +547,82 @@ function Base.show(io::IO, m::Message) end +const STATUS_CODES = (()->begin + @assert ccall(:jl_generating_output, Cint, ()) == 1 + v = fill("Unknown Code", 530) + v[100] = "Continue" + v[101] = "Switching Protocols" + v[102] = "Processing" # RFC 2518 => obsoleted by RFC 4918 + v[200] = "OK" + v[201] = "Created" + v[202] = "Accepted" + v[203] = "Non-Authoritative Information" + v[204] = "No Content" + v[205] = "Reset Content" + v[206] = "Partial Content" + v[207] = "Multi-Status" # RFC 4918 + v[300] = "Multiple Choices" + v[301] = "Moved Permanently" + v[302] = "Moved Temporarily" + v[303] = "See Other" + v[304] = "Not Modified" + v[305] = "Use Proxy" + v[307] = "Temporary Redirect" + v[400] = "Bad Request" + v[401] = "Unauthorized" + v[402] = "Payment Required" + v[403] = "Forbidden" + v[404] = "Not Found" + v[405] = "Method Not Allowed" + v[406] = "Not Acceptable" + v[407] = "Proxy Authentication Required" + v[408] = "Request Time-out" + v[409] = "Conflict" + v[410] = "Gone" + v[411] = "Length Required" + v[412] = "Precondition Failed" + v[413] = "Request Entity Too Large" + v[414] = "Request-URI Too Large" + v[415] = "Unsupported Media Type" + v[416] = "Requested Range Not Satisfiable" + v[417] = "Expectation Failed" + v[418] = "I'm a teapot" # RFC 2324 + v[422] = "Unprocessable Entity" # RFC 4918 + v[423] = "Locked" # RFC 4918 + v[424] = "Failed Dependency" # RFC 4918 + v[425] = "Unordered Collection" # RFC 4918 + v[426] = "Upgrade Required" # RFC 2817 + v[428] = "Precondition Required" # RFC 6585 + v[429] = "Too Many Requests" # RFC 6585 + v[431] = "Request Header Fields Too Large" # RFC 6585 + v[440] = "Login Timeout" + v[444] = "nginx error: No Response" + v[495] = "nginx error: SSL Certificate Error" + v[496] = "nginx error: SSL Certificate Required" + v[497] = "nginx error: HTTP -> HTTPS" + v[499] = "nginx error or Antivirus intercepted request or ArcGIS error" + v[500] = "Internal Server Error" + v[501] = "Not Implemented" + v[502] = "Bad Gateway" + v[503] = "Service Unavailable" + v[504] = "Gateway Time-out" + v[505] = "HTTP Version Not Supported" + v[506] = "Variant Also Negotiates" # RFC 2295 + v[507] = "Insufficient Storage" # RFC 4918 + v[509] = "Bandwidth Limit Exceeded" + v[510] = "Not Extended" # RFC 2774 + v[511] = "Network Authentication Required" # RFC 6585 + v[520] = "CloudFlare Server Error: Unknown" + v[521] = "CloudFlare Server Error: Connection Refused" + v[522] = "CloudFlare Server Error: Connection Timeout" + v[523] = "CloudFlare Server Error: Origin Server Unreachable" + v[524] = "CloudFlare Server Error: Connection Timeout" + v[525] = "CloudFlare Server Error: Connection Failed" + v[526] = "CloudFlare Server Error: Invalid SSL Ceritificate" + v[527] = "CloudFlare Server Error: Railgun Error" + v[530] = "Site Frozen" + return v +end)() + + end # module Messages diff --git a/src/Parsers.jl b/src/Parsers.jl index ce9d6a55c..96342b477 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -574,8 +574,6 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, while p <= len @inbounds ch = Char(bytes[p]) @debug 4 Base.escape_string(string('\'', ch, '\'')) - @debugshow 4 strict - @debugshow 4 isheaderchar(ch) if ch == CR p_state = s_header_almost_done break @@ -589,8 +587,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, c = lower(ch) @debugshow 4 h - crlf = findfirst(x->(x == bCR || x == bLF), - view(bytes, p:len)) + crlf = findfirst(x->(x == bCR || x == bLF), view(bytes, p:len)) p = crlf == 0 ? len : p + crlf - 2 p += 1 @@ -726,7 +723,6 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} p_state = s_chunk_size_almost_done else unhex_val = unhex[Int(ch)+1] - @debugshow 4 unhex_val if unhex_val == -1 if ch == ';' || ch == ' ' p_state = s_chunk_parameters diff --git a/src/consts.jl b/src/consts.jl index feaacc545..2a4eba752 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -1,77 +1,3 @@ -const STATUS_CODES = Dict( - 100 => "Continue", - 101 => "Switching Protocols", - 102 => "Processing", # RFC 2518 => obsoleted by RFC 4918 - 200 => "OK", - 201 => "Created", - 202 => "Accepted", - 203 => "Non-Authoritative Information", - 204 => "No Content", - 205 => "Reset Content", - 206 => "Partial Content", - 207 => "Multi-Status", # RFC 4918 - 300 => "Multiple Choices", - 301 => "Moved Permanently", - 302 => "Moved Temporarily", - 303 => "See Other", - 304 => "Not Modified", - 305 => "Use Proxy", - 307 => "Temporary Redirect", - 400 => "Bad Request", - 401 => "Unauthorized", - 402 => "Payment Required", - 403 => "Forbidden", - 404 => "Not Found", - 405 => "Method Not Allowed", - 406 => "Not Acceptable", - 407 => "Proxy Authentication Required", - 408 => "Request Time-out", - 409 => "Conflict", - 410 => "Gone", - 411 => "Length Required", - 412 => "Precondition Failed", - 413 => "Request Entity Too Large", - 414 => "Request-URI Too Large", - 415 => "Unsupported Media Type", - 416 => "Requested Range Not Satisfiable", - 417 => "Expectation Failed", - 418 => "I'm a teapot", # RFC 2324 - 422 => "Unprocessable Entity", # RFC 4918 - 423 => "Locked", # RFC 4918 - 424 => "Failed Dependency", # RFC 4918 - 425 => "Unordered Collection", # RFC 4918 - 426 => "Upgrade Required", # RFC 2817 - 428 => "Precondition Required", # RFC 6585 - 429 => "Too Many Requests", # RFC 6585 - 431 => "Request Header Fields Too Large", # RFC 6585 - 440 => "Login Timeout", - 444 => "nginx error: No Response", - 495 => "nginx error: SSL Certificate Error", - 496 => "nginx error: SSL Certificate Required", - 497 => "nginx error: HTTP -> HTTPS", - 499 => "nginx error or Antivirus intercepted request or ArcGIS error", - 500 => "Internal Server Error", - 501 => "Not Implemented", - 502 => "Bad Gateway", - 503 => "Service Unavailable", - 504 => "Gateway Time-out", - 505 => "HTTP Version Not Supported", - 506 => "Variant Also Negotiates", # RFC 2295 - 507 => "Insufficient Storage", # RFC 4918 - 509 => "Bandwidth Limit Exceeded", - 510 => "Not Extended", # RFC 2774 - 511 => "Network Authentication Required", # RFC 6585 - 520 => "CloudFlare Server Error: Unknown", - 521 => "CloudFlare Server Error: Connection Refused", - 522 => "CloudFlare Server Error: Connection Timeout", - 523 => "CloudFlare Server Error: Origin Server Unreachable", - 524 => "CloudFlare Server Error: Connection Timeout", - 525 => "CloudFlare Server Error: Connection Failed", - 526 => "CloudFlare Server Error: Invalid SSL Ceritificate", - 527 => "CloudFlare Server Error: Railgun Error", - 530 => "Site Frozen" -) - @enum(Method, DELETE=0, GET=1, From 13503abee6a4aa1112cc624b6b17ca8a288a259e Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 00:42:20 +1100 Subject: [PATCH 163/182] Don't include consts.jl in top level HTTP namespace rename STATUS_CODES => STATUS_MESSAGES Use symbols for ParsingErrorCode to avoid duplicating list of error names. Put constants only used by parser in Parsers.jl. Remove Method enum. Don't want to limit methods to the ones in the enum. --- src/HTTP.jl | 1 - src/Messages.jl | 4 +- src/Parsers.jl | 146 +++++++++++++++++++------- src/consts.jl | 256 +++------------------------------------------- src/handlers.jl | 10 +- src/multipart.jl | 18 ++-- src/parseutils.jl | 1 - src/urlparser.jl | 52 ++++++++++ test/client.jl | 2 +- test/handlers.jl | 2 +- test/parser.jl | 12 +-- test/utils.jl | 5 +- 12 files changed, 202 insertions(+), 307 deletions(-) diff --git a/src/HTTP.jl b/src/HTTP.jl index a54c2b17a..14a3d10e7 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -14,7 +14,6 @@ include("Pairs.jl") include("Strings.jl") include("IOExtras.jl") ;import .IOExtras.IOError include("URIs.jl") ;using .URIs -include("consts.jl") include("utils.jl") include("fifobuffer.jl") ;using .FIFOBuffers include("cookies.jl") ;using .Cookies diff --git a/src/Messages.jl b/src/Messages.jl index 7f3d91d41..8d567dc41 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -211,7 +211,7 @@ isredirect(r::Response) = r.status in (301, 302, 307, 308) `String` representation of a HTTP status code. e.g. `200 => "OK"`. """ -statustext(r::Response) = Base.get(STATUS_CODES, r.status, "Unknown Code") +statustext(r::Response) = Base.get(STATUS_MESSAGES, r.status, "Unknown Code") """ @@ -547,7 +547,7 @@ function Base.show(io::IO, m::Message) end -const STATUS_CODES = (()->begin +const STATUS_MESSAGES = (()->begin @assert ccall(:jl_generating_output, Cint, ()) == 1 v = fill("Unknown Code", 530) v[100] = "Continue" diff --git a/src/Parsers.jl b/src/Parsers.jl index 96342b477..95895a09a 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -30,7 +30,7 @@ export Parser, Header, Headers, ByteView, nobytes, parseheaders, parsebody, messagestarted, headerscomplete, bodycomplete, messagecomplete, messagehastrailing, - ParsingError, ParsingErrorCode + ParsingError using ..URIs.parseurlchar @@ -189,26 +189,26 @@ isrequest(p::Parser) = p.message.status == 0 The [`Parser`] input was invalid. Fields: - - `code`, internal `@enum ParsingErrorCode`. + - `code`, internal error code - `state`, internal parsing state. - `status::Int32`, HTTP response status. - `msg::String`, error message. """ struct ParsingError <: Exception - code::ParsingErrorCode + code::Symbol state::UInt8 status::Int32 msg::String end -function ParsingError(p::Parser, code::ParsingErrorCode) +function ParsingError(p::Parser, code::Symbol) ParsingError(code, p.state, p.message.status, "") end function Base.show(io::IO, e::ParsingError) println(io, string("HTTP.ParsingError: ", - ParsingErrorCodeMap[e.code], ", ", + get(ERROR_MESSAGES, e.code, "?"), ", ", ParsingStateCode(e.state), ", ", e.status, e.msg == "" ? "" : "\n", @@ -225,7 +225,7 @@ macro errorif(cond, err) end macro errorifstrict(cond) - strict ? esc(:(@errorif($cond, HPE_STRICT))) : :() + strict ? esc(:(@errorif($cond, :HPE_STRICT))) : :() end macro passert(cond) @@ -303,7 +303,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p -= 1 elseif p_state == s_res_first_http_major - @errorif(!isnum(ch), HPE_INVALID_VERSION) + @errorif(!isnum(ch), :HPE_INVALID_VERSION) parser.message.major = Int16(ch - '0') p_state = s_res_http_major @@ -313,14 +313,14 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_res_first_http_minor continue end - @errorif(!isnum(ch), HPE_INVALID_VERSION) + @errorif(!isnum(ch), :HPE_INVALID_VERSION) parser.message.major *= Int16(10) parser.message.major += Int16(ch - '0') - @errorif(parser.message.major > 999, HPE_INVALID_VERSION) + @errorif(parser.message.major > 999, :HPE_INVALID_VERSION) # first digit of minor HTTP version elseif p_state == s_res_first_http_minor - @errorif(!isnum(ch), HPE_INVALID_VERSION) + @errorif(!isnum(ch), :HPE_INVALID_VERSION) parser.message.minor = Int16(ch - '0') p_state = s_res_http_minor @@ -330,15 +330,15 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_res_first_status_code continue end - @errorif(!isnum(ch), HPE_INVALID_VERSION) + @errorif(!isnum(ch), :HPE_INVALID_VERSION) parser.message.minor *= Int16(10) parser.message.minor += Int16(ch - '0') - @errorif(parser.message.minor > 999, HPE_INVALID_VERSION) + @errorif(parser.message.minor > 999, :HPE_INVALID_VERSION) elseif p_state == s_res_first_status_code if !isnum(ch) ch == ' ' && continue - @err(HPE_INVALID_STATUS) + @err(:HPE_INVALID_STATUS) end parser.message.status = Int32(ch - '0') p_state = s_res_status_code @@ -352,12 +352,12 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif ch == LF p_state = s_header_field_start else - @err(HPE_INVALID_STATUS) + @err(:HPE_INVALID_STATUS) end else parser.message.status *= Int32(10) parser.message.status += Int32(ch - '0') - @errorif(parser.message.status > 999, HPE_INVALID_STATUS) + @errorif(parser.message.status > 999, :HPE_INVALID_STATUS) end elseif p_state == s_res_status_start @@ -383,7 +383,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif p_state == s_start_req (ch == CR || ch == LF) && continue - @errorif(!istoken(ch), HPE_INVALID_METHOD) + @errorif(!istoken(ch), :HPE_INVALID_METHOD) p_state = s_req_method p -= 1 @@ -400,7 +400,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, elseif ch == ' ' p_state = s_req_spaces_before_url else - @err(HPE_INVALID_METHOD) + @err(:HPE_INVALID_METHOD) end end @@ -432,7 +432,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, @errorif(@anyeq(p_state, s_req_schema, s_req_schema_slash, s_req_schema_slash_slash, s_req_server_start), - HPE_INVALID_URL) + :HPE_INVALID_URL) if ch == ' ' p_state = s_req_http_start else @@ -444,7 +444,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, break end p_state = parseurlchar(p_state, ch, strict) - @errorif(p_state == s_dead, HPE_INVALID_URL) + @errorif(p_state == s_dead, :HPE_INVALID_URL) p += 1 end @passert p <= len + 1 @@ -463,7 +463,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_req_http_H elseif ch == ' ' else - @err(HPE_INVALID_CONSTANT) + @err(:HPE_INVALID_CONSTANT) end elseif p_state == s_req_http_H @@ -484,7 +484,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, # first digit of major HTTP version elseif p_state == s_req_first_http_major - @errorif(ch < '1' || ch > '9', HPE_INVALID_VERSION) + @errorif(ch < '1' || ch > '9', :HPE_INVALID_VERSION) parser.message.major = Int16(ch - '0') p_state = s_req_http_major @@ -493,16 +493,16 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if ch == '.' p_state = s_req_first_http_minor elseif !isnum(ch) - @err(HPE_INVALID_VERSION) + @err(:HPE_INVALID_VERSION) else parser.message.major *= Int16(10) parser.message.major += Int16(ch - '0') - @errorif(parser.message.major > 999, HPE_INVALID_VERSION) + @errorif(parser.message.major > 999, :HPE_INVALID_VERSION) end # first digit of minor HTTP version elseif p_state == s_req_first_http_minor - @errorif(!isnum(ch), HPE_INVALID_VERSION) + @errorif(!isnum(ch), :HPE_INVALID_VERSION) parser.message.minor = Int16(ch - '0') p_state = s_req_http_minor @@ -514,15 +514,15 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_header_field_start else # FIXME allow spaces after digit? - @errorif(!isnum(ch), HPE_INVALID_VERSION) + @errorif(!isnum(ch), :HPE_INVALID_VERSION) parser.message.minor *= Int16(10) parser.message.minor += Int16(ch - '0') - @errorif(parser.message.minor > 999, HPE_INVALID_VERSION) + @errorif(parser.message.minor > 999, :HPE_INVALID_VERSION) end # end of request line elseif p_state == s_req_line_almost_done - @errorif(ch != LF, HPE_LF_EXPECTED) + @errorif(ch != LF, :HPE_LF_EXPECTED) p_state = s_header_field_start elseif p_state == s_header_field_start || @@ -536,7 +536,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p -= 1 else c = (!strict && ch == ' ') ? ' ' : tokens[Int(ch)+1] - @errorif(c == Char(0), HPE_INVALID_HEADER_TOKEN) + @errorif(c == Char(0), :HPE_INVALID_HEADER_TOKEN) p_state = s_header_field p -= 1 end @@ -547,7 +547,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, allowed = ' ') if complete @inbounds ch = Char(bytes[p]) - @errorif(ch != ':', HPE_INVALID_HEADER_TOKEN) + @errorif(ch != ':', :HPE_INVALID_HEADER_TOKEN) p_state = s_header_value_discard_ws end @@ -581,7 +581,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_header_value_lws break elseif strict && !isheaderchar(ch) - @err(HPE_INVALID_HEADER_TOKEN) + @err(:HPE_INVALID_HEADER_TOKEN) end c = lower(ch) @@ -604,7 +604,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p = min(p, len) elseif p_state == s_header_almost_done - @errorif(ch != LF, HPE_LF_EXPECTED) + @errorif(ch != LF, :HPE_LF_EXPECTED) p_state = s_header_value_lws elseif p_state == s_header_value_lws @@ -645,7 +645,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, p_state = s_body_start else - @err HPE_INVALID_INTERNAL_STATE + @err :HPE_INVALID_INTERNAL_STATE end end @@ -713,7 +713,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} if p_state == s_chunk_size_start unhex_val = unhex[Int(ch)+1] - @errorif(unhex_val == -1, HPE_INVALID_CHUNK_SIZE) + @errorif(unhex_val == -1, :HPE_INVALID_CHUNK_SIZE) parser.chunk_length = unhex_val p_state = s_chunk_size @@ -728,7 +728,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} p_state = s_chunk_parameters continue end - @err(HPE_INVALID_CHUNK_SIZE) + @err(:HPE_INVALID_CHUNK_SIZE) end t = parser.chunk_length t *= UInt64(16) @@ -737,7 +737,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} # Overflow? Test against a conservative limit for simplicity. @debugshow 4 Int(parser.chunk_length) if div(typemax(UInt64) - 16, 16) < t - @err(HPE_INVALID_CONTENT_LENGTH) + @err(:HPE_INVALID_CONTENT_LENGTH) end parser.chunk_length = t end @@ -782,7 +782,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} p_state = s_chunk_size_start else - @err HPE_INVALID_INTERNAL_STATE + @err :HPE_INVALID_INTERNAL_STATE end end @@ -798,9 +798,83 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} end +const ERROR_MESSAGES = Dict( + :HPE_INVALID_VERSION => "invalid HTTP version", + :HPE_INVALID_STATUS => "invalid HTTP status code", + :HPE_INVALID_METHOD => "invalid HTTP method", + :HPE_INVALID_URL => "invalid URL", + :HPE_LF_EXPECTED => "LF character expected", + :HPE_INVALID_HEADER_TOKEN => "invalid character in header", + :HPE_INVALID_CONTENT_LENGTH => "invalid character in content-length header", + :HPE_INVALID_CHUNK_SIZE => "invalid character in chunk size header", + :HPE_INVALID_CONSTANT => "invalid constant string", + :HPE_INVALID_INTERNAL_STATE => "encountered unexpected internal state", + :HPE_STRICT => "strict mode assertion failed", +) + + +""" +Tokens as defined by rfc 2616. Also lowercases them. + token = 1* + separators = "(" | ")" | "<" | ">" | "@" + | "," | ";" | ":" | "\" | <"> + | "/" | "[" | "]" | "?" | "=" + | "{" | "}" | SP | HT +""" + +const tokens = Char[ +#= 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel =# + 0, 0, 0, 0, 0, 0, 0, 0, +#= 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si =# + 0, 0, 0, 0, 0, 0, 0, 0, +#= 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb =# + 0, 0, 0, 0, 0, 0, 0, 0, +#= 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us =# + 0, 0, 0, 0, 0, 0, 0, 0, +#= 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' =# + 0, '!', 0, '#', '$', '%', '&', '\'', +#= 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / =# + 0, 0, '*', '+', 0, '-', '.', 0, +#= 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 =# + '0', '1', '2', '3', '4', '5', '6', '7', +#= 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? =# + '8', '9', 0, 0, 0, 0, 0, 0, +#= 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G =# + 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', +#= 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O =# + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +#= 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W =# + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +#= 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ =# + 'x', 'y', 'z', 0, 0, 0, '^', '_', +#= 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g =# + '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', +#= 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o =# + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', +#= 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w =# + 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +#= 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del =# + 'x', 'y', 'z', 0, '|', 0, '~', 0 ] + +istoken(c) = tokens[UInt8(c)+1] != Char(0) + + +const unhex = Int8[ + -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 + ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 +] + + Base.show(io::IO, p::Parser) = print(io, "Parser(", "state=", ParsingStateCode(p.state), ", ", "trailing=", p.trailing, ", ", "message=", p.message, ")") + end # module Parsers diff --git a/src/consts.jl b/src/consts.jl index 2a4eba752..d08726410 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -1,140 +1,22 @@ -@enum(Method, - DELETE=0, - GET=1, - HEAD=2, - POST=3, - PUT=4, - # pathological - CONNECT=5, - OPTIONS=6, - TRACE=7, - # WebDAV - COPY=8, - LOCK=9, - MKCOL=10, - MOVE=11, - PROPFIND=12, - PROPPATCH=13, - SEARCH=14, - UNLOCK=15, - BIND=16, - REBIND=17, - UNBIND=18, - ACL=19, - # subversion - REPORT=20, - MKACTIVITY=21, - CHECKOUT=22, - MERGE=23, - # upnp - MSEARCH=24, - NOTIFY=25, - SUBSCRIBE=26, - UNSUBSCRIBE=27, - # RFC-5789 - PATCH=28, - PURGE=29, - # CalDAV - MKCALENDAR=30, - # RFC-2068, section 19.6.1.2 - LINK=31, - UNLINK=32, - xHTTP, - NOMETHOD -) - -const MethodMap = Dict( - "HTTP" => xHTTP, - "DELETE" => DELETE, - "GET" => GET, - "HEAD" => HEAD, - "POST" => POST, - "PUT" => PUT, - "CONNECT" => CONNECT, - "OPTIONS" => OPTIONS, - "TRACE" => TRACE, - "COPY" => COPY, - "LOCK" => LOCK, - "MKCOL" => MKCOL, - "MOVE" => MOVE, - "PROPFIND" => PROPFIND, - "PROPPATCH" => PROPPATCH, - "SEARCH" => SEARCH, - "UNLOCK" => UNLOCK, - "BIND" => BIND, - "REBIND" => REBIND, - "UNBIND" => UNBIND, - "ACL" => ACL, - "REPORT" => REPORT, - "MKACTIVITY" => MKACTIVITY, - "CHECKOUT" => CHECKOUT, - "MERGE" => MERGE, - "MSEARCH" => MSEARCH, - "NOTIFY" => NOTIFY, - "SUBSCRIBE" => SUBSCRIBE, - "UNSUBSCRIBE" => UNSUBSCRIBE, - "PATCH" => PATCH, - "PURGE" => PURGE, - "MKCALENDAR" => MKCALENDAR, - "LINK" => LINK, - "UNLINK" => UNLINK, -) -Base.convert(::Type{Method}, s::String) = MethodMap[s] - -# parsing codes -@enum(ParsingErrorCode, - HPE_OK, - HPE_INVALID_VERSION, - HPE_INVALID_STATUS, - HPE_INVALID_METHOD, - HPE_INVALID_URL, - HPE_LF_EXPECTED, - HPE_INVALID_HEADER_TOKEN, - HPE_INVALID_CONTENT_LENGTH, - HPE_UNEXPECTED_CONTENT_LENGTH, - HPE_INVALID_CHUNK_SIZE, - HPE_INVALID_CONSTANT, - HPE_INVALID_INTERNAL_STATE, - HPE_STRICT, - HPE_UNKNOWN, -) - -const ParsingErrorCodeMap = Dict( - HPE_OK => "success", - HPE_INVALID_VERSION => "invalid HTTP version", - HPE_INVALID_STATUS => "invalid HTTP status code", - HPE_INVALID_METHOD => "invalid HTTP method", - HPE_INVALID_URL => "invalid URL", - HPE_LF_EXPECTED => "LF character expected", - HPE_INVALID_HEADER_TOKEN => "invalid character in header", - HPE_INVALID_CONTENT_LENGTH => "invalid character in content-length header", - HPE_UNEXPECTED_CONTENT_LENGTH => "unexpected content-length header", - HPE_INVALID_CHUNK_SIZE => "invalid character in chunk size header", - HPE_INVALID_CONSTANT => "invalid constant string", - HPE_INVALID_INTERNAL_STATE => "encountered unexpected internal state", - HPE_STRICT => "strict mode assertion failed", - HPE_UNKNOWN => "an unknown error occurred", -) - # parsing state codes @enum(ParsingStateCode ,es_dead=1 - ,es_start_req_or_res=2 - ,es_res_or_resp_H=3 - ,es_res_first_http_major=9 - ,es_res_http_major=10 - ,es_res_first_http_minor=11 - ,es_res_http_minor=12 - ,es_res_first_status_code=13 - ,es_res_status_code=14 - ,es_res_status_start=15 - ,es_res_status=16 - ,es_res_line_almost_done=17 - ,es_start_req=18 - ,es_req_method=19 - ,es_req_spaces_before_url=20 - ,es_req_url_start=21 - ,es_req_schema=22 + ,es_start_req_or_res + ,es_res_or_resp_H + ,es_res_first_http_major + ,es_res_http_major + ,es_res_first_http_minor + ,es_res_http_minor + ,es_res_first_status_code + ,es_res_status_code + ,es_res_status_start + ,es_res_status + ,es_res_line_almost_done + ,es_start_req + ,es_req_method + ,es_req_spaces_before_url + ,es_req_url_start + ,es_req_schema ,es_req_schema_slash ,es_req_schema_slash_slash ,es_req_server_start @@ -183,113 +65,9 @@ for i in instances(ParsingStateCode) @eval const $(Symbol(string(i)[2:end])) = UInt8($i) end + const CR = '\r' const bCR = UInt8('\r') const LF = '\n' const bLF = UInt8('\n') const CRLF = "\r\n" - -#= Tokens as defined by rfc 2616. Also lowercases them. - # token = 1* - # separators = "(" | ")" | "<" | ">" | "@" - # | "," | ";" | ":" | "\" | <"> - # | "/" | "[" | "]" | "?" | "=" - # | "{" | "}" | SP | HT - =# -const tokens = Char[ -#= 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel =# - 0, 0, 0, 0, 0, 0, 0, 0, -#= 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si =# - 0, 0, 0, 0, 0, 0, 0, 0, -#= 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb =# - 0, 0, 0, 0, 0, 0, 0, 0, -#= 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us =# - 0, 0, 0, 0, 0, 0, 0, 0, -#= 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' =# - 0, '!', 0, '#', '$', '%', '&', '\'', -#= 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / =# - 0, 0, '*', '+', 0, '-', '.', 0, -#= 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 =# - '0', '1', '2', '3', '4', '5', '6', '7', -#= 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? =# - '8', '9', 0, 0, 0, 0, 0, 0, -#= 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G =# - 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', -#= 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O =# - 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', -#= 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W =# - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', -#= 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ =# - 'x', 'y', 'z', 0, 0, 0, '^', '_', -#= 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g =# - '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', -#= 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o =# - 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', -#= 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w =# - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', -#= 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 del =# - 'x', 'y', 'z', 0, '|', 0, '~', 0 ] - -istoken(c) = tokens[UInt8(c)+1] != Char(0) - -const unhex = Int8[ - -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - , 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1 - ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1 - ,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 -] - -# url parsing -const normal_url_char = Bool[ -#= 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel =# - false, false, false, false, false, false, false, false, -#= 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si =# - false, true, false, false, true, false, false, false, -#= 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb =# - false, false, false, false, false, false, false, false, -#= 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us =# - false, false, false, false, false, false, false, false, -#= 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' =# - false, true, true, false, true, true, true, true, -#= 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / =# - true, true, true, true, true, true, true, true, -#= 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 =# - true, true, true, true, true, true, true, true, -#= 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? =# - true, true, true, true, true, true, true, false, -#= 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G =# - true, true, true, true, true, true, true, true, -#= 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O =# - true, true, true, true, true, true, true, true, -#= 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W =# - true, true, true, true, true, true, true, true, -#= 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ =# - true, true, true, true, true, true, true, true, -#= 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g =# - true, true, true, true, true, true, true, true, -#= 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o =# - true, true, true, true, true, true, true, true, -#= 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w =# - true, true, true, true, true, true, true, true, -#= 120 x 121 y 122 z 123 { 124, 125 } 126 ~ 127 del =# - true, true, true, true, true, true, true, false, -] - -@enum(http_host_state, - s_http_host_dead = 1, - s_http_userinfo_start =2, - s_http_userinfo = 3, - s_http_host_start = 4, - s_http_host_v6_start = 5, - s_http_host = 6, - s_http_host_v6, - s_http_host_v6_end, - s_http_host_v6_zone_start, - s_http_host_v6_zone, - s_http_host_port_start, - s_http_host_port, -) diff --git a/src/handlers.jl b/src/handlers.jl index cb410e182..64902cbf3 100644 --- a/src/handlers.jl +++ b/src/handlers.jl @@ -66,19 +66,14 @@ struct Router <: Handler end 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)) -end const EMPTYVAL = Val(()) """ HTTP.register!(r::Router, url, handler) -HTTP.register!(r::Router, m::Union{HTTP.Method, String}, url, handler) +HTTP.register!(r::Router, m::String, url, handler) Function to map request urls matching `url` and an optional method `m` to another `handler::HTTP.Handler`. URLs are registered one at a time, and multiple urls can map to the same handler. -Methods can be passed as a string `"GET"` or enum object directly `HTTP.GET`. The URL can be passed as a String or `HTTP.URI` object directly. Requests can be routed based on: method, scheme, hostname, or path. The following examples show how various urls will direct how a request is routed by a server: @@ -91,10 +86,9 @@ The following examples show how various urls will direct how a request is routed - `"/gmail/userId/*/inbox`: match any request matching the path pattern, "*" is used as a wildcard that matches any value between the two "/" """ register!(r::Router, url, handler) = register!(r, "", url, handler) -register!(r::Router, m::HTTP.Method, url, handler) = register!(r, string(m), url, handler) function register!(r::Router, method::String, url, handler) - m = isempty(method) ? Any : typeof(METHODS[method]) + m = isempty(method) ? Any : typeof(Val(Symbol(method))) # get scheme, host, split path into strings & vals uri = url isa String ? HTTP.URI(url) : url s = uri.scheme diff --git a/src/multipart.jl b/src/multipart.jl index 98d4a61ef..7487f56d0 100644 --- a/src/multipart.jl +++ b/src/multipart.jl @@ -59,7 +59,7 @@ function Form(d::Dict) io = IOBuffer() len = length(d) for (i, (k, v)) in enumerate(d) - write(io, (i == 1 ? "" : "$CRLF") * "--" * boundary * "$CRLF") + write(io, (i == 1 ? "" : "\r\n") * "--" * boundary * "\r\n") write(io, "Content-Disposition: form-data; name=\"$k\"") if isa(v, IO) writemultipartheader(io, v) @@ -68,10 +68,10 @@ function Form(d::Dict) push!(data, v) io = IOBuffer() else - write(io, "$CRLF$CRLF") + write(io, "\r\n\r\n") write(io, escapeuri(v)) end - i == len && write(io, "$CRLF--" * boundary * "--" * "$CRLF") + i == len && write(io, "\r\n--" * boundary * "--" * "\r\n") end seekstart(io) push!(data, io) @@ -79,12 +79,12 @@ function Form(d::Dict) end function writemultipartheader(io::IOBuffer, i::IOStream) - write(io, "; filename=\"$(i.name[7:end-1])\"$CRLF") - write(io, "Content-Type: $(HTTP.sniff(i))$CRLF$CRLF") + write(io, "; filename=\"$(i.name[7:end-1])\"\r\n") + write(io, "Content-Type: $(HTTP.sniff(i))\r\n\r\n") return end function writemultipartheader(io::IOBuffer, i::IO) - write(io, "$CRLF$CRLF") + write(io, "\r\n\r\n") return end @@ -114,9 +114,9 @@ Base.mark(m::Multipart{T}) where {T} = mark(m.data) Base.reset(m::Multipart{T}) where {T} = reset(m.data) function writemultipartheader(io::IOBuffer, i::Multipart) - write(io, "; filename=\"$(i.filename)\"$CRLF") + write(io, "; filename=\"$(i.filename)\"\r\n") contenttype = i.contenttype == "" ? HTTP.sniff(i.data) : i.contenttype - write(io, "Content-Type: $(contenttype)$CRLF") - write(io, i.contenttransferencoding == "" ? "$CRLF" : "Content-Transfer-Encoding: $(i.contenttransferencoding)$CRLF$CRLF") + write(io, "Content-Type: $(contenttype)\r\n") + write(io, i.contenttransferencoding == "" ? "\r\n" : "Content-Transfer-Encoding: $(i.contenttransferencoding)\r\n\r\n") return end diff --git a/src/parseutils.jl b/src/parseutils.jl index c3e68925f..360d169c6 100644 --- a/src/parseutils.jl +++ b/src/parseutils.jl @@ -13,7 +13,6 @@ macro anyeq(var, vals...) end @inline lower(c) = Char(UInt32(c) | 0x20) -@inline isurlchar(c) = c > '\u80' ? true : normal_url_char[Int(c) + 1] @inline ismark(c) = @anyeq(c, '-', '_', '.', '!', '~', '*', '\'', '(', ')') @inline isalpha(c) = 'a' <= lower(c) <= 'z' @inline isnum(c) = '0' <= c <= '9' diff --git a/src/urlparser.jl b/src/urlparser.jl index 65aa2c18f..581ea48db 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -6,6 +6,21 @@ struct URLParsingError <: Exception end Base.show(io::IO, p::URLParsingError) = println(io, "HTTP.URLParsingError: ", p.msg) +@enum(http_host_state, + s_http_host_dead, + s_http_userinfo_start, + s_http_userinfo, + s_http_host_start, + s_http_host_v6_start, + s_http_host, + s_http_host_v6, + s_http_host_v6_end, + s_http_host_v6_zone_start, + s_http_host_v6_zone, + s_http_host_port_start, + s_http_host_port, +) + @enum(http_parser_url_fields, UF_SCHEME = 1 , UF_HOST = 2 @@ -254,3 +269,40 @@ function http_parser_parse_url(url::AbstractString, isconnect::Bool=false) return URI(url, parts...) end + +const normal_url_char = Bool[ +#= 0 nul 1 soh 2 stx 3 etx 4 eot 5 enq 6 ack 7 bel =# + false, false, false, false, false, false, false, false, +#= 8 bs 9 ht 10 nl 11 vt 12 np 13 cr 14 so 15 si =# + false, true, false, false, true, false, false, false, +#= 16 dle 17 dc1 18 dc2 19 dc3 20 dc4 21 nak 22 syn 23 etb =# + false, false, false, false, false, false, false, false, +#= 24 can 25 em 26 sub 27 esc 28 fs 29 gs 30 rs 31 us =# + false, false, false, false, false, false, false, false, +#= 32 sp 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' =# + false, true, true, false, true, true, true, true, +#= 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / =# + true, true, true, true, true, true, true, true, +#= 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 =# + true, true, true, true, true, true, true, true, +#= 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? =# + true, true, true, true, true, true, true, false, +#= 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G =# + true, true, true, true, true, true, true, true, +#= 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O =# + true, true, true, true, true, true, true, true, +#= 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W =# + true, true, true, true, true, true, true, true, +#= 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ =# + true, true, true, true, true, true, true, true, +#= 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g =# + true, true, true, true, true, true, true, true, +#= 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o =# + true, true, true, true, true, true, true, true, +#= 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w =# + true, true, true, true, true, true, true, true, +#= 120 x 121 y 122 z 123 { 124, 125 } 126 ~ 127 del =# + true, true, true, true, true, true, true, false, +] + +@inline isurlchar(c) = c > '\u80' ? true : normal_url_char[Int(c) + 1] diff --git a/test/client.jl b/test/client.jl index 8b1a23c15..1754f22c0 100644 --- a/test/client.jl +++ b/test/client.jl @@ -191,7 +191,7 @@ for sch in ("http", "https") r = HTTP.get(cli, uri) @test status(r) == 200 - r = HTTP.request(HTTP.GET, "$sch://httpbin.org/ip") + r = HTTP.request("GET", "$sch://httpbin.org/ip") @test status(r) == 200 uri = HTTP.URI("$sch://httpbin.org/ip") diff --git a/test/handlers.jl b/test/handlers.jl index 8af4baf32..654c23cdf 100644 --- a/test/handlers.jl +++ b/test/handlers.jl @@ -34,7 +34,7 @@ req.uri = "/next/path/to/greatness" r = HTTP.Router() HTTP.register!(r, "GET", "/sget", f) HTTP.register!(r, "POST", "/spost", f) -HTTP.register!(r, HTTP.POST, "/tpost", f) +HTTP.register!(r, "POST", "/tpost", f) req = HTTP.Request("GET", "/sget") @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(200) req = HTTP.Request("POST", "/sget") diff --git a/test/parser.jl b/test/parser.jl index 44f4603e3..d33110158 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1776,10 +1776,10 @@ const responses = Message[ respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "FFFFFFFFFFFFFFF" * "\r\n..." e = try Response(respstr) catch e e end - @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH + @test isa(e, ParsingError) && e.code == :HPE_INVALID_CONTENT_LENGTH respstr = "HTTP/1.1 200 OK\r\n" * "Transfer-Encoding: chunked\r\n\r\n" * "10000000000000000" * "\r\n..." e = try Response(respstr) catch e e end - @test isa(e, ParsingError) && e.code == Parsers.HPE_INVALID_CONTENT_LENGTH + @test isa(e, ParsingError) && e.code == :HPE_INVALID_CONTENT_LENGTH for len in (1000, 100000) b = IOBuffer() @@ -1848,11 +1848,9 @@ const responses = Message[ parse!(p, r, b, "GET / HTTP/1.1\r\n" * "Content-Type: text/plain\r\n" * "Content-Length: 6\r\n\r\n" * "fooba") @test String(take!(b)) == "fooba" - for m in instances(Parsers.Method) - m in (Parsers.xHTTP, Parsers.NOMETHOD, Parsers.CONNECT) && continue - me = m == Parsers.MSEARCH ? "M-SEARCH" : "$m" - r = Request("$me / HTTP/1.1\r\n\r\n") - @test r.method == string(me) + for m in ["GET", "PUT", "M-SEARCH", "FOOMETHOD"] + r = Request("$m / HTTP/1.1\r\n\r\n") + @test r.method == string(m) end for m in ("HTTP/1.1", "hello world") diff --git a/test/utils.jl b/test/utils.jl index ffab98a41..a064263b1 100644 --- a/test/utils.jl +++ b/test/utils.jl @@ -1,11 +1,12 @@ @testset "utils.jl" begin import HTTP.Parsers +import HTTP.URIs @test HTTP.Strings.escapehtml("&\"'<>") == "&"'<>" -@test Parsers.isurlchar('\u81') -@test !Parsers.isurlchar('\0') +@test URIs.isurlchar('\u81') +@test !URIs.isurlchar('\0') for c = '\0':'\x7f' if c in ('.', '-', '_', '~') From 934b4deb0b6bc138935e6c930af36f0d0b0f71c0 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 01:13:34 +1100 Subject: [PATCH 164/182] Server doc updates --- docs/src/index.md | 9 ++---- src/HTTP.jl | 2 +- src/Servers.jl | 81 ++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 74 insertions(+), 18 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 79de83970..f55d0519a 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -53,8 +53,9 @@ Base.DNSError ## Server / Handlers ```@docs -HTTP.serve -HTTP.Server +HTTP.listen +HTTP.Servers.serve +HTTP.Servers.Server HTTP.Handler HTTP.HandlerFunction HTTP.Router @@ -159,10 +160,6 @@ HTTP.Parsers.headerscomplete HTTP.Parsers.bodycomplete HTTP.Parsers.messagecomplete HTTP.Parsers.messagehastrailing -HTTP.Parsers.waitingforeof -HTTP.Parsers.seteof -HTTP.Parsers.connectionclosed -HTTP.Parsers.setnobody ``` ## Messages Interface diff --git a/src/HTTP.jl b/src/HTTP.jl index 14a3d10e7..267472de7 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -126,7 +126,7 @@ SSLContext options the mbed TLS library. ["... peer must present a valid certificate, handshake is aborted if verification failed."](https://tls.mbed.org/api/ssl_8h.html#a5695285c9dbfefec295012b566290f37) - - sslconfig = SSLConfig(require_ssl_verification)` + - `sslconfig = SSLConfig(require_ssl_verification)` Basic Authenticaiton options diff --git a/src/Servers.jl b/src/Servers.jl index 2a87c9f20..d30af427f 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -141,10 +141,10 @@ function handle_request(f, server, io, i, verbose=true) catch e if e isa HTTP.ParsingError HTTP.@log "error parsing request on connection i=$i: " * - HTTP.ParsingErrorCodeMap[err.code] - response.status = e.code == Parsers.HPE_INVALID_VERSION ? 505 : - e.code == Parsers.HPE_INVALID_METHOD ? 405 : 400 - response.body = HTTP.ParsingErrorCodeMap[err.code] + HTTP.Parsers.ERROR_MESSAGES[err.code] + response.status = e.code == :HPE_INVALID_VERSION ? 505 : + e.code == :HPE_INVALID_METHOD ? 405 : 400 + response.body = HTTP.Parsers.ERROR_MESSAGES[err.code] else close(io) rethrow(e) @@ -384,15 +384,54 @@ function getsslcontext(tcp, sslconfig) return ssl end +const nosslconfig = SSLConfig() + + +""" + HTTP.listen(host="localhost", port=8081; ) do http::HTTP.Stream + ... + end + +Listen for HTTP connections and execute the `do` function for each request. + +Optional keyword arguments: + - `ssl::Bool = false`, use https. + - `require_ssl_verification = true`, pass `MBEDTLS_SSL_VERIFY_REQUIRED` to + the mbed TLS library. + ["... peer must present a valid certificate, handshake is aborted if + verification failed."](https://tls.mbed.org/api/ssl_8h.html#a5695285c9dbfefec295012b566290f37) + - `sslconfig = SSLConfig(require_ssl_verification)` + - `pipeline_limit = 16`, number of concurrent requests per connection. + +e.g. +``` + HTTP.listen() do http + @show http.message + @show header(http, "Content-Type") + while !eof(http) + println("body data: ", String(readavailable(http))) + end + setstatus(http, 404) + setheader(http, "Foo-Header" => "bar") + startwrite(http) + write(http, "response body") + write(http, "more response body") + end +``` +""" function listen(f::Function, host::String="127.0.0.1", port::UInt16=UInt16(8081); ssl::Bool=false, - require_ssl_verification::Bool=false, - sslconfig::SSLConfig=SSLConfig(require_ssl_verification), + require_ssl_verification::Bool=true, + sslconfig::SSLConfig=nosslconfig, pipeline_limit::Int=ConnectionPool.default_pipeline_limit, kw...) + if sslconfig === nosslconfig + sslconfig = SSLConfig(require_ssl_verification) + end + @info "Listening on: $(host):$(port)" tcpserver = Base.listen(getaddrinfo(host), port) @@ -429,6 +468,11 @@ function listen(f::Function, end +""" +Start a timeout monitor task to close the `Connection` if it is inactive. +Create a `Transaction` object for each HTTP Request received. +""" + function handle_connection(f::Function, c::Connection; readtimeout::Int=0, kw...) @@ -458,7 +502,14 @@ function handle_connection(f::Function, c::Connection; end -function handle_transaction(f::Function, t; verbose=false, kw...) +""" +Create a `HTTP.Stream` and parse the Request headers from a `HTTP.Transaction`. +If there is a parse error, send an error Response. +Otherwise, execute stream processing function `f`. +""" + +function handle_transaction(f::Function, t::Transaction; + verbose=false, kw...) request = HTTP.Request() http = Streams.Stream(request, ConnectionPool.getparser(t), t) @@ -472,10 +523,10 @@ function handle_transaction(f::Function, t; verbose=false, kw...) return elseif e isa HTTP.ParsingError @error e - status = e.code == Parsers.HPE_INVALID_VERSION ? 505 : - e.code == Parsers.HPE_INVALID_METHOD ? 405 : 400 + status = e.code == :HPE_INVALID_VERSION ? 505 : + e.code == :HPE_INVALID_METHOD ? 405 : 400 write(http.stream, - Response(status, body = HTTP.ParsingErrorCodeMap[err.code])) + Response(status, body = HTTP.Parsers.ERROR_MESSAGES[e.code])) else rethrow(e) end @@ -499,7 +550,15 @@ function handle_transaction(f::Function, t; verbose=false, kw...) end -function handle_stream(f::Function, http) +""" +Execute stream processing function `f`. +If there is an error and the stream is still open, +send a 500 response with the error message. + +Close the `Stream` for read and write (in case `f` has not already done so). +""" + +function handle_stream(f::Function, http::Stream) try f(http) From cc76f4f49f9ea3473527215b554d9b84d0389a29 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 02:16:05 +1100 Subject: [PATCH 165/182] untested integration of Server and serve with listen --- src/Servers.jl | 268 ++++++++++++------------------------------------- 1 file changed, 65 insertions(+), 203 deletions(-) diff --git a/src/Servers.jl b/src/Servers.jl index d30af427f..58f3dd19a 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -106,126 +106,6 @@ mutable struct Server{T <: Scheme, H <: HTTP.Handler} new{T, H}(handler, logger, ch, ch2, options) end -backtrace() = sprint(Base.show_backtrace, catch_backtrace()) - - -function handle_request(f, server, io, i, verbose=true) - - logger = server.logger - - request = HTTP.Request() - http = Streams.Stream(request, ConnectionPool.getparser(io), io) - response = request.response - response.status = 200 - - try - startread(http) - - if header(request, "Expect") == "100-continue" - if server.options.support100continue - response.status = 100 - startwrite(http) - response.status = 200 - else - response.status = 417 - end - end - - if http.parser.message.upgrade - HTTP.@log "received upgrade request on connection i=$i" - response.status = 501 - response.body = - Vector{UInt8}("upgrade requests are not currently supported") - end - - catch e - if e isa HTTP.ParsingError - HTTP.@log "error parsing request on connection i=$i: " * - HTTP.Parsers.ERROR_MESSAGES[err.code] - response.status = e.code == :HPE_INVALID_VERSION ? 505 : - e.code == :HPE_INVALID_METHOD ? 405 : 400 - response.body = HTTP.Parsers.ERROR_MESSAGES[err.code] - else - close(io) - rethrow(e) - end - end - - if iserror(response) - startwrite(http) - write(http, response.body) - close(io) - return - end - - HTTP.@log "received request on connection i=$i" - verbose && (show(logger, request); println(logger, "")) - - @async try - - try - f(http) - catch e - if !iswritable(io) - showerror(logger, e) - println(logger, backtrace()) - response.status = 500 - startwrite(http) - write(http, sprint(showerror, e)) - else - rethrow(e) - end - end - - closeread(http) - closewrite(http) - - catch e - close(io) - rethrow(e) - end -end - - - -function handle_connection(f, server::Server{T, H}, i, io::Connection{ST}, rl, verbose) where {T, H, ST} - logger = server.logger - rate = Float64(server.options.ratelimit.num) - rl.allowance += 1.0 # because it was just decremented right before we got here - HTTP.@log "processing on connection i=$i..." - while isopen(io) - update!(rl, server.options.ratelimit) - if rl.allowance > rate - HTTP.@log "throttling on connection i=$i" - rl.allowance = rate - end - if rl.allowance < 1.0 - HTTP.@log "sleeping on connection i=$i due to rate limiting" - sleep(1.0) - else - rl.allowance -= 1.0 - end - handle_request(f, server, ConnectionPool.Transaction{ST}(io), i) - end -end - - -init_connection(::Server{http}, tcp) = tcp - -function init_connection(server::Server{https}, tcp) - tls_config = server.options.tlsconfig::HTTP.MbedTLS.SSLConfig - try - tls = HTTP.MbedTLS.SSLContext() - HTTP.MbedTLS.setup!(tls, tls_config) - HTTP.MbedTLS.associate!(tls, tcp) - HTTP.MbedTLS.handshake!(tls) - return tls - catch e - close(tcp) - error("Error establishing SSL connection: $e") - rethrow(e) - end -end mutable struct RateLimit allowance::Float64 @@ -240,89 +120,48 @@ function update!(rl::RateLimit, ratelimit) return nothing end +function check_rate_limit(tcp; + rate_limits=nothing, + rate_limit=Rational{Int64}=Int64(5)//Int64(1), kw...) + ip = getsockname(tcp)[1] + rate = Float64(rate_limit.num) + rl = get!(ratelimits, ip, RateLimit(rate, Dates.now())) + update!(rl, rate_limit) + if rl.allowance > rate + @warn "throttling $ip" + rl.allowance = rate + end + if rl.allowance < 1.0 + @warn "discarding connection from $ip due to rate limiting" + return false + else + rl.allowance -= 1.0 + end + return true +end + + @enum Signals KILL -function serve(f, server::Server{T, H}, host, port, verbose) where {T, H} - logger = server.logger - HTTP.@log "starting server to listen on: $(host):$(port)" - tcpserver = Base.listen(host, port) - ratelimits = Dict{IPAddr, RateLimit}() - rate = Float64(server.options.ratelimit.num) - i = 0 +function serve(server::Server{T, H}, host, port, verbose) where {T, H} + +#= FIXME @async begin while true val = take!(server.in) val == KILL && close(tcpserver) end end - while true - try - # accept blocks until a new connection is detected - tcp = accept(tcpserver) - ip = getsockname(tcp)[1] - rl = get!(ratelimits, ip, RateLimit(rate, Dates.now())) - update!(rl, server.options.ratelimit) - if rl.allowance > rate - HTTP.@log "throttling $ip" - rl.allowance = rate - end - if rl.allowance < 1.0 - HTTP.@log "discarding connection from $ip due to rate limiting" - close(tcp) - else - rl.allowance -= 1.0 - HTTP.@log "new tcp connection accepted, reading request..." - tcp = init_connection(server, tcp) - SocketType = T == https ? HTTP.MbedTLS.SSLContext : TCPSocket - c = Connection{SocketType}(tcp) - let server=server, i=i, c=c, rl=rl - wait_for_timeout = Ref{Bool}(true) - readtimeout = server.options.readtimeout - @async while wait_for_timeout[] - if inactiveseconds(c) > readtimeout - - # FIXME send a 408 ? - - close(io) - HTTP.@log "Connection timeout i=$i" - break - end - sleep(8 + rand() * 4) - end - @async try - handle_connection(f, server, i, c, rl, verbose) - catch e - if e isa EOFError - HTTP.@log "connection i=$i: $e" - else - rethrow(e) - end - finally - HTTP.@log "finished processing on connection i=$i" - wait_for_timeout[] = false - close(c) - end - end - i += 1 - end - catch e - if typeof(e) <: InterruptException - HTTP.@log "interrupt detected, shutting down..." - interrupt() - break - else - if !isopen(tcpserver) - HTTP.@log "server TCPServer is closed, shutting down..." - # Server was closed while waiting to accept client. Exit gracefully. - interrupt() - break - end - HTTP.@log "error encountered: $e" - HTTP.@log "resuming serving..." - end - end - end - close(tcpserver) +=# + + listen(host, port; + ssl=(T == https), + sslconfig=server.options.tlsconfig, + verbose=verbose, + isvalid=check_rate_limit, + rate_limits=Dict{IPAddr, RateLimit}(), + rate_limit=server.options.ratelimit) + return end @@ -352,8 +191,8 @@ By default, `HTTP.serve` aims to "never die", catching and recovering from all i """ function serve end -serve(f, server::Server, host=IPv4(127,0,0,1), port=8081; verbose::Bool=true) = serve(f, server, host, port, verbose) -function serve(f, host::IPAddr, port::Int, +serve(server::Server, host=IPv4(127,0,0,1), port=8081; verbose::Bool=true) = serve(server, host, port, verbose) +function serve(host::IPAddr, port::Int, handler=(req, rep) -> HTTP.Response("Hello World!"), logger::I=STDOUT; cert::String="", @@ -361,9 +200,9 @@ function serve(f, host::IPAddr, port::Int, verbose::Bool=true, args...) where {I} server = Server(handler, logger; cert=cert, key=key, args...) - return serve(f, server, host, port, verbose) + return serve(server, host, port, verbose) end -serve(f, ; host::IPAddr=IPv4(127,0,0,1), +serve(; host::IPAddr=IPv4(127,0,0,1), port::Int=8081, handler=(req, rep) -> HTTP.Response("Hello World!"), logger::IO=STDOUT, @@ -371,7 +210,7 @@ serve(f, ; host::IPAddr=IPv4(127,0,0,1), key::String="", verbose::Bool=true, args...) = - serve(f, host, port, handler, logger; cert=cert, key=key, verbose=verbose, args...) + serve(host, port, handler, logger; cert=cert, key=key, verbose=verbose, args...) @@ -386,6 +225,8 @@ end const nosslconfig = SSLConfig() +const nolimit = typemax(Int) + """ HTTP.listen(host="localhost", port=8081; ) do http::HTTP.Stream @@ -402,6 +243,11 @@ Optional keyword arguments: verification failed."](https://tls.mbed.org/api/ssl_8h.html#a5695285c9dbfefec295012b566290f37) - `sslconfig = SSLConfig(require_ssl_verification)` - `pipeline_limit = 16`, number of concurrent requests per connection. + - `reuse_limit = nolimit`, number of times a connection is allowed to be reused + after the first request. + - `isvalid::Function (::TCPSocket) -> Bool`, check accepted connection before + processing requests. e.g. to implement source IP filtering, rate-limiting, + etc. e.g. ``` @@ -426,6 +272,7 @@ function listen(f::Function, require_ssl_verification::Bool=true, sslconfig::SSLConfig=nosslconfig, pipeline_limit::Int=ConnectionPool.default_pipeline_limit, + isvalid::Function = (tcp; kw...)->true, kw...) if sslconfig === nosslconfig @@ -439,6 +286,10 @@ function listen(f::Function, while isopen(tcpserver) try io = accept(tcpserver) + if !isvalid(io; kw...) + close(io) + continue + end io = ssl ? getsslcontext(io, sslconfig) : io let io = Connection(host, string(port), pipeline_limit, io) @info "Accept: $io" @@ -474,6 +325,7 @@ Create a `Transaction` object for each HTTP Request received. """ function handle_connection(f::Function, c::Connection; + reuse_limit::Int=nolimit, readtimeout::Int=0, kw...) wait_for_timeout = Ref{Bool}(true) @@ -491,9 +343,14 @@ function handle_connection(f::Function, c::Connection; end try + count = 0 while isopen(c) io = Transaction(c) - handle_transaction(f, io; kw...) + handle_transaction(f, io; close=(count == reuse_limit), kw...) + if count == reuse_limit + close(c) + end + count += 1 end finally wait_for_timeout[] = false @@ -509,12 +366,16 @@ Otherwise, execute stream processing function `f`. """ function handle_transaction(f::Function, t::Transaction; + close=false, verbose=false, kw...) request = HTTP.Request() http = Streams.Stream(request, ConnectionPool.getparser(t), t) response = request.response response.status = 200 + if close + setheader(response, "Connection" => "close") + end try startread(http) @@ -525,8 +386,9 @@ function handle_transaction(f::Function, t::Transaction; @error e status = e.code == :HPE_INVALID_VERSION ? 505 : e.code == :HPE_INVALID_METHOD ? 405 : 400 - write(http.stream, - Response(status, body = HTTP.Parsers.ERROR_MESSAGES[e.code])) + write(t, Response(status, body = HTTP.Parsers.ERROR_MESSAGES[e.code])) + close(t) + return else rethrow(e) end From 3d5fc5869a5528b8184ea7245e180d0a2dcb5f69 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 02:21:28 +1100 Subject: [PATCH 166/182] FIXME --- src/Servers.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Servers.jl b/src/Servers.jl index 58f3dd19a..53459f06b 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -160,7 +160,11 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} verbose=verbose, isvalid=check_rate_limit, rate_limits=Dict{IPAddr, RateLimit}(), - rate_limit=server.options.ratelimit) + rate_limit=server.options.ratelimit) do http + + #FIXME run server.handler using http + + end return end From b7ce63cfc4d2f11751c82c5d229a8dd53bb414fa Mon Sep 17 00:00:00 2001 From: Eric Forgy Date: Sun, 14 Jan 2018 01:05:45 +0700 Subject: [PATCH 167/182] Add MicroLogging for Windows v0.6 --- src/Servers.jl | 6 +----- src/compat.jl | 5 +---- test/REQUIRE | 1 + 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Servers.jl b/src/Servers.jl index 53459f06b..8d147f60a 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -5,13 +5,9 @@ using ..Streams using ..Messages using ..Parsers using ..ConnectionPool -import ..@debug, ..@debugshow, ..DEBUG_LEVEL +import ..@info, ..@warn, ..@error, ..@debug, ..@debugshow, ..DEBUG_LEVEL using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! -if VERSION < v"0.7.0-DEV.2575" -import ..@info, ..@warn, ..@error -end - if !isdefined(Base, :Nothing) const Nothing = Void diff --git a/src/compat.jl b/src/compat.jl index 1a3d272d3..eb9c575a9 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -10,10 +10,7 @@ else # Julia v0.6 pairs(x) = [k => v for (k,v) in x] - macro debug(s) DEBUG_LEVEL > 0 ? :(("D- ", $(esc(s)))) : :() end - macro info(s) DEBUG_LEVEL > 0 ? :(println("I- ", $(esc(s)))) : :() end - macro warn(s) DEBUG_LEVEL > 0 ? :(println("W- ", $(esc(s)))) : :() end - macro error(s, a...) DEBUG_LEVEL > 0 ? :(println("E- ", $(esc((s, a...))))) : :() end + using MicroLogging # https://github.com/JuliaLang/Compat.jl/blob/master/src/Compat.jl#L551 import Base: Val diff --git a/test/REQUIRE b/test/REQUIRE index 404f5b366..c4c018438 100644 --- a/test/REQUIRE +++ b/test/REQUIRE @@ -1,2 +1,3 @@ JSON XMLDict +MicroLogging From 37e9bb1325d7de19270239e7f80ff3ffa7a529f4 Mon Sep 17 00:00:00 2001 From: Eric Forgy Date: Sun, 14 Jan 2018 01:21:55 +0700 Subject: [PATCH 168/182] Rename handlers.jl to Handlers.jl --- src/{handlers.jl => Handlers.jl} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{handlers.jl => Handlers.jl} (100%) diff --git a/src/handlers.jl b/src/Handlers.jl similarity index 100% rename from src/handlers.jl rename to src/Handlers.jl From b263d139ad93a7a033ddfa5b3be17b8a260bd80e Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 10:57:22 +1100 Subject: [PATCH 169/182] untested implementation of HTTP.serve() calling HTTP.listen() --- src/Servers.jl | 32 +++++++++++++++++++++++--------- src/compat.jl | 4 ---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Servers.jl b/src/Servers.jl index 8d147f60a..27e6caafe 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -141,16 +141,17 @@ end function serve(server::Server{T, H}, host, port, verbose) where {T, H} -#= FIXME + tcpserver = Ref{Base.TCPServer}() + @async begin while true val = take!(server.in) - val == KILL && close(tcpserver) + val == KILL && close(tcpserver[]) end end -=# listen(host, port; + tcpref=tcpserver, ssl=(T == https), sslconfig=server.options.tlsconfig, verbose=verbose, @@ -158,8 +159,15 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} rate_limits=Dict{IPAddr, RateLimit}(), rate_limit=server.options.ratelimit) do http - #FIXME run server.handler using http + request = http.message + request.body = read(http) + + response = request.response + + handle(server.handler, request, response) + startwrite(http) + write(http, response.body) end return @@ -248,6 +256,8 @@ Optional keyword arguments: - `isvalid::Function (::TCPSocket) -> Bool`, check accepted connection before processing requests. e.g. to implement source IP filtering, rate-limiting, etc. + - `tcpref::Ref{Base.TCPServer}`, this reference is set to the underlying + `TCPServer`. e.g. to allow closing the server. e.g. ``` @@ -272,7 +282,8 @@ function listen(f::Function, require_ssl_verification::Bool=true, sslconfig::SSLConfig=nosslconfig, pipeline_limit::Int=ConnectionPool.default_pipeline_limit, - isvalid::Function = (tcp; kw...)->true, + isvalid::Function=(tcp; kw...)->true, + tcpref::Ref{Base.TCPServer}=Ref{Base.TCPServer}(), kw...) if sslconfig === nosslconfig @@ -282,6 +293,8 @@ function listen(f::Function, @info "Listening on: $(host):$(port)" tcpserver = Base.listen(getaddrinfo(host), port) + tcpref[] = tcpserver + try while isopen(tcpserver) try @@ -346,7 +359,8 @@ function handle_connection(f::Function, c::Connection; count = 0 while isopen(c) io = Transaction(c) - handle_transaction(f, io; close=(count == reuse_limit), kw...) + handle_transaction(f, io; final_transaction=(count == reuse_limit), + kw...) if count == reuse_limit close(c) end @@ -366,14 +380,14 @@ Otherwise, execute stream processing function `f`. """ function handle_transaction(f::Function, t::Transaction; - close=false, - verbose=false, kw...) + final_transaction::Bool=false, + verbose::Bool=false, kw...) request = HTTP.Request() http = Streams.Stream(request, ConnectionPool.getparser(t), t) response = request.response response.status = 200 - if close + if final_transaction setheader(response, "Connection" => "close") end diff --git a/src/compat.jl b/src/compat.jl index eb9c575a9..edb2bf1a8 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -11,10 +11,6 @@ else # Julia v0.6 pairs(x) = [k => v for (k,v) in x] using MicroLogging - - # https://github.com/JuliaLang/Compat.jl/blob/master/src/Compat.jl#L551 - import Base: Val - (::Type{Val})(x) = (Base.@_pure_meta; Val{x}()) end macro uninit(expr) From 25944c8ac5b945ac81d73d86b9a0ce8abc73cd43 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 14:36:26 +1100 Subject: [PATCH 170/182] Server integration and tests. Add kw option to set http_version. In closewrite(::Stream{Request}) close the connection for non keep-alive HTTP/1.0. --- src/MessageRequest.jl | 4 +- src/Messages.jl | 5 ++- src/Servers.jl | 91 ++++++++++++++++++++++++------------------- src/Streams.jl | 6 ++- test/runtests.jl | 2 +- test/server.jl | 89 +++++++++++++++++++++++++++++------------- 6 files changed, 125 insertions(+), 72 deletions(-) diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index d3c5bd6bd..12481878d 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -21,6 +21,7 @@ export MessageLayer function request(::Type{MessageLayer{Next}}, method::String, uri::URI, headers::Headers, body; + http_version=v"1.1", parent=nothing, iofunction=nothing, kw...) where Next path = method == "CONNECT" ? hostport(uri) : resource(uri) @@ -38,7 +39,8 @@ function request(::Type{MessageLayer{Next}}, end end - req = Request(method, path, headers, bodybytes(body); parent=parent) + req = Request(method, path, headers, bodybytes(body); + parent=parent, version=http_version) return request(Next, uri, req, body; iofunction=iofunction, kw...) end diff --git a/src/Messages.jl b/src/Messages.jl index 8d567dc41..bce1c5892 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -152,10 +152,11 @@ end Request() = Request("", "") -function Request(method::String, uri, headers=[], body=UInt8[]; parent=nothing) +function Request(method::String, uri, headers=[], body=UInt8[]; + version=v"1.1", parent=nothing) r = Request(method, uri == "" ? "/" : uri, - v"1.1", + version, mkheaders(headers), body, Response(), diff --git a/src/Servers.jl b/src/Servers.jl index 27e6caafe..f9ed0f649 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -117,12 +117,12 @@ function update!(rl::RateLimit, ratelimit) end function check_rate_limit(tcp; - rate_limits=nothing, - rate_limit=Rational{Int64}=Int64(5)//Int64(1), kw...) + ratelimits=nothing, + ratelimit::Rational{Int64}=Int64(5)//Int64(1), kw...) ip = getsockname(tcp)[1] - rate = Float64(rate_limit.num) + rate = Float64(ratelimit.num) rl = get!(ratelimits, ip, RateLimit(rate, Dates.now())) - update!(rl, rate_limit) + update!(rl, ratelimit) if rl.allowance > rate @warn "throttling $ip" rl.allowance = rate @@ -144,6 +144,9 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} tcpserver = Ref{Base.TCPServer}() @async begin + while !isassigned(tcpserver) + sleep(1) + end while true val = take!(server.in) val == KILL && close(tcpserver[]) @@ -155,9 +158,10 @@ function serve(server::Server{T, H}, host, port, verbose) where {T, H} ssl=(T == https), sslconfig=server.options.tlsconfig, verbose=verbose, - isvalid=check_rate_limit, - rate_limits=Dict{IPAddr, RateLimit}(), - rate_limit=server.options.ratelimit) do http + tcpisvalid=server.options.ratelimit > 0 ? check_rate_limit : + (tcp; kw...) -> true, + ratelimits=Dict{IPAddr, RateLimit}(), + ratelimit=server.options.ratelimit) do http request = http.message request.body = read(http) @@ -199,7 +203,7 @@ By default, `HTTP.serve` aims to "never die", catching and recovering from all i """ function serve end -serve(server::Server, host=IPv4(127,0,0,1), port=8081; verbose::Bool=true) = serve(server, host, port, verbose) +serve(server::Server, host=ip"127.0.0.1", port=8081; verbose::Bool=true) = serve(server, host, port, verbose) function serve(host::IPAddr, port::Int, handler=(req, rep) -> HTTP.Response("Hello World!"), logger::I=STDOUT; @@ -210,7 +214,7 @@ function serve(host::IPAddr, port::Int, server = Server(handler, logger; cert=cert, key=key, args...) return serve(server, host, port, verbose) end -serve(; host::IPAddr=IPv4(127,0,0,1), +serve(; host::IPAddr=ip"127.0.0.1", port::Int=8081, handler=(req, rep) -> HTTP.Response("Hello World!"), logger::IO=STDOUT, @@ -253,7 +257,7 @@ Optional keyword arguments: - `pipeline_limit = 16`, number of concurrent requests per connection. - `reuse_limit = nolimit`, number of times a connection is allowed to be reused after the first request. - - `isvalid::Function (::TCPSocket) -> Bool`, check accepted connection before + - `tcpisvalid::Function (::TCPSocket) -> Bool`, check accepted connection before processing requests. e.g. to implement source IP filtering, rate-limiting, etc. - `tcpref::Ref{Base.TCPServer}`, this reference is set to the underlying @@ -276,13 +280,15 @@ e.g. ``` """ +listen(f, host, port; kw...) = listen(f, string(host), Int(port); kw...) + function listen(f::Function, - host::String="127.0.0.1", port::UInt16=UInt16(8081); + host::String="127.0.0.1", port::Int=8081; ssl::Bool=false, require_ssl_verification::Bool=true, sslconfig::SSLConfig=nosslconfig, pipeline_limit::Int=ConnectionPool.default_pipeline_limit, - isvalid::Function=(tcp; kw...)->true, + tcpisvalid::Function=(tcp; kw...)->true, tcpref::Ref{Base.TCPServer}=Ref{Base.TCPServer}(), kw...) @@ -290,7 +296,7 @@ function listen(f::Function, sslconfig = SSLConfig(require_ssl_verification) end - @info "Listening on: $(host):$(port)" + @info "Listening on: $host:$port" tcpserver = Base.listen(getaddrinfo(host), port) tcpref[] = tcpserver @@ -299,30 +305,36 @@ function listen(f::Function, while isopen(tcpserver) try io = accept(tcpserver) - if !isvalid(io; kw...) - close(io) - continue - end - io = ssl ? getsslcontext(io, sslconfig) : io - let io = Connection(host, string(port), pipeline_limit, io) - @info "Accept: $io" - @async try - handle_connection(f, io; kw...) - catch e - @error "Error: $io" e catch_stacktrace() - finally - close(io) - @info "Closed: $io" - end - end catch e - if typeof(e) <: InterruptException - @warn "Interrupted: listen($host,$port)" - close(tcpserver) + if e isa Base.UVError + @warn "$e" + break else rethrow(e) end end + if !tcpisvalid(io; kw...) + close(io) + continue + end + io = ssl ? getsslcontext(io, sslconfig) : io + let io = Connection(host, string(port), pipeline_limit, io) + @info "Accept: $io" + @async try + handle_connection(f, io; kw...) + catch e + @error "Error: $io" e + finally + close(io) + @info "Closed: $io" + end + end + end + catch e + if typeof(e) <: InterruptException + @warn "Interrupted: listen($host,$port)" + else + rethrow(e) end finally close(tcpserver) @@ -385,11 +397,6 @@ function handle_transaction(f::Function, t::Transaction; request = HTTP.Request() http = Streams.Stream(request, ConnectionPool.getparser(t), t) - response = request.response - response.status = 200 - if final_transaction - setheader(response, "Connection" => "close") - end try startread(http) @@ -412,13 +419,19 @@ function handle_transaction(f::Function, t::Transaction; @info http.message end + response = request.response + response.status = 200 + if final_transaction || hasheader(request, "Connection", "close") + setheader(response, "Connection" => "close") + end + @async try handle_stream(f, http) catch e if isioerror(e) @warn e else - @error e catch_stacktrace() + @error e end close(t) end @@ -440,7 +453,7 @@ function handle_stream(f::Function, http::Stream) f(http) catch e if isopen(http) && !iswritable(http) - @error e catch_stacktrace() + @error e http.message.response.status = 500 startwrite(http) write(http, sprint(showerror, e)) diff --git a/src/Streams.jl b/src/Streams.jl index 471a63d51..186f0d146 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -129,8 +129,10 @@ function IOExtras.closewrite(http::Stream{Request}) closebody(http) closewrite(http.stream) - if hasheader(http.message, "Connection", "close") - # Close conncetion if client sent "Connection: close"... + if hasheader(http.message, "Connection", "close") || + http.message.version < v"1.1" && + !hasheader(http.message, "Connection", "keep-alive") + @debug 1 "✋ \"Connection: close\": $(http.stream)" close(http.stream) end diff --git a/test/runtests.jl b/test/runtests.jl index 1dbea7572..8c2ca95d7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,7 +16,7 @@ using HTTP.Test include("client.jl"); include("handlers.jl") -# include("server.jl") + include("server.jl") include("async.jl"); end; diff --git a/test/server.jl b/test/server.jl index c451b3bd4..553ab033f 100644 --- a/test/server.jl +++ b/test/server.jl @@ -1,6 +1,18 @@ using HTTP using HTTP.Test +function testget(url) + mktempdir() do d + cd(d) do + cmd = `"curl -v -s $url > tmpout 2>&1"` + cmd = `bash -c $cmd` + println(cmd) + run(cmd) + return String(read(joinpath(d, "tmpout"))) + end + end +end + @testset "HTTP.Servers.serve" begin # test kill switch @@ -8,43 +20,65 @@ server = HTTP.Servers.Server() tsk = @async HTTP.Servers.serve(server) sleep(1.0) put!(server.in, HTTP.Servers.KILL) -sleep(0.1) +sleep(2) @test istaskdone(tsk) + # test http vs. https # echo response serverlog = HTTP.FIFOBuffer() -server = HTTP.Servers.Server((req, rep) -> HTTP.Response(String(req)), serverlog) +server = HTTP.Servers.Server((req, rep) -> begin + rep.body = req.body + return rep +end, serverlog) + +server.options.ratelimit=0 tsk = @async HTTP.Servers.serve(server) sleep(1.0) -r = HTTP.get("http://127.0.0.1:8081/"; readtimeout=30) -@test HTTP.status(r) == 200 -@test String(take!(r)) == "" -print(String(readavailable(serverlog))) +r = testget("http://127.0.0.1:8081/") +@test ismatch(r"HTTP/1.1 200 OK", r) + +rv = [] +n = 3 +@sync for i = 1:n + @async begin + r = testget(repeat("http://127.0.0.1:8081/$i ", n)) + #println(r) + push!(rv, r) + end + sleep(0.01) +end +for i = 1:n + @test length(filter(l->ismatch(r"HTTP/1.1 200 OK", l), + split(rv[i], "\n"))) == n +end + +r = HTTP.get("http://127.0.0.1:8081/"; readtimeout=30) +@test r.status == 200 +@test String(r.body) == "" + # invalid HTTP sleep(2.0) tcp = connect(ip"127.0.0.1", 8081) write(tcp, "GET / HTP/1.1\r\n\r\n") +!HTTP.Parsers.strict && +@test ismatch(r"HTTP/1.1 505 HTTP Version Not Supported", String(read(tcp))) sleep(2.0) -log = String(readavailable(serverlog)) -print(log) -!HTTP.strict && @test contains(log, "invalid HTTP version") -# bad method +# no URL sleep(2.0) tcp = connect(ip"127.0.0.1", 8081) -write(tcp, "BADMETHOD / HTTP/1.1\r\n\r\n") +write(tcp, "SOMEMETHOD HTTP/1.1\r\nContent-Length: 0\r\n\r\n") +r = String(read(tcp)) +!HTTP.Parsers.strict && @test ismatch(r"HTTP/1.1 400 Bad Request", r) +!HTTP.Parsers.strict && @test ismatch(r"invalid URL", r) sleep(2.0) -log = String(readavailable(serverlog)) - -print(log) -@test contains(log, "invalid HTTP method") # Expect: 100-continue sleep(2.0) @@ -54,24 +88,24 @@ sleep(2.0) log = String(readavailable(serverlog)) -@test contains(log, "sending 100 Continue response to get request body") +#@test contains(log, "sending 100 Continue response to get request body") client = String(readavailable(tcp)) @test client == "HTTP/1.1 100 Continue\r\n\r\n" + write(tcp, "Body of Request") sleep(2.0) -log = String(readavailable(serverlog)) +#log = String(readavailable(serverlog)) client = String(readavailable(tcp)) -println("log:") -println(log) -println() +#println("log:") +#println(log) +#println() println("client:") println(client) @test contains(client, "HTTP/1.1 200 OK\r\n") -@test contains(client, "Connection: keep-alive\r\n") -@test contains(client, "Content-Length: 15\r\n") -@test contains(client, "\r\n\r\nBody of Request") +@test contains(client, "Transfer-Encoding: chunked\r\n") +@test contains(client, "Body of Request") put!(server.in, HTTP.Servers.KILL) @@ -110,11 +144,12 @@ put!(server.in, HTTP.Servers.KILL) # handler throw error # keep-alive vs. close: issue #81 -tsk = @async HTTP.Servers.serve(HTTP.Server((req, res) -> Response("Hello\n"), STDOUT), ip"127.0.0.1", 8083) +tsk = @async HTTP.Servers.serve(HTTP.Servers.Server((req, res) -> (res.body = "Hello\n"; res), STDOUT), ip"127.0.0.1", 8083) sleep(2.0) -r = HTTP.request(HTTP.Request(major=1, minor=0, uri=HTTP.URI("http://127.0.0.1:8083/"), headers=["Host"=>"127.0.0.1:8083"])) -@test HTTP.status(r) == 200 -@test HTTP.headers(r)["Connection"] == "close" +r = HTTP.request("GET", "http://127.0.0.1:8083/", ["Host"=>"127.0.0.1:8083"]; http_version=v"1.0") +@info r +@test r.status == 200 +#@test HTTP.header(r, "Connection") == "close" # body too big From cb4a89438e06c543570b8a606635b72bd7c707ad Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 14:37:05 +1100 Subject: [PATCH 171/182] oops --- test/server.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/server.jl b/test/server.jl index 553ab033f..60b714373 100644 --- a/test/server.jl +++ b/test/server.jl @@ -147,7 +147,6 @@ put!(server.in, HTTP.Servers.KILL) tsk = @async HTTP.Servers.serve(HTTP.Servers.Server((req, res) -> (res.body = "Hello\n"; res), STDOUT), ip"127.0.0.1", 8083) sleep(2.0) r = HTTP.request("GET", "http://127.0.0.1:8083/", ["Host"=>"127.0.0.1:8083"]; http_version=v"1.0") -@info r @test r.status == 200 #@test HTTP.header(r, "Connection") == "close" From 1df2227e55c4c2fbd070c28caa8e89753e2cce6e Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 14:52:27 +1100 Subject: [PATCH 172/182] disable extra debug printf in test/server.jl --- test/server.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/server.jl b/test/server.jl index 60b714373..6b46494e1 100644 --- a/test/server.jl +++ b/test/server.jl @@ -6,7 +6,7 @@ function testget(url) cd(d) do cmd = `"curl -v -s $url > tmpout 2>&1"` cmd = `bash -c $cmd` - println(cmd) + #println(cmd) run(cmd) return String(read(joinpath(d, "tmpout"))) end From b2b5a1429f90b3e83fab74232b9cf2838626b59b Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 21:10:42 +1100 Subject: [PATCH 173/182] Return status 413 for too-large headers. https://github.com/JuliaWeb/HTTP.jl/pull/135#discussion_r161343043 --- src/Messages.jl | 18 ++++++++++++++++-- src/Servers.jl | 4 ++++ test/server.jl | 7 +++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Messages.jl b/src/Messages.jl index bce1c5892..ff88366d9 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -59,7 +59,7 @@ Streaming of request and response bodies is handled by the module Messages -export Message, Request, Response, +export Message, Request, Response, HeaderSizeError, reset!, iserror, isredirect, ischunked, issafe, isidempotent, header, hasheader, setheader, defaultheader, appendheader, @@ -385,6 +385,13 @@ function readstartline!(m::Parsers.Message, r::Request) end +""" +Arbitrary limit to protect against denial of service attacks. +""" +const header_size_limit = 0x10000 + +struct HeaderSizeError <: Exception end + """ readheaders(::IO, ::Parser, ::Message) @@ -394,11 +401,18 @@ Throw `EOFError` if input is incomplete. function readheaders(io::IO, parser::Parser, message::Message) + n = 0 while !headerscomplete(parser) && !eof(io) - excess = parseheaders(parser, readavailable(io)) do h + bytes = readavailable(io) + n += length(bytes) + excess = parseheaders(parser, bytes) do h appendheader(message, h) end unread!(io, excess) + n -= length(excess) + if n > header_size_limit + throw(HeaderSizeError()) + end end if !headerscomplete(parser) throw(EOFError()) diff --git a/src/Servers.jl b/src/Servers.jl index f9ed0f649..65fdc523a 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -410,6 +410,10 @@ function handle_transaction(f::Function, t::Transaction; write(t, Response(status, body = HTTP.Parsers.ERROR_MESSAGES[e.code])) close(t) return + elseif e isa HeaderSizeError + write(t, Response(413)) + close(t) + return else rethrow(e) end diff --git a/test/server.jl b/test/server.jl index 6b46494e1..18dd372e1 100644 --- a/test/server.jl +++ b/test/server.jl @@ -26,6 +26,7 @@ sleep(2) # test http vs. https + # echo response serverlog = HTTP.FIFOBuffer() server = HTTP.Servers.Server((req, rep) -> begin @@ -61,6 +62,12 @@ r = HTTP.get("http://127.0.0.1:8081/"; readtimeout=30) @test String(r.body) == "" +# large headers +sleep(2.0) +tcp = connect(ip"127.0.0.1", 8081) +write(tcp, "GET / HTTP/1.1\r\n$(repeat("Foo: Bar\r\n", 10000))\r\n") +@test ismatch(r"HTTP/1.1 413 Request Entity Too Large", String(read(tcp))) + # invalid HTTP sleep(2.0) tcp = connect(ip"127.0.0.1", 8081) From be69847c86428a28b3058ad9a9189db7b3ccc240 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Sun, 14 Jan 2018 21:25:01 +1100 Subject: [PATCH 174/182] randomise server port number in tests --- test/server.jl | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/test/server.jl b/test/server.jl index 18dd372e1..8fb013f5a 100644 --- a/test/server.jl +++ b/test/server.jl @@ -1,6 +1,8 @@ using HTTP using HTTP.Test +port = rand(8000:8999) + function testget(url) mktempdir() do d cd(d) do @@ -17,7 +19,7 @@ end # test kill switch server = HTTP.Servers.Server() -tsk = @async HTTP.Servers.serve(server) +tsk = @async HTTP.Servers.serve(server, "localhost", port) sleep(1.0) put!(server.in, HTTP.Servers.KILL) sleep(2) @@ -35,18 +37,18 @@ server = HTTP.Servers.Server((req, rep) -> begin end, serverlog) server.options.ratelimit=0 -tsk = @async HTTP.Servers.serve(server) +tsk = @async HTTP.Servers.serve(server, "localhost", port) sleep(1.0) -r = testget("http://127.0.0.1:8081/") +r = testget("http://127.0.0.1:$port/") @test ismatch(r"HTTP/1.1 200 OK", r) rv = [] n = 3 @sync for i = 1:n @async begin - r = testget(repeat("http://127.0.0.1:8081/$i ", n)) + r = testget(repeat("http://127.0.0.1:$port/$i ", n)) #println(r) push!(rv, r) end @@ -57,20 +59,20 @@ for i = 1:n split(rv[i], "\n"))) == n end -r = HTTP.get("http://127.0.0.1:8081/"; readtimeout=30) +r = HTTP.get("http://127.0.0.1:$port/"; readtimeout=30) @test r.status == 200 @test String(r.body) == "" # large headers sleep(2.0) -tcp = connect(ip"127.0.0.1", 8081) +tcp = connect(ip"127.0.0.1", port) write(tcp, "GET / HTTP/1.1\r\n$(repeat("Foo: Bar\r\n", 10000))\r\n") @test ismatch(r"HTTP/1.1 413 Request Entity Too Large", String(read(tcp))) # invalid HTTP sleep(2.0) -tcp = connect(ip"127.0.0.1", 8081) +tcp = connect(ip"127.0.0.1", port) write(tcp, "GET / HTP/1.1\r\n\r\n") !HTTP.Parsers.strict && @test ismatch(r"HTTP/1.1 505 HTTP Version Not Supported", String(read(tcp))) @@ -79,7 +81,7 @@ sleep(2.0) # no URL sleep(2.0) -tcp = connect(ip"127.0.0.1", 8081) +tcp = connect(ip"127.0.0.1", port) write(tcp, "SOMEMETHOD HTTP/1.1\r\nContent-Length: 0\r\n\r\n") r = String(read(tcp)) !HTTP.Parsers.strict && @test ismatch(r"HTTP/1.1 400 Bad Request", r) @@ -89,7 +91,7 @@ sleep(2.0) # Expect: 100-continue sleep(2.0) -tcp = connect(ip"127.0.0.1", 8081) +tcp = connect(ip"127.0.0.1", port) write(tcp, "POST / HTTP/1.1\r\nContent-Length: 15\r\nExpect: 100-continue\r\n\r\n") sleep(2.0) @@ -151,9 +153,10 @@ put!(server.in, HTTP.Servers.KILL) # handler throw error # keep-alive vs. close: issue #81 -tsk = @async HTTP.Servers.serve(HTTP.Servers.Server((req, res) -> (res.body = "Hello\n"; res), STDOUT), ip"127.0.0.1", 8083) +port += 1 +tsk = @async HTTP.Servers.serve(HTTP.Servers.Server((req, res) -> (res.body = "Hello\n"; res), STDOUT), ip"127.0.0.1", port) sleep(2.0) -r = HTTP.request("GET", "http://127.0.0.1:8083/", ["Host"=>"127.0.0.1:8083"]; http_version=v"1.0") +r = HTTP.request("GET", "http://127.0.0.1:$port/", ["Host"=>"127.0.0.1:$port"]; http_version=v"1.0") @test r.status == 200 #@test HTTP.header(r, "Connection") == "close" From 6d978be5031bdaa1c973e0151a4fce7182d26de2 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 15 Jan 2018 09:11:19 +1100 Subject: [PATCH 175/182] iso8859_1_to_utf8 per #141 --- src/Messages.jl | 13 ++++++++++++- src/Strings.jl | 1 - 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Messages.jl b/src/Messages.jl index ff88366d9..3cee668be 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -65,7 +65,8 @@ export Message, Request, Response, HeaderSizeError, header, hasheader, setheader, defaultheader, appendheader, mkheaders, readheaders, headerscomplete, readtrailers, writeheaders, readstartline!, writestartline, - bodylength, unknown_length + bodylength, unknown_length, + load import ..HTTP @@ -365,6 +366,16 @@ function Base.String(m::Message) end +#Like https://github.com/JuliaIO/FileIO.jl/blob/v0.6.1/src/FileIO.jl#L19 ? +function load(m::Message) + if hasheader(m, "Content-Type", "ISO-8859-1") + return iso8859_1_to_utf8(m.body) + else + String(m.body) + end +end + + """ readstartline!(::Parsers.Message, ::Message) diff --git a/src/Strings.jl b/src/Strings.jl index 3dcf1cd6f..53cb08163 100644 --- a/src/Strings.jl +++ b/src/Strings.jl @@ -52,7 +52,6 @@ end Convert from ISO8859_1 to UTF8. """ -iso8859_1_to_utf8(str::String) = iso8859_1_to_utf8(Vector{UInt8}(str)) function iso8859_1_to_utf8(bytes::Vector{UInt8}) io = IOBuffer() for b in bytes From aff5cac7b89e23a500de45d473f083fc08e41eaa Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 15 Jan 2018 12:35:07 +1100 Subject: [PATCH 176/182] typo --- src/Parsers.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parsers.jl b/src/Parsers.jl index 95895a09a..eb5db17ac 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -761,7 +761,7 @@ function parsebody(parser::Parser, bytes::ByteView)::Tuple{ByteView,ByteView} elseif p_state == s_chunk_data to_read = Int(min(parser.chunk_length, len - p + 1)) - @passert parser.chunk_length != 0 && + @passert parser.chunk_length != 0 @passert result == nobytes result = view(bytes, p:p + to_read - 1) From 61e197d677f1d30ffb2128bb900813b92d92687c Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 16 Jan 2018 03:45:04 +1100 Subject: [PATCH 177/182] Naming consistancy tweaks. URLs are the subset of URIs that specify a means of acessing a resource in addition to specifying the resource name. e.g http://foo.com/bar?a=b is a URL but bar?a=b is not. Changes: - Variables of type URI that must contain a compelte URL renamed uri -> url. - Remove the "URL" constructor for URIs. It seems to have been constructing URI's that were not URLs for some input, which seems wrong. The other "URI" constructors should suffice. - Simplify the main kwargs URI constructor by putting the sanity checks in preconditions. Changes: - Renames HTTP.Request.uri -> HTTP.Request.target - Added "target=" kw arg to request(MessageLayer, ...) to allow setting the target to somthing that cannot be derived from the request URL. - Remove the old behaviour (I assume untested) of handling "CONNECT" as a special case and using the Request URL host:port as the target. I seems that this would have created a request for the server to proxy to itself. - Remove the now redundant option isconnect from the url parser. A client making a CONNECT Request should use target="$host:$port". A server implemetning a CONNECT proxy should use http_parse_host(target) --- src/AWS4AuthRequest.jl | 18 ++-- src/BasicAuthRequest.jl | 6 +- src/CanonicalizeRequest.jl | 4 +- src/ConnectionRequest.jl | 8 +- src/CookieRequest.jl | 18 ++-- src/HTTP.jl | 28 +++--- src/Handlers.jl | 2 +- src/MessageRequest.jl | 11 +-- src/Messages.jl | 33 +++++-- src/Parsers.jl | 14 ++- src/RedirectRequest.jl | 8 +- src/RetryRequest.jl | 4 +- src/URIs.jl | 193 ++++++++++++++++--------------------- src/client.jl | 150 +++------------------------- src/urlparser.jl | 31 +++--- 15 files changed, 199 insertions(+), 329 deletions(-) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index de80ac7a5..936de153d 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -24,7 +24,7 @@ abstract type AWS4AuthLayer{Next <: Layer} <: Layer end export AWS4AuthLayer function request(::Type{AWS4AuthLayer{Next}}, - uri::URI, req, body; kw...) where Next + url::URI, req, body; kw...) where Next @static if VERSION > v"0.7.0-DEV.2915" if !haskey(kw, :aws_access_key_id) && @@ -33,21 +33,21 @@ function request(::Type{AWS4AuthLayer{Next}}, end end - sign_aws4!(req.method, uri, req.headers, req.body; kw...) + sign_aws4!(req.method, url, req.headers, req.body; kw...) - return request(Next, uri, req, body; kw...) + return request(Next, url, req, body; kw...) end function sign_aws4!(method::String, - uri::URI, + url::URI, headers::Headers, body::Vector{UInt8}; body_sha256::Vector{UInt8}=digest(MD_SHA256, body), body_md5::Vector{UInt8}=digest(MD_MD5, body), t::DateTime=now(Dates.UTC), - aws_service::String=String(split(uri.host, ".")[1]), - aws_region::String=String(split(uri.host, ".")[2]), + aws_service::String=String(split(url.host, ".")[1]), + aws_region::String=String(split(url.host, ".")[2]), aws_access_key_id::String=ENV["AWS_ACCESS_KEY_ID"], aws_secret_access_key::String=ENV["AWS_SECRET_ACCESS_KEY"], aws_session_token::String=get(ENV, "AWS_SESSION_TOKEN", ""), @@ -87,13 +87,13 @@ function sign_aws4!(method::String, signed_headers = join(sort([lowercase(k) for (k,v) in headers]), ";") # Sort Query String... - query = queryparams(uri.query) + query = queryparams(url.query) query = Pair[k => query[k] for k in sort(collect(keys(query)))] # Create hash of canonical request... canonical_form = string(method, "\n", - aws_service == "s3" ? uri.path - : escapepath(uri.path), "\n", + aws_service == "s3" ? url.path + : escapepath(url.path), "\n", escapeuri(query), "\n", join(sort(canonical_headers), "\n"), "\n\n", signed_headers, "\n", diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index e8a6e1e15..cb64d5be6 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -18,16 +18,16 @@ abstract type BasicAuthLayer{Next <: Layer} <: Layer end export BasicAuthLayer function request(::Type{BasicAuthLayer{Next}}, - method::String, uri::URI, headers, body; kw...) where Next + method::String, url::URI, headers, body; kw...) where Next - userinfo = uri.userinfo + userinfo = url.userinfo if !isempty(userinfo) && getkv(headers, "Authorization", "") == "" @debug 1 "Adding Authorization: Basic header." setkv(headers, "Authorization", "Basic $(base64encode(userinfo))") end - return request(Next, method, uri, headers, body; kw...) + return request(Next, method, url, headers, body; kw...) end diff --git a/src/CanonicalizeRequest.jl b/src/CanonicalizeRequest.jl index b49adf73d..ac435dfd2 100644 --- a/src/CanonicalizeRequest.jl +++ b/src/CanonicalizeRequest.jl @@ -15,11 +15,11 @@ abstract type CanonicalizeLayer{Next <: Layer} <: Layer end export CanonicalizeLayer function request(::Type{CanonicalizeLayer{Next}}, - method::String, uri, headers, body; kw...) where Next + method::String, url, headers, body; kw...) where Next headers = canonicalizeheaders(headers) - res = request(Next, method, uri, headers, body; kw...) + res = request(Next, method, url, headers, body; kw...) res.headers = canonicalizeheaders(res.headers) diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index 7034561c3..e75bad3cc 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -24,11 +24,11 @@ See [`isioerror`](@ref). abstract type ConnectionPoolLayer{Next <: Layer} <: Layer end export ConnectionPoolLayer -function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; +function request(::Type{ConnectionPoolLayer{Next}}, url::URI, req, body; socket_type::Type=TCPSocket, kw...) where Next - IOType = ConnectionPool.Transaction{sockettype(uri, socket_type)} - io = getconnection(IOType, uri.host, uri.port; kw...) + IOType = ConnectionPool.Transaction{sockettype(url, socket_type)} + io = getconnection(IOType, url.host, url.port; kw...) try return request(Next, io, req, body; kw...) @@ -40,7 +40,7 @@ function request(::Type{ConnectionPoolLayer{Next}}, uri::URI, req, body; end -sockettype(uri::URI, default) = uri.scheme in ("wss", "https") ? SSLContext : +sockettype(url::URI, default) = url.scheme in ("wss", "https") ? SSLContext : default diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index b157fe52f..e127f5087 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -21,40 +21,40 @@ abstract type CookieLayer{Next <: Layer} <: Layer end export CookieLayer function request(::Type{CookieLayer{Next}}, - method::String, uri::URI, headers, body; + method::String, url::URI, headers, body; cookiejar::Dict{String, Set{Cookie}}=default_cookiejar, kw...) where Next - hostcookies = get!(cookiejar, uri.host, Set{Cookie}()) + hostcookies = get!(cookiejar, url.host, Set{Cookie}()) - cookies = getcookies(hostcookies, uri) + cookies = getcookies(hostcookies, url) if !isempty(cookies) setkv(headers, "Cookie", string(getkv(headers, "Cookie", ""), cookies)) end - res = request(Next, method, uri, headers, body; kw...) + res = request(Next, method, url, headers, body; kw...) - setcookies(hostcookies, uri.host, res.headers) + setcookies(hostcookies, url.host, res.headers) return res end -function getcookies(cookies, uri) +function getcookies(cookies, url) tosend = Vector{Cookie}() expired = Vector{Cookie}() # Check if cookies should be added to outgoing request based on host... for cookie in cookies - if Cookies.shouldsend(cookie, uri.scheme == "https", - uri.host, uri.path) + if Cookies.shouldsend(cookie, url.scheme == "https", + url.host, url.path) t = cookie.expires if t != Dates.DateTime() && t < Dates.now(Dates.UTC) @debug 1 "Deleting expired Cookie: $cookie.name" push!(expired, cookie) else - @debug 1 "Sending Cookie: $cookie.name to $uri.host" + @debug 1 "Sending Cookie: $cookie.name to $url.host" push!(tosend, cookie) end end diff --git a/src/HTTP.jl b/src/HTTP.jl index 267472de7..c3bb0c011 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -138,8 +138,8 @@ Basic Authenticaiton options AWS Authenticaiton options - `awsauthorization = false`, enable AWS4 Authentication. - - `aws_service = split(uri.host, ".")[1]` - - `aws_region = split(uri.host, ".")[2]` + - `aws_service = split(url.host, ".")[1]` + - `aws_region = split(url.host, ".")[2]` - `aws_access_key_id = ENV["AWS_ACCESS_KEY_ID"]` - `aws_secret_access_key = ENV["AWS_SECRET_ACCESS_KEY"]` - `aws_session_token = get(ENV, "AWS_SESSION_TOKEN", "")` @@ -281,13 +281,13 @@ end ``` """ -request(method::String, uri::URI, headers::Headers, body; kw...)::Response = - request(HTTP.stack(;kw...), method, uri, headers, body; kw...) +request(method::String, url::URI, headers::Headers, body; kw...)::Response = + request(HTTP.stack(;kw...), method, url, headers, body; kw...) const nobody = UInt8[] -request(method, uri, headers=Header[], body=nobody; kw...)::Response = - request(string(method), URI(uri), mkheaders(headers), body; kw...) +request(method, url, headers=Header[], body=nobody; kw...)::Response = + request(string(method), URI(url), mkheaders(headers), body; kw...) """ @@ -313,8 +313,8 @@ end ``` """ -open(f::Function, method::String, uri, headers=Header[]; kw...)::Response = - request(method, uri, headers, nothing; iofunction=f, kw...) +open(f::Function, method::String, url, headers=Header[]; kw...)::Response = + request(method, url, headers, nothing; iofunction=f, kw...) """ @@ -324,7 +324,7 @@ open(f::Function, method::String, uri, headers=Header[]; kw...)::Response = Shorthand for `HTTP.request("GET", ...)`. See [`HTTP.request`](@ref). """ -get(a...; kw...) = request("GET", a..., kw...) +get(u, a...; kw...) = request("GET", u, a...; kw...) """ @@ -333,7 +333,7 @@ get(a...; kw...) = request("GET", a..., kw...) Shorthand for `HTTP.request("PUT", ...)`. See [`HTTP.request`](@ref). """ -put(a...; kw...) = request("PUT", a..., kw...) +put(u, h, b; kw...) = request("PUT", u, h, b; kw...) """ @@ -342,7 +342,7 @@ put(a...; kw...) = request("PUT", a..., kw...) Shorthand for `HTTP.request("POST", ...)`. See [`HTTP.request`](@ref). """ -post(a...; kw...) = request("POST", a..., kw...) +post(u, h, b; kw...) = request("POST", u, h, b; kw...) """ @@ -351,7 +351,7 @@ post(a...; kw...) = request("POST", a..., kw...) Shorthand for `HTTP.request("HEAD", ...)`. See [`HTTP.request`](@ref). """ -head(a...; kw...) = request("HEAD", a..., kw...) +head(u; kw...) = request("HEAD", u; kw...) @@ -455,7 +455,7 @@ relationship with [`HTTP.Response`](@ref), [`HTTP.Parser`](@ref), │ │ HTTP.StatusError │─ ─ │ │ │ │ └───────────────────┘ │ │ │ ┌───────────────────┐ │ │ │ - │ request(method, uri, headers, body) -> │ HTTP.Response │ │ │ + │ request(method, url, headers, body) -> │ HTTP.Response │ │ │ │ ────────────────────────── └─────────▲─────────┘ │ │ │ │ ║ ║ │ │ │ ┌────────────────────────────────────────────────────────────┐ │ │ │ @@ -487,7 +487,7 @@ relationship with [`HTTP.Response`](@ref), [`HTTP.Parser`](@ref), ││ HTTP.Request │ │ │ HTTP.Response │ │ ││ │ │ │ │ ││ method::String ◀───┼──▶ status::Int │ │ -││ uri::String │ │ │ headers::Vector{Pair} │ +││ target::String │ │ │ headers::Vector{Pair} │ ││ headers::Vector{Pair} │ │ │ body::Vector{UInt8} │ │ ││ body::Vector{UInt8} │ │ │ │ │└──────────────────▲───────────────┘ │ └───────────────▲────────────────┼─┘ diff --git a/src/Handlers.jl b/src/Handlers.jl index 64902cbf3..79ae08a21 100644 --- a/src/Handlers.jl +++ b/src/Handlers.jl @@ -127,7 +127,7 @@ function handle(r::Router, req, resp) # get the url/path of the request m = Val(Symbol(req.method)) # get scheme, host, split path into strings and get Vals - uri = HTTP.URI(req.uri) + uri = HTTP.URI(req.target) s = get(SCHEMES, uri.scheme, EMPTYVAL) h = Val(Symbol(uri.host)) p = uri.path diff --git a/src/MessageRequest.jl b/src/MessageRequest.jl index 12481878d..96dd945d5 100644 --- a/src/MessageRequest.jl +++ b/src/MessageRequest.jl @@ -20,13 +20,12 @@ struct MessageLayer{Next <: Layer} <: Layer end export MessageLayer function request(::Type{MessageLayer{Next}}, - method::String, uri::URI, headers::Headers, body; + method::String, url::URI, headers::Headers, body; http_version=v"1.1", + target=resource(url), parent=nothing, iofunction=nothing, kw...) where Next - path = method == "CONNECT" ? hostport(uri) : resource(uri) - - defaultheader(headers, "Host" => uri.host) + defaultheader(headers, "Host" => url.host) if !hasheader(headers, "Content-Length") && !hasheader(headers, "Transfer-Encoding") && @@ -39,10 +38,10 @@ function request(::Type{MessageLayer{Next}}, end end - req = Request(method, path, headers, bodybytes(body); + req = Request(method, target, headers, bodybytes(body); parent=parent, version=http_version) - return request(Next, uri, req, body; iofunction=iofunction, kw...) + return request(Next, url, req, body; iofunction=iofunction, kw...) end diff --git a/src/Messages.jl b/src/Messages.jl index 3cee668be..012836a6d 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -86,9 +86,18 @@ abstract type Message end Represents a HTTP Response Message. - `version::VersionNumber` + [RFC7230 2.6](https://tools.ietf.org/html/rfc7230#section-2.6) + - `status::Int16` + [RFC7230 3.1.2](https://tools.ietf.org/html/rfc7230#section-3.1.2) + [RFC7231 6](https://tools.ietf.org/html/rfc7231#section-6) + - `headers::Vector{Pair{String,String}}` + [RFC7230 3.2](https://tools.ietf.org/html/rfc7230#section-3.2) + - `body::Vector{UInt8}` + [RFC7230 3.3](https://tools.ietf.org/html/rfc7230#section-3.3) + - `request`, the `Request` that yielded this `Response`. """ @@ -132,18 +141,30 @@ end Represents a HTTP Request Message. - `method::String` -- `uri::String` + [RFC7230 3.1.1](https://tools.ietf.org/html/rfc7230#section-3.1.1) + +- `target::String` + [RFC7230 5.3](https://tools.ietf.org/html/rfc7230#section-5.3) + - `version::VersionNumber` + [RFC7230 2.6](https://tools.ietf.org/html/rfc7230#section-2.6) + - `headers::Vector{Pair{String,String}}` + [RFC7230 3.2](https://tools.ietf.org/html/rfc7230#section-3.2) + - `body::Vector{UInt8}` + [RFC7230 3.3](https://tools.ietf.org/html/rfc7230#section-3.3) + - `response`, the `Response` to this `Request` + - `parent`, the `Response` (if any) that led to this request (e.g. in the case of a redirect). + [RFC7230 6.4](https://tools.ietf.org/html/rfc7231#section-6.4) """ mutable struct Request <: Message method::String - uri::String + target::String version::VersionNumber headers::Headers body::Vector{UInt8} @@ -153,10 +174,10 @@ end Request() = Request("", "") -function Request(method::String, uri, headers=[], body=UInt8[]; +function Request(method::String, target, headers=[], body=UInt8[]; version=v"1.1", parent=nothing) r = Request(method, - uri == "" ? "/" : uri, + target == "" ? "/" : target, version, mkheaders(headers), body, @@ -319,7 +340,7 @@ e.g. `"GET /path HTTP/1.1\\r\\n"` or `"HTTP/1.1 200 OK\\r\\n"` """ function writestartline(io::IO, r::Request) - write(io, "$(r.method) $(r.uri) $(httpversion(r))\r\n") + write(io, "$(r.method) $(r.target) $(httpversion(r))\r\n") return end @@ -391,7 +412,7 @@ end function readstartline!(m::Parsers.Message, r::Request) r.version = VersionNumber(m.major, m.minor) r.method = m.method - r.uri = m.url + r.target = m.target return end diff --git a/src/Parsers.jl b/src/Parsers.jl index eb5db17ac..80f30ad96 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -53,16 +53,20 @@ const Headers = Vector{Header} """ - `method::String`: the HTTP method + [RFC7230 3.1.1](https://tools.ietf.org/html/rfc7230#section-3.1.1) - `major` and `minor`: HTTP version - - `url::String`: request URL + [RFC7230 2.6](https://tools.ietf.org/html/rfc7230#section-2.6) + - `target::String`: request target + [RFC7230 5.3](https://tools.ietf.org/html/rfc7230#section-5.3) - `status::Int`: response status + [RFC7230 3.1.2](https://tools.ietf.org/html/rfc7230#section-3.1.2) """ mutable struct Message method::String major::Int16 minor::Int16 - url::String + target::String status::Int32 Message() = reset!(new()) @@ -72,7 +76,7 @@ function reset!(m::Message) m.method = "" m.major = 0 m.minor = 0 - m.url = "" + m.target = "" m.status = 0 return m end @@ -452,8 +456,8 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, write(parser.valuebuffer, view(bytes, start:p-1)) if p_state >= s_req_http_start - parser.message.url = take!(parser.valuebuffer) - @debugshow 4 parser.message.url + parser.message.target = take!(parser.valuebuffer) + @debugshow 4 parser.message.target end p = min(p, len) diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index f418b5731..4ceff56c3 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -18,12 +18,12 @@ abstract type RedirectLayer{Next <: Layer} <: Layer end export RedirectLayer function request(::Type{RedirectLayer{Next}}, - method::String, uri::URI, headers, body; + method::String, url::URI, headers, body; redirect_limit=3, forwardheaders=false, kw...) where Next count = 0 while true - res = request(Next, method, uri, headers, body; kw...) + res = request(Next, method, url, headers, body; kw...) if (count == redirect_limit || !isredirect(res) @@ -38,14 +38,14 @@ function request(::Type{RedirectLayer{Next}}, else setkv(kw, :parent, res) end - uri = absuri(location, uri) + url = absuri(location, url) if forwardheaders headers = filter(h->!(h[1] in ("Host", "Cookie")), headers) else headers = Header[] end - @debug 1 "➡️ Redirect: $uri" + @debug 1 "➡️ Redirect: $url" count += 1 end diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 54407f625..59d3af365 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -25,7 +25,7 @@ e.g. `HTTP.IOError`, `Base.DNSError`, `Base.EOFError` and `HTTP.StatusError` abstract type RetryLayer{Next <: Layer} <: Layer end export RetryLayer -function request(::Type{RetryLayer{Next}}, uri, req, body; +function request(::Type{RetryLayer{Next}}, url, req, body; retries::Int=4, retry_non_idempotent::Bool=false, kw...) where Next @@ -42,7 +42,7 @@ function request(::Type{RetryLayer{Next}}, uri, req, body; return s, retry end) - retry_request(Next, uri, req, body; kw...) + retry_request(Next, url, req, body; kw...) end diff --git a/src/URIs.jl b/src/URIs.jl index 6c3a290d4..de19f7eee 100644 --- a/src/URIs.jl +++ b/src/URIs.jl @@ -2,16 +2,18 @@ module URIs import Base.== +import ..@require, ..precondition_error + include("urlparser.jl") -export URI, URL, hostport, resource, queryparams, absuri, +export URI, + resource, queryparams, absuri, escapeuri, unescapeuri, escapepath + """ - HTTP.URL(host; userinfo="", path="", query="", fragment="", isconnect=false) - HTTP.URI(; scheme="", host="", port="", ...) - HTTP.URI(str; isconnect=false) - parse(HTTP.URI, str::String; isconnect=false) + HTTP.URI(; scheme="", host="", port="", etc...) + HTTP.URI(str) = parse(HTTP.URI, str::String) A type representing a valid uri. Can be constructed from distinct parts using the various supported keyword arguments. With a raw, already-encoded uri string, use `parse(HTTP.URI, str)` @@ -19,130 +21,105 @@ to parse the `HTTP.URI` directly. The `HTTP.URI` constructors will automatically `query` arguments, typically provided as `"key"=>"value"::Pair` or `Dict("key"=>"value")`. Note that multiple values for a single query key can provided like `Dict("key"=>["value1", "value2"])`. -For efficiency, the internal representation is stored as a set of offsets and lengths to the various uri components. -To access and return these components as strings, use the various accessor methods: - * `HTTP.scheme`: returns the scheme (if any) associated with the uri - * `HTTP.userinfo`: returns the userinfo (if any) associated with the uri - * `HTTP.host`: returns the host only of the uri - * `HTTP.port`: returns the port of the uri; will return "80" or "443" by default if the scheme is "http" or "https", respectively - * `HTTP.hostport`: returns the "host:port" combination; if the port is not provided or is the default port for the uri scheme, it will be omitted - * `HTTP.path`: returns the path for a uri - * `HTTP.query`: returns the query for a uri - * `HTTP.fragment`: returns the fragment for a uri - * `HTTP.resource`: returns the path-query-fragment combination +The `URI` struct stores the compelte URI in the `uri::String` field and the +component parts in the following `SubString` fields: + * `scheme`, e.g. `"http"` or `"https"` + * `userinfo`, e.g. `"username:password"` + * `host` e.g. `"julialang.org"` + * `port` e.g. `"80"` or `""` + * `path` e.g `"/"` + * `query` e.g. `"Foo=1&Bar=2"` + * `fragment` + +The `HTTP.resource(::URI)` function returns a target-resource string for the URI +[RFC7230 5.3](https://tools.ietf.org/html/rfc7230#section-5.3). +e.g. `"\$path?\$query#\$fragment"`. + +The `HTTP.queryparams(::URI)` function returns a `Dict` containing the `query`. """ + struct URI uri::String scheme::SubString + userinfo::SubString host::SubString port::SubString path::SubString query::SubString fragment::SubString - userinfo::SubString end + URI(uri::URI) = uri -function URI(;host::AbstractString="", path::AbstractString="", - scheme::AbstractString="", userinfo::AbstractString="", - port::Union{Integer,AbstractString}="", query="", - fragment::AbstractString="", isconnect::Bool=false) - host != "" && scheme == "" && !isconnect && (scheme = "http") + +const emptyuri = (()->begin + uri = "" + empty = SubString(uri) + return URI(uri, empty, empty, empty, empty, empty, empty, empty) +end)() + +URI(;kw...) = merge(emptyuri; kw...) + +function Base.merge(uri::URI; scheme::AbstractString=uri.scheme, + userinfo::AbstractString=uri.userinfo, + host::AbstractString=uri.host, + port::Union{Integer,AbstractString}=uri.port, + path::AbstractString=uri.path, + query=uri.query, + fragment::AbstractString=uri.fragment) + + @require isempty(scheme) || path != "*" + @require isempty(host) || host[end] != '/' + @require scheme in uses_authority || isempty(host) + @require !isempty(host) || isempty(port) + @require !(scheme in ["http", "https"]) || isempty(path) || path[1] == '/' + @require !isempty(path) || !isempty(query) || isempty(fragment) + io = IOBuffer() - printuri(io, scheme, userinfo, host, string(port), - path, escapeuri(query), fragment) - uri = String(take!(io)) - return URI(uri, isconnect=isconnect) -end -# we assume `str` is at least host & port -# if all others keywords are empty, assume CONNECT -# can include path, userinfo, query, & fragment -function URL(str::AbstractString; userinfo::AbstractString="", path::AbstractString="", - query="", fragment::AbstractString="", - isconnect::Bool=false) - if str != "" - if startswith(str, "http") || startswith(str, "https") - str = string(str, path, ifelse(query == "", "", "?" * escapeuri(query)), - ifelse(fragment == "", "", "#$fragment")) - else - if startswith(str, "/") || str == "*" - # relative uri like "/" or "*", leave it alone - elseif path == "" && userinfo == "" && query == "" && fragment == "" && ':' in str - isconnect = true - else - str = string("http://", userinfo == "" ? "" : "$userinfo@", - str, path, ifelse(query == "", "", "?" * escapeuri(query)), - ifelse(fragment == "", "", "#$fragment")) - end - end - end - return Base.parse(URI, str; isconnect=isconnect) + isempty(scheme) || print(io, scheme, scheme in uses_authority ? + "://" : ":") + isempty(userinfo) || print(io, userinfo, "@") + isempty(host) || print(io, hoststring(host)) + isempty(port) || print(io, ":", port) + isempty(path) || print(io, path) + isempty(query) || print(io, "?", escapeuri(query)) + isempty(fragment) || print(io, "#", fragment) + + return URI(String(take!(io))) end -URI(str::AbstractString; isconnect::Bool=false) = - Base.parse(URI, str; isconnect=isconnect) -Base.parse(::Type{URI}, str::AbstractString; isconnect::Bool=false) = - http_parser_parse_url(str, isconnect) +URI(str::AbstractString) = Base.parse(URI, str) -==(a::URI,b::URI) = a.scheme == b.scheme && - hostport(a) == hostport(b) && - a.path == b.path && - a.query == b.query && - a.fragment == b.fragment && - a.userinfo == b.userinfo +Base.parse(::Type{URI}, str::AbstractString) = http_parser_parse_url(str) -@inline function resource(uri::URI) - string(uri.path, - isempty(uri.query) ? "" : "?$(uri.query)", - isempty(uri.fragment) ? "" : "#$(uri.fragment)") -end -function hostport(uri::URI) - s = uri.scheme - h = uri.host - p = uri.port - if s == "http" && p == "80" || - s == "https" && p == "443" - p = "" - end - return string(':' in h ? "[$h]" : h, isempty(p) ? "" : ":$p") -end +==(a::URI,b::URI) = a.scheme == b.scheme && + a.host == b.host && + normalport(a) == normalport(b) && + a.path == b.path && + a.query == b.query && + a.fragment == b.fragment && + a.userinfo == b.userinfo + +# "request-target" per https://tools.ietf.org/html/rfc7230#section-5.3 +resource(uri::URI) = string( isempty(uri.path) ? "/" : uri.path, + !isempty(uri.query) ? "?" : "", uri.query, + !isempty(uri.fragment) ? "#" : "", uri.fragment) + +normalport(uri::URI) = uri.scheme == "http" && uri.port == "80" || + uri.scheme == "https" && uri.port == "443" ? + "" : uri.port + +hoststring(h) = ':' in h ? "[$h]" : h Base.show(io::IO, uri::URI) = print(io, "HTTP.URI(\"", uri, "\")") Base.print(io::IO, u::URI) = print(io, u.uri) -function printuri(io::IO, - sch::AbstractString, - userinfo::AbstractString, - host::AbstractString, - port::AbstractString, - path::AbstractString, - query::AbstractString, - fragment::AbstractString) - - if sch in uses_authority - print(io, sch, "://") - !isempty(userinfo) && print(io, userinfo, "@") - print(io, ':' in host ? "[$host]" : host) - print(io, ((sch == "http" && port == "80") || - (sch == "https" && port == "443") || isempty(port)) ? "" : ":$port") - elseif path != "" && path != "*" && sch != "" - print(io, sch, ":") - elseif host != "" && port != "" # CONNECT - print(io, host, ":", port) - end - if (isempty(host) || host[end] != '/') && - (isempty(path) || path[1] != '/') && - (!isempty(fragment) || !isempty(path)) - path = (!isempty(sch) && sch == "http" || sch == "https") ? string("/", path) : path - end - print(io, path, isempty(query) ? "" : "?$query", isempty(fragment) ? "" : "#$fragment") -end - +Base.string(u::URI) = u.uri queryparams(uri::URI) = queryparams(uri.query) @@ -152,9 +129,9 @@ function queryparams(q::AbstractString) for e in split(q, "&", keep=false))) end + # Validate known URI formats -const uses_authority = ["hdfs", "ftp", "http", "gopher", "nntp", "telnet", "imap", "wais", "file", "mms", "https", "shttp", "snews", "prospero", "rtsp", "rtspu", "rsync", "svn", "svn+ssh", "sftp" ,"nfs", "git", "git+ssh", "ldap", "s3", "ws"] -const uses_params = ["ftp", "hdl", "prospero", "http", "imap", "https", "shttp", "rtsp", "rtspu", "sip", "sips", "mms", "sftp", "tel"] +const uses_authority = ["https", "http", "hdfs", "ftp", "gopher", "nntp", "telnet", "imap", "wais", "file", "mms", "shttp", "snews", "prospero", "rtsp", "rtspu", "rsync", "svn", "svn+ssh", "sftp" ,"nfs", "git", "git+ssh", "ldap", "s3", "ws"] const non_hierarchical = ["gopher", "hdl", "mailto", "news", "telnet", "wais", "imap", "snews", "sip", "sips"] const uses_query = ["http", "wais", "imap", "https", "shttp", "mms", "gopher", "rtsp", "rtspu", "sip", "sips", "ldap"] const uses_fragment = ["hdfs", "ftp", "hdl", "http", "gopher", "news", "nntp", "wais", "https", "shttp", "snews", "file", "prospero"] @@ -256,11 +233,7 @@ function absuri(uri::URI, context::URI) @assert !isempty(context.host) @assert isempty(uri.port) - return URI(scheme = context.scheme, - host = context.host, - port = context.port, - path = uri.path, - query = uri.query) + return merge(context; path=uri.path, query=uri.query) end end # module diff --git a/src/client.jl b/src/client.jl index 65e526360..fb25b213b 100644 --- a/src/client.jl +++ b/src/client.jl @@ -36,7 +36,7 @@ end global const DEFAULT_CLIENT = Client() # build Request -function request(client::Client, method, uri::URI; +function request(client::Client, method, url::URI; headers::Dict=Dict(), body="", enablechunked::Bool=true, @@ -119,6 +119,12 @@ function request(client::Client, method, uri::URI; setkv(newargs, :bodylength, length(body)) end + if body != "" + Base.depwarn( + "The body= option is deprecated. Use request(method, uri,headers, body)", + :body) + end + if !enablechunked && isa(body, IO) body = read(body) end @@ -131,150 +137,22 @@ function request(client::Client, method, uri::URI; end end - return request(m, uri, h, body; args...) + return request(m, url, h, body; args...) end -#request(uri::AbstractString; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, GET, URIs.URL(uri; query=query); verbose=verbose, args...) -#request(uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, GET, uri; verbose=verbose, args...) -#request(method, uri::String; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, convert(HTTP.Method, method), URIs.URL(uri; query=query); verbose=verbose, args...) -#request(method, uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, convert(HTTP.Method, method), uri; verbose=verbose, args...) for f in [:get, :post, :put, :delete, :head, :trace, :options, :patch, :connect] meth = f_str = uppercase(string(f)) @eval begin -#= - @doc """ - $($f)(uri; kwargs...) -> Response - $($f)(client::HTTP.Client, uri; kwargs...) -> Response - -Build and execute an http "$($f_str)" request. Query parameters can be passed via the `query` keyword argument as a `Dict`. Multiple -query parameters with the same key can be passed like `Dict("key1"=>["value1", "value2"], "key2"=>...)`. -Returns a `Response` object that includes the resulting status code (`HTTP.status(r)` and `HTTP.statustext(r)`), -response headers (`HTTP.headers(r)`), cookies (`HTTP.cookies(r)`), response history if redirects were involved -(`HTTP.history(r)`), and response body (`HTTP.body(r)` or `String(r)` or `take!(r)`). - -The body or payload for a request can be given through the `body` keyword arugment. -The body can be given as a `String`, `Vector{UInt8}`, `IO`, `HTTP.FIFOBuffer` or `Dict` argument type. -See examples below for how to use an `HTTP.FIFOBuffer` for asynchronous streaming uploads. - -If the body is provided as a `Dict`, the request body will be uploaded using the multipart/form-data encoding. -The key-value pairs in the Dict will constitute the name and value of each multipart boundary chunk. -Files and other large data arguments can be provided as values as IO arguments: either an `IOStream` such as returned via `open(file)`, -an `IOBuffer` for in-memory data, or even an `HTTP.FIFOBuffer`. For complete control over the multipart details, an -`HTTP.Multipart` type is provided to support setting the `Content-Type`, `filename`, and `Content-Transfer-Encoding` if desired. See `?HTTP.Multipart` for more details. - -Additional keyword arguments supported, include: - - * `headers::Dict`: headers given as Dict to be sent with the request - * `body`: a request body can be given as a `String`, `Vector{UInt8}`, `IO`, `HTTP.FIFOBuffer` or `Dict`; see example below for how to utilize `HTTP.FIFOBuffer` for "streaming" request bodies; a `Dict` argument will be converted to a multipart form upload - * `stream::Bool=false`: enable response body streaming; depending on the response body size, the request will return before the full body has been received; as the response body is read, additional bytes will be recieved and put in the response body. Readers should read until `eof(response.body) == true`; see below for an example of response streaming - * `chunksize::Int`: if a request body is larger than `chunksize`, the "chunked-transfer" http mechanism will be used and chunks will be sent no larger than `chunksize`; default = `nothing` - * `connecttimeout::Float64`: sets a timeout on how long to wait when trying to connect to a remote host; default = Inf. Note that while setting a timeout will affect the actual program control flow, there are current lower-level limitations that mean underlying resources may not actually be freed until their own timeouts occur (i.e. libuv sockets only timeout after 75 seconds, with no option to configure) - * `readtimeout::Float64`: sets a timeout on how long to wait when receiving a response from a remote host; default = Int - * `tlsconfig::TLS.SSLConfig`: a valid `TLS.SSLConfig` which will be used to initialize every https connection; default = `nothing` - * `maxredirects::Int`: the maximum number of redirects that will automatically be followed for an http request; default = 5 - * `allowredirects::Bool`: whether redirects should be allowed to be followed at all; default = `true` - * `forwardheaders::Bool`: whether user-provided headers should be forwarded on redirects; default = `false` - * `retries::Int`: # of times a request will be tried before throwing an error; default = 3 - * `managecookies::Bool`: whether the request client should automatically store and add cookies from/to requests (following appropriate host-specific & expiration rules); default = `true` - * `statusraise::Bool`: whether an `HTTP.StatusError` should be raised on a non-2XX response status code; default = `true` - * `insecure::Bool`: whether an "https" connection should allow insecure connections (no TLS verification); default = `false` - * `canonicalizeheaders::Bool`: whether header field names should be canonicalized in responses, e.g. `content-type` is canonicalized to `Content-Type`; default = `true` - * `logbody::Bool`: whether the request body should be logged when `verbose=true` is passed; default = `true` - -Simple request example: -```julia -julia> resp = HTTP.get("http://httpbin.org/ip") -HTTP.Response: -\"\"\" -HTTP/1.1 200 OK -Connection: keep-alive -X-Powered-By: Flask -Content-Length: 32 -Via: 1.1 vegur -Access-Control-Allow-Credentials: true -X-Processed-Time: 0.000903129577637 -Date: Wed, 23 Aug 2017 23:35:59 GMT -Content-Type: application/json -Access-Control-Allow-Origin: * -Server: meinheld/0.6.1 -Content-Length: 32 - -{ - "origin": "50.207.241.62" -} -\"\"\" - - -julia> String(resp) -"{\n \"origin\": \"65.130.216.45\"\n}\n" -``` - -Response streaming example (asynchronous download): -```julia -julia> r = HTTP.get("http://httpbin.org/stream/100"; stream=true) -HTTP.Response: -\"\"\" -HTTP/1.1 200 OK -Connection: keep-alive -X-Powered-By: Flask -Transfer-Encoding: chunked -Via: 1.1 vegur -Access-Control-Allow-Credentials: true -X-Processed-Time: 0.000981092453003 -Date: Wed, 23 Aug 2017 23:36:56 GMT -Content-Type: application/json -Access-Control-Allow-Origin: * -Server: meinheld/0.6.1 - -[HTTP.Response body of 27415 bytes] -Content-Length: 27390 - -{"id": 0, "origin": "50.207.241.62", "args": {}, "url": "http://httpbin.org/stream/100", "headers": {"Connection": "close", "User-Agent": "HTTP.jl/0.0.0", "Host": "httpbin.org", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json"}} -{"id": 1, "origin": "50.207.241.62", "args": {}, "url": "http://httpbin.org/stream/100", "headers": {"Connection": "close", "User-Agent": "HTTP.jl/0.0.0", "Host": "httpbin.org", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json"}} -{"id": 2, "origin": "50.207.241.62", "args": {}, "url": "http://httpbin.org/stream/100", "headers": {"Connection": "close", "User-Agent": "HTTP.jl/0.0.0", "Host": "httpbin.org", " -⋮ -\"\"\" - -julia> body = HTTP.body(r) -HTTP.FIFOBuffers.FIFOBuffer(27390, 1048576, 27390, 1, 27391, -1, 27390, UInt8[0x7b, 0x22, 0x69, 0x64, 0x22, 0x3a, 0x20, 0x30, 0x2c, 0x20 … 0x6e, 0x2f, 0x6a, 0x73, 0x6f, 0x6e, 0x22, 0x7d, 0x7d, 0x0a], Condition(Any[]), Task (done) @0x0000000112d84250, true) - -julia> while true - println(String(readavailable(body))) - eof(body) && break - end -{"id": 0, "origin": "50.207.241.62", "args": {}, "url": "http://httpbin.org/stream/100", "headers": {"Connection": "close", "User-Agent": "HTTP.jl/0.0.0", "Host": "httpbin.org", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json"}} -{"id": 1, "origin": "50.207.241.62", "args": {}, "url": "http://httpbin.org/stream/100", "headers": {"Connection": "close", "User-Agent": "HTTP.jl/0.0.0", "Host": "httpbin.org", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json"}} -{"id": 2, "origin": "50.207.241.62", "args": {}, "url": "http://httpbin.org/stream/100", "headers": {"Connection": "close", "User-Agent": "HTTP.jl/0.0.0", "Host": "httpbin.org", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json"}} -{"id": 3, "origin": "50.207.241.62", "args": {}, "url": "http://httpbin.org/stream/100", "headers": {"Connection": "close", "User-Agent": "HTTP.jl/0.0.0", "Host": "httpbin.org", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json"}} -... -``` - -Request streaming example (asynchronous upload): -```julia -# create a FIFOBuffer for sending our request body -f = HTTP.FIFOBuffer() -# write initial data -write(f, "hey") -# start an HTTP.post asynchronously -t = @async HTTP.post("http://httpbin.org/post"; body=f) -write(f, " there ") # as we write to f, it triggers another chunk to be sent in our async request -write(f, "sailor") -close(f) # setting eof on f causes the async request to send a final chunk and return the response - -resp = wait(t) # get our response by getting the result of our asynchronous task -``` - """ =# function $(f) end - - ($f)(uri::AbstractString; verbose::Bool=false, query="", args...) = request(DEFAULT_CLIENT, $meth, URIs.URL(uri; query=query, isconnect=$(f_str == "CONNECT")); verbose=verbose, args...) - ($f)(uri::URI; verbose::Bool=false, args...) = request(DEFAULT_CLIENT, $meth, uri; verbose=verbose, args...) - ($f)(client::Client, uri::AbstractString; query="", args...) = request(client, $meth, URIs.URL(uri; query=query, isconnect=$(f_str == "CONNECT")); args...) - ($f)(client::Client, uri::URI; args...) = request(client, $meth, uri; args...) + ($f)(client::Client, url::URI; kw...) = request(client, $meth, url; kw...) + ($f)(client::Client, url::AbstractString; kw...) = request(client, $meth, URI(url); kw...) + ($f)(url::URI; kw...) = request(DEFAULT_CLIENT, $meth, url; kw...) + ($f)(url::AbstractString; kw...) = request(DEFAULT_CLIENT, $meth, URI(url); kw...) end end -function download(uri::AbstractString, file; threshold::Int=50000000, verbose::Bool=false, query="", args...) - res = request(GET, uri; verbose=verbose, query=query, stream=true, args...) +function download(url::AbstractString, file; threshold::Int=50000000, verbose::Bool=false, query="", args...) + res = request(GET, url; verbose=verbose, query=query, stream=true, args...) body = HTTP.body(res) file = Base.get(HTTP.headers(res), "Content-Encoding", "") == "gzip" ? string(file, ".gz") : file threshold_step = threshold diff --git a/src/urlparser.jl b/src/urlparser.jl index 581ea48db..564d260fa 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -23,12 +23,12 @@ Base.show(io::IO, p::URLParsingError) = println(io, "HTTP.URLParsingError: ", p. @enum(http_parser_url_fields, UF_SCHEME = 1 - , UF_HOST = 2 - , UF_PORT = 3 - , UF_PATH = 4 - , UF_QUERY = 5 - , UF_FRAGMENT = 6 - , UF_USERINFO = 7 + , UF_USERINFO = 2 + , UF_HOST = 3 + , UF_PORT = 4 + , UF_PATH = 5 + , UF_QUERY = 6 + , UF_FRAGMENT = 7 , UF_MAX = 8 ) const UF_SCHEME_MASK = 0x01 @@ -125,7 +125,7 @@ function http_parse_host_char(s::http_host_state, ch) return s_http_host_dead end -function http_parse_host(host::SubString, foundat) +function http_parse_host(host::SubString, foundat=false) host1 = port1 = userinfo1 = 1 host2 = port2 = userinfo2 = 0 @@ -135,7 +135,7 @@ function http_parse_host(host::SubString, foundat) @inbounds p = host[i] new_s = http_parse_host_char(s, p) - if new_s == s_http_host_dead + if new_s == s_http_host_dead throw(URLParsingError("encountered invalid host character: \n" * "$host\n$(lpad("", i-1, "-"))^")) end @@ -181,9 +181,10 @@ function http_parse_host(host::SubString, foundat) end -function http_parser_parse_url(url::AbstractString, isconnect::Bool=false) +function http_parser_parse_url(url::AbstractString) + + s = s_req_spaces_before_url - s = ifelse(isconnect, s_req_server_start, s_req_spaces_before_url) old_uf = UF_MAX off1 = off2 = 0 foundat = false @@ -226,7 +227,7 @@ function http_parser_parse_url(url::AbstractString, isconnect::Bool=false) uf = UF_FRAGMENT mask |= UF_FRAGMENT_MASK else - throw(URLParsingError("ended in unexpected parsing state: $s")) + throw(URLParsingError("ended in unexpected parsing state: $s\n$url")) end if uf == old_uf off2 = i @@ -244,7 +245,7 @@ function http_parser_parse_url(url::AbstractString, isconnect::Bool=false) end check = ~(UF_HOST_MASK | UF_PATH_MASK) if (mask & UF_SCHEME_MASK > 0) && (mask | check == check) - throw(URLParsingError("URI must include host or path with scheme")) + throw(URLParsingError("URI must include host or path with scheme\n$url")) end if mask & UF_HOST_MASK > 0 host, port, userinfo = http_parse_host(parts[UF_HOST], foundat) @@ -261,12 +262,6 @@ function http_parser_parse_url(url::AbstractString, isconnect::Bool=false) mask |= UF_USERINFO_MASK end end - # CONNECT requests can only contain "hostname:port" - if isconnect - chk = UF_HOST_MASK | UF_PORT_MASK - ((mask | chk) > chk) && throw(URLParsingError("connect requests must contain and can only contain both hostname and port")) - end - return URI(url, parts...) end From 6c6dfbb8912219047e09d3df3ba6b4c04f6a9bac Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 16 Jan 2018 03:51:12 +1100 Subject: [PATCH 178/182] update tests and compat --- docs/src/layers.monopic | Bin 8713 -> 8717 bytes src/compat.jl | 2 ++ test/client.jl | 5 ++- test/handlers.jl | 12 +++---- test/loopback.jl | 6 ++-- test/parser.jl | 45 +++++++++++++++---------- test/uri.jl | 72 +++++++++++++++++++++++----------------- 7 files changed, 83 insertions(+), 59 deletions(-) diff --git a/docs/src/layers.monopic b/docs/src/layers.monopic index aa13010c9e83a1b6887f73ad29fc45d43f12b25e..49c4568b2421a79b3af1a23dc08b1e8dfcdfe16b 100644 GIT binary patch delta 5800 zcmZXYcQD-FyT{elq9j`M-ih985M_gvuzC=^mms>2mL*tLud4)6SMNk3qL=7p5iQCR zJ%}J~zQ5m{-@SM4%xliP=R7m#pYwjsGw1zQ9j;!NLjrVyjDnUBCo|qaivpIW3?cQG zXYou8Qm?Ed<5D(XOG{2L9+gdmEVB>n#Bh3m|j zqoP}s>eHRThTVdTia3y4*sGU)=jg!fdyt2YTymXO6x$CgG~50o%Y;*&_}|`I)_V-$u^`s&2!8v&v47bCen8ym=wh zOnb38<&@d4ykVc(Uak`w#Nq*MZP-_!15NXk2pw0{eY-We|G2ENqlY`~z~E)lET8S~ zwKO14+pt2vpXhgcQqkL0NXd)t>y%1bIHRuk(aFo+>!ebxk83x>WuSr2?~-HM*O!O! zM=DNv_noKXGmb2QuPX}Yq5FXavtQj^Vp85@RmDdY^-Qff9ZTTRzoKv2cy=}z85fa) zptENa2dg=x#fA-0)j*DpJikeWj0 z$R++rbGVI}&lcuPZ^~S!U>*4BG<*wtL@gsmkX#^89TFn}Jfs&SO$0gAkw|=(Nc))S zBS**MCpNxkmp3;g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H+bb^L zvs7OGu+-9kS3dvtL6WrGxjf%lo~fbJMQ$eYd%3tM#2T9YR?ML4Jpy9&T{vSZThj^E zzHX!{n9Yh`rR=jURA@IJkm=`~&lfw6+d72X!$vrZ`sX|A3)Y6ePPTpMi zryHL0?Xq|w9F>6ZaS>hY5e8A=NYgAt_bohhDlN@u`H(V@@paS6>Ab#)QdQQQlOkNk zy)wgJnMpBdJNjpP+DAK|(@9q|sUkrUx~Y+qIn-clDR4x&$Dh%;*vAGPprz9 zadYYExKlZ~GrVDtoqQ16$ueYTc~Z5FznC6eoXsXXt*W=G2u}**{eCe~bkEcj1g v for (k,v) in x] + Base.SubString(s) = SubString(s, 1) + using MicroLogging end diff --git a/test/client.jl b/test/client.jl index 1754f22c0..e5bdfd567 100644 --- a/test/client.jl +++ b/test/client.jl @@ -1,3 +1,6 @@ +using HTTP +using HTTP.Test + @testset "HTTP.Client" begin using JSON @@ -24,7 +27,7 @@ for sch in ("http", "https") @test status(HTTP.get("$sch://httpbin.org/encoding/utf8")) == 200 println("pass query to uri") - r = HTTP.get("$sch://httpbin.org/response-headers"; query=Dict("hey"=>"dude")) + r = HTTP.get(merge(HTTP.URI("$sch://httpbin.org/response-headers"); query=Dict("hey"=>"dude"))) h = Dict(r.headers) @test (haskey(h, "Hey") ? h["Hey"] == "dude" : h["hey"] == "dude") diff --git a/test/handlers.jl b/test/handlers.jl index 654c23cdf..6d107ad26 100644 --- a/test/handlers.jl +++ b/test/handlers.jl @@ -20,7 +20,7 @@ r = HTTP.Router() HTTP.register!(r, "/path/to/greatness", f) @test length(methods(r.func)) == 2 req = HTTP.Request() -req.uri = "/path/to/greatness" +req.target = "/path/to/greatness" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(200) p = "/next/path/to/greatness" @@ -28,7 +28,7 @@ f2 = HTTP.HandlerFunction((req, resp) -> HTTP.Response(201)) HTTP.register!(r, p, f2) @test length(methods(r.func)) == 3 req = HTTP.Request() -req.uri = "/next/path/to/greatness" +req.target = "/next/path/to/greatness" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(201) r = HTTP.Router() @@ -57,16 +57,16 @@ f4 = HTTP.HandlerFunction((req, resp) -> HTTP.Response(203)) HTTP.register!(r, "/test/*/ghotra/seven", f4) req = HTTP.Request() -req.uri = "/test" +req.target = "/test" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(200) -req.uri = "/test/sarv" +req.target = "/test/sarv" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(201) -req.uri = "/test/sarv/ghotra" +req.target = "/test/sarv/ghotra" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(202) -req.uri = "/test/sar/ghotra/seven" +req.target = "/test/sar/ghotra/seven" @test HTTP.handle(r, req, HTTP.Response()) == HTTP.Response(203) end diff --git a/test/loopback.jl b/test/loopback.jl index 4c3c5fd7c..371cce865 100644 --- a/test/loopback.jl +++ b/test/loopback.jl @@ -104,7 +104,7 @@ function Base.unsafe_write(lb::Loopback, p::Ptr{UInt8}, n::UInt) println("📡 $(sprint(showcompact, req))") push!(server_events, "Request: $(sprint(showcompact, req))") - if req.uri == "/abort" + if req.target == "/abort" reset(lb) response = HTTP.Response(403, ["Connection" => "close", "Content-Length" => 0]; request=req) @@ -118,10 +118,10 @@ function Base.unsafe_write(lb::Loopback, p::Ptr{UInt8}, n::UInt) l = length(req.body) response = HTTP.Response(200, ["Content-Length" => l], body = req.body; request=req) - if req.uri == "/echo" + if req.target == "/echo" push!(server_events, "Response: $(sprint(showcompact, response))") write(lb.io, response) - elseif (m = match(r"^/delay([0-9]*)$", req.uri)) != nothing + elseif (m = match(r"^/delay([0-9]*)$", req.target)) != nothing t = parse(Int, first(m.captures)) sleep(t/10) push!(server_events, "Response: $(sprint(showcompact, response))") diff --git a/test/parser.jl b/test/parser.jl index d33110158..89a2de3e5 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1,3 +1,6 @@ +using HTTP +using HTTP.Test + module ParserTest using ..Test @@ -1453,17 +1456,23 @@ const responses = Message[ r = Request(req.raw) #r = HTTP.parse(HTTP.Request, req.raw; extraref=upgrade) end - uri = parse(HTTP.URI, r.uri; isconnect= req.method == "CONNECT") + if r.method == "CONNECT" + host, port, userinfo = HTTP.URIs.http_parse_host(SubString(r.target)) + @test host == req.host + @test port == req.port + else + target = parse(HTTP.URI, r.target) + @test target.query == req.query_string + @test target.fragment == req.fragment + @test target.path == req.request_path + @test target.host == req.host + @test target.userinfo == req.userinfo + @test target.port in (req.port, "80", "443") + @test string(target) == req.request_url + end @test r.version.major == req.http_major @test r.version.minor == req.http_minor @test r.method == string(req.method) - @test uri.query == req.query_string - @test uri.fragment == req.fragment - @test uri.path == req.request_path - @test uri.host == req.host - @test uri.userinfo == req.userinfo - @test uri.port in (req.port, "80", "443") - @test string(uri) == req.request_url @test length(r.headers) == req.num_headers @test Dict(HTTP.CanonicalizeRequest.canonicalizeheaders(r.headers)) == Dict(req.headers) @test String(r.body) == req.body @@ -1532,7 +1541,7 @@ const responses = Message[ req = Request() req.method = "POST" - req.uri = "/" + req.target = "/" req.headers = ["Host"=>"foo.com", "Transfer-Encoding"=>"chunked", "Trailer-Key"=>"Trailer-Value"] req.body = Vector{UInt8}("foobar") @@ -1555,7 +1564,7 @@ const responses = Message[ req = Request() req.method = "CONNECT" - req.uri = "www.google.com:443" # FIXME; isconnect=true) + req.target = "www.google.com:443" @test Request(reqstr) == req @@ -1563,7 +1572,7 @@ const responses = Message[ req = Request() req.method = "CONNECT" - req.uri = "127.0.0.1:6060" #FIXME; isconnect=true) + req.target = "127.0.0.1:6060" @test Request(reqstr) == req @@ -1571,7 +1580,7 @@ const responses = Message[ # # req = HTTP.Request() # req.method = "CONNECT" - # req.uri = HTTP.URI("/_goRPC_"; isconnect=true) + # req.target = HTTP.URI("/_goRPC_"; isconnect=true) # @test HTTP.parse(HTTP.Request, reqstr) == req @@ -1579,7 +1588,7 @@ const responses = Message[ req = Request() req.method = "NOTIFY" - req.uri = "*" + req.target = "*" req.headers = ["Server"=>"foo"] @test Request(reqstr) == req @@ -1588,7 +1597,7 @@ const responses = Message[ req = Request() req.method = "OPTIONS" - req.uri = "*" + req.target = "*" req.headers = ["Server"=>"foo"] @test Request(reqstr) == req @@ -1604,7 +1613,7 @@ const responses = Message[ req = Request() req.method = "HEAD" - req.uri = "/" + req.target = "/" req.headers = ["Host"=>"issue8261.com", "Connection"=>"close", "Content-Length"=>"0"] @test Request(reqstr) == req @@ -1621,7 +1630,7 @@ const responses = Message[ req = Request() req.method = "POST" - req.uri = "/cgi-bin/process.cgi" + req.target = "/cgi-bin/process.cgi" req.headers = ["User-Agent"=>"Mozilla/4.0 (compatible; MSIE5.01; Windows NT)", "Host"=>"www.tutorialspoint.com", "Content-Type"=>"text/xml; charset=utf-8", @@ -1675,7 +1684,7 @@ const responses = Message[ if !HTTP.Parsers.strict r = HTTP.parse(HTTP.Messages.Request, reqstr) @test r.method == "GET" - @test r.uri == "/" + @test r.target == "/" @test length(r.headers) == 1 end @@ -1684,7 +1693,7 @@ const responses = Message[ if !HTTP.Parsers.strict r = parse(HTTP.Messages.Request, reqstr) @test r.method == "GET" - @test r.uri == "/" + @test r.target == "/" @test length(r.headers) == 1 end diff --git a/test/uri.jl b/test/uri.jl index f534e6b23..883a740c7 100644 --- a/test/uri.jl +++ b/test/uri.jl @@ -1,3 +1,5 @@ +using HTTP +using HTTP.Test mutable struct URLTest name::String @@ -32,12 +34,12 @@ end @testset "HTTP.URI" begin # constructor @test string(HTTP.URI("")) == "" - @test HTTP.URI(host="google.com") == HTTP.URI("http://google.com") - @test HTTP.URI(host="google.com", path="/") == HTTP.URI("http://google.com/") - @test HTTP.URI(host="google.com", userinfo="user") == HTTP.URI("http://user@google.com") - @test HTTP.URI(host="google.com", path="user") == HTTP.URI("http://google.com/user") - @test HTTP.URI(host="google.com", query=Dict("key"=>"value")) == HTTP.URI("http://google.com?key=value") - @test HTTP.URI(host="google.com", fragment="user") == HTTP.URI("http://google.com/#user") + @test HTTP.URI(scheme="http", host="google.com") == HTTP.URI("http://google.com") + @test HTTP.URI(scheme="http", host="google.com", path="/") == HTTP.URI("http://google.com/") + @test HTTP.URI(scheme="http", host="google.com", userinfo="user") == HTTP.URI("http://user@google.com") + @test HTTP.URI(scheme="http", host="google.com", path="/user") == HTTP.URI("http://google.com/user") + @test HTTP.URI(scheme="http", host="google.com", query=Dict("key"=>"value")) == HTTP.URI("http://google.com?key=value") + @test HTTP.URI(scheme="http", host="google.com", path="/", fragment="user") == HTTP.URI("http://google.com/#user") urls = [("hdfs://user:password@hdfshost:9000/root/folder/file.csv#frag", ["root", "folder", "file.csv"]), ("https://user:password@httphost:9000/path1/path2;paramstring?q=a&p=r#frag", ["path1", "path2;paramstring"]), @@ -63,7 +65,7 @@ end end @test parse(HTTP.URI, "hdfs://user:password@hdfshost:9000/root/folder/file.csv") == HTTP.URI(host="hdfshost", path="/root/folder/file.csv", scheme="hdfs", port=9000, userinfo="user:password") - @test parse(HTTP.URI, "http://google.com:80/some/path") == HTTP.URI(host="google.com", path="/some/path") + @test parse(HTTP.URI, "http://google.com:80/some/path") == HTTP.URI(scheme="http", host="google.com", path="/some/path") @test HTTP.Strings.lower(UInt8('A')) == UInt8('a') @test HTTP.escapeuri(Char(1)) == "%01" @@ -109,156 +111,156 @@ end ,"http://hostname/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(8, 8) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(16, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("proxy request with port" ,"http://hostname:444/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(8, 8) # UF_HOST ,Offset(17, 3) # UF_PORT ,Offset(20, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("CONNECT request" ,"hostname:443" ,true ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(1, 8) # UF_HOST ,Offset(10, 3) # UF_PORT ,Offset(0, 0) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("proxy ipv6 request" ,"http://[1:2::3:4]/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(9, 8) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(18, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("proxy ipv6 request with port" ,"http://[1:2::3:4]:67/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(9, 8) # UF_HOST ,Offset(19, 2) # UF_PORT ,Offset(21, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("CONNECT ipv6 address" ,"[1:2::3:4]:443" ,true ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(2, 8) # UF_HOST ,Offset(12, 3) # UF_PORT ,Offset(0, 0) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("ipv4 in ipv6 address" ,"http://[2001:0000:0000:0000:0000:0000:1.9.1.1]/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(9,37) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(47, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("extra ? in query string" ,"http://a.tbcdn.cn/p/fp/2010c/??fp-header-min.css,fp-base-min.css,fp-channel-min.css,fp-product-min.css,fp-mall-min.css,fp-category-min.css,fp-sub-min.css,fp-gdp4p-min.css,fp-css3-min.css,fp-misc-min.css?t=20101022.css" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(8,10) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(18,12) # UF_PATH ,Offset(31,187) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("space URL encoded" ,"/toto.html?toto=a%20b" ,false ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(0, 0) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(1,10) # UF_PATH ,Offset(12,10) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("URL fragment" ,"/toto.html#titi" ,false ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(0, 0) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(1,10) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(12, 4) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("complex URL fragment" ,"http://www.webmasterworld.com/r.cgi?f=21&d=8405&url=http://www.example.com/index.html?foo=bar&hello=world#midpage" ,false ,(Offset( 1, 4) # UF_SCHEMA + ,Offset( 0, 0) # UF_USERINFO ,Offset( 8, 22) # UF_HOST ,Offset( 0, 0) # UF_PORT ,Offset( 30, 6) # UF_PATH ,Offset( 37, 69) # UF_QUERY ,Offset(107, 7) # UF_FRAGMENT - ,Offset( 0, 0) # UF_USERINFO ) ,false ), URLTest("complex URL from node js url parser doc" ,"http://host.com:8080/p/a/t/h?query=string#hash" ,false ,( Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(8, 8) # UF_HOST ,Offset(17, 4) # UF_PORT ,Offset(21, 8) # UF_PATH ,Offset(30,12) # UF_QUERY ,Offset(43, 4) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("complex URL with basic auth from node js url parser doc" ,"http://a:b@host.com:8080/p/a/t/h?query=string#hash" ,false ,( Offset(1, 4) # UF_SCHEMA + ,Offset(8, 3) # UF_USERINFO ,Offset(12, 8) # UF_HOST ,Offset(21, 4) # UF_PORT ,Offset(25, 8) # UF_PATH ,Offset(34,12) # UF_QUERY ,Offset(47, 4) # UF_FRAGMENT - ,Offset(8, 3) # UF_USERINFO ) ,false ), URLTest("double @" @@ -297,12 +299,12 @@ end ,"http://a%20:b@host.com/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(8, 6) # UF_USERINFO ,Offset(15, 8) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(23, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(8, 6) # UF_USERINFO ) ,false ), URLTest("carriage return in URL" @@ -317,12 +319,12 @@ end ,"http://a::b@host.com/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(8, 4) # UF_USERINFO ,Offset(13, 8) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(21, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(8, 4) # UF_USERINFO ) ,false ), URLTest("line feed in URL" @@ -333,12 +335,12 @@ end ,"http://@hostname/fo" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(9, 8) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(17, 3) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("proxy line feed in hostname" @@ -357,12 +359,12 @@ end ,"http://a!;-_!=+\$@host.com/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(8, 9) # UF_USERINFO ,Offset(18, 8) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(26, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(8, 9) # UF_USERINFO ) ,false ), URLTest("proxy only empty basic auth" @@ -381,24 +383,24 @@ end ,"http://[fe80::a%25eth0]/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(9,14) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(24, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("ipv6 address with Zone ID, but '%' is not percent-encoded" ,"http://[fe80::a%eth0]/" ,false ,(Offset(1, 4) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(9,12) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(22, 1) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("ipv6 address ending with '%'" @@ -417,24 +419,24 @@ end ,"/foo\tbar/" ,false ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(0, 0) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(1, 9) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ), URLTest("form feed in URL" ,"/foo\fbar/" ,false ,(Offset(0, 0) # UF_SCHEMA + ,Offset(0, 0) # UF_USERINFO ,Offset(0, 0) # UF_HOST ,Offset(0, 0) # UF_PORT ,Offset(1, 9) # UF_PATH ,Offset(0, 0) # UF_QUERY ,Offset(0, 0) # UF_FRAGMENT - ,Offset(0, 0) # UF_USERINFO ) ,false ) @@ -442,10 +444,18 @@ end for u in urltests println("TEST - uri.jl: $(u.name)") - if u.shouldthrow - @test_throws HTTP.URIs.URLParsingError parse(HTTP.URI, u.url; isconnect=u.isconnect) + if u.isconnect + if u.shouldthrow + @test_throws HTTP.URIs.URLParsingError HTTP.URIs.http_parse_host(SubString(u.url)) + else + host, port, userinfo = HTTP.URIs.http_parse_host(SubString(u.url)) + @test host == u.expecteduri.host + @test port == u.expecteduri.port + end + elseif u.shouldthrow + @test_throws HTTP.URIs.URLParsingError parse(HTTP.URI, u.url) else - url = parse(HTTP.URI, u.url; isconnect=u.isconnect) + url = parse(HTTP.URI, u.url) @test u.expecteduri == url end end From 7d92068b0a38d4965be549f20970d09753e983bb Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 16 Jan 2018 04:10:26 +1100 Subject: [PATCH 179/182] try to fix Inexact Error in win32 --- src/Messages.jl | 2 +- src/client.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Messages.jl b/src/Messages.jl index 012836a6d..fc0e09793 100644 --- a/src/Messages.jl +++ b/src/Messages.jl @@ -75,7 +75,7 @@ using ..IOExtras using ..Parsers import ..Parsers: headerscomplete, reset! -const unknown_length = typemax(Int64) +const unknown_length = typemax(Int) abstract type Message end diff --git a/src/client.jl b/src/client.jl index fb25b213b..d7c838172 100644 --- a/src/client.jl +++ b/src/client.jl @@ -121,7 +121,7 @@ function request(client::Client, method, url::URI; if body != "" Base.depwarn( - "The body= option is deprecated. Use request(method, uri,headers, body)", + "The body= option is deprecated. Use request(method, uri, headers, body)", :body) end From d27163cb0a2847866412f07bf0a3154924e0be8a Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 16 Jan 2018 15:37:41 +1100 Subject: [PATCH 180/182] Update @ensure macro to print values of failed comparison. (expr-fu stolen from Test.jl) --- src/debug.jl | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/debug.jl b/src/debug.jl index 696028207..568d327c6 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -35,26 +35,57 @@ end @require precondition [message] Throw `ArgumentError` if `precondition` is false. """ -macro require(precondition, msg = string(precondition)) - esc(:(if ! $precondition throw(precondition_error($msg, backtrace()[1])) end)) +macro require(condition, msg = string(condition)) + esc(:(if ! $condition throw(precondition_error($msg, backtrace()[1])) end)) end -@noinline function postcondition_error(msg, frame) +@noinline function postcondition_error(msg, frame, ls="", l="", rs="", r="") msg = string(sprint(StackTraces.show_spec_linfo, StackTraces.lookup(frame)[2]), " failed to ensure ", msg) + if ls != "" + msg = string(msg, "\n", ls, " = ", sprint(show, l), + "\n", rs, " = ", sprint(show, r)) + end return AssertionError(msg) end +# Copied from stdlib/Test/src/Test.jl:get_test_result() +iscondition(ex) = isa(ex, Expr) && + ex.head == :call && + length(ex.args) == 3 && + first(string(ex.args[1])) != '.' && + (!isa(ex.args[2], Expr) || ex.args[2].head != :...) && + (!isa(ex.args[3], Expr) || ex.args[3].head != :...) && + (ex.args[1] === :(==) || + Base.operator_precedence(ex.args[1]) == + Base.operator_precedence(:(==))) + """ @ensure postcondition [message] Throw `ArgumentError` if `postcondition` is false. """ -macro ensure(postcondition, msg = string(postcondition)) - esc(:(if ! $postcondition throw(postcondition_error($msg, backtrace()[1])) end)) +macro ensure(condition, msg = string(condition)) + + if DEBUG_LEVEL < 0 + return :() + end + + if iscondition(condition) + l,r = condition.args[2], condition.args[3] + ls, rs = string(l), string(r) + return esc(quote + if ! $condition + throw(postcondition_error($msg, backtrace()[1], + $ls, $l, $rs, $r)) + end + end) + end + + esc(:(if ! $condition throw(postcondition_error($msg, backtrace()[1])) end)) end From b6ea4c7cea54a3099e45c9d127f5daa3bdb54aa5 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 16 Jan 2018 16:02:40 +1100 Subject: [PATCH 181/182] Add postconditions to URI(;kw...) and parse(::URI, ...) to ensure that the result can be losslessly transformed back into the input. Don't allow '*' at the start of URI path. https://github.com/JuliaWeb/HTTP.jl/commit/1226c4b29d6833bb93bc753b8683d9a848cefcaf#r26878192 Add sentinal const blank_userinfo to handle test cases with empty '@' userinfo. Remove UF_XXX and UF_XXX_MASK constants. It's simpler to use the state machine constants to keep track of state, and use local variables to keep track of values --- src/URIs.jl | 68 ++++++++++++++++++++++------- src/urlparser.jl | 109 +++++++++++++++++------------------------------ 2 files changed, 93 insertions(+), 84 deletions(-) diff --git a/src/URIs.jl b/src/URIs.jl index de19f7eee..b116d636a 100644 --- a/src/URIs.jl +++ b/src/URIs.jl @@ -3,6 +3,8 @@ module URIs import Base.== import ..@require, ..precondition_error +import ..@ensure, ..postcondition_error + include("urlparser.jl") @@ -52,7 +54,6 @@ end URI(uri::URI) = uri - const emptyuri = (()->begin uri = "" empty = SubString(uri) @@ -69,31 +70,40 @@ function Base.merge(uri::URI; scheme::AbstractString=uri.scheme, query=uri.query, fragment::AbstractString=uri.fragment) - @require isempty(scheme) || path != "*" @require isempty(host) || host[end] != '/' @require scheme in uses_authority || isempty(host) @require !isempty(host) || isempty(port) @require !(scheme in ["http", "https"]) || isempty(path) || path[1] == '/' @require !isempty(path) || !isempty(query) || isempty(fragment) - io = IOBuffer() + ports = string(port) + querys = query isa String ? query : escapeuri(query) - isempty(scheme) || print(io, scheme, scheme in uses_authority ? - "://" : ":") - isempty(userinfo) || print(io, userinfo, "@") - isempty(host) || print(io, hoststring(host)) - isempty(port) || print(io, ":", port) - isempty(path) || print(io, path) - isempty(query) || print(io, "?", escapeuri(query)) - isempty(fragment) || print(io, "#", fragment) - - return URI(String(take!(io))) + str = uristring(scheme, userinfo, host, ports, path, querys, fragment) + result = parse(URI, str) + + if uri === emptyuri + @ensure result.scheme == scheme + @ensure result.userinfo == userinfo + @ensure result.host == host + @ensure result.port == ports + @ensure result.path == path + @ensure result.query == querys + end + + return result end URI(str::AbstractString) = Base.parse(URI, str) -Base.parse(::Type{URI}, str::AbstractString) = http_parser_parse_url(str) +function Base.parse(::Type{URI}, str::AbstractString) + + uri = http_parser_parse_url(str) + + @ensure uristring(uri) == str + return uri +end ==(a::URI,b::URI) = a.scheme == b.scheme && @@ -121,6 +131,34 @@ Base.print(io::IO, u::URI) = print(io, u.uri) Base.string(u::URI) = u.uri +nouserinfo(ui) = isempty(ui) && !(ui === blank_userinfo) + +function formaturi(io::IO, + scheme::AbstractString, + userinfo::AbstractString, + host::AbstractString, + port::AbstractString, + path::AbstractString, + query::AbstractString, + fragment::AbstractString) + + isempty(scheme) || print(io, scheme, scheme in uses_authority ? + "://" : ":") + nouserinfo(userinfo) || print(io, userinfo, "@") + isempty(host) || print(io, hoststring(host)) + isempty(port) || print(io, ":", port) + isempty(path) || print(io, path) + isempty(query) || print(io, "?", query) + isempty(fragment) || print(io, "#", fragment) + + return io +end + +uristring(a...) = String(take!(formaturi(IOBuffer(), a...))) + +uristring(u::URI) = uristring(u.scheme, u.userinfo, u.host, u.port, + u.path, u.query, u.fragment) + queryparams(uri::URI) = queryparams(uri.query) function queryparams(q::AbstractString) @@ -131,7 +169,7 @@ end # Validate known URI formats -const uses_authority = ["https", "http", "hdfs", "ftp", "gopher", "nntp", "telnet", "imap", "wais", "file", "mms", "shttp", "snews", "prospero", "rtsp", "rtspu", "rsync", "svn", "svn+ssh", "sftp" ,"nfs", "git", "git+ssh", "ldap", "s3", "ws"] +const uses_authority = ["https", "http", "ws", "wss", "hdfs", "ftp", "gopher", "nntp", "telnet", "imap", "wais", "file", "mms", "shttp", "snews", "prospero", "rtsp", "rtspu", "rsync", "svn", "svn+ssh", "sftp" ,"nfs", "git", "git+ssh", "ldap", "s3"] const non_hierarchical = ["gopher", "hdl", "mailto", "news", "telnet", "wais", "imap", "snews", "sip", "sips"] const uses_query = ["http", "wais", "imap", "https", "shttp", "mms", "gopher", "rtsp", "rtspu", "sip", "sips", "ldap"] const uses_fragment = ["hdfs", "ftp", "hdl", "http", "gopher", "news", "nntp", "wais", "https", "shttp", "snews", "file", "prospero"] diff --git a/src/urlparser.jl b/src/urlparser.jl index 564d260fa..618110a8b 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -21,32 +21,7 @@ Base.show(io::IO, p::URLParsingError) = println(io, "HTTP.URLParsingError: ", p. s_http_host_port, ) -@enum(http_parser_url_fields, - UF_SCHEME = 1 - , UF_USERINFO = 2 - , UF_HOST = 3 - , UF_PORT = 4 - , UF_PATH = 5 - , UF_QUERY = 6 - , UF_FRAGMENT = 7 - , UF_MAX = 8 -) -const UF_SCHEME_MASK = 0x01 -const UF_HOST_MASK = 0x02 -const UF_PORT_MASK = 0x04 -const UF_PATH_MASK = 0x08 -const UF_QUERY_MASK = 0x10 -const UF_FRAGMENT_MASK = 0x20 -const UF_USERINFO_MASK = 0x40 - -@inline function Base.getindex(A::Vector{T}, i::http_parser_url_fields) where {T} - @inbounds v = A[Int(i)] - return v -end -@inline function Base.setindex!(A::Vector{T}, v::T, i::http_parser_url_fields) where {T} - @inbounds v = setindex!(A, v, Int(i)) - return v -end +const blank_userinfo = SubString("blank_userinfo", 1, 0) # url parsing function parseurlchar(s, ch::Char, strict::Bool) @@ -54,7 +29,7 @@ function parseurlchar(s, ch::Char, strict::Bool) strict && (ch == '\t' || ch == '\f') && return s_dead if s == s_req_spaces_before_url || s == s_req_url_start - (ch == '/' || ch == '*') && return s_req_path + (ch == '/') && return s_req_path isalpha(ch) && return s_req_schema elseif s == s_req_schema isalphanum(ch) && return s @@ -181,18 +156,21 @@ function http_parse_host(host::SubString, foundat=false) end -function http_parser_parse_url(url::AbstractString) +http_parser_parse_url(url::AbstractString) = http_parser_parse_url(String(url)) + +function http_parser_parse_url(url::String) s = s_req_spaces_before_url - old_uf = UF_MAX + old_uf = -1 off1 = off2 = 0 foundat = false empty = SubString(url, 1, 0) - parts = [empty, empty, empty, empty, empty, empty, empty] + scheme = userinfo = host = port = path = query = fragment = empty mask = 0x00 + end_i = endof(url) for i in eachindex(url) @inbounds p = url[i] olds = s @@ -207,62 +185,55 @@ function http_parser_parse_url(url::AbstractString) s_req_query_string_start, s_req_fragment_start) continue - elseif s == s_req_schema - uf = UF_SCHEME - mask |= UF_SCHEME_MASK elseif s == s_req_server_with_at foundat = true - uf = UF_HOST - mask |= UF_HOST_MASK - elseif s == s_req_server - uf = UF_HOST - mask |= UF_HOST_MASK - elseif s == s_req_path - uf = UF_PATH - mask |= UF_PATH_MASK - elseif s == s_req_query_string - uf = UF_QUERY - mask |= UF_QUERY_MASK - elseif s == s_req_fragment - uf = UF_FRAGMENT - mask |= UF_FRAGMENT_MASK + uf = s_req_server + elseif @anyeq(s, s_req_schema, + s_req_server_with_at, + s_req_server, + s_req_path, + s_req_query_string, + s_req_fragment) + uf = s else throw(URLParsingError("ended in unexpected parsing state: $s\n$url")) end if uf == old_uf off2 = i - continue + if i != end_i + continue + end end - if old_uf != UF_MAX - parts[old_uf] = SubString(url, off1, off2) + + + @label save_part + if old_uf != -1 + part = SubString(url, off1, off2) + old_uf == s_req_schema && (scheme = part) + old_uf == s_req_server && (host = part) + old_uf == s_req_path && (path = part) + old_uf == s_req_query_string && (query = part) + old_uf == s_req_fragment && (fragment = part) end + off1 = i off2 = i + if i == end_i && uf != old_uf + old_uf = uf + @goto save_part + end old_uf = uf end - if old_uf != UF_MAX - parts[old_uf] = SubString(url, off1, off2) - end - check = ~(UF_HOST_MASK | UF_PATH_MASK) - if (mask & UF_SCHEME_MASK > 0) && (mask | check == check) + if !isempty(scheme) && isempty(host) && isempty(path) throw(URLParsingError("URI must include host or path with scheme\n$url")) end - if mask & UF_HOST_MASK > 0 - host, port, userinfo = http_parse_host(parts[UF_HOST], foundat) - if !isempty(host) - parts[UF_HOST] = host - mask |= UF_HOST_MASK - end - if !isempty(port) - parts[UF_PORT] = port - mask |= UF_PORT_MASK - end - if !isempty(userinfo) - parts[UF_USERINFO] = userinfo - mask |= UF_USERINFO_MASK + if !isempty(host) + host, port, userinfo = http_parse_host(host, foundat) + if foundat && isempty(userinfo) + userinfo = blank_userinfo end end - return URI(url, parts...) + return URI(url, scheme, userinfo, host, port, path, query, fragment) end const normal_url_char = Bool[ From 23dab2166571c6780589103de468f34eb8541a6e Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Tue, 16 Jan 2018 16:34:09 +1100 Subject: [PATCH 182/182] handle asterix-form Request Target https://tools.ietf.org/html/rfc7230#section-5.3.4 --- src/Parsers.jl | 28 ++++++++++++++++++++-------- src/consts.jl | 5 +++-- src/urlparser.jl | 4 ++-- test/parser.jl | 20 ++++++++++++-------- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/Parsers.jl b/src/Parsers.jl index 80f30ad96..13a874351 100644 --- a/src/Parsers.jl +++ b/src/Parsers.jl @@ -402,22 +402,34 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, if parser.message.method == "HTTP" && ch == '/' p_state = s_res_first_http_major elseif ch == ' ' - p_state = s_req_spaces_before_url + p_state = s_req_spaces_before_target else @err(:HPE_INVALID_METHOD) end end - elseif p_state == s_req_spaces_before_url + elseif p_state == s_req_spaces_before_target ch == ' ' && continue if parser.message.method == "CONNECT" p_state = s_req_server_start + p -= 1 + elseif ch == '*' + p_state = s_req_target_wildcard else - p_state = s_req_url_start + p_state = s_req_target_start + p -= 1 + end + + elseif p_state == s_req_target_wildcard + + if @anyeq(ch, ' ', CR, LF) + parser.message.target = "*" + p_state = s_req_http_start + else + @err(:HPE_INVALID_TARGET) end - p -= 1 - elseif @anyeq(p_state, s_req_url_start, + elseif @anyeq(p_state, s_req_target_start, s_req_server_start, s_req_server, s_req_server_with_at, @@ -436,7 +448,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, @errorif(@anyeq(p_state, s_req_schema, s_req_schema_slash, s_req_schema_slash_slash, s_req_server_start), - :HPE_INVALID_URL) + :HPE_INVALID_TARGET) if ch == ' ' p_state = s_req_http_start else @@ -448,7 +460,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#, break end p_state = parseurlchar(p_state, ch, strict) - @errorif(p_state == s_dead, :HPE_INVALID_URL) + @errorif(p_state == s_dead, :HPE_INVALID_TARGET) p += 1 end @passert p <= len + 1 @@ -806,7 +818,7 @@ const ERROR_MESSAGES = Dict( :HPE_INVALID_VERSION => "invalid HTTP version", :HPE_INVALID_STATUS => "invalid HTTP status code", :HPE_INVALID_METHOD => "invalid HTTP method", - :HPE_INVALID_URL => "invalid URL", + :HPE_INVALID_TARGET => "invalid HTTP Request Target", :HPE_LF_EXPECTED => "LF character expected", :HPE_INVALID_HEADER_TOKEN => "invalid character in header", :HPE_INVALID_CONTENT_LENGTH => "invalid character in content-length header", diff --git a/src/consts.jl b/src/consts.jl index d08726410..90bf94b86 100644 --- a/src/consts.jl +++ b/src/consts.jl @@ -14,8 +14,9 @@ ,es_res_line_almost_done ,es_start_req ,es_req_method - ,es_req_spaces_before_url - ,es_req_url_start + ,es_req_spaces_before_target + ,es_req_target_start + ,es_req_target_wildcard ,es_req_schema ,es_req_schema_slash ,es_req_schema_slash_slash diff --git a/src/urlparser.jl b/src/urlparser.jl index 618110a8b..046862f05 100644 --- a/src/urlparser.jl +++ b/src/urlparser.jl @@ -28,7 +28,7 @@ function parseurlchar(s, ch::Char, strict::Bool) @anyeq(ch, ' ', '\r', '\n') && return s_dead strict && (ch == '\t' || ch == '\f') && return s_dead - if s == s_req_spaces_before_url || s == s_req_url_start + if s == s_req_spaces_before_target || s == s_req_target_start (ch == '/') && return s_req_path isalpha(ch) && return s_req_schema elseif s == s_req_schema @@ -160,7 +160,7 @@ http_parser_parse_url(url::AbstractString) = http_parser_parse_url(String(url)) function http_parser_parse_url(url::String) - s = s_req_spaces_before_url + s = s_req_spaces_before_target old_uf = -1 off1 = off2 = 0 diff --git a/test/parser.jl b/test/parser.jl index 89a2de3e5..cf7645d4b 100644 --- a/test/parser.jl +++ b/test/parser.jl @@ -1461,14 +1461,18 @@ const responses = Message[ @test host == req.host @test port == req.port else - target = parse(HTTP.URI, r.target) - @test target.query == req.query_string - @test target.fragment == req.fragment - @test target.path == req.request_path - @test target.host == req.host - @test target.userinfo == req.userinfo - @test target.port in (req.port, "80", "443") - @test string(target) == req.request_url + if r.target == "*" + @test r.target == req.request_path + else + target = parse(HTTP.URI, r.target) + @test target.query == req.query_string + @test target.fragment == req.fragment + @test target.path == req.request_path + @test target.host == req.host + @test target.userinfo == req.userinfo + @test target.port in (req.port, "80", "443") + @test string(target) == req.request_url + end end @test r.version.major == req.http_major @test r.version.minor == req.http_minor