17
17
import traceback
18
18
19
19
PY3 = sys .version_info >= (3 , 0 )
20
+ UNICODE_ENCODING = "utf-8"
20
21
21
22
if PY3 :
22
23
from http .client import INTERNAL_SERVER_ERROR
@@ -96,7 +97,7 @@ def do_REQUEST(self):
96
97
self .send_response (INTERNAL_SERVER_ERROR )
97
98
self .send_header ("Connection" , "close" )
98
99
self .end_headers ()
99
- self .wfile .write ("CLOUDFLARE_ERROR_500S_BOX" .encode ("utf8" ))
100
+ self .wfile .write ("CLOUDFLARE_ERROR_500S_BOX" .encode (UNICODE_ENCODING ))
100
101
return
101
102
102
103
if hasattr (self , "data" ):
@@ -127,7 +128,7 @@ def do_REQUEST(self):
127
128
128
129
if not any (_ in self .params for _ in ("id" , "query" )):
129
130
self .send_response (OK )
130
- self .send_header ("Content-type" , "text/html" )
131
+ self .send_header ("Content-type" , "text/html; charset=%s" % UNICODE_ENCODING )
131
132
self .send_header ("Connection" , "close" )
132
133
self .end_headers ()
133
134
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):
171
172
self .end_headers ()
172
173
else :
173
174
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 )) )
175
176
else :
176
177
self .send_response (NOT_FOUND )
177
178
self .send_header ("Connection" , "close" )
@@ -190,7 +191,7 @@ def do_POST(self):
190
191
length = int (self .headers .get ("Content-length" , 0 ))
191
192
if length :
192
193
data = self .rfile .read (length )
193
- data = unquote_plus (data .decode ("utf8" ))
194
+ data = unquote_plus (data .decode (UNICODE_ENCODING ))
194
195
self .data = data
195
196
self .do_REQUEST ()
196
197
0 commit comments