-
Notifications
You must be signed in to change notification settings - Fork 12
add aiohttp support #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,16 +18,21 @@ class MockSmtpCall(Exception): | |
| pass | ||
|
|
||
|
|
||
| def block_http(whitelist): | ||
| def http_fail(host): | ||
| logger.warning('Denied HTTP connection to: %s' % host) | ||
| raise MockHttpCall(host) | ||
|
|
||
|
|
||
| def block_http_httplib(whitelist): | ||
| def whitelisted(self, host, *args, **kwargs): | ||
| try: | ||
| string_type = basestring | ||
| except NameError: | ||
| # python3 | ||
| string_type = str | ||
| if isinstance(host, string_type) and host not in whitelist: | ||
| logger.warning('Denied HTTP connection to: %s' % host) | ||
| raise MockHttpCall(host) | ||
| http_fail(host) | ||
|
|
||
| logger.debug('Allowed HTTP connection to: %s' % host) | ||
| return self.old(host, *args, **kwargs) | ||
|
|
||
|
|
@@ -39,6 +44,34 @@ def whitelisted(self, host, *args, **kwargs): | |
| httplib.HTTPConnection.__init__ = whitelisted | ||
|
|
||
|
|
||
| def block_http_aiohttp(whitelist): | ||
| try: | ||
| from aiohttp.client import ClientSession | ||
| except ImportError: | ||
| # no aiohttp installed | ||
| return | ||
|
|
||
| from yarl import URL | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is yarl? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems separate https://github.com/aio-libs/yarl
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a requirement of |
||
|
|
||
| def patch(self, method, url, **kwargs): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am fine with naming the patched method patch, but if you do it, please rename whitelisted above too. It should be consistent. |
||
| yurl = URL(url) | ||
|
|
||
| if yurl.host not in whitelist: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a log statement when an url passes the whitelist, as above. |
||
| http_fail(yurl.host) | ||
|
|
||
| return self.old_request(method, url, **kwargs) | ||
|
|
||
| if not getattr(ClientSession, 'blockage', False): | ||
| logger.debug('Monkey patching httplib') | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You aren't monkeypatching httplib here. This debug message might be confusing. |
||
| ClientSession.old_request = ClientSession._request | ||
| ClientSession._request = patch | ||
|
|
||
|
|
||
| def block_http(whitelist): | ||
| block_http_httplib(whitelist) | ||
| block_http_aiohttp(whitelist) | ||
|
|
||
|
|
||
| def block_smtp(whitelist): | ||
| def whitelisted(self, host, *args, **kwargs): | ||
| if isinstance(host, basestring) and host not in whitelist: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add log message at level info here. If one day aiohttp changes import locations, this code will silently fail without giving a hint as to why it fails. better yet, if you want, don't catch for import errors at all but use pkg_resources to check if the aiohttp package is installed.
Then a changed import in aiohttp will trigger an exception which is even better than a log message.