Example #1
0
class Handler:

    def __init__(self, libraries, opts, testing=False, notify_changes=None):
        ctx = Context(libraries, opts, testing=testing, notify_changes=notify_changes)
        self.auth_controller = None
        if opts.auth:
            has_ssl = opts.ssl_certfile is not None and opts.ssl_keyfile is not None
            prefer_basic_auth = {'auto':has_ssl, 'basic':True}.get(opts.auth_mode, False)
            self.auth_controller = AuthController(
                user_credentials=ctx.user_manager, prefer_basic_auth=prefer_basic_auth, ban_time_in_minutes=opts.ban_for, ban_after=opts.ban_after)
        self.router = Router(ctx=ctx, url_prefix=opts.url_prefix, auth_controller=self.auth_controller)
        for module in SRV_MODULES:
            module = import_module('calibre.srv.' + module)
            self.router.load_routes(itervalues(vars(module)))
        self.router.finalize()
        self.router.ctx.url_for = self.router.url_for
        self.dispatch = self.router.dispatch

    def set_log(self, log):
        self.router.ctx.log = log
        if self.auth_controller is not None:
            self.auth_controller.log = log

    def set_jobs_manager(self, jobs_manager):
        self.router.ctx.jobs_manager = jobs_manager

    def close(self):
        self.router.ctx.library_broker.close()

    @property
    def ctx(self):
        return self.router.ctx
Example #2
0
class Handler(object):

    def __init__(self, libraries, opts, testing=False, notify_changes=None):
        ctx = Context(libraries, opts, testing=testing, notify_changes=notify_changes)
        self.auth_controller = None
        if opts.auth:
            has_ssl = opts.ssl_certfile is not None and opts.ssl_keyfile is not None
            prefer_basic_auth = {'auto':has_ssl, 'basic':True}.get(opts.auth_mode, False)
            self.auth_controller = AuthController(user_credentials=ctx.user_manager, prefer_basic_auth=prefer_basic_auth)
        self.router = Router(ctx=ctx, url_prefix=opts.url_prefix, auth_controller=self.auth_controller)
        for module in SRV_MODULES:
            module = import_module('calibre.srv.' + module)
            self.router.load_routes(vars(module).itervalues())
        self.router.finalize()
        self.router.ctx.url_for = self.router.url_for
        self.dispatch = self.router.dispatch

    def set_log(self, log):
        self.router.ctx.log = log
        if self.auth_controller is not None:
            self.auth_controller.log = log

    def set_jobs_manager(self, jobs_manager):
        self.router.ctx.jobs_manager = jobs_manager

    def close(self):
        self.router.ctx.library_broker.close()

    @property
    def ctx(self):
        return self.router.ctx
Example #3
0
class Handler(object):
    def __init__(self, libraries, opts, testing=False):
        ctx = Context(libraries, opts, testing=testing)
        self.auth_controller = None
        if opts.auth:
            has_ssl = opts.ssl_certfile is not None and opts.ssl_keyfile is not None
            prefer_basic_auth = {
                'auto': has_ssl,
                'basic': True
            }.get(opts.auth_mode, 'digest')
            self.auth_controller = AuthController(
                user_credentials=ctx.user_manager,
                prefer_basic_auth=prefer_basic_auth)
        self.router = Router(ctx=ctx,
                             url_prefix=opts.url_prefix,
                             auth_controller=self.auth_controller)
        for module in ('content', 'ajax', 'code'):
            module = import_module('calibre.srv.' + module)
            self.router.load_routes(vars(module).itervalues())
        self.router.finalize()
        self.router.ctx.url_for = self.router.url_for
        self.dispatch = self.router.dispatch

    def set_log(self, log):
        self.router.ctx.log = log
        if self.auth_controller is not None:
            self.auth_controller.log = log

    def close(self):
        self.router.ctx.library_broker.close()
