http.rb 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72cf60480847b43b60b63378d803acae19a12c127ceb9695ed8b0eca99ca57a4
4
- data.tar.gz: aed0e179795a1f408823cb5cecca6bb2c877a1e4a90c3a31f21a115a5270a2e6
3
+ metadata.gz: 2483f99ec13020c1e1c780b84c7dcddc68437687b1176e7fb88ba228adf084af
4
+ data.tar.gz: 710dd8eed5142ac832553595e581bd2597174dd1131fb14a7c51d4e7f17f4992
5
5
  SHA512:
6
- metadata.gz: b8e095a766f24d074324536e89dc07871bc8670785fe14f95470a06e64a4107e0c7c2df44f746c8dbd9c31d7b79336e8548aaf5601e4121db86f5787c1ee66bb
7
- data.tar.gz: c5637e2490a1605d4112162bad17c12d70e1536af814901c1bdc2572eb730f15e54b0eb8db2db3ff6057d872b9969b7852c5767872b9108ce257a160a9ebf24f
6
+ metadata.gz: dbe519101ddbd07038de07ca7bbcb28bbb2b7c53d0f650df86092433271469da3491326dce2d9c3698f78cb381de5a9f48b57054b0b8b8301adc1118e2cead87
7
+ data.tar.gz: 87e66da4b78467b289dafc3a280a7f9592af77e39f6878128c3586a0ea7ade24e2589239e8beb35d43c8e52e31ca79626f04d1ee1f31347b55f5ddf41945145a
data/CHANGELOG CHANGED
@@ -1,5 +1,19 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 20261011
4
+
5
+ 1.6.0: The response's uri is the URI it came from, which, where redirects were followed, is where they ended.
6
+
7
+ Nothing recorded where a response had come from: the request objects are made from a path rather than a URI, so Net::HTTP left response.uri nil, and a caller whose request was redirected had no way of knowing where to. A spider resolving the links on a page against the page's address needs it, as does anything noticing that it has been sent to a login page.
8
+
9
+ 1. ~ lib/HTTP/REDIRECT.rb: + HTTP.response_uri, which leaves out any credentials embedded in the URI, a response being something which is passed around and printed.
10
+ 2. ~ HTTP.request: response.uri is set to response_uri of the URI requested, so that after redirects it is the last of them.
11
+ 3. ~ HTTP::TooManyRedirects: the message names the URI of the redirect not followed as well as its Location.
12
+ 4. ~ test/HTTP/REDIRECT_test.rb: + the final URI without a redirect, after one, with no_redirect, and without embedded credentials; the URI of the redirect TooManyRedirects carries. All fail against 1.5.0.
13
+ 5. ~ README.md: + the response's uri, under Redirections, and in the Limiting redirections example.
14
+ 6. ~ HTTP::VERSION: /1.5.0/1.6.0/
15
+
16
+
3
17
  ## 20261011
4
18
 
5
19
  1.5.0: + max_redirects; + HTTP::TooManyRedirects.
data/README.md CHANGED
@@ -112,6 +112,14 @@ A body repeated keeps its `Content-Type`, form data's included. Where the method
112
112
 
113
113
  Redirects are followed with the headers they were given, except that `Authorization` and `Cookie`, and the `username:` and `password:` options, go no further than the origin (scheme, host, and port) they were given to. Where a redirect makes the request a GET, `Content-Type` is left behind, there being no body for it to describe.
114
114
 
115
+ The response's `uri` is the URI it came from, which, where redirects were followed, is where they ended:
116
+
117
+ ```ruby
118
+ response = HTTP.get('http://example.com/moved')
119
+ response.uri
120
+ # => #<URI::HTTP http://example.com/new_location>
121
+ ```
122
+
115
123
  ### Preventing redirections
116
124
 
117
125
  ```ruby
@@ -129,6 +137,7 @@ HTTP.get('http://example.com', {}, {}, {max_redirects: 5})
129
137
  begin
130
138
  HTTP.get('http://example.com/loop')
131
139
  rescue HTTP::TooManyRedirects => e
140
+ e.response.uri
132
141
  e.response['location']
133
142
  end
134
143
  ```
