Project

General

Profile

Feature #6546 ยป net.http.proxy_from_env.patch

drbrain (Eric Hodel), 06/09/2012 10:00 AM

View differences:

lib/net/http.rb (working copy)
277 277
  #     # always connect to your.proxy.addr:8080
278 278
  #   }
279 279
  #
280
  # You may also create a proxy from the HTTP_PROXY environment variable:
281
  #
282
  #   Net::HTTP::Proxy(:ENV).start('www.example.com') { |http|
283
  #     # always connect through ENV['HTTP_PROXY']
284
  #   }
285
  #
280 286
  # Net::HTTP::Proxy returns a Net::HTTP instance when proxy_addr is nil so
281 287
  # there is no need for conditional code.
282 288
  #
......
882 888
    # the port to use to access the proxy, and a username and password
883 889
    # if authorization is required to use the proxy.
884 890
    #
891
    # You may also supply <code>:ENV</code> to read proxy settings from the
892
    # environment variables +HTTP_PROXY+, +HTTP_PROXY_USER+ and
893
    # +HTTP_PROXY_PASS+ (or their lowercase equivalents).
894
    #
885 895
    # You can replace any use of the Net::HTTP class with use of the
886 896
    # proxy class created.
887 897
    #
......
907 917
    #     # and password
908 918
    #   }
909 919
    #
910
    # Note that net/http does not use the HTTP_PROXY environment variable.
911
    # If you want to use a proxy, you must set it explicitly.
912
    #
913 920
    def HTTP.Proxy(p_addr, p_port = nil, p_user = nil, p_pass = nil)
914 921
      return self unless p_addr
915
      delta = ProxyDelta
916
      proxyclass = Class.new(self)
917
      proxyclass.module_eval {
918
        include delta
922

  
923
      if p_addr == :ENV then
924
        env_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
925

  
926
        return self unless env_proxy
927

  
928
        env_proxy =~ /:([^\]]+)\z/ # handle IPv6 address like [::1]:8000
