Wrong content-length in wsgi_application on Python 3

Bug #1050638 reported by Piotr Szymankiewicz
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
ladon
New
Undecided
Unassigned

Bug Description

At the end of wsgi_application.py

 content_length = str(len(output))

is performed before encoding

 if sys.version_info[0]>=3:
  # Python 3 support
  if type(output)==str:
   output = bytes(output,charset)

When output contains non-ascii characters 'Content-Length' is wrong.

Revision history for this message
Андрей Асеев (mahnopus) wrote :

never late to fix some good oldies
just reorder end lines and move python version checker before response headers

  if sys.version_info[0]>=3:
   # Python 3 support
   if type(output)==str:
    output = bytes(output,charset)
    content_length = str(len(output))

  response_headers += [
   ('Content-Type', "%s; charset=%s" % (content_type,charset)),
   ('Content-Length', content_length)
  ]
  start_response(status, response_headers)

  if hasattr(output,'read'):
   # File-like object
   block_size = 4096
   if 'wsgi.file_wrapper' in environ:
    return environ['wsgi.file_wrapper'](output, block_size)
   else:
    return iter(lambda: output.read(block_size), '')

  return [output]

Revision history for this message
jsgaarde (jakob-simon-gaarde) wrote : Re: [Bug 1050638] Re: Wrong content-length in wsgi_application on Python 3

Hi.

The official site for Ladon is now: https://bitbucket.org/jakobsg/ladon
Please make a branch and a pull request, then I can review your change and
merge it to master.

Br Jakob

On Wed, Aug 23, 2017 at 10:27 PM, Андрей Асеев <email address hidden> wrote:

> never late to fix some good oldies
> just reorder end lines and move python version checker before response
> headers
>
> if sys.version_info[0]>=3:
> # Python 3 support
> if type(output)==str:
> output = bytes(output,charset)
> content_length = str(len(output))
>
> response_headers += [
> ('Content-Type', "%s; charset=%s" %
> (content_type,charset)),
> ('Content-Length', content_length)
> ]
> start_response(status, response_headers)
>
> if hasattr(output,'read'):
> # File-like object
> block_size = 4096
> if 'wsgi.file_wrapper' in environ:
> return environ['wsgi.file_wrapper'](output,
> block_size)
> else:
> return iter(lambda:
> output.read(block_size), '')
>
> return [output]
>
> --
> You received this bug notification because you are subscribed to ladon.
> https://bugs.launchpad.net/bugs/1050638
>
> Title:
> Wrong content-length in wsgi_application on Python 3
>
> Status in ladon:
> New
>
> Bug description:
> At the end of wsgi_application.py
>
> content_length = str(len(output))
>
> is performed before encoding
>
> if sys.version_info[0]>=3:
> # Python 3 support
> if type(output)==str:
> output = bytes(output,charset)
>
> When output contains non-ascii characters 'Content-Length' is wrong.
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/ladon/+bug/1050638/+subscriptions
>

--
Med venlig hilsen / Best regards
Jakob Simon-Gaarde

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.