goliath-reverse-proxy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ *.gem
3
+ .bundle
4
+ spec/reports
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ goliath-reverse-proxy (0.1.0)
5
+ em-http-request
6
+ em-synchrony
7
+ goliath
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ addressable (2.2.8)
13
+ async-rack (0.5.1)
14
+ rack (~> 1.1)
15
+ cookiejar (0.3.0)
16
+ diff-lcs (1.1.3)
17
+ em-http-request (1.0.2)
18
+ addressable (>= 2.2.3)
19
+ cookiejar
20
+ em-socksify
21
+ eventmachine (>= 1.0.0.beta.4)
22
+ http_parser.rb (>= 0.5.3)
23
+ em-socksify (0.2.0)
24
+ eventmachine (>= 1.0.0.beta.4)
25
+ em-synchrony (1.0.1)
26
+ eventmachine (>= 1.0.0.beta.1)
27
+ eventmachine (1.0.0.beta.4)
28
+ goliath (0.9.4)
29
+ async-rack
30
+ em-synchrony (>= 1.0.0)
31
+ eventmachine (>= 1.0.0.beta.3)
32
+ http_parser.rb
33
+ http_router (~> 0.9.0)
34
+ log4r
35
+ multi_json
36
+ rack (>= 1.2.2)
37
+ rack-contrib
38
+ rack-respond_to
39
+ http_parser.rb (0.5.3)
40
+ http_router (0.9.7)
41
+ rack (>= 1.0.0)
42
+ url_mount (~> 0.2.1)
43
+ log4r (1.1.10)
44
+ multi_json (1.3.5)
45
+ rack (1.4.1)
46
+ rack-accept-media-types (0.9)
47
+ rack-contrib (1.1.0)
48
+ rack (>= 0.9.1)
49
+ rack-respond_to (0.9.8)
50
+ rack-accept-media-types (>= 0.6)
51
+ rspec (2.10.0)
52
+ rspec-core (~> 2.10.0)
53
+ rspec-expectations (~> 2.10.0)
54
+ rspec-mocks (~> 2.10.0)
55
+ rspec-core (2.10.1)
56
+ rspec-expectations (2.10.0)
57
+ diff-lcs (~> 1.1.3)
58
+ rspec-mocks (2.10.1)
59
+ url_mount (0.2.1)
60
+ rack
61
+
62
+ PLATFORMS
63
+ ruby
64
+
65
+ DEPENDENCIES
66
+ goliath-reverse-proxy!
67
+ rspec (~> 2.10)
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ goliath-reverse-proxy
2
+ =====================
3
+
4
+ Goliath reverse proxy middleware. Forwards all requests to another server.
5
+
6
+ Dependencies
7
+ ============
8
+ All the work is done by em-http-request, em-syncrony, and goliath.
9
+
10
+ Example
11
+ =======
12
+
13
+ ```ruby
14
+ require 'goliath'
15
+ require 'goliath/rack/reverse_proxy'
16
+
17
+ class Proxy < Goliath::API
18
+ # Include other middleware here before the proxy
19
+ # Params is required to pass along data
20
+ use Goliath::Rack::Params
21
+ use Goliath::Rack::ReverseProxy, base_url: 'http://127.0.0.1:8000'
22
+
23
+ def response(env)
24
+ [200, {}, []]
25
+ end
26
+ end
27
+ ```
28
+
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = 'goliath-reverse-proxy'
5
+ gem.version = '0.1.0'
6
+ gem.authors = ["Daniel Farrell"]
7
+ gem.email = 'danielfarrell76@gmail.com'
8
+ gem.homepage = 'https://github.com/danielfarrell/goliath-reverse-proxy'
9
+ gem.summary = 'Reverse proxy middlware for Goliath'
10
+ gem.files = `git ls-files`.split("\n")
11
+ gem.test_files = `git ls-files -- spec/*`.split("\n")
12
+
13
+ gem.add_dependency "goliath"
14
+ gem.add_dependency "em-synchrony"
15
+ gem.add_dependency "em-http-request"
16
+
17
+ gem.add_development_dependency "rspec", "~> 2.10"
18
+
19
+ gem.extra_rdoc_files = ['README.md']
20
+
21
+ gem.description = <<-EOF
22
+ Turns Goliath into a reverse proxy server. Useful when paired with other
23
+ middleware, such as authentication.
24
+ EOF
25
+ end
@@ -0,0 +1,36 @@
1
+ require 'em-synchrony/em-http'
2
+
3
+ module Goliath
4
+ module Rack
5
+ class ReverseProxy
6
+ include Goliath::Rack::AsyncMiddleware
7
+
8
+ def initialize(app, options)
9
+ @app = app
10
+ @options = options
11
+ end
12
+
13
+ def post_process(env, status, headers, body)
14
+ method = env['REQUEST_METHOD'].downcase.to_sym
15
+
16
+ url = "#{@options[:base_url]}#{env['REQUEST_URI']}"
17
+
18
+ env.each do |key, value|
19
+ headers[$1] = value if key =~ /HTTP_(.*)/
20
+ end
21
+ headers['CONTENT_TYPE'] = env['CONTENT_TYPE']
22
+ headers['HTTP_HOST'] = env['SERVER_NAME']
23
+ headers['X-Forwarded-Host'] = env['HTTP_HOST']
24
+ headers['REMOTE_USER'] = env['REMOTE_USER']
25
+
26
+ params = {:head => headers}
27
+ params[:body] = env['params'] if [:put, :post, :patch].include? method
28
+
29
+ http = EM::HttpRequest.new(url).send(method, params)
30
+
31
+ [http.response_header.status, http.response_header, [http.response]]
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'goliath/rack/reverse_proxy'
3
+
4
+ describe Goliath::Rack::ReverseProxy do
5
+ it 'should load without errors' do
6
+ lambda { Goliath::Rack::ReverseProxy.new('app', {:base_url => 'http://localhost'}) }.should_not raise_error
7
+ end
8
+
9
+ describe 'while used as middleware' do
10
+ before(:each) do
11
+ @app = double('app').as_null_object
12
+ @app.should_receive(:call).and_return(initial_response)
13
+ @env = Goliath::Env.new
14
+ @proxy = Goliath::Rack::ReverseProxy.new(@app, {:base_url => 'http://localhost'})
15
+ end
16
+
17
+ it 'should create url to proxy to' do
18
+ @env['REQUEST_METHOD'] = 'GET'
19
+ @env['REQUEST_URI'] = '/a/test/file.html'
20
+ action = double('http action').as_null_object
21
+ action.should_receive(:get).and_return(response_object)
22
+ EM::HttpRequest.should_receive(:new).with('http://localhost/a/test/file.html').and_return(action)
23
+ @proxy.call(@env)
24
+ end
25
+
26
+ it 'should build the needed headers' do
27
+ @env['REQUEST_METHOD'] = 'GET'
28
+ @env['REQUEST_URI'] = '/'
29
+ @env['SERVER_NAME'] = 'test.com'
30
+ @env['HTTP_HOST'] = 'testing.com'
31
+ @env['REMOTE_USER'] = 'bob'
32
+ action = double('http action').as_null_object
33
+ headers = {"HOST"=>"testing.com", "CONTENT_TYPE"=>nil, "HTTP_HOST"=>"test.com", "X-Forwarded-Host"=>"testing.com", "REMOTE_USER"=>"bob"}
34
+ action.should_receive(:get).with(:head => headers).and_return(response_object)
35
+ EM::HttpRequest.should_receive(:new).with('http://localhost/').and_return(action)
36
+ @proxy.call(@env)
37
+ end
38
+
39
+ it 'should do a delete if a get was sent' do
40
+ @env['REQUEST_METHOD'] = 'DELETE'
41
+ @env['REQUEST_URI'] = '/some/item/23'
42
+ action = double('http action').as_null_object
43
+ action.should_receive(:delete).and_return(response_object)
44
+ EM::HttpRequest.should_receive(:new).with('http://localhost/some/item/23').and_return(action)
45
+ @proxy.call(@env)
46
+ end
47
+
48
+ it 'should do a post with body if a post was sent' do
49
+ @env['REQUEST_METHOD'] = 'POST'
50
+ @env['REQUEST_URI'] = '/some/item'
51
+ action = double('http action').as_null_object
52
+ action.should_receive(:post).and_return(response_object)
53
+ EM::HttpRequest.should_receive(:new).with('http://localhost/some/item').and_return(action)
54
+ @proxy.call(@env)
55
+ end
56
+
57
+ it 'should return the response from the proxied request' do
58
+ @env['REQUEST_METHOD'] = 'GET'
59
+ @env['REQUEST_URI'] = '/a/test/file.html'
60
+ results = response_object(401, {}, ['testing'])
61
+ action = double('http action').as_null_object
62
+ action.should_receive(:get).and_return(results)
63
+ EM::HttpRequest.should_receive(:new).with('http://localhost/a/test/file.html').and_return(action)
64
+ response = @proxy.call(@env)
65
+ response.should == [401, {}, [["testing"]]]
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,35 @@
1
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+ $:.unshift File.expand_path(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'goliath'
6
+ require 'ostruct'
7
+
8
+ class ResponseHeader < Hash
9
+ def status
10
+ @status || 200
11
+ end
12
+
13
+ def status=(status)
14
+ @status = status
15
+ end
16
+ end
17
+
18
+ class Response
19
+ attr_reader :response, :response_header
20
+
21
+ def initialize(code, headers, response)
22
+ @response = response
23
+ @response_header = ResponseHeader.new(headers)
24
+ @response_header.status = code
25
+ end
26
+
27
+ end
28
+
29
+ def response_object(code=200, headers={}, body=[])
30
+ Response.new(code, headers, body)
31
+ end
32
+
33
+ def initial_response
34
+ [200, {}, []]
35
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goliath-reverse-proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Farrell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: goliath
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: em-synchrony
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: em-http-request
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
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'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '2.10'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.10'
78
+ description: ! " Turns Goliath into a reverse proxy server. Useful when paired
79
+ with other\n middleware, such as authentication.\n"
80
+ email: danielfarrell76@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files:
84
+ - README.md
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - Gemfile.lock
89
+ - README.md
90
+ - goliath-reverse-proxy.gemspec
91
+ - lib/goliath/rack/reverse_proxy.rb
92
+ - spec/goliath/reverse_proxy_spec.rb
93
+ - spec/spec_helper.rb
94
+ homepage: https://github.com/danielfarrell/goliath-reverse-proxy
95
+ licenses: []
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.23
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Reverse proxy middlware for Goliath
118
+ test_files:
119
+ - spec/goliath/reverse_proxy_spec.rb
120
+ - spec/spec_helper.rb