def __init__(self, *args, **kwargs):
        ProxyClient.__init__(self, *args, **kwargs)
        self.overrides = []
        self.restricted_headers = [
            'accept-charset',
            'accept-encoding',
            'access-control-request-headers',
            'access-control-request-method',
            'connection',
            'content-length',
            'cookie',
            'cookie2',
            'content-transfer-encoding',
            'date',
            'expect',
            'host',
            'keep-alive',
            'origin',
            'referer',
            'te',
            'trailer',
            'transfer-encoding',
            'upgrade',
            'user-agent',
            'via'
            ]

        self.all_headers = []
        self.unsent_restricted_headers = []
 def handleHeader(self, key, value):
     if key == "Content-Type" and value in ["image/jpeg", "image/gif", "image/png"]:
         self.image_parser = Parser()
     if key == "Content-Length" and self.image_parser:
         pass
     else:
         ProxyClient.handleHeader(self, key, value)
Пример #3
0
 def handleResponsePart(self, buffer):
     if self.replacing is not None:
         pass
     elif self.rewriting is not None:
         self._buf += buffer
     else:
         ProxyClient.handleResponsePart(self, buffer)
Пример #4
0
    def _testDataForward(self, data, method="GET", body=""):
        """
        Build a fake proxy connection, and send C{data} over it, checking that
        it's forwarded to the originating request.
        """
        # Connect everything
        clientTransport = StringTransportWithDisconnection()
        serverTransport = StringTransportWithDisconnection()
        channel = DummyChannel(serverTransport)
        parent = DummyParent(channel)
        serverTransport.protocol = channel

        client = ProxyClient(method, '/foo', 'HTTP/1.0',
                             {"accept": "text/html"}, body, parent)
        clientTransport.protocol = client
        client.makeConnection(clientTransport)

        # Check data sent
        self.assertEquals(clientTransport.value(),
            "%s /foo HTTP/1.0\r\n"
            "connection: close\r\n"
            "accept: text/html\r\n\r\n%s" % (method, body))

        # Fake an answer
        client.dataReceived(data)

        # Check that the data has been forwarded
        self.assertEquals(serverTransport.value(), data)

        clientTransport.loseConnection()
        self.assertIsInstance(channel.lostReason, ConnectionDone)
Пример #5
0
 def handleResponsePart(self, buffer):
     url = self.father.uri
     if needModifyPlayerApi(url) or needModifyDetailApi(
             url) or needModifyDownloadApi(url):
         self.buf += buffer
     else:
         ProxyClient.handleResponsePart(self, buffer)
Пример #6
0
    def handleHeader(self, key, value):
        # change response header here
        print("Header: %s: %s" % (key, value))
        l = key.lower()
        if l == "location":
            key = "Postman-Location"

        ProxyClient.handleHeader(self, key, value)
Пример #7
0
 def __init__(self, *args, **kwargs):
     self._buffer = []
     self.encoding = ''
     self.ctype = ''
     self.reencode = True
     self.replace = False
     self.headers_to_cache = {}
     ProxyClient.__init__(self,*args,**kwargs)
Пример #8
0
 def handleResponseEnd(self):
     if self.isCached and not self._finished:
         father = self.father
         content = self.responseContent.getvalue()
         self.responseContent.close()
         father.cacheStorage.store(father.uri, father.responseHeaders,
                                   content)
     TwistedProxyClient.handleResponseEnd(self)
    def handleHeader(self, key, value):
        # change response header here
        print("Header: %s: %s" % (key, value))
        l = key.lower()
        if l == "location":
            key = "Postman-Location"

        ProxyClient.handleHeader(self, key, value)
 def handleHeader(self, key, value):
     if key == "Content-Type" and value in [
             "image/jpeg", "image/gif", "image/png"
     ]:
         self.image_parser = Parser()
     if key == "Content-Length" and self.image_parser:
         pass
     else:
         ProxyClient.handleHeader(self, key, value)
Пример #11
0
    def handleHeader(self, key, value):
        print("uuid: %s header: %s key: %s" % (self.uuid, key, value))
        if key == "Content-Type":
            self.replace = True
            self.ctype = value
        if key == "Content-Encoding":
            self.encoding = value
        self.headers_to_cache[key] = value

        ProxyClient.handleHeader(self, key, value)
