def test_NoEtag(self):

        server = Session("www.example.com")
        request = RequestResponse(server, methods.GET, "/")
        self.assertEqual(request.generateRequestHeader(), """GET / HTTP/1.1
Host: www.example.com

""".replace("\n", "\r\n")
)
    def test_EtagNoneMatch(self):

        server = Session("www.example.com")
        request = RequestResponse(server, methods.GET, "/", "\"etag\"", False)
        self.assertEqual(request.generateRequestHeader(), """GET / HTTP/1.1
Host: www.example.com
If-None-Match: "etag"

""".replace("\n", "\r\n")
)
Ejemplo n.º 3
0
    def test_NoEtag(self):

        server = Session("www.example.com")
        request = RequestResponse(server, methods.GET, "/")
        self.assertEqual(
            request.generateRequestHeader(), """GET / HTTP/1.1
Host: www.example.com
User-Agent: Secret-Agent

""".replace("\n", "\r\n"))
Ejemplo n.º 4
0
    def test_EtagNoneMatch(self):

        server = Session("www.example.com")
        request = RequestResponse(server, methods.GET, "/", "\"etag\"", False)
        self.assertEqual(
            request.generateRequestHeader(), """GET / HTTP/1.1
Host: www.example.com
User-Agent: Secret-Agent
If-None-Match: "etag"

""".replace("\n", "\r\n"))
    def test_Content(self):

        server = Session("www.example.com")
        request = RequestResponse(server, methods.GET, "/")
        rawdata = "Here is some data\r\non multiple lines."
        data = RequestDataString(rawdata, "text/plain")
        request.setData(data, None)
        self.assertEqual(request.generateRequestHeader(), """GET / HTTP/1.1
Host: www.example.com
Content-Length: %d
Content-Type: text/plain

""".replace("\n", "\r\n") % (len(rawdata),)
)
Ejemplo n.º 6
0
    def test_Content(self):

        server = Session("www.example.com")
        request = RequestResponse(server, methods.GET, "/")
        rawdata = "Here is some data\r\non multiple lines."
        data = RequestDataString(rawdata, "text/plain")
        request.setData(data, None)
        self.assertEqual(
            request.generateRequestHeader(), """GET / HTTP/1.1
Host: www.example.com
User-Agent: Secret-Agent
Content-Length: %d
Content-Type: text/plain

""".replace("\n", "\r\n") % (len(rawdata), ))
Ejemplo n.º 7
0
    def initialise(self, host, base_uri):
        # Set host change
        self.setServer(host)

        # Loop repeating until we can do it or get a fatal error
        first_time = True
        while True:

            # Create OPTIONS request for the base_uri
            request = RequestResponse(self, methods.OPTIONS, base_uri)
            request.setSession(self)
            sout = ResponseDataString()
            request.setData(None, sout)

            # Add request and process it
            self.sendRequest(request)

            # Check response
            if request.getStatusCode() == statuscodes.Unauthorized:

                # If we had authorization before, then chances are auth details are wrong - so delete and try again with new auth
                if self.hasAuthorization():

                    self.authorization = None

                    # Display error so user knows why the prompt occurs again
                    self.displayHTTPError(request)

                # Get authorization object (prompt the user) and redo the request
                self.authorization, cancelled = self.getAuthorizor(first_time, request.getResponseHeaders(headers.WWWAuthenticate))

                # Check for auth cancellation
                if cancelled:

                    self.authorization = None
                    return False

                first_time = False

                # Repeat the request loop with new authorization
                continue

            # Look for success and exit loop for further processing
            if request.getStatusCode() in (statuscodes.OK, statuscodes.NoContent):

                # Grab the server string
                if request.hasResponseHeader(headers.Server):
                    self.setServerDescriptor = self.setServerDescriptor(request.getResponseHeader(headers.Server))

                # Now check the response headers for a DAV version (may be more than one)
                self.version = ()
                for dav_version in request.getResponseHeaders(headers.DAV):

                    # Tokenize on commas
                    for token in dav_version.split(","):

                        token = token.strip()
                        self.addVersion(token)

                self.setServerType(self.version)

                # Put other strings into capability
                capa = ""
                for name, value in request.getResponseHeaders().iteritems():

                    if (not name.lower().startswith(headers.Server) and
                        not name.lower().startswith(headers.Date) and
                        name.lower().startswith("Content-")):

                        capa += "%s: %s\n" % (name, value,)

                self.setServerCapability(capa)

                # Just assume any version is fine for now
                break

            # If we get here we had some kind of fatal error
            self.handleHTTPError(request)
            return False

        self.initialised = True

        return True