pretty_proxy 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pretty_proxy.rb +68 -73
- data/spec/pretty_proxy_spec.rb +4 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 718ffdcdc9a88b9103bfe1aa2e32cc32039a8b20
|
4
|
+
data.tar.gz: 4a5614d06ed052826da3f9eaac0e443b2ef82946
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 633d5ca4cb8cd20c745a17eefa402eb9328da09c06b13aeccecdf74dbfd6a16c3511f0e1d3dfae190687787f36508d4e089c69a061a46c69cf3e264c097b12b0
|
7
|
+
data.tar.gz: c00c15756541fdd878f1db00728752e48546acdf3107d21d5c0fc11fb3214856acc115788db9bff16d50ce5eaebcda77c90cf774d2eb2cfb5c4bd92062478c55
|
data/lib/pretty_proxy.rb
CHANGED
@@ -11,24 +11,23 @@ require 'rack-proxy'
|
|
11
11
|
# the hyperlinks point to the proxy version of the page if it exist.
|
12
12
|
#
|
13
13
|
# @example A terrible example
|
14
|
-
#
|
14
|
+
# # You can run this example with 'rake heresy_example' in the gem folder
|
15
|
+
# # and see the result in localhost:9292/proxy/
|
16
|
+
# require 'pretty_proxy'
|
15
17
|
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
18
|
+
# class Heresy < PrettyProxy
|
19
|
+
# def sugared_rewrite_response(triplet, requested_to_proxy_env, rewritten_env)
|
20
|
+
# status, headers, page = triplet
|
21
|
+
# page = page.gsub(/(MTG )?Magic(: The Gathering)?/, 'Yu-Gi-Oh')
|
22
|
+
# [status, headers, page]
|
23
|
+
# end
|
21
24
|
# end
|
22
|
-
# end
|
23
25
|
#
|
24
|
-
#
|
26
|
+
# run Heresy.new('/proxy/', 'http://magiccards.info', '/')
|
25
27
|
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
# @note: If you want to make a Rack app who use the proxy to point to
|
30
|
-
# another path of the same app you have to use a server in multithread
|
31
|
-
# mode, otherwise requests to the proxy will end in a deadlock.
|
28
|
+
# If you want to make a Rack app who use the proxy to point to
|
29
|
+
# another path of the same app you have to use a server in multithread
|
30
|
+
# mode, otherwise requests to the proxy will end in a deadlock.
|
32
31
|
# The proxy request the original page but the server don't respond because
|
33
32
|
# is waiting the proxy request to be resolved. The proxy request don't end
|
34
33
|
# because need the original page. A timeout error occur.
|
@@ -39,21 +38,19 @@ require 'rack-proxy'
|
|
39
38
|
# support more than deflate and gzip; exception classes with more
|
40
39
|
# than a message;
|
41
40
|
#
|
42
|
-
# Glossary:
|
43
|
-
# 'a valid proxy url/path': The path (or the path of the url) start with
|
44
|
-
# the proxy_path and is followed by a original_path.
|
45
|
-
# 'in(side)/out(side) the proxy control': The url have (or not) the path
|
46
|
-
# starting with a original_path and the scheme, port and host are the
|
47
|
-
# same of the original_domain.
|
48
|
-
#
|
49
41
|
# The exception classes (except Error) inherit Error, and Error inherit
|
50
42
|
# ArgumentError. They are empty yet, only have a message.
|
51
43
|
#
|
52
|
-
#
|
53
|
-
#
|
54
|
-
#
|
44
|
+
# Glossary:
|
45
|
+
#
|
46
|
+
# 'a valid proxy url/path': The path (or the path of the url) start with
|
47
|
+
# the proxy_path and is followed by a original_path.
|
48
|
+
#
|
49
|
+
# 'in(side)/out(side) the proxy control': The url have (or not) the path
|
50
|
+
# starting with a original_path and the scheme, port and host are the
|
51
|
+
# same of the original_domain.
|
55
52
|
#
|
56
|
-
# @author
|
53
|
+
# @author Henrique Becker
|
57
54
|
class PrettyProxy < Rack::Proxy
|
58
55
|
# The supertype of any exceptions explicitly raised by the methods
|
59
56
|
class Error < ArgumentError; end
|
@@ -94,15 +91,12 @@ class PrettyProxy < Rack::Proxy
|
|
94
91
|
end
|
95
92
|
end
|
96
93
|
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
# !@attribute original_paths
|
104
|
-
# @param a input who will be validated as in the initialize
|
105
|
-
# @return the clone of the internal value
|
94
|
+
# @!attribute proxy_path
|
95
|
+
# return the clone of the internal value
|
96
|
+
# @!attribute original_domain
|
97
|
+
# return the clone of the internal value
|
98
|
+
# @!attribute original_paths
|
99
|
+
# return the clone of the internal value
|
106
100
|
[:proxy_path, :original_domain, :original_paths].each do | reader |
|
107
101
|
define_method(reader) { instance_variable_get("@#{reader.to_s}").clone }
|
108
102
|
end
|
@@ -123,8 +117,8 @@ class PrettyProxy < Rack::Proxy
|
|
123
117
|
end
|
124
118
|
|
125
119
|
# Take a proxy url and return the original URL behind the proxy. Preserve the
|
126
|
-
#
|
127
|
-
# @param [String, URI::HTTP, URI::HTTPS] A URL.
|
120
|
+
# query and fragment, if any. For the rewrite of a request @see rewrite_env.
|
121
|
+
# @param url [String, URI::HTTP, URI::HTTPS] A URL.
|
128
122
|
# @return [URI::HTTP, URI::HTTPS] A URI object.
|
129
123
|
# @raise PrettyProxy::ProxyError
|
130
124
|
def unproxify_url(url)
|
@@ -151,10 +145,10 @@ class PrettyProxy < Rack::Proxy
|
|
151
145
|
end
|
152
146
|
|
153
147
|
# Take a hyperlink and the url of the proxy page (not the original page)
|
154
|
-
#
|
155
|
-
#
|
156
|
-
#
|
157
|
-
#
|
148
|
+
# where it come from and return the rewritten hyperlink. If the page
|
149
|
+
# pointed vy the hyperlink is in the proxy control the rewritten hyperlink
|
150
|
+
# gonna point to the proxyfied version, otherwise gonna point to the original
|
151
|
+
# version.
|
158
152
|
# @param hyperlink [String, URI::HTTP, URI::HTTPS] A string with a relative
|
159
153
|
# path or an url (string or URI).
|
160
154
|
# @param proxy_page_url [String, URI::HTTP, URI::HTTPS] The url from the
|
@@ -196,7 +190,7 @@ class PrettyProxy < Rack::Proxy
|
|
196
190
|
end
|
197
191
|
|
198
192
|
# Take a (X)HTML Document and apply proxify_hyperlink to the 'href'
|
199
|
-
#
|
193
|
+
# attribute of each 'a' element.
|
200
194
|
# @param html [String] A (X)HTML document.
|
201
195
|
# @param proxy_url [String, URI::HTTP, URI::HTTPS] The url where the
|
202
196
|
# the proxified version of the page will be displayed.
|
@@ -229,12 +223,12 @@ class PrettyProxy < Rack::Proxy
|
|
229
223
|
end
|
230
224
|
|
231
225
|
# Modify a Rack environment hash of a request to the proxy version of
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
#
|
236
|
-
#
|
237
|
-
# @param
|
226
|
+
# a page to a request to the original page. As in Rack::proxy is used
|
227
|
+
# by #call for require the original page before call rewrite_response in
|
228
|
+
# the response. If you want to use your own rewrite rules maybe is more
|
229
|
+
# wise to subclass Rack::Proxy instead subclass this class. The purpose
|
230
|
+
# of this class is mainly implement and enforce these rules for you.
|
231
|
+
# @param env [Hash{String => String}] A Rack environment hash.
|
238
232
|
# (see: {http://rack.rubyforge.org/doc/SPEC.html})
|
239
233
|
# @return [Hash{String => String}] A unproxified copy of the argument.
|
240
234
|
# @raise PrettyProxy::ProxyError
|
@@ -269,20 +263,20 @@ class PrettyProxy < Rack::Proxy
|
|
269
263
|
end
|
270
264
|
|
271
265
|
# Mainly apply the proxify_html to the body of the response if it is a html.
|
272
|
-
#
|
273
|
-
#
|
274
|
-
#
|
275
|
-
#
|
276
|
-
#
|
277
|
-
#
|
278
|
-
#
|
266
|
+
# Raise an error if the 'content-encoding' is other than deflate, gzip or
|
267
|
+
# identity. Change the 'content-length' header for the new body bytesize.
|
268
|
+
# Remove the 'transfer-encoding' if it is chunked, and act as not chunked.
|
269
|
+
# This method is inherited of Rack::Proxy, but in the original it have only
|
270
|
+
# the first parameter (the triplet). This version have the request Rack env
|
271
|
+
# to the proxy and the rewritten Rack env as second and third parameters,
|
272
|
+
# respectively.
|
279
273
|
# @param triplet [Array<(Integer, Hash{String => String}, #each)>] A Rack
|
280
274
|
# response (see {http://rack.rubyforge.org/doc/SPEC.html}) for the request
|
281
275
|
# to the original site.
|
282
|
-
# @param [Hash{String => String}] A Rack environment
|
283
|
-
# the proxy version.
|
284
|
-
# @param [Hash{String => String}] A Rack environment hash.
|
285
|
-
# the proxy to point to the original version.
|
276
|
+
# @param requested_to_proxy_env [Hash{String => String}] A Rack environment
|
277
|
+
# hash. The requested to the proxy version.
|
278
|
+
# @param rewritten_env [Hash{String => String}] A Rack environment hash.
|
279
|
+
# The rewritten by the proxy to point to the original version.
|
286
280
|
# @return [Array<(Integer, Hash{String => String}, #each)>] A unproxified
|
287
281
|
# copy of the first argument.
|
288
282
|
# @raise PrettyProxy::ProxyError
|
@@ -336,27 +330,27 @@ class PrettyProxy < Rack::Proxy
|
|
336
330
|
|
337
331
|
# @abstract This method is called only over (X)HTML responses, after they are
|
338
332
|
# decompressed and the hyperlinks proxified, before they are compressed
|
339
|
-
# again and the new content-length calculated.
|
340
|
-
#
|
341
|
-
# in the return. Return a modified clone of the
|
342
|
-
# the argument.
|
333
|
+
# again and the new content-length calculated.
|
334
|
+
# @note The body of the triplet is a String and not a object who respond to #each,
|
335
|
+
# the same has to be true in the return. Return a modified clone of the
|
336
|
+
# response, don't change the argument.
|
343
337
|
# @param triplet [Array<(Integer, Hash{String => String}, String)>] Not a
|
344
338
|
# valid Rack response, the third element is a string with the response body.
|
345
|
-
# @param [Hash{String => String}] A Rack environment
|
346
|
-
# the proxy version.
|
347
|
-
# @param [Hash{String => String}] A Rack environment hash.
|
348
|
-
# the proxy to point to the original version.
|
339
|
+
# @param requested_to_proxy_env [Hash{String => String}] A Rack environment
|
340
|
+
# hash. The requested to the proxy version.
|
341
|
+
# @param rewritten_env [Hash{String => String}] A Rack environment hash.
|
342
|
+
# The rewritten by the proxy to point to the original version.
|
349
343
|
# @return [Array<(Integer, Hash{String => String}, String)>] A unproxified
|
350
344
|
# copy of the first argument.
|
351
345
|
def sugared_rewrite_response(triplet, requested_to_proxy_env, rewritten_env)
|
352
346
|
triplet
|
353
347
|
end
|
354
348
|
|
355
|
-
# Make this class a Rack app.
|
356
|
-
#
|
357
|
-
#
|
358
|
-
#
|
359
|
-
#
|
349
|
+
# Make this class a Rack app. It's overriden to repass to the rewrite_response
|
350
|
+
# the original Rack environment (request to the proxy) and the rewritten env
|
351
|
+
# (modified to point the original page request).
|
352
|
+
# If you don't know the parameters and return of this method, please read
|
353
|
+
# {http://rack.rubyforge.org/doc/SPEC.html}.
|
360
354
|
def call(env)
|
361
355
|
# in theory we only need to repass the rewritten_env, any original env info
|
362
356
|
# needed can be passed as a environment application variable
|
@@ -372,7 +366,7 @@ class PrettyProxy < Rack::Proxy
|
|
372
366
|
Utils.same_domain?(@original_domain, uri)
|
373
367
|
end
|
374
368
|
|
375
|
-
# Check if the URI::HTTP(S) is a page who can be accessed through the proxy
|
369
|
+
# Check if the URI::HTTP(S) is a page who can be accessed through the proxy.
|
376
370
|
def inside_proxy_control?(uri)
|
377
371
|
same_domain_as_original?(uri) &&
|
378
372
|
valid_path_for_proxy?(@proxy_path + uri.path[1..-1])
|
@@ -402,7 +396,7 @@ class PrettyProxy < Rack::Proxy
|
|
402
396
|
end
|
403
397
|
|
404
398
|
# api private Don't use the methods of this class. They are for internal use only.
|
405
|
-
|
399
|
+
module Utils
|
406
400
|
def self.relative_path?(hyperlink)
|
407
401
|
! hyperlink.scheme
|
408
402
|
end
|
@@ -414,6 +408,7 @@ class PrettyProxy < Rack::Proxy
|
|
414
408
|
end
|
415
409
|
|
416
410
|
def self.validate_proxy_path(proxy_path)
|
411
|
+
fail ConfigError, "proxy_path argument don't start with a '/'" unless proxy_path.start_with? '/'
|
417
412
|
fail ConfigError, "proxy_path argument don't end with a '/'" unless proxy_path.end_with? '/'
|
418
413
|
# NOTE: if the user want to proxify 'www.site.net', and not 'www.site.net/'?
|
419
414
|
# Well, majority of the internet answers for this are 'the right way is to use the trailing slash'
|
data/spec/pretty_proxy_spec.rb
CHANGED
@@ -83,6 +83,10 @@ describe PrettyProxy do
|
|
83
83
|
|
84
84
|
# TODO: Add specs for '/' in the start of the proxy_path
|
85
85
|
let (:right_args) { correct_new_args_example }
|
86
|
+
context "when proxy_path doesn't start with a '/'" do
|
87
|
+
it { expect {new.call('proxy/', right_args[1], right_args[2])}.to raise_error(PrettyProxy::ConfigError) }
|
88
|
+
end
|
89
|
+
|
86
90
|
context "when proxy_path doesn't end with a '/'" do
|
87
91
|
it { expect {new.call('/proxy', right_args[1], right_args[2])}.to raise_error(PrettyProxy::ConfigError) }
|
88
92
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pretty_proxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrique Becker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -176,4 +176,4 @@ specification_version: 4
|
|
176
176
|
summary: A Rack::Proxy child pretty url oriented
|
177
177
|
test_files:
|
178
178
|
- spec/pretty_proxy_spec.rb
|
179
|
-
has_rdoc:
|
179
|
+
has_rdoc: true
|