Skip to content

Commit

Permalink
Merge branch 'master' into jn/readbytes-internals
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash authored Feb 17, 2024
2 parents e83cc6d + 040cf0a commit be46f7d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "HTTP"
uuid = "cd3eb016-35fb-5094-929b-558a96fad6f3"
authors = ["Jacob Quinn", "contributors: https://github.com/JuliaWeb/HTTP.jl/graphs/contributors"]
version = "1.10.1"
version = "1.10.2"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
4 changes: 2 additions & 2 deletions src/Connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ Read until `find_delimiter(bytes)` returns non-zero.
Return view of bytes up to the delimiter.
"""
function IOExtras.readuntil(c::Connection, f::F #=Vector{UInt8} -> Int=#,
sizehint=4096)::ByteView where {F <: Function}
sizehint=4096) where {F <: Function}
buf = c.buffer
if bytesavailable(buf) == 0
read_to_buffer(c, sizehint)
end
while (bytes = IOExtras.readuntil(buf, f)) == nobytes
while isempty(begin bytes = IOExtras.readuntil(buf, f) end)
read_to_buffer(c, sizehint)
end
return bytes
Expand Down
7 changes: 3 additions & 4 deletions src/IOExtras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Sockets
using MbedTLS: SSLContext, MbedException
using OpenSSL: SSLStream

export bytes, isbytes, nbytes, ByteView, nobytes,
export bytes, isbytes, nbytes, nobytes,
startwrite, closewrite, startread, closeread, readuntil,
tcpsocket, localport, safe_getpeername

Expand Down Expand Up @@ -104,7 +104,6 @@ function safe_getpeername(io)
end


const ByteView = typeof(view(UInt8[], 1:0))
const nobytes = view(UInt8[], 1:0)

readuntil(args...) = Base.readuntil(args...)
Expand All @@ -114,8 +113,8 @@ Read from an `IO` stream until `find_delimiter(bytes)` returns non-zero.
Return view of bytes up to the delimiter.
"""
function readuntil(buf::IOBuffer,
find_delimiter::F #= Vector{UInt8} -> Int =#
)::ByteView where {F <: Function}
find_delimiter::F #= Vector{UInt8} -> Int =#
) where {F <: Function}
l = find_delimiter(view(buf.data, buf.ptr:buf.size))
if l == 0
return nobytes
Expand Down
30 changes: 20 additions & 10 deletions src/Servers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ function shutdown(fn::Function)
try
fn()
catch
msg = current_exceptions_to_string()
@error "shutdown function $fn failed. $msg"
@error begin
msg = current_exceptions_to_string()
"shutdown function $fn failed. $msg"
end
end
end

