def __init__(oSelf, uMaxThreads, asFilePaths, arContentRegExps,
                 arNegativeContentRegExps, bUnicode,
                 uNumberOfRelevantLinesBeforeMatch,
                 uNumberOfRelevantLinesAfterMatch):
        oSelf.asFilePaths = asFilePaths
        oSelf.arContentRegExps = arContentRegExps
        oSelf.arNegativeContentRegExps = arNegativeContentRegExps
        oSelf.bUnicode = bUnicode
        oSelf.uNumberOfRelevantLinesBeforeMatch = uNumberOfRelevantLinesBeforeMatch
        oSelf.uNumberOfRelevantLinesAfterMatch = uNumberOfRelevantLinesAfterMatch
        oSelf.uNumberOfFiles = len(asFilePaths)
        oSelf.uScannedBytes = 0
        oSelf.oException = None
        oSelf.oasNotScannedFilePaths = cList()
        oSelf.dMatched_auLineNumbers_by_sFilePath = {}
        oSelf.dRelevant_asbLines_by_uLineNumber_by_sFilePath = {}
        oSelf.oScanThreadsStarted = cCounter(uMaxThreads)
        oSelf.oScanThreadsFinished = cCounter(0)
        oSelf.oFilePathsQueue = queue.Queue()
        oSelf.oFileScansStarted = cCounter(0)
        oSelf.oFileScansFinished = cCounter(0)
        for sFilePath in oSelf.asFilePaths:
            oSelf.oFilePathsQueue.put(sFilePath)
        oSelf.fDebug("initialized")

        aoThreads = [cThread(oSelf.fScanThread) for x in range(uMaxThreads)
                     ] + [cThread(oSelf.fStatusThread)]
        for oThread in aoThreads:
            oThread.fStart(bVital=False)
        for oThread in aoThreads:
            oThread.fWait()
        oSelf.fDebug("Finished scanning")
        oSelf.asNotScannedFilePaths = oSelf.oasNotScannedFilePaths.axValue
Exemple #2
0
    def __init__(
        oSelf,
        uMaxThreads,
        asFolderPaths,
        bRecursive,
        arPathRegExps,
        arNegativePathRegExps,
        arNameRegExps,
        arNegativeNameRegExps,
        bVerbose,
    ):
        oSelf.oUnhandledItemPathsQueue = queue.Queue()
        oSelf.bRecursive = bRecursive
        oSelf.arPathRegExps = arPathRegExps
        oSelf.arNegativePathRegExps = arNegativePathRegExps
        oSelf.arNameRegExps = arNameRegExps
        oSelf.arNegativeNameRegExps = arNegativeNameRegExps
        oSelf.bVerbose = bVerbose

        oSelf.oMatchesByFilePath = cDict()
        oSelf.oException = None
        oSelf.oNumberOfFilesFound = cCounter()
        oSelf.oNumberOfFoldersFound = cCounter()
        oSelf.oNumberOfItemsFound = cCounter(0)
        oSelf.oNumberOfItemsCompleted = cCounter()
        oSelf.oScanThreadsStarted = cCounter(uMaxThreads)
        oSelf.oScanThreadsFinished = cCounter(0)

        if oSelf.bVerbose:
            oConsole.fOutput(
                COLOR_BUSY,
                CHAR_BUSY,
                COLOR_NORMAL,
                " Scanning ",
                COLOR_INFO,
                str(len(asFolderPaths)),
                COLOR_NORMAL,
                " folder paths",
                " (and descendants)" if bRecursive else "",
                "...",
            )
        for sFolderPath in set(asFolderPaths):
            oSelf.oNumberOfItemsFound.fuIncrease()
            if os.path.isdir(sFolderPath):
                oSelf.oUnhandledItemPathsQueue.put(sFolderPath)
                oSelf.fDebug()
            else:
                oSelf.oNumberOfItemsCompleted.fuIncrease()
                oSelf.fDebug()

        aoThreads = [cThread(oSelf.fScanThread) for x in range(uMaxThreads)
                     ] + [cThread(oSelf.fStatusThread)]

        for oThread in aoThreads:
            oThread.fStart(bVital=False)
        for oThread in aoThreads:
            oThread.fWait()
