net.http.request_response_uri.4.patch

Eric Hodel, 12/20/2012 02:11 PM

Download (10.5 KB)

View differences:

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}>"
......
82 96
  #
83 97

  
84 98
  def exec(sock, ver, path)   #:nodoc: internal use only
99
    self['host'] = "#{@uri.host}:#{@uri.port}" if @uri
100

  
85 101
    if @body
86 102
      send_request_with_body sock, ver, path, @body
87 103
    elsif @body_stream
......
93 109
    end
94 110
  end
95 111

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

  
115
    @uri.host ||= host
116
    @uri.port = port
117

  
118
    scheme = ssl ? 'https' : 'http'
119

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

  
126
    @uri
127
  end
128

  
96 129
  private
97 130

  
98 131
  class Chunker #:nodoc:
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.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
......
472 476
        uri = uri_or_host
473 477
        start(uri.hostname, uri.port,
474 478
              :use_ssl => uri.scheme == 'https') {|http|
475
          return http.request_get(uri.request_uri, &block)
479
          return http.request_get(uri, &block)
476 480
        }
477 481
      end
478 482
    end
......
496 500
    #                  { "q" => "ruby", "max" => "50" }
497 501
    #
498 502
    def HTTP.post_form(url, params)
499
      req = Post.new(url.request_uri)
503
      req = Post.new(url)
500 504
      req.form_data = params
501 505
      req.basic_auth url.user, url.password if url.user
502 506
      start(url.hostname, url.port,
......
868 872
        conn_port    = port
869 873
      end
870 874

  
871
      D "opening connection to #{conn_address}..."
875
      D "opening connection to #{conn_address}:#{conn_port}..."
872 876
      s = Timeout.timeout(@open_timeout, Net::OpenTimeout) {
873 877
        TCPSocket.open(conn_address, conn_port, @local_host, @local_port)
874 878
      }
......
884 888
        end
885 889
        @ssl_context = OpenSSL::SSL::SSLContext.new
886 890
        @ssl_context.set_params(ssl_parameters)
891
        D "starting SSL for #{conn_address}:#{conn_port}..."
887 892
        s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
888 893
        s.sync_close = true
894
        D "SSL established"
889 895
      end
890 896
      @socket = BufferedIO.new(s)
891 897
      @socket.read_timeout = @read_timeout
......
1077 1083

  
1078 1084
    public
1079 1085

  
1080
    # Gets data from +path+ on the connected-to host.
1086
    # Retrieves data from +path+ on the connected-to host which may be an
1087
    # absolute path String or a URI to extract the path from.
1088
    #
1081 1089
    # +initheader+ must be a Hash like { 'Accept' => '*/*', ... },
1082 1090
    # and it defaults to an empty hash.
1083 1091
    # If +initheader+ doesn't have the key 'accept-encoding', then
......
1403 1411
          begin
1404 1412
            res = HTTPResponse.read_new(@socket)
1405 1413
          end while res.kind_of?(HTTPContinue)
1414

  
1415
          res.uri = req.uri
1416

  
1406 1417
          res.reading_body(@socket, req.response_body_permitted?) {
1407 1418
            yield res if block_given?
1408 1419
          }
......
1444 1455
      if not req.response_body_permitted? and @close_on_empty_response
1445 1456
        req['connection'] ||= 'close'
1446 1457
      end
1458

  
1459
      host = req['host'] || address
1460
      host = $1 if host =~ /(.*):\d+$/
1461
      req.update_uri host, port, use_ssl?
1462

  
1447 1463
      req['host'] ||= addr_port()
1448 1464
    end
1449 1465

  
test/net/http/test_http.rb (working copy)
409 409
      _test_request__HEAD http
410 410
      _test_request__POST http
411 411
      _test_request__stream_body http
412
      _test_request__uri http
413
      _test_request__uri_host http
412 414
    }
413 415
  end
414 416

  
......
488 490
    assert_equal data, res.body
489 491
  end
490 492

  
493
  def _test_request__path(http)
494
    uri = URI 'https://example/'
495
    req = Net::HTTP::Get.new('/')
496

  
497
    res = http.request(req)
498

  
499
    assert_kind_of URI::Generic, req.uri
500

  
501
    refute_equal uri, req.uri
502

  
503
    assert_equal uri, res.uri
504

  
505
    refute_same uri,     req.uri
506
    refute_same req.uri, res.uri
507
  end
508

  
509
  def _test_request__uri(http)
510
    uri = URI 'https://example/'
511
    req = Net::HTTP::Get.new(uri)
512

  
513
    res = http.request(req)
514

  
515
    assert_kind_of URI::Generic, req.uri
516

  
517
    refute_equal uri, req.uri
518

  
519
    assert_equal req.uri, res.uri
520

  
521
    refute_same uri,     req.uri
522
    refute_same req.uri, res.uri
523
  end
524

  
525
  def _test_request__uri_host(http)
526
    uri = URI 'http://example/'
527

  
528
    req = Net::HTTP::Get.new(uri)
529
    req['host'] = 'other.example'
530

  
531
    res = http.request(req)
532

  
533
    assert_kind_of URI::Generic, req.uri
534

  
535
    assert_equal URI("http://example:#{http.port}"), res.uri
536
  end
537

  
491 538
  def test_send_request
492 539
    start {|http|
493 540
      _test_send_request__GET http
......
837 884
    assert_equal(http.local_port, res.body)
838 885
  end
839 886
end
887

  
test/net/http/test_httpresponse.rb (working copy)
203 203
    assert_equal 'hello', body
204 204
  end
205 205

  
206
  def test_uri_equals
207
    uri = URI 'http://example'
208

  
209
    response = Net::HTTPResponse.new '1.1', 200, 'OK'
210

  
211
    response.uri = nil
212

  
213
    assert_nil response.uri
214

  
215
    response.uri = uri
216

  
217
    assert_equal uri, response.uri
218
    refute_same  uri, response.uri
219
  end
220

  
206 221
private
207 222

  
208 223
  def dummy_io(str)