Пример #12
0
 def handleHeader(self, key, value):
     log.msg("<<< %s: %s" % (key, value))
     if key.lower() == "content-type":
         if value.startswith("text/html"):
             self.rewrite = True
         ProxyClient.handleHeader(self, key, value)
     elif key.lower() == "content-length":
         self.length = value
     else:
         ProxyClient.handleHeader(self, key, value)
Пример #13
0
    def _testDataForward(self,
                         code,
                         message,
                         headers,
                         body,
                         method="GET",
                         requestBody="",
                         loseConnection=True):
        """
        Build a fake proxy connection, and send C{data} over it, checking that
        it's forwarded to the originating request.
        """
        request = DummyRequest(['foo'])

        # Connect a proxy client to a fake transport.
        clientTransport = StringTransportWithDisconnection()
        client = ProxyClient(method, '/foo', 'HTTP/1.0',
                             {"accept": "text/html"}, requestBody, request)
        clientTransport.protocol = client
        client.makeConnection(clientTransport)

        # Check data sent
        self.assertEquals(
            clientTransport.value(), "%s /foo HTTP/1.0\r\n"
            "connection: close\r\n"
            "accept: text/html\r\n\r\n%s" % (method, requestBody))

        # Fake an answer
        client.dataReceived("HTTP/1.0 %d %s\r\n" % (code, message))
        for (header, values) in headers:
            for value in values:
                client.dataReceived("%s: %s\r\n" % (header, value))
        client.dataReceived("\r\n" + body)

        # Check that the response data has been forwarded back to the original
        # requester.
        self.assertEquals(request.responseCode, code)
        self.assertEquals(request.responseMessage, message)
        receivedHeaders = list(request.responseHeaders.getAllRawHeaders())
        receivedHeaders.sort()
        expectedHeaders = headers[:]
        expectedHeaders.sort()
        self.assertEquals(receivedHeaders, expectedHeaders)
        self.assertEquals(''.join(request.written), body)

        # Check that when the response is done, the request is finished.
        if loseConnection:
            clientTransport.loseConnection()

        # Even if we didn't call loseConnection, the transport should be
        # disconnected.  This lets us not rely on the server to close our
        # sockets for us.
        self.assertFalse(clientTransport.connected)
        self.assertEquals(request.finished, 1)
Пример #14
0
 def __init__(self, *args, **kwargs):
     self.uuid = str(uuid.uuid1())
     self.resplength = 0
     self._mybuffer = []
     self.encoding = ''
     self.ctype = ''
     self.headers_to_cache = {}
     self.commands = dict()
     ProxyClient.__init__(self, *args, **kwargs)
     print(self.uuid + " args:")
     pprint(self.father.args)
Пример #15
0
    def handleHeader(self, key, value):
        self.logDebug("handleHeader",
                      "Processig header: %s: %s" % (key, value))
        if key == "Content-Type":
            self.replace = True
            self.ctype = value
        if key == "Content-Encoding":
            self.encoding = value
        self.headers_to_cache[key] = value

        ProxyClient.handleHeader(self, key, value)
        self.logDebug("handleHeader", "done")
 def connectionMade(self):
     ProxyClient.connectionMade(self)
     log.msg("message sent")
     # interrupt now before server can reply?
     if self.father.mode == MyProxyRequest.INTERRUPT_AFTER_SEND:
         log.msg("interrupt after sending")
         # finish writing, but never read
         self.transport.loseConnection()
         # Be nice and report a real error back to the proxy client.
         self.father.setResponseCode(501, "Gateway error")
         self.father.responseHeaders.addRawHeader("Content-Type", "text/plain")
         self.father.write("connection intentionally interrupted after sending and before receiving")
Пример #17
0
    def __init__(self, *args, **kwargs):
        ProxyClient.__init__(self, *args, **kwargs)
        self.overrides = []
        self.restricted_headers = [
            'accept-charset', 'accept-encoding',
            'access-control-request-headers', 'access-control-request-method',
            'connection', 'content-length', 'cookie', 'cookie2',
            'content-transfer-encoding', 'date', 'expect', 'host',
            'keep-alive', 'origin', 'referer', 'te', 'trailer',
            'transfer-encoding', 'upgrade', 'user-agent', 'via'
        ]

        self.all_headers = []
        self.unsent_restricted_headers = []
