示例#1
0
    def testInitURLMapping(self):
        """Initializes the url/script map."""
        class TestOptions:
            login_url = '/_ah/login'
            logout_url = '/_ah/logout'

        url_mapping = typhoonae.initURLMapping(self.conf, TestOptions())
        for pattern, handler_path, path, login_required, admin_only in url_mapping:
            if pattern.match('/foo'):
                self.assertEqual(handler_path, 'app.py')
示例#2
0
    def testInitURLMapping(self):
        """Initializes the url/script map."""

        class TestOptions:
            login_url = '/_ah/login'
            logout_url = '/_ah/logout'

        url_mapping = typhoonae.initURLMapping(self.conf, TestOptions())
        for pattern, handler_path, path, login_required, admin_only in url_mapping:
            if pattern.match('/foo'):
                self.assertEqual(handler_path, 'app.py')
示例#3
0
def serve(conf, options):
    """Implements the server loop.

    Args:
        conf: The application configuration.
        options: Command line options.
    """

    # Inititalize URL mapping
    url_mapping = typhoonae.initURLMapping(conf, options)

    while True:
        try:
            (inp, out, unused_err, env) = fcgiapp.Accept()
        except:
            raise FastCGIException()

        # Initialize application environment
        os_env = dict(os.environ)
        os.environ.clear()
        os.environ.update(env)
        os.environ['APPENGINE_RUNTIME'] = conf.runtime
        os.environ['APPLICATION_ID'] = conf.application
        os.environ['CURRENT_VERSION_ID'] = (options.current_version_id
                                            or conf.version + ".1")
        os.environ['AUTH_DOMAIN'] = options.auth_domain
        os.environ['SERVER_SOFTWARE'] = options.server_software
        os.environ['SCRIPT_NAME'] = ''
        os.environ['TZ'] = 'UTC'
        os.environ['USER'] = '******'

        # Get user info and set the user environment variables
        email, admin, user_id = typhoonae.handlers.login.getUserInfo(
            os.environ.get('HTTP_COOKIE', None))
        os.environ['USER_EMAIL'] = email
        os.environ['USER_ID'] = user_id
        os.environ['REQUEST_ID_HASH'] = ''  # TODO use appropriate value
        if admin:
            os.environ['USER_IS_ADMIN'] = '1'
        else:
            os.environ['USER_IS_ADMIN'] = '0'
        nickname = None
        if email:
            nickname = email.split('@')[0]
        os.environ['USER_NICKNAME'] = nickname or ''
        os.environ['USER_ORGANIZATION'] = ''

        # CGI handler chain
        cgi_handler_chain = CGIHandlerChain(
            typhoonae.blobstore.handlers.UploadCGIHandler(
                upload_url=options.upload_url))

        # Redirect standard input and output streams
        sys.stdin = cgi_handler_chain(CGIInAdapter(inp), os.environ)
        sys.stdout = CGIOutAdapter(out)

        # Compute script path and set PATH_TRANSLATED environment variable
        path_info = os.environ['PATH_INFO']
        for pattern, handler_path, script, login_required, admin_only in url_mapping:
            if re.match(pattern, path_info) is not None:
                os.environ['PATH_TRANSLATED'] = script
                break

        http_auth = os.environ.get('HTTP_AUTHORIZATION', False)

        internal = os.environ.get('X-TyphoonAE-Secret') == 'secret'

        try:
            if http_auth and not email and not internal:
                match = re.match(BASIC_AUTH_PATTERN, http_auth)
                if match:
                    user, pw = base64.b64decode(match.group(1)).split(':')
                    print('Status: 301 Permanently Moved')
                    print(
                        'Set-Cookie: ' +
                        typhoonae.handlers.login.getSetCookieHeaderValue(
                            user, admin=True))
                    print('Location: %s\r\n' % os.environ['REQUEST_URI'])
            elif ((login_required or admin_only) and not email and not internal
                  and not options.debug_mode):
                print('Status: 302 Requires login')
                print('Location: %s\r\n' %
                      google.appengine.api.users.create_login_url(path_info))
            else:
                # Load and run the application module
                run_module(handler_path, script)
                if options.debug_mode:
                    return
        except Exception, e:
            # Handle all exceptions and write the traceback to the log
            logging.error(e, exc_info=sys.exc_info())
            print('Status: 500 Internal Server Error')
            print('Content-Type: text/plain')
            print('Content-Length: 22')
            print
            print('Internal Server Error')
        finally:
