{"action":"created","issue":{"url":"https://api.github.com/repos/benoitc/gunicorn/issues/1353","repository_url":"https://api.github.com/repos/benoitc/gunicorn","labels_url":"https://api.github.com/repos/benoitc/gunicorn/issues/1353/labels{/name}","comments_url":"https://api.github.com/repos/benoitc/gunicorn/issues/1353/comments","events_url":"https://api.github.com/repos/benoitc/gunicorn/issues/1353/events","html_url":"https://github.com/benoitc/gunicorn/issues/1353","id":178284123,"number":1353,"title":"wsgi.py > send_headers: encoding problem.","user":{"login":"rsm-gh","id":11134652,"avatar_url":"https://avatars.githubusercontent.com/u/11134652?v=3","gravatar_id":"","url":"https://api.github.com/users/rsm-gh","html_url":"https://github.com/rsm-gh","followers_url":"https://api.github.com/users/rsm-gh/followers","following_url":"https://api.github.com/users/rsm-gh/following{/other_user}","gists_url":"https://api.github.com/users/rsm-gh/gists{/gist_id}","starred_url":"https://api.github.com/users/rsm-gh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsm-gh/subscriptions","organizations_url":"https://api.github.com/users/rsm-gh/orgs","repos_url":"https://api.github.com/users/rsm-gh/repos","events_url":"https://api.github.com/users/rsm-gh/events{/privacy}","received_events_url":"https://api.github.com/users/rsm-gh/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-21T08:48:25Z","updated_at":"2016-09-21T11:32:29Z","closed_at":null,"body":"\r\nWhile serving a file with `django` and `nginx` I got an encoding error coming from `gunicorn`. The problem is that the `send_headers` method tries to convert the `header_str` in to  `ascii` while sometimes it is necessary to use `utf-8` for serving paths with special characters.\r\n\r\n```\r\n    def send_headers(self):\r\n        if self.headers_sent:\r\n            return\r\n        tosend = self.default_headers()\r\n        tosend.extend([\"%s: %s\\r\\n\" % (k, v) for k, v in self.headers])\r\n\r\n        header_str = \"%s\\r\\n\" % \"\".join(tosend)\r\n        util.write(self.sock, util.to_bytestring(header_str, \"ascii\"))\r\n        self.headers_sent = True\r\n```\r\n\r\nI just changed `ascii` in to `utf-8` to fix my problem, but maybe there should be some setting to change this.\r\n\r\nAnd in case that you be curious of how I was using the `Response` object, I got it from an `HTTPResponse` from `django`:\r\n\r\n```\r\n\r\ndef return_file_nginx(harddrive_path):\r\n\r\n    filename=os.path.basename(harddrive_path)\r\n\r\n    if os.path.exists(harddrive_path):\r\n        \r\n        nginx_path = harddrive_path.split(SERVER_PATHS.main,1)[1]\r\n        \r\n        response = HttpResponse()\r\n        response['Content-Length'] = os.path.getsize(harddrive_path)\r\n        response['Content-Disposition'] = 'attachment; filename=\"{}\"'.format(filename)\r\n        response['X-Accel-Redirect'] = nginx_path\r\n        return response\r\n\r\n    return default_404()\r\n```"},"comment":{"url":"https://api.github.com/repos/benoitc/gunicorn/issues/comments/248584900","html_url":"https://github.com/benoitc/gunicorn/issues/1353#issuecomment-248584900","issue_url":"https://api.github.com/repos/benoitc/gunicorn/issues/1353","id":248584900,"user":{"login":"jamadden","id":1256082,"avatar_url":"https://avatars.githubusercontent.com/u/1256082?v=3","gravatar_id":"","url":"https://api.github.com/users/jamadden","html_url":"https://github.com/jamadden","followers_url":"https://api.github.com/users/jamadden/followers","following_url":"https://api.github.com/users/jamadden/following{/other_user}","gists_url":"https://api.github.com/users/jamadden/gists{/gist_id}","starred_url":"https://api.github.com/users/jamadden/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jamadden/subscriptions","organizations_url":"https://api.github.com/users/jamadden/orgs","repos_url":"https://api.github.com/users/jamadden/repos","events_url":"https://api.github.com/users/jamadden/events{/privacy}","received_events_url":"https://api.github.com/users/jamadden/received_events","type":"User","site_admin":false},"created_at":"2016-09-21T11:32:29Z","updated_at":"2016-09-21T11:32:29Z","body":"This is a bit of a grey area, but one thing is clear: `send_headers` could be using `latin-1`, instead of   `ascii`, but *never* `utf-8`. Latin-1 encoding is specified by all the relevant standards (and ASCII is a subset of latin-1). There was a change from utf-8 to latin-1 in #1102.\r\n\r\nNow, https://github.com/benoitc/gunicorn/commit/5f4ebd2eb2b08783a5fbefe79d09fcb3fc1fbc73 changed latin-1 to ascii. The referenced standards do suggest that headers *should* be in ASCII. gunicorn is being particularly strict here, and it could be argued that it should go back to latin-1 encoding, but that raises other potential interoperability concerns. (Do your applications and clients work successfully with latin-1?)\r\n\r\nThis could be a Python 2/Python 3 thing, since the version isn't specified. If this code is running on Python 2, the bug is in the user code that sets the header, because the header [should already be a bytes/str object, not unicode](https://github.com/gevent/gevent/issues/614#issuecomment-121618408)."}}
{"actor":{"display_login":"jamadden"}}