@@ -1425,79 +1425,36 @@ def validate_folder_data(data: dict[str, Any], url: str) -> TypeGuard[FolderData
14251425 return True
14261426
14271427
1428- def _validate_content_type (content_type : str , url : str ) -> None :
1429- """
1430- Validate that the Content-Type is allowed.
1431- """
1432- is_valid = False
1433- for allowed in ("application/json" , "text/json" , "text/plain" ):
1434- if allowed in content_type :
1435- is_valid = True
1436- break
1437- if not is_valid :
1438- allowed_types = ["application/json" , "text/json" , "text/plain" ]
1439- raise ValueError (
1440- f"Invalid Content-Type from { sanitize_for_log (url )} : { sanitize_for_log (content_type )} . "
1441- f"Expected one of: { ', ' .join (allowed_types )} "
1442- )
1443-
1444-
1445- def _check_content_length (cl : str | None , url : str ) -> None :
1446- """
1447- Validate Content-Length header if present.
1448- """
1449- if not cl :
1450- return
1451- try :
1452- if int (cl ) > MAX_RESPONSE_SIZE :
1453- raise ValueError (
1454- f"Response too large from { sanitize_for_log (url )} "
1455- f"({ int (cl ) / (1024 * 1024 ):.2f} MB)"
1456- )
1457- except ValueError as e :
1458- if "Response too large" in str (e ):
1459- raise
1460- log .warning (
1461- f"Malformed Content-Length header from { sanitize_for_log (url )} : { sanitize_for_log (cl )} . "
1462- "Falling back to streaming size check."
1463- )
1464-
1465-
1466- def _stream_response_chunks (r : httpx .Response , url : str ) -> bytes :
1467- """
1468- Stream and check actual size.
1469- """
1470- chunks = []
1471- current_size = 0
1472- for chunk in r .iter_bytes (chunk_size = 16 * 1024 ):
1473- current_size += len (chunk )
1474- if current_size > MAX_RESPONSE_SIZE :
1475- raise ValueError (
1476- f"Response too large from { sanitize_for_log (url )} "
1477- f"(> { MAX_RESPONSE_SIZE / (1024 * 1024 ):.2f} MB)"
1478- )
1479- chunks .append (chunk )
1480- return b"" .join (chunks )
1481-
1482-
1483- def _read_and_validate_response_body (r : httpx .Response , url : str ) -> bytes :
1484- """
1485- Read the response body while enforcing size limits.
1486- """
1487- _check_content_length (r .headers .get ("Content-Length" ), url )
1488- return _stream_response_chunks (r , url )
1489-
1490-
14911428# _api_stats_lock, _api_get, _api_delete, _api_post, _api_post_form,
14921429# retry_with_jitter, _retry_request imported from api_client above
14931430def _parse_and_cache_response (url : str , r : httpx .Response ) -> dict :
1494- """
1495- Validate, stream, parse, and cache a blocklist response.
1496- """
1497- content_type = r .headers .get ("Content-Type" , "" ).lower ()
1498- _validate_content_type (content_type , url )
1499-
1500- body_bytes = _read_and_validate_response_body (r , url )
1431+ """Validate, stream, parse, and cache a blocklist response."""
1432+ def _validate_ct () -> None :
1433+ ct = r .headers .get ("Content-Type" , "" ).lower ()
1434+ if not any (t in ct for t in ("application/json" , "text/json" , "text/plain" )):
1435+ raise ValueError (f"Invalid Content-Type from { sanitize_for_log (url )} : { sanitize_for_log (ct )} ." )
1436+
1437+ def _read_body () -> bytes :
1438+ cl = r .headers .get ("Content-Length" )
1439+ if cl :
1440+ try :
1441+ if int (cl ) > MAX_RESPONSE_SIZE :
1442+ raise ValueError (f"Response too large from { sanitize_for_log (url )} ({ int (cl ) / (1024 * 1024 ):.2f} MB)" )
1443+ except ValueError as e :
1444+ if "Response too large" in str (e ):
1445+ raise
1446+ log .warning (f"Malformed Content-Length header from { sanitize_for_log (url )} : { sanitize_for_log (cl )} . Falling back to streaming check." )
1447+ chunks = []
1448+ current_size = 0
1449+ for chunk in r .iter_bytes (chunk_size = 16 * 1024 ):
1450+ current_size += len (chunk )
1451+ if current_size > MAX_RESPONSE_SIZE :
1452+ raise ValueError (f"Response too large from { sanitize_for_log (url )} (> { MAX_RESPONSE_SIZE / (1024 * 1024 ):.2f} MB)" )
1453+ chunks .append (chunk )
1454+ return b"" .join (chunks )
1455+
1456+ _validate_ct ()
1457+ body_bytes = _read_body ()
15011458
15021459 try :
15031460 data = json .loads (body_bytes )
0 commit comments