Example #1
0
    def test_empty(self):
        jsonstr = " "
        ff = filtermanager.parseJsonString(jsonstr, exitOnTestFailure=False)
        self.assertEqual(len(ff), 0)

        jsonstr = None
        ff = filtermanager.parseJsonString(jsonstr, exitOnTestFailure=False)
        self.assertEqual(len(ff), 0)
Example #2
0
    def test_empty(self):
        jsonstr = " "
        ff = filtermanager.parseJsonString(jsonstr, exitOnTestFailure=False)
        self.assert_(len(ff)==0)

        jsonstr = None
        ff = filtermanager.parseJsonString(jsonstr, exitOnTestFailure=False)
        self.assert_(len(ff)==0)
Example #3
0
 def test_failure(self):
     jsonstr = """{
        "callable": "",
        "apply_to": "file",
        "path": ".html",
        "settings": {"str": "http://127.0.0.1/assets/", "new": "http://cdn.someserver.com/"},
        "name": "String replacer example"}"""
     ff = filtermanager.parseJsonString(jsonstr, exitOnTestFailure=False)
     self.assertEqual(len(ff), 0)
Example #4
0
 def test_failure(self):
     jsonstr = """{
        "callable": "",
        "apply_to": "file",
        "path": ".html",
        "settings": {"str": "http://127.0.0.1/assets/", "new": "http://cdn.someserver.com/"},
        "name": "String replacer example"}"""
     ff = filtermanager.parseJsonString(jsonstr, exitOnTestFailure=False)
     self.assert_(len(ff)==0)
Example #5
0
 def test_single(self):
     jsonstr = """{
        "callable": "outpost.filterinc.replacestr",
        "apply_to": "file",
        "path": ".html",
        "settings": {"str": "http://127.0.0.1/assets/", "new": "http://cdn.someserver.com/"},
        "name": "String replacer example"}"""
     ff = filtermanager.parseJsonString(jsonstr, exitOnTestFailure=False)
     self.assertEqual(len(ff), 1)
     self.assertEqual(ff[0].name, "String replacer example")
Example #6
0
 def test_multiple(self):
     jsonstr = """[{
        "callable": "outpost.filterinc.replacestr",
        "apply_to": "file",
        "path": ".html",
        "settings": {"str": "http://127.0.0.1/assets/", "new": "http://cdn.someserver.com/"},
        "name": "String replacer example"},
        {
        "callable": "outpost.filterinc.replacestr",
        "apply_to": "file",
        "path": ".html",
        "settings": {"str": "http://127.0.0.1/assets/", "new": "http://cdn.someserver.com/"},
        "name": "2. String replacer example"}]"""
     ff = filtermanager.parseJsonString(jsonstr, exitOnTestFailure=False)
     self.assertEqual(len(ff), 2)
     self.assertEqual(ff[0].name, "String replacer example")
     self.assertEqual(ff[1].name, "2. String replacer example")
Example #7
0
def setup(global_config, **settings):
    """
    Parse ini file settings, setup defaults and register views

    :param global_config:
    :param settings:
    :return: returns pyramid configurator
    """
    log = logging.getLogger("outpost")

    fileroute=proxyroute = None
    debug = settings.get("debug")

    # parse filter
    fstr = settings.get("filter")
    settings["filter"] = filtermanager.parseJsonString(fstr, exitOnTestFailure=not debug)

    # set up local file directory
    directory = settings.get("files.directory")
    # bw 0.2.6 renamed ini file setting
    if directory is None:
        directory = settings.get("server.directory")
    if not directory:
        log.info("Local directory path empty ('files.directory'). File serving disabled.")
    else:
        # extend relative directory
        wd = os.getcwd()+os.sep
        if directory.startswith("."+os.sep):
            directory = wd + directory[2:]
        elif directory.find(":") == -1 and not directory.startswith(os.sep):
            directory = wd + directory
        settings["files.directory"] = directory

        fileroute = settings.get("files.route", "")
        if not fileroute.startswith("/"):
            fileroute = "/"+fileroute
        if not fileroute.endswith("/"):
            fileroute += "/"

        log.info("Serving files with path prefix '%s' from directory '%s'" % (fileroute, directory))

    # normalize default path
    path = settings.get("server.default_path", "")
    if path and not path.startswith("/"):
        settings["server.default_path"] = "/"+path

    # set up proxy routing
    host = settings.get("proxy.host")
    # bw 0.2.6 renamed ini file setting
    if host is None:
        host = settings.get("proxy.domain")
    if not host:
        log.info("Proxy target host empty ('proxy.host'). Request proxy disabled.")
    else:
        proxyroute = settings.get("proxy.route")
        if not proxyroute.startswith("/"):
            proxyroute = "/"+proxyroute
        if not proxyroute.endswith("/"):
            proxyroute += "/"
        log.info("Proxying requests with path prefix '%s' to '%s'", proxyroute, host)

    if directory and fileroute==proxyroute:
        raise filtermanager.ConfigurationError("File and proxy routing is equal.")

    # setup pyramid configuration and routes
    config = Configurator(settings = settings)

    # route proxy requests
    def regproxy(route):
        # handle all /proxy/... urls by the proxy server
        config.add_route("proxy", route+"*subpath")
        config.add_view(callProxy, route_name="proxy")

    # route the local directory
    def regfile(route):
        config.add_route("files", route+"*subpath")
        config.add_view(serveFile, route_name="files")

    # swap order of route registration to handle fallbacks
    fallback = settings.get("server.fallback")

    if proxyroute and fallback!="proxy":
        regproxy(proxyroute)

    if directory:
        regfile(fileroute)

    if proxyroute and fallback=="proxy":
        regproxy(proxyroute)

    config.commit()

    return config