示例#4
0
def serve(conf, options):
    """Implements the server loop.

    Args:
        conf: The application configuration.
        options: Command line options.
    """

    # Inititalize URL mapping
    url_mapping = typhoonae.initURLMapping(conf, options)

    while True:
        try:
            (inp, out, unused_err, env) = fcgiapp.Accept()
        except:
            raise FastCGIException()

        # Initialize application environment
        os_env = dict(os.environ)
        os.environ.clear()
        os.environ.update(env)
        os.environ['APPENGINE_RUNTIME'] = conf.runtime
        os.environ['APPLICATION_ID'] = conf.application
        os.environ['CURRENT_VERSION_ID'] = (options.current_version_id
                                            or conf.version + ".1")
        os.environ['AUTH_DOMAIN'] = options.auth_domain
        os.environ['SERVER_SOFTWARE'] = options.server_software
        os.environ['SCRIPT_NAME'] = ''
        os.environ['TZ'] = 'UTC'
        os.environ['USER'] = '******'

        # Get user info and set the user environment variables
        email, admin, user_id = typhoonae.handlers.login.getUserInfo(
            os.environ.get('HTTP_COOKIE', None))
        os.environ['USER_EMAIL'] = email
        os.environ['USER_ID'] = user_id
        os.environ['REQUEST_ID_HASH'] = ''    # TODO use appropriate value
        if admin:
            os.environ['USER_IS_ADMIN'] = '1'
        else:
            os.environ['USER_IS_ADMIN'] = '0'
        nickname = None
        if email:
            nickname = email.split('@')[0]
        os.environ['USER_NICKNAME'] = nickname or ''
        os.environ['USER_ORGANIZATION'] = ''

        # CGI handler chain
        cgi_handler_chain = CGIHandlerChain(
            typhoonae.blobstore.handlers.UploadCGIHandler(
                upload_url=options.upload_url))

        # Redirect standard input and output streams
        sys.stdin = cgi_handler_chain(CGIInAdapter(inp), os.environ)
        sys.stdout = CGIOutAdapter(out)

        # Compute script path and set PATH_TRANSLATED environment variable
        path_info = os.environ['PATH_INFO']
        for pattern, handler_path, script, login_required, admin_only in url_mapping:
            if re.match(pattern, path_info) is not None:
                os.environ['PATH_TRANSLATED'] = script
                break

        http_auth = os.environ.get('HTTP_AUTHORIZATION', False)

        internal = os.environ.get('X-TyphoonAE-Secret') == 'secret'

        try:
            if http_auth and not email and not internal:
                match = re.match(BASIC_AUTH_PATTERN, http_auth)
                if match:
                    user, pw = base64.b64decode(match.group(1)).split(':')
                    print('Status: 301 Permanently Moved')
                    print('Set-Cookie: ' + typhoonae.handlers.login.
                          getSetCookieHeaderValue(user, admin=True))
                    print('Location: %s\r\n' % os.environ['REQUEST_URI'])
            elif ((login_required or admin_only) and not email
                    and not internal and not options.debug_mode):
                print('Status: 302 Requires login')
                print('Location: %s\r\n' %
                      google.appengine.api.users.create_login_url(path_info))
            else:
                # Load and run the application module
                run_module(handler_path, script)
                if options.debug_mode:
                    return
        except Exception, e:
            # Handle all exceptions and write the traceback to the log
            logging.error(e, exc_info=sys.exc_info())
            print('Status: 500 Internal Server Error')
            print('Content-Type: text/plain')
            print('Content-Length: 22')
            print
            print('Internal Server Error')
        finally: