diff --git a/changelogs/fragments/win_get_url-Fix-restricted-header-handling.yml b/changelogs/fragments/win_get_url-Fix-restricted-header-handling.yml new file mode 100644 index 00000000000..8343c85ec03 --- /dev/null +++ b/changelogs/fragments/win_get_url-Fix-restricted-header-handling.yml @@ -0,0 +1,2 @@ +bugfixes: + - "win_get_url - Fix handling of restricted headers as per (https://github.com/ansible/ansible/issues/57880)" diff --git a/lib/ansible/modules/windows/win_get_url.ps1 b/lib/ansible/modules/windows/win_get_url.ps1 index 455f0b54884..8f3af457e37 100644 --- a/lib/ansible/modules/windows/win_get_url.ps1 +++ b/lib/ansible/modules/windows/win_get_url.ps1 @@ -81,7 +81,25 @@ Function Invoke-AnsibleWebRequest { $web_request.Method = $Method.($web_request.GetType().Name) foreach ($header in $headers.GetEnumerator()) { - $web_request.Headers.Add($header.Key, $header.Value) + # some headers need to be set on the property itself + switch ($header.Key) { + Accept { $web_request.Accept = $header.Value } + Connection { $web_request.Connection = $header.Value } + Content-Length { $web_request.ContentLength = $header.Value } + Content-Type { $web_request.ContentType = $header.Value } + Expect { $web_request.Expect = $header.Value } + Date { $web_request.Date = $header.Value } + Host { $web_request.Host = $header.Value } + If-Modified-Since { $web_request.IfModifiedSince = $header.Value } + Range { $web_request.AddRange($header.Value) } + Referer { $web_request.Referer = $header.Value } + Transfer-Encoding { + $web_request.SendChunked = $true + $web_request.TransferEncoding = $header.Value + } + User-Agent { $web_request.UserAgent = $header.Value } + default { $web_request.Headers.Add($header.Key, $header.Value) } + } } if ($timeout) { diff --git a/test/integration/targets/win_get_url/tasks/tests_url.yml b/test/integration/targets/win_get_url/tasks/tests_url.yml index 66a9b26ee6c..1fb65627b1f 100644 --- a/test/integration/targets/win_get_url/tasks/tests_url.yml +++ b/test/integration/targets/win_get_url/tasks/tests_url.yml @@ -188,6 +188,8 @@ dest: '{{ testing_dir }}\headers.txt' headers: testing: 123 + User-Agent: 'badAgent' + accept: 'text/html' register: headers - name: get result of send request with headers @@ -201,3 +203,5 @@ - headers is changed - headers.status_code == 200 - (headers_actual.content | b64decode | from_json).headers.Testing == '123' + - (headers_actual.content | b64decode | from_json).headers["User-Agent"] == 'badAgent' + - (headers_actual.content | b64decode | from_json).headers.Accept == 'text/html'