@@ -76,7 +76,7 @@ def __init__(
76
76
raise ValueError ("Unparseable raw_request: " , raw_request ) from exc
77
77
78
78
# If ? is found in path, strip form data for matching route
79
- self .form = self .path .split ('?' )[0 ]
79
+ self .form = self .path .split ("?" )[0 ]
80
80
81
81
def __hash__ (self ) -> int :
82
82
return hash (self .method ) ^ hash (self .form )
@@ -86,7 +86,7 @@ def __eq__(self, other: "_HTTPRequest") -> bool:
86
86
return self .method == other .method and self .form == other .form
87
87
88
88
def __repr__ (self ) -> str :
89
- return f"_HTTPRequest(path={ repr (self .path )} , method={ repr (self .method )} , form= { repr ( self . form ) } )"
89
+ return f"_HTTPRequest(path={ repr (self .path )} , method={ repr (self .method )} )"
90
90
91
91
92
92
class MIMEType :
@@ -218,9 +218,9 @@ def __init__(
218
218
The data to return in the response body, if ``filename`` is not ``None``.
219
219
:param str filename: If not ``None``,
220
220
return the contents of the specified file, and ignore ``body``.
221
- :param int value of Cache-Control: max-age=<value> header.
222
- 604800 (7 days) is is default for files served not using route decorator.
223
- 0 if not specified in the route decorator HTTPResponse.
221
+ :param int cache: sets Cache-Control: max-age=<value> header.
222
+ 604800 (7 days) is is default for files served. If using the route decorator,
223
+ then the default is 0 unless specified HTTPResponse.
224
224
:param str root: root directory for filename, without a trailing slash
225
225
"""
226
226
self .status = status
@@ -236,17 +236,15 @@ def send(self, conn: Any) -> None:
236
236
if self .filename :
237
237
try :
238
238
file_length = os .stat (self .root + self .filename )[6 ]
239
- self ._send_file_response (conn , self . filename , self . root , file_length , self . cache )
239
+ self ._send_file_response (conn , file_length )
240
240
except OSError :
241
- self ._send_response (
242
- conn ,
243
- HTTPStatus .NOT_FOUND ,
244
- MIMEType .TEXT_PLAIN ,
245
- f"{ HTTPStatus .NOT_FOUND } { self .filename } \r \n " ,
246
- 0
247
- )
241
+ self .status = HTTPStatus .NOT_FOUND
242
+ self .content_type = MIMEType .TEXT_PLAIN
243
+ self .body = f"{ HTTPStatus .NOT_FOUND } { self .filename } \r \n " .encode ()
244
+ self .cache = 0
245
+ self ._send_response (conn )
248
246
else :
249
- self ._send_response (conn , self . status , self . content_type , self . body , self . cache )
247
+ self ._send_response (conn )
250
248
251
249
def send_chunk_headers (self , conn : Any ):
252
250
"""Send Headers for a chunked response over the given socket."""
@@ -260,24 +258,27 @@ def send_body_chunk(self, conn: Any, chunk: str):
260
258
:param Any conn: Current connection.
261
259
:param str chunk: String data to be sent.
262
260
"""
263
- size = "%X\r \n " .encode () % len (chunk )
261
+ size = "%X\r \n " .encode () % len (chunk )
264
262
self ._send_bytes (conn , size )
265
- self ._send_bytes (conn , chunk .encode () + b' \r \n ' )
263
+ self ._send_bytes (conn , chunk .encode () + b" \r \n " )
266
264
267
- def _send_response (self , conn , status , content_type , body , cache ):
265
+ def _send_response (self , conn ):
268
266
self ._send_bytes (
269
- conn , self ._HEADERS_FORMAT .format (status , content_type , len (body ), cache )
267
+ conn ,
268
+ self ._HEADERS_FORMAT .format (
269
+ self .status , self .content_type , len (self .body ), self .cache
270
+ ),
270
271
)
271
- self ._send_bytes (conn , body )
272
+ self ._send_bytes (conn , self . body )
272
273
273
- def _send_file_response (self , conn , filename , root , file_length , cache ):
274
+ def _send_file_response (self , conn , file_length ):
274
275
self ._send_bytes (
275
276
conn ,
276
277
self ._HEADERS_FORMAT .format (
277
- self .status , MIMEType .mime_type (filename ), file_length , cache
278
+ self .status , MIMEType .mime_type (self . filename ), file_length , self . cache
278
279
),
279
280
)
280
- with open (root + filename , "rb" ) as file :
281
+ with open (self . root + self . filename , "rb" ) as file :
281
282
while bytes_read := file .read (2048 ):
282
283
self ._send_bytes (conn , bytes_read )
283
284
@@ -325,7 +326,7 @@ def route_func(request):
325
326
raw_text = request.raw_request.decode("utf8")
326
327
print("Received a request of length", len(raw_text), "bytes")
327
328
return HTTPResponse(body="hello world")
328
-
329
+
329
330
@server.route(path, method)
330
331
def route_func(request, conn):
331
332
raw_text = request.raw_request.decode("utf8")
@@ -382,7 +383,7 @@ def poll(self, timeout=-1):
382
383
Call this method inside your main event loop to get the server to
383
384
check for new incoming client requests. When a request comes in,
384
385
the application callable will be invoked.
385
-
386
+
386
387
:param int timeout: Optional timeout value in seconds.
387
388
"""
388
389
try :
@@ -408,7 +409,9 @@ def poll(self, timeout=-1):
408
409
if response is None :
409
410
return
410
411
elif request .method == "GET" :
411
- response = HTTPResponse (filename = request .path , cache = 604800 , root = self .root_path )
412
+ response = HTTPResponse (
413
+ filename = request .path , cache = 604800 , root = self .root_path
414
+ )
412
415
else :
413
416
response = HTTPResponse (status = HTTPStatus .INTERNAL_SERVER_ERROR )
414
417
@@ -418,10 +421,10 @@ def poll(self, timeout=-1):
418
421
if ex .errno == EAGAIN :
419
422
# there is no data available right now, try again later.
420
423
return
421
- elif ex .errno == ECONNRESET :
424
+ if ex .errno == ECONNRESET :
422
425
# connection reset by peer, try again later.
423
426
return
424
- elif ex .errno == ETIMEDOUT :
427
+ if ex .errno == ETIMEDOUT :
425
428
# connection timed out
426
429
return
427
430
raise
0 commit comments