Пример #1
0
 def resolve(self, host, port, family=0):
     if is_valid_ip(host):
         addresses = [host]
     else:
         # gethostbyname doesn't take callback as a kwarg
         self.channel.gethostbyname(host, family, (yield gen.Callback(1)))
         callback_args = yield gen.Wait(1)
         assert isinstance(callback_args, gen.Arguments)
         assert not callback_args.kwargs
         result, error = callback_args.args
         if error:
             raise Exception('C-Ares returned error %s: %s while resolving %s' %
                             (error, pycares.errno.strerror(error), host))
         addresses = result.addresses
     addrinfo = []
     for address in addresses:
         if '.' in address:
             address_family = socket.AF_INET
         elif ':' in address:
             address_family = socket.AF_INET6
         else:
             address_family = socket.AF_UNSPEC
         if family != socket.AF_UNSPEC and family != address_family:
             raise Exception('Requested socket family %d but got %d' %
                             (family, address_family))
         addrinfo.append((address_family, (address, port)))
     raise gen.Return(addrinfo)
Пример #2
0
    def __init__(self, method, uri, version="HTTP/1.0", headers=None,
                 body=None, remote_ip=None, protocol=None, host=None,
                 files=None, connection=None):
        self.method = method
        self.uri = uri
        self.version = version
        self.headers = headers or httputil.HTTPHeaders()
        self.body = body or ""

        # set remote IP and protocol
        self.remote_ip = remote_ip
        if protocol:
            self.protocol = protocol
        elif connection and isinstance(connection.stream,
                                       iostream.SSLIOStream):
            self.protocol = "https"
        else:
            self.protocol = "http"

        # xheaders can override the defaults
        if connection and connection.xheaders:
            # Squid uses X-Forwarded-For, others use X-Real-Ip
            ip = self.headers.get("X-Forwarded-For", self.remote_ip)
            ip = ip.split(',')[-1].strip()
            ip = self.headers.get(
                "X-Real-Ip", ip)
            if netutil.is_valid_ip(ip):
                self.remote_ip = ip
            # AWS uses X-Forwarded-Proto
            proto = self.headers.get(
                "X-Scheme", self.headers.get("X-Forwarded-Proto", self.protocol))
            if proto in ("http", "https"):
                self.protocol = proto

        self.host = host or self.headers.get("Host") or "127.0.0.1"
        self.files = files or {}
        self.connection = connection
        self._start_time = time.time()
        self._finish_time = None

        self.path, sep, self.query = uri.partition('?')
        self.arguments = parse_qs_bytes(self.query, keep_blank_values=True)
        self.query_arguments = copy.deepcopy(self.arguments)
        self.body_arguments = {}
Пример #3
0
 def test_is_valid_ip(self):
     self.assertTrue(is_valid_ip('127.0.0.1'))
     self.assertTrue(is_valid_ip('4.4.4.4'))
     self.assertTrue(is_valid_ip('::1'))
     self.assertTrue(is_valid_ip('2620:0:1cfe:face:b00c::3'))
     self.assertTrue(not is_valid_ip('www.google.com'))
     self.assertTrue(not is_valid_ip('localhost'))
     self.assertTrue(not is_valid_ip('4.4.4.4<'))
     self.assertTrue(not is_valid_ip(' 127.0.0.1'))
     self.assertTrue(not is_valid_ip(''))
     self.assertTrue(not is_valid_ip(' '))
     self.assertTrue(not is_valid_ip('\n'))
     self.assertTrue(not is_valid_ip('\x00'))