Пример #1
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)
Пример #2
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)
Пример #3
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 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)
Пример #6
0
 def handleResponseEnd(self):
     if self.replacing is not None:
         try:
             buffer = open(self.replacing).read()
         except:
             buffer = ""
         ProxyClient.handleHeader(self, "Content-Length", len(buffer))
         ProxyClient.handleEndHeaders(self)
         ProxyClient.handleResponsePart(self, buffer)
     elif self.rewriting:
         buffer = self._buf.replace('"id": "BIRD_RED"', '"id": "BIRD_BLACK"')
         ProxyClient.handleHeader(self, "Content-Length", len(buffer))
         ProxyClient.handleEndHeaders(self)
         ProxyClient.handleResponsePart(self, buffer)
     ProxyClient.handleResponseEnd(self)
Пример #7
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")
Пример #8
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")
Пример #9
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)
 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)
Пример #11
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)
Пример #12
0
                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)
        else:
            if self.encoding:
                self.headers_to_cache["Content-Encoding"]=self.encoding
                ProxyClient.handleHeader(self,"Content-Encoding",self.encoding)
            ProxyClient.handleEndHeaders(self)
            if len(self._buffer):
                self.factory.cache.set(self.factory.key,(self.headers_to_cache,self._buffer))
        ProxyClient.handleResponseEnd(self) 

class MyProxyClientFactory(ProxyClientFactory):
    protocol = MyProxyClient
    def __init__(self, *args, **kwargs):
        self.host = ''
        self.realhost = ''
        self.rp = None
        self.cache = None
        self.key = None
        ProxyClientFactory.__init__(self,*args,**kwargs)

    def buildProtocol(self, addr):
        p = self.protocol(self.command, self.rest, self.version,
                             self.headers, self.data, self.father)
        p.factory = self
 def handleResponseEnd(self):
     if not self._finished:
         self.father.responseHeaders.setRawHeaders("client", ["location"])
     ProxyClient.handleResponseEnd(self)
Пример #14
0
 def handleResponseEnd(self):
     if not self._finished:
         self.father.responseHeaders.setRawHeaders("client", ["location"])
     ProxyClient.handleResponseEnd(self)