From ae93f7d82b26f02ceb3a008a5975c0e98c3778bf Mon Sep 17 00:00:00 2001 From: Daniel Matz Date: Fri, 4 Nov 2022 09:05:54 -0500 Subject: [PATCH] Allow specifying the username --- README.md | 4 ++++ src/Curl/Curl.jl | 1 + src/Curl/Easy.jl | 4 ++++ src/Downloads.jl | 11 +++++++++++ 4 files changed, 20 insertions(+) diff --git a/README.md b/README.md index b1cbb3b..409c052 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ download(url, [ output = tempname() ]; [ verbose = false, ] [ debug = , ] [ downloader = , ] + [ username = , ] ) -> output ``` - `url :: AbstractString` @@ -45,6 +46,7 @@ download(url, [ output = tempname() ]; - `verbose :: Bool` - `debug :: (type, message) --> Any` - `downloader :: Downloader` +- `username :: AbstractString` Download a file from the given url, saving it to `output` or if not specified, a temporary path. The `output` can also be an `IO` handle, in which case the body @@ -97,6 +99,7 @@ request(url; [ debug = , ] [ throw = true, ] [ downloader = , ] + [ username = , ] ) -> Union{Response, RequestError} ``` - `url :: AbstractString` @@ -110,6 +113,7 @@ request(url; - `debug :: (type, message) --> Any` - `throw :: Bool` - `downloader :: Downloader` +- `username :: AbstractString` Make a request to the given url, returning a `Response` object capturing the status, headers and other information about the response. The body of the diff --git a/src/Curl/Curl.jl b/src/Curl/Curl.jl index fa76379..4d54a84 100644 --- a/src/Curl/Curl.jl +++ b/src/Curl/Curl.jl @@ -6,6 +6,7 @@ export set_url, set_method, set_verbose, + set_username, set_debug, set_body, set_upload_size, diff --git a/src/Curl/Easy.jl b/src/Curl/Easy.jl index cf63f50..a490251 100644 --- a/src/Curl/Easy.jl +++ b/src/Curl/Easy.jl @@ -135,6 +135,10 @@ function set_verbose(easy::Easy, verbose::Bool) setopt(easy, CURLOPT_VERBOSE, verbose) end +function set_username(easy::Easy, username::AbstractString) + setopt(easy, CURLOPT_USERNAME, username) +end + function set_debug(easy::Easy, debug::Function) hasmethod(debug, Tuple{String,String}) || throw(ArgumentError("debug callback must take (::String, ::String)")) diff --git a/src/Downloads.jl b/src/Downloads.jl index 216aa84..a1f0466 100644 --- a/src/Downloads.jl +++ b/src/Downloads.jl @@ -178,6 +178,7 @@ end [ verbose = false, ] [ debug = , ] [ downloader = , ] + [ username = , ] ) -> output url :: AbstractString @@ -189,6 +190,7 @@ end verbose :: Bool debug :: (type, message) --> Any downloader :: Downloader + username :: AbstractString Download a file from the given url, saving it to `output` or if not specified, a temporary path. The `output` can also be an `IO` handle, in which case the body @@ -253,6 +255,7 @@ function download( verbose :: Bool = false, debug :: Union{Function, Nothing} = nothing, downloader :: Union{Downloader, Nothing} = nothing, + username :: Union{AbstractString, Nothing} = nothing, ) :: ArgWrite arg_write(output) do output response = request( @@ -265,6 +268,7 @@ function download( verbose = verbose, debug = debug, downloader = downloader, + username = username, )::Response status_ok(response) && return output throw(RequestError(url, Curl.CURLE_OK, "", response)) @@ -285,6 +289,7 @@ end [ debug = , ] [ throw = true, ] [ downloader = , ] + [ username = , ] ) -> Union{Response, RequestError} url :: AbstractString @@ -298,6 +303,7 @@ end debug :: (type, message) --> Any throw :: Bool downloader :: Downloader + username :: AbstractString Make a request to the given url, returning a `Response` object capturing the status, headers and other information about the response. The body of the @@ -329,6 +335,7 @@ function request( debug :: Union{Function, Nothing} = nothing, throw :: Bool = true, downloader :: Union{Downloader, Nothing} = nothing, + username :: Union{AbstractString, Nothing} = nothing, ) :: Union{Response, RequestError} if downloader === nothing lock(DOWNLOAD_LOCK) do @@ -359,6 +366,10 @@ function request( set_debug(easy, debug) add_headers(easy, headers) + if !isnothing(username) + set_username(easy, username) + end + # libcurl does not set the default header reliably so set it # explicitly unless user has specified it, xref # https://github.com/JuliaLang/Pkg.jl/pull/2357