Пример #1
0
 def _set_response_initial(self, data: bytes):
     """Parse first bytes from http response."""
     res = re.match(_HTTP_RESPONSE_STATUS_LINE,
                    data.decode().rstrip('\r\n'))
     if not res:
         raise HttpParsingError('response line parsing error')
     self.response_initial = res.groupdict()
Пример #2
0
    async def acquire(self, urlparsed: ParseResult, verify, ssl, timeouts,
                      http2):
        """Acquire connection."""
        if not urlparsed.hostname:
            raise HttpParsingError('missing hostname')

        # Faster without timeout
        if not self.timeouts.pool_acquire:
            conn = await self.pool.acquire(urlparsed)
            return await self.after_acquire(urlparsed, conn, verify, ssl,
                                            timeouts, http2)

        try:
            conn = await wait_for(self.pool.acquire(urlparsed),
                                  self.timeouts.pool_acquire)
            return await self.after_acquire(urlparsed, conn, verify, ssl,
                                            timeouts, http2)
        except TimeoutException:
            raise ConnectionPoolAcquireTimeout()
Пример #3
0
    async def _connect(self, urlparsed: ParseResult, verify: bool,
                       ssl_context: SSLContext, dns_info, http2: bool) -> None:
        """Get reader and writer."""
        if not urlparsed.hostname:
            raise HttpParsingError('missing hostname')

        key = f'{urlparsed.hostname}-{urlparsed.port}'

        if self.writer:
            # python 3.6 doesn't have writer.is_closing
            is_closing = getattr(
                self.writer, 'is_closing',
                self.writer._transport.is_closing)  # type: ignore
        else:

            def is_closing():
                return True  # noqa

        dns_info_copy = dns_info.copy()
        dns_info_copy['server_hostname'] = dns_info_copy.pop('hostname')

        if not (self.key and key == self.key and not is_closing()):
            self.close()

            if urlparsed.scheme == 'https':
                ssl_context = ssl_context or ssl.create_default_context(
                    ssl.Purpose.SERVER_AUTH, )
                # flag will be removed when fully http2 support
                if http2:  # pragma: no cover
                    ssl_context.set_alpn_protocols(['h2', 'http/1.1'])
                if not verify:
                    ssl_context.check_hostname = False
                    ssl_context.verify_mode = ssl.CERT_NONE
            else:
                del dns_info_copy['server_hostname']
            port = urlparsed.port or (443
                                      if urlparsed.scheme == 'https' else 80)
            dns_info_copy['port'] = port
            self.reader, self.writer = await open_connection(
                **dns_info_copy, ssl=ssl_context)

            self.temp_key = key
            await self._connection_made()
Пример #4
0
    async def _connect(self, urlparsed: ParseResult, verify: bool,
                       ssl_context: SSLContext, http2: bool):
        """Get reader and writer."""
        if not urlparsed.hostname:
            raise HttpParsingError('missing hostname')

        key = '%s-%s' % (urlparsed.hostname, urlparsed.port)

        if self.writer:
            # python 3.6 doesn't have writer.is_closing
            is_closing = getattr(
                self.writer, 'is_closing',
                self.writer._transport.is_closing)  # type: ignore
        else:
            def is_closing(): return True  # noqa

        if not (self.key and key == self.key and not is_closing()):
            if self.writer:
                self.writer.close()

            if urlparsed.scheme == 'https':
                ssl_context = ssl_context or ssl.create_default_context(
                    ssl.Purpose.SERVER_AUTH,
                )
                if http2:  # flag will be removed when fully http2 support
                    ssl_context.set_alpn_protocols(['h2', 'http/1.1'])
                if not verify:
                    ssl_context.check_hostname = False
                    ssl_context.verify_mode = ssl.CERT_NONE
            port = urlparsed.port or (
                443 if urlparsed.scheme == 'https' else 80)
            self.reader, self.writer = await asyncio.open_connection(
                urlparsed.hostname,
                port,
                ssl=ssl_context
            )

            self.temp_key = key
            await self._connection_made()