Beispiel #1
0
    def _partial_application_url(self, scheme=None, host=None, port=None):
        """
        Construct the URL defined by request.application_url, replacing any
        of the default scheme, host, or port portions with user-supplied
        variants.

        If ``scheme`` is passed as ``https``, and the ``port`` is *not*
        passed, the ``port`` value is assumed to ``443``.  Likewise, if
        ``scheme`` is passed as ``http`` and ``port`` is not passed, the
        ``port`` value is assumed to be ``80``.
        """
        e = self.environ
        if scheme is None:
            scheme = e['wsgi.url_scheme']
        else:
            if scheme == 'https':
                if port is None:
                    port = '443'
            if scheme == 'http':
                if port is None:
                    port = '80'
        url = scheme + '://'
        if port is not None:
            port = str(port)
        if host is None:
            host = e.get('HTTP_HOST')
        if host is None:
            host = e['SERVER_NAME']
        if port is None:
            if ':' in host:
                host, port = host.split(':', 1)
            else:
                port = e['SERVER_PORT']
        else:
            if ':' in host:
                host, _ = host.split(':', 1)
        if scheme == 'https':
            if port == '443':
                port = None
        elif scheme == 'http':
            if port == '80':
                port = None
        url += host
        if port:
            url += ':%s' % port

        url_encoding = getattr(self, 'url_encoding', 'utf-8') # webob 1.2b3+
        bscript_name = bytes_(self.script_name, url_encoding)
        return url + url_quote(bscript_name, PATH_SAFE)
Beispiel #2
0
 def render_template(self, content, vars, filename=None):
     """ Return a bytestring representing a templated file based on the
     input (content) and the variable names defined (vars).  ``filename``
     is used for exception reporting."""
     # this method must not be named "template_renderer" fbo of extension
     # scaffolds that need to work under pyramid 1.2 and 1.3, and which
     # need to do "template_renderer =
     # staticmethod(paste_script_template_renderer)"
     content = native_(content, fsenc)
     try:
         return bytes_(
             substitute_double_braces(content, TypeMapper(vars)), fsenc)
     except Exception as e:
         _add_except(e, ' in file %s' % filename)
         raise