Compare commits

...

8 Commits

5 changed files with 47 additions and 12 deletions

View File

@ -8,11 +8,11 @@ class Headers:
def has(self, key: str) -> bool:
return key in self.headers.keys()
def get(self, key: str) -> str | None:
def get(self, key: str) -> str:
if self.has(key):
return self.headers[key]
return None
return ''
def add(self, key, value) -> None:
self.headers[key] = value

View File

@ -47,6 +47,20 @@ def uwuify_text(text):
return '. '.join(uwuified_sentences)
def apply_url_params(body, params: str):
body = body.decode('utf-8')
soup = BeautifulSoup(body, 'html.parser')
for a_tag in soup.find_all('a', href=True):
original_href = a_tag['href']
if '?' in original_href:
new_href = f"{original_href}&{params}"
else:
new_href = f"{original_href}?{params}"
a_tag['href'] = new_href
return str(soup)
def uwuify(body):
body = body.decode('utf-8')
soup = BeautifulSoup(body, 'html.parser')
@ -90,6 +104,17 @@ patchers: List[Patcher] = [
lambda response, request: Response(
response.code,
response.headers,
re.sub(r'sludge', lambda match: 'sludge' + ' (/&#x73;&#x6c;&#x28c;&#x64;&#x361;&#x292;/)' if random.randint(0, 5) < 1 else 'sludge', response.body.decode('utf-8')).encode('utf-8')
) if 'text/html' in response.headers.values() else response
re.sub(r'sludge', lambda match: 'sludge' + ' (/&#x73;&#x6c;&#x28c;&#x64;&#x361;&#x292;/)' if random.randint(0, 5) < 1 else 'sludge', response.body.decode()).encode('utf-8')
) if 'text/html' in response.headers.values() else response,
lambda response, request: Response(
response.code,
response.headers,
apply_url_params(response.body.replace(b'<head>', b'<head><style>:root,body,body>main>section{animation:swing 180s infinite ease-in-out;transform-origin:center}@keyframes swing{0%{transform:rotate(0deg)}50%{transform:rotate(-1deg)}100%{transform:rotate(1deg)}}</style>'), 'swing=true').encode('utf-8')
) if 'text/html' in response.headers.values() and (random.randint(0, 100) < 1 or is_subdict({'swing': 'true'}, request.path.params)) else response,
# spiin!
lambda response, request: Response(
response.code,
response.headers,
apply_url_params(response.body.replace(b'<head>', b'<head><style>:root,body,body>main>section,body>main>section>flex-grid>flex-grid-item{animation:spiin 480s infinite ease-in-out;transform-origin:center}@keyframes spiin{0%{transform:rotate(0deg)}50%{transform:rotate(180)}100%{transform:rotate(360deg)}}</style>'), 'spiin=true').encode('utf-8')
) if 'text/html' in response.headers.values() and (random.randint(0, 1000) < 1 or is_subdict({'spiin': 'true'}, request.path.params)) else response
]

View File

@ -44,7 +44,9 @@ class Request:
body_start = request_str.find('\r\n\r\n') + 4
body = Body(request_bytes[body_start:], headers.get('Content-Type') or 'text/plain')
log.info(f'received request for {path.path} from {headers.get('X-Real-IP')}')
if not 'Nim httpclient' in headers.get('user-agent'):
log.info(f'received request for {path.path} from {headers.get('X-Real-IP')}')
return cls(method, path, version, headers, body)
def match(self):

View File

@ -30,7 +30,7 @@ class Route:
return response
except Exception as e:
log.error(traceback.format_exc)
log.error(traceback.format_exc())
return error_page(500)
def matches(self, request: 'Request') -> bool:
@ -52,7 +52,7 @@ routes = [
lambda request, *_: Response(
ResponseCode.OK,
{'Content-Type': 'text/html'},
(parse_file('./home.html', dict(prev='\\/')).encode('utf-8') if request.method == Method.GET else (
((parse_file('./home.html', dict(prev='\\/')).encode('utf-8') if not 'Nim httpclient' in request.headers.get('user-agent') else error_page(200).body) if request.method == Method.GET else (
[
(lambda form_data: (
(lambda time: (
@ -83,20 +83,28 @@ routes = [
Route(
lambda path: os.path.isfile('.' + path.path) and path.path.startswith('/html/') and (path.path.endswith('.html') or '/thoughts/' in path.path),
[Method.GET],
lambda request, *_: Response(
lambda request, *_: [print(request.path), Response(
ResponseCode.OK,
{'Content-Type': 'text/html'},
parse_file('.' + request.path.path, dict(prev=request.headers.get('Referer').replace('/', '\\/') if request.headers.has('Referer') else '')).encode('utf-8')
)
parse_file('.' + request.path.path, dict((k, v.replace('\'', '')) for k, v in map(lambda item: (item[0], item[1]), request.path.params.items()))).encode('utf-8')
)][-1]
),
Route(
lambda path: os.path.isfile('.' + path.path) and (path.path.startswith('/font/') or path.path.startswith('/files/')),
lambda path: os.path.isfile('.' + path.path) and (path.path.startswith('/font/') or path.path.startswith('/files/') or path.path.startswith('/.well-known/')),
[Method.GET],
lambda request, *_: Response(
ResponseCode.OK,
*raw_file_contents('.' + request.path.path)
)
),
Route(
lambda request: request.path == '/robots.txt',
[Method.GET],
lambda *_: Response(
ResponseCode.OK,
*raw_file_contents('./robots.txt')
)
),
Route(
lambda request: request.path == '/status',
[Method.GET],

View File

@ -25,7 +25,7 @@ def handle_client(client: socket.socket, addr: Tuple[str, int]) -> None:
.execute(request, client, addr) \
.send(client)
log.info('destroy thread')
log.debug('destroy thread')
def main() -> None:
http_thread = threading.Thread(name='http', target=serve, args=('0.0.0.0', config['http-port'], handle_client))