Пример #1
0
 def convert(self, value):
     """
     Convert values to an appropriate type. dicts, lists and tuples are
     replaced by their converting alternatives. Strings are checked to
     see if they have a conversion format and are converted if they do.
     """
     if not isinstance(value, ConvertingDict) and isinstance(value, dict):
         value = ConvertingDict(value)
         value.configurator = self
     elif not isinstance(value, ConvertingList) and isinstance(value, list):
         value = ConvertingList(value)
         value.configurator = self
     elif not isinstance(value, ConvertingTuple) and\
              isinstance(value, tuple):
         value = ConvertingTuple(value)
         value.configurator = self
     elif is_string(value): # str for py3k
         m = self.CONVERT_PATTERN.match(value)
         if m:
             d = m.groupdict()
             prefix = d['prefix']
             converter = self.value_converters.get(prefix, None)
             if converter:
                 suffix = d['suffix']
                 converter = getattr(self, converter)
                 value = converter(suffix)
     return value
Пример #2
0
 def convert(self, value):
     """
     Convert values to an appropriate type. dicts, lists and tuples are
     replaced by their converting alternatives. Strings are checked to
     see if they have a conversion format and are converted if they do.
     """
     if not isinstance(value, ConvertingDict) and isinstance(value, dict):
         value = ConvertingDict(value)
         value.configurator = self
     elif not isinstance(value, ConvertingList) and isinstance(value, list):
         value = ConvertingList(value)
         value.configurator = self
     elif not isinstance(value, ConvertingTuple) and\
             isinstance(value, tuple):
         value = ConvertingTuple(value)
         value.configurator = self
     elif is_string(value):  # str for py3k
         m = self.CONVERT_PATTERN.match(value)
         if m:
             d = m.groupdict()
             prefix = d['prefix']
             converter = self.value_converters.get(prefix, None)
             if converter:
                 suffix = d['suffix']
                 converter = getattr(self, converter)
                 value = converter(suffix)
     return value
Пример #3
0
def wsgi_environ(connection, parser):
    """return a :ref:`WSGI <apps-wsgi>` compatible environ dictionary
based on the current request. If the reqi=uest headers are not ready it returns
nothing."""
    version = parser.get_version()
    input = BytesIO()
    for b in parser.get_body():
        input.write(b)
    input.seek(0)
    protocol = parser.get_protocol()
    environ = {
        "wsgi.input": input,
        "wsgi.errors": sys.stderr,
        "wsgi.version": version,
        "wsgi.run_once": True,
        "wsgi.url_scheme": protocol,
        "SERVER_SOFTWARE": pulsar.SERVER_SOFTWARE,
        "REQUEST_METHOD": parser.get_method(),
        "QUERY_STRING": parser.get_query_string(),
        "RAW_URI": parser.get_url(),
        "SERVER_PROTOCOL": protocol,
        'CONTENT_TYPE': '',
        "CONTENT_LENGTH": '',
        'SERVER_NAME': connection.server.server_name,
        'SERVER_PORT': connection.server.server_port,
        "wsgi.multithread": False,
        "wsgi.multiprocess":False
    }
    # REMOTE_HOST and REMOTE_ADDR may not qualify the remote addr:
    # http://www.ietf.org/rfc/rfc3875
    url_scheme = "http"
    forward = connection.address
    server = None
    url_scheme = "http"
    script_name = os.environ.get("SCRIPT_NAME", "")
    headers = mapping_iterator(parser.get_headers())
    for header, value in headers:
        header = header.lower()
        if header == 'x-forwarded-for':
            forward = value
        elif header == "x-forwarded-protocol" and value == "ssl":
            url_scheme = "https"
        elif header == "x-forwarded-ssl" and value == "on":
            url_scheme = "https"
        elif header == "host":
            server = value
        elif header == "script_name":
            script_name = value
        elif header == "content-type":
            environ['CONTENT_TYPE'] = value
            continue
        elif header == "content-length":
            environ['CONTENT_LENGTH'] = value
            continue
        key = 'HTTP_' + header.upper().replace('-', '_')
        environ[key] = value
    environ['wsgi.url_scheme'] = url_scheme
    if is_string(forward):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()
        remote = forward.split(":")
        if len(remote) < 2:
            remote.append('80')
    else:
        remote = forward
    environ['REMOTE_ADDR'] = remote[0]
    environ['REMOTE_PORT'] = str(remote[1])
    if server is not None:
        server =  host_and_port_default(url_scheme, server)
        environ['SERVER_NAME'] = server[0]
        environ['SERVER_PORT'] = server[1]
    path_info = parser.get_path()
    if path_info is not None:
        if script_name:
            path_info = path_info.split(script_name, 1)[1]
        environ['PATH_INFO'] = unquote(path_info)
    environ['SCRIPT_NAME'] = script_name
    return environ