Opened 12 months ago
Closed 8 months ago
#13701 closed defect (fixed)
UnicodeEncodeError in trac/web/_fcgi.py", line 1212
Reported by: | Owned by: | Jun Omae | |
---|---|---|---|
Priority: | normal | Milestone: | 1.6.1 |
Component: | web frontend | Version: | 1.6 |
Severity: | major | Keywords: | fcgi |
Cc: | Branch: | ||
Release Notes: |
|
||
API Changes: | |||
Internal Changes: |
Description (last modified by )
this error caused 500 error the click the attachment in ticket.
source:/branches/1.6-stable/trac/web/_fcgi.py#L1212
fixed the issue by added 'ignore' in encode method.
2024-01-06 10:19:57,084 Trac[main] ERROR: [127.0.0.1] Internal Server Error: <RequestWithSession "GET '/attachment/ticket/8/2023è\x92\x99æ\x96°å\x86¬ä»¤è\x90¥_代ç\x90\x86ä»·.png'">, referrer 'https://trac.foo.bar/ticket/8' Traceback (most recent call last): File "/u0/app/trac/lib64/python3.11/site-packages/trac/web/main.py", line 609, in dispatch_request dispatcher.dispatch(req) File "/u0/app/trac/lib64/python3.11/site-packages/trac/web/main.py", line 301, in dispatch raise e File "/u0/app/trac/lib64/python3.11/site-packages/trac/web/main.py", line 266, in dispatch req.send(output, content_type or 'text/html') File "/u0/app/trac/lib64/python3.11/site-packages/trac/web/api.py", line 875, in send self._send(content, content_type, status) File "/u0/app/trac/lib64/python3.11/site-packages/trac/web/api.py", line 1011, in _send self.write(content) File "/u0/app/trac/lib64/python3.11/site-packages/trac/web/api.py", line 977, in write self._write(b''.join(buf)) File "/u0/app/trac/lib64/python3.11/site-packages/trac/web/_fcgi.py", line 1212, in write req.stdout.write(_str_join(generator()).encode('iso-8859-1')) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnicodeEncodeError: 'latin-1' codec can't encode characters in position 25-26: ordinal not in range(256)
Attachments (0)
Change History (9)
comment:1 by , 12 months ago
Component: | web frontend/mod_wsgi → web frontend |
---|---|
Description: | modified (diff) |
Keywords: | fcgi added; UnicodeEncodeError removed |
comment:2 by , 12 months ago
follow-up: 5 comment:3 by , 12 months ago
Milestone: | → 1.6.1 |
---|---|
Owner: | set to |
Status: | new → assigned |
Replying to gavin.kou@…:
fixed the issue by added 'ignore' in encode method.
Instead, try the following patch:
-
trac/web/_fcgi.py
diff --git a/trac/web/_fcgi.py b/trac/web/_fcgi.py index 12d04a6b4..10cf92bd4 100644
a b class WSGIServer(Server): 1209 1209 for header in headers: 1210 1210 yield '%s: %s\r\n' % header 1211 1211 yield '\r\n' 1212 req.stdout.write(_str_join(generator()).encode(' iso-8859-1'))1212 req.stdout.write(_str_join(generator()).encode('utf-8')) 1213 1213 1214 1214 req.stdout.write(data) 1215 1215 req.stdout.flush()
comment:4 by , 12 months ago
thanks so much for quick fixing.
that's a attachment with chinese characters, as right as you commented in #comment:2
the trac 1.6 is running with Python 3.11.2 behind Nginx with fastcgi interface.
i have tried Python 3.5 as well , which mentioned in wiki:TracDownload#Trac16StableRelease , this UnicodeEncodeError occured too.
will verify the changing .encode('iso-8859-1', 'ignore') to .encode('utf-8').
keep posted.
comment:5 by , 12 months ago
Replying to Jun Omae:
verified, this patch works!
thanks, so much.
$ cat -n "/u0/app/trac/lib64/python3.11/site-packages/trac/web/_fcgi.py" | grep 1212 -A 3 -B 3 1209 for header in headers: 1210 yield '%s: %s\r\n' % header 1211 yield '\r\n' 1212 #req.stdout.write(_str_join(generator()).encode('iso-8859-1','ignore')) 1213 req.stdout.write(_str_join(generator()).encode('utf-8')) 1214 1215 req.stdout.write(data) $
Replying to gavin.kou@…:
fixed the issue by added 'ignore' in encode method.
Instead, try the following patch:
trac/web/_fcgi.py
diff --git a/trac/web/_fcgi.py b/trac/web/_fcgi.py index 12d04a6b4..10cf92bd4 100644
a b class WSGIServer(Server): 1209 1209 for header in headers: 1210 1210 yield '%s: %s\r\n' % header 1211 1211 yield '\r\n' 1212 req.stdout.write(_str_join(generator()).encode(' iso-8859-1'))1212 req.stdout.write(_str_join(generator()).encode('utf-8')) 1213 1213 1214 1214 req.stdout.write(data) 1215 1215 req.stdout.flush()
comment:6 by , 12 months ago
Okay. Found how to reproduce it.
- Log in with username which has non-ascii characters.
- Visit attachment page, as the result, an exception raised
tracd
The same UnicodeEncodeError
is raised while sending response headers.
Traceback (most recent call last): File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/main.py", line 609, in dispatch_request dispatcher.dispatch(req) File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/main.py", line 301, in dispatch raise e File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/main.py", line 266, in dispatch req.send(output, content_type or 'text/html') File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/api.py", line 875, in send self._send(content, content_type, status) File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/api.py", line 1011, in _send self.write(content) File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/api.py", line 977, in write self._write(b''.join(buf)) File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/wsgi.py", line 259, in _write self.handler.send_header(name, value) File "/usr/lib/python3.11/http/server.py", line 526, in send_header ("%s: %s\r\n" % (keyword, value)).encode('latin-1', 'strict')) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnicodeEncodeError: 'latin-1' codec can't encode characters in position 9-11: ordinal not in range(256)
mod_wsgi
The mod_wsgi raises ValueError
if the response headers have non latin-1 characters.
Traceback (most recent call last): File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/main.py", line 609, in dispatch_request dispatcher.dispatch(req) File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/main.py", line 301, in dispatch raise e File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/main.py", line 266, in dispatch req.send(output, content_type or 'text/html') File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/api.py", line 875, in send self._send(content, content_type, status) File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/api.py", line 1008, in _send self.end_headers(exc_info) File "/venv/trac/1.6/lib/python3.11/site-packages/trac/web/api.py", line 799, in end_headers self._write = self._start_response(self._status, self._outheaders, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: unicode object contains non latin-1 characters
comment:7 by , 9 months ago
Proposed changes in [a7e892a08/jomae.git]. After the changes, only ascii characters for the ETag header are used even if logged in with username contains non-ascii characters.
comment:8 by , 9 months ago
Also, I consider that it might be good to check the status and headers do not contain latin-1 characters in WSGIGateway._start_response
like mod_wsgi.
-
trac/web/wsgi.py
diff --git a/trac/web/wsgi.py b/trac/web/wsgi.py index 36f74d329..ec4702f78 100644
a b 16 16 17 17 from abc import ABCMeta, abstractmethod 18 18 import errno 19 import re 19 20 import sys 20 21 from http.server import HTTPServer, BaseHTTPRequestHandler 21 22 from socketserver import ThreadingMixIn … … class WSGIGateway(object, metaclass=ABCMeta): 131 132 132 133 def _start_response(self, status, headers, exc_info=None): 133 134 """Callback for starting a HTTP response.""" 135 ctrl_re = re.compile(r'[\x00-\x1f\x7f]') 136 137 def check_header(item, label): 138 if not isinstance(item, str): 139 raise TypeError('Expected str instance in %s' % label) 140 try: 141 item.encode('latin-1') 142 except UnicodeEncodeError: 143 raise ValueError('Non latin-1 characters are used in %s' % 144 label) from None 145 if ctrl_re.search(item): 146 raise ValueError('Control characters are used in %s' % label) 147 148 check_header(status, 'status') 149 for name, value in headers: 150 check_header(name, 'headers') 151 check_header(value, 'headers') 152 134 153 if exc_info: 135 154 try: 136 155 if self.headers_sent: # Re-raise original exception
comment:9 by , 8 months ago
Release Notes: | modified (diff) |
---|---|
Resolution: | → fixed |
Status: | assigned → closed |
Fixed in [17786:17787] and merged in [17788].
Unable to reproduce it.
2023蒙新冬令营_代理价.png
to the created ticketPlease provide more information. Also, are you using non-ascii characters in username?
Request and response headers