Add strict name validation for the LVM backend
[ganeti-local] / lib / http / __init__.py
index 008cf9c..1d32e64 100644 (file)
@@ -299,13 +299,15 @@ class HttpVersionNotSupported(HttpException):
   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)
 
 
@@ -340,7 +342,7 @@ def WaitForSocketCondition(sock, event, timeout):
       if not io_events:
         # Timeout
         return None
-      for (evfd, evcond) in io_events:
+      for (_, evcond) in io_events:
         if evcond & check:
           return evcond
   finally:
@@ -367,15 +369,12 @@ def SocketOperation(sock, op, arg1, timeout):
   # 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.
@@ -404,9 +403,9 @@ def SocketOperation(sock, op, arg1, timeout):
       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
@@ -548,6 +547,19 @@ def Handshake(sock, write_timeout):
     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.
 
@@ -628,6 +640,8 @@ class HttpBase(object):
     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
@@ -744,7 +758,7 @@ class HttpMessageWriter(object):
   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)
@@ -848,7 +862,7 @@ class HttpMessageReader(object):
         # the CRLF."
         if idx == 0:
           # TODO: Limit number of CRLFs/empty lines for safety?
-          buf = buf[:2]
+          buf = buf[2:]
           continue
 
         if idx > 0:
@@ -937,7 +951,7 @@ class HttpMessageReader(object):
   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
@@ -1005,7 +1019,7 @@ class HttpMessageReader(object):
     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