net.http.request_response_uri.patch

Eric Hodel, 05/23/2012 11:48 AM

Download (12.9 KB)

View differences:

lib/net/http/response.rb (working copy)
132 132
    initialize_http_header nil
133 133
    @body = nil
134 134
    @read = false
135
    @uri  = nil
135 136
  end
136 137

  
137 138
  # The HTTP version supported by the server.
......
146 147
  attr_reader :message
147 148
  alias msg message   # :nodoc: obsolete
148 149

  
150
  # The URI used to fetch this response.  The response URI is only available
151
  # if a URI was used to create the request.
152
  attr_reader :uri
153

  
149 154
  def inspect
150 155
    "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
151 156
  end
......
171 176
    error! unless self.kind_of?(Net::HTTPSuccess)
172 177
  end
173 178

  
179
  def uri= uri # :nodoc:
180
    @uri = uri.dup if uri
181
  end
182

  
174 183
  #
175 184
  # header (for backward compatibility only; DO NOT USE)
176 185
  #
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
      path = uri_or_path.request_uri
18
    else
19
      @uri = nil
20
      path = uri_or_path
21
    end
22

  
14 23
    raise ArgumentError, "no HTTP request path given" unless path
15 24
    raise ArgumentError, "HTTP request path is empty" if path.empty?
16 25
    @path = path
......
24 33

  
25 34
  attr_reader :method
26 35
  attr_reader :path
36
  attr_reader :uri
27 37

  
28 38
  def inspect
29 39
    "\#<#{self.class} #{@method}>"
......
81 91
    end
82 92
  end
83 93

  
94
  def update_uri(host, port, ssl) # :nodoc: internal use only
95
    return unless @uri
96

  
97
    @uri.host = host
98
    @uri.port = port
99

  
100
    scheme = ssl ? 'https' : 'http'
101

  
102
    # convert the class of the URI
103
    unless scheme == @uri.scheme then
104
      new_uri = @uri.to_s.sub(/^https?/, scheme)
105
      @uri = URI new_uri
106
    end
107

  
108
    @uri
109
  end
110

  
84 111
  private
85 112

  
86 113
  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
......
453 457
        uri = uri_or_host
454 458
        start(uri.hostname, uri.port,
455 459
              :use_ssl => uri.scheme == 'https') {|http|
456
          return http.request_get(uri.request_uri, &block)
460
          return http.request_get(uri, &block)
457 461
        }
458 462
      end
459 463
    end
......
477 481
    #                  { "q" => "ruby", "max" => "50" }
478 482
    #
479 483
    def HTTP.post_form(url, params)
480
      req = Post.new(url.request_uri)
484
      req = Post.new(url)
481 485
      req.form_data = params
482 486
      req.basic_auth url.user, url.password if url.user
483 487
      start(url.hostname, url.port,
......
992 996

  
993 997
    public
994 998

  
995
    # Gets data from +path+ on the connected-to host.
999
    # Retrieves data from +path+ on the connected-to host which may be an
1000
    # absolute path String or a URI to extract the path from.
1001
    #
996 1002
    # +initheader+ must be a Hash like { 'Accept' => '*/*', ... },
997 1003
    # and it defaults to an empty hash.
998 1004
    # If +initheader+ doesn't have the key 'accept-encoding', then
......
1336 1342
          begin
1337 1343
            res = HTTPResponse.read_new(@socket)
1338 1344
          end while res.kind_of?(HTTPContinue)
1345

  
1346
          res.uri = req.uri
1347

  
1339 1348
          res.reading_body(@socket, req.response_body_permitted?) {
1340 1349
            yield res if block_given?
1341 1350
          }
......
1377 1386
      if not req.response_body_permitted? and @close_on_empty_response
1378 1387
        req['connection'] ||= 'close'
1379 1388
      end
1389

  
1390
      req.update_uri address, port, use_ssl?
1391

  
1380 1392
      req['host'] ||= addr_port()
1381 1393
    end
1382 1394

  
test/net/http/test_httpresponse.rb (working copy)
32 32
    assert_equal('XXX YYY', res.header['x-bar'])
33 33
  end
34 34

  
35
  def test_uri_equals
36
    uri = URI 'http://example'
37

  
38
    response = Net::HTTPResponse.new '1.1', 200, 'OK'
39

  
40
    response.uri = nil
41

  
42
    assert_nil response.uri
43

  
44
    response.uri = uri
45

  
46
    assert_equal uri, response.uri
47
    refute_same  uri, response.uri
48
  end
49

  
35 50
private
36 51

  
37 52
  def dummy_io(str)
test/net/http/test_http.rb (working copy)
224 224
      _test_request__HEAD http
225 225
      _test_request__POST http
226 226
      _test_request__stream_body http
227
      _test_request__uri http
227 228
    }
228 229
  end
229 230

  
......
303 304
    assert_equal data, res.body
304 305
  end
305 306

  
307
  def _test_request__uri(http)
308
    uri = URI 'https://example/'
309
    req = Net::HTTP::Get.new(uri)
310

  
311
    res = http.request(req)
312

  
313
    assert_kind_of URI::Generic, req.uri
314

  
315
    refute_equal uri, req.uri
316

  
317
    assert_equal req.uri, res.uri
318
  end
319

  
306 320
  def test_send_request
307 321
    start {|http|
308 322
      _test_send_request__GET http
......
616 630
    }
617 631
  end
618 632
end
633

  
test/net/http/test_http_generic_request.rb (revision 0)
1
require 'net/http'
2
require 'test/unit'
3
require 'stringio'
4

  
5
class HTTPGenericResponseTest < Test::Unit::TestCase
6

  
7
  def setup
