Skip to content

Commit

Permalink
Fix content-type backwards compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoz committed Feb 29, 2024
1 parent e9122b1 commit 96bb87d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/66127.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix content type backwards compatablity with http proxy post requests in the http utils module.
11 changes: 11 additions & 0 deletions salt/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ def query(
else:
http_proxy_url = f"http://{proxy_host}:{proxy_port}"

if header_dict is None:
header_dict = {}

if method == "POST" and "Content-Type" not in header_dict:
log.debug(
"Content-Type not provided for POST request, assuming application/x-www-form-urlencoded"
)
header_dict["Content-Type"] = "application/x-www-form-urlencoded"
if "Content-Length" not in header_dict:
header_dict["Content-Length"] = f"{len(data)}"

match = re.match(
r"https?://((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)($|/)",
url,
Expand Down
26 changes: 26 additions & 0 deletions tests/pytests/unit/utils/test_http.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import urllib

import pytest
import requests
from pytestshellutils.utils import ports
Expand Down Expand Up @@ -309,3 +311,27 @@ def test_backends_decode_body_true(httpserver, backend):
)
body = ret.get("body", "")
assert isinstance(body, str)


def test_requests_post_content_type(httpserver):
url = httpserver.url_for("/post-content-type")
data = urllib.parse.urlencode({"payload": "test"})
opts = {
"proxy_host": "127.0.0.1",
"proxy_port": 88,
}
with patch("requests.Session") as mock_session:
sess = MagicMock()
sess.headers = {}
mock_session.return_value = sess
ret = http.query(
url,
method="POST",
data=data,
backend="tornado",
opts=opts,
)
assert "Content-Type" in sess.headers
assert sess.headers["Content-Type"] == "application/x-www-form-urlencoded"
assert "Content-Length" in sess.headers
assert sess.headers["Content-Length"] == "12"

0 comments on commit 96bb87d

Please sign in to comment.