Python源码示例:twisted.web.proxy.ProxyClient()
示例1
def makeProxyClient(self, request, method=b"GET", headers=None,
requestBody=b""):
"""
Make a L{ProxyClient} object used for testing.
@param request: The request to use.
@param method: The HTTP method to use, GET by default.
@param headers: The HTTP headers to use expressed as a dict. If not
provided, defaults to {'accept': 'text/html'}.
@param requestBody: The body of the request. Defaults to the empty
string.
@return: A L{ProxyClient}
"""
if headers is None:
headers = {b"accept": b"text/html"}
path = b'/' + request.postpath
return ProxyClient(
method, path, b'HTTP/1.0', headers, requestBody, request)
示例2
def assertForwardsHeaders(self, proxyClient, requestLine, headers):
"""
Assert that C{proxyClient} sends C{headers} when it connects.
@param proxyClient: A L{ProxyClient}.
@param requestLine: The request line we expect to be sent.
@param headers: A dict of headers we expect to be sent.
@return: If the assertion is successful, return the request body as
bytes.
"""
self.connectProxy(proxyClient)
requestContent = proxyClient.transport.value()
receivedLine, receivedHeaders, body = self._parseOutHeaders(
requestContent)
self.assertEqual(receivedLine, requestLine)
self.assertEqual(receivedHeaders, headers)
return body
示例3
def makeProxyClient(self, request, method=b"GET", headers=None,
requestBody=b""):
"""
Make a L{ProxyClient} object used for testing.
@param request: The request to use.
@param method: The HTTP method to use, GET by default.
@param headers: The HTTP headers to use expressed as a dict. If not
provided, defaults to {'accept': 'text/html'}.
@param requestBody: The body of the request. Defaults to the empty
string.
@return: A L{ProxyClient}
"""
if headers is None:
headers = {b"accept": b"text/html"}
path = b'/' + request.postpath
return ProxyClient(
method, path, b'HTTP/1.0', headers, requestBody, request)
示例4
def assertForwardsHeaders(self, proxyClient, requestLine, headers):
"""
Assert that C{proxyClient} sends C{headers} when it connects.
@param proxyClient: A L{ProxyClient}.
@param requestLine: The request line we expect to be sent.
@param headers: A dict of headers we expect to be sent.
@return: If the assertion is successful, return the request body as
bytes.
"""
self.connectProxy(proxyClient)
requestContent = proxyClient.transport.value()
receivedLine, receivedHeaders, body = self._parseOutHeaders(
requestContent)
self.assertEqual(receivedLine, requestLine)
self.assertEqual(receivedHeaders, headers)
return body
示例5
def makeProxyClient(self, request, method="GET", headers=None,
requestBody=""):
"""
Make a L{ProxyClient} object used for testing.
@param request: The request to use.
@param method: The HTTP method to use, GET by default.
@param headers: The HTTP headers to use expressed as a dict. If not
provided, defaults to {'accept': 'text/html'}.
@param requestBody: The body of the request. Defaults to the empty
string.
@return: A L{ProxyClient}
"""
if headers is None:
headers = {"accept": "text/html"}
path = '/' + request.postpath
return ProxyClient(
method, path, 'HTTP/1.0', headers, requestBody, request)
示例6
def assertForwardsHeaders(self, proxyClient, requestLine, headers):
"""
Assert that C{proxyClient} sends C{headers} when it connects.
@param proxyClient: A L{ProxyClient}.
@param requestLine: The request line we expect to be sent.
@param headers: A dict of headers we expect to be sent.
@return: If the assertion is successful, return the request body as
bytes.
"""
self.connectProxy(proxyClient)
requestContent = proxyClient.transport.value()
receivedLine, receivedHeaders, body = self._parseOutHeaders(
requestContent)
self.assertEquals(receivedLine, requestLine)
self.assertEquals(receivedHeaders, headers)
return body
示例7
def connectProxy(self, proxyClient):
"""
Connect a proxy client to a L{StringTransportWithDisconnection}.
@param proxyClient: A L{ProxyClient}.
@return: The L{StringTransportWithDisconnection}.
"""
clientTransport = StringTransportWithDisconnection()
clientTransport.protocol = proxyClient
proxyClient.makeConnection(clientTransport)
return clientTransport
示例8
def test_forward(self):
"""
When connected to the server, L{ProxyClient} should send the saved
request, with modifications of the headers, and then forward the result
to the parent request.
"""
return self._testDataForward(
200, b"OK", [(b"Foo", [b"bar", b"baz"])], b"Some data\r\n")
示例9
def test_headersCleanups(self):
"""
The headers given at initialization should be modified:
B{proxy-connection} should be removed if present, and B{connection}
should be added.
"""
client = ProxyClient(b'GET', b'/foo', b'HTTP/1.0',
{b"accept": b"text/html", b"proxy-connection": b"foo"}, b'', None)
self.assertEqual(client.headers,
{b"accept": b"text/html", b"connection": b"close"})
示例10
def test_buildProtocol(self):
"""
L{ProxyClientFactory.buildProtocol} should produce a L{ProxyClient}
with the same values of attributes (with updates on the headers).
"""
factory = ProxyClientFactory(b'GET', b'/foo', b'HTTP/1.0',
{b"accept": b"text/html"}, b'Some data',
None)
proto = factory.buildProtocol(None)
self.assertIsInstance(proto, ProxyClient)
self.assertEqual(proto.command, b'GET')
self.assertEqual(proto.rest, b'/foo')
self.assertEqual(proto.data, b'Some data')
self.assertEqual(proto.headers,
{b"accept": b"text/html", b"connection": b"close"})
示例11
def __init__(self, *args, **kwargs):
self.intercept = {'song': b'/eapi/v3/song/detail/', 'search': b'/eapi/cloudsearch/pc', 'url': b'/eapi/song/enhance/player/url', 'album': b'/eapi/v1/album', 'artist': b'/eapi/v1/artist', 'playlist': b'/eapi/v3/playlist/detail', 'discovery': b'/eapi/v1/discovery/new/songs', 'linux': b'/api/linux/forward'}
self.interval = {self.intercept['song']: 10, self.intercept['search']: 100, 'default': 10}
self.temp_buffer = {self.intercept['song']: None, self.intercept['search']: None}
self.timestamp = {self.intercept['song']: time.time(), self.intercept['search']: time.time()}
proxy.ProxyClient.__init__(self, *args, **kwargs)
示例12
def handleResponsePart(self, buffer):
if self.rest in [self.intercept['song'], self.intercept['search'], self.intercept['playlist'], self.intercept['discovery'], self.intercept['linux']] or self.intercept['album'] in self.rest or self.intercept['artist'] in self.rest:
print(self.headers)
if self.headers[b'content-length'] != mainland_proxy.url_request_length:
print('response intercepted: ', self.rest)
if self.rest not in self.timestamp:
self.timestamp[self.rest] = time.time()
self.temp_buffer[self.rest] = None
if time.time() - self.timestamp[self.rest] > self.interval.get(self.rest, self.interval['default']):
self.temp_buffer[self.rest] = 0
self.timestamp[self.rest] = time.time()
if self.temp_buffer[self.rest] != None:
buffer = self.temp_buffer[self.rest] + buffer
buffer_str = sh_gzip_decompress(buffer)
if buffer_str == None or b'unexpected end of file' in buffer_str:
self.temp_buffer[self.rest] = buffer
self.timestamp[self.rest] = time.time()
return
else:
del self.temp_buffer[self.rest]
del self.timestamp[self.rest]
buffer_str = modify_response(buffer_str)
#print buffer_str
buffer = sh_gzip_compress(buffer_str)
if self.rest == self.intercept['url']:
self.check_buffer(buffer)
proxy.ProxyClient.handleResponsePart(self, buffer)
示例13
def handleResponsePart(self, buffer):
#Buffer the output if we intend to modify it
if self.father.has_response_modifiers():
self.father.response_buffer.write(buffer)
else:
proxy.ProxyClient.handleResponsePart(self, buffer)
示例14
def handleResponseEnd(self):
#Process the buffered output if we are modifying it
if self.father.has_response_modifiers():
if not self._finished:
#Replace the StringIO with a string for the modifiers
data = self.father.response_buffer.getvalue()
self.father.response_buffer.close()
self.father.response_buffer = data
#Do editing of response headers / content here
self.father.run_response_modifiers()
self.father.responseHeaders.setRawHeaders('content-length', [len(self.father.response_buffer)])
self.father.write(self.father.response_buffer)
proxy.ProxyClient.handleResponseEnd(self)
示例15
def connectProxy(self, proxyClient):
"""
Connect a proxy client to a L{StringTransportWithDisconnection}.
@param proxyClient: A L{ProxyClient}.
@return: The L{StringTransportWithDisconnection}.
"""
clientTransport = StringTransportWithDisconnection()
clientTransport.protocol = proxyClient
proxyClient.makeConnection(clientTransport)
return clientTransport
示例16
def test_forward(self):
"""
When connected to the server, L{ProxyClient} should send the saved
request, with modifications of the headers, and then forward the result
to the parent request.
"""
return self._testDataForward(
200, b"OK", [(b"Foo", [b"bar", b"baz"])], b"Some data\r\n")
示例17
def test_headersCleanups(self):
"""
The headers given at initialization should be modified:
B{proxy-connection} should be removed if present, and B{connection}
should be added.
"""
client = ProxyClient(b'GET', b'/foo', b'HTTP/1.0',
{b"accept": b"text/html", b"proxy-connection": b"foo"}, b'', None)
self.assertEqual(client.headers,
{b"accept": b"text/html", b"connection": b"close"})
示例18
def test_buildProtocol(self):
"""
L{ProxyClientFactory.buildProtocol} should produce a L{ProxyClient}
with the same values of attributes (with updates on the headers).
"""
factory = ProxyClientFactory(b'GET', b'/foo', b'HTTP/1.0',
{b"accept": b"text/html"}, b'Some data',
None)
proto = factory.buildProtocol(None)
self.assertIsInstance(proto, ProxyClient)
self.assertEqual(proto.command, b'GET')
self.assertEqual(proto.rest, b'/foo')
self.assertEqual(proto.data, b'Some data')
self.assertEqual(proto.headers,
{b"accept": b"text/html", b"connection": b"close"})
示例19
def handleHeader(self, key, value):
proxy.ProxyClient.handleHeader(self, key, value)
if key.lower() == b'content-type':
if value.decode().split(';')[0] == 'text/html':
self.parser = WordParser()
示例20
def handleResponsePart(self, buffer):
proxy.ProxyClient.handleResponsePart(self, buffer)
if hasattr(self, 'parser'):
self.parser.feed(buffer.decode())
self.father.wordCounter.addWords(self.parser.getWords())
del self.parser
示例21
def handleHeader(self, key, value):
if (key.lower() == "content-type" and
value == "application/vnd.docker.raw-stream"):
self._handleRawStream()
return proxy.ProxyClient.handleHeader(self, key, value)
示例22
def handleResponsePart(self, buffer):
"""
If we're not in streaming mode, buffer the response part(s).
"""
if self._streaming:
proxy.ProxyClient.handleResponsePart(self, buffer)
else:
self._responsePartBuffer += buffer
示例23
def handleResponseEnd(self):
"""
If we're completing a chunked response, up-call to handle it like a
regular reverse proxy.
If we're completing a non-chunked response, fire the post-hooks.
If we're completing a hijacked response, pass through the connection
close.
"""
if self.http:
if self._streaming:
return proxy.ProxyClient.handleResponseEnd(self)
else:
contentType = self.father.responseHeaders.getRawHeaders("content-type")
if contentType:
contentType = contentType[0]
else:
contentType = None
body = self._responsePartBuffer
self._fireListener(
{"PowerstripProtocolVersion": 1,
"ModifiedServerResponse":
{"Body": body,
"Code": self.father.code,
"ContentType": contentType}})
else:
self.father.transport.loseConnection()
示例24
def rawDataReceived(self, data):
if self.http:
return proxy.ProxyClient.rawDataReceived(self, data)
self.father.transport.write(data)
示例25
def connectProxy(self, proxyClient):
"""
Connect a proxy client to a L{StringTransportWithDisconnection}.
@param proxyClient: A L{ProxyClient}.
@return: The L{StringTransportWithDisconnection}.
"""
clientTransport = StringTransportWithDisconnection()
clientTransport.protocol = proxyClient
proxyClient.makeConnection(clientTransport)
return clientTransport
示例26
def test_forward(self):
"""
When connected to the server, L{ProxyClient} should send the saved
request, with modifications of the headers, and then forward the result
to the parent request.
"""
return self._testDataForward(
200, "OK", [("Foo", ["bar", "baz"])], "Some data\r\n")
示例27
def test_headersCleanups(self):
"""
The headers given at initialization should be modified:
B{proxy-connection} should be removed if present, and B{connection}
should be added.
"""
client = ProxyClient('GET', '/foo', 'HTTP/1.0',
{"accept": "text/html", "proxy-connection": "foo"}, '', None)
self.assertEquals(client.headers,
{"accept": "text/html", "connection": "close"})
示例28
def test_buildProtocol(self):
"""
L{ProxyClientFactory.buildProtocol} should produce a L{ProxyClient}
with the same values of attributes (with updates on the headers).
"""
factory = ProxyClientFactory('GET', '/foo', 'HTTP/1.0',
{"accept": "text/html"}, 'Some data',
None)
proto = factory.buildProtocol(None)
self.assertIsInstance(proto, ProxyClient)
self.assertEquals(proto.command, 'GET')
self.assertEquals(proto.rest, '/foo')
self.assertEquals(proto.data, 'Some data')
self.assertEquals(proto.headers,
{"accept": "text/html", "connection": "close"})