def test_noMessageInvalidStatusLocationExists(self): """ If no C{message} argument is passed to the L{PageRedirect} constructor and C{code} isn't a valid HTTP status code, C{message} stays L{None}. """ e = error.PageRedirect(b"InvalidCode", location=b"/outputFile") self.assertEqual(e.message, None)
def handleStatus_301(self): l = self.headers.get('location') if not l: self.handleStatusDefault() url = l[0] if self.followRedirect: scheme, host, port, path = \ _parse(url, defaultPort=self.transport.getPeer().port) self.factory.setURL(url) if self.factory.scheme == 'https': from twisted.internet import ssl contextFactory = ssl.ClientContextFactory() reactor.connectSSL(self.factory.host, self.factory.port, self.factory, contextFactory) else: reactor.connectTCP(self.factory.host, self.factory.port, self.factory) else: self.handleStatusDefault() self.factory.noPage( failure.Failure( error.PageRedirect(self.status, self.message, location=url))) self.quietLoss = 1 self.transport.loseConnection()
def test_messageExistsLocationExists(self): """ If a C{message} argument is passed to the L{PageRedirect} constructor, the C{message} isn't affected by the value of C{status}. """ e = error.PageRedirect(b"200", b"My own message", location=b"/foo") self.assertEqual(e.message, b"My own message to /foo")
def test_noMessageValidStatus(self): """ If no C{message} argument is passed to the L{PageRedirect} constructor and the C{code} argument is a valid HTTP status code, C{code} is mapped to a descriptive string to which C{message} is assigned. """ e = error.PageRedirect(b"200", location=b"/outputFile") self.assertEqual(e.message, b"OK to /outputFile")
def test_messageExistsNoLocation(self): """ If a C{message} argument is passed to the L{PageRedirect} constructor and no location is provided, C{message} doesn't try to include the empty location. """ e = error.PageRedirect(b"200", b"My own message") self.assertEqual(e.message, b"My own message")
def cb_process_resp(body, response): # Emulate HTTPClientFactory and raise t.w.e.Error # and PageRedirect if we have errors. if response.code > 299 and response.code < 400: raise tw_error.PageRedirect(response.code, body) elif response.code > 399: raise tw_error.Error(response.code, body) return body
def test_noMessageValidStatusNoLocation(self): """ If no C{message} argument is passed to the L{PageRedirect} constructor and C{location} is also empty and the C{code} argument is a valid HTTP status code, C{code} is mapped to a descriptive string to which C{message} is assigned without trying to include an empty location. """ e = error.PageRedirect(b"200") self.assertEqual(e.message, b"OK")
def cb_process_resp(body, response): # twisted.web.error imports reactor from twisted.web import error as tw_error # Emulate HTTPClientFactory and raise t.w.e.Error # and PageRedirect if we have errors. if response.code > 299 and response.code < 400: raise tw_error.PageRedirect(response.code, body) elif response.code > 399: raise tw_error.Error(response.code, body) return body
def _handleResponse(self, response, method, uri, headers, redirectCount): """ Handle the response, making another request if it indicates a redirect. """ if response.code in self._redirectResponses: if method not in ('GET', 'HEAD'): err = error.PageRedirect(response.code, location=uri) raise ResponseFailed([failure.Failure(err)], response) return self._handleRedirect(response, method, uri, headers, redirectCount) elif response.code in self._seeOtherResponses: return self._handleRedirect(response, 'GET', uri, headers, redirectCount) return response
def _handleResponse(self, response, method, uri, headers, redirectCount): """ Handle the response, making another request if it indicates a redirect. """ if response.code in (http.MOVED_PERMANENTLY, http.FOUND, http.TEMPORARY_REDIRECT): if method not in ('GET', 'HEAD'): err = error.PageRedirect(response.code, location=uri) raise ResponseFailed([failure.Failure(err)], response) return self._handleRedirect(response, method, uri, headers, redirectCount) elif response.code == http.SEE_OTHER: return self._handleRedirect(response, 'GET', uri, headers, redirectCount) return response
def cb_process_resp(body, response): if response.code == 404: raise NotFound(response.code, body) if response.code == 401: raise UnAuthenticated(response.code, body) if response.code == 403: raise UnAuthorized(response.code, body) if response.code == 409: raise Conflict(response.code, body) elif response.code > 299 and response.code < 400: raise error.PageRedirect(response.code, body) elif response.code > 399: raise RequestError(response.code, body) headers = {} for k, v in response.headers.getAllRawHeaders(): headers[k.lower()] = v.pop() response.headers = headers return response, body
def handleStatus_301(self): l = self.headers.get('location') if not l: self.handleStatusDefault() return url = l[0] if self.followRedirect: scheme, host, port, path = \ _parse(url, defaultPort=self.transport.getPeer().port) self.factory._redirectCount += 1 if self.factory._redirectCount >= self.factory.redirectLimit: err = error.InfiniteRedirection( self.status, 'Infinite redirection detected', location=url) self.factory.noPage(failure.Failure(err)) self.quietLoss = True self.transport.loseConnection() return self._completelyDone = False self.factory.setURL(url) if self.factory.scheme == 'https': from twisted.internet import ssl contextFactory = ssl.ClientContextFactory() reactor.connectSSL(self.factory.host, self.factory.port, self.factory, contextFactory) else: reactor.connectTCP(self.factory.host, self.factory.port, self.factory) else: self.handleStatusDefault() self.factory.noPage( failure.Failure( error.PageRedirect(self.status, self.message, location=url))) self.quietLoss = True self.transport.loseConnection()