Skip to content

Commit

Permalink
Ensure we don't try to show an open FIFOBuffer for requests/responses.
Browse files Browse the repository at this point in the history
…Fixes #85
  • Loading branch information
quinnj committed Aug 30, 2017
1 parent 2197c13 commit 8a48266
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
10 changes: 8 additions & 2 deletions src/fifobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function Base.seek(f::FIFOBuffer, pos::Tuple{Int64, Int64, Int64})
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)
Expand Down Expand Up @@ -170,7 +171,7 @@ end
function Base.read(f::FIFOBuffer, ::Type{Tuple{UInt8,Bool}})
# no data to read
if f.nb == 0
if current_task() == f.task
if current_task() == f.task || f.eof
return 0x00, false
else # async: block till there's data to read
f.eof && return 0x00, false
Expand Down Expand Up @@ -210,7 +211,12 @@ function Base.write(f::FIFOBuffer, b::UInt8)
push!(f.buffer, 0x00)
f.len += 1
else
return 0
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
Expand Down
1 change: 1 addition & 0 deletions src/multipart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ end

Form(f::Form) = f
Base.eof(f::Form) = f.index > length(f.data)
Base.isopen(f::Form) = false
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
Expand Down
2 changes: 1 addition & 1 deletion src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ function parse!(r::Union{Request, Response}, parser, bytes, len=length(bytes);
@debug(PARSING_DEBUG, @__LINE__, "exiting maybe unfinished...")
@debug(PARSING_DEBUG, @__LINE__, ParsingStateCode(p_state))
b = p_state == start_state || p_state == s_dead
he = b | (p_state >= s_headers_done)
he = b | (p_state >= s_chunk_size_start)

This comment has been minimized.

Copy link
@samoconnor

samoconnor Aug 30, 2017

Contributor

Are you sure this is the right fix?
The patch I posted in #86 was just a speculative hack.
Perhaps the chunk states should be moved to after the header states here:?
https://github.com/JuliaWeb/HTTP.jl/blob/master/src/consts.jl#L292
Perhaps there is some good reason for the sequence to be the way it is?

This comment has been minimized.

Copy link
@quinnj

quinnj Aug 30, 2017

Author Member

I think it's probably fine; this is our own construct anyway (i.e. not ported from http-parser.c). I just pushed another commit though that should end up a tad cleaner. See 9200ae3

m = b | (p_state >= s_body_identity_eof)
return errno, he, m, String(bytes[p:end])

Expand Down
36 changes: 20 additions & 16 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,26 +344,30 @@ function Base.show(io::IO, r::Union{Request,Response}; opts=RequestOptions())
startline(io, r)
headers(io, r)
buf = IOBuffer()
body(buf, r, opts)
b = take!(buf)
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, "")
if isopen(r.body)
print(io, "\n[open HTTP.FIFOBuffer with $(length(r.body)) bytes to read")
else
body(buf, r, opts)
b = take!(buf)
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
print(io, String(b))
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
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]")
print(io, String(b))
end
else
print(io, String(b))
end
print(io, "\"\"\"")
end

0 comments on commit 8a48266

Please sign in to comment.