rails-reverse-proxy 0.1.0

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: 67630ceeaa4f695dfeca3e2632a99a5a7900ca1d
4
+ data.tar.gz: 1d0b65c5203afa612fcc1376362d0a1253abd687
5
+ SHA512:
6
+ metadata.gz: bed344464210aecdaf54c06b01c1f788972784b478b09d40aecbb426d8b3dcdac2a1e0261fefd6b7d872b9c971e664e079c1cc99d1de8ddd1ef998d01572831d
7
+ data.tar.gz: e49e00eac285cc13a2a42c0e312171bf1329f1e16420badf5e6450a0b54a0b974d6100beed5696cb255d22b6d48a485ca8df9d8c0067ae4fab707d8e6f875901
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0"
12
+ gem "jeweler", "~> 2.0.1"
13
+ gem "simplecov", ">= 0"
14
+ end
@@ -0,0 +1,80 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (4.2.1)
5
+ i18n (~> 0.7)
6
+ json (~> 1.7, >= 1.7.7)
7
+ minitest (~> 5.1)
8
+ thread_safe (~> 0.3, >= 0.3.4)
9
+ tzinfo (~> 1.1)
10
+ addressable (2.3.8)
11
+ builder (3.2.2)
12
+ descendants_tracker (0.0.4)
13
+ thread_safe (~> 0.3, >= 0.3.1)
14
+ docile (1.1.5)
15
+ faraday (0.9.1)
16
+ multipart-post (>= 1.2, < 3)
17
+ git (1.2.9.1)
18
+ github_api (0.12.3)
19
+ addressable (~> 2.3)
20
+ descendants_tracker (~> 0.0.4)
21
+ faraday (~> 0.8, < 0.10)
22
+ hashie (>= 3.3)
23
+ multi_json (>= 1.7.5, < 2.0)
24
+ nokogiri (~> 1.6.3)
25
+ oauth2
26
+ hashie (3.4.2)
27
+ highline (1.7.2)
28
+ i18n (0.7.0)
29
+ jeweler (2.0.1)
30
+ builder
31
+ bundler (>= 1.0)
32
+ git (>= 1.2.5)
33
+ github_api
34
+ highline (>= 1.6.15)
35
+ nokogiri (>= 1.5.10)
36
+ rake
37
+ rdoc
38
+ json (1.8.3)
39
+ jwt (1.5.0)
40
+ mini_portile (0.6.2)
41
+ minitest (5.7.0)
42
+ multi_json (1.11.0)
43
+ multi_xml (0.5.5)
44
+ multipart-post (2.0.0)
45
+ nokogiri (1.6.6.2)
46
+ mini_portile (~> 0.6.0)
47
+ oauth2 (1.0.0)
48
+ faraday (>= 0.8, < 0.10)
49
+ jwt (~> 1.0)
50
+ multi_json (~> 1.3)
51
+ multi_xml (~> 0.5)
52
+ rack (~> 1.2)
53
+ rack (1.6.1)
54
+ rake (10.4.2)
55
+ rdoc (3.12.2)
56
+ json (~> 1.4)
57
+ shoulda (3.5.0)
58
+ shoulda-context (~> 1.0, >= 1.0.1)
59
+ shoulda-matchers (>= 1.4.1, < 3.0)
60
+ shoulda-context (1.2.1)
61
+ shoulda-matchers (2.8.0)
62
+ activesupport (>= 3.0.0)
63
+ simplecov (0.10.0)
64
+ docile (~> 1.1.0)
65
+ json (~> 1.8)
66
+ simplecov-html (~> 0.10.0)
67
+ simplecov-html (0.10.0)
68
+ thread_safe (0.3.5)
69
+ tzinfo (1.2.2)
70
+ thread_safe (~> 0.1)
71
+
72
+ PLATFORMS
73
+ ruby
74
+
75
+ DEPENDENCIES
76
+ bundler (~> 1.0)
77
+ jeweler (~> 2.0.1)
78
+ rdoc (~> 3.12)
79
+ shoulda
80
+ simplecov
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2015 James Hu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
1
+ # rails-reverse-proxy
2
+
3
+ ## Installation
4
+
5
+ ```
6
+ gem install rails-reverse-proxy
7
+ ```
8
+
9
+ ## Copyright
10
+
11
+ Copyright (c) 2015 James Hu. See LICENSE.txt for
12
+ further details.
13
+
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "rails-reverse-proxy"
18
+ gem.homepage = "http://github.com/axsuul/rails-reverse-proxy"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Reverse proxy for Ruby on Rails}
21
+ gem.description = %Q{Reverse proxy for Ruby on Rails}
22
+ gem.email = "hello@james.hu"
23
+ gem.authors = ["James Hu"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ desc "Code coverage detail"
36
+ task :simplecov do
37
+ ENV['COVERAGE'] = "true"
38
+ Rake::Task['test'].execute
39
+ end
40
+
41
+ task :default => :test
42
+
43
+ require 'rdoc/task'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "rails-reverse-proxy #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,2 @@
1
+ module ReverseProxy
2
+ end
@@ -0,0 +1,143 @@
1
+ require 'rack'
2
+ require 'rack-proxy'
3
+
4
+ module ReverseProxy
5
+ class Client
6
+ @@callback_methods = [
7
+ :on_response,
8
+ :on_set_cookies,
9
+ :on_success,
10
+ :on_redirect,
11
+ :on_missing,
12
+ :on_error,
13
+ :on_complete
14
+ ]
15
+
16
+ # Define callback setters
17
+ @@callback_methods.each do |method|
18
+ define_method(method) do |&block|
19
+ self.callbacks[method] = block
20
+ end
21
+ end
22
+
23
+ attr_accessor :url, :callbacks
24
+
25
+ def initialize(url)
26
+ self.url = url
27
+ self.callbacks = {}
28
+
29
+ # Initialize default callbacks with empty Proc
30
+ @@callback_methods.each do |method|
31
+ self.callbacks[method] = Proc.new {}
32
+ end
33
+
34
+ yield(self) if block_given?
35
+ end
36
+
37
+ def request(env, options = {}, &block)
38
+ options.reverse_merge!(
39
+ headers: {},
40
+ path: nil,
41
+ username: nil,
42
+ password: nil
43
+ )
44
+
45
+ source_request = Rack::Request.new(env)
46
+
47
+ # We can pass in a custom path
48
+ uri = URI.parse("#{url}#{options[:path] || env['ORIGINAL_FULLPATH']}")
49
+
50
+ # Initialize request
51
+ target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(uri.request_uri)
52
+
53
+ # Setup headers
54
+ target_request_headers = extract_http_request_headers(source_request.env).merge(options[:headers])
55
+
56
+ target_request.initialize_http_header(target_request_headers)
57
+
58
+ # Basic auth
59
+ target_request.basic_auth(options[:username], options[:password]) if options[:username] and options[:password]
60
+
61
+ # Setup body
62
+ if target_request.request_body_permitted? \
63
+ && 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
+ # Hold the response here
72
+ target_response = nil
73
+
74
+ # Don't encode response/support compression which was
75
+ # causing content length not match the actual content
76
+ # length of the response which ended up causing issues
77
+ # within Varnish (503)
78
+ target_request['Accept-Encoding'] = nil
79
+
80
+ # Make the request
81
+ Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == "https")) do |http|
82
+ target_response = http.request(target_request)
83
+ end
84
+
85
+ status_code = target_response.code.to_i
86
+ payload = [status_code, target_response]
87
+
88
+ callbacks[:on_response].call(payload)
89
+
90
+ if set_cookie_headers = target_response.to_hash['set-cookie']
91
+ set_cookies_hash = {}
92
+
93
+ set_cookie_headers.each do |set_cookie_header|
94
+ set_cookie_hash = CookieJar::CookieValidation.parse_set_cookie(set_cookie_header)
95
+ set_cookie_hash[:value] = CGI.unescape(set_cookie_hash[:value])
96
+
97
+ name = set_cookie_hash.delete(:name)
98
+
99
+ set_cookies_hash[name] = set_cookie_hash
100
+ end
101
+
102
+ callbacks[:on_set_cookies].call(payload | [set_cookies_hash])
103
+ end
104
+
105
+ case status_code
106
+ when 200..299
107
+ callbacks[:on_success].call(payload)
108
+ when 300..399
109
+ if redirect_url = target_response['Location']
110
+ callbacks[:on_redirect].call(payload | [redirect_url])
111
+ end
112
+ when 400..499
113
+ callbacks[:on_missing].call(payload)
114
+ when 500..599
115
+ callbacks[:on_error].call(payload)
116
+ end
117
+
118
+ callbacks[:on_complete].call(payload)
119
+
120
+ payload
121
+ end
122
+
123
+ private
124
+
125
+ def extract_http_request_headers(env)
126
+ headers = env.reject do |k, v|
127
+ !(/^HTTP_[A-Z_]+$/ === k) || v.nil?
128
+ end.map do |k, v|
129
+ [reconstruct_header_name(k), v]
130
+ end.inject(Rack::Utils::HeaderHash.new) do |hash, k_v|
131
+ k, v = k_v
132
+ hash[k] = v
133
+ hash
134
+ end
135
+
136
+ headers
137
+ end
138
+
139
+ def reconstruct_header_name(name)
140
+ name.sub(/^HTTP_/, "").gsub("_", "-")
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,60 @@
1
+ module ReverseProxy
2
+ module Controller
3
+ def reverse_proxy(proxy_url, options = {})
4
+ proxy_uri = Addressable::URI.parse(proxy_url)
5
+
6
+ client = ReverseProxy::Client.new(proxy_url) do |config|
7
+ config.on_response do |code, response|
8
+ blacklist = [
9
+ 'Connection', # Always close connection
10
+ 'Transfer-Encoding', # Let Rails calculate this
11
+ 'Content-Length' # Let Rails calculate this
12
+ ]
13
+
14
+ response.each_capitalized do |key, value|
15
+ next if blacklist.include?(key)
16
+
17
+ headers[key] = value
18
+ end
19
+ end
20
+
21
+ config.on_set_cookies do |code, response, set_cookies|
22
+ set_cookies.each do |key, attributes|
23
+ cookies[key] = attributes
24
+ end
25
+ end
26
+
27
+ config.on_redirect do |code, response, redirect_url|
28
+ request_uri = Addressable::URI.parse(request.url)
29
+ redirect_uri = Addressable::URI.parse(redirect_url)
30
+
31
+ # Make redirect uri absolute if it's relative by
32
+ # joining it with the request url
33
+ redirect_uri = request_uri.join(redirect_url) if redirect_uri.host.nil?
34
+
35
+ unless redirect_uri.port.nil?
36
+ # Make sure it's consistent with our request port
37
+ redirect_uri.port = request.port if redirect_uri.port == proxy_uri.port
38
+ end
39
+
40
+ redirect_to redirect_uri.to_s, status: code and return
41
+ end
42
+
43
+ config.on_complete do |code, response|
44
+ content_type = response['Content-Type']
45
+ body = response.body.to_s
46
+
47
+ if content_type and content_type.match /image/
48
+ send_data body, content_type: content_type, disposition: "inline", status: code
49
+ else
50
+ render text: body, content_type: content_type, status: code
51
+ end
52
+ end
53
+
54
+ yield(config) if block_given?
55
+ end
56
+
57
+ client.request(env, options)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "rails-reverse-proxy"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Hu"]
12
+ s.date = "2015-06-05"
13
+ s.description = "Reverse proxy for Ruby on Rails"
14
+ s.email = "hello@james.hu"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/reverse-proxy.rb",
28
+ "lib/reverse-proxy/client.rb",
29
+ "lib/reverse-proxy/controller.rb",
30
+ "rails-reverse-proxy.gemspec"
31
+ ]
32
+ s.homepage = "http://github.com/axsuul/rails-reverse-proxy"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "2.0.14"
36
+ s.summary = "Reverse proxy for Ruby on Rails"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 4
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
43
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
46
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<shoulda>, [">= 0"])
49
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
50
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
51
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
52
+ s.add_dependency(%q<simplecov>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
59
+ s.add_dependency(%q<simplecov>, [">= 0"])
60
+ end
61
+ end
62
+
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-reverse-proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Hu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shoulda
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.0.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.0.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Reverse proxy for Ruby on Rails
84
+ email: hello@james.hu
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.md
90
+ files:
91
+ - .document
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - VERSION
98
+ - lib/reverse-proxy.rb
99
+ - lib/reverse-proxy/client.rb
100
+ - lib/reverse-proxy/controller.rb
101
+ - rails-reverse-proxy.gemspec
102
+ homepage: http://github.com/axsuul/rails-reverse-proxy
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.14
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Reverse proxy for Ruby on Rails
126
+ test_files: []