Logo Search packages:      
Sourcecode: zim version File versions  Download package

def zim::www::WWWInterface::__call__ (   self,
  environ,
  start_response 
)

Main function for handling a single request. Arguments are the file
handle to write the output to and the path to serve. Any exceptions
will result in a error response being written.

First argument is a dictionary with environment variables and some special
variables. See the PEP for expected variables. The second argument is a
function that can be called for example like:

      start_response(200, [('Content-Type', 'text/plain')])

This method is supposed to take care of sending the response line and
the headers.

The return value of this call is a list of lines with the content to
be served.

Definition at line 108 of file www.py.

00108                                                  :
            '''Main function for handling a single request. Arguments are the file
            handle to write the output to and the path to serve. Any exceptions
            will result in a error response being written.

            First argument is a dictionary with environment variables and some special
            variables. See the PEP for expected variables. The second argument is a
            function that can be called for example like:

                  start_response(200, [('Content-Type', 'text/plain')])

            This method is supposed to take care of sending the response line and
            the headers.

            The return value of this call is a list of lines with the content to
            be served.
            '''
            headerlist = []
            headers = Headers(headerlist)
            path = environ.get('PATH_INFO', '/')
            try:
                  methods = ('GET', 'HEAD')
                  if not environ['REQUEST_METHOD'] in methods:
                        raise WWWError('405', headers=[('Allow', ', '.join(methods))])

                  # TODO clean up path from any '../' (and ..\)

                  if not path:
                        path = '/'
                  elif path == '/favicon.ico':
                        path = '/+icons/favicon.ico'
                  elif path.startswith('/%2B'):
                        path = '/+' + path[4:] # HACK, very local decoding

                  if self.notebook is None:
                        raise NoConfigError
                  elif path == '/':
                        headers.add_header('Content-Type', 'text/html', charset='utf-8')
                        content = self.render_index()
                  elif path.startswith('/+docs/'):
                        pass # TODO document root
                  elif path.startswith('/+file/'):
                        pass # TODO attachment or raw source
                  elif path.startswith('/+icons/'):
                        # TODO check if favicon is overridden or something
                        file = data_file('pixmaps/%s' % path[8:])
                        if path.endswith('.png'):
                              headers['Content-Type'] = 'image/png'
                        elif path.endswith('.ico'):
                              headers['Content-Type'] = 'image/vnd.microsoft.icon'
                        content = [file.read(encoding=None)]
                  else:
                        # Must be a page or a namespace (html file or directory path)
                        headers.add_header('Content-Type', 'text/html', charset='utf-8')
                        if path.endswith('.html'):
                              pagename = path[:-5].replace('/', ':')
                        elif path.endswith('/'):
                              pagename = path[:-1].replace('/', ':')
                        else:
                              raise PageNotFoundError(path)

                        pagename = urllib.unquote(pagename)
                        path = self.notebook.resolve_path(pagename)
                        page = self.notebook.get_page(path)
                        if page.hascontent:
                              content = self.render_page(page)
                        elif page.haschildren:
                              content = self.render_index(page)
                        else:
                              raise PageNotFoundError(page)
            except Exception, error:
                  headerlist = []
                  headers = Headers(headerlist)
                  headers.add_header('Content-Type', 'text/plain', charset='utf-8')
                  if isinstance(error, WWWError):
                        logger.error(error.msg)
                        if error.headers:
                              header.extend(error.headers)
                        start_response(error.status, headerlist)
                        content = unicode(error).splitlines(True)
                  # TODO also handle template errors as special here
                  else:
                        # Unexpected error - maybe a bug, do not expose output on bugs
                        # to the outside world
                        logger.exception('Unexpected error:')
                        start_response('500 Internal Server Error', headerlist)
                        content = ['Internal Server Error']
                  if environ['REQUEST_METHOD'] == 'HEAD':
                        return []
                  else:
                        return [string.encode('utf-8') for string in content]
            else:
                  start_response('200 OK', headerlist)
                  if environ['REQUEST_METHOD'] == 'HEAD':
                        return []
                  elif 'utf-8' in headers['Content-Type']:
                        return [string.encode('utf-8') for string in content]
                  else:
                        return content

      def render_index(self, namespace=None):


Generated by  Doxygen 1.6.0   Back to index