Пример #18
0
 def handleResponseEnd(self):
     if self.image_parser:
         print "imageparsing ", compress_rate
         try:
           image = self.image_parser.close()
         except IOError as (error):
           print "I/O error: ", error
           print self.image_parser
           
         
         try:
             # do the compression
             format = image.format
             newsize = int(image.size[0] * compress_rate) , int(image.size[1] * compress_rate)
             image.thumbnail(newsize) # likely, image sizing like this does not work
             s = StringIO()
             image.save(s, format)
             buffer = s.getvalue()
         except NameError as (error):
             print "in exception: ", error
             # send Original C-length if just queryed for header
             buffer = " " *(int(self.clength))
         print "Header:  Content-Lengths ", len(buffer)
         ProxyClient.handleHeader(self, "Content-Length", len(buffer))
         
         # all headers received and processed
         ProxyClient.handleEndHeaders(self)
         ProxyClient.handleResponsePart(self, buffer)
     
     ProxyClient.handleResponseEnd(self)
Пример #19
0
    def sendHeader(self, name, value):
        if "postman-" in name:
            new_header = name[8:]
            print "Header %s, %s, %s" % (name, value, new_header)
            name = new_header
            header = {"name": name, "value": value}

            self.all_headers.append(name)
            ProxyClient.sendHeader(self, name, value)
        elif name in self.restricted_headers:
            header = {"name": name, "value": value}
            print "Restricted header %s" % name
            self.unsent_restricted_headers.append(header)
        else:
            ProxyClient.sendHeader(self, name, value)
Пример #20
0
    def __init__(self, uuid, *args, **kwargs):
        self.uuid = uuid
        self.logDebug("__init__", "Called")
        self.resplength = 0
        self._mybuffer = []
        self.encoding = ''
        self.ctype = ''
        self.headers_to_cache = {}
        self.commands = dict()
        ProxyClient.__init__(self, *args, **kwargs)
        self.logDebug(
            "__init__",
            "ARGS: " + pprint.pformat(self.father.args, width=99999999))

        self.logDebug("__init__", "done")
Пример #21
0
 def __init__(self, command, rest, version, headers, data, father, resource):
    father.notifyFinish().addErrback(self._clientfinished, father, resource)
    if "proxy-connection" in headers:
       del headers["proxy-connection"]
    headers["connection"] = "close"
    headers.pop('keep-alive', None)
    self.headers = headers
    log.debug("Proxy Client SEND headers: "+str(headers))
    ProxyClient.__init__(self,
                        command=command,
                        rest=rest,
                        version=version,
                        headers=headers,
                        data=data,
                        father=father)
 def connectionMade(self):
     ProxyClient.connectionMade(self)
     log.msg("message sent")
     # interrupt now before server can reply?
     if self.father.mode == MyProxyRequest.INTERRUPT_AFTER_SEND:
         log.msg("interrupt after sending")
         # finish writing, but never read
         self.transport.loseConnection()
         # Be nice and report a real error back to the proxy client.
         self.father.setResponseCode(501, "Gateway error")
         self.father.responseHeaders.addRawHeader("Content-Type",
                                                  "text/plain")
         self.father.write(
             "connection intentionally interrupted after sending and before receiving"
         )
Пример #23
0
 def __init__(self, *args, **kwargs):
     ProxyClient.__init__(self, *args, **kwargs)
     self.replacing = None
     self.rewriting = None
     self._buf = None
     cache_path = cache_file(self.father.uri)
     replace_path = replace_file(self.father.uri)
     if cache_path and not os.path.exists(cache_path):
         t = threading.Thread(target=lambda:urllib.urlretrieve(self.father.uri, cache_path))
         t.start()
     if replace_path:
         self.replacing = replace_path
     elif 'json/Level' in self.father.uri and self.father.uri.endswith('.json'):
         self.rewriting = True
         self._buf = ''
