20
20
21
21
from .mime_type import MIMEType
22
22
from .status import HTTPStatus , CommonHTTPStatus
23
+ from .headers import HTTPHeaders
23
24
24
25
25
26
class HTTPResponse :
26
27
"""Details of an HTTP response. Use in `HTTPServer.route` decorator functions."""
27
28
28
29
http_version : str
29
30
status : HTTPStatus
30
- headers : Dict [ str , str ]
31
+ headers : HTTPHeaders
31
32
content_type : str
32
33
33
34
filename : Optional [str ]
@@ -39,7 +40,7 @@ def __init__( # pylint: disable=too-many-arguments
39
40
self ,
40
41
status : Union [HTTPStatus , Tuple [int , str ]] = CommonHTTPStatus .OK_200 ,
41
42
body : str = "" ,
42
- headers : Dict [str , str ] = None ,
43
+ headers : Union [ HTTPHeaders , Dict [str , str ] ] = None ,
43
44
content_type : str = MIMEType .TYPE_TXT ,
44
45
filename : Optional [str ] = None ,
45
46
root_path : str = "" ,
@@ -52,7 +53,7 @@ def __init__( # pylint: disable=too-many-arguments
52
53
"""
53
54
self .status = status if isinstance (status , HTTPStatus ) else HTTPStatus (* status )
54
55
self .body = body
55
- self .headers = headers or {}
56
+ self .headers = headers . copy () if isinstance ( headers , HTTPHeaders ) else HTTPHeaders ( headers )
56
57
self .content_type = content_type
57
58
self .filename = filename
58
59
self .root_path = root_path
@@ -64,21 +65,18 @@ def _construct_response_bytes( # pylint: disable=too-many-arguments
64
65
status : HTTPStatus = CommonHTTPStatus .OK_200 ,
65
66
content_type : str = MIMEType .TYPE_TXT ,
66
67
content_length : Union [int , None ] = None ,
67
- headers : Dict [ str , str ] = None ,
68
+ headers : HTTPHeaders = None ,
68
69
body : str = "" ,
69
70
) -> bytes :
70
71
"""Constructs the response bytes from the given parameters."""
71
72
72
73
response = f"{ http_version } { status .code } { status .text } \r \n "
73
74
74
- # Make a copy of the headers so that we don't modify the incoming dict
75
- response_headers = {} if headers is None else headers .copy ()
75
+ headers .setdefault ("Content-Type" , content_type )
76
+ headers .setdefault ("Content-Length" , content_length or len (body ))
77
+ headers .setdefault ("Connection" , "close" )
76
78
77
- response_headers .setdefault ("Content-Type" , content_type )
78
- response_headers .setdefault ("Content-Length" , content_length or len (body ))
79
- response_headers .setdefault ("Connection" , "close" )
80
-
81
- for header , value in response_headers .items ():
79
+ for header , value in headers .items ():
82
80
response += f"{ header } : { value } \r \n "
83
81
84
82
response += f"\r \n { body } "
@@ -122,7 +120,7 @@ def _send_response( # pylint: disable=too-many-arguments
122
120
status : HTTPStatus ,
123
121
content_type : str ,
124
122
body : str ,
125
- headers : Dict [ str , str ] = None ,
123
+ headers : HTTPHeaders = None ,
126
124
):
127
125
self ._send_bytes (
128
126
conn ,
@@ -140,7 +138,7 @@ def _send_file_response( # pylint: disable=too-many-arguments
140
138
filename : str ,
141
139
root_path : str ,
142
140
file_length : int ,
143
- headers : Dict [ str , str ] = None ,
141
+ headers : HTTPHeaders = None ,
144
142
):
145
143
self ._send_bytes (
146
144
conn ,
0 commit comments