about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorUltraQbik <no1skill@yandex.ru>2024-08-22 04:45:39 +0300
committerUltraQbik <no1skill@yandex.ru>2024-08-22 04:45:39 +0300
commit9a539001f3f1e77a6ea8332f659695d7ddcedb2f (patch)
treeb26ba03bda05a5584f384e18317d380cffcbf178 /src
parent4f741ce90cd857aba53de06047b9221fa27b0c61 (diff)
downloadhttpy-9a539001f3f1e77a6ea8332f659695d7ddcedb2f.tar.gz
httpy-9a539001f3f1e77a6ea8332f659695d7ddcedb2f.zip
Massive refactoring addition of API
Diffstat (limited to 'src')
-rw-r--r--src/APIv1.py34
-rw-r--r--src/socks.py20
2 files changed, 54 insertions, 0 deletions
diff --git a/src/APIv1.py b/src/APIv1.py
new file mode 100644
index 0000000..e0e4848
--- /dev/null
+++ b/src/APIv1.py
@@ -0,0 +1,34 @@
+import random
+from src.socks import *
+from ssl import SSLSocket
+from src.request import Request
+
+
+async def respond(client: SSLSocket, request: Request) -> tuple[int, bytes]:
+    """
+    Respond to clients API request
+    """
+
+    # decode API request
+    split_path = request.path.split("/")
+    api_level1 = split_path[2]
+    api_level2 = split_path[3]
+    api_request = split_path[4]
+
+    # do something with it (oh god)
+    if api_level1 == "file":
+        if api_level2 == "generated":
+            if api_request == "1gib":
+                return 200, random.randbytes(2**30 * 1)
+            elif api_request == "5gib":
+                return 200, random.randbytes(2**30 * 5)
+            elif api_request == "10gib":
+                return 200, random.randbytes(2**30 * 10)
+            elif api_request == "20gib":
+                return 200, random.randbytes(2**30 * 20)
+            else:
+                return 400, b''
+        else:
+            return 400, b''
+    else:
+        return 400, b''
diff --git a/src/socks.py b/src/socks.py
new file mode 100644
index 0000000..119a62a
--- /dev/null
+++ b/src/socks.py
@@ -0,0 +1,20 @@
+import time
+from ssl import SSLSocket
+
+
+async def ssl_sock_accept(sock: SSLSocket) -> tuple[SSLSocket, str]:
+    while True:
+        try:
+            return sock.accept()
+        except BlockingIOError:
+            time.sleep(1.e-3)
+
+
+async def ssl_sock_recv(sock: SSLSocket, buflen: int = 1024):
+    while (msg := sock.recv(buflen)) == b'':
+        time.sleep(1.e-3)
+    return msg
+
+
+async def ssl_sock_sendall(sock: SSLSocket, data: bytes):
+    sock.sendall(data)