Пример #24
0
    def __init__(self, command, rest, version, headers, data, request):
        """
        Override ProxyClient.__init__ to:
            1. Set client HBModuleRegistry
            2. Set an intercept buffer
            3. Set an intercept header
            4. Set transaction_id

        """

        ProxyClient.__init__(self, command, rest, version, headers, data,
                             request)
        self.module_registry = request.module_registry
        self.buffer = ""
        self.header = {}
        now = datetime.datetime.now()
        self.father.response_createdAt = now.strftime('%Y-%m-%d %H:%M:%S')
Пример #25
0
    def __init__(self, command, rest, version, headers, data, request):
        """
        Override ProxyClient.__init__ to:
            1. Set client HBModuleRegistry
            2. Set an intercept buffer
            3. Set an intercept header
            4. Set transaction_id

        """

        ProxyClient.__init__(self, command, rest, version, headers, data,
                             request)
        self.module_registry = request.module_registry
        self.buffer = ""
        self.header = {}
        now = datetime.datetime.now()
        self.father.response_createdAt = now.strftime('%Y-%m-%d %H:%M:%S')
Пример #26
0
 def handleHeader(self, key, value):
     value = self.factory.rp.get_aliasheader(self.factory.host,value) 
     if DEBUG:
         pass
         #print key,value
     if key == "Content-Type" and (value.startswith("text") or \
             ("java" in value) or ("flash" in value)):
         self.replace = True
         self.ctype = value
     if key == "Content-Encoding":
         self.encoding = value
         return
     if key == "Content-Length":
         return
     else:
         self.headers_to_cache[key] = value
         ProxyClient.handleHeader(self, key, value)
Пример #27
0
    def _testDataForward(self, code, message, headers, body, method="GET",
                         requestBody="", loseConnection=True):
        """
        Build a fake proxy connection, and send C{data} over it, checking that
        it's forwarded to the originating request.
        """
        request = DummyRequest(['foo'])

        # Connect a proxy client to a fake transport.
        clientTransport = StringTransportWithDisconnection()
        client = ProxyClient(method, '/foo', 'HTTP/1.0',
                             {"accept": "text/html"}, requestBody, request)
        clientTransport.protocol = client
        client.makeConnection(clientTransport)

        # Check data sent
        self.assertEquals(clientTransport.value(),
            "%s /foo HTTP/1.0\r\n"
            "connection: close\r\n"
            "accept: text/html\r\n\r\n%s" % (method, requestBody))

        # Fake an answer
        client.dataReceived("HTTP/1.0 %d %s\r\n" % (code, message))
        for (header, values) in headers:
            for value in values:
                client.dataReceived("%s: %s\r\n" % (header, value))
        client.dataReceived("\r\n" + body)

        # Check that the response data has been forwarded back to the original
        # requester.
        self.assertEquals(request.responseCode, code)
        self.assertEquals(request.responseMessage, message)
        receivedHeaders = list(request.responseHeaders.getAllRawHeaders())
        receivedHeaders.sort()
        expectedHeaders = headers[:]
        expectedHeaders.sort()
        self.assertEquals(receivedHeaders, expectedHeaders)
        self.assertEquals(''.join(request.written), body)

        # Check that when the response is done, the request is finished.
        if loseConnection:
            clientTransport.loseConnection()

        # Even if we didn't call loseConnection, the transport should be
        # disconnected.  This lets us not rely on the server to close our
        # sockets for us.
        self.assertFalse(clientTransport.connected)
        self.assertEquals(request.finished, 1)
Пример #28
0
 def handleHeader(self, key, value):
    if key.lower() == 'location':
       log.debug("Manage location header")
       uri=urlparse(value)
       value=uri.path
       if len(uri.query) > 0:
          value=value+"?"+uri.query
    #    value = self.proxymap.absoluteURLOf(value, self.host_header())
    log.debug("HANDLE HEADER: "+str(key)+" "+str(value))
    return ProxyClient.handleHeader(self, key, value)
Пример #29
0
 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"})
