pretty_proxy 1.0.2 → 2.0.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
  SHA1:
3
- metadata.gz: 718ffdcdc9a88b9103bfe1aa2e32cc32039a8b20
4
- data.tar.gz: 4a5614d06ed052826da3f9eaac0e443b2ef82946
3
+ metadata.gz: 53c916eb8a133fba2863121b82f713be339870d9
4
+ data.tar.gz: 20203a8923d5fdf6a74a28f88fb9cd48ed29ec0a
5
5
  SHA512:
6
- metadata.gz: 633d5ca4cb8cd20c745a17eefa402eb9328da09c06b13aeccecdf74dbfd6a16c3511f0e1d3dfae190687787f36508d4e089c69a061a46c69cf3e264c097b12b0
7
- data.tar.gz: c00c15756541fdd878f1db00728752e48546acdf3107d21d5c0fc11fb3214856acc115788db9bff16d50ce5eaebcda77c90cf774d2eb2cfb5c4bd92062478c55
6
+ metadata.gz: 3df53ebf6c8ddcff87a78488ca2d21f862017d5916c6daa7aad70cf242bfe17238b431c538baca7586c51ed55d04d8edb8e491f153ec5042c38d2b380741ca7a
7
+ data.tar.gz: 9a5eb9afaa314dd26f3efa3e491431413aea0d1df5f08640f5e8a8e904992ba3560ccb312a7e5a2e8a6f791ab7e0f88d1179fa72e3c431cf01b84b444431fc6a
@@ -23,7 +23,8 @@
23
23
  " </div>",
24
24
  " <div>",
25
25
  " <a href=\"ORIGINAL_DOMAIN/PROXY_PATH/p1\" >and yet another link</a>",
26
- " <p><a href=\"../PROXY_PATH/p1\" >the last link</a></p>",
26
+ " <p><a href=\"../PROXY_PATH/p1\" >almost there</a></p>",
27
+ " <p><a href=\"/p1\" >the last link</a></p>",
27
28
  " </div>",
28
29
  "</body>",
29
30
  "</html>"
data/lib/pretty_proxy.rb CHANGED
@@ -84,10 +84,11 @@ class PrettyProxy < Rack::Proxy
84
84
 
85
85
  @proxy_path = proxy_path.clone
86
86
  @original_domain = URI(original_domain.clone)
87
+ @original_paths = Set.new
87
88
  if original_paths.respond_to? :each
88
- @original_paths = original_paths.clone
89
+ original_paths.each { | value | @original_paths << value.clone }
89
90
  else
90
- @original_paths = [original_paths.clone]
91
+ @original_paths << original_paths.clone
91
92
  end
92
93
  end
93
94
 
@@ -96,7 +97,8 @@ class PrettyProxy < Rack::Proxy
96
97
  # @!attribute original_domain
97
98
  # return the clone of the internal value
98
99
  # @!attribute original_paths
99
- # return the clone of the internal value
100
+ # return the clone of the internal value (always a Set, no matter what is
101
+ # passed to initialize).
100
102
  [:proxy_path, :original_domain, :original_paths].each do | reader |
101
103
  define_method(reader) { instance_variable_get("@#{reader.to_s}").clone }
102
104
  end
@@ -158,32 +160,33 @@ class PrettyProxy < Rack::Proxy
158
160
  def proxify_hyperlink(hyperlink, proxy_page_url)
159
161
  hyperlink = URI(hyperlink.clone)
160
162
  proxy_page_url = URI(proxy_page_url)
161
- if Utils.relative_path? hyperlink
162
- # recreate the original site url from the relative path
163
+ if hyperlink.scheme
164
+ if inside_proxy_control? hyperlink
165
+ unless point_to_a_proxy_page?(hyperlink, proxy_page_url)
166
+ hyperlink = proxify_uri(hyperlink, proxy_page_url)
167
+ end
168
+ end
169
+ else
170
+ # recreate the original site url from the relative/absolute path
163
171
  absolute_link = unproxify_url proxy_page_url
164
172
  absolute_link.path = Pathname.new(absolute_link.path).join(hyperlink.path).to_s
165
173
  if inside_proxy_control? absolute_link
166
- if same_domain_as_original?(proxy_page_url) &&
167
- valid_path_for_proxy?(absolute_link.path)
168
- # in the case of a relative path in the original page who points
169
- # to a proxy page, and the proxy page is inside the proxy control
170
- # we have to use the absolute_link or the page will be double proxified
171
- # example: ../proxy/content in http://example.com/proxy/content, with
172
- # original_path as '/' is http://example.com/proxy/proxy/content
173
- hyperlink = absolute_link
174
+ if Pathname.new(hyperlink.path).relative?
175
+ if same_domain_as_original?(proxy_page_url) &&
176
+ valid_path_for_proxy?(absolute_link.path)
177
+ # in the case of a relative path in the original page who points
178
+ # to a proxy page, and the proxy page is inside the proxy control
179
+ # we have to use the absolute_link or the page will be double proxified
180
+ # example: ../proxy/content in http://example.com/proxy/content, with
181
+ # original_path as '/' is http://example.com/proxy/proxy/content
182
+ hyperlink = absolute_link
183
+ end
184
+ else # is absolute
185
+ hyperlink.path = @proxy_path + hyperlink.path[1..-1]
174
186
  end
175
187
  else
176
188
  hyperlink = absolute_link
177
189
  end
178
- else
179
- if inside_proxy_control? hyperlink
180
- unless point_to_a_proxy_page?(hyperlink, proxy_page_url)
181
- hyperlink.scheme = proxy_page_url.scheme
182
- hyperlink.host = proxy_page_url.host
183
- hyperlink.port = proxy_page_url.port
184
- hyperlink.path = @proxy_path + hyperlink.path[1..-1]
185
- end
186
- end
187
190
  end
188
191
 
189
192
  hyperlink.to_s
@@ -375,17 +378,19 @@ class PrettyProxy < Rack::Proxy
375
378
  # Check if the absolute path begin with a proxy_path and is followed by a
376
379
  # original_paths element.
377
380
  def valid_path_for_proxy?(absolute_path)
381
+ return false unless absolute_path.start_with?(@proxy_path)
382
+
378
383
  path_without_proxy_prefix = absolute_path[(@proxy_path.size-1)..-1]
379
- # if we don't add the trailing slash '/about' and '/about_us' match
380
- original_paths_with_trailing_slash = []
381
- @original_paths.each do | path |
382
- original_paths_with_trailing_slash << (path.end_with?('/') ? path : "#{path}/")
383
- end
384
384
 
385
- absolute_path.start_with?(@proxy_path) &&
386
- original_paths_with_trailing_slash.any? do | original_path |
385
+ @original_paths.any? do | original_path |
386
+ # if we don't test this '/about' and '/about_us' will match
387
+ if original_path.end_with? '/'
387
388
  path_without_proxy_prefix.start_with? original_path
389
+ else
390
+ path_without_proxy_prefix == original_path ||
391
+ path_without_proxy_prefix.start_with?("#{original_path}/")
388
392
  end
393
+ end
389
394
  end
390
395
 
391
396
  # Take a url and the proxy domain (scheme, host and port) and return if
@@ -395,12 +400,21 @@ class PrettyProxy < Rack::Proxy
395
400
  valid_path_for_proxy?(hyperlink.path)
396
401
  end
397
402
 
403
+ # api private Don't use this method.
404
+ def proxify_uri(uri, proxy_domain)
405
+ uri = uri.clone
406
+
407
+ uri.scheme = proxy_domain.scheme
408
+ uri.host = proxy_domain.host
409
+ uri.port = proxy_domain.port
410
+ uri.path = @proxy_path + uri.path[1..-1]
411
+
412
+ uri
413
+ end
414
+ private :proxify_uri
415
+
398
416
  # api private Don't use the methods of this class. They are for internal use only.
399
417
  module Utils
400
- def self.relative_path?(hyperlink)
401
- ! hyperlink.scheme
402
- end
403
-
404
418
  def self.same_domain?(u1, u2)
405
419
  u1.scheme == u2.scheme &&
406
420
  u1.host == u2.host &&
@@ -39,7 +39,8 @@ describe PrettyProxy do
39
39
  </div>
40
40
  <div>
41
41
  <a href="ARG_4" >and yet another link</a>
42
- <p><a href="ARG_5" >the last link</a></p>
42
+ <p><a href="ARG_5" >almost there</a></p>
43
+ <p><a href="ARG_6" >the last link</a></p>
43
44
  </div>
44
45
  </body>
45
46
  </html>
@@ -53,13 +54,14 @@ describe PrettyProxy do
53
54
  'http://othersite.net',
54
55
  '../p3', '../p2/p2_2/',
55
56
  'http://site.net/proxy/p1',
56
- '../proxy/p1']) }
57
+ '../proxy/p1', '/p1']) }
57
58
 
58
59
  let(:proxified_html) { generate_html_for_test(['http://site.net/proxy/p2/p2_2/',
59
60
  'http://othersite.net',
60
61
  'http://site.net/p3', '../p2/p2_2/',
61
62
  'http://site.net/proxy/p1',
62
- 'http://site.net/proxy/p1']) }
63
+ 'http://site.net/proxy/p1',
64
+ '/proxy/p1']) }
63
65
 
64
66
  let (:correct_new_args_example) { ['/proxy/', 'http://myoriginalsite.com', '/content'] }
65
67
  let (:pp) { described_class.new(*correct_new_args_example) }
@@ -113,7 +115,7 @@ describe PrettyProxy do
113
115
  describe "##{reader_method.to_s}" do
114
116
  return_changers = { proxy_path: :chop!,
115
117
  original_domain: ->(uri){ uri.host = 'otherdomain.com'},
116
- original_paths: :shift }
118
+ original_paths: :clear }
117
119
 
118
120
  it_behaves_like 'an reader method who encapsulate a mutable variable' do
119
121
  let(:reader_method_name) { reader_method }
@@ -185,13 +187,16 @@ describe PrettyProxy do
185
187
  describe '#proxify_hyperlink' do
186
188
  let (:pp) { described_class.new('/proxy/', 'http://site.net', ['/p1', '/p2/p2_2/']) }
187
189
 
188
- it "proxify absolute hyperlinks to inside the proxy control" do
190
+ it "proxify absolute paths to inside the proxy control" do
191
+ expect(pp.proxify_hyperlink('/p2/p2_2/', 'http://theproxy.net/proxy/p1')).to eq '/proxy/p2/p2_2/'
192
+ end
193
+ it "proxify hyperlinks with scheme to inside the proxy control" do
189
194
  expect(pp.proxify_hyperlink('http://site.net/p2/p2_2/', 'http://theproxy.net/proxy/p1')).to eq 'http://theproxy.net/proxy/p2/p2_2/'
190
195
  end
191
- it "don't change absolute hyperlinks to ouside the proxy control" do
196
+ it "don't change hyperlinks with scheme to ouside the proxy control" do
192
197
  expect(pp.proxify_hyperlink('http://othersite.net', 'http://theproxy.net/proxy/p1')).to eq 'http://othersite.net'
193
198
  end
194
- it 'change to absolute hyperlinks the relative paths to outside the proxy control' do
199
+ it 'change to urls the relative paths to outside the proxy control' do
195
200
  expect(pp.proxify_hyperlink('../p3', 'http://theproxy.net/proxy/p1')).to eq 'http://site.net/p3'
196
201
  expect(pp.proxify_hyperlink('../p2/p2_2', 'http://theproxy.net/proxy/p1')).to eq 'http://site.net/p2/p2_2' # without the trailing '/'
197
202
  end
@@ -202,11 +207,11 @@ describe PrettyProxy do
202
207
  context 'when the proxy itself is inside the proxy control' do
203
208
  let (:pp) { described_class.new('/proxy/', 'http://site.net', '/') }
204
209
 
205
- it "dont't change absolute hyperlinks to the proxy itself" do
210
+ it "dont't change urls to the proxy itself" do
206
211
  expect(pp.proxify_hyperlink('http://site.net/proxy/p1', 'http://site.net/proxy/p1')).to eq 'http://site.net/proxy/p1'
207
212
  expect(pp.proxify_hyperlink('http://site.net/proxy/p1', 'http://site.net/proxy/p2/p2_2/')).to eq 'http://site.net/proxy/p1'
208
213
  end
209
- it 'change to absolute hyperlinks the relative paths to the proxy itself' do
214
+ it 'change to urls the relative paths to the proxy itself' do
210
215
  expect(pp.proxify_hyperlink('../proxy/p1', 'http://site.net/proxy/p1')).to eq 'http://site.net/proxy/p1'
211
216
  expect(pp.proxify_hyperlink('../../proxy/p1', 'http://site.net/proxy/p2/p2_2/')).to eq 'http://site.net/proxy/p1'
212
217
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrique Becker