def request(self, method, uri, headers=None, data=None): if data is not None: if headers is None: headers = Headers() headers.removeHeader('Content-Type') headers.addRawHeader('Content-Type', 'application/json; charset=utf-8') return BasicAgent.request(self, method, uri, headers, data)
def download_request(self, request): from twisted.internet import reactor timeout = request.meta.get('download_timeout') or self._connectTimeout agent = self._get_agent(request, timeout) # request details url = urldefrag(request.url)[0] method = to_bytes(request.method) headers = TxHeaders(request.headers) if isinstance(agent, self._TunnelingAgent): headers.removeHeader(b'Proxy-Authorization') if request.body: bodyproducer = _RequestBodyProducer(request.body) else: bodyproducer = None start_time = time() d = agent.request(method, to_bytes(url, encoding='ascii'), headers, bodyproducer) # set download latency d.addCallback(self._cb_latency, request, start_time) # response body is ready to be consumed d.addCallback(self._cb_bodyready, request) d.addCallback(self._cb_bodydone, request, url) # check download timeout self._timeout_cl = reactor.callLater(timeout, d.cancel) d.addBoth(self._cb_timeout, request, url, timeout) return d
def test_removeHeaderDoesntExist(self): """ L{Headers.removeHeader} is a no-operation when the specified header is not found. """ h = Headers() h.removeHeader(b"test") self.assertEqual(list(h.getAllRawHeaders()), [])
def test_removeHeaderDoesntExist(self): """ L{Headers.removeHeader} is a no-operation when the specified header is not found. """ h = Headers() h.removeHeader("test") self.assertEqual(list(h.getAllRawHeaders()), [])
def test_removeHeader(self): """ Check that L{Headers.removeHeader} removes the given header. """ h = Headers() h.setRawHeaders(b"foo", [b"lemur"]) self.assertTrue(h.hasHeader(b"foo")) h.removeHeader(b"foo") self.assertFalse(h.hasHeader(b"foo")) h.setRawHeaders(b"bar", [b"panda"]) self.assertTrue(h.hasHeader(b"bar")) h.removeHeader(b"Bar") self.assertFalse(h.hasHeader(b"bar"))
def test_removeHeader(self): """ Check that L{Headers.removeHeader} removes the given header. """ h = Headers() h.setRawHeaders("foo", ["lemur"]) self.assertTrue(h.hasHeader("foo")) h.removeHeader("foo") self.assertFalse(h.hasHeader("foo")) h.setRawHeaders("bar", ["panda"]) self.assertTrue(h.hasHeader("bar")) h.removeHeader("Bar") self.assertFalse(h.hasHeader("bar"))
def download_request(self, request): from twisted.internet import reactor timeout = request.meta.get('download_timeout') or self._connectTimeout agent = self._get_agent(request, timeout) # request details url = urldefrag(request.url)[0] method = to_bytes(request.method) headers = TxHeaders(request.headers) if isinstance(agent, self._TunnelingAgent): headers.removeHeader(b'Proxy-Authorization') if request.body: bodyproducer = _RequestBodyProducer(request.body) elif method == b'POST': # Setting Content-Length: 0 even for POST requests is not a # MUST per HTTP RFCs, but it's common behavior, and some # servers require this, otherwise returning HTTP 411 Length required # # RFC 7230#section-3.3.2: # "a Content-Length header field is normally sent in a POST # request even when the value is 0 (indicating an empty payload body)." # # Twisted < 17 will not add "Content-Length: 0" by itself; # Twisted >= 17 fixes this; # Using a producer with an empty-string sends `0` as Content-Length # for all versions of Twisted. bodyproducer = _RequestBodyProducer(b'') else: bodyproducer = None start_time = time() d = agent.request(method, to_bytes(url, encoding='ascii'), headers, bodyproducer) # set download latency d.addCallback(self._cb_latency, request, start_time) # response body is ready to be consumed d.addCallback(self._cb_bodyready, request) d.addCallback(self._cb_bodydone, request, url) # check download timeout self._timeout_cl = reactor.callLater(timeout, d.cancel) d.addBoth(self._cb_timeout, request, url, timeout) return d
def download_request(self, request): timeout = request.meta.get('download_timeout') or self._connectTimeout agent = self._get_agent(request, timeout) # request details url = urldefrag(request.url)[0] method = to_bytes(request.method) headers = TxHeaders(request.headers) if isinstance(agent, self._TunnelingAgent): headers.removeHeader(b'Proxy-Authorization') if request.body: bodyproducer = _RequestBodyProducer(request.body) elif method == b'POST': # Setting Content-Length: 0 even for POST requests is not a # MUST per HTTP RFCs, but it's common behavior, and some # servers require this, otherwise returning HTTP 411 Length required # # RFC 7230#section-3.3.2: # "a Content-Length header field is normally sent in a POST # request even when the value is 0 (indicating an empty payload body)." # # Twisted < 17 will not add "Content-Length: 0" by itself; # Twisted >= 17 fixes this; # Using a producer with an empty-string sends `0` as Content-Length # for all versions of Twisted. bodyproducer = _RequestBodyProducer(b'') else: bodyproducer = None start_time = time() d = agent.request( method, to_bytes(url, encoding='ascii'), headers, bodyproducer) # set download latency d.addCallback(self._cb_latency, request, start_time) # response body is ready to be consumed d.addCallback(self._cb_bodyready, request) d.addCallback(self._cb_bodydone, request, url) # check download timeout self._timeout_cl = reactor.callLater(timeout, d.cancel) d.addBoth(self._cb_timeout, request, url, timeout) return d
def download_request(self, request): timeout = request.meta.get('download_timeout') or self._connectTimeout agent = self._get_agent(request, timeout) # request details url = urldefrag(request.url)[0] method = request.method headers = TxHeaders(request.headers) if isinstance(agent, self._TunnelingAgent): headers.removeHeader('Proxy-Authorization') bodyproducer = _RequestBodyProducer(request.body) if request.body else None start_time = time() d = agent.request(method, url, headers, bodyproducer) # set download latency d.addCallback(self._cb_latency, request, start_time) # response body is ready to be consumed d.addCallback(self._cb_bodyready, request) d.addCallback(self._cb_bodydone, request, url) # check download timeout self._timeout_cl = reactor.callLater(timeout, d.cancel) d.addBoth(self._cb_timeout, request, url, timeout) return d