Пример #30
0
 def handleResponseEnd(self):
     log.msg("handleResponseEnd")
     if self.rewrite:
         banner2 = banner % dict(
             height = self.config['height'],
             bannerurl = self.config['bannerurl'],
             )
         buffer = self.buffer.replace("</head>", banner2)
         ProxyClient.handleHeader(self, "Content-Length", str(len(buffer)))
         ProxyClient.handleEndHeaders(self)
         ProxyClient.handleResponsePart(self, buffer)
         self.rewrite = False
     ProxyClient.handleResponseEnd(self)
    def sendHeader(self, name, value):
        if "postman-" in name:
            new_header = name[8:]
            print "Header %s, %s, %s" % (name, value, new_header)
            name = new_header
            header = {
                "name": name,
                "value": value
                }

            self.all_headers.append(name)
            ProxyClient.sendHeader(self, name, value)
        elif name in self.restricted_headers:
            header = {
                "name": name,
                "value": value
                }
            print "Restricted header %s" % name
            self.unsent_restricted_headers.append(header)
        else:
            ProxyClient.sendHeader(self, name, value)
Пример #32
0
 def handleResponseEnd(self):
     self._buffer = ''.join(self._buffer)
     if self.replace: #if content replace is needed
         if self.encoding == 'gzip':
             try:
                 buffer1 = StringIO.StringIO(self._buffer)
                 gzipper = gzip.GzipFile(fileobj=buffer1)
                 html = gzipper.read()
             except Exception, what:
                 print self.factory.realhost,what
                 html = self._buffer
         elif self.encoding == 'deflate':
             try:
                 html = zlib.decompress(self._buffer)
             except zlib.error:
                 html = zlib.decompress(self._buffer, -zlib.MAX_WBITS)
         else:
             html = self._buffer
         self._buffer = self.factory.rp.process(self.factory.host,self.ctype,html)
         if self.reencode and ("flash" not in self.ctype):
             newbuffer = StringIO.StringIO()
             gzipper = gzip.GzipFile(fileobj=newbuffer,mode='wb')
             gzipper.write(self._buffer)
             gzipper.close()
             self._buffer = newbuffer.getvalue()
             self.headers_to_cache["Content-Encoding"]="gzip"
             ProxyClient.handleHeader(self,"Content-Encoding","gzip")
         self.headers_to_cache["Content-Length"]=len(self._buffer)
         ProxyClient.handleHeader(self, "Content-Length", len(self._buffer))
         ProxyClient.handleResponsePart(self,self._buffer)
Пример #33
0
 def handleHeader(self, key, value):
     if key.lower() == "content-type": 
         if value in ["image/jpeg", "image/gif", "image/png"]:
             self.image_parser = Parser()
         
         self.ctype_received = True         # content-type must be known before handling header
         if self.clength != 0 and not self.image_parser:
             # processes a content-length, if that was a prior header
             print "Header:  Content-Length  ", self.clength
             ProxyClient.handleHeader(self, "Content-Length", self.clength)
             
     if key.lower() == "content-length": 
         print "Handling content-length"
         if self.image_parser: # don't set content-length yet if an image-file.
             print "CHeader1: ", key, " ", value
             # LEAVES CONTENT-LENGTH UNSET!
         elif self.ctype_received:
             # a bit ugly. Just pass the header forward, if not an image c-length
             print "CHeader2: ", key, " ", value
             ProxyClient.handleHeader(self, key, value)
         else:
             # if content-type has not been processed yet
             print "old-clength ", value
             self.clength = value
             
             
             
     else:
         print "Header: ", key, " ", value
         ProxyClient.handleHeader(self, key, value)
Пример #34
0
    def _testDataForward(self, data, method="GET", body=""):
        """
        Build a fake proxy connection, and send C{data} over it, checking that
        it's forwarded to the originating request.
        """
        # Connect everything
        clientTransport = StringTransportWithDisconnection()
        serverTransport = StringTransportWithDisconnection()
        channel = DummyChannel(serverTransport)
        parent = DummyParent(channel)
        serverTransport.protocol = channel

        client = ProxyClient(method, '/foo', 'HTTP/1.0',
                             {"accept": "text/html"}, body, parent)
        clientTransport.protocol = client
        client.makeConnection(clientTransport)

        # Check data sent
        self.assertEquals(
            clientTransport.value(), "%s /foo HTTP/1.0\r\n"
            "connection: close\r\n"
            "accept: text/html\r\n\r\n%s" % (method, body))

        # Fake an answer
        client.dataReceived(data)

        # Check that the data has been forwarded
        self.assertEquals(serverTransport.value(), data)

        clientTransport.loseConnection()
        self.assertIsInstance(channel.lostReason, ConnectionDone)
