Skip to content
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

proxy: add support for scheme prefixes socks4:// and socks5:// in http_proxy environment variable #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions python2/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
ssl_SSLError = getattr(ssl, "SSLError", None)
ssl_CertificateError = getattr(ssl, "CertificateError", None)

# socks.PROXY_TYPE_SOCKS4, socks.PROXY_TYPE_SOCKS5, socks.PROXY_TYPE_HTTP
proxy_schemes = { 'socks4' : 1, 'socks5' : 2, 'http' : 3 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use what black --diff python2/ python3/ says here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put this after socks import or somewhere not between two ssl related blocks.


def _ssl_wrap_socket(
sock, key_file, cert_file, disable_validation, ca_certs, ssl_version, hostname, key_password
Expand Down Expand Up @@ -1123,6 +1125,10 @@ def proxy_info_from_url(url, method="http", noproxy=None):
port = dict(https=443, http=80)[method]

proxy_type = 3 # socks.PROXY_TYPE_HTTP
if len(url.scheme) > 0:
_scheme_prefix = url.scheme.lower()
if _scheme_prefix in proxy_schemes:
proxy_type = proxy_schemes[_scheme_prefix]
Comment on lines 1127 to +1131
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proxy_type = 3 # socks.PROXY_TYPE_HTTP
if len(url.scheme) > 0:
_scheme_prefix = url.scheme.lower()
if _scheme_prefix in proxy_schemes:
proxy_type = proxy_schemes[_scheme_prefix]
proxy_type = proxy_schemes.get(url.scheme.lower(), 3) # socks.PROXY_TYPE_HTTP

pi = ProxyInfo(
proxy_type=proxy_type,
proxy_host=host,
Expand Down
6 changes: 6 additions & 0 deletions python3/httplib2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def has_timeout(timeout):
# A request will be tried 'RETRIES' times if it fails at the socket/connection level.
RETRIES = 2

# socks.PROXY_TYPE_SOCKS4, socks.PROXY_TYPE_SOCKS5, socks.PROXY_TYPE_HTTP
proxy_schemes = { 'socks4' : 1, 'socks5' : 2, 'http' : 3 }

# All exceptions raised here derive from HttpLib2Error
class HttpLib2Error(Exception):
Expand Down Expand Up @@ -1104,6 +1106,10 @@ def proxy_info_from_url(url, method="http", noproxy=None):
port = dict(https=443, http=80)[method]

proxy_type = 3 # socks.PROXY_TYPE_HTTP
if len(url.scheme) > 0:
_scheme_prefix = url.scheme.lower()
if _scheme_prefix in proxy_schemes:
proxy_type = proxy_schemes[_scheme_prefix]
pi = ProxyInfo(
proxy_type=proxy_type,
proxy_host=host,
Expand Down