"""quixote.publish

Logic for publishing modules and objects on the Web.
"""

__revision__ = "$Id: publish.py,v 1.135 2002/10/03 14:35:52 gward Exp $"

import sys, os, traceback, cStringIO
import time, types, socket, re
import cgi
import struct
try:
    import zlib # for COMPRESS_PAGES option
except ImportError:
    pass

from quixote import errors, fcgi
from quixote.http_request import HTTPRequest, get_content_type
from quixote.http_response import HTTPResponse

try:
    import cgitb                        # Only available in Python 2.2
except ImportError:
    cgitb = None

def _get_module (name):
    """Get a module object by name."""
    __import__(name)
    module = sys.modules[name]
    return module
    
# Error message to dispay when DISPLAY_EXCEPTIONS in config file is not
# true.  Note that SERVER_ADMIN must be fetched from the environment and
# plugged in here -- we can't do it now because the environment isn't
# really setup for us yet if running as a FastCGI script.
INTERNAL_ERROR_MESSAGE = """\
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>Internal Server Error</title></head>
<body>
<h1>Internal Server Error</h1>
<p>An internal error occurred while handling your request.</p>

<p>The server administrator should have been notified of the problem.
You may wish to contact the server administrator (%s) and inform them of
the time the error occurred, and anything you might have done to trigger
the error.</p>

<p>If you are the server administrator, more information may be
available in either the server's error log or Quixote's error log.</p>
</body>
</html>
"""

