diff mbox series

[v2,1/5] fetch2/wget: enable classes derived from Wget to set custom http headers

Message ID 20250227090853.1632280-2-l.goehrs@pengutronix.de
State New
Headers show
Series fetch2/github_release_artifact: fetcher for (private) release artifacts | expand

Commit Message

Leonard Göhrs Feb. 27, 2025, 9:08 a.m. UTC
This allows derived fetchers to e.g. set a `Authorization: Bearer ...`
header for token based authentication, or `Accept:` headers to restrict
which file type the fetcher expects.

Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
---
 lib/bb/fetch2/__init__.py | 1 +
 lib/bb/fetch2/wget.py     | 8 ++++++++
 2 files changed, 9 insertions(+)

Comments

Richard Purdie March 6, 2025, 11:19 a.m. UTC | #1
On Thu, 2025-02-27 at 10:08 +0100, Leonard Göhrs via lists.openembedded.org wrote:
> This allows derived fetchers to e.g. set a `Authorization: Bearer ...`
> header for token based authentication, or `Accept:` headers to restrict
> which file type the fetcher expects.
> 
> Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
> ---
>  lib/bb/fetch2/__init__.py | 1 +
>  lib/bb/fetch2/wget.py     | 8 ++++++++
>  2 files changed, 9 insertions(+)
> 
> diff --git a/lib/bb/fetch2/__init__.py b/lib/bb/fetch2/__init__.py
> index 93fe012ec..06d4fd011 100644
> --- a/lib/bb/fetch2/__init__.py
> +++ b/lib/bb/fetch2/__init__.py
> @@ -1272,6 +1272,7 @@ class FetchData(object):
>          self.basename = None
>          self.basepath = None
>          (self.type, self.host, self.path, self.user, self.pswd, self.parm) = decodeurl(d.expand(url))
> +        self.headers = None
>          self.date = self.getSRCDate(d)
>          self.url = url
>          if not self.user and "user" in self.parm:

I think I'd prefer to call this http_headers and not set a default as
it is specific to the wget fetcher only.

> diff --git a/lib/bb/fetch2/wget.py b/lib/bb/fetch2/wget.py
> index 6cb728ab4..fdf1f2860 100644
> --- a/lib/bb/fetch2/wget.py
> +++ b/lib/bb/fetch2/wget.py
> @@ -121,6 +121,10 @@ class Wget(FetchMethod):
>                  # Authorization header.
>                  fetchcmd += " --user=%s --password=%s" % (ud.user, ud.pswd)
>  
> +        if ud.headers:

If use hasattr() here you don't need the default above.

> +            for key, value in ud.headers.items():
> +                fetchcmd += " --header='%s: %s'" % (key, value)
> +
>          uri = ud.url.split(";")[0]

Cheers,

Richard
diff mbox series

Patch

diff --git a/lib/bb/fetch2/__init__.py b/lib/bb/fetch2/__init__.py
index 93fe012ec..06d4fd011 100644
--- a/lib/bb/fetch2/__init__.py
+++ b/lib/bb/fetch2/__init__.py
@@ -1272,6 +1272,7 @@  class FetchData(object):
         self.basename = None
         self.basepath = None
         (self.type, self.host, self.path, self.user, self.pswd, self.parm) = decodeurl(d.expand(url))
+        self.headers = None
         self.date = self.getSRCDate(d)
         self.url = url
         if not self.user and "user" in self.parm:
diff --git a/lib/bb/fetch2/wget.py b/lib/bb/fetch2/wget.py
index 6cb728ab4..fdf1f2860 100644
--- a/lib/bb/fetch2/wget.py
+++ b/lib/bb/fetch2/wget.py
@@ -121,6 +121,10 @@  class Wget(FetchMethod):
                 # Authorization header.
                 fetchcmd += " --user=%s --password=%s" % (ud.user, ud.pswd)
 
+        if ud.headers:
+            for key, value in ud.headers.items():
+                fetchcmd += " --header='%s: %s'" % (key, value)
+
         uri = ud.url.split(";")[0]
         if os.path.exists(ud.localpath):
             # file exists, but we didnt complete it.. trying again..
@@ -394,6 +398,10 @@  class Wget(FetchMethod):
                 if ud.user and ud.pswd:
                     add_basic_auth(ud.user + ':' + ud.pswd, r)
 
+                if ud.headers:
+                    for key, value in ud.headers.items():
+                        r.add_header(key, value)
+
                 try:
                     import netrc
                     auth_data = netrc.netrc().authenticators(urllib.parse.urlparse(uri).hostname)