This commit is contained in:
2021-03-25 12:16:40 +01:00
parent 7639383782
commit edfdc94747
5 changed files with 42 additions and 11 deletions

View File

@@ -239,7 +239,7 @@ class HTMLDownloadHandler(DownloadHandler):
new_url = self.__download_image(tag["src"], host, base_url) new_url = self.__download_image(tag["src"], host, base_url)
processed[tag["src"]] = new_url processed[tag["src"]] = new_url
if new_url: if new_url:
tag["src"] = new_url tag["src"] = os.path.basename(new_url)
except Exception as e: except Exception as e:
logging.error("Failed to download image: %s, skipping...", tag["src"], exc_info=e) logging.error("Failed to download image: %s, skipping...", tag["src"], exc_info=e)

View File

@@ -28,20 +28,41 @@ class IncompleteResponse(HTTPException):
def __init(self, cause): def __init(self, cause):
self.cause = cause self.cause = cause
class HTTPServerException(Exception): class HTTPServerException(Exception):
""" Base class for HTTP Server exceptions """ """ Base class for HTTP Server exceptions """
status_code: str
message: str
class BadRequest(HTTPServerException): class BadRequest(HTTPServerException):
""" Malformed HTTP request""" """ Malformed HTTP request"""
status_code = 400
message = "Bad Request"
class MethodNotAllowed(HTTPServerException): class MethodNotAllowed(HTTPServerException):
""" Method is not allowed """ """ Method is not allowed """
status_code = 405
message = "Method Not Allowed"
def __init(self, allowed_methods): def __init(self, allowed_methods):
self.allowed_methods = allowed_methods self.allowed_methods = allowed_methods
class InternalServerError(HTTPServerException):
""" Internal Server Error """
status_code = 500
message = "Internal Server Error"
class NotImplemented(HTTPServerException): class NotImplemented(HTTPServerException):
""" Functionality not implemented """ """ Functionality not implemented """
status_code = 501
message = "Not Implemented"
class NotFound(HTTPServerException): class NotFound(HTTPServerException):
""" Resource not found """ """ Resource not found """
status_code = 404
message = "Not Found"

View File

@@ -97,8 +97,6 @@ class ContentLengthRetriever(Retriever):
cur_payload_size += len(buffer) cur_payload_size += len(buffer)
yield buffer yield buffer
return b""
class RawRetriever(Retriever): class RawRetriever(Retriever):

View File

@@ -78,7 +78,8 @@ class RequestHandler:
def _has_body(self, headers): def _has_body(self, headers):
return "transfer-encoding" in headers or "content-encoding" in headers return "transfer-encoding" in headers or "content-encoding" in headers
def _get_date(self): @staticmethod
def _get_date():
now = datetime.now() now = datetime.now()
stamp = mktime(now.timetuple()) stamp = mktime(now.timetuple())
return format_date_time(stamp) return format_date_time(stamp)
@@ -114,3 +115,13 @@ class RequestHandler:
logging.debug("Sending: %r", message) logging.debug("Sending: %r", message)
self.conn.conn.sendall(message) self.conn.conn.sendall(message)
@staticmethod
def send_error(client: socket, code, message):
message = f"HTTP/1.1 {code} {message}\r\n"
message += RequestHandler._get_date() + "\r\n"
message += "Content-Length: 0\r\n"
message += "\r\n"
logging.debug("Sending: %r", message)
client.sendall(message.encode(FORMAT))

View File

@@ -1,10 +1,10 @@
import logging import logging
import multiprocessing as mp import multiprocessing as mp
import socket
import threading import threading
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
from logging import Logger
import socket
from httplib.exceptions import HTTPServerException, InternalServerError
from server.RequestHandler import RequestHandler from server.RequestHandler import RequestHandler
THREAD_LIMIT = 128 THREAD_LIMIT = 128
@@ -23,10 +23,8 @@ def worker(address, name, logging_level, queue: mp.Queue, stop_event: mp.Event):
class Worker: class Worker:
host: str host: str
name: str name: str
logger: Logger
queue: mp.Queue queue: mp.Queue
executor: ThreadPoolExecutor executor: ThreadPoolExecutor
stop_event: mp.Event stop_event: mp.Event
@@ -68,8 +66,11 @@ class Worker:
handler = RequestHandler(conn, self.host) handler = RequestHandler(conn, self.host)
handler.listen() handler.listen()
except Exception: except HTTPServerException as e:
logging.debug("Internal error") RequestHandler.send_error(conn, e.status_code, e.message)
except Exception as e:
RequestHandler.send_error(conn, InternalServerError.status_code, InternalServerError.message)
logging.debug("Internal error", exc_info=e)
conn.shutdown(socket.SHUT_RDWR) conn.shutdown(socket.SHUT_RDWR)
conn.close() conn.close()