http_proxy_from_env 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/http_proxy_from_env.gemspec +23 -0
- data/lib/http_proxy_from_env/net/http.rb +125 -0
- data/lib/http_proxy_from_env/open_uri.rb +17 -0
- data/lib/http_proxy_from_env/uri/generic.rb +67 -0
- data/lib/http_proxy_from_env/version.rb +3 -0
- data/lib/http_proxy_from_env.rb +7 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 mechamogera
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# HttpProxyFromEnv
|
2
|
+
|
3
|
+
Net::HTTP automatically detects and uses proxies from the environment.
|
4
|
+
http\_proxy\_from\_env is made from https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/36476
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'http_proxy_from_env'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install http_proxy_from_env
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```
|
23
|
+
# only net/http patch
|
24
|
+
require 'http_proxy_from_env/net/http'
|
25
|
+
```
|
26
|
+
|
27
|
+
or
|
28
|
+
|
29
|
+
```
|
30
|
+
# only open-uri patch
|
31
|
+
require 'http_proxy_from_env/open-uri'
|
32
|
+
```
|
33
|
+
|
34
|
+
or
|
35
|
+
|
36
|
+
```
|
37
|
+
# all patch
|
38
|
+
require 'http_proxy_from_env
|
39
|
+
```
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'http_proxy_from_env/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "http_proxy_from_env"
|
8
|
+
spec.version = HttpProxyFromEnv::VERSION
|
9
|
+
spec.authors = ["mechamogera"]
|
10
|
+
spec.email = ["mechamosura@gmail.com"]
|
11
|
+
spec.description = %q{Net::HTTP automatically detects and uses proxies from the environment.}
|
12
|
+
spec.summary = %q{Net::HTTP automatically detects and uses proxies from the environment.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'http_proxy_from_env/uri/generic.rb'
|
3
|
+
|
4
|
+
class Net::HTTP
|
5
|
+
unless method_defined?(:proxy_from_env)
|
6
|
+
def self.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil)
|
7
|
+
http = super address, port
|
8
|
+
|
9
|
+
if proxy_class? then # from Net::HTTP::Proxy()
|
10
|
+
http.proxy_from_env = @proxy_from_env
|
11
|
+
http.proxy_address = @proxy_address
|
12
|
+
http.proxy_port = @proxy_port
|
13
|
+
http.proxy_user = @proxy_user
|
14
|
+
http.proxy_pass = @proxy_pass
|
15
|
+
elsif p_addr == :ENV then
|
16
|
+
http.proxy_from_env = true
|
17
|
+
else
|
18
|
+
http.proxy_address = p_addr
|
19
|
+
http.proxy_port = p_port || default_port
|
20
|
+
http.proxy_user = p_user
|
21
|
+
http.proxy_pass = p_pass
|
22
|
+
end
|
23
|
+
|
24
|
+
http
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil)
|
28
|
+
return self unless p_addr
|
29
|
+
|
30
|
+
Class.new(self) {
|
31
|
+
@is_proxy_class = true
|
32
|
+
|
33
|
+
if p_addr == :ENV then
|
34
|
+
@proxy_from_env = true
|
35
|
+
@proxy_address = nil
|
36
|
+
@proxy_port = nil
|
37
|
+
else
|
38
|
+
@proxy_from_env = false
|
39
|
+
@proxy_address = p_addr
|
40
|
+
@proxy_port = p_port || default_port
|
41
|
+
end
|
42
|
+
|
43
|
+
@proxy_user = p_user
|
44
|
+
@proxy_pass = p_pass
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
@proxy_from_env = false
|
49
|
+
@proxy_uri = nil
|
50
|
+
@proxy_address = nil
|
51
|
+
@proxy_port = nil
|
52
|
+
@proxy_user = nil
|
53
|
+
@proxy_pass = nil
|
54
|
+
|
55
|
+
attr_writer :proxy_from_env
|
56
|
+
attr_writer :proxy_address
|
57
|
+
attr_writer :proxy_port
|
58
|
+
attr_writer :proxy_user
|
59
|
+
attr_writer :proxy_pass
|
60
|
+
|
61
|
+
def proxy?
|
62
|
+
if @proxy_from_env then
|
63
|
+
proxy_uri
|
64
|
+
else
|
65
|
+
@proxy_address
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def proxy_from_env?
|
70
|
+
@proxy_from_env
|
71
|
+
end
|
72
|
+
|
73
|
+
def proxy_uri
|
74
|
+
@proxy_uri ||= URI("http://#{address}:#{port}").find_proxy
|
75
|
+
end
|
76
|
+
|
77
|
+
def proxy_address
|
78
|
+
if @proxy_from_env then
|
79
|
+
proxy_uri && proxy_uri.hostname
|
80
|
+
else
|
81
|
+
@proxy_address
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def proxy_port
|
86
|
+
if @proxy_from_env then
|
87
|
+
proxy_uri && proxy_uri.port
|
88
|
+
else
|
89
|
+
@proxy_port
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def edit_path(path)
|
94
|
+
if proxy? and not use_ssl? then
|
95
|
+
"http://#{addr_port}#{path}"
|
96
|
+
else
|
97
|
+
path
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def conn_address
|
102
|
+
proxy? ? proxy_address : address
|
103
|
+
end
|
104
|
+
|
105
|
+
def conn_port
|
106
|
+
proxy? ? proxy_port : prot
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def proxy_user
|
111
|
+
if @proxy_from_env then
|
112
|
+
proxy_uri && proxy_uri.user
|
113
|
+
else
|
114
|
+
@proxy_user
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def proxy_pass
|
119
|
+
if @proxy_from_env then
|
120
|
+
proxy_uri && proxy_uri.password
|
121
|
+
else
|
122
|
+
@proxy_pass
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module OpenURI
|
4
|
+
class << self
|
5
|
+
alias :_open_http :open_http
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.open_http(buf, target, proxy, options)
|
9
|
+
proxy_wrap = proxy
|
10
|
+
if proxy_wrap && proxy.first.class == URI::HTTP
|
11
|
+
proxy_uri, proxy_user, proxy_pass = proxy
|
12
|
+
proxy_wrap = [proxy_uri, proxy_user || proxy_uri.user, proxy_pass || proxy_uri.password]
|
13
|
+
end
|
14
|
+
|
15
|
+
_open_http(buf, target, proxy_wrap, options)
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
class URI::Generic
|
4
|
+
unless method_defined? :find_proxy
|
5
|
+
def find_proxy
|
6
|
+
raise BadURIError, "relative URI: #{self}" if self.relative?
|
7
|
+
name = self.scheme.downcase + '_proxy'
|
8
|
+
proxy_uri = nil
|
9
|
+
if name == 'http_proxy' && ENV.include?('REQUEST_METHOD') # CGI?
|
10
|
+
# HTTP_PROXY conflicts with *_proxy for proxy settings and
|
11
|
+
# HTTP_* for header information in CGI.
|
12
|
+
# So it should be careful to use it.
|
13
|
+
pairs = ENV.reject {|k, v| /\Ahttp_proxy\z/i !~ k }
|
14
|
+
case pairs.length
|
15
|
+
when 0 # no proxy setting anyway.
|
16
|
+
proxy_uri = nil
|
17
|
+
when 1
|
18
|
+
k, _ = pairs.shift
|
19
|
+
if k == 'http_proxy' && ENV[k.upcase] == nil
|
20
|
+
# http_proxy is safe to use because ENV is case sensitive.
|
21
|
+
proxy_uri = ENV[name]
|
22
|
+
else
|
23
|
+
proxy_uri = nil
|
24
|
+
end
|
25
|
+
else # http_proxy is safe to use because ENV is case sensitive.
|
26
|
+
proxy_uri = ENV.to_hash[name]
|
27
|
+
end
|
28
|
+
if !proxy_uri
|
29
|
+
# Use CGI_HTTP_PROXY. cf. libwww-perl.
|
30
|
+
proxy_uri = ENV["CGI_#{name.upcase}"]
|
31
|
+
end
|
32
|
+
elsif name == 'http_proxy'
|
33
|
+
unless proxy_uri = ENV[name]
|
34
|
+
if proxy_uri = ENV[name.upcase]
|
35
|
+
warn 'The environment variable HTTP_PROXY is discouraged. Use http_proxy.'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
else
|
39
|
+
proxy_uri = ENV[name] || ENV[name.upcase]
|
40
|
+
end
|
41
|
+
|
42
|
+
if proxy_uri.nil? || proxy_uri.empty?
|
43
|
+
return nil
|
44
|
+
end
|
45
|
+
|
46
|
+
if self.hostname
|
47
|
+
require 'socket'
|
48
|
+
begin
|
49
|
+
addr = IPSocket.getaddress(self.hostname)
|
50
|
+
return nil if /\A127\.|\A::1\z/ =~ addr
|
51
|
+
rescue SocketError
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
name = 'no_proxy'
|
56
|
+
if no_proxy = ENV[name] || ENV[name.upcase]
|
57
|
+
no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
|
58
|
+
if /(\A|\.)#{Regexp.quote host}\z/i =~ self.host &&
|
59
|
+
(!port || self.port == port.to_i)
|
60
|
+
return nil
|
61
|
+
end
|
62
|
+
}
|
63
|
+
end
|
64
|
+
URI.parse(proxy_uri)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: http_proxy_from_env
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- mechamogera
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-06-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Net::HTTP automatically detects and uses proxies from the environment.
|
47
|
+
email:
|
48
|
+
- mechamosura@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- http_proxy_from_env.gemspec
|
59
|
+
- lib/http_proxy_from_env.rb
|
60
|
+
- lib/http_proxy_from_env/net/http.rb
|
61
|
+
- lib/http_proxy_from_env/open_uri.rb
|
62
|
+
- lib/http_proxy_from_env/uri/generic.rb
|
63
|
+
- lib/http_proxy_from_env/version.rb
|
64
|
+
homepage: ''
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
hash: 2997952803154654772
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
hash: 2997952803154654772
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.25
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Net::HTTP automatically detects and uses proxies from the environment.
|
95
|
+
test_files: []
|