uri: Handle HTTP exception raised (#84015)

* Handle HTTP exceptions raised when reading the content
  such as IncompleteRead

Fixes: #83794

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/84592/head
Abhijeet Kasurde 11 months ago committed by GitHub
parent 186c716af1
commit c9097f73a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
---
bugfixes:
- uri - Handle HTTP exceptions raised while reading the content (https://github.com/ansible/ansible/issues/83794).

@ -432,6 +432,7 @@ url:
sample: https://www.ansible.com/
"""
import http
import json
import os
import re
@ -733,6 +734,8 @@ def main():
# there was no content, but the error read()
# may have been stored in the info as 'body'
content = info.pop('body', b'')
except http.client.HTTPException as http_err:
module.fail_json(msg=f"HTTP Error while fetching {url}: {to_native(http_err)}")
elif r:
content = r
else:

@ -6,10 +6,30 @@ import sys
if __name__ == '__main__':
PORT = int(sys.argv[1])
content_type_json = "application/json"
class Handler(http.server.SimpleHTTPRequestHandler):
pass
def do_GET(self):
if self.path == '/chunked':
self.request.sendall(
b'HTTP/1.1 200 OK\r\n'
b'Transfer-Encoding: chunked\r\n'
b'\r\n'
b'a\r\n' # size of the chunk (0xa = 10)
b'123456'
)
elif self.path.endswith('json'):
try:
with open(self.path[1:]) as f:
self.send_response(200)
self.send_header("Content-type", content_type_json)
self.end_headers()
self.wfile.write(bytes(f.read(), "utf-8"))
except IOError:
self.send_error(404)
else:
self.send_error(404)
Handler.extensions_map['.json'] = 'application/json'
Handler.extensions_map['.json'] = content_type_json
httpd = socketserver.TCPServer(("", PORT), Handler)
httpd.serve_forever()

@ -100,6 +100,19 @@
- "{{fail_checksum.results}}"
- "{{fail.results}}"
- name: Request IncompleteRead from localhost
uri:
return_content: yes
url: http://localhost:{{ http_port }}/chunked
register: r
ignore_errors: true
- name: Check if IncompleteRead raises error
assert:
that:
- r.failed
- "'HTTP Error while fetching' in r.msg"
- name: test https fetch to a site with mismatched hostname and certificate
uri:
url: "https://{{ badssl_host }}/"

Loading…
Cancel
Save