rack-proxy 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rack-proxy (0.3.7)
4
+ rack-proxy (0.4.0)
5
5
  rack
6
6
 
7
7
  GEM
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Jacek Becela jacek.becela@gmail.com
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.
@@ -9,21 +9,21 @@ module Rack
9
9
  def initialize(request, host, port = nil)
10
10
  @request, @host, @port = request, host, port
11
11
  end
12
-
12
+
13
13
  def status
14
14
  response.code.to_i
15
15
  end
16
-
16
+
17
17
  def headers
18
18
  h = Utils::HeaderHash.new
19
-
20
- response.each_header do |k, v|
19
+
20
+ response.to_hash.each do |k, v|
21
21
  h[k] = v
22
22
  end
23
-
23
+
24
24
  h
25
25
  end
26
-
26
+
27
27
  def body
28
28
  self
29
29
  end
@@ -34,7 +34,7 @@ module Rack
34
34
  ensure
35
35
  session.end_request_hacked
36
36
  end
37
-
37
+
38
38
  def to_s
39
39
  @body ||= begin
40
40
  lines = []
@@ -42,18 +42,18 @@ module Rack
42
42
  each do |line|
43
43
  lines << line
44
44
  end
45
-
45
+
46
46
  lines.join
47
47
  end
48
48
  end
49
-
49
+
50
50
  protected
51
-
51
+
52
52
  # Net::HTTPResponse
53
53
  def response
54
54
  @response ||= session.begin_request_hacked(@request)
55
55
  end
56
-
56
+
57
57
  # Net::HTTP
58
58
  def session
59
59
  @session ||= begin
@@ -62,7 +62,7 @@ module Rack
62
62
  http.start
63
63
  end
64
64
  end
65
-
65
+
66
66
  end
67
67
 
68
68
  end
data/lib/rack/proxy.rb CHANGED
@@ -5,8 +5,13 @@ module Rack
5
5
 
6
6
  # Subclass and bring your own #rewrite_request and #rewrite_response
7
7
  class Proxy
8
- VERSION = "0.3.7"
9
-
8
+ VERSION = "0.4.0"
9
+
10
+ # @option opts [String, URI::HTTP] :backend Backend host to proxy requests to
11
+ def initialize opts = {}
12
+ @backend = URI(opts[:backend]) if opts[:backend]
13
+ end
14
+
10
15
  def call(env)
11
16
  rewrite_response(perform_request(rewrite_env(env)))
12
17
  end
@@ -15,7 +20,7 @@ module Rack
15
20
  def rewrite_env(env)
16
21
  env
17
22
  end
18
-
23
+
19
24
  # Return a rack triplet [status, headers, body]
20
25
  def rewrite_response(triplet)
21
26
  triplet
@@ -25,9 +30,15 @@ module Rack
25
30
 
26
31
  def perform_request(env)
27
32
  source_request = Rack::Request.new(env)
28
-
33
+
29
34
  # Initialize request
30
- target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(source_request.fullpath)
35
+ if source_request.fullpath == ""
36
+ full_path = URI.parse(env['REQUEST_URI']).request_uri
37
+ else
38
+ full_path = source_request.fullpath
39
+ end
40
+
41
+ target_request = Net::HTTP.const_get(source_request.request_method.capitalize).new(full_path)
31
42
 
32
43
  # Setup headers
33
44
  target_request.initialize_http_header(extract_http_request_headers(source_request.env))
@@ -35,18 +46,24 @@ module Rack
35
46
  # Setup body
36
47
  if target_request.request_body_permitted? && source_request.body
37
48
  target_request.body_stream = source_request.body
38
- target_request.content_length = source_request.content_length
49
+ target_request.content_length = source_request.content_length.to_i
39
50
  target_request.content_type = source_request.content_type if source_request.content_type
40
51
  end
41
-
52
+
42
53
  # Create a streaming response (the actual network communication is deferred, a.k.a. streamed)
43
- target_response = HttpStreamingResponse.new(target_request, source_request.host, source_request.port)
54
+ if @backend
55
+ target_response = HttpStreamingResponse.new(target_request, @backend.host, @backend.port)
56
+
57
+ target_response.use_ssl = "https" == @backend.scheme
58
+ else
59
+ target_response = HttpStreamingResponse.new(target_request, source_request.host, source_request.port)
60
+
61
+ target_response.use_ssl = "https" == source_request.scheme
62
+ end
44
63
 
45
- target_response.use_ssl = "https" == source_request.scheme
46
-
47
64
  [target_response.status, target_response.headers, target_response.body]
48
65
  end
49
-
66
+
50
67
  def extract_http_request_headers(env)
51
68
  headers = env.reject do |k, v|
52
69
  !(/^HTTP_[A-Z_]+$/ === k) || v.nil?