Example #4
0
class Handler(object):

    def __init__(self, libraries, opts, testing=False):
        ctx = Context(libraries, opts, testing=testing)
        self.auth_controller = None
        if opts.auth:
            has_ssl = opts.ssl_certfile is not None and opts.ssl_keyfile is not None
            prefer_basic_auth = {'auto':has_ssl, 'basic':True}.get(opts.auth_mode, 'digest')
            self.auth_controller = AuthController(user_credentials=ctx.user_manager, prefer_basic_auth=prefer_basic_auth)
        self.router = Router(ctx=ctx, url_prefix=opts.url_prefix, auth_controller=self.auth_controller)
        for module in ('content', 'ajax', 'code', 'legacy', 'opds', 'books'):
            module = import_module('calibre.srv.' + module)
            self.router.load_routes(vars(module).itervalues())
        self.router.finalize()
        self.router.ctx.url_for = self.router.url_for
        self.dispatch = self.router.dispatch

    def set_log(self, log):
        self.router.ctx.log = log
        if self.auth_controller is not None:
            self.auth_controller.log = log

    def set_jobs_manager(self, jobs_manager):
        self.router.ctx.jobs_manager = jobs_manager

    def close(self):
        self.router.ctx.library_broker.close()
Example #5
0
    def test_route_finding(self):
        'Test route finding'
        from calibre.srv.routes import Router, endpoint, HTTPNotFound
        router = Router()

        def find(path):
            path = filter(None, path.split('/'))
            ep, args = router.find_route(path)
            args = list(args)
            return ep, args

        @endpoint('/')
        def root(ctx, data):
            pass

        @endpoint('/defval/{a=1}')
        def defval(ctx, data, a):
            pass

        @endpoint('/varpath/{a}/{b}')
        def varpath(ctx, data, a, b):
            pass

        @endpoint('/soak/{+rest}')
        def soak(ctx, dest, rest):
            pass

        @endpoint('/soak_opt/{+rest="xxx"}')
        def soak_opt(ctx, dest, rest):
            pass

        for x in locals().itervalues():
            if getattr(x, 'is_endpoint', False):
                router.add(x)
        router.finalize()

        ep, args = find('/')
        self.ae(ep, root), self.assertFalse(args)
        ep, args = find('/defval')
        self.ae(ep, defval), self.ae(args, [1])
        ep, args = find('/defval/2')
        self.ae(ep, defval), self.ae(args, [2])
        self.assertRaises(HTTPNotFound, find,
                          '/defval/a')  # a must be an integer
        self.assertRaises(HTTPNotFound, find, '/varpath')
        self.assertRaises(HTTPNotFound, find, '/varpath/x')
        self.assertRaises(HTTPNotFound, find, '/varpath/x/y/z')
        self.assertRaises(HTTPNotFound, find, '/soak')
        ep, args = find('/varpath/x/y')
        self.ae(ep, varpath), self.ae(args, ['x', 'y'])
        ep, args = find('/soak/x')
        self.ae(ep, soak), self.ae(args, ['x'])
        self.ae(router.routes['/soak'].soak_up_extra, 'rest')
        ep, args = find('/soak/x/y/z')
        self.ae(ep, soak), self.ae(args, ['x/y/z'])
        ep, args = find('/soak_opt')
        self.ae(ep, soak_opt), self.ae(args, ['xxx'])
        ep, args = find('/soak_opt/a/b')
        self.ae(ep, soak_opt), self.ae(args, ['a/b'])
Example #6
0
    def test_route_finding(self):
        'Test route finding'
        from calibre.srv.routes import Router, endpoint, HTTPNotFound
        router = Router()

        def find(path):
            path = filter(None, path.split('/'))
            ep, args = router.find_route(path)
            args = list(args)
            return ep, args

        @endpoint('/')
        def root(ctx, data):
            pass

        @endpoint('/defval/{a=1}')
        def defval(ctx, data, a):
            pass

        @endpoint('/varpath/{a}/{b}')
        def varpath(ctx, data, a, b):
            pass

        @endpoint('/soak/{+rest}')
        def soak(ctx, dest, rest):
            pass

        @endpoint('/soak_opt/{+rest="xxx"}')
        def soak_opt(ctx, dest, rest):
            pass

        for x in locals().itervalues():
            if getattr(x, 'is_endpoint', False):
                router.add(x)
        router.finalize()

        ep, args = find('/')
        self.ae(ep, root), self.assertFalse(args)
        ep, args = find('/defval')
        self.ae(ep, defval), self.ae(args, [1])
        ep, args = find('/defval/2')
        self.ae(ep, defval), self.ae(args, [2])
        self.assertRaises(HTTPNotFound, find, '/defval/a')  # a must be an integer
        self.assertRaises(HTTPNotFound, find, '/varpath')
        self.assertRaises(HTTPNotFound, find, '/varpath/x')
        self.assertRaises(HTTPNotFound, find, '/varpath/x/y/z')
        self.assertRaises(HTTPNotFound, find, '/soak')
        ep, args = find('/varpath/x/y')
        self.ae(ep, varpath), self.ae(args, ['x', 'y'])
        ep, args = find('/soak/x')
        self.ae(ep, soak), self.ae(args, ['x'])
        self.ae(router.routes['/soak'].soak_up_extra, 'rest')
        ep, args = find('/soak/x/y/z')
        self.ae(ep, soak), self.ae(args, ['x/y/z'])
        ep, args = find('/soak_opt')
        self.ae(ep, soak_opt), self.ae(args, ['xxx'])
        ep, args = find('/soak_opt/a/b')
        self.ae(ep, soak_opt), self.ae(args, ['a/b'])
