slowproxy 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f7ea6418d70a1dd7b806e74b654902b6c62e175
4
- data.tar.gz: dacd479083a700b4dbb63ba98559cb445688cbe4
3
+ metadata.gz: 3dcec1ec81a38744cef9f2ce07661d8f90ad969f
4
+ data.tar.gz: 8d1fdd0a2fad34b56c41b490980a20faa3df3c40
5
5
  SHA512:
6
- metadata.gz: b0e6842cac9bf037cf1c836150a8509f27ac60fb4dbbe458242770675bb66b22085b7081603192e0da3977ac2b9279f313b2e59cc170df7e2f16bf8232d424bf
7
- data.tar.gz: 22ff37cdb2b0941d2df6b64b047861f0c0f225df1ab9f59cf81cb185ab101d2fddeec2b45744c760cbcd4682b368bd2ab1c7330e95ef1d090a4947f0ecea14ab
6
+ metadata.gz: 4567ec6977d45b0c6d1696b288c926f75bf770a7ae94101b1ef22cea84e3cfb4a09a86ac1d71264313fd7296fc555a3ba1feb6b2c2360fa69d9f73bb0dd5dc5d
7
+ data.tar.gz: 34b81cdf1e48c444bbcde0295deea629a1d202f551df9cc62ccc6ca8f15015ecf15bd304b0a22076fd311e7ef4e7e57a74ad5c589c6104f89b8f186d4bbbb4e9
@@ -56,5 +56,97 @@ module Slowproxy
56
56
  set_via(res)
57
57
  res.body = response.body
58
58
  end
59
+
60
+ def wait_for_connect
61
+ @wait ||= 1 / ((SlowBufferedIO.bps / 8.0) / 1024)
62
+ end
63
+
64
+ def do_CONNECT(req, res)
65
+ # Proxy Authentication
66
+ proxy_auth(req, res)
67
+
68
+ ua = Thread.current[:WEBrickSocket] # User-Agent
69
+ raise WEBrick::HTTPStatus::InternalServerError,
70
+ "[BUG] cannot get socket" unless ua
71
+
72
+ host, port = req.unparsed_uri.split(":", 2)
73
+ # Proxy authentication for upstream proxy server
74
+ if proxy = proxy_uri(req, res)
75
+ proxy_request_line = "CONNECT #{host}:#{port} HTTP/1.0"
76
+ if proxy.userinfo
77
+ credentials = "Basic " + [proxy.userinfo].pack("m").delete("\n")
78
+ end
79
+ host, port = proxy.host, proxy.port
80
+ end
81
+
82
+ begin
83
+ @logger.debug("CONNECT: upstream proxy is `#{host}:#{port}'.")
84
+ os = TCPSocket.new(host, port) # origin server
85
+
86
+ if proxy
87
+ @logger.debug("CONNECT: sending a Request-Line")
88
+ os << proxy_request_line << CRLF
89
+ @logger.debug("CONNECT: > #{proxy_request_line}")
90
+ if credentials
91
+ @logger.debug("CONNECT: sending a credentials")
92
+ os << "Proxy-Authorization: " << credentials << CRLF
93
+ end
94
+ os << CRLF
95
+ proxy_status_line = os.gets(LF)
96
+ @logger.debug("CONNECT: read a Status-Line form the upstream server")
97
+ @logger.debug("CONNECT: < #{proxy_status_line}")
98
+ if %r{^HTTP/\d+\.\d+\s+200\s*} =~ proxy_status_line
99
+ while line = os.gets(LF)
100
+ break if /\A(#{CRLF}|#{LF})\z/om =~ line
101
+ end
102
+ else
103
+ raise WEBrick::HTTPStatus::BadGateway
104
+ end
105
+ end
106
+ @logger.debug("CONNECT #{host}:#{port}: succeeded")
107
+ res.status = WEBrick::HTTPStatus::RC_OK
108
+ rescue => ex
109
+ @logger.debug("CONNECT #{host}:#{port}: failed `#{ex.message}'")
110
+ res.set_error(ex)
111
+ raise WEBrick::HTTPStatus::EOFError
112
+ ensure
113
+ if handler = @config[:ProxyContentHandler]
114
+ handler.call(req, res)
115
+ end
116
+ res.send_response(ua)
117
+ access_log(@config, req, res)
118
+
119
+ # Should clear request-line not to send the response twice.
120
+ # see: HTTPServer#run
121
+ req.parse(NullReader) rescue nil
122
+ end
123
+
124
+ begin
125
+ while fds = IO::select([ua, os])
126
+ if fds[0].member?(ua)
127
+ buf = ua.sysread(1024);
128
+ @logger.debug("CONNECT: #{buf.bytesize} byte from User-Agent")
129
+ # Write slowly
130
+ @logger.debug "wait for write (#{wait_for_connect}s)"
131
+ sleep wait_for_connect
132
+ # /Write slowly
133
+ os.syswrite(buf)
134
+ elsif fds[0].member?(os)
135
+ # Read slowly
136
+ @logger.debug "wait for read (#{wait_for_connect}s)"
137
+ sleep wait_for_connect
138
+ # /Read slowly
139
+ buf = os.sysread(1024);
140
+ @logger.debug("CONNECT: #{buf.bytesize} byte from #{host}:#{port}")
141
+ ua.syswrite(buf)
142
+ end
143
+ end
144
+ rescue => ex
145
+ os.close
146
+ @logger.debug("CONNECT #{host}:#{port}: closed")
147
+ end
148
+
149
+ raise WEBrick::HTTPStatus::EOFError
150
+ end
59
151
  end
60
152
  end
@@ -1,3 +1,3 @@
1
1
  module Slowproxy
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slowproxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - labocho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-18 00:00:00.000000000 Z
11
+ date: 2014-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler