]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
♻ Instantiate `HTTPException` only when needed, optimization refactor (#5356)
authorTimothée Mazzucotelli <pawamoy@pm.me>
Sat, 10 Jun 2023 12:05:35 +0000 (14:05 +0200)
committerGitHub <noreply@github.com>
Sat, 10 Jun 2023 12:05:35 +0000 (14:05 +0200)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
fastapi/security/http.py

index 8b677299dde420023e28b57109368f4b857ebb24..8fc0aafd9fb1c1642970f71231be593361260268 100644 (file)
@@ -73,11 +73,6 @@ class HTTPBasic(HTTPBase):
             unauthorized_headers = {"WWW-Authenticate": f'Basic realm="{self.realm}"'}
         else:
             unauthorized_headers = {"WWW-Authenticate": "Basic"}
-        invalid_user_credentials_exc = HTTPException(
-            status_code=HTTP_401_UNAUTHORIZED,
-            detail="Invalid authentication credentials",
-            headers=unauthorized_headers,
-        )
         if not authorization or scheme.lower() != "basic":
             if self.auto_error:
                 raise HTTPException(
@@ -87,6 +82,11 @@ class HTTPBasic(HTTPBase):
                 )
             else:
                 return None
+        invalid_user_credentials_exc = HTTPException(
+            status_code=HTTP_401_UNAUTHORIZED,
+            detail="Invalid authentication credentials",
+            headers=unauthorized_headers,
+        )
         try:
             data = b64decode(param).decode("ascii")
         except (ValueError, UnicodeDecodeError, binascii.Error):