8
    super
9

  
10
    @GR = Net::HTTPGenericRequest
11

  
12
    @request = @GR.new 'POST', true, true, '/'
13
  end
14

  
15
  def test_initialize
16
    request = @GR.new 'GET', false, true, '/index', 'X-Header' => 'value'
17

  
18
    assert_equal 'GET',    request.method
19
    assert_equal '/index', request.path
20
    assert_nil             request.uri
21

  
22
    assert request.response_body_permitted?
23
    refute request.request_body_permitted?
24

  
25
    assert_equal 'value', request['X-Header']
26
  end
27

  
28
  def test_initialize_uri
29
    uri = URI 'http://example/index?hello=world'
30
    request = @GR.new 'GET', false, true, uri, 'X-Header' => 'value'
31

  
32
    assert_equal 'GET',                request.method
33
    assert_equal '/index?hello=world', request.path
34
    assert_equal uri,                  request.uri
35
    refute_same  uri,                  request.uri
36

  
37
    assert request.response_body_permitted?
38
    refute request.request_body_permitted?
39

  
40
    assert_equal 'value', request['X-Header']
41
  end
42

  
43
  def test_body_equals
44
    @request.body = 'hello'
45

  
46
    assert_equal 'hello', @request.body
47
  end
48

  
49
  def test_body_stream_equals
50
    @request.body_stream = StringIO.new 'hello'
51

  
52
    assert_equal 'hello', @request.body_stream.read
53
  end
54

  
55
  def test_set_body_internal
56
    @request.set_body_internal 'hello'
57

  
58
    assert_equal 'hello', @request.body
59
  end
60

  
61
  def test_set_body_internal_already_set
62
    @request.body = 'hello'
63

  
64
    assert_raises ArgumentError do
65
      @request.set_body_internal 'other'
66
    end
67

  
68
    @request.body = nil
69
    @request.body_stream = StringIO.new 'hello'
70

  
71
    assert_raises ArgumentError do
72
      @request.set_body_internal 'other'
73
    end
74
  end
75

  
76
  def test_send_request_with_body
77
    socket = StringIO.new
78
    def socket.continue_timeout() end
79

  
80
    @request.send :send_request_with_body, socket, '1.1', '/', 'hello'
81

  
82
    expected = <<-EXPECTED.chomp
83
POST / HTTP/1.1\r
84
Accept: */*\r
85
User-Agent: Ruby\r
86
Content-Length: 5\r
87
Content-Type: application/x-www-form-urlencoded\r
88
\r
89
hello
90
    EXPECTED
91

  
92
    assert_equal expected, socket.string
93
  end
94

  
95
  def test_send_request_with_body_stream
96
    stream = StringIO.new 'hello'
97
    @request['transfer-encoding'] = 'chunked'
98

  
99
    socket = StringIO.new
100
    def socket.continue_timeout() end
101

  
102
    @request.send :send_request_with_body_stream, socket, '1.1', '/', stream
103

  
104
    expected = <<-EXPECTED
105
POST / HTTP/1.1\r
106
Accept: */*\r
107
User-Agent: Ruby\r
108
Transfer-Encoding: chunked\r
109
Content-Type: application/x-www-form-urlencoded\r
110
\r
111
5\r
112
hello\r
113
0\r
114
\r
115
    EXPECTED
116

  
117
    assert_equal expected, socket.string
118
  end
119

  
120
  def test_send_request_with_body_data_multipart
121
    params = [%w[hello world]]
122
    socket = StringIO.new
123
    def socket.continue_timeout() end
124

  
125
    @request.set_form params, 'multipart/form-data'
126

  
127
    @request.send :send_request_with_body_data, socket, '1.1', '/', params
128

  
129
    sent = socket.string
130

  
131
    sent =~ /^Content-Type: multipart\/form-data; boundary=(.*?)\r\n/
132

  
133
    boundary = $1
134

  
135
    sent = sent.gsub(/#{Regexp.escape boundary}/, '<boundary>')
136

  
137
    expected = <<-EXPECTED
138
POST / HTTP/1.1\r
139
Accept: */*\r
140
User-Agent: Ruby\r
141
Content-Type: multipart/form-data; boundary=<boundary>\r
142
Content-Length: 173\r
143
\r
144
--<boundary>\r
145
Content-Disposition: form-data; name="hello"\r
146
\r
147
world\r
148
--<boundary>--\r
149
    EXPECTED
150

  
151
    assert_equal expected, sent
152
  end
153

  
154
  def test_send_request_with_body_data_urlencoded
155
    params = %w[hello world]
156
    socket = StringIO.new
157
    def socket.continue_timeout() end
158

  
159
    @request.send :send_request_with_body_data, socket, '1.1', '/', params
160

  
161
    expected = <<-EXPECTED.chomp
162
POST / HTTP/1.1\r
163
Accept: */*\r
164
User-Agent: Ruby\r
165
Content-Type: application/x-www-form-urlencoded\r
166
Content-Length: 11\r
167
\r
168
hello&world
169
    EXPECTED
170

  
171
    assert_equal expected, socket.string
172
  end
173

  
174
  def test_update_uri
175
    uri = URI 'http://example/index'
176
    request = @GR.new 'GET', false, true, uri, 'X-Header' => 'value'
177

  
178
    request.update_uri 'other.example', 443, true
179

  
180
    assert_equal URI('https://other.example:443/index'), request.uri
181
  end
182

  
183
  def test_update_uri_path
184
    @request.update_uri 'other.example', 80, false
185

  
186
    assert_nil @request.uri
187
  end
188

  
189
end
190