code = 501
+class HttpBadGateway(HttpException):
+ """502 Bad Gateway
+
+ RFC2616, 10.5.3: The server, while acting as a gateway or proxy,
+ received an invalid response from the upstream server it accessed in
+ attempting to fulfill the request.
+
+ """
+ code = 502
+
+
class HttpServiceUnavailable(HttpException):
"""503 Service Unavailable
code = 503
+class HttpGatewayTimeout(HttpException):
+ """504 Gateway Timeout
+
+ RFC2616, 10.5.5: The server, while acting as a gateway or proxy, did
+ not receive a timely response from the upstream server specified by
+ the URI (e.g. HTTP, FTP, LDAP) or some other auxiliary server
+ (e.g. DNS) it needed to access in attempting to complete the
+ request.
+
+ """
+ code = 504
+
+
class HttpVersionNotSupported(HttpException):
"""505 HTTP Version Not Supported
code = 505
-class HttpJsonConverter:
+class HttpJsonConverter: # pylint: disable-msg=W0232
CONTENT_TYPE = "application/json"
- def Encode(self, data):
+ @staticmethod
+ def Encode(data):
return serializer.DumpJson(data)
- def Decode(self, data):
+ @staticmethod
+ def Decode(data):
return serializer.LoadJson(data)
if not io_events:
# Timeout
return None
- for (evfd, evcond) in io_events:
+ for (_, evcond) in io_events:
if evcond & check:
return evcond
finally:
# TODO: event_poll/event_check/override
if op in (SOCKOP_SEND, SOCKOP_HANDSHAKE):
event_poll = select.POLLOUT
- event_check = select.POLLOUT
elif op == SOCKOP_RECV:
event_poll = select.POLLIN
- event_check = select.POLLIN | select.POLLPRI
elif op == SOCKOP_SHUTDOWN:
event_poll = None
- event_check = None
# The timeout is only used when OpenSSL requests polling for a condition.
# It is not advisable to have no timeout for shutdown.
if event is None:
raise HttpSocketTimeout()
- if (op == SOCKOP_RECV and
- event & (select.POLLNVAL | select.POLLHUP | select.POLLERR)):
- return ""
+ if event & (select.POLLNVAL | select.POLLHUP | select.POLLERR):
+ # Let the socket functions handle these
+ break
if not event & wait_for_event:
continue
raise HttpError("Error in SSL handshake: %s" % err)
+def InitSsl():
+ """Initializes the SSL infrastructure.
+
+ This function is idempotent.
+
+ """
+ if not OpenSSL.rand.status():
+ raise EnvironmentError("OpenSSL could not collect enough entropy"
+ " for the PRNG")
+
+ # TODO: Maybe add some additional seeding for OpenSSL's PRNG
+
+
class HttpSslParams(object):
"""Data class for SSL key and certificate.
we do on our side.
"""
+ # some parameters are unused, but this is the API
+ # pylint: disable-msg=W0613
assert self._ssl_params, "SSL not initialized"
return (self._ssl_cert.digest("sha1") == cert.digest("sha1") and
def HasMessageBody(self):
"""Checks whether the HTTP message contains a body.
- Can be overriden by subclasses.
+ Can be overridden by subclasses.
"""
return bool(self._msg.body)
# the CRLF."
if idx == 0:
# TODO: Limit number of CRLFs/empty lines for safety?
- buf = buf[:2]
+ buf = buf[2:]
continue
if idx > 0:
def ParseStartLine(self, start_line):
"""Parses the start line of a message.
- Must be overriden by subclass.
+ Must be overridden by subclass.
@type start_line: string
@param start_line: Start line string
if hdr_content_length:
try:
self.content_length = int(hdr_content_length)
- except ValueError:
+ except (TypeError, ValueError):
self.content_length = None
if self.content_length is not None and self.content_length < 0:
self.content_length = None