Exemple #3
0
 def __init__(oSelf, fNewConnectionHandler, szHostname = zNotProvided, uzPort = zNotProvided, o0SSLContext = None, n0zSecureTimeoutInSeconds = zNotProvided):
   oSelf.__fNewConnectionHandler = fNewConnectionHandler;
   oSelf.__sHostname = fxGetFirstProvidedValue(szHostname, socket.gethostname());
   oSelf.__uPort = fxGetFirstProvidedValue(uzPort, 443 if o0SSLContext else 80);
   oSelf.__o0SSLContext = o0SSLContext;
   assert o0SSLContext is None or c0SSLException is not None, \
       "Cannot load SSL support";
   oSelf.__n0zSecureTimeoutInSeconds = n0zSecureTimeoutInSeconds;
   
   oSelf.__bStopping = False;
   oSelf.__oTerminatedLock = cLock(
     "%s.__oTerminatedLock" % oSelf.__class__.__name__,
     bLocked = True
   );
   
   oSelf.__oPythonSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0);
   oSelf.__oPythonSocket.settimeout(None);
   oSelf.__oPythonSocket.bind((oSelf.__sHostname, oSelf.__uPort));
   oSelf.__oPythonSocket.listen(1);
   oSelf.__oMainThread = cThread(oSelf.__fMain);
   oSelf.__oMainThread.fStart(bVital = False);
   oSelf.fAddEvents(
     "new connection", # Fired first for both secure and nonsecure connections
     "new nonsecure connection", # Fired for nonsecure connections only.
     "new secure connection", # Fired after SSL negotiation has completed successfully.
     "connection cannot be secured", # Fired if SSL negotiation failed to complete successfully and before timeout.
     "terminated" # Fired when the acceptor stops accepting connections.
   );
 def fStartConnectionHandlerThread(oConnectionFromClient, oResponse):
     oThread = cThread(fConnectionHandler,
                       *txConnectionHandlerArguments)
     oSelf.__oPropertyAccessTransactionLock.fAcquire()
     try:
         oSelf.__aoSecureConnectionsFromClient.append(
             oConnectionFromClient)
         oSelf.__aoSecureConnectionThreads.append(oThread)
     finally:
         oSelf.__oPropertyAccessTransactionLock.fRelease()
     oThread.fStart(bVital=False)
Exemple #5
0
 def __fHandleNewConnection(oSelf, oConnectionAcceptor, oConnection):
   fShowDebugOutput("New connection %s..." % (oConnection,));
   oSelf.__oPropertyAccessTransactionLock.fAcquire();
   try:
     assert not oSelf.bTerminated, \
       "Received a new connection after we've terminated!?";
     if oSelf.__bStopping:
       fDebugOutput("Stopping connection since we are stopping...");
       bHandleConnection = False;
     else:
       oThread = cThread(oSelf.__fConnectionThread, oConnection);
       oSelf.__aoConnections.append(oConnection);
       oSelf.__aoConnectionThreads.append(oThread);
       bHandleConnection = True;
   finally:
     oSelf.__oPropertyAccessTransactionLock.fRelease();
   if bHandleConnection:
     oConnection.fAddCallback("terminated", oSelf.__fHandleTerminatedCallbackFromConnection);
     oThread.fStart();
   else:
     oConnection.fStop();
