em-proxy 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +3 -1
- data/em-proxy.gemspec +2 -1
- data/examples/duplex.rb +9 -8
- data/examples/http_proxy.rb +4 -2
- data/lib/em-proxy/connection.rb +16 -4
- data/spec/proxy_spec.rb +3 -3
- metadata +47 -11
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
em-proxy (0.1.
|
4
|
+
em-proxy (0.1.6)
|
5
5
|
eventmachine
|
6
6
|
|
7
7
|
GEM
|
@@ -19,6 +19,7 @@ GEM
|
|
19
19
|
eventmachine
|
20
20
|
eventmachine (1.0.0.beta.4)
|
21
21
|
http_parser.rb (0.5.3)
|
22
|
+
rake (0.9.2.2)
|
22
23
|
rspec (2.7.0)
|
23
24
|
rspec-core (~> 2.7.0)
|
24
25
|
rspec-expectations (~> 2.7.0)
|
@@ -35,4 +36,5 @@ DEPENDENCIES
|
|
35
36
|
ansi
|
36
37
|
em-http-request
|
37
38
|
em-proxy!
|
39
|
+
rake
|
38
40
|
rspec
|
data/em-proxy.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "em-proxy"
|
6
|
-
s.version = "0.1.
|
6
|
+
s.version = "0.1.7"
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Ilya Grigorik"]
|
9
9
|
s.email = ["ilya@igvita.com"]
|
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
s.add_development_dependency "rspec"
|
18
18
|
s.add_development_dependency "em-http-request"
|
19
19
|
s.add_development_dependency "ansi"
|
20
|
+
s.add_development_dependency "rake"
|
20
21
|
|
21
22
|
s.files = `git ls-files`.split("\n")
|
22
23
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
data/examples/duplex.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'lib/em-proxy'
|
2
2
|
|
3
|
-
Proxy.start(:host => "0.0.0.0", :port =>
|
3
|
+
Proxy.start(:host => "0.0.0.0", :port => 8000, :debug => true) do |conn|
|
4
4
|
@start = Time.now
|
5
5
|
@data = Hash.new("")
|
6
6
|
|
7
|
-
conn.server :
|
8
|
-
conn.server :
|
7
|
+
conn.server :prod, :host => "127.0.0.1", :port => 8100 # production, will render resposne
|
8
|
+
conn.server :test, :host => "127.0.0.1", :port => 8200 # testing, internal only
|
9
9
|
|
10
10
|
conn.on_data do |data|
|
11
11
|
# rewrite User-Agent
|
12
12
|
data.gsub(/User-Agent: .*?\r\n/, "User-Agent: em-proxy/0.1\r\n")
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
conn.on_response do |server, resp|
|
16
16
|
# only render response from production
|
17
17
|
@data[server] += resp
|
@@ -21,16 +21,17 @@ Proxy.start(:host => "0.0.0.0", :port => 80, :debug => true) do |conn|
|
|
21
21
|
conn.on_finish do |name|
|
22
22
|
p [:on_finish, name, Time.now - @start]
|
23
23
|
p @data
|
24
|
+
:close if name == :prod
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
27
28
|
#
|
28
|
-
# ruby examples/appserver.rb
|
29
|
-
# ruby examples/appserver.rb
|
29
|
+
# ruby examples/appserver.rb 8100 1
|
30
|
+
# ruby examples/appserver.rb 8200 3
|
30
31
|
# ruby examples/line_interceptor.rb
|
31
|
-
# curl localhost
|
32
|
+
# curl localhost:8000
|
32
33
|
#
|
33
34
|
# > [:on_finish, 1.008561]
|
34
35
|
# > {:prod=>"HTTP/1.1 200 OK\r\nConnection: close\r\nDate: Fri, 01 May 2009 04:20:00 GMT\r\nContent-Type: text/plain\r\n\r\nhello world: 0",
|
35
36
|
# :test=>"HTTP/1.1 200 OK\r\nConnection: close\r\nDate: Fri, 01 May 2009 04:20:00 GMT\r\nContent-Type: text/plain\r\n\r\nhello world: 1"}
|
36
|
-
#
|
37
|
+
#
|
data/examples/http_proxy.rb
CHANGED
@@ -2,8 +2,9 @@ require 'em-proxy'
|
|
2
2
|
require 'http/parser' # gem install http_parser.rb
|
3
3
|
require 'uuid' # gem install uuid
|
4
4
|
|
5
|
-
# > ruby
|
5
|
+
# > ruby http_proxy.rb
|
6
6
|
# > curl --proxy localhost:9889 www.google.com
|
7
|
+
# > curl --proxy x.x.x.x:9889 www.google.com - bind ip example
|
7
8
|
|
8
9
|
host = "0.0.0.0"
|
9
10
|
port = 9889
|
@@ -17,7 +18,8 @@ Proxy.start(:host => host, :port => port) do |conn|
|
|
17
18
|
puts "New session: #{session} (#{h.inspect})"
|
18
19
|
|
19
20
|
host, port = h['Host'].split(':')
|
20
|
-
conn.server session, :host => host, :port => (port || 80)
|
21
|
+
conn.server session, :host => host, :port => (port || 80) #, :bind_host => conn.sock[0] - # for bind ip
|
22
|
+
|
21
23
|
conn.relay_to_servers @buffer
|
22
24
|
|
23
25
|
@buffer.clear
|
data/lib/em-proxy/connection.rb
CHANGED
@@ -43,7 +43,7 @@ module EventMachine
|
|
43
43
|
# initialize connections to backend servers
|
44
44
|
#
|
45
45
|
def server(name, opts)
|
46
|
-
srv = EventMachine::
|
46
|
+
srv = EventMachine::bind_connect(opts[:bind_host], opts[:bind_port], opts[:host], opts[:port], EventMachine::ProxyServer::Backend, @debug) do |c|
|
47
47
|
c.name = name
|
48
48
|
c.plexer = self
|
49
49
|
c.proxy_incoming_to(self, 10240) if opts[:relay_server]
|
@@ -57,8 +57,20 @@ module EventMachine
|
|
57
57
|
# [ip, port] of the connected client
|
58
58
|
#
|
59
59
|
def peer
|
60
|
-
|
61
|
-
|
60
|
+
@peer ||= begin
|
61
|
+
peername = get_peername
|
62
|
+
peername ? Socket.unpack_sockaddr_in(peername).reverse : nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# [ip, port] of the local server connect
|
68
|
+
#
|
69
|
+
def sock
|
70
|
+
@sock ||= begin
|
71
|
+
sockname = get_sockname
|
72
|
+
sockname ? Socket.unpack_sockaddr_in(sockname).reverse : nil
|
73
|
+
end
|
62
74
|
end
|
63
75
|
|
64
76
|
#
|
@@ -95,7 +107,7 @@ module EventMachine
|
|
95
107
|
end
|
96
108
|
|
97
109
|
# if all connections are terminated downstream, then notify client
|
98
|
-
if @servers.values.compact.size.zero?
|
110
|
+
if (@servers.values.compact.size.zero? && close != :keep) || (close == :close)
|
99
111
|
close_connection_after_writing
|
100
112
|
end
|
101
113
|
end
|
data/spec/proxy_spec.rb
CHANGED
@@ -133,7 +133,7 @@ describe Proxy do
|
|
133
133
|
lambda {
|
134
134
|
EM.run do
|
135
135
|
EventMachine.add_timer(0.1) do
|
136
|
-
http =EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
|
136
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
|
137
137
|
http.callback { EventMachine.stop }
|
138
138
|
end
|
139
139
|
|
@@ -151,7 +151,7 @@ describe Proxy do
|
|
151
151
|
lambda {
|
152
152
|
EM.run do
|
153
153
|
EventMachine.add_timer(0.1) do
|
154
|
-
http =EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
|
154
|
+
http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
|
155
155
|
http.callback { EventMachine.stop }
|
156
156
|
end
|
157
157
|
|
@@ -164,4 +164,4 @@ describe Proxy do
|
|
164
164
|
end
|
165
165
|
}.should_not raise_error
|
166
166
|
end
|
167
|
-
end
|
167
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-proxy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-10-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: eventmachine
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: rspec
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: em-http-request
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,10 +53,15 @@ dependencies:
|
|
43
53
|
version: '0'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
47
62
|
- !ruby/object:Gem::Dependency
|
48
63
|
name: ansi
|
49
|
-
requirement:
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
50
65
|
none: false
|
51
66
|
requirements:
|
52
67
|
- - ! '>='
|
@@ -54,7 +69,28 @@ dependencies:
|
|
54
69
|
version: '0'
|
55
70
|
type: :development
|
56
71
|
prerelease: false
|
57
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
58
94
|
description: EventMachine Proxy DSL
|
59
95
|
email:
|
60
96
|
- ilya@igvita.com
|
@@ -112,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
148
|
version: '0'
|
113
149
|
requirements: []
|
114
150
|
rubyforge_project: em-proxy
|
115
|
-
rubygems_version: 1.8.
|
151
|
+
rubygems_version: 1.8.24
|
116
152
|
signing_key:
|
117
153
|
specification_version: 3
|
118
154
|
summary: EventMachine Proxy DSL
|