1616
1717STATUS = {k : v [0 ] for k , v in BaseHTTPRequestHandler .responses .items ()}
1818CRLF = "\r \n "
19+ ASCII = "ascii"
1920
2021
2122class Protocol :
@@ -25,7 +26,7 @@ def __init__(self):
2526 self .headers = {}
2627
2728 def on_header (self , name : bytes , value : bytes ):
28- self .headers [name .decode ("ascii" )] = value .decode ("ascii" )
29+ self .headers [name .decode (ASCII )] = value .decode (ASCII )
2930
3031 def on_body (self , body : bytes ):
3132 try :
@@ -34,7 +35,7 @@ def on_body(self, body: bytes):
3435 self .body = body
3536
3637 def on_url (self , url : bytes ):
37- self .url = url .decode ("ascii" )
38+ self .url = url .decode (ASCII )
3839
3940
4041class Request :
@@ -51,7 +52,7 @@ def add_data(self, data):
5152
5253 @property
5354 def method (self ):
54- return self ._parser .get_method ().decode ("ascii" )
55+ return self ._parser .get_method ().decode (ASCII )
5556
5657 @property
5758 def path (self ):
@@ -110,7 +111,7 @@ def get_protocol_data(self, str_format_fun_name="capitalize"):
110111 for k , v in self .headers .items ()
111112 )
112113 )
113- return "{0}\r \n {1}\r \n \r \n " .format (status_line , header_lines ).encode ("utf-8" )
114+ return "{0}\r \n {1}\r \n \r \n " .format (status_line , header_lines ).encode (ENCODING )
114115
115116 def set_base_headers (self ):
116117 self .headers = {
@@ -121,7 +122,7 @@ def set_base_headers(self):
121122 "Content-Length" : str (len (self .body )),
122123 }
123124 if not self .is_file_object :
124- self .headers ["Content-Type" ] = "text/plain; charset=utf-8 "
125+ self .headers ["Content-Type" ] = f "text/plain; charset={ ENCODING } "
125126 elif self .magic :
126127 self .headers ["Content-Type" ] = do_the_magic (self .magic , self .body )
127128
@@ -237,7 +238,6 @@ def _parse_requestline(line):
237238
238239 @classmethod
239240 def register (cls , method , uri , * responses , ** config ):
240-
241241 if "body" in config or "status" in config :
242242 raise AttributeError ("Did you mean `Entry.single_register(...)`?" )
243243
@@ -263,7 +263,6 @@ def single_register(
263263 match_querystring = True ,
264264 exception = None ,
265265 ):
266-
267266 response = (
268267 exception
269268 if exception
0 commit comments