Exemple #6
0
def fTestClient(
  oHTTPClient,
  oCertificateStore,
  nEndWaitTimeoutInSeconds,
):
  oServersShouldBeRunningLock = threading.Lock();
  oServersShouldBeRunningLock.acquire(); # Released once servers should stop runnning.
  oConsole.fPrint("\xFE\xFE\xFE\xFE Making a first test request to %s " % oTestURL, sPadding = "\xFE");
  (oRequest, o0Response) = oHTTPClient.fto0GetRequestAndResponseForURL(oTestURL);
  assert o0Response, \
      "No response!?";
  oResponse = o0Response;
  oConsole.fPrint("  oRequest = %s" % oRequest.fsSerialize());
  oConsole.fPrint("  oResponse = %s" % oResponse.fsSerialize());
  oConsole.fPrint("\xFE\xFE\xFE\xFE Making a second test request to %s " % oTestURL, sPadding = "\xFE");
  (oRequest, o0Response) = oHTTPClient.fto0GetRequestAndResponseForURL(oTestURL);
  assert o0Response, \
      "No response!?";
  oResponse = o0Response;
  oConsole.fPrint("  oRequest = %s" % oRequest);
  oConsole.fPrint("  oResponse = %s" % oResponse);
  if oHTTPClient.__class__.__name__ == "cHTTPClient": 
    # cHTTPClient specific checks
    asConnectionPoolsProtocolHostPort = set(oHTTPClient._cHTTPClient__doConnectionsToServerPool_by_sProtocolHostPort.keys());
    assert asConnectionPoolsProtocolHostPort == set((oTestURL.sBase,)), \
        "Expected a oHTTPClient instance to have one cConnectionsToServerPool instance for %s, but found %s" % \
        (oTestURL.sBase, repr(asConnectionPoolsProtocolHostPort));
    oConnectionsToServerPool = oHTTPClient._cHTTPClient__doConnectionsToServerPool_by_sProtocolHostPort.get(oTestURL.sBase);
    assert oConnectionsToServerPool, \
        "Expected a cConnectionsToServerPool instance for %s, but found none" % oTestURL;
    aoConnections = oConnectionsToServerPool._cHTTPConnectionsToServerPool__aoConnections;
    assert len(aoConnections) == 1, \
        "Expected a cConnectionsToServerPool instance with one connection for %s, but found %d connections" % \
        (oTestURL, len(aoConnections));
  if oHTTPClient.__class__.__name__ == "cHTTPClientUsingProxyServer": 
    # cHTTPClientUsingProxyServer specific checks
    aoConnectionsToProxyNotConnectedToAServer = oHTTPClient._cHTTPClientUsingProxyServer__aoConnectionsToProxyNotConnectedToAServer;
    assert len(aoConnectionsToProxyNotConnectedToAServer) == 1, \
        "Expected one connection to the proxy, but found %d connections" % len(aoConnectionsToProxyNotConnectedToAServer);
    doSecureConnectionToServerThroughProxy_by_sProtocolHostPort = oHTTPClient._cHTTPClientUsingProxyServer__doSecureConnectionToServerThroughProxy_by_sProtocolHostPort;
    asSecureConnectionTargets = doSecureConnectionToServerThroughProxy_by_sProtocolHostPort.keys();
    assert len(asSecureConnectionTargets) == 0, \
        "Expected no secure connections, but found %s" % repr(asSecureConnectionTargets);
  
  # Wrapping SSL secured sockets in SSL is not currently supported, so the
  # client cannot secure a connection to a server over a secure connection to a
  # proxy.
  oProxyServerURLForSecureTestURL = oHTTPClient.fo0GetProxyServerURLForURL(oSecureTestURL);
  # If we are not using a proxy, or the URL for the proxy server is not secure,
  # we can test a secure connection to the server.
  if not oProxyServerURLForSecureTestURL or not oProxyServerURLForSecureTestURL.bSecure: 
    oConsole.fPrint("\xFE\xFE\xFE\xFE Making a first test request to %s " % oSecureTestURL, sPadding = "\xFE");
    (oRequest, o0Response) = oHTTPClient.fto0GetRequestAndResponseForURL(oSecureTestURL);
    assert o0Response, \
        "No response!?";
    oResponse = o0Response;
    oConsole.fPrint("  oRequest = %s" % oRequest);
    oConsole.fPrint("  oResponse = %s" % oResponse);
    oConsole.fPrint("\xFE\xFE\xFE\xFE Making a second test request to %s " % oSecureTestURL, sPadding = "\xFE");
    (oRequest, o0Response) = oHTTPClient.fto0GetRequestAndResponseForURL(oSecureTestURL);
    assert o0Response, \
        "No response!?";
    oResponse = o0Response;
    oConsole.fPrint("  oRequest = %s" % oRequest);
    oConsole.fPrint("  oResponse = %s" % oResponse);
    if oHTTPClient.__class__.__name__ == "cHTTPClient": 
      # cHTTPClient specific checks
      asConnectionPoolsProtocolHostPort = set(oHTTPClient._cHTTPClient__doConnectionsToServerPool_by_sProtocolHostPort.keys());
      assert asConnectionPoolsProtocolHostPort == set((oTestURL.sBase, oSecureTestURL.sBase)), \
          "Expected a oHTTPClient instance to have a cConnectionsToServerPool instance for %s and %s, but found %s" % \
          (oTestURL.sBase, oSecureTestURL.sBase, repr(asConnectionPoolsProtocolHostPort));
      
      oConnectionsToServerPool = oHTTPClient._cHTTPClient__doConnectionsToServerPool_by_sProtocolHostPort.get(oSecureTestURL.sBase);
      assert oConnectionsToServerPool, \
          "Expected a cConnectionsToServerPool instance for %s, but found none" % oSecureTestURL;
      aoConnections = oConnectionsToServerPool._cHTTPConnectionsToServerPool__aoConnections;
      assert len(aoConnections) == 1, \
          "Expected a cConnectionsToServerPool instance with one connection for %s, but found %d connections" % \
          (oSecureTestURL, len(aoConnections));
    if oHTTPClient.__class__.__name__ == "cHTTPClientUsingProxyServer": 
      # cHTTPClientUsingProxyServer specific checks
      aoConnectionsToProxyNotConnectedToAServer = oHTTPClient._cHTTPClientUsingProxyServer__aoConnectionsToProxyNotConnectedToAServer;
      doSecureConnectionToServerThroughProxy_by_sProtocolHostPort = oHTTPClient._cHTTPClientUsingProxyServer__doSecureConnectionToServerThroughProxy_by_sProtocolHostPort;
      asSecureConnectionTargets = doSecureConnectionToServerThroughProxy_by_sProtocolHostPort.keys();
      bFoundUnexpectedNonSecureConnections = len(aoConnectionsToProxyNotConnectedToAServer) != 0;
      bFoundUnexpectedSecureConnections = set(asSecureConnectionTargets) != set((oSecureTestURL.sBase,));
      if bFoundUnexpectedNonSecureConnections or bFoundUnexpectedSecureConnections:
        if bFoundUnexpectedNonSecureConnections:
          print "The HTTP client has unexpected non-secure connections!";
        if bFoundUnexpectedSecureConnections:
          print "The HTTP client has unexpected secure connections!";
        print "Non-secure connections:";
        for oNonSecureConnection in aoConnectionsToProxyNotConnectedToAServer:
          print "* %s" % repr(oNonSecureConnection);
        print "Secure connections:";
        for (sProtocolHostPort, oSecureConnection) in doSecureConnectionToServerThroughProxy_by_sProtocolHostPort.items():
          print "* %S => %s" % (sProtocolHostPort, repr(oSecureConnection));
        raise AssertionError();
  
  # Create a server on a socket but do not listen so connections are refused.
  oConnectionRefusedServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0);
  oConnectionRefusedServerSocket.bind((oConnectionRefusedURL.sHostname, oConnectionRefusedURL.uPort));

  # Create a server on a socket that sends out-of-band data.
  oOutOfBandDataServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0);
  oOutOfBandDataServerSocket.bind((oOutOfBandDataURL.sHostname, oOutOfBandDataURL.uPort));
  oOutOfBandDataServerSocket.listen(1);
  oResponse = cHTTPConnection.foCreateResponse(s0Data = "Hello, world!");
  sResponseWithOutOfBandData = oResponse.fsSerialize() + oResponse.fsSerialize();
  def fOutOfBandDataServerThread():
    (oClientSocket, (sClientIP, uClientPort)) = oOutOfBandDataServerSocket.accept();
    oConsole.fPrint("Out-of-band data server receiving request...");
    oClientSocket.recv(0x1000);
    oConsole.fPrint("Out-of-band data server sending valid response with out-of-band data...");
    oClientSocket.send(oResponse.fsSerialize() + "X");
    oConsole.fPrint("Out-of-band data server thread terminated.");
    oClientSocket.close();
  
  oOutOfBandDataServerThread = cThread(fOutOfBandDataServerThread);
  oOutOfBandDataServerThread.fStart(bVital = False);
  
  # Create a server on a socket that immediately closes the connection.
  oConnectionDisconnectedServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0);
  oConnectionDisconnectedServerSocket.bind((oConnectionDisconnectedURL.sHostname, oConnectionDisconnectedURL.uPort));
  oConnectionDisconnectedServerSocket.listen(1);
  def fConnectionDisconnectedServerThread():
    (oClientSocket, (sClientIP, uClientPort)) = oConnectionDisconnectedServerSocket.accept();
    oConsole.fPrint("Disconnect server is disconnecting the connection...");
    oClientSocket.close();
    oConsole.fPrint("Disconnect server thread terminated.");
    
  oConnectionDisconnectedServerThread = cThread(fConnectionDisconnectedServerThread);
  oConnectionDisconnectedServerThread.fStart(bVital = False);
  
  # Create a server on a socket that immediately shuts down the connection.
  oConnectionShutdownServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0);
  oConnectionShutdownServerSocket.bind((oConnectionShutdownURL.sHostname, oConnectionShutdownURL.uPort));
  oConnectionShutdownServerSocket.listen(1);
  def fConnectionShutdownServerThread():
    (oClientSocket, (sClientIP, uClientPort)) = oConnectionShutdownServerSocket.accept();
    oConsole.fPrint("Shutdown server is shutting down the connection for writing...");
    oClientSocket.shutdown(socket.SHUT_WR);
    oConsole.fPrint("Shutdown server is sleeping to keep the connection open....");
    oServersShouldBeRunningLock.acquire();
    oServersShouldBeRunningLock.release();
    oConsole.fPrint("Shutdown server thread terminated.");
    
  oConnectionShutdownServerThread = cThread(fConnectionShutdownServerThread);
  oConnectionShutdownServerThread.fStart(bVital = False);

  # Create a server on a socket that does not send a response.
  oResponseTimeoutServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0);
  oResponseTimeoutServerSocket.bind((oResponseTimeoutURL.sHostname, oResponseTimeoutURL.uPort));
  oResponseTimeoutServerSocket.listen(1);
  def fResponseTimeoutServerThread():
    (oClientSocket, (sClientIP, uClientPort)) = oResponseTimeoutServerSocket.accept();
    oConsole.fPrint("Response timeout server receiving request...");
    oClientSocket.recv(0x1000);
    oConsole.fPrint("Response timeout server is sleeping to avoid sending a response...");
    oServersShouldBeRunningLock.acquire();
    oServersShouldBeRunningLock.release();
    oConsole.fPrint("Response timeout thread terminated.");
    
  oResponseTimeoutServerThread = cThread(fResponseTimeoutServerThread);
  oResponseTimeoutServerThread.fStart(bVital = False);

  # Create a server on a socket that sends an invalid response.
  oInvalidHTTPMessageServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0);
  oInvalidHTTPMessageServerSocket.bind((oInvalidHTTPMessageURL.sHostname, oInvalidHTTPMessageURL.uPort));
  oInvalidHTTPMessageServerSocket.listen(1);
  sInvalidResponse = "Hello, world!\r\n";
  def fInvalidHTTPMessageServerThread():
    (oClientSocket, (sClientIP, uClientPort)) = oInvalidHTTPMessageServerSocket.accept();
    oConsole.fPrint("Invalid HTTP Message server received request; sending invalid response...");
    oClientSocket.recv(0x1000); # This should cover the request, which we discard.
    oClientSocket.send(sInvalidResponse);
    oConsole.fPrint("Invalid HTTP Message server thread terminated.");
  
  oInvalidHTTPMessageServerThread = cThread(fInvalidHTTPMessageServerThread);
  oInvalidHTTPMessageServerThread.fStart(bVital = False);
  
  for (uNumberOfRequests, oURL, cExpectedExceptionClass, acAcceptableExceptionClasses, auAcceptableStatusCodes) in (
    (1, oUnknownHostnameURL,
        cDNSUnknownHostnameException, [],
        [400]),
    (1, oInvalidAddressURL,
        cTCPIPInvalidAddressException, [],
        [400]),
    (1, oConnectionRefusedURL,
        cTCPIPConnectionRefusedException, [cTCPIPConnectTimeoutException],
        [502]),
    (1, oConnectTimeoutURL,
        cTCPIPConnectTimeoutException, [],
        [502, 504]),
    (1, oConnectionDisconnectedURL,
        cTCPIPConnectionDisconnectedException, [],
        [502]),
    (1, oConnectionShutdownURL,
        cTCPIPConnectionShutdownException, [],
        [502]),
    (1, oResponseTimeoutURL,
        cTCPIPDataTimeoutException, [],
        [504]),
    (2, oOutOfBandDataURL,
        cHTTPOutOfBandDataException, [],
        [502]),
    (1, oInvalidHTTPMessageURL,
        cHTTPInvalidMessageException, [],
        [502]),
  ):
    oConsole.fPrint("\xFE\xFE\xFE\xFE Making a test request to %s " % oURL, sPadding = "\xFE");
    if oHTTPClient.__class__.__name__ == "cHTTPClient":
      oConsole.fStatus("  * Expecting %s exception..." % cExpectedExceptionClass.__name__);
      auAcceptableStatusCodes = None;
    if oHTTPClient.__class__.__name__ == "cHTTPClientUsingProxyServer":
      if auAcceptableStatusCodes:
        oConsole.fStatus("  * Expecting a HTTP %s reponse..." % "/".join(["%03d" % uStatusCode for uStatusCode in auAcceptableStatusCodes]));
        cExpectedExceptionClass = None;
    for uConnectionNumber in xrange(1, uNumberOfRequests + 1):
      if uConnectionNumber < uNumberOfRequests:
        # We do not yet expect an exception, so we won't handle one.
        o0Response = oHTTPClient.fo0GetResponseForURL(oURL);
        assert o0Response, \
            "No response!?";
        oResponse = o0Response;
        oConsole.fPrint("  oResponse = %s" % oResponse);
      else:
        try:
          # Use a short connect timeout to speed things up: all connections should be created in about 1 second except the
          # one that purposefully times out and this way we do not have to wait for that to happen very long.
          o0Response = oHTTPClient.fo0GetResponseForURL(oURL);
          assert o0Response, \
              "No response!?";
          oResponse = o0Response;
          if auAcceptableStatusCodes:
            assert oResponse.uStatusCode in auAcceptableStatusCodes, \
                "Expected a HTTP %s response, got %s" % \
                ("/".join(["%03d" % uStatusCode for uStatusCode in auAcceptableStatusCodes]), oResponse.fsGetStatusLine());
          oConsole.fOutput("  oResponse = %s" % oResponse);
        except Exception as oException:
          if oException.__class__ is cExpectedExceptionClass:
            oConsole.fPrint(OK, "  + Threw %s." % repr(oException));
          elif oException.__class__ in acAcceptableExceptionClasses:
            oConsole.fPrint(WARNING, "  ~ Threw %s." % repr(oException));
            oConsole.fPrint("    Expected %s." % cExpectedExceptionClass.__name__);
          else:
            oConsole.fPrint(ERROR, "  - Threw %s." % repr(oException));
            if cExpectedExceptionClass:
              oConsole.fPrint("    Expected %s." % cExpectedExceptionClass.__name__);
            else:
              oConsole.fPrint("    No exception expected.");
            raise;
        else:
          if cExpectedExceptionClass:
            oConsole.fPrint(ERROR, "  - Expected %s." % cExpectedExceptionClass.__name__);
            raise AssertionError("No exception");
  
  # Allow server threads to stop.
  oServersShouldBeRunningLock.release();
  
  oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping HTTP Client ", sPadding = "\xFE");
  oHTTPClient.fStop();
  assert oHTTPClient.fbWait(nEndWaitTimeoutInSeconds), \
    "HTTP Client did not stop in time";
  
  oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping connection refused server ", sPadding = "\xFE");
  oConnectionRefusedServerSocket.close(); # Has no thread.

  oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping out-of-band data server ", sPadding = "\xFE");
  oOutOfBandDataServerSocket.close();
  assert oOutOfBandDataServerThread.fbWait(nEndWaitTimeoutInSeconds), \
      "Out-of-band data server thread (%d/0x%X) did not stop in time." % \
      (oOutOfBandDataServerThread.uId, oOutOfBandDataServerThread.uId);
  
  oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping connection closed server ", sPadding = "\xFE");
  oConnectionDisconnectedServerSocket.close();
  assert oConnectionDisconnectedServerThread.fbWait(nEndWaitTimeoutInSeconds), \
      "Connection closed server thread (%d/0x%X) did not stop in time." % \
      (oConnectionDisconnectedServerThread.uId, oConnectionDisconnectedServerThread.uId);
  
  oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping connection shutdown server ", sPadding = "\xFE");
  oConnectionShutdownServerSocket.close();
  assert oConnectionShutdownServerThread.fbWait(nEndWaitTimeoutInSeconds), \
      "Connection shutdown server thread (%d/0x%X) did not stop in time." % \
      (oConnectionShutdownServerThread.uId, oConnectionShutdownServerThread.uId);
  
  oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping response timeout server ", sPadding = "\xFE");
  oResponseTimeoutServerSocket.close();
  assert oResponseTimeoutServerThread.fbWait(nEndWaitTimeoutInSeconds), \
      "Connection shutdown server thread (%d/0x%X) did not stop in time." % \
      (oResponseTimeoutServerThread.uId, oResponseTimeoutServerThread.uId);
  
  oConsole.fPrint("\xFE\xFE\xFE\xFE Stopping invalid http message server ", sPadding = "\xFE");
  oInvalidHTTPMessageServerSocket.close();
  assert oInvalidHTTPMessageServerThread.fbWait(nEndWaitTimeoutInSeconds), \
      "Invalid http message server thread (%d/0x%X) did not stop in time." % \
      (oInvalidHTTPMessageServerThread.uId, oInvalidHTTPMessageServerThread.uId);