Expand Down Expand Up @@ -393,8 +395,10 @@ function listenloop(f, listener, conns, tcpisvalid,
if e isa Base.IOError && e.code == Base.UV_ECONNABORTED
verbose >= 0 && @infov 1 "Server on $(listener.hostname):$(listener.hostport) closing"
else
msg = current_exceptions_to_string()
@errorv 2 "Server on $(listener.hostname):$(listener.hostport) errored. $msg"
@errorv 2 begin
msg = current_exceptions_to_string()
"Server on $(listener.hostname):$(listener.hostport) errored. $msg"
end
# quick little sleep in case there's a temporary
# local error accepting and this might help avoid quickly re-erroring
sleep(0.05 + rand() * 0.05)
Expand Down Expand Up @@ -433,8 +437,10 @@ function handle_connection(f, c::Connection, listener, readtimeout, access_log)
if e isa ParseError
write(c, Response(e.code == :HEADER_SIZE_EXCEEDS_LIMIT ? 431 : 400, string(e.code)))
end
msg = current_exceptions_to_string()
@debugv 1 "handle_connection startread error. $msg"
@debugv 1 begin
msg = current_exceptions_to_string()
"handle_connection startread error. $msg"
end
break
end

Expand All @@ -461,8 +467,10 @@ function handle_connection(f, c::Connection, listener, readtimeout, access_log)
# The remote can close the stream whenever it wants to, but there's nothing
# anyone can do about it on this side. No reason to log an error in that case.
level = e isa Base.IOError && !isopen(c) ? Logging.Debug : Logging.Error
msg = current_exceptions_to_string()
@logmsgv 1 level "handle_connection handler error. $msg"
@logmsgv 1 level begin
msg = current_exceptions_to_string()
"handle_connection handler error. $msg"
end

if isopen(http) && !iswritable(http)
request.response.status = 500
Expand All @@ -478,8 +486,10 @@ function handle_connection(f, c::Connection, listener, readtimeout, access_log)
end
catch
# we should be catching everything inside the while loop, but just in case
msg = current_exceptions_to_string()
@errorv 1 "error while handling connection. $msg"
@errorv 1 begin
msg = current_exceptions_to_string()
"error while handling connection. $msg"
end
finally
if readtimeout > 0
wait_for_timeout[] = false
Expand Down
3 changes: 2 additions & 1 deletion src/Streams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ function readall!(http::Stream, buf::Base.GenericIOBuffer=PipeBuffer())
return n
end

function IOExtras.readuntil(http::Stream, f::Function)::ByteView
function IOExtras.readuntil(http::Stream, f::Function)
UInt(ntoread(http)) == 0 && return Connections.nobytes
try
bytes = IOExtras.readuntil(http.stream, f)
Expand All @@ -322,6 +322,7 @@ function IOExtras.readuntil(http::Stream, f::Function)::ByteView
catch ex
ex isa EOFError() || rethrow()
# if we error, it means we didn't find what we were looking for
# TODO: this seems very sketchy
return UInt8[]
end
end
Expand Down
6 changes: 4 additions & 2 deletions src/WebSockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,10 @@ function upgrade(f::Function, http::Streams.Stream; suppress_close_error::Bool=f
f(ws)
catch e
if !isok(e)
msg = current_exceptions_to_string()
suppress_close_error || @error "$(ws.id): Unexpected websocket server error. $msg"
suppress_close_error || @error begin
msg = current_exceptions_to_string()
"$(ws.id): Unexpected websocket server error. $msg"
end
end
if !isclosed(ws)
if e isa WebSocketError && e.message isa CloseFrameBody
Expand Down
9 changes: 3 additions & 6 deletions src/clientlayers/ConnectionRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ function connectionlayer(handler)
io = newconnection(IOType, url.host, url.port; readtimeout=readtimeout, connect_timeout=connect_timeout, kw...)
catch e
if logerrors
msg = current_exceptions_to_string()
@error msg type=Symbol("HTTP.ConnectError") method=req.method url=req.url context=req.context logtag=logtag
@error current_exceptions_to_string() type=Symbol("HTTP.ConnectError") method=req.method url=req.url context=req.context logtag=logtag
end
req.context[:connect_errors] = get(req.context, :connect_errors, 0) + 1
throw(ConnectError(string(url), e))
Expand Down Expand Up @@ -127,12 +126,10 @@ function connectionlayer(handler)
root_err = ExceptionUnwrapping.unwrap_exception_to_root(e)
# don't log if it's an HTTPError since we should have already logged it
if logerrors && root_err isa StatusError
msg = current_exceptions_to_string()
@error msg type=Symbol("HTTP.StatusError") method=req.method url=req.url context=req.context logtag=logtag
@error current_exceptions_to_string() type=Symbol("HTTP.StatusError") method=req.method url=req.url context=req.context logtag=logtag
end
if logerrors && !ExceptionUnwrapping.has_wrapped_exception(e, HTTPError)
msg = current_exceptions_to_string()
@error msg type=Symbol("HTTP.ConnectionRequest") method=req.method url=req.url context=req.context logtag=logtag
@error current_exceptions_to_string() type=Symbol("HTTP.ConnectionRequest") method=req.method url=req.url context=req.context logtag=logtag
end
@debugv 1 "❗️ ConnectionLayer $root_err. Closing: $io"
if @isdefined(stream) && stream.nwritten == -1
Expand Down
6 changes: 1 addition & 5 deletions src/clientlayers/StreamRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ function streamlayer(stream::Stream; iofunction=nothing, decompress::Union{Nothi
if timedout === nothing || !timedout[]
req.context[:io_errors] = get(req.context, :io_errors, 0) + 1
if logerrors
msg = current_exceptions_to_string()
@error msg type=Symbol("HTTP.IOError") method=req.method url=req.url context=req.context logtag=logtag
@error current_exceptions_to_string() type=Symbol("HTTP.IOError") method=req.method url=req.url context=req.context logtag=logtag
end
end
rethrow()
Expand Down Expand Up @@ -143,9 +142,6 @@ function readbody(stream::Stream, res::Response, decompress::Union{Nothing, Bool
end
end

# 2 most common types of IOBuffers
const IOBuffers = Union{IOBuffer, Base.GenericIOBuffer{SubArray{UInt8, 1, Vector{UInt8}, Tuple{UnitRange{Int64}}, true}}}

function readbody!(stream::Stream, res::Response, buf_or_stream, lock)
n = 0
if !iserror(res)
Expand Down
3 changes: 1 addition & 2 deletions src/clientlayers/TimeoutRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ function timeoutlayer(handler)
req = stream.message.request
req.context[:timeout_errors] = get(req.context, :timeout_errors, 0) + 1
if logerrors
msg = current_exceptions_to_string()
@error msg type=Symbol("HTTP.TimeoutError") method=req.method url=req.url context=req.context timeout=readtimeout logtag=logtag
@error current_exceptions_to_string() type=Symbol("HTTP.TimeoutError") method=req.method url=req.url context=req.context timeout=readtimeout logtag=logtag
end
e = Exceptions.TimeoutError(readtimeout)
end
Expand Down

0 comments on commit be46f7d

Please sign in to comment.