-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
111 lines (87 loc) · 2.76 KB
/
server.py
File metadata and controls
111 lines (87 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
import socket
import os
HOST = "0.0.0.0"
PORT = 40000
PACKAGE_ROOT = "./packages"
def http_response(body=b"", status="200 OK", content_type="text/plain"):
hdr = (
f"HTTP/1.0 {status}\r\n"
f"Content-Length: {len(body)}\r\n"
f"Content-Type: text/plain\r\n"
f"Connection: close\r\n"
"\r\n"
)
return hdr.encode("ascii") + body
def read_request(conn):
buf = b""
while b"\r\n\r\n" not in buf and len(buf) < 4096:
chunk = conn.recv(512)
if not chunk:
break
buf += chunk
return buf.decode("ascii", errors="ignore")
def list_packages():
if not os.path.isdir(PACKAGE_ROOT):
return []
return sorted(
d for d in os.listdir(PACKAGE_ROOT)
if os.path.isdir(os.path.join(PACKAGE_ROOT, d))
)
def serve_file(path):
if not os.path.isfile(path):
return None
with open(path, "rb") as f:
return f.read()
def handle_client(conn):
req = read_request(conn)
if not req:
return
first = req.splitlines()[0]
print("REQ:", first)
parts = first.split()
if len(parts) < 2:
conn.sendall(http_response(status="400 Bad Request"))
return
method, path = parts[0], parts[1]
if method != "GET":
conn.sendall(http_response(b"GET only\n", "405 Method Not Allowed"))
return
if path == "/ping":
conn.sendall(http_response(b"pong\n"))
return
if path.startswith("/packages/"):
fs_path = os.path.normpath(os.path.join(PACKAGE_ROOT, path[len("/packages/"):]))
root = os.path.abspath(PACKAGE_ROOT)
target = os.path.abspath(fs_path)
if not target.startswith(root):
conn.sendall(http_response(b"Forbidden\n", "403 Forbidden"))
return
data = serve_file(target)
if data is None:
conn.sendall(http_response(b"Not Found\n", "404 Not Found"))
return
content_type = "application/json" if target.endswith(".json") else "text/plain"
conn.sendall(http_response(data, content_type=content_type))
return
if path == "/packages":
body = "\n".join(list_packages()).encode()
conn.sendall(http_response(body))
return
conn.sendall(http_response(b"Not Found\n", "404 Not Found"))
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(5)
print(f"UwU package repo listening on {PORT}")
while True:
try:
conn, addr = s.accept()
handle_client(conn)
conn.close()
except KeyboardInterrupt:
print("\nbye")
break
if __name__ == "__main__":
main()