diff mbox series

[2/2] fetch2/crate: identify as bitbake to crates.io

Message ID 20260428140325.3948844-3-johanam@axis.com
State New
Headers show
Series crate.io data access policy (user agent) | expand

Commit Message

Johan Anderholm April 28, 2026, 2:03 p.m. UTC
In order to adhere to the data access policy of crates.io, a user-agent
header that identifies the application must be used.

Reference: https://github.com/rust-lang/crates.io/issues/13482
Reference: https://crates.io/data-access
Signed-off-by: Johan Anderholm <johanam@axis.com>
---
 lib/bb/fetch2/crate.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/bb/fetch2/crate.py b/lib/bb/fetch2/crate.py
index 2d3078899..5b6c715a3 100644
--- a/lib/bb/fetch2/crate.py
+++ b/lib/bb/fetch2/crate.py
@@ -22,10 +22,25 @@  from   bb.fetch2.wget import Wget
 class Crate(Wget):
 
     """Class to fetch crates via wget"""
+    using_crates_io = False
 
     def _cargo_bitbake_path(self, rootdir):
         return os.path.join(rootdir, "cargo_home", "bitbake")
 
+    def identify_as_bitbake(self):
+        """
+        Should wget identify as bitbake?
+        """
+        # In order to comply with the data access policy of crates.io, a
+        # user-agent with application and contact information should be used:
+        #
+        # https://crates.io/data-access
+        if self.using_crates_io:
+            return True
+        else:
+            # Default to whatever Wget think is proper
+            return super(Crate, self).identify_as_bitbake()
+
     def supports(self, ud, d):
         """
         Check to see if a given url is for this fetcher
@@ -65,10 +80,11 @@  class Crate(Wget):
         # host (this is to allow custom crate registries to be specified
         host = '/'.join(parts[2:-2])
 
-        # if using upstream just fix it up nicely
+        # if using upstream fix it up nicely and make sure user-agent is set
         if host == 'crates.io':
             host = 'crates.io/api/v1/crates'
             cdn_host = 'static.crates.io/crates'
+            self.using_crates_io = True
         else:
             cdn_host = host