Example #7
0
class Handler(object):

    def __init__(self, libraries, opts, testing=False):
        self.router = Router(ctx=Context(libraries, opts, testing=testing), url_prefix=opts.url_prefix)
        for module in ('content', 'ajax'):
            module = import_module('calibre.srv.' + module)
            self.router.load_routes(vars(module).itervalues())
        self.router.finalize()
        self.router.ctx.url_for = self.router.url_for
        self.dispatch = self.router.dispatch

    def set_log(self, log):
        self.router.ctx.log = log
Example #8
0
class Handler(object):

    def __init__(self, libraries, opts, testing=False):
        self.router = Router(ctx=Context(libraries, opts, testing=testing), url_prefix=opts.url_prefix)
        for module in ('content', 'ajax', 'code'):
            module = import_module('calibre.srv.' + module)
            self.router.load_routes(vars(module).itervalues())
        self.router.finalize()
        self.router.ctx.url_for = self.router.url_for
        self.dispatch = self.router.dispatch

    def set_log(self, log):
        self.router.ctx.log = log

    def close(self):
        self.router.ctx.library_broker.close()
Example #9
0
    def test_route_finding(self):
        "Test route finding"
        from calibre.srv.routes import Router, endpoint, HTTPNotFound

        router = Router()

        def find(path):
            path = filter(None, path.split("/"))
            ep, args = router.find_route(path)
            args = list(args)
            return ep, args

        @endpoint("/")
        def root(ctx, data):
            pass

        @endpoint("/defval/{a=1}")
        def defval(ctx, data, a):
            pass

        @endpoint("/varpath/{a}/{b}")
        def varpath(ctx, data, a, b):
            pass

        @endpoint("/soak/{+rest}")
        def soak(ctx, dest, rest):
            pass

        @endpoint('/soak_opt/{+rest="xxx"}')
        def soak_opt(ctx, dest, rest):
            pass

        @endpoint("/needs quoting/{x}")
        def quoting(ctx, dest, x):
            pass

        for x in locals().itervalues():
            if getattr(x, "is_endpoint", False):
                router.add(x)
        router.finalize()

        ep, args = find("/")
        self.ae(ep, root), self.assertFalse(args)
        ep, args = find("/defval")
        self.ae(ep, defval), self.ae(args, [1])
        ep, args = find("/defval/2")
        self.ae(ep, defval), self.ae(args, [2])
        self.assertRaises(HTTPNotFound, find, "/defval/a")  # a must be an integer
        self.assertRaises(HTTPNotFound, find, "/varpath")
        self.assertRaises(HTTPNotFound, find, "/varpath/x")
        self.assertRaises(HTTPNotFound, find, "/varpath/x/y/z")
        self.assertRaises(HTTPNotFound, find, "/soak")
        ep, args = find("/varpath/x/y")
        self.ae(ep, varpath), self.ae(args, ["x", "y"])
        ep, args = find("/soak/x")
        self.ae(ep, soak), self.ae(args, ["x"])
        self.ae(router.routes["/soak"].soak_up_extra, "rest")
        ep, args = find("/soak/x/y/z")
        self.ae(ep, soak), self.ae(args, ["x/y/z"])
        ep, args = find("/soak_opt")
        self.ae(ep, soak_opt), self.ae(args, ["xxx"])
        ep, args = find("/soak_opt/a/b")
        self.ae(ep, soak_opt), self.ae(args, ["a/b"])

        self.ae(router.url_for("/needs quoting", x="a/b c"), "/needs quoting/a%2Fb%20c")
        self.ae(router.url_for(None), "/")