diff --git a/Project.toml b/Project.toml index c5f32a95b..08903b0c0 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,7 @@ Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" IniFile = "83e8ac13-25f8-5344-8a64-a9f2b223428f" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" +LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36" MbedTLS = "739be429-bea8-5141-9913-cc70e7f3736d" NetworkOptions = "ca575930-c2e3-43a9-ace4-1e988b2c1908" Sockets = "6462fe0b-24de-5631-8697-dd941f90decc" diff --git a/src/BasicAuthRequest.jl b/src/BasicAuthRequest.jl index f8c72cb47..c797ae80e 100644 --- a/src/BasicAuthRequest.jl +++ b/src/BasicAuthRequest.jl @@ -3,7 +3,7 @@ module BasicAuthRequest using ..Base64 using URIs import ..Messages: setheader, hasheader -import ..@debug, ..DEBUG_LEVEL +using LoggingExtras export basicauthlayer """ @@ -16,7 +16,7 @@ function basicauthlayer(handler) if basicauth userinfo = unescapeuri(req.url.userinfo) if !isempty(userinfo) && !hasheader(req.headers, "Authorization") - @debug 1 "Adding Authorization: Basic header." + @debugv 1 "Adding Authorization: Basic header." setheader(req.headers, "Authorization" => "Basic $(base64encode(userinfo))") end end diff --git a/src/ConnectionPool.jl b/src/ConnectionPool.jl index 754c2fd64..3be388882 100644 --- a/src/ConnectionPool.jl +++ b/src/ConnectionPool.jl @@ -22,8 +22,9 @@ module ConnectionPool export Connection, newconnection, getrawstream, inactiveseconds using ..IOExtras, ..Sockets +using LoggingExtras -import ..@debug, ..DEBUG_LEVEL, ..taskid +import ..DEBUG_LEVEL, ..taskid import ..@require, ..precondition_error, ..@ensure, ..postcondition_error using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake! import NetworkOptions @@ -221,7 +222,7 @@ end function IOExtras.startwrite(c::Connection) @require !iswritable(c) c.writable = true - @debug 2 "👁 Start write:$c" + @debugv 2 "👁 Start write:$c" return end @@ -233,7 +234,7 @@ Signal that an entire Request Message has been written to the `Connection`. function IOExtras.closewrite(c::Connection) @require iswritable(c) c.writable = false - @debug 2 "🗣 Write done: $c" + @debugv 2 "🗣 Write done: $c" flush(c) return end @@ -245,7 +246,7 @@ function IOExtras.startread(c::Connection) @require !isreadable(c) c.timestamp = time() c.readable = true - @debug 2 "👁 Start read: $c" + @debugv 2 "👁 Start read: $c" return end @@ -254,7 +255,7 @@ Wait for `c` to receive data or reach EOF. Close `c` on EOF or if response data arrives when no request was sent. """ function monitor_idle_connection(c::Connection) - if eof(c.io) ;@debug 2 "💀 Closed: $c" + if eof(c.io) ;@debugv 2 "💀 Closed: $c" close(c.io) end end @@ -267,7 +268,7 @@ Signal that an entire Response Message has been read from the `Connection`. function IOExtras.closeread(c::Connection) @require isreadable(c) c.readable = false - @debug 2 "✉️ Read done: $c" + @debugv 2 "✉️ Read done: $c" if c.clientconnection release(POOL, connectionkey(c), c) # Ignore SSLContext as it already monitors idle connections for TLS close_notify messages @@ -357,7 +358,7 @@ function newconnection(::Type{T}, end function keepalive!(tcp) - @debug 2 "setting keepalive on tcp socket" + @debugv 2 "setting keepalive on tcp socket" err = ccall(:uv_tcp_keepalive, Cint, (Ptr{Nothing}, Cint, Cuint), tcp.handle, 1, 1) err != 0 && error("error setting keepalive on socket") @@ -379,7 +380,7 @@ function getconnection(::Type{TCPSocket}, p::UInt = isempty(port) ? UInt(80) : parse(UInt, port) - @debug 2 "TCP connect: $host:$p..." + @debugv 2 "TCP connect: $host:$p..." addrs = Sockets.getalladdrinfo(host) @@ -449,7 +450,7 @@ function getconnection(::Type{SSLContext}, kw...)::SSLContext port = isempty(port) ? "443" : port - @debug 2 "SSL connect: $host:$port..." + @debugv 2 "SSL connect: $host:$port..." tcp = getconnection(TCPSocket, host, port; kw...) return sslconnection(tcp, host; kw...) end diff --git a/src/ConnectionRequest.jl b/src/ConnectionRequest.jl index e92693746..5c0ef7e7d 100644 --- a/src/ConnectionRequest.jl +++ b/src/ConnectionRequest.jl @@ -6,7 +6,7 @@ using ..IOExtras using ..ConnectionPool using MbedTLS: SSLContext, SSLConfig using Base64: base64encode -import ..@debug, ..DEBUG_LEVEL +using LoggingExtras import ..Streams: Stream # hasdotsuffix reports whether s ends in "."+suffix. @@ -69,7 +69,7 @@ function connectionlayer(handler) userinfo = unescapeuri(url.userinfo) if !isempty(userinfo) && !hasheader(req.headers, "Proxy-Authorization") - @debug 1 "Adding Proxy-Authorization: Basic header." + @debugv 1 "Adding Proxy-Authorization: Basic header." setheader(req.headers, "Proxy-Authorization" => "Basic $(base64encode(userinfo))") end else @@ -106,7 +106,7 @@ function connectionlayer(handler) return resp catch e - @debug 1 "❗️ ConnectionLayer $e. Closing: $io" + @debugv 1 "❗️ ConnectionLayer $e. Closing: $io" try; close(io); catch; end rethrow(isioerror(e) ? IOError(e, "during request($url)") : e) end @@ -117,7 +117,7 @@ sockettype(url::URI, default) = url.scheme in ("wss", "https") ? SSLContext : de function connect_tunnel(io, target_url, req) target = "$(URIs.hoststring(target_url.host)):$(target_url.port)" - @debug 1 "📡 CONNECT HTTPS tunnel to $target" + @debugv 1 "📡 CONNECT HTTPS tunnel to $target" headers = Dict("Host" => target) if (auth = header(req, "Proxy-Authorization"); !isempty(auth)) headers["Proxy-Authorization"] = auth diff --git a/src/ContentTypeRequest.jl b/src/ContentTypeRequest.jl index 64495902c..ea7ea80a4 100644 --- a/src/ContentTypeRequest.jl +++ b/src/ContentTypeRequest.jl @@ -5,7 +5,7 @@ import ..sniff import ..Form using ..Messages import ..IOExtras -import ..@debug, ..DEBUG_LEVEL +using LoggingExtras export contenttypedetectionlayer @@ -17,7 +17,7 @@ function contenttypedetectionlayer(handler) sn = sniff(bytes(req.body)) setheader(req.headers, "Content-Type" => sn) - @debug 1 "setting Content-Type header to: $sn" + @debugv 1 "setting Content-Type header to: $sn" end return handler(req; kw...) end diff --git a/src/CookieRequest.jl b/src/CookieRequest.jl index 895653ac4..820a113bd 100644 --- a/src/CookieRequest.jl +++ b/src/CookieRequest.jl @@ -1,10 +1,11 @@ module CookieRequest import ..Dates +using LoggingExtras using URIs using ..Cookies using ..Messages: Request, ascii_lc_isequal, header, setheader -import ..@debug, ..DEBUG_LEVEL, ..access_threaded +import ..access_threaded const default_cookiejar = Dict{String, Set{Cookie}}[] @@ -67,10 +68,10 @@ function getcookies(cookies, url) url.host, url.path) t = cookie.expires if t != Dates.DateTime(1) && t < Dates.now(Dates.UTC) - @debug 1 "Deleting expired Cookie: $cookie.name" + @debugv 1 "Deleting expired Cookie: $cookie.name" push!(expired, cookie) else - @debug 1 "Sending Cookie: $cookie.name to $url.host" + @debugv 1 "Sending Cookie: $cookie.name to $url.host" push!(tosend, cookie) end end @@ -82,7 +83,7 @@ end function setcookies(cookies, host, headers) for (k, v) in headers ascii_lc_isequal(k, "set-cookie") || continue - @debug 1 "Set-Cookie: $v (from $host)" + @debugv 1 "Set-Cookie: $v (from $host)" push!(cookies, Cookies.readsetcookie(host, v)) end end diff --git a/src/DebugRequest.jl b/src/DebugRequest.jl index 598332a27..d24619839 100644 --- a/src/DebugRequest.jl +++ b/src/DebugRequest.jl @@ -1,25 +1,26 @@ module DebugRequest +using Logging, LoggingExtras import ..DEBUG_LEVEL -using ..IOExtras -import ..Streams: Stream - -include("IODebug.jl") export debuglayer """ debuglayer(stream::Stream) -> HTTP.Response -Wrap the `IO` stream in an `IODebug` stream and print Message data. +If `verbose` keyword arg is > 0, or the HTTP.jl global `DEBUG_LEVEL[]` is > 0, +then enabled debug logging with verbosity `verbose` for the lifetime of the request. """ function debuglayer(handler) - return function(stream::Stream; verbose::Int=0, kw...) - # if debugging, wrap stream.stream in IODebug - if verbose >= 3 || DEBUG_LEVEL[] >= 3 - stream = Stream(stream.message, IODebug(stream.stream)) + return function(request; verbose::Int=0, kw...) + # if debugging, enable by wrapping request in custom logger logic + if verbose >= 0 || DEBUG_LEVEL[] >= 0 + LoggingExtras.with(level=Debug, verbosity=verbose) do + handler(request; verbose=verbose, kw...) + end + else + return handler(request; verbose=verbose, kw...) end - return handler(stream; verbose=verbose, kw...) end end diff --git a/src/HTTP.jl b/src/HTTP.jl index fab06395d..875a0232d 100644 --- a/src/HTTP.jl +++ b/src/HTTP.jl @@ -9,6 +9,7 @@ Base.@deprecate escape escapeuri using Base64, Sockets, Dates using URIs +using LoggingExtras function access_threaded(f, v::Vector) tid = Threads.threadid() @@ -330,7 +331,7 @@ function request(method, url, h=Header[], b=nobody; return request(HTTP.stack(), method, url, headers, body, query; kw...) end -const STREAM_LAYERS = [timeoutlayer, exceptionlayer, debuglayer] +const STREAM_LAYERS = [timeoutlayer, exceptionlayer] const REQUEST_LAYERS = [redirectlayer, defaultheaderslayer, basicauthlayer, contenttypedetectionlayer, cookielayer, retrylayer, canonicalizelayer] pushlayer!(layer; request::Bool=true) = push!(request ? REQUEST_LAYERS : STREAM_LAYERS, layer) @@ -348,8 +349,9 @@ function stack( layers2 = foldr((x, y) -> x(y), STREAM_LAYERS, init=layers) # request layers # messagelayer must be the 1st/outermost layer to convert initial args to Request + # we also want debuglayer to be early to ensure any debug logging is handled correctly in other layers layers3 = foldr((x, y) -> x(y), requestlayers; init=connectionlayer(layers2)) - return messagelayer(foldr((x, y) -> x(y), REQUEST_LAYERS; init=layers3)) + return messagelayer(debuglayer(foldr((x, y) -> x(y), REQUEST_LAYERS; init=layers3))) end function request(stack::Base.Callable, method, url, h=Header[], b=nobody, q=nothing; diff --git a/src/Handlers.jl b/src/Handlers.jl index 6cf029a5d..890c8f36b 100644 --- a/src/Handlers.jl +++ b/src/Handlers.jl @@ -1,9 +1,9 @@ module Handlers -export serve, Router, register! +export serve, Router, register!, getparams, getcookies using URIs -using ..Messages, ..Streams, ..IOExtras, ..Servers, ..Sockets +using ..Messages, ..Streams, ..IOExtras, ..Servers, ..Sockets, ..Cookies """ streamhandler(request_handler) -> stream handler @@ -291,4 +291,17 @@ function (r::Router)(req) end end +getparams(req) = get(req.context, :params, nothing) + +function cookie_middleware(handler) + function (req) + if !haskey(req.context, :cookies) + req.context[:cookies] = Cookies.cookies(req) + end + return handler(req) + end +end + +getcookies(req) = get(() => Cookie[], req.context, :cookies) + end # module diff --git a/src/IODebug.jl b/src/IODebug.jl deleted file mode 100644 index 231f7513f..000000000 --- a/src/IODebug.jl +++ /dev/null @@ -1,94 +0,0 @@ - -import ..debug_header - -struct IODebug{T <: IO} <: IO - io::T -end - -logwrite(iod::IODebug, f, x) = show_io_debug(stdout, "➡️ ", f, x) -logread(iod::IODebug, f, x) = show_io_debug(stdout, "⬅️ ", f, x) - -Base.wait_close(iod::IODebug) = Base.wait_close(iod.io) - -Base.write(iod::IODebug, a...) = - (logwrite(iod, :write, join(a)); - write(iod.io, a...)) - -Base.write(iod::IODebug, a::Array) = - (logwrite(iod, :write, join(a)); - write(iod.io, a)) - -Base.write(iod::IODebug, x::String) = - (logwrite(iod, :write, x); - write(iod.io, x)) - -Base.unsafe_write(iod::IODebug, x::Ptr{UInt8}, n::UInt) = - (logwrite(iod, :unsafe_write, unsafe_string(x,n)); - unsafe_write(iod.io, x, n)) - -Base.unsafe_read(iod::IODebug, x::Ptr{UInt8}, n::UInt) = - (r = unsafe_read(iod.io, x, n); - logread(iod, :unsafe_read, unsafe_string(x,n)); r) - -Base.read(iod::IODebug, n::Integer) = - (r = read(iod.io, n); - logread(iod, :read, String(copy(r))); r) - -Base.readavailable(iod::IODebug) = - (r = readavailable(iod.io); - logread(iod, :readavailable, String(copy(r))); r) - -Base.readuntil(iod::IODebug, f) = - (r = readuntil(iod.io, f); - logread(iod, :readuntil, String(copy(r))); r) - -Base.readuntil(iod::IODebug, f, h) = - (r = readuntil(iod.io, f, h); - logread(iod, :readuntil, String(copy(r))); r) - -Base.eof(iod::IODebug) = eof(iod.io) -Base.close(iod::IODebug) = close(iod.io) -Base.isopen(iod::IODebug) = isopen(iod.io) -Base.iswritable(iod::IODebug) = iswritable(iod.io) -Base.isreadable(iod::IODebug) = isreadable(iod.io) -IOExtras.startread(iod::IODebug) = startread(iod.io) -IOExtras.startwrite(iod::IODebug) = startwrite(iod.io) -IOExtras.closeread(iod::IODebug) = closeread(iod.io) -IOExtras.closewrite(iod::IODebug) = closewrite(iod.io) - -Base.bytesavailable(iod::IODebug) = bytesavailable(iod.io) - -Base.show(io::IO, iod::IODebug) = show(io, iod.io) - -function show_log(io::IO, iod::IODebug) - lock(io) - println(io, "$(typeof(iod)):\nio: $(iod.io)") - prevop = "" - for (operation, f, bytes) in iod.log - if prevop != "" && prevop != operation - println(io) - end - show_io_debug(io, operation, f, bytes) - prevop = operation - end - println(io) - unlock(io) -end - - -function show_io_debug(io::IO, operation, f, bytes) - prefix = string(debug_header(), rpad(operation, 4)) - i = j = 1 - while i <= length(bytes) - j = findnext(isequal('\n'), bytes, i) - if j === nothing || j == 0 - j = prevind(bytes, length(bytes)+1) - end - println(io, prefix, "\"", escape_string(bytes[i:j]), "\"", - i == 1 ? " ($f)" : "") - if i == 1 - prefix = rpad("DEBUG:", length(prefix) - 1) - end - i = nextind(bytes, j) - end -end diff --git a/src/RedirectRequest.jl b/src/RedirectRequest.jl index 50fb755cf..80471ec98 100644 --- a/src/RedirectRequest.jl +++ b/src/RedirectRequest.jl @@ -4,7 +4,7 @@ using URIs using ..Messages using ..Pairs: setkv import ..Header -import ..@debug, ..DEBUG_LEVEL +using LoggingExtras export redirectlayer, nredirects @@ -51,7 +51,7 @@ function redirectlayer(handler) else req.headers = Header[] end - @debug 1 "➡️ Redirect: $url" + @debugv 1 "➡️ Redirect: $url" count += 1 end @assert false "Unreachable!" diff --git a/src/RetryRequest.jl b/src/RetryRequest.jl index 1c495e635..ea652ffef 100644 --- a/src/RetryRequest.jl +++ b/src/RetryRequest.jl @@ -5,7 +5,8 @@ using ..Sockets using ..IOExtras using ..MessageRequest using ..Messages -import ..@debug, ..DEBUG_LEVEL, ..sprintcompact +import ..sprintcompact +using LoggingExtras export retrylayer @@ -33,10 +34,10 @@ function retrylayer(handler) check=(s, ex)->begin retry = isrecoverable(ex, req, retry_non_idempotent, get(req.context, :retrycount, 0)) if retry - @debug 1 "🔄 Retry $ex: $(sprintcompact(req))" + @debugv 1 "🔄 Retry $ex: $(sprintcompact(req))" reset!(req.response) else - @debug 1 "🚷 No Retry: $(no_retry_reason(ex, req))" + @debugv 1 "🚷 No Retry: $(no_retry_reason(ex, req))" end return s, retry end) diff --git a/src/Servers.jl b/src/Servers.jl index ce8ddc736..d645f94a7 100644 --- a/src/Servers.jl +++ b/src/Servers.jl @@ -23,7 +23,8 @@ import MbedTLS using Dates -import ..@debug, ..DEBUG_LEVEL, ..taskid, ..access_threaded +import ..taskid, ..access_threaded +using LoggingExtras # rate limiting mutable struct RateLimit @@ -388,7 +389,7 @@ function handle_transaction(f, c::Connection, server; final_transaction::Bool=fa http = Stream(request, c) try - @debug 2 "server startread" + @debugv 2 "server startread" startread(http) if !isopen(server) close(c) @@ -418,9 +419,9 @@ function handle_transaction(f, c::Connection, server; final_transaction::Bool=fa if isopen(http) && !iswritable(http) error("Server never wrote a response") end - @debug 2 "server closeread" + @debugv 2 "server closeread" closeread(http) - @debug 2 "server closewrite" + @debugv 2 "server closewrite" closewrite(http) catch e # The remote can close the stream whenever it wants to, but there's nothing diff --git a/src/StreamRequest.jl b/src/StreamRequest.jl index 6061a7be1..29aef0fb1 100644 --- a/src/StreamRequest.jl +++ b/src/StreamRequest.jl @@ -6,7 +6,8 @@ using ..Streams import ..ConnectionPool using ..MessageRequest import ..RedirectRequest: nredirects -import ..@debug, ..DEBUG_LEVEL, ..printlncompact, ..sprintcompact +import ..sprintcompact +using LoggingExtras export streamlayer @@ -21,19 +22,17 @@ 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). """ -function streamlayer(stream::Stream; iofunction=nothing, verbose=0, redirect_limit::Int=3, kw...)::Response +function streamlayer(stream::Stream; iofunction=nothing, redirect_limit::Int=3, kw...)::Response response = stream.message req = response.request io = stream.stream - verbose == 1 && printlncompact(req) - @debug 2 "client startwrite" + @debugv 1 sprintcompact(req) + @debugv 2 "client startwrite" startwrite(stream) - if verbose == 2 - println(req) - if iofunction === nothing && !isbytes(req.body) - println("$(typeof(req)).body: $(sprintcompact(req.body))") - end + @debugv 2 sprint(show, req) + if iofunction === nothing && !isbytes(req.body) + @debugv 2 "$(typeof(req)).body: $(sprintcompact(req.body))" end write_error = nothing @@ -42,13 +41,13 @@ function streamlayer(stream::Stream; iofunction=nothing, verbose=0, redirect_lim if iofunction === nothing @async try writebody(stream, req) - @debug 2 "client closewrite" + @debugv 2 "client closewrite" closewrite(stream) catch e write_error = e isopen(io) && try; close(io); catch; end end - @debug 2 "client startread" + @debugv 2 "client startread" startread(stream) readbody(stream, response, redirect_limit == nredirects(req)) else @@ -68,13 +67,13 @@ function streamlayer(stream::Stream; iofunction=nothing, verbose=0, redirect_lim end end - @debug 2 "client closewrite" + @debugv 2 "client closewrite" closewrite(stream) - @debug 2 "client closeread" + @debugv 2 "client closeread" closeread(stream) - verbose == 1 && printlncompact(response) - verbose == 2 && println(response) + @debugv 1 sprintcompact(response) + @debugv 2 sprint(show, response) return response end diff --git a/src/Streams.jl b/src/Streams.jl index a75c4a56c..9616a8e5a 100644 --- a/src/Streams.jl +++ b/src/Streams.jl @@ -13,7 +13,7 @@ import ..Messages: header, hasheader, setheader, import ..ConnectionPool: getrawstream import ..@require, ..precondition_error import ..@ensure, ..postcondition_error -import ..@debug, ..DEBUG_LEVEL +using LoggingExtras mutable struct Stream{M <: Message, S <: IO} <: IO message::M @@ -144,7 +144,7 @@ function IOExtras.closewrite(http::Stream{<:Request}) http.message.version < v"1.1" && !hasheader(http.message, "Connection", "keep-alive") - @debug 1 "✋ \"Connection: close\": $(http.stream)" + @debugv 1 "✋ \"Connection: close\": $(http.stream)" close(http.stream) end end @@ -178,7 +178,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)" + @debugv 1 "✅ Continue: $(http.stream)" readheaders(http.stream, http.message) end end @@ -188,7 +188,7 @@ function handle_continue(http::Stream{<:Request}) if !iswritable(http.stream) startwrite(http.stream) end - @debug 1 "✅ Continue: $(http.stream)" + @debugv 1 "✅ Continue: $(http.stream)" writeheaders(http.stream, Response(100)) end end @@ -251,7 +251,7 @@ function Base.read(http::Stream, ::Type{UInt8}) if http.warn_not_to_read_one_byte_at_a_time @warn "Reading one byte at a time from HTTP.Stream is inefficient.\n" * "Use: io = BufferedInputStream(http::HTTP.Stream) instead.\n" * - "See: https://github.com/BioJulia/BufferedStreams.jl" stacktrace() + "See: https://github.com/BioJulia/BufferedStreams.jl" http.warn_not_to_read_one_byte_at_a_time = false end @@ -330,9 +330,9 @@ function isaborted(http::Stream{<:Response}) if iswritable(http.stream) && iserror(http.message) && hasheader(http.message, "Connection", "close") - @debug 1 "✋ Abort on $(sprint(writestartline, http.message)): " * + @debugv 1 "✋ Abort on $(sprint(writestartline, http.message)): " * "$(http.stream)" - @debug 2 "✋ $(http.message)" + @debugv 2 "✋ $(http.message)" return true end return false @@ -345,7 +345,7 @@ function IOExtras.closeread(http::Stream{<:Response}) if hasheader(http.message, "Connection", "close") # Close conncetion if server sent "Connection: close"... - @debug 1 "✋ \"Connection: close\": $(http.stream)" + @debugv 1 "✋ \"Connection: close\": $(http.stream)" close(http.stream) # Error if Message is not complete... incomplete(http) && throw(EOFError()) diff --git a/src/TimeoutRequest.jl b/src/TimeoutRequest.jl index 1dbb3226a..38246a0c1 100644 --- a/src/TimeoutRequest.jl +++ b/src/TimeoutRequest.jl @@ -1,7 +1,7 @@ module TimeoutRequest using ..ConnectionPool -import ..@debug, ..DEBUG_LEVEL +using LoggingExtras import ..Streams: Stream struct ReadTimeoutError <:Exception @@ -33,7 +33,7 @@ function timeoutlayer(handler) if isreadable(io) && inactiveseconds(io) > readtimeout timedout[] = true close(io) - @debug 1 "💥 Read inactive > $(readtimeout)s: $io" + @debugv 1 "💥 Read inactive > $(readtimeout)s: $io" break end sleep(readtimeout / 10) diff --git a/src/WebSockets.jl b/src/WebSockets.jl index 8a37288b3..0eb2ab2f6 100644 --- a/src/WebSockets.jl +++ b/src/WebSockets.jl @@ -7,7 +7,8 @@ using ..IOExtras using ..Streams import ..ConnectionPool using HTTP: header, headercontains -import ..@debug, ..DEBUG_LEVEL, ..@require, ..precondition_error +import ..@require, ..precondition_error +using LoggingExtras import ..string const WS_FINAL = 0x80 @@ -183,7 +184,7 @@ end function IOExtras.closewrite(ws::WebSocket; statuscode=nothing) @require !ws.txclosed opcode = WS_FINAL | WS_CLOSE - @debug 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, 0x00))" + @debugv 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, 0x00))" if statuscode === nothing write(ws.io, [opcode, 0x00]) else @@ -213,10 +214,10 @@ function wswrite(ws::WebSocket, opcode::UInt8, bytes::AbstractVector{UInt8}) txpayload = ws.txpayload end - @debug 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, len, extended_len, mask))" + @debugv 1 "WebSocket ⬅️ $(WebSocketHeader(opcode, len, extended_len, mask))" write(ws.io, vcat(opcode, len, extended_len, mask)) - @debug 2 " ⬅️ $(txpayload[1:n])" + @debugv 2 " ⬅️ $(txpayload[1:n])" unsafe_write(ws.io, pointer(txpayload), n) end @@ -284,7 +285,7 @@ readframe(ws::WebSocket) = first(_readframe(ws)) function _readframe(ws::WebSocket) h = readheader(ws.io) - @debug 1 "WebSocket ➡️ $h" + @debugv 1 "WebSocket ➡️ $h" len = Int(h.length) @@ -293,7 +294,7 @@ function _readframe(ws::WebSocket) resize!(ws.rxpayload, len) end unsafe_read(ws.io, pointer(ws.rxpayload), len) - @debug 2 " ➡️ \"$(String(ws.rxpayload[1:len]))\"" + @debugv 2 " ➡️ \"$(String(ws.rxpayload[1:len]))\"" end if h.hasmask diff --git a/src/download.jl b/src/download.jl index 2c1e94e4c..499a62928 100644 --- a/src/download.jl +++ b/src/download.jl @@ -104,7 +104,7 @@ function download(url::AbstractString, local_path=nothing, headers=Header[]; upd format_bytes_per_second(x) = format_bytes(x) * "/s" - @debug 1 "downloading $url" + @debugv 1 "downloading $url" local file hdrs = String[] HTTP.open("GET", url, headers; kw...) do stream