Exemple #7
0
 def __init__(oSelf,
              oConsole,
              sHostname,
              uPort,
              sName,
              bListen=True,
              bAccept=True,
              u0MinRequestBytes=None,
              u0MaxRequestBytes=None,
              s0Response=None,
              bShutdownForReading=False,
              bShutdownForWriting=False,
              bDisconnect=False):
     oSelf.oConsole = oConsole
     # not imported but passed as an argument because it may not be available, in which case a stub is used.
     oSelf.sHostname = sHostname
     oSelf.uPort = uPort
     oSelf.sName = sName
     oSelf.oServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM,
                                         0)
     #    oSelf.oServerSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1); # Do not go to TIME_WAIT after close.
     oSelf.oConsole.fOutput(
         DIM, "* ", oSelf.sName,
         " test server: binding to %s:%d..." % (sHostname, uPort))
     oSelf.oServerSocket.bind((sHostname, uPort))
     oSelf.bListening = bListen
     oSelf.bAccepting = bListen and bAccept
     oSelf.oTerminatedLock = cLock("Terminated lock", bLocked=True)
     if not bListen:
         oSelf.oConsole.fOutput(DIM, "* ", oSelf.sName,
                                " test server: closing server socket...")
         oSelf.oServerSocket.close()
     else:
         oSelf.bStopping = False
         # If we are accepting connections, allow the next connection to be queued while we're handling the current
         # connection (see below)
         oSelf.oConsole.fOutput(DIM, "* ", oSelf.sName,
                                " test server: listening...")
         oSelf.oServerSocket.listen(1 if bAccept else 0)
         oSelf.u0MinRequestBytes = u0MinRequestBytes
         if u0MinRequestBytes is not None:
             assert bAccept, "Cannot have `bAccept is False` and `u0MinRequestBytes is not None`!"
             assert u0MaxRequestBytes is not None, "Cannot have `u0MinRequestBytes is not None` if `u0MaxRequestBytes is None`!"
             oSelf.uMaxRequestBytes = u0MaxRequestBytes
         assert bAccept or s0Response is None, "Cannot have `bAccept is False` and `s0Response is not None`!"
         oSelf.s0Response = s0Response
         assert bAccept or not bShutdownForReading, "Cannot have `bAccept is False` and `bShutdownForReading is True`!"
         oSelf.bShutdownForReading = bShutdownForReading
         assert bAccept or not bShutdownForWriting, "Cannot have `bAccept is False` and `bShutdownForWriting is True`!"
         oSelf.bShutdownForWriting = bShutdownForWriting
         assert bAccept or not bShutdownForWriting, "Cannot have `bAccept is False` and `bDisconnect is True`!"
         oSelf.bDisconnect = bDisconnect
         if not bAccept:
             # If we are NOT accepting connections, make a connection to the server but we do not
             # accept it. Further connections cannot be made until it is accepted but will be queued
             # This is because we've told the socket not to queue additional connections (see above).
             oSelf.oHelperClientSocket = socket.socket(
                 socket.AF_INET, socket.SOCK_STREAM, 0)
             oSelf.oHelperClientSocket.connect((sHostname, uPort))
             oSelf.oConsole.fOutput(
                 DIM, "* ", oSelf.sName,
                 " test server: NOT accepting connections...")
         else:
             oSelf.oServerThreadStartedLock = cLock(
                 "Server thread started lock", bLocked=True)
             oSelf.oServerThread = cThread(oSelf.fServerThread)
             oSelf.oServerThread.fStart(bVital=False)
             oSelf.oServerThreadStartedLock.fWait()