class Publisher:
    """
    The core of Quixote and of any Quixote application.  This class is
    responsible for converting each HTTP request into a search of
    Python's package namespace and, ultimately, a call of a Python
    function/method/callable object.

    Each invocation of a driver script should have one Publisher
    instance that lives for as long as the driver script itself.  Eg. if
    your driver script is plain CGI, each Publisher instance will handle
    exactly one HTTP request; if you have a FastCGI driver, then each
    Publisher will handle every HTTP request handed to that driver
    script process.

    Instance attributes:
      root_namespace : module | instance | class
        the Python namespace that will be searched for objects to
        fulfill each HTTP request
      exit_now : boolean
        used for internal state management.  If true, the loop in
        publish_cgi() will terminate at the end of the current request.
      access_log : file
        file to which every access will be logged; set by
        setup_logs() (None if no access log)
      error_log : file
        file to which application errors (exceptions caught by Quixote,
        as well as anything printed to stderr by application code) will
        be logged; set by setup_logs().  Set to sys.stderr if no
        ERROR_LOG setting in the application config file.
      config : Config
        holds all configuration info for this application.  If the
        application doesn't have a config file, uses the default values
        from the quixote.config module.
    """

    def __init__ (self, root_namespace, config=None):
        from quixote.config import Config
        global _publisher

        if _publisher is not None:
            raise RuntimeError, "only one instance of Publisher allowed"
        _publisher = self

        if type(root_namespace) is types.StringType:
            self.root_namespace = _get_module(root_namespace)
        else:
            # Should probably check that root_namespace is really a
            # namespace, ie. a module, class, or instance -- but it's
            # tricky to know if something is really a class or instance
            # (because of ExtensionClass), and who knows what other
            # namespaces are lurking out there in the world?
            self.root_namespace = root_namespace

        # for PublishError exception handling
        self.namespace_stack = [self.root_namespace]

        self.exit_now = 0
        self.access_log = None
        self.error_log = sys.stderr     # possibly overridden in setup_logs()
        sys.stdout = self.error_log     # print is handy for debugging

        # Initialize default config object with all the default values from
        # the config variables at the top of the config module, ie. if
        # ERROR_LOG is set to "/var/log/quxiote-error.log", then
        # config.ERROR_LOG will also be "/var/log/quixote-error.log".  If
        # application FCGI/CGI scripts need to override any of these
        # defaults, they can do so by direct manipulation of the config
        # object, or by reading a config file:
        #   app.read_config("myapp.conf")
        if config is None:
            self.config = Config()
        else:
            self.set_config(config)

    def configure (self, **kwargs):
        self.config.set_from_dict(kwargs)

    def read_config (self, filename):
        self.config.read_file(filename)

    def set_config (self, config):
        from quixote.config import Config
        if not isinstance(config, Config):
            raise TypeError, "'config' must be a Config instance"
        self.config = config

    def setup_logs (self):
        """
        Open all log files specified in the config file, and reassign
        sys.stdout and sys.stderr to go to the error log.  If the error
        log is not specified, redirect sys.stdout to sys.stderr.
        """

        if self.config.access_log is not None:
            try:
                self.access_log = open(self.config.access_log, 'a', 1)
            except IOError, exc:
                sys.stderr.write("error opening access log %s: %s\n" 
                                 % (`self.config.access_log`, exc.strerror))

        if self.config.error_log is not None:
            try:
                self.error_log = open(self.config.error_log, 'a', 1)
                sys.stderr = self.error_log
            except IOError, exc:
                # leave self.error_log as it was, most likely sys.stderr
                sys.stderr.write("error opening error log %s: %s\n"
                                 % (`self.config.error_log`, exc.strerror))

        if self.config.debug_log is not None:
            try:
                debug_log = open(self.config.debug_log, 'a', 1)
                sys.stdout = debug_log
            except IOError, exc:
                sys.stderr.write("error opening debug log %s: %s\n"
                                 % (`self.config.debug_log`, exc.strerror))
            

    def shutdown_logs (self):
        """
        Close log files and restore sys.stdout and sys.stderr to their
        original values.
        """
        if sys.stdout is sys.__stdout__:
            raise RuntimeError, "'setup_logs()' never called"
        if sys.stdout is not sys.stderr:
            sys.stdout.close()
        sys.stdout = sys.__stdout__
        self.access_log.close()
        if self.error_log is not sys.__stderr__:
            self.error_log.close()
            sys.stderr = sys.__stderr__

    def log (self, msg):
        """
        Write an message to the error log with a time stamp.
        """
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S",
                                  time.localtime(time.time()))
        self.error_log.write("[%s] %s\n" % (timestamp, msg))

    debug = log # backwards compatibility

    def create_request (self, stdin, env):
        ctype = get_content_type(env)
        if ctype == "multipart/form-data":
            from quixote.upload import HTTPUploadRequest
            req = HTTPUploadRequest(stdin, env, content_type=ctype)
            req.set_upload_dir(self.config.upload_dir,
                               self.config.upload_dir_mode)
            return req
        else:
            return HTTPRequest(stdin, env, content_type=ctype)

    def parse_request (self, request):
        """Parse the request information waiting in 'request'.
        """
        self.start_time = time.time()
        request.process_inputs()

    def start_request (self, request):
        """Called at the start of each request.  Overridden by
        SessionPublisher to handle session details.
        """
        pass

    def log_request (self, request):
        """Log a request in the access_log file.
        """
        if self.access_log is not None:
            if request.session:
                user = request.session.user or "-"
            else:
                user = "-"
            now = time.time()
            seconds = now - self.start_time
            now = time.strftime('%Y-%m-%d %H:%M:%S',
                                time.localtime(time.time()))

            env = request.environ
            method = env.get('REQUEST_METHOD')

            # Under Apache, REQUEST_URI == SCRIPT_NAME + PATH_INFO.
            # Not everyone uses Apache, so we have to stick to
            # environment variables in the CGI spec.  Note that this
            # relies on PATH_INFO under IIS being fixed by HTTPRequest,
            # because IIS gets it wrong.
            request_uri = env.get('SCRIPT_NAME') + env.get('PATH_INFO', '')
            query = env.get('QUERY_STRING', '')
            if query:
                query = "?" + query
            proto = env.get('SERVER_PROTOCOL')

            self.access_log.write('%s %s %s %d "%s %s %s" %s %r %0.2fsec\n' %
                                   (request.environ.get('REMOTE_ADDR'),
                                    str(user),
                                    now,
                                    os.getpid(),
                                    method,
                                    request_uri + query,
                                    proto,
                                    request.response.status_code,
                                    request.environ.get('HTTP_USER_AGENT', ''),
                                    seconds
                                   ))


    def finish_successful_request (self, request):
        """Called at the end of a successful request.  Overridden by
        SessionPublisher to handle session details."""
        pass

    def finish_interrupted_request (self, request, exc):
        """
        Called at the end of an interrupted request.  Requests are
        interrupted by raising a PublishError exception.  This method
        should return a string object which will be used as the result of
        the request.

        This method searches for the nearest namespace with a
        _q_exception_handler attribute.  That attribute is expected to be
        a function and is called with the request and exception instance
        as arguments and should return the error page (e.g. a string).  If
        the handler doesn't want to handle a particular error it can
        re-raise it and the next nearest handler will be found.  If no
        _q_exception_handler is found, the default Quixote handler is
        used.
        """

        # Throw away the existing response object and start a new one
        # for the error document we're going to create here.
        request.response = HTTPResponse()

        # set response status code so every custom doesn't have to do it
        request.response.set_status(exc.status_code)

        if self.config.secure_errors and exc.private_msg:
            exc.private_msg = None # hide it
            
        # walk up stack and find handler for the exception
        stack = self.namespace_stack[:]
        while 1:
            handler = None
            while stack:
                object = stack.pop()
                if hasattr(object, "_q_exception_handler"):
                    handler = object._q_exception_handler
                    break
            if handler is None:
                handler = errors.default_exception_handler
            
            try:
                return handler(request, exc)
            except errors.PublishError:
                assert handler is not errors.default_exception_handler
                continue # exception was re-raised or another exception occured


    def finish_failed_request (self, request):
        """
        Called at the end of an failed request.  Any exception (other
        than PublishError) causes a request to fail.  This method should
        return a string object which will be used as the result of the
        request.
        """
        # build new response to be safe
        original_response = request.response
        request.response = HTTPResponse()
        #self.log("caught an error (%s), reporting it." %
        #         sys.exc_info()[1])

        (exc_type, exc_value, tb) = sys.exc_info()
        error_summary = traceback.format_exception_only(exc_type, exc_value)
        error_summary = error_summary[0][0:-1] # de-listify and strip newline

        plain_error_msg = self._generate_plaintext_error(request,
                                                         original_response,
                                                         exc_type, exc_value,
                                                         tb)

        if not self.config.display_exceptions:
            # DISPLAY_EXCEPTIONS is false, so return the most
            # secure (and cryptic) page.
            request.response.set_header("Content-Type", "text/html")
            user_error_msg = self._generate_internal_error(request)
        elif self.config.display_exceptions == 'html' and cgitb is not None:
            # Generate a spiffy HTML display using cgitb
            request.response.set_header("Content-Type", "text/html")
            user_error_msg = self._generate_cgitb_error(request,
                                                        original_response,
                                                        exc_type, exc_value,
                                                        tb)
        else:
            # Generate a plaintext page containing the traceback
            request.response.set_header("Content-Type", "text/plain")
            user_error_msg = plain_error_msg

        self.log("exception caught")
        self.error_log.write(plain_error_msg)

        if self.config.error_email:
            self.mail_error(plain_error_msg, error_summary)

        request.response.set_status(500)
        return user_error_msg
        

    def _generate_internal_error (self, request):
        admin = request.environ.get('SERVER_ADMIN',
                                    "<i>email address unknown</i>")
        return INTERNAL_ERROR_MESSAGE % admin


    def _generate_plaintext_error (self, request, original_response,
                                   exc_type, exc_value, tb):
        error_file = cStringIO.StringIO()

        # format the traceback
        traceback.print_exception(exc_type, exc_value, tb, file=error_file)

        # include request and response dumps
        error_file.write('\n')
        error_file.write(request.dump())
        error_file.write('\n')

        return error_file.getvalue()

        
    def _generate_cgitb_error (self, request, original_response,
                               exc_type, exc_value, tb):
        error_file = cStringIO.StringIO()
        hook = cgitb.Hook(file=error_file)
        hook(exc_type, exc_value, tb)
        error_file.write('<h2>Original Request</h2>')
        error_file.write(request.dump_html())
        error_file.write('<h2>Original Response</h2><pre>')
        original_response.write(error_file)
        error_file.write('</pre>')
        return error_file.getvalue()

        
    def mail_error (self, msg, error_summary):
        """Send an email notifying someone of a traceback."""

        cmd = "/usr/lib/sendmail '%s'" % self.config.error_email
        pipe = os.popen(cmd, 'w')
        pipe.write('Subject: Quixote Traceback (%s)\n' % error_summary)
        pipe.write('From: Quixote on %s <%s>\n' % (socket.gethostname(),
                                                   self.config.error_email))
        pipe.write('To: %s\n' % self.config.error_email)
        pipe.write('\n')
        pipe.write(msg)
        pipe.close()

    def get_component (self, container, component, path, request):
        """Get one component of a path from a namespace.
        """
        # First security check: if the container doesn't even have an
        # _q_exports list, fail now: all Quixote-traversable namespaces
        # (modules, packages, instances) must have an export list!
        if not hasattr(container, '_q_exports'):
            raise errors.TraversalError(
                        private_msg="%r has no _q_exports list" % container)

        # Second security check: call _q_access function if it's present.
        if hasattr(container, '_q_access'):
            # will raise AccessError if access failed
            container._q_access(request)
            
        # Third security check: make sure the current name component is
        # in the export list or is _q_index.
        if (component == "_q_index") or (component in container._q_exports):
            is_module = type(container) is types.ModuleType
            has_component = hasattr(container, component)
            if is_module and not has_component and component != "_q_index":
                # get next module
                mod_name = container.__name__ + '.' + component
                # If we get an ImportError here we don't catch it.  It means
                # that either someone exported something that doesn't exist or
                # an exception was raised from deeping in the code.  A
                # traceback should be generated in either case.
                object = _get_module(mod_name)

            elif has_component:
                object = getattr(container, component)

            else: 
                # not a module, doesn't have 'component'
                raise errors.TraversalError(
                    private_msg="object %r has no attribute %r" % (
                                                         container,
                                                         component))
        else:
            # Component is not in exports list.  Check if namespace provides a
            # '_q_getname()' function, which translates an arbitrary string
            # into an object that we continue traversing.  This is very handy;
            # it lets you put user-space objects into your URL-space,
            # eliminating the need for digging ID strings out of a query, or
            # checking PATHINFO after Quixote's done with it.  But it is a
            # compromise to security: it opens up the traversal algorithm to
            # arbitrary names not listed in _q_exports!
            if hasattr(container, "_q_getname"):
                object = container._q_getname(request, component)
            else:
                raise errors.TraversalError(
                    private_msg="%r not in _q_exports list for %r" % (
                                                        component,
                                                        container))
        
        self.namespace_stack.append(object)
        return object

    _slash_pat = re.compile("//*")

    def traverse_url (self, path, request):
        """traverse_url(path : string, request : HTTPRequest) -> object : any

        Perform traversal based on the provided path, starting at the root
        object.  It returns the script name and path info values for
        the arrived-at object, along with the object itself.

        It's expected that the final object is something callable like a
        function or a method; intermediate objects along the way will
        usually be packages or modules.

        To prevent crackers from writing URLs that traverse private
        objects, every package, module, or object along the way must have
        a _q_exports attribute containing a list of publicly visible
        names.  Not having a _q_exports attribute is an error, though
        having _q_exports be an empty list is OK.  If a component of the path
        isn't in _q_exports, that also produces an error.
        """
        
        # If someone accesses a Quixote driver script without a trailing
        # slash, we'll wind up here with an empty path.  This won't
        # work; relative references in the page generated by the root
        # namespace's _q_index() will be off.  Fix it by redirecting the
        # user to the right URL; when the client follows the redirect,
        # we'll wind up here again with path == '/'.
        if (not path and self.config.fix_trailing_slash):
            request.redirect(request.environ['SCRIPT_NAME'] + '/' ,
                             permanent=1)
            return

        # replace repeated slashes with a single slash
        if path.find("//") != -1:
            path = self._slash_pat.sub("/", path)

        # split path apart; /foo/bar/baz  -> ['foo', 'bar', 'baz']
        #                   /foo/bar/     -> ['foo', 'bar', '']
        path_components = path[1:].split('/')

        # Traverse starting at the root
        object = self.root_namespace
        self.namespace_stack = [object] # reset

        # Loop over the components of the path
        for component in path_components:
            if component == "":
                # "/q/foo/" == "/q/foo/_q_index"
                component = "_q_index"
            object = self.get_component(object, component, path, request)

        if not (isstring(object) or callable(object)):
            # We went through all the components of the path and ended up at
            # something which isn't callable, like a module or an instance
            # without a __call__ method.
            if path[-1] != '/':
                if not request.form and self.config.fix_trailing_slash:
                    # This is for the convenience of users who type in paths.
                    # Repair the path and redirect.  This should not happen for
                    # URLs within the site.
                    request.redirect(request.get_path() + "/", permanent=1)
                    return

                else:
                    # Automatic redirects disabled or there is form data.  If
                    # there is form data then the programmer is using the
                    # wrong path.  A redirect won't work if the form data came
                    # from a POST anyhow.
                    raise errors.TraversalError(
                        "object is neither callable nor string "
                        "(missing trailing slash?)",
                        private_msg=repr(object),
                        path=path)
            else:
                raise errors.TraversalError(
                    "object is neither callable nor string",
                    private_msg=repr(object),
                    path=path)

        return object

    def try_publish (self, request, path):
        """try_publish(request : HTTPRequest, path : string) -> string
        
        The master method that does all the work for a single request.  Uses
        traverse_url() to get a callable object.  The object is called and
        the output is returned.  Exceptions are handled by the caller. 
        """

        self.start_request(request)

        # Traverse package to a (hopefully-) callable object
        object = self.traverse_url(path, request)

        # None means no output -- traverse_url() just issued a redirect.
        if object is None:
            return None

        # Anything else must be either a string...
        if isstring(object):
            output = object

        # ...or a callable.
        elif callable(object):
            try:
                output = object(request)
            except SystemExit:
                output = "SystemExit exception caught, shutting down"
                self.log(output)
                self.exit_now = 1

            if output is None:
                raise RuntimeError, 'callable %s returned None' % repr(object)

        # Uh-oh: 'object' is neither a string nor a callable.
        else:
            raise RuntimeError(
                "object is neither callable nor a string: %s" % repr(object))


        # The callable ran OK, commit any changes to the session
        self.finish_successful_request(request)

        return output

    _gzip_header = ("\037\213" # magic
                    "\010" # compression method
                    "\000" # flags
                    "\000\000\000\000" # time, who cares?
                    "\002"
                    "\377")

    def compress_output (self, request, output):

        encoding = request.get_encoding(["gzip", "x-gzip"])
        n = len(output)
        if n > 200 and encoding:
            co = zlib.compressobj(6, zlib.DEFLATED, -zlib.MAX_WBITS,
                                  zlib.DEF_MEM_LEVEL, 0)
            chunks = [self._gzip_header,
                      co.compress(output),
                      co.flush(),
                      struct.pack("<ll", zlib.crc32(output), len(output))]
            output = "".join(chunks)
            #self.log("gzip (original size %d, ratio %.1f)" % 
            #           (n, float(n)/len(output)))
            request.response.set_header("Content-Encoding", encoding)
        return output

    def process_request (self, request, env):
        """process_request(request : HTTPRequest, env : dict) : string

        Process a single request, given an HTTPRequest object.  The
        try_publish() method will be called to do the work and
        exceptions will be handled here.
        """
        global _request
        _request = request

        output = self.try_publish(request, env.get('PATH_INFO', ''))
        self.log_request(request)

        if output and self.config.compress_pages:
            output = self.compress_output(request, str(output))

        return output

    def publish (self, stdin, stdout, stderr, env):
        """publish(stdin : file, stdout : file, stderr : file, env : dict)

        Create an HTTPRequest object from the environment and from
        standard input, process it, and write the response to standard
        output.
        """
        request = self.create_request(stdin, env)
        try:
            self.parse_request(request)
            output = self.process_request(request, env)
        except errors.PublishError, exc:
            # Exit the publishing loop and return a result right away.
            output = self.finish_interrupted_request(request, exc)
        except:
            # Some other exception, generate error messages to the logs, etc.
            output = self.finish_failed_request(request)

        # Output results from Response object
        if output:
            request.response.set_body(output)
        request.response.write(stdout)

    
    def publish_cgi (self):
        """publish_cgi() -> None

        Entry point from CGI and FCGI scripts; it will repeatedly do the
        publish() function until there are no more requests (only once for
        CGI scripts).
        """

        # On Windows sys.stdout is opened in text mode by default
        if sys.platform == "win32":
            import msvcrt
            msvcrt.setmode(sys.__stdout__.fileno(), os.O_BINARY)

        while fcgi.isFCGI() and not self.exit_now:
            f = fcgi.FCGI()
            self.publish(f.inp, f.out, f.err, f.env)
            f.Finish()
            if self.config.run_once:
                break