929
        p_addr = $` || env_proxy
930
        p_port = $1.to_i if $1
931

  
932
        p_user = ENV['http_proxy_user'] || ENV['HTTP_PROXY_USER']
933
        p_pass = ENV['http_proxy_pass'] || ENV['HTTP_PROXY_PASS']
934
      end
935

  
936
      Class.new(self) {
937
        include Net::HTTP::ProxyDelta
919 938
        # with proxy
920 939
        @is_proxy_class = true
921 940
        @proxy_address = p_addr
......
923 942
        @proxy_user    = p_user
924 943
        @proxy_pass    = p_pass
925 944
      }
926
      proxyclass
927 945
    end
928 946

  
929 947
    class << HTTP
test/net/http/test_http.rb (working copy)
5 5
require 'stringio'
6 6
require_relative 'utils'
7 7

  
8
class TestNetHTTP < Test::Unit::TestCase
9

  
10
  def test_class_Proxy
11
    no_proxy_class = Net::HTTP.Proxy nil
12

  
13
    assert_equal Net::HTTP, no_proxy_class
14

  
15
    proxy_class = Net::HTTP.Proxy 'proxy.example', 8000, 'user', 'pass'
16

  
17
    refute_equal Net::HTTP, proxy_class
18

  
19
    assert_operator proxy_class, :<, Net::HTTP
20

  
21
    assert_equal 'proxy.example', proxy_class.proxy_address
22
    assert_equal 8000,            proxy_class.proxy_port
23
    assert_equal 'user',          proxy_class.proxy_user
24
    assert_equal 'pass',          proxy_class.proxy_pass
25
  end
26

  
27
  def test_class_Proxy_from_ENV
28
    clean_http_proxy_env do
29
      ENV['HTTP_PROXY']      = 'proxy.example:8000'
30
      ENV['HTTP_PROXY_USER'] = 'user'
31
      ENV['HTTP_PROXY_PASS'] = 'pass'
32

  
33
      proxy_class = Net::HTTP.Proxy :ENV
34

  
35
      refute_equal Net::HTTP, proxy_class
36

  
37
      assert_operator proxy_class, :<, Net::HTTP
38

  
39
      assert_equal 'proxy.example', proxy_class.proxy_address
40
      assert_equal 8000,            proxy_class.proxy_port
41
      assert_equal 'user',          proxy_class.proxy_user
42
      assert_equal 'pass',          proxy_class.proxy_pass
43
    end
44
  end
45

  
46
  def test_class_Proxy_from_ENV_ipv6
47
    clean_http_proxy_env do
48
      ENV['HTTP_PROXY'] = '[::1]:8000'
49

  
50
      proxy_class = Net::HTTP.Proxy :ENV
51

  
52
      refute_equal Net::HTTP, proxy_class
53

  
54
      assert_operator proxy_class, :<, Net::HTTP
55

  
56
      assert_equal '[::1]', proxy_class.proxy_address
57
      assert_equal 8000,    proxy_class.proxy_port
58
      assert_nil            proxy_class.proxy_user
59
      assert_nil            proxy_class.proxy_pass
60
    end
61
  end
62

  
63
  def test_class_Proxy_from_ENV_lowercase
64
    clean_http_proxy_env do
65
      ENV['http_proxy']      = 'proxy.example:8000'
66
      ENV['http_proxy_user'] = 'user'
67
      ENV['http_proxy_pass'] = 'pass'
68

  
69
      proxy_class = Net::HTTP.Proxy :ENV
70

  
71
      refute_equal Net::HTTP, proxy_class
72

  
73
      assert_operator proxy_class, :<, Net::HTTP
74

  
75
      assert_equal 'proxy.example', proxy_class.proxy_address
76
      assert_equal 8000,            proxy_class.proxy_port
77
      assert_equal 'user',          proxy_class.proxy_user
78
      assert_equal 'pass',          proxy_class.proxy_pass
79
    end
80
  end
81

  
82
  def test_class_Proxy_from_ENV_none
83
    clean_http_proxy_env do
84
      no_proxy_class = Net::HTTP.Proxy :ENV
85

  
86
      assert_equal Net::HTTP, no_proxy_class
87
    end
88
  end
89

  
90
  def test_class_Proxy_from_ENV_no_port
91
    clean_http_proxy_env do
92
      ENV['HTTP_PROXY'] = 'proxy.example'
93

  
94
      proxy_class = Net::HTTP.Proxy :ENV
95

  
96
      refute_equal Net::HTTP, proxy_class
97

  
98
      assert_operator proxy_class, :<, Net::HTTP
99

  
100
      assert_equal 'proxy.example', proxy_class.proxy_address
101
      assert_equal 80,              proxy_class.proxy_port
102
      assert_nil                    proxy_class.proxy_user
103
      assert_nil                    proxy_class.proxy_pass
104
    end
105
  end
106

  
107
  def clean_http_proxy_env
108
    orig = {
109
      'HTTP_PROXY'      => ENV['HTTP_PROXY'],
110
      'http_proxy'      => ENV['HTTP_PROXY'],
111
      'HTTP_PROXY_USER' => ENV['HTTP_PROXY_USER'],
112
      'http_proxy_user' => ENV['http_proxy_user'],
113
      'HTTP_PROXY_PASS' => ENV['HTTP_PROXY_PASS'],
114
      'http_proxy_pass' => ENV['http_proxy_pass'],
115
    }
116

  
117
    orig.each_key do |key|
118
      ENV.delete key
119
    end
120

  
121
    yield
122
  ensure
123
    orig.each do |key, value|
124
      ENV[key] = value
125
    end
126
  end
127

  
128
end
129

  
8 130
module TestNetHTTP_version_1_1_methods
9 131

  
10 132
  def test_s_get