|
13 | 13 | import re |
14 | 14 | import socket |
15 | 15 | from collections import deque |
16 | | -from errno import ( |
17 | | - ECONNABORTED, |
18 | | - ECONNREFUSED, |
19 | | - ECONNRESET, |
20 | | - EPIPE, |
21 | | - EPROTOTYPE, |
22 | | - ESHUTDOWN, |
23 | | -) |
| 16 | +from errno import errorcode |
24 | 17 | from functools import partial |
25 | 18 | from io import open |
26 | 19 | from json import dumps as json_dumps, loads as json_loads |
@@ -128,12 +121,17 @@ class RequestHandler(BaseHTTPRequestHandler, object): |
128 | 121 | } |
129 | 122 |
|
130 | 123 | SWALLOWED_ERRORS = { |
131 | | - ECONNABORTED, |
132 | | - ECONNREFUSED, |
133 | | - ECONNRESET, |
134 | | - EPIPE, |
135 | | - EPROTOTYPE, |
136 | | - ESHUTDOWN, |
| 124 | + 'ECONNABORTED', |
| 125 | + 'ECONNREFUSED', |
| 126 | + 'ECONNRESET', |
| 127 | + 'EPIPE', |
| 128 | + 'EPROTOTYPE', |
| 129 | + 'ESHUTDOWN', |
| 130 | + 'WSAECONNABORTED', |
| 131 | + 'WSAECONNREFUSED', |
| 132 | + 'WSAECONNRESET', |
| 133 | + 'WSAEPROTOTYPE', |
| 134 | + 'WSAESHUTDOWN', |
137 | 135 | } |
138 | 136 |
|
139 | 137 | def __init__(self, request, client_address, server): |
@@ -176,11 +174,23 @@ def handle_one_request(self): |
176 | 174 | try: |
177 | 175 | super(RequestHandler, self).handle_one_request() |
178 | 176 | return |
179 | | - except (HTTPError, OSError) as exc: |
| 177 | + except Exception as exc: |
180 | 178 | self.close_connection = True |
181 | 179 | self.log.exception('Request failed') |
182 | | - if (isinstance(exc, HTTPError) |
183 | | - or getattr(exc, 'errno', None) in self.SWALLOWED_ERRORS): |
| 180 | + if isinstance(exc, HTTPError): |
| 181 | + return |
| 182 | + error = getattr(exc, 'errno', None) |
| 183 | + if error and errorcode.get(error) in self.SWALLOWED_ERRORS: |
| 184 | + return |
| 185 | + raise exc |
| 186 | + |
| 187 | + def finish(self): |
| 188 | + try: |
| 189 | + super(RequestHandler, self).finish() |
| 190 | + except Exception as exc: |
| 191 | + self.log.exception('File object failed to close cleanly') |
| 192 | + error = getattr(exc, 'errno', None) |
| 193 | + if error and errorcode.get(error) in self.SWALLOWED_ERRORS: |
184 | 194 | return |
185 | 195 | raise exc |
186 | 196 |
|
|
0 commit comments