# class Publisher


class SessionPublisher (Publisher):
    
    def __init__ (self, root_namespace, config=None, session_mgr=None):
        from quixote.session import SessionManager
        Publisher.__init__(self, root_namespace, config)
        if session_mgr is None:
            self.session_mgr = SessionManager()
        else:
            self.session_mgr = session_mgr

    def set_session_manager (self, session_mgr):
        self.session_mgr = session_mgr

    def start_request (self, request):
        # Get the session object and stick it onto the request
        request.session = self.session_mgr.get_session(request)
        request.session.start_request(request)

    def finish_successful_request (self, request):
        if request.session is not None:
            request.session.finish_request(request)
            self.session_mgr.maintain_session(request, request.session)
        self.session_mgr.commit_changes(request.session)

    def finish_interrupted_request (self, request, exc):
        output = Publisher.finish_interrupted_request(self, request, exc)

        # commit the current transaction so that any changes to the
        # session objects are saved and are visible on the next HTTP
        # hit.  Remember, AccessError is a subclass of PublishError,
        # so this code will be run for both typos in the URL and for
        # the user not being logged in.
        #
        # The assumption here is that the UI code won't make changes
        # to the core database before checking permissions and raising
        # a PublishError; if you must do this (though it's hard to see
        # why this would be necessary), you'll have to abort the
        # current transaction, make your session changes, and then
        # raise the PublishError.
        # 
        # XXX We should really be able to commit session changes and
        # database changes separately, but that requires ZODB
        # incantations that we currently don't know.
        self.session_mgr.commit_changes(request.session)

        return output

    def finish_failed_request (self, request):
        if self.session_mgr:
            self.session_mgr.abort_changes(request.session)
        return Publisher.finish_failed_request(self, request)

# class SessionPublisher


# Publisher singleton, only one of these per process.
_publisher = None

# The request currently being processed.
_request = None

# N.B. _publisher and _request should be accessed by functions only.  This
# allows multithreaded implementations to use per thread tables for these
# objects.

def get_publisher():
    global _publisher
    return _publisher

def get_request():
    global _request
    return _request

def get_path(n=0):
    global _request
    return _request.get_path(n)

def redirect(location, permanent=0):
    global _request
    return _request.redirect(location, permanent)

def get_session():
    global _request
    return _request.session

def get_session_manager ():
    global _publisher
    return _publisher.session_mgr

def get_user():
    global _request
    session = _request.session
    if session is None:
        return None
    else:
        return session.user


if sys.hexversion >= 0x02020000:    # Python 2.2 or greater
    def isstring (x):
        return isinstance(x, types.StringTypes)
else:
    if hasattr(types, 'UnicodeType'):
        _string_types = (types.StringType, types.UnicodeType)
    else:
        _string_types = (types.StringType,)

    def isstring (x):
        return type(x) in _string_types

