Skip to content

Commit

Permalink
Implement cookie filter #799
Browse files Browse the repository at this point in the history
  • Loading branch information
panda73111 authored and asvetlov committed Jun 3, 2016
1 parent 0c912be commit 0016999
Show file tree
Hide file tree
Showing 7 changed files with 793 additions and 52 deletions.
31 changes: 11 additions & 20 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import sys
import traceback
import warnings
import http.cookies
import urllib.parse

from multidict import MultiDictProxy, MultiDict, CIMultiDict, upstr

import aiohttp
from .client_reqrep import ClientRequest, ClientResponse
from .errors import WSServerHandshakeError
from .helpers import CookieJar
from .websocket import WS_KEY, WebSocketParser, WebSocketWriter
from .websocket_client import ClientWebSocketResponse
from . import hdrs, helpers
Expand Down Expand Up @@ -52,13 +52,13 @@ def __init__(self, *, connector=None, loop=None, cookies=None,
if loop.get_debug():
self._source_traceback = traceback.extract_stack(sys._getframe(1))

self._cookies = http.cookies.SimpleCookie()
self._cookie_jar = CookieJar(loop=loop)

# For Backward compatability with `share_cookies` connectors
if connector._share_cookies:
self._update_cookies(connector.cookies)
self._cookie_jar.update_cookies(connector.cookies)
if cookies is not None:
self._update_cookies(cookies)
self._cookie_jar.update_cookies(cookies)
self._connector = connector
self._default_auth = auth
self._version = version
Expand Down Expand Up @@ -173,10 +173,13 @@ def _request(self, method, url, *,
skip_headers.add(upstr(i))

while True:

cookies = self._cookie_jar.filter_cookies(url)

req = self._request_class(
method, url, params=params, headers=headers,
skip_auto_headers=skip_headers, data=data,
cookies=self.cookies, encoding=encoding,
cookies=cookies, encoding=encoding,
auth=auth, version=version, compress=compress, chunked=chunked,
expect100=expect100,
loop=self._loop, response_class=self._response_class)
Expand All @@ -196,7 +199,8 @@ def _request(self, method, url, *,
except OSError as exc:
raise aiohttp.ClientOSError(*exc.args) from exc

self._update_cookies(resp.cookies)
self._cookie_jar.update_cookies(resp.cookies, resp.url)

# For Backward compatability with `share_cookie` connectors
if self._connector._share_cookies:
self._connector.update_cookies(resp.cookies)
Expand Down Expand Up @@ -357,19 +361,6 @@ def _ws_connect(self, url, *,
autoping,
self._loop)

def _update_cookies(self, cookies):
"""Update shared cookies."""
if isinstance(cookies, dict):
cookies = cookies.items()

for name, value in cookies:
if isinstance(value, http.cookies.Morsel):
# use dict method because SimpleCookie class modifies value
# before Python 3.4
dict.__setitem__(self.cookies, name, value)
else:
self.cookies[name] = value

def _prepare_headers(self, headers):
""" Add default headers and transform it to CIMultiDict
"""
Expand Down Expand Up @@ -463,7 +454,7 @@ def connector(self):
@property
def cookies(self):
"""The session cookies."""
return self._cookies
return self._cookie_jar.cookies

@property
def version(self):
Expand Down
27 changes: 2 additions & 25 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import aiohttp
import functools
import http.cookies
import re
import ssl
import sys
import traceback
Expand All @@ -20,7 +19,7 @@
from .errors import HttpProxyError, ProxyConnectionError
from .errors import ClientOSError, ClientTimeoutError
from .errors import FingerprintMismatch
from .helpers import BasicAuth
from .helpers import BasicAuth, is_ip_address
from .resolver import DefaultResolver


Expand Down Expand Up @@ -417,28 +416,6 @@ def _create_connection(self, req):

_marker = object()

ipv4_pattern = ('^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}'
'(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$')
ipv6_pattern = (
'^(?:(?:(?:[A-F0-9]{1,4}:){6}|(?=(?:[A-F0-9]{0,4}:){0,6}'
'(?:[0-9]{1,3}\.){3}[0-9]{1,3}$)(([0-9A-F]{1,4}:){0,5}|:)'
'((:[0-9A-F]{1,4}){1,5}:|:)|::(?:[A-F0-9]{1,4}:){5})'
'(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}'
'(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])|(?:[A-F0-9]{1,4}:){7}'
'[A-F0-9]{1,4}|(?=(?:[A-F0-9]{0,4}:){0,7}[A-F0-9]{0,4}$)'
'(([0-9A-F]{1,4}:){1,7}|:)((:[0-9A-F]{1,4}){1,7}|:)|(?:[A-F0-9]{1,4}:){7}'
':|:(:[A-F0-9]{1,4}){7})$')
ipv4_regex = re.compile(ipv4_pattern)
ipv6_regex = re.compile(ipv6_pattern)


def host_is_ip(host):
if re.match(ipv4_pattern, host) or re.match(ipv6_pattern, host,
flags=re.IGNORECASE):
return True
else:
return False


class TCPConnector(BaseConnector):
"""TCP connector.
Expand Down Expand Up @@ -590,7 +567,7 @@ def clear_resolved_hosts(self, host=None, port=None):

@asyncio.coroutine
def _resolve_host(self, host, port):
if not self._use_resolver or host_is_ip(host):
if not self._use_resolver or is_ip_address(host):
return [{'hostname': host, 'host': host, 'port': port,
'family': self._family, 'proto': 0, 'flags': 0}]

Expand Down
Loading

0 comments on commit 0016999

Please sign in to comment.