net.http.request_response_uri.3.patch

Remove bit rot from patch 2 - Eric Hodel, 07/21/2012 08:16 AM

Download (9.94 KB)

View differences:

lib/net/http/response.rb (working copy)
79 79
    initialize_http_header nil
80 80
    @body = nil
81 81
    @read = false
82
    @uri  = nil
82 83
  end
83 84

  
84 85
  # The HTTP version supported by the server.
......
93 94
  attr_reader :message
94 95
  alias msg message   # :nodoc: obsolete
95 96

  
97
  # The URI used to fetch this response.  The response URI is only available
98
  # if a URI was used to create the request.
99
  attr_reader :uri
100

  
96 101
  def inspect
97 102
    "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
98 103
  end
......
118 123
    error! unless self.kind_of?(Net::HTTPSuccess)
119 124
  end
120 125

  
126
  def uri= uri # :nodoc:
127
    @uri = uri.dup if uri
128
  end
129

  
121 130
  #
122 131
  # header (for backward compatibility only; DO NOT USE)
123 132
  #
lib/net/http/generic_request.rb (working copy)
7 7

  
8 8
  include Net::HTTPHeader
9 9

  
10
  def initialize(m, reqbody, resbody, path, initheader = nil)
10
  def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
11 11
    @method = m
12 12
    @request_has_body = reqbody
13 13
    @response_has_body = resbody
14

  
15
    if URI === uri_or_path then
16
      @uri = uri_or_path.dup
17
      host = @uri.hostname
18
      host += ":#{@uri.port}" if @uri.port != @uri.class::DEFAULT_PORT
19
      path = uri_or_path.request_uri
20
    else
21
      @uri = nil
22
      host = nil
23
      path = uri_or_path
24
    end
25

  
14 26
    raise ArgumentError, "no HTTP request path given" unless path
15 27
    raise ArgumentError, "HTTP request path is empty" if path.empty?
16 28
    @path = path
......
29 41
    initialize_http_header initheader
30 42
    self['Accept'] ||= '*/*'
31 43
    self['User-Agent'] ||= 'Ruby'
44
    self['Host'] ||= host
32 45
    @body = nil
33 46
    @body_stream = nil
34 47
    @body_data = nil
......
36 49

  
37 50
  attr_reader :method
38 51
  attr_reader :path
52
  attr_reader :uri
39 53

  
40 54
  def inspect
41 55
    "\#<#{self.class} #{@method}>"
......
93 107
    end
94 108
  end
95 109

  
110
  def update_uri(host, port, ssl) # :nodoc: internal use only
111
    return unless @uri
112

  
113
    @uri.host = host
114
    @uri.port = port
115

  
116
    scheme = ssl ? 'https' : 'http'
117

  
118
    # convert the class of the URI
119
    unless scheme == @uri.scheme then
120
      new_uri = @uri.to_s.sub(/^https?/, scheme)
121
      @uri = URI new_uri
122
    end
123

  
124
    @uri
125
  end
126

  
96 127
  private
97 128

  
98 129
  class Chunker #:nodoc:
lib/net/http.rb (working copy)
93 93
  #   uri = URI('http://example.com/some_path?query=string')
94 94
  #
95 95
  #   Net::HTTP.start(uri.host, uri.port) do |http|
96
  #     request = Net::HTTP::Get.new uri.request_uri
96
  #     request = Net::HTTP::Get.new uri
97 97
  #
98 98
  #     response = http.request request # Net::HTTPResponse object
99 99
  #   end
......
111 111
  # will automatically open a connection to the server if one is not currently
112 112
  # open.  You can manually close the connection with #finish.
113 113
  #
114
  # For all the Net::HTTP request objects and shortcut request methods you may
115
  # supply either a String for the request path or a URI from which Net::HTTP
116
  # will extract the request path.
117
  #
114 118
  # === Response Data
115 119
  #
116 120
  #   uri = URI('http://example.com/index.html')
......
168 172
  # creates a urlencoded POST body:
169 173
  #
170 174
  #   uri = URI('http://www.example.com/todo.cgi')
171
  #   req = Net::HTTP::Post.new(uri.path)
175
  #   req = Net::HTTP::Post.new(uri)
172 176
  #   req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')
173 177
  #
174 178
  #   res = Net::HTTP.start(uri.hostname, uri.port) do |http|
......
186 190
  # multipart/form-data use Net::HTTPRequest#body= and
187 191
  # Net::HTTPRequest#content_type=:
188 192
  #
189
  #   req = Net::HTTP::Post.new(uri.path)
193
  #   req = Net::HTTP::Post.new(uri)
190 194
  #   req.body = multipart_data
191 195
  #   req.content_type = 'multipart/form-data'
192 196
  #
......
203 207
  #   uri = URI('http://example.com/cached_response')
204 208
  #   file = File.stat 'cached_response'
205 209
  #
206
  #   req = Net::HTTP::Get.new(uri.request_uri)
210
  #   req = Net::HTTP::Get.new(uri)
207 211
  #   req['If-Modified-Since'] = file.mtime.rfc2822
208 212
  #
209 213
  #   res = Net::HTTP.start(uri.hostname, uri.port) {|http|
......
221 225
  #
222 226
  #   uri = URI('http://example.com/index.html?key=value')
223 227
  #
224
  #   req = Net::HTTP::Get.new(uri.request_uri)
228
  #   req = Net::HTTP::Get.new(uri)
225 229
  #   req.basic_auth 'user', 'pass'
226 230
  #
227 231
  #   res = Net::HTTP.start(uri.hostname, uri.port) {|http|
