Skip to content

Commit

Permalink
Work on #179 and additional clean-up of an XML view.
Browse files Browse the repository at this point in the history
  • Loading branch information
amcgregor committed Feb 24, 2021
1 parent 28812a6 commit 2840daf
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions web/ext/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ def render_response(self, context:Context, result:Response) -> bool:
assert check_argument_types()
if __debug__: self._log.trace(f"Replacing context.response object with: {result!r}", extra=context.extra)

# We migrate across certain response headers the developer may have assigned "too early".
for header, value in context.response.headers.items():
if header.startswith('Access-') or \
header.startswith('Cross-') or \
header.startswith('Content-') or \
header.startswith('X-') or \
'Origin' in header or \
header in ('Allow', 'Server', 'Strict-Transport-Security', 'Upgrade-Insecure-Requests'):
result.headers[header] = value

context.response = result

return True
Expand Down Expand Up @@ -247,8 +257,8 @@ def render_text(self, context:Context, result:str) -> bool:
response: Response = context.response
response.text = result

if not resp.content_type:
resp.content_type = 'text/html' if HTML_LIKE.search(result) else 'text/plain'
if not response.content_type:
response.content_type = 'text/html' if HTML_LIKE.search(result) else 'text/plain'

return True

Expand Down Expand Up @@ -374,10 +384,12 @@ def render_minidom(self, context:Context, result:minidom.Document) -> bool:
assert check_argument_types()
if __debug__: self._log.trace(f"Applying a MiniDOM object: {result!r}", extra=context.extra)

if not context.response.content_type:
context.response.content_type = 'text/xml' if __debug__ else 'application/xml'
cs: str = context.response.charset = 'utf-8'
response: Response = context.response # Local alias.

if not response.content_type:
response.content_type = 'text/xml' if __debug__ else 'application/xml'
response.charset = 'utf-8'

context.response.body = (result.toprettyxml if cs.startswith('text/') else result.toxml)(encoding=cs)
response.body = (result.toprettyxml if cs.startswith('text/') else result.toxml)(encoding=response.charset)

return True

0 comments on commit 2840daf

Please sign in to comment.