Merge ~troyanov/maas:fix-1990014 into maas:master

Proposed by Anton Troyanov
Status: Merged
Approved by: Anton Troyanov
Approved revision: a55ae18a9ea56201cebd772e0a140c0a39ec481b
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~troyanov/maas:fix-1990014
Merge into: maas:master
Diff against target: 13 lines (+1/-1)
1 file modified
src/maasserver/middleware.py (+1/-1)
Reviewer Review Type Date Requested Status
MAAS Lander Approve
Christian Grabowski Approve
Review via email: mp+430049@code.launchpad.net

Commit message

LP:1990014 fix debug middleware

When response jas no `content` attribute, default value will be
returned.

Default value should be of type `bytes`, not `str`, otherwise it will
fail to decode with an exception:

```
AttributeError: 'str' object has no attribute 'decode'.
```

From Django documentation:
  HttpResponse.content
  A bytestring representing the content, encoded from a string if necessary.

To post a comment you must log in.
Revision history for this message
Christian Grabowski (cgrabowski) wrote :

+1

review: Approve
Revision history for this message
Anton Troyanov (troyanov) wrote :

Unfortunately I didn't find a way on how to write a test that would reproduce the failure.

For testing I used steps to reproduce provided by Joao Andre Simioni

1. Add `debug_http: true` to regiond.conf,
2. Restart MAAS, and try to get: http://localhost:5240/MAAS/images-stream/grub-efi-signed/amd64/generic/uefi/20210819.0/grub2-signed.tar.xz
3. You get a 500 Internal Server Error with the body as 'str' object has no attribute 'decode'

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b fix-1990014 lp:~troyanov/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/651/consoleText
COMMIT: f27283a647ac1775c6f881fd3e8298fab9909875

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b fix-1990014 lp:~troyanov/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: a55ae18a9ea56201cebd772e0a140c0a39ec481b

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/maasserver/middleware.py b/src/maasserver/middleware.py
2index 0012557..db5872d 100644
3--- a/src/maasserver/middleware.py
4+++ b/src/maasserver/middleware.py
5@@ -331,7 +331,7 @@ class DebuggingLoggerMiddleware:
6 response = self.get_response(request)
7 if settings.DEBUG_HTTP and logger.isEnabledFor(self.log_level):
8 header = " Response dump ".center(79, "#")
9- content = getattr(response, "content", "{no content}")
10+ content = getattr(response, "content", b"{no content}")
11 try:
12 decoded_content = content.decode("utf-8")
13 except UnicodeDecodeError:

Subscribers

People subscribed via source and target branches