-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_error.py
29 lines (23 loc) · 870 Bytes
/
http_error.py
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
from response import Response
from connection import Connection
class HTTPError(Exception):
def __init__(self, status, reason, body=None):
self.status = status
self.reason = reason
self.body = body
@staticmethod
def send_error(conn: Connection, err):
try:
status = err.status
reason = err.reason
body = (err.body or err.reason + '\n').encode('iso-8859-1')
except:
status = 500
reason = b'Internal Server Error'
body = b'Internal Server Error'
resp = Response(status, reason,
[('Content-Length', len(body))],
body.decode('iso-8859-1'))
if conn.connection_is_keep_alive:
resp.headers += [('Connection', 'Keep-Alive')]
Response.send_response(conn.sock, resp)