43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
import os
|
|
from socket import socket
|
|
from typing import Dict
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
class ResponseHandler:
|
|
client: socket
|
|
url: str
|
|
headers: Dict[str, str]
|
|
|
|
def __init__(self, url: str, client: socket):
|
|
self.headers = {}
|
|
self.url = url
|
|
self.client = client
|
|
pass
|
|
|
|
def get_html_filename(self):
|
|
filename = "index.html"
|
|
|
|
parsed = urlparse(self.url)
|
|
if parsed.netloc == "":
|
|
parsed = urlparse("//" + self.url)
|
|
if len(parsed.path) != 0:
|
|
index = parsed.path.rfind("/")
|
|
if index == -1:
|
|
filename = parsed.path
|
|
elif parsed.path[-1] != "/":
|
|
filename = parsed.path[index:]
|
|
|
|
result = os.path.basename(filename).strip()
|
|
return result
|
|
|
|
|
|
class PlainResponseHandler(ResponseHandler):
|
|
def __init__(self, url: str, client: socket):
|
|
super().__init__(url, client)
|
|
|
|
|
|
class ChunkedResponseHandler(ResponseHandler):
|
|
def __init__(self, url: str, client: socket):
|
|
super().__init__(url, client)
|