1717import traceback
1818
1919PY3 = sys .version_info >= (3 , 0 )
20+ UNICODE_ENCODING = "utf-8"
2021
2122if PY3 :
2223 from http .client import INTERNAL_SERVER_ERROR
@@ -96,7 +97,7 @@ def do_REQUEST(self):
9697 self .send_response (INTERNAL_SERVER_ERROR )
9798 self .send_header ("Connection" , "close" )
9899 self .end_headers ()
99- self .wfile .write ("CLOUDFLARE_ERROR_500S_BOX" .encode ("utf8" ))
100+ self .wfile .write ("CLOUDFLARE_ERROR_500S_BOX" .encode (UNICODE_ENCODING ))
100101 return
101102
102103 if hasattr (self , "data" ):
@@ -127,7 +128,7 @@ def do_REQUEST(self):
127128
128129 if not any (_ in self .params for _ in ("id" , "query" )):
129130 self .send_response (OK )
130- self .send_header ("Content-type" , "text/html" )
131+ self .send_header ("Content-type" , "text/html; charset=%s" % UNICODE_ENCODING )
131132 self .send_header ("Connection" , "close" )
132133 self .end_headers ()
133134 self .wfile .write (b"<html><p><h3>GET:</h3><a href='/?id=1'>link</a></p><hr><p><h3>POST:</h3><form method='post'>ID: <input type='text' name='id'><input type='submit' value='Submit'></form></p></html>" )
@@ -171,7 +172,7 @@ def do_REQUEST(self):
171172 self .end_headers ()
172173 else :
173174 self .end_headers ()
174- self .wfile .write (output . encode ( "utf8" ) if PY3 else output )
175+ self .wfile .write (quote ( output if isinstance ( output , bytes ) else output . encode ( UNICODE_ENCODING )) )
175176 else :
176177 self .send_response (NOT_FOUND )
177178 self .send_header ("Connection" , "close" )
@@ -190,7 +191,7 @@ def do_POST(self):
190191 length = int (self .headers .get ("Content-length" , 0 ))
191192 if length :
192193 data = self .rfile .read (length )
193- data = unquote_plus (data .decode ("utf8" ))
194+ data = unquote_plus (data .decode (UNICODE_ENCODING ))
194195 self .data = data
195196 self .do_REQUEST ()
196197
0 commit comments