data/lib/HTTP/REDIRECT.rb CHANGED
@@ -13,7 +13,7 @@ module HTTP
13
13
 
14
14
  def initialize(response)
15
15
  @response = response
16
- super("Too many redirects: #{response.code} to #{response['location']}")
16
+ super("Too many redirects: #{response.code} from #{response.uri} to #{response['location']}")
17
17
  end
18
18
  end
19
19
 
@@ -68,4 +68,14 @@ module HTTP
68
68
  options.reject{|k, v| %i{username password}.include?(k)}
69
69
  end
70
70
  module_function :redirect_options
71
+
72
+ # Embedded credentials are left out, a response being something which is passed
73
+ # around and printed. URI#userinfo= ignores nil, whereas URI#user= takes the password
74
+ # with it.
75
+ def response_uri(uri)
76
+ uri = uri.dup
77
+ uri.user = nil if uri.user
78
+ uri
79
+ end
80
+ module_function :response_uri
71
81
  end
data/lib/HTTP/VERSION.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # HTTP::VERSION
3
3
 
4
4
  module HTTP
5
- VERSION = '1.5.0'
5
+ VERSION = '1.6.0'
6
6
  end
data/lib/HTTP/request.rb CHANGED
@@ -41,6 +41,7 @@ module HTTP
41
41
  http.request(request_object)
42
42
  end
43
43
  )
44
+ response.uri = response_uri(uri)
44
45
  if !no_redirect && redirect?(response)
45
46
  raise TooManyRedirects.new(response) if max_redirects < 1
46
47
  redirect_uri = uri.merge(response['location'])
@@ -7,6 +7,29 @@ describe "redirect behaviour" do
7
7
  let(:same_origin_uri){'http://example.com/elsewhere'}
8
8
  let(:other_origin_uri){'http://redirected.com/path'}
9
9
 
10
+ describe "the final URI" do
11
+ it "is the requested URI when there is no redirect" do
12
+ stub_request(:get, uri).to_return(status: 200)
13
+ _(HTTP.get(uri).uri.to_s).must_equal(uri)
14
+ end
15
+
16
+ it "is the URI redirected to when a redirect is followed" do
17
+ stub_request(:get, uri).to_return(status: 302, headers: {'location' => other_origin_uri})
18
+ stub_request(:get, other_origin_uri).to_return(status: 200)
19
+ _(HTTP.get(uri).uri.to_s).must_equal(other_origin_uri)
20
+ end
21
+
22
+ it "is the URI which redirected when no_redirect is set" do
23
+ stub_request(:get, uri).to_return(status: 302, headers: {'location' => other_origin_uri})
24
+ _(HTTP.get(uri, {}, {}, {no_redirect: true}).uri.to_s).must_equal(uri)
25
+ end
26
+
27
+ it "carries no embedded credentials" do
28
+ stub_request(:get, uri).with(basic_auth: ['alice', 'secret']).to_return(status: 200)
29
+ _(HTTP.get('http://alice:secret@example.com/path').uri.to_s).must_equal(uri)
30
+ end
31
+ end
32
+
10
33
  describe "headers" do
11
34
  %w{301 302 303 307 308}.each do |code|
12
35
  it "are carried through a #{code}" do
@@ -230,6 +253,7 @@ describe "redirect behaviour" do
230
253
  it "raises TooManyRedirects, with the redirect not followed, when there are more" do
231
254
  error = _(->{HTTP.get('http://example.com/0', {}, {}, {max_redirects: 1})}).must_raise(HTTP::TooManyRedirects)
232
255
  _(error.response['location']).must_equal('/2')
256
+ _(error.response.uri.to_s).must_equal('http://example.com/1')
233
257
  assert_not_requested(:get, 'http://example.com/2')
234
258
  end
235
259
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran