From 539fc8b7608c8f3046a6fd1fa247e4e96a0053ff Mon Sep 17 00:00:00 2001 From: Hiroshi Fujita Date: Wed, 30 Oct 2024 03:11:18 +0900 Subject: [PATCH] Fix content-type header case sensitivity (#9961) --- .../workflow/nodes/http_request/entities.py | 4 +-- api/core/workflow/nodes/http_request/node.py | 3 +- .../workflow/nodes/test_http.py | 34 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/api/core/workflow/nodes/http_request/entities.py b/api/core/workflow/nodes/http_request/entities.py index dec76a277e..36ded104c1 100644 --- a/api/core/workflow/nodes/http_request/entities.py +++ b/api/core/workflow/nodes/http_request/entities.py @@ -94,7 +94,7 @@ class Response: @property def is_file(self): content_type = self.content_type - content_disposition = self.response.headers.get("Content-Disposition", "") + content_disposition = self.response.headers.get("content-disposition", "") return "attachment" in content_disposition or ( not any(non_file in content_type for non_file in NON_FILE_CONTENT_TYPES) @@ -103,7 +103,7 @@ class Response: @property def content_type(self) -> str: - return self.headers.get("Content-Type", "") + return self.headers.get("content-type", "") @property def text(self) -> str: diff --git a/api/core/workflow/nodes/http_request/node.py b/api/core/workflow/nodes/http_request/node.py index 483d0e2b7e..a037bee665 100644 --- a/api/core/workflow/nodes/http_request/node.py +++ b/api/core/workflow/nodes/http_request/node.py @@ -142,10 +142,11 @@ class HttpRequestNode(BaseNode[HttpRequestNodeData]): Extract files from response """ files = [] + is_file = response.is_file content_type = response.content_type content = response.content - if content_type: + if is_file and content_type: # extract filename from url filename = path.basename(url) # extract extension if possible diff --git a/api/tests/integration_tests/workflow/nodes/test_http.py b/api/tests/integration_tests/workflow/nodes/test_http.py index 9eea63f722..0da6622658 100644 --- a/api/tests/integration_tests/workflow/nodes/test_http.py +++ b/api/tests/integration_tests/workflow/nodes/test_http.py @@ -430,3 +430,37 @@ def test_multi_colons_parse(setup_http_mock): assert urlencode({"Redirect": "http://example2.com"}) in result.process_data.get("request", "") assert 'form-data; name="Redirect"\r\n\r\nhttp://example6.com' in result.process_data.get("request", "") # assert "http://example3.com" == resp.get("headers", {}).get("referer") + + +def test_image_file(monkeypatch): + from types import SimpleNamespace + + monkeypatch.setattr( + "core.tools.tool_file_manager.ToolFileManager.create_file_by_raw", + lambda *args, **kwargs: SimpleNamespace(id="1"), + ) + + node = init_http_node( + config={ + "id": "1", + "data": { + "title": "http", + "desc": "", + "method": "get", + "url": "https://cloud.dify.ai/logo/logo-site.png", + "authorization": { + "type": "no-auth", + "config": None, + }, + "params": "", + "headers": "", + "body": None, + }, + } + ) + + result = node._run() + assert result.process_data is not None + assert result.outputs is not None + resp = result.outputs + assert len(resp.get("files", [])) == 1