rack-dynamic-reverse-proxy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac35613cc15b5c7e96a8240ea13ef5ffb1116ca0
4
+ data.tar.gz: 0ce1fe7512712011191b596082a8f95741938cac
5
+ SHA512:
6
+ metadata.gz: 89a8e491c791965e6226250293fffe04fc3313bd8da10772d240381a12abf197464aa7c6a7bd918faea8ad9305039e81a238ead2193d18047a4e2c9a6cadfa61
7
+ data.tar.gz: 2102af2cb903f88ef3d0f7d9d9ef2f5820058afa5fb55cbaf89a9f7d9eafe9a2c5c2ea50f08537d62e0553634a6d2c8836ca75426e45d96db0a9ab36b7dce350
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
11
+ /bin
12
+ .DS_Store
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-dynamic-reverse-proxy.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Shogo Kawaguchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # A Dynamic Reverse Proxy for Rack
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'rack-dynamic-reverse-proxy'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ ```
14
+ $ bundle
15
+ ```
16
+
17
+ Or install it yourself as:
18
+
19
+ ```
20
+ $ gem install rack-dynamic-reverse-proxy
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require "rack-dynamic-reverse-proxy"
27
+
28
+ use Rack::DynamicReverseProxy do
29
+ reverse_proxy_options preserve_host: true
30
+
31
+ reverse_proxy_rule do |env|
32
+ if /test/.match env.fullpath
33
+ 'http://example.com/'
34
+ end
35
+ end
36
+ end
37
+
38
+ app = proc do |env|
39
+ [
40
+ 200,
41
+ { 'Content-Type' => 'text/html' },
42
+ ['<html><body>test</body></html>']
43
+ ]
44
+ end
45
+ run app
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/k-shogo/rack-dynamic-reverse-proxy/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1 @@
1
+ require 'rack/dynamic_reverse_proxy'
@@ -0,0 +1,134 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require "rack-proxy"
4
+ require 'rack/reverse_proxy_rule'
5
+
6
+ module Rack
7
+ class DynamicReverseProxy
8
+
9
+ def initialize(app = nil, &block)
10
+ @app = app || lambda {|env| [404, [], []] }
11
+ @matchers = []
12
+ @rules = []
13
+ @global_options = {
14
+ preserve_host: true,
15
+ x_forwarded_host: true,
16
+ matching: :all,
17
+ replace_response_host: false
18
+ }
19
+ instance_eval &block if block_given?
20
+ end
21
+
22
+ def call(env)
23
+ rackreq = Rack::Request.new(env)
24
+ rule = get_rule(rackreq)
25
+ # return @app.call(env) if matcher.nil?
26
+ return @app.call(env) if rule.nil?
27
+
28
+ # proxy(env, rackreq, matcher)
29
+ proxy(env, rackreq, rule)
30
+ end
31
+
32
+ private
33
+
34
+ def proxy(env, source_request, matcher)
35
+ # uri = matcher.get_uri(source_request.fullpath, env)
36
+ uri = matcher.get_uri
37
+ return @app.call(env) if uri.nil?
38
+
39
+ # options = @global_options.dup.merge(matcher.options)
40
+ options = @global_options.dup
41
+
42
+ # Initialize request
43
+ target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri)
44
+
45
+ # Setup headers
46
+ target_request_headers = extract_http_request_headers(source_request.env)
47
+
48
+ if options[:preserve_host]
49
+ target_request_headers['HOST'] = "#{uri.host}:#{uri.port}"
50
+ end
51
+
52
+ if options[:x_forwarded_host]
53
+ target_request_headers['X-Forwarded-Host'] = source_request.host
54
+ target_request_headers['X-Forwarded-Port'] = "#{source_request.port}"
55
+ end
56
+
57
+ target_request.initialize_http_header(target_request_headers)
58
+
59
+ # Basic auth
60
+ target_request.basic_auth options[:username], options[:password] if options[:username] and options[:password]
61
+
62
+ # Setup body
63
+ if target_request.request_body_permitted? && source_request.body
64
+ source_request.body.rewind
65
+ target_request.body_stream = source_request.body
66
+ end
67
+
68
+ target_request.content_length = source_request.content_length || 0
69
+ target_request.content_type = source_request.content_type if source_request.content_type
70
+
71
+ # Create a streaming response (the actual network communication is deferred, a.k.a. streamed)
72
+ target_response = HttpStreamingResponse.new(target_request, uri.host, uri.port)
73
+
74
+ target_response.use_ssl = "https" == uri.scheme
75
+
76
+ # Let rack set the transfer-encoding header
77
+ response_headers = target_response.headers
78
+ response_headers.delete('transfer-encoding')
79
+
80
+ # Replace the location header with the proxy domain
81
+ if response_headers['location'] && options[:replace_response_host]
82
+ response_location = URI(response_headers['location'][0])
83
+ response_location.host = source_request.host
84
+ response_headers['location'] = response_location.to_s
85
+ end
86
+
87
+ [target_response.status, response_headers, target_response.body]
88
+ end
89
+
90
+ def extract_http_request_headers(env)
91
+ headers = env.reject do |k, v|
92
+ !(/^HTTP_[A-Z_]+$/ === k) || v.nil?
93
+ end.map do |k, v|
94
+ [reconstruct_header_name(k), v]
95
+ end.inject(Utils::HeaderHash.new) do |hash, k_v|
96
+ k, v = k_v
97
+ hash[k] = v
98
+ hash
99
+ end
100
+
101
+ x_forwarded_for = (headers["X-Forwarded-For"].to_s.split(/, +/) << env["REMOTE_ADDR"]).join(", ")
102
+
103
+ headers.merge!("X-Forwarded-For" => x_forwarded_for)
104
+ end
105
+
106
+ def reconstruct_header_name(name)
107
+ name.sub(/^HTTP_/, "").gsub("_", "-")
108
+ end
109
+
110
+ def get_rule rackreq
111
+ rules = @rules.select do |rule|
112
+ rule.match? rackreq
113
+ end
114
+
115
+ if rules.length < 1
116
+ nil
117
+ else
118
+ rules.first
119
+ end
120
+ end
121
+
122
+ def reverse_proxy_options options
123
+ @global_options = options
124
+ end
125
+
126
+ def reverse_proxy_rule rule = nil, &block
127
+ if block_given?
128
+ @rules << ReverseProxyRule.new(block)
129
+ else
130
+ @rules << ReverseProxyRule.new(rule)
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,24 @@
1
+ module Rack
2
+ class ReverseProxyRule
3
+
4
+ def initialize rule
5
+ if rule.kind_of?(Proc) || rule.respond_to?(:call)
6
+ @rule = rule
7
+ else
8
+ raise "Invalid Rule for reverse_proxy"
9
+ end
10
+ end
11
+
12
+ attr_reader :rule, :url
13
+
14
+ def match? env
15
+ @url = @rule.call env
16
+ end
17
+
18
+ def get_uri
19
+ return nil if @url.nil?
20
+ URI.parse(@url)
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rack-dynamic-reverse-proxy"
7
+ spec.version = '0.0.1'
8
+ spec.authors = ["Shogo Kawaguchi"]
9
+ spec.email = ["platycod0n.ramosa@gmail.com"]
10
+
11
+ if spec.respond_to?(:metadata)
12
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
13
+ end
14
+
15
+ spec.summary = %q{A Dynamic Reverse Proxy for Rack}
16
+ spec.description = %q{A Dynamic Reverse Proxy for Rack}
17
+ spec.homepage = ""
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+
28
+ spec.add_dependency "rack", ">= 1.0.0"
29
+ spec.add_dependency "rack-proxy", "~> 0.5"
30
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-dynamic-reverse-proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shogo Kawaguchi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-proxy
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.5'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.5'
69
+ description: A Dynamic Reverse Proxy for Rack
70
+ email:
71
+ - platycod0n.ramosa@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/rack-dynamic-reverse-proxy.rb
84
+ - lib/rack/dynamic_reverse_proxy.rb
85
+ - lib/rack/reverse_proxy_rule.rb
86
+ - rack-dynamic-reverse-proxy.gemspec
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata:
91
+ allowed_push_host: https://rubygems.org
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A Dynamic Reverse Proxy for Rack
112
+ test_files: []