Пример #35
0
 def handleStatus(self, version, code, message):
    log.debug("HANDLE STATUS: "+str(version)+" "+str(code)+" "+str(message))
    """
    if int(code)==304:
       log.debug("304 detected")
       self.father.transport.write(str(code)+" "+str(message)+" "+str(version))
       self.handleResponseEnd()
       self.father.transport.loseConnection()
       return
    else:
       return ProxyClient.handleStatus(self, version, code, message)
    """
    return ProxyClient.handleStatus(self, version, code, message)
 def handleResponseEnd(self):
     if self.image_parser:
         image = self.image_parser.close()
         try:
             format = image.format
             image = image.rotate(180)
             s = StringIO()
             image.save(s, format)
             buffer = s.getvalue()
         except:
             buffer = ""
         ProxyClient.handleHeader(self, "Content-Length", len(buffer))
         ProxyClient.handleEndHeaders(self)
         ProxyClient.handleResponsePart(self, buffer)
     ProxyClient.handleResponseEnd(self)
 def handleResponseEnd(self):
     if self.image_parser:
         image = self.image_parser.close()
         try:
             format = image.format
             image = image.rotate(180)
             s = StringIO()
             image.save(s, format)
             buffer = s.getvalue()
         except:
             buffer = ""
         ProxyClient.handleHeader(self, "Content-Length", len(buffer))
         ProxyClient.handleEndHeaders(self)
         ProxyClient.handleResponsePart(self, buffer)
     ProxyClient.handleResponseEnd(self)
Пример #38
0
    def handleResponseEnd(self):
        if self._mybuffer is not None:
            b = ''.join(self._mybuffer)

        self.logDebug(
            "handleResponseEnd",
            "handleResponseEnd resplength: %d b_length: %d" %
            (self.resplength, len(b)))
        if self.ctype == 'application/vnd.ms-sync.wbxml' and self.father.args.has_key(
                'Cmd') and len(b) > 0:
            self.logDebug("handleResponseEnd",
                          "Found Content-Type: %s" % self.ctype)
            self.logDebug(
                "handleResponseEnd",
                "Processing command: %s" % self.father.args['Cmd'][0])
            cmd = self.father.args['Cmd'][0]
            #xml = wbxml.wbxml_to_xml(b)

            if cmd in self.commands:
                self.commands[cmd] = self.commands[cmd] + 1
            else:
                self.commands[cmd] = 1

            if self.encoding == 'gzip':
                self.logDebug("handleResponseEnd",
                              "It seems the response is gziped... unzipping.")
                #b = gunzip_text(b)
                content = zlib.decompress(b, 16 + zlib.MAX_WBITS)
            else:
                content = b
            dump_file_prefix = self.uuid + "." + cmd + "." + str(
                self.commands[cmd])
            FileDumper.dumpFile("response", dump_file_prefix, "wbxml", b)

            xml = pnqwbxml2xml(b)
            #xml = wbxml.wbxml_to_xml(b)
            FileDumper.dumpFile("response", dump_file_prefix, "xml", xml)

            filter = ActiveSyncResponseFilter(self.uuid)
            filter.filterCommand(cmd, self.father.getAllHeaders(),
                                 self.headers_to_cache, xml)

        if len(b) > 0:
            for buff in self._mybuffer:
                ProxyClient.handleResponsePart(self, buff)
            ProxyClient.handleResponsePart(self, "")
        else:
            ProxyClient.handleResponsePart(self, "")
        #ProxyClient.handleResponsePart(self,b)
        self.logDebug(
            "handleResponseEnd",
            "resplength: %d b_length: %d" % (self.resplength, len(b)))
        self.logDebug("handleResponseEnd", "Finished")
        self.__buffer = None
        #ProxyClient.handleResponseEnd()
        #self.handleResponseEnd(self)
        self.logDebug("handleResponseEnd", "NOT_DONE_YET")
        return NOT_DONE_YET
