fix(web): handle case-insensitive image media types
This commit is contained in:
1 parent
b4f381ca55
commit
8adab28aee
2 files changed
+58
-4
No files matched your search
@@ -1163,10 +1163,13 @@ class WebFetchTool(Tool):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
ctype = r.headers.get("content-type", "")
|
ctype = r.headers.get("content-type", "")
|
||||||
if ctype.startswith("image/"):
|
# Media types are case-insensitive; parameter values retain their case.
|
||||||
|
media_type, separator, parameters = ctype.partition(";")
|
||||||
|
image_ctype = media_type.strip().lower() + separator + parameters
|
||||||
|
if image_ctype.startswith("image/"):
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
raw = await r.aread()
|
raw = await r.aread()
|
||||||
return build_image_content_blocks(raw, ctype, url, f"(Image fetched from: {url})")
|
return build_image_content_blocks(raw, image_ctype, url, f"(Image fetched from: {url})")
|
||||||
finally:
|
finally:
|
||||||
if stream is not None:
|
if stream is not None:
|
||||||
await stream.__aexit__(None, None, None)
|
await stream.__aexit__(None, None, None)
|
||||||
@@ -1255,8 +1258,11 @@ class WebFetchTool(Tool):
|
|||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
ctype = r.headers.get("content-type", "")
|
ctype = r.headers.get("content-type", "")
|
||||||
if ctype.startswith("image/"):
|
# Media types are case-insensitive; parameter values retain their case.
|
||||||
return build_image_content_blocks(r.content, ctype, url, f"(Image fetched from: {url})")
|
media_type, separator, parameters = ctype.partition(";")
|
||||||
|
image_ctype = media_type.strip().lower() + separator + parameters
|
||||||
|
if image_ctype.startswith("image/"):
|
||||||
|
return build_image_content_blocks(r.content, image_ctype, url, f"(Image fetched from: {url})")
|
||||||
|
|
||||||
if "application/json" in ctype:
|
if "application/json" in ctype:
|
||||||
text, extractor = json.dumps(r.json(), indent=2, ensure_ascii=False), "json"
|
text, extractor = json.dumps(r.json(), indent=2, ensure_ascii=False), "json"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import base64
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
from unittest.mock import AsyncMock, patch
|
from unittest.mock import AsyncMock, patch
|
||||||
@@ -464,6 +465,53 @@ async def test_web_fetch_blocks_private_redirect_before_readability_request(monk
|
|||||||
assert requested == ["https://attacker.example/start"]
|
assert requested == ["https://attacker.example/start"]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize("preflight_fails", [False, True], ids=["stream", "fallback"])
|
||||||
|
@pytest.mark.parametrize("content_type,expected_mime", [
|
||||||
|
("image/png", "image/png"),
|
||||||
|
("Image/PNG", "image/png"),
|
||||||
|
("Image/PNG; profile=MiXeD", "image/png; profile=MiXeD"),
|
||||||
|
])
|
||||||
|
async def test_web_fetch_image_media_type_is_case_insensitive(
|
||||||
|
monkeypatch, preflight_fails, content_type, expected_mime,
|
||||||
|
):
|
||||||
|
tool = WebFetchTool(config=WebFetchConfig(use_jina_reader=False))
|
||||||
|
png = base64.b64decode(
|
||||||
|
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/"
|
||||||
|
"iZk9HQAAAABJRU5ErkJggg=="
|
||||||
|
)
|
||||||
|
requests: list[httpx.Request] = []
|
||||||
|
|
||||||
|
def handler(request: httpx.Request) -> httpx.Response:
|
||||||
|
requests.append(request)
|
||||||
|
if preflight_fails and len(requests) == 1:
|
||||||
|
raise httpx.ConnectError("Image preflight unavailable", request=request)
|
||||||
|
return httpx.Response(
|
||||||
|
200, headers={"Content-Type": content_type}, content=png, request=request,
|
||||||
|
)
|
||||||
|
|
||||||
|
transport = httpx.MockTransport(handler)
|
||||||
|
real_async_client = httpx.AsyncClient
|
||||||
|
|
||||||
|
class TransportAsyncClient(real_async_client):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
kwargs.pop("proxy", None)
|
||||||
|
kwargs.pop("transport", None)
|
||||||
|
super().__init__(*args, transport=transport, **kwargs)
|
||||||
|
|
||||||
|
monkeypatch.setattr("nanobot.agent.tools.web.httpx.AsyncClient", TransportAsyncClient)
|
||||||
|
monkeypatch.setattr(web_module, "_pinned_dns_transport", lambda: object())
|
||||||
|
with patch("nanobot.security.network.socket.getaddrinfo", _fake_resolve_public):
|
||||||
|
result = await tool.execute(url="https://example.com/image.png")
|
||||||
|
|
||||||
|
assert isinstance(result, list), f"Expected native image blocks, requests={len(requests)}"
|
||||||
|
image = next(block for block in result if block["type"] == "image_url")
|
||||||
|
prefix, encoded = image["image_url"]["url"].split(";base64,", 1)
|
||||||
|
assert prefix == f"data:{expected_mime}"
|
||||||
|
assert base64.b64decode(encoded, validate=True) == png
|
||||||
|
assert len(requests) == (2 if preflight_fails else 1)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_web_fetch_blocks_private_redirect_before_returning_image(monkeypatch):
|
async def test_web_fetch_blocks_private_redirect_before_returning_image(monkeypatch):
|
||||||
tool = WebFetchTool(config=WebFetchConfig(use_jina_reader=False))
|
tool = WebFetchTool(config=WebFetchConfig(use_jina_reader=False))
|
||||||
|
|||||||
Reference in new issue
Block a user