GoogleCloudPlatform / webapp2

webapp2 is a framework for Google App Engine
https://webapp2.readthedocs.org
Other
141 stars 63 forks source link

self.response.headers does not accept unicode string #116

Closed altbdoor closed 8 years ago

altbdoor commented 8 years ago

I am writing Py3 compatible code, so I usually import unicode_literals at the top of each .py file. In one of the handler methods, I am trying to change the response headers to serve Content-Type: application/json.

from __future__ import absolute_import, division, print_function, unicode_literals

# ...

def example_handler(self):
    self.response.headers['Content-Type'] = 'application/json'
    self.response.out.write(json.dumps('hello world'))

I am using the latest GAE SDK for Python (1.9.40 - 2016-07-15), and I encountered the error below.

ERROR    2016-07-29 02:12:28,805 webapp2.py:1528] header names must be str, got 'unicode' (u'Content-Type')
Traceback (most recent call last):
  File "C:\xampp\htdocs\google_appengine\lib\webapp2-2.3\webapp2.py", line 1522, in __call__
    return response(environ, start_response)
  File "C:\xampp\htdocs\google_appengine\lib\webob-1.1.1\webob\response.py", line 939, in __call__
    start_response(self.status, headerlist)
  File "C:\xampp\htdocs\google_appengine\google\appengine\runtime\wsgi.py", line 196, in _StartResponse
    (_GetTypeName(name), name))
InvalidResponseError: header names must be str, got 'unicode' (u'Content-Type')

This seems to be the issue with self.response.headers['Access-Control-Allow-Origin'] = '*' as well. Does webapp2 only accept byte strings for response headers?

theacodes commented 8 years ago

It would appear so, but I'm happy to accept a pull request to allow it to support both.

(We should probably look to how flask/werkzeug handles this)

mymtw commented 8 years ago

@jonparrott I have tried to reproduce this bug on python 3. I wrote similar app and installed webapp2, but without of gae sdk. And I didn't encounter with the same error when I tried to run app. There are were an errors with compatibility between python 2 and 3, but the same bug it wasn't. I resolved compatibility errors in webapp2 and webapp2_extras/local.py and app started. If this trouble exists in gae sdk, so I'm afraid, that I will not can help here

altbdoor commented 8 years ago

@mymtwcom, thank you very much for your investigation. I tried looking into the GAE source code as well, and I found the following on /google/appengine/runtime/wsgi.py, line 194, as described in the stack trace.

      if not isinstance(name, str):
        raise InvalidResponseError('header names must be str, got %r (%r)' %
                                   (_GetTypeName(name), name))

I would assume that webapp2 and webob are both returning proper, valid responses, but only GAE is complaining about the data type.

I searched in GAE's issues page, only to find a similar ticket opened since 2012.

Sorry for all the trouble @mymtwcom and @jonparrott, for mistaking this as a webapp2 issue. I will take this to GAE's issue tracker.

mymtw commented 8 years ago

@altbdoor thx for your response too, I'm afraid, that they will not fix this bug soon(near future) :))) Off-topic @jonparrott Does webapp2 currently needs in PR for versions compatibility of python 2-3, even if it will not fix current issue(bug)?? (cuz the trouble was in GAE)

theacodes commented 8 years ago

I can prod on accepting unicode headers internally, but I think it's unlikely to see that change. As such, I'm going to close this issue.

Does webapp2 currently needs in PR for versions compatibility of python 2-3, even if it will not fix current issue(bug)?? (cuz the trouble was in GAE)

Absolutely, webapp2 doesn't just run on App Engine. I'm happy to review and accept any contributions to support Python 3.