This file is indexed.

/usr/share/pyshared/quixote/publish.py is in python-quixote 2.7~b2-1+b2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""Logic for publishing modules and objects on the Web.
"""

import sys, traceback, StringIO
import time
import urlparse
import cgitb

from quixote.errors import PublishError, format_publish_error
from quixote import util
from quixote.config import Config
from quixote.http_response import HTTPResponse
from quixote.http_request import HTTPRequest
from quixote.logger import DefaultLogger

# 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 "-//W3C//DTD HTML 4.0//EN"
        "http://www.w3.org/TR/REC-html40/strict.dtd">
<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 traversal of the
    application's directory tree 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_directory : Directory
        the root directory that will be searched for objects to fulfill
        each request.  This can be any object with a _q_traverse method
        that acts like Directory._q_traverse.
      logger : DefaultLogger
        controls access log and error log behavior
      session_manager : NullSessionManager
         keeps track of sessions
      config : Config
        holds all configuration info for this application.  If the
        application doesn't provide values then default values
        from the quixote.config module are used.
      _request : HTTPRequest
        the HTTP request currently being processed.
    """

    def __init__(self, root_directory, logger=None, session_manager=None,
                 config=None, **kwargs):
        global _publisher
        if config is None:
            self.config = Config(**kwargs)
        else:
            if kwargs:
                raise ValueError("cannot provide both 'config' object and"
                                 " config arguments")
            self.config = config
        if logger is None:
            self.logger = DefaultLogger(error_log=self.config.error_log,
                                        access_log=self.config.access_log,
                                        error_email=self.config.error_email)
        else:
            self.logger = logger
        if session_manager is not None:
            self.session_manager = session_manager
        else:
            from quixote.session import NullSessionManager
            self.session_manager = NullSessionManager()

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

        if not hasattr(getattr(root_directory, '_q_traverse'), '__call__'):
            raise TypeError(
                'Expected something with a _q_traverse method, got %r' %
                root_directory)
        self.root_directory = root_directory
        self._request = None

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

    def log(self, msg):
        self.logger.log(msg)

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

    def start_request(self):
        """Called at the start of each request.
        """
        self.session_manager.start_request()

    def _set_request(self, request):
        """Set the current request object.
        """
        self._request = request

    def _clear_request(self):
        """Unset the current request object.
        """
        self._request = None

    def get_request(self):
        """Return the current request object.
        """
        return self._request

    def finish_successful_request(self):
        """Called at the end of a successful request.
        """
        self.session_manager.finish_successful_request()

    def format_publish_error(self, exc):
        return format_publish_error(exc)

    def finish_interrupted_request(self, 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.
        """
        if not self.config.display_exceptions and exc.private_msg:
            exc.private_msg = None # hide it
        request = get_request()
        request.response = HTTPResponse(status=exc.status_code)
        output = self.format_publish_error(exc)
        self.session_manager.finish_successful_request()
        return output

    def finish_failed_request(self):
        """
        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
        request = get_request()
        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':
            # 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.logger.log_internal_error(error_summary, plain_error_msg)
        if exc_type is SystemExit:
            raise
        request.response.set_status(500)
        self.session_manager.finish_failed_request()
        return user_error_msg


    def _generate_internal_error(self, request):
        admin = request.get_environ('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 = StringIO.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 = StringIO.StringIO()
        hook = cgitb.Hook(file=error_file)
        hook(exc_type, exc_value, tb)
        error_file.write('<h2>Original Request</h2>')
        error_file.write(str(util.dump_request(request)))
        error_file.write('<h2>Original Response</h2><pre>')
        original_response.write(error_file)
        error_file.write('</pre>')
        return error_file.getvalue()


    def try_publish(self, request):
        """(request : HTTPRequest) -> object

        The master method that does all the work for a single request.
        Exceptions are handled by the caller.
        """
        self.start_request()
        path = request.get_environ('PATH_INFO', '')
        if path and path[:1] != '/':
            return redirect(
                request.get_environ('SCRIPT_NAME', '') + '/' + path,
                permanent=True)
        components = path[1:].split('/')
        output = self.root_directory._q_traverse(components)
        # The callable ran OK, commit any changes to the session
        self.finish_successful_request()
        return output

    def filter_output(self, request, output):
        """Hook for post processing the output.  Subclasses may wish to
        override (e.g. check HTML syntax).
        """
        return output

    def process_request(self, request):
        """(request : HTTPRequest) -> HTTPResponse

        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.
        """
        self._set_request(request)
        start_time = time.time()
        try:
            self.parse_request(request)
            output = self.try_publish(request)
        except PublishError, exc:
            # Exit the publishing loop and return a result right away.
            output = self.finish_interrupted_request(exc)
        except:
            # Some other exception, generate error messages to the logs, etc.
            output = self.finish_failed_request()
        output = self.filter_output(request, output)
        self.logger.log_request(request, start_time)
        if output:
            if self.config.compress_pages and request.get_encoding(["gzip"]):
                compress = True
            else:
                compress = False
            request.response.set_body(output, compress)
        self._clear_request()
        return request.response

    def process(self, stdin, env):
        """(stdin : stream, env : dict) -> HTTPResponse
        
        Process a single request, given a stream, stdin, containing the 
        incoming request and a dictionary, env, containing the web server's 
        environment.
        
        An HTTPRequest object is created and the process_request() method is 
        called and passed the request object.
        """
        request = HTTPRequest(stdin, env)
        return self.process_request(request)


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

def get_publisher():
    return _publisher

def get_request():
    return _publisher.get_request()

def get_response():
    return _publisher.get_request().response

def get_field(name, default=None):
    return _publisher.get_request().get_field(name, default)

def get_cookie(name, default=None):
    return _publisher.get_request().get_cookie(name, default)

def get_path(n=0):
    return _publisher.get_request().get_path(n)

def redirect(location, permanent=False):
    """(location : string, permanent : boolean = false) -> string

    Create a redirection response.  If the location is relative, then it
    will automatically be made absolute.  The return value is an HTML
    document indicating the new URL (useful if the client browser does
    not honor the redirect).
    """
    request = _publisher.get_request()
    location = urlparse.urljoin(request.get_url(), str(location))
    return request.response.redirect(location, permanent)

def get_session():
    return _publisher.get_request().session

def get_session_manager():
    return _publisher.session_manager

def get_user():
    session = _publisher.get_request().session
    if session is None:
        return None
    else:
        return session.user

def get_wsgi_app():
    from quixote.wsgi import QWIP
    return QWIP(_publisher)

def cleanup():
    global _publisher
    _publisher = None