Esempio n. 1
0
 def test_print_configuration(self):
     with patch('rhythmweb.conf.logging') as log:
         root = Mock()
         log.getLogger.return_value = root
         config = Configuration()
         config.parser.set('server', 'log.level', 'DEBUG')
         config.configure_logger()
         root.debug.assert_has_calls([
             call('Logger configured'),
             call('Showing app configuration:')])
Esempio n. 2
0
 def test_default_configuration(self):
     base_path = os.path.dirname(__file__)
     config_path = os.path.join(base_path, '..', 'cfg', 'rb-serve.conf')
     config = Configuration()
     self.assertEquals('default', config.get_string('theme'))
     self.assertEquals('touch', config.get_string('theme.mobile'))
     self.assertEquals('0.0.0.0', config.get_string('hostname'))
     self.assertEquals(7000, config.get_int('port'))
     self.assertEquals('/tmp/rb-serve.log', config.get_string('log.file'))
     self.assertEquals('%(levelname)s	%(asctime)s	%(name)s: %(message)s', config.get_string('log.format'))
     self.assertEquals('INFO', config.get_string('log.level'))
     self.assertEquals(False, config.get_boolean('debug'))
Esempio n. 3
0
 def __init__(self):
     self.config = Configuration()
     self.cgi_server = None
     self.is_running = False
     self._watch_id = None
Esempio n. 4
0
class Server(object):

    def __init__(self):
        self.config = Configuration()
        self.cgi_server = None
        self.is_running = False
        self._watch_id = None

    def start(self):
        log.info('   STARTING SERVER')
        hostname = self.config.get_string('hostname')
        port = self.config.get_int('port')
        self.cgi_server = make_server(
            hostname,
            port,
            self.handle_request,
            handler_class=WSGIRequestHandler)
        self._watch_id = GObject.io_add_watch(
            self.cgi_server.socket,
            GObject.IO_IN,
            self.io_watch_handle_request)
        self.is_running = True
        log.info('   CGI SERVER STARTED')

    def stop(self):
        if self.cgi_server:
            log.info('   STOPPING CGI SERVER')
            GObject.source_remove(self._watch_id)
            self.cgi_server.server_close()
        self.cgi_server = None
        self.is_running = False
        log.info('   SERVER STOPPED')

    def io_watch_handle_request(self, source, cb_condition):
        log.debug('Handling request')
        if not self.is_running:
            log.fatal('NOT RUNNING')
            return False
        self.cgi_server.handle_request()
        return True

    def handle_request(self, environ, response):
        method = environ.get('REQUEST_METHOD', 'GET')
        path = environ.get('PATH_INFO', '/')
        if path == '/':
            path = '/index.html'
        agent = environ.get('HTTP_USER_AGENT', '')
        group = 'mobile' if match_mobile.match(agent) else 'default'
        response = Response(response)
        log.debug('Handling request {} {} for agent {} ({})'.format(method, path, agent, group))
        try:
            if method == 'GET':
                content = app.get_file(path, group)
                if content:
                    return response.reply_with_file(path, content)
                content = app.route(path)

            if method == 'POST':
                post = self.parse_post_parameters(environ)
                content = app.route(path, **post)

            if content is None:
                return response.reply_with_not_found()
            return response.reply_with_json(content)

        except ValueError as e:
            return response.reply_with_client_error(e)

        except TypeError:
            return response.reply_with_method_not_allowed(method)

        except ServerError as e:
            return response.reply_with_server_error(e)

    def parse_post_parameters(self, environ):
        parsed = cgi.FieldStorage(
            fp=environ['wsgi.input'],
            environ=environ,
            keep_blank_values=True
        )
        post = {}
        for key in parsed.keys():
            post[key] = parsed[key].value
        return post