......
238 242
  #   uri = URI('http://example.com/large_file')
239 243
  #
240 244
  #   Net::HTTP.start(uri.host, uri.port) do |http|
241
  #     request = Net::HTTP::Get.new uri.request_uri
245
  #     request = Net::HTTP::Get.new uri
242 246
  #
243 247
  #     http.request request do |response|
244 248
  #       open 'large_file', 'w' do |io|
......
257 261
  #
258 262
  #   Net::HTTP.start(uri.host, uri.port,
259 263
  #     :use_ssl => uri.scheme == 'https') do |http|
260
  #     request = Net::HTTP::Get.new uri.request_uri
264
  #     request = Net::HTTP::Get.new uri
261 265
  #
262 266
  #     response = http.request request # Net::HTTPResponse object
263 267
  #   end
......
471 475
        uri = uri_or_host
472 476
        start(uri.hostname, uri.port,
473 477
              :use_ssl => uri.scheme == 'https') {|http|
474
          return http.request_get(uri.request_uri, &block)
478
          return http.request_get(uri, &block)
475 479
        }
476 480
      end
477 481
    end
......
495 499
    #                  { "q" => "ruby", "max" => "50" }
496 500
    #
497 501
    def HTTP.post_form(url, params)
498
      req = Post.new(url.request_uri)
502
      req = Post.new(url)
499 503
      req.form_data = params
500 504
      req.basic_auth url.user, url.password if url.user
501 505
      start(url.hostname, url.port,
......
862 866
        conn_port    = port
863 867
      end
864 868

  
865
      D "opening connection to #{conn_address}..."
869
      D "opening connection to #{conn_address}:#{conn_port}..."
866 870
      s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
867 871
        TCPSocket.open(conn_address, conn_port, @local_host, @local_port)
868 872
      }
......
878 882
        end
879 883
        @ssl_context = OpenSSL::SSL::SSLContext.new
880 884
        @ssl_context.set_params(ssl_parameters)
885
        D "starting SSL for #{conn_address}:#{conn_port}..."
881 886
        s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
882 887
        s.sync_close = true
888
        D "SSL established"
883 889
      end
884 890
      @socket = BufferedIO.new(s)
885 891
      @socket.read_timeout = @read_timeout
......
1068 1074

  
1069 1075
    public
1070 1076

  
1071
    # Gets data from +path+ on the connected-to host.
1077
    # Retrieves data from +path+ on the connected-to host which may be an
1078
    # absolute path String or a URI to extract the path from.
1079
    #
1072 1080
    # +initheader+ must be a Hash like { 'Accept' => '*/*', ... },
1073 1081
    # and it defaults to an empty hash.
1074 1082
    # If +initheader+ doesn't have the key 'accept-encoding', then
......
1394 1402
          begin
1395 1403
            res = HTTPResponse.read_new(@socket)
1396 1404
          end while res.kind_of?(HTTPContinue)
1405

  
1406
          res.uri = req.uri
1407

  
1397 1408
          res.reading_body(@socket, req.response_body_permitted?) {
1398 1409
            yield res if block_given?
1399 1410
          }
......
1435 1446
      if not req.response_body_permitted? and @close_on_empty_response
1436 1447
        req['connection'] ||= 'close'
1437 1448
      end
1449

  
1450
      host = req['host'] || address
1451
      host = $1 if host =~ /(.*):\d+$/
1452
      req.update_uri host, port, use_ssl?
1453

  
1438 1454
      req['host'] ||= addr_port()
1439 1455
    end
1440 1456

  
test/net/http/test_httpresponse.rb (working copy)
184 184
    assert_equal 'hello', body
185 185
  end
186 186

  
187
  def test_uri_equals
188
    uri = URI 'http://example'
189

  
190
    response = Net::HTTPResponse.new '1.1', 200, 'OK'
191

  
192
    response.uri = nil
193

  
194
    assert_nil response.uri
195

  
196
    response.uri = uri
197

  
198
    assert_equal uri, response.uri
199
    refute_same  uri, response.uri
200
  end
201

  
187 202
private
188 203

  
189 204
  def dummy_io(str)
test/net/http/test_http.rb (working copy)
385 385
      _test_request__HEAD http
386 386
      _test_request__POST http
387 387
      _test_request__stream_body http
388
      _test_request__uri http
389
      _test_request__uri_host http
388 390
    }
389 391
  end
390 392

  
......
464 466
    assert_equal data, res.body
465 467
  end
466 468

  
469
  def _test_request__uri(http)
470
    uri = URI 'https://example/'
471
    req = Net::HTTP::Get.new(uri)
472

  
473
    res = http.request(req)
474

  
475
    assert_kind_of URI::Generic, req.uri
476

  
477
    refute_equal uri, req.uri
478

  
479
    assert_equal req.uri, res.uri
480

  
481
    refute_same uri,     req.uri
482
    refute_same req.uri, res.uri
483
  end
484

  
485
  def _test_request__uri_host(http)
486
    uri = URI 'http://example/'
487

  
488
    req = Net::HTTP::Get.new(uri)
489
    req['host'] = 'other.example'
490

  
491
    res = http.request(req)
492

  
493
    assert_kind_of URI::Generic, req.uri
494

  
495
    assert_equal URI("http://other.example:#{http.port}"), res.uri
496
  end
497

  
467 498
  def test_send_request
468 499
    start {|http|
469 500
      _test_send_request__GET http
......
813 844
    assert_equal(http.local_port, res.body)
814 845
  end
815 846
end
847