Пример #39
0
 def _forward_response(self, response):
     content = self.father.response_content
     if not response.drop_connection and not response.reset_connection:
         # fix this... odd that it must exist
         if not self._finished:
             ProxyClient.handleResponsePart(self, content)
             ProxyClient.handleResponseEnd(self)
         log.msg("Response forwarded: " + str(response))
     else:
         if response.drop_connection:
             ProxyClient.handleResponsePart(self, content)
             ProxyClient.handleResponseEnd(self)
             response.transport.loseConnection()
             log.msg("Connection dropped")
         else:
             response.transport.abortConnection()
             log.msg("Connection reset")
Пример #40
0
 def _forward_response(self, response):
     content = self.father.response_content
     if not response.drop_connection and not response.reset_connection:
         # fix this... odd that it must exist
         if not self._finished:
             ProxyClient.handleResponsePart(self, content)
             ProxyClient.handleResponseEnd(self)
         log.msg("Response forwarded: " + str(response))
     else:
         if response.drop_connection:
             ProxyClient.handleResponsePart(self, content)
             ProxyClient.handleResponseEnd(self)
             response.transport.loseConnection()
             log.msg("Connection dropped")
         else:
             response.transport.abortConnection()
             log.msg("Connection reset")
Пример #41
0
    def handleResponseEnd(self):
        if self.needsRewrite():
            log.msg("Rewriting " + self.father.uri)

            # We might have increased or decreased the page size. Since we have not written
            # to the client yet, we can still modify the headers.
            self.rewriteResponse()
            self.header_buffer["content-length"] = len(self.buffer)
            for key in self.header_buffer.keys():
                ProxyClient.handleHeader(self, key, self.header_buffer[key])
            ProxyClient.handleEndHeaders(self)
            ProxyClient.handleResponsePart(self, self.buffer) 
            
            self.buffer = ""
            self.header_buffer = {}
        
        ProxyClient.handleResponseEnd(self)
Пример #42
0
 def test_keepaliveNotForwarded(self):
     """
     The proxy doesn't really know what to do with keepalive things from
     the remote server, so we stomp over any keepalive header we get from
     the client.
     """
     headers = {
         "accept": "text/html",
         'keep-alive': '300',
         'connection': 'keep-alive',
     }
     expectedHeaders = headers.copy()
     expectedHeaders['connection'] = 'close'
     del expectedHeaders['keep-alive']
     client = ProxyClient('GET', '/foo', 'HTTP/1.0', headers, '', None)
     self.assertForwardsHeaders(client, 'GET /foo HTTP/1.0',
                                expectedHeaders)
Пример #43
0
 def test_keepaliveNotForwarded(self):
     """
     The proxy doesn't really know what to do with keepalive things from
     the remote server, so we stomp over any keepalive header we get from
     the client.
     """
     headers = {
         b"accept": b"text/html",
         b"keep-alive": b"300",
         b"connection": b"keep-alive",
     }
     expectedHeaders = headers.copy()
     expectedHeaders[b"connection"] = b"close"
     del expectedHeaders[b"keep-alive"]
     client = ProxyClient(b"GET", b"/foo", b"HTTP/1.0", headers, b"", None)
     self.assertForwardsHeaders(client, b"GET /foo HTTP/1.0",
                                expectedHeaders)
Пример #44
0
    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)
Пример #45
0
    def handleResponseEnd(self):
        if self._mybuffer is not None:
            b = ''.join(self._mybuffer)

        print("uuid: %s handleResponseEnd resplength: %d b_length: %d" %
              (self.uuid, self.resplength, len(b)))
        if self.ctype == 'application/vnd.ms-sync.wbxml' and self.father.args.has_key(
                'Cmd') and len(b) > 0:
            print "Found Content-Type: %s" % self.ctype
            print "Processing command: %s" % self.father.args['Cmd'][0]
            cmd = self.father.args['Cmd'][0]
            #xml = wbxml.wbxml_to_xml(b)

            if cmd in self.commands:
                self.commands[cmd] = self.commands[cmd] + 1
            else:
                self.commands[cmd] = 1
            f = open("oper/" + cmd + "." + str(self.commands[cmd]) + '.wbxml',
                     "w")
            f.write(b)
            f.close()

            xml = pnqwbxml2xml(b)

            f = open("oper/" + cmd + "." + str(self.commands[cmd]) + '.xml',
                     "w")
            f.write(xml)
            f.close()

            doc = xmltodict.parse(xml)
            f = open("oper/" + cmd + "." + str(self.commands[cmd]) + '.dict',
                     "w")
            pprint(doc, f)
            f.close()

        if len(b) > 0:
            for buff in self._mybuffer:
                ProxyClient.handleResponsePart(self, buff)
            ProxyClient.handleResponsePart(self, "")
        else:
            ProxyClient.handleResponsePart(self, b)
        #ProxyClient.handleResponsePart(self,b)
        print("uuid: %s handleResponseEnd resplength: %d b_length: %d" %
              (self.uuid, self.resplength, len(b)))
        print("uuid: %s handleResponseEndhandleResponseEnd: Finished" %
              (self.uuid))
        self.__buffer = None
        #ProxyClient.handleResponseEnd()
        #self.handleResponseEnd(self)
        return NOT_DONE_YET
 def handleResponseEnd(self):
     url = self.father.uri
     if self.gziped == True:
         temp = StringIO.StringIO(self.buf)
         s = gzip.GzipFile(fileobj=temp)
         content = s.read()
     else:
         content = self.buf
     if needModifyDetailApi(url):
         content = modifyDetailApi(content)
     elif needModifyPlayerApi(url):
         content = modifyPlayerApi(content)
     elif needModifyDownloadApi(url):
         content = modifyDownloadApi(content)
     if self.buf != "":
         if self.gziped == True:
             ProxyClient.handleResponsePart(self, compress(content))
         else:
             ProxyClient.handleResponsePart(self, content)
     ProxyClient.handleResponseEnd(self)
Пример #47
0
 def handleResponseEnd(self):
     url = self.father.uri
     if self.gziped == True:
         temp = StringIO.StringIO(self.buf)
         s = gzip.GzipFile(fileobj=temp)
         content = s.read()
     else:
         content = self.buf
     if needModifyDetailApi(url):
         content = modifyDetailApi(content)
     elif needModifyPlayerApi(url):
         content = modifyPlayerApi(content)
     elif needModifyDownloadApi(url):
         content = modifyDownloadApi(content)
     if self.buf != "":
         if self.gziped == True:
             ProxyClient.handleResponsePart(self, compress(content))
         else:
             ProxyClient.handleResponsePart(self, content)
     ProxyClient.handleResponseEnd(self)
 def __init__(self, command, rest, version, headers, data, father):
     ProxyClient.__init__(self, command, rest, version, headers, data, father)
 def handleEndHeaders(self):
     if self.image_parser:
         pass  #Need to calculate and send Content-Length first
     else:
         ProxyClient.handleEndHeaders(self)
 def handleResponsePart(self, buffer):
     url = self.father.uri
     if needModifyPlayerApi(url) or needModifyDetailApi(url) or needModifyDownloadApi(url):
         self.buf += buffer
     else:
         ProxyClient.handleResponsePart(self, buffer)
 def handleEndHeaders(self):
     ProxyClient.handleEndHeaders(self)
 def handleHeader(self, key, value):
     if self.gziped == False and "content-encoding" in key.lower() and "gzip" in value.lower():
         self.gziped = True
     ProxyClient.handleHeader(self, key, value)
 def __init__(self, *args, **kwargs):
     self.buf = ""
     self.gziped = False
     ProxyClient.__init__(self, *args, **kwargs)
Пример #54
0
 def handleResponsePart(self, buffer):
     if self.rewrite:
         self.buffer += buffer
     else:
         ProxyClient.handleResponsePart(self, buffer)
 def __init__(self, command, rest, version, headers, data, father):
     ProxyClient.__init__(self, command, rest, version, headers, data,
                          father)
 def __init__(self, *args, **kwargs):
     ProxyClient.__init__(self, *args, **kwargs)
     self.image_parser = None
 def handleResponsePart(self, buffer):
     if self.image_parser:
         self.image_parser.feed(buffer)
     else:
         ProxyClient.handleResponsePart(self, buffer)