Skip to content

Commit 9b49df8

Browse files
committed
Unified root to root_path across all files
1 parent 9a021bd commit 9b49df8

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

adafruit_httpserver/response.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class HTTPResponse:
2121
content_type: str
2222

2323
filename: Optional[str]
24-
root_directory: str
24+
root_path: str
2525

2626
body: str
2727

@@ -32,7 +32,7 @@ def __init__(
3232
headers: Dict[str, str] = None,
3333
content_type: str = MIMEType.TEXT_PLAIN,
3434
filename: Optional[str] = None,
35-
root_directory: str = "",
35+
root_path: str = "",
3636
http_version: str = "HTTP/1.1"
3737
) -> None:
3838
"""
@@ -46,7 +46,7 @@ def __init__(
4646
self.headers = headers or {}
4747
self.content_type = content_type
4848
self.filename = filename
49-
self.root_directory = root_directory
49+
self.root_path = root_path
5050
self.http_version = http_version
5151

5252
@staticmethod
@@ -82,11 +82,11 @@ def send(self, conn: Union[SocketPool.Socket, socket.socket]) -> None:
8282

8383
if self.filename is not None:
8484
try:
85-
file_length = os.stat(self.root_directory + self.filename)[6]
85+
file_length = os.stat(self.root_path + self.filename)[6]
8686
self._send_file_response(
8787
conn,
8888
filename = self.filename,
89-
root_directory = self.root_directory,
89+
root_path = self.root_path,
9090
file_length = file_length
9191
)
9292
except OSError:
@@ -127,7 +127,7 @@ def _send_file_response(
127127
self,
128128
conn: Union[SocketPool.Socket, socket.socket],
129129
filename: str,
130-
root_directory: str,
130+
root_path: str,
131131
file_length: int
132132
):
133133
self._send_bytes(
@@ -138,7 +138,7 @@ def _send_file_response(
138138
content_length = file_length
139139
),
140140
)
141-
with open(root_directory + filename, "rb") as file:
141+
with open(root_path + filename, "rb") as file:
142142
while bytes_read := file.read(2048):
143143
self._send_bytes(conn, bytes_read)
144144

adafruit_httpserver/server.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ def route_decorator(func: Callable) -> Callable:
5050

5151
return route_decorator
5252

53-
def serve_forever(self, host: str, port: int = 80, root: str = "") -> None:
53+
def serve_forever(self, host: str, port: int = 80, root_path: str = "") -> None:
5454
"""Wait for HTTP requests at the given host and port. Does not return.
5555
5656
:param str host: host name or IP address
5757
:param int port: port
5858
:param str root: root directory to serve files from
5959
"""
60-
self.start(host, port, root)
60+
self.start(host, port, root_path)
6161

6262
while True:
6363
try:
6464
self.poll()
6565
except OSError:
6666
continue
6767

68-
def start(self, host: str, port: int = 80, root: str = "") -> None:
68+
def start(self, host: str, port: int = 80, root_path: str = "") -> None:
6969
"""
7070
Start the HTTP server at the given host and port. Requires calling
7171
poll() in a while loop to handle incoming requests.
@@ -74,7 +74,7 @@ def start(self, host: str, port: int = 80, root: str = "") -> None:
7474
:param int port: port
7575
:param str root: root directory to serve files from
7676
"""
77-
self.root_path = root
77+
self.root_path = root_path
7878

7979
self._sock = self._socket_source.socket(
8080
self._socket_source.AF_INET, self._socket_source.SOCK_STREAM
@@ -105,7 +105,7 @@ def poll(self):
105105

106106
# If no handler exists and request method is GET, try to serve a file.
107107
elif request.method == HTTPMethod.GET:
108-
response = HTTPResponse(filename=request.path, root=self.root_path)
108+
response = HTTPResponse(filename=request.path, root_path=self.root_path)
109109

110110
# If no handler exists and request method is not GET, return 400 Bad Request.
111111
else:

0 commit comments

Comments
 (0)