@@ -2,45 +2,46 @@ require "test_helper"
2
2
  require "rack/http_streaming_response"
3
3
 
4
4
  class HttpStreamingResponseTest < Test::Unit::TestCase
5
-
5
+
6
6
  def setup
7
7
  host, req = "trix.pl", Net::HTTP::Get.new("/")
8
8
  @response = Rack::HttpStreamingResponse.new(req, host)
9
9
  end
10
-
10
+
11
11
  def test_streaming
12
12
  # Response status
13
13
  assert @response.status == 200
14
-
14
+
15
15
  # Headers
16
16
  headers = @response.headers
17
-
17
+
18
18
  assert headers.size > 0
19
- assert headers["content-type"] == "text/html"
20
- assert headers["CoNtEnT-TyPe"] == "text/html"
21
- assert headers["content-length"].to_i > 0
22
-
19
+
20
+ assert headers["content-type"] == ["text/html;charset=utf-8"]
21
+ assert headers["CoNtEnT-TyPe"] == headers["content-type"]
22
+ assert headers["content-length"].first.to_i > 0
23
+
23
24
  # Body
24
25
  chunks = []
25
26
  @response.body.each do |chunk|
26
27
  chunks << chunk
27
28
  end
28
-
29
+
29
30
  assert chunks.size > 0
30
31
  chunks.each do |chunk|
31
32
  assert chunk.is_a?(String)
32
33
  end
33
-
34
-
34
+
35
+
35
36
  end
36
-
37
+
37
38
  def test_to_s
38
- assert_equal @response.headers["Content-Length"].to_i, @response.body.to_s.size
39
+ assert_equal @response.headers["Content-Length"].first.to_i, @response.body.to_s.size
39
40
  end
40
-
41
+
41
42
  def test_to_s_called_twice
42
43
  body = @response.body
43
44
  assert_equal body.to_s, body.to_s
44
45
  end
45
-
46
+
46
47
  end
@@ -42,4 +42,10 @@ class RackProxyTest < Test::Unit::TestCase
42
42
  assert !headers.key?('CONNECTION')
43
43
  assert !headers.key?('NOT-HTTP-HEADER')
44
44
  end
45
+
46
+ def test_handles_missing_content_length
47
+ assert_nothing_thrown do
48
+ post "/", nil, "CONTENT_LENGTH" => nil
49
+ end
50
+ end
45
51
  end
metadata CHANGED
@@ -1,64 +1,60 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rack-proxy
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 3
9
- - 7
10
- version: 0.3.7
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Jacek Becela
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-02-02 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
12
+ date: 2013-06-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :runtime
33
- name: rack
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
38
25
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
46
- type: :development
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
47
31
  name: rack-test
48
- version_requirements: *id002
49
- description: A Rack app that provides request/response rewriting proxy capabilities with streaming.
50
- email:
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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
+ description: A Rack app that provides request/response rewriting proxy capabilities
47
+ with streaming.
48
+ email:
51
49
  - jacek.becela@gmail.com
52
50
  executables: []
53
-
54
51
  extensions: []
55
-
56
52
  extra_rdoc_files: []
57
-
58
- files:
53
+ files:
59
54
  - .gitignore
60
55
  - Gemfile
61
56
  - Gemfile.lock
57
+ - LICENSE
62
58
  - Rakefile
63
59
  - Readme
64
60
  - lib/net_http_hacked.rb
@@ -70,41 +66,31 @@ files:
70
66
  - test/net_http_hacked_test.rb
71
67
  - test/rack_proxy_test.rb
72
68
  - test/test_helper.rb
73
- has_rdoc: true
74
69
  homepage: http://rubygems.org/gems/rack-proxy
75
70
  licenses: []
76
-
77
71
  post_install_message:
78
72
  rdoc_options: []
79
-
80
- require_paths:
73
+ require_paths:
81
74
  - lib
82
- required_ruby_version: !ruby/object:Gem::Requirement
75
+ required_ruby_version: !ruby/object:Gem::Requirement
83
76
  none: false
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- hash: 3
88
- segments:
89
- - 0
90
- version: "0"
91
- required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
82
  none: false
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- hash: 3
97
- segments:
98
- - 0
99
- version: "0"
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
100
87
  requirements: []
101
-
102
88
  rubyforge_project: rack-proxy
103
- rubygems_version: 1.6.2
89
+ rubygems_version: 1.8.23
104
90
  signing_key:
105
91
  specification_version: 3
106
92
  summary: A request/response rewriting HTTP proxy. A Rack app.
107
- test_files:
93
+ test_files:
108
94
  - test/http_streaming_response_test.rb
109
95
  - test/net_http_hacked_test.rb
110
96
  - test/rack_proxy_test.rb