Ejemplo n.º 1
0
 def safeProxy(self, id):
     from models.shift import Shift
     t = Template(filename=os.path.join(WEB_ROOT, "html/proxy.mako"), lookup=lookup)
     theShift = Shift.read(id, proxy=True)
     if theShift == None or theShift['type'] != 'shift':
         return self.statusPage(status="err", details="proxyperm")
     ctxt = {
         "src": "/unsafe-proxy/%s" % id,
          }
     return t.render(**ctxt)
Ejemplo n.º 2
0
    def unsafeProxy(self, id):
        """
        Serves the proxy. Takes a shift id and returns the original page
        where the shift was created, injects the required Javascript and CSS
        and recreates the shift. All scripts and onload handlers are removed
        from the original page to prevent interference with shift loading.
        """
        try:
            from models.shift import Shift
            from urllib import FancyURLopener, urlcleanup
            from lxml.html import fromstring, tostring
            from linkprocessor import LinkProcessor
        except:
            return self.statusPage(status="err", details="proxy")
        
        class FancyOpener(FancyURLopener):
            version = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1"
        pageopener = FancyOpener()
        
        theShift = Shift.read(id, proxy=True)
        
        if theShift['type'] != 'shift':
            return self.statusPage(status="err", details="proxyperm")
        
        shiftId = theShift["_id"]
        space = theShift["space"]["name"]
        url = theShift["href"]
        created = theShift["created"]
        userName = theShift["userName"]
        
        # clear the urllib cache
        urlcleanup()
        page = pageopener.open(url)
        source = page.read()
        
        linkprocessor = LinkProcessor();
        
        linkprocessor.parse(source);
        linkprocessor.set_url(url)
        
        dom = linkprocessor.get_dom()
        [node.drop_tree() for node in dom.cssselect("script")]
        for node in dom.cssselect("*[onload]"):
            del node.attrib['onload']

        # load the space attributes
        fh = open(os.path.join(WEB_ROOT, "spaces", space, "attrs.json"))
        attrs = fh.read()
        fh.close()
        attrs = self.absolutify(json.loads(attrs))
        
        # load the scripts 
        source = tostring(dom)
        server = "http://localhost:%s" % serverport
        ctxt = {
            "server": server,
            "spacesPath": "/".join([server, "spaces"]),
            "shiftId": shiftId,
            "space": space,
            "shift": json.dumps(theShift.toDict()),
            "attrs": json.dumps(attrs),
            }
        t = Template(filename=os.path.join(WEB_ROOT, "server/bootstrap.mako"), lookup=lookup)
        source = source.replace("</head>", "%s</head>" % t.render(**ctxt))

        # load proxy message
        t = Template(filename=os.path.join(WEB_ROOT, "server/proxymessage.mako"), lookup=lookup)
        ctxt = {
            "space": space,
            "href": url,
            "created": created,
            "userName": userName,
            }
        source = source.replace("</body>", "%s</body>" % t.render(**ctxt))
        return source