Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3006.x] Fix content-type backwards compatability #66163

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
13 changes: 13 additions & 0 deletions salt/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,19 @@ def query(
agent = f"{agent} http.query()"
header_dict["User-agent"] = agent

if (
proxy_host
and proxy_port
and 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)}"

if backend == "requests":
sess = requests.Session()
sess.auth = auth
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"
Loading