xproxy 0.0.1 → 0.1.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.
- data/lib/xproxy.rb +7 -2
- data/lib/xproxy/application.rb +21 -0
- data/lib/xproxy/version.rb +1 -1
- data/xproxy.gemspec +2 -0
- metadata +37 -6
- data/lib/xproxy/map.rb +0 -46
- data/lib/xproxy/proxy.rb +0 -44
data/lib/xproxy.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rack/header'
|
2
|
+
require 'rack/proxy'
|
3
|
+
|
4
|
+
module XProxy
|
5
|
+
class Application
|
6
|
+
def initialize(host, headers)
|
7
|
+
@host, @headers = host, headers
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
host, headers = @host, @headers
|
12
|
+
|
13
|
+
app = Rack::Builder.new do
|
14
|
+
use Rack::Header, headers
|
15
|
+
run Rack::Proxy.new(host)
|
16
|
+
end
|
17
|
+
|
18
|
+
app.call(env)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/xproxy/version.rb
CHANGED
data/xproxy.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xproxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -43,6 +43,38 @@ dependencies:
|
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: 1.4.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rackheader
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.0.1
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.0.1
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rackproxy
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.0.2
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.0.2
|
46
78
|
description: A Rack compatible HTTP Proxy
|
47
79
|
email:
|
48
80
|
- ajjahn@gmail.com
|
@@ -56,8 +88,7 @@ files:
|
|
56
88
|
- README.md
|
57
89
|
- Rakefile
|
58
90
|
- lib/xproxy.rb
|
59
|
-
- lib/xproxy/
|
60
|
-
- lib/xproxy/proxy.rb
|
91
|
+
- lib/xproxy/application.rb
|
61
92
|
- lib/xproxy/version.rb
|
62
93
|
- xproxy.gemspec
|
63
94
|
homepage: https://github.com/ajjahn/xproxy
|
@@ -74,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
74
105
|
version: '0'
|
75
106
|
segments:
|
76
107
|
- 0
|
77
|
-
hash: -
|
108
|
+
hash: -66576529968765014
|
78
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
110
|
none: false
|
80
111
|
requirements:
|
@@ -83,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
114
|
version: '0'
|
84
115
|
segments:
|
85
116
|
- 0
|
86
|
-
hash: -
|
117
|
+
hash: -66576529968765014
|
87
118
|
requirements: []
|
88
119
|
rubyforge_project:
|
89
120
|
rubygems_version: 1.8.18
|
data/lib/xproxy/map.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
|
3
|
-
module XProxy
|
4
|
-
class Map < Hash
|
5
|
-
|
6
|
-
def match(path, options={})
|
7
|
-
self[path] = options
|
8
|
-
end
|
9
|
-
|
10
|
-
def translate(request)
|
11
|
-
request_path = normalize_path(request.path)
|
12
|
-
uri = nil
|
13
|
-
each do |match_path, options|
|
14
|
-
match_path = normalize_path(match_path)
|
15
|
-
|
16
|
-
puts request_path
|
17
|
-
|
18
|
-
if request_path.start_with?(match_path)
|
19
|
-
request.path = options[:include_match] ? request.path : strip_match(request.path, match_path)
|
20
|
-
uri = translate_uri(URI(options[:to]), request)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
uri
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def normalize_path(path)
|
30
|
-
"/#{path.split('/').delete_if(&:empty?).join('/')}"
|
31
|
-
end
|
32
|
-
|
33
|
-
def strip_match(request_path, match_path)
|
34
|
-
normalize_path(request_path.gsub(match_path, ''))
|
35
|
-
end
|
36
|
-
|
37
|
-
def translate_uri(to ,from)
|
38
|
-
to.path = from.path
|
39
|
-
to.opaque = from.opaque
|
40
|
-
to.query = from.query
|
41
|
-
to.fragment = from.fragment
|
42
|
-
|
43
|
-
to
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
data/lib/xproxy/proxy.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'rack/request'
|
3
|
-
|
4
|
-
module XProxy
|
5
|
-
class Proxy
|
6
|
-
|
7
|
-
def initialize(app, &block)
|
8
|
-
@app = app
|
9
|
-
@map = XProxy::Map.new
|
10
|
-
@map.instance_exec(&block)
|
11
|
-
end
|
12
|
-
|
13
|
-
def call(env)
|
14
|
-
request = Rack::Request.new(env)
|
15
|
-
|
16
|
-
return @app.call(env) unless uri = @map.translate(URI(request.url))
|
17
|
-
|
18
|
-
# Get the request method
|
19
|
-
request_method = request.request_method.downcase
|
20
|
-
request_method[0..0] = request_method[0..0].upcase
|
21
|
-
|
22
|
-
forward_request = Net::HTTP.const_get(request_method).new(uri.request_uri)
|
23
|
-
if forward_request.request_body_permitted? and request.body
|
24
|
-
forward_request.body_stream = request.body
|
25
|
-
forward_request.content_length = request.content_length
|
26
|
-
forward_request.content_type = request.content_type
|
27
|
-
end
|
28
|
-
|
29
|
-
forward_request["Referer"] = request.referer
|
30
|
-
|
31
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
32
|
-
http.use_ssl = uri.scheme == 'https' ? true : false
|
33
|
-
forward_response = http.request(forward_request)
|
34
|
-
|
35
|
-
headers = {}
|
36
|
-
forward_response.each_header do |k,v|
|
37
|
-
headers[k] = v unless k.to_s =~ /status/i
|
38
|
-
end
|
39
|
-
headers['Access-Control-Allow-Origin'] = '*'
|
40
|
-
|
41
|
-
[forward_response.code.to_i, headers, [forward_response.read_body]]
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|