skylight 0.0.2 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,100 @@
1
+ module Skylight
2
+ module Vendor
3
+ module Excon
4
+ class SSLSocket < Socket
5
+
6
+ def initialize(data = {})
7
+ @data = data
8
+ check_nonblock_support
9
+
10
+ super
11
+
12
+ # create ssl context
13
+ ssl_context = OpenSSL::SSL::SSLContext.new
14
+
15
+ if @data[:ssl_verify_peer]
16
+ # turn verification on
17
+ ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER
18
+
19
+ if @data[:ssl_ca_path]
20
+ ssl_context.ca_path = @data[:ssl_ca_path]
21
+ elsif @data[:ssl_ca_file]
22
+ ssl_context.ca_file = @data[:ssl_ca_file]
23
+ end
24
+ else
25
+ # turn verification off
26
+ ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
27
+ end
28
+
29
+ if @data.has_key?(:client_cert) && @data.has_key?(:client_key)
30
+ ssl_context.cert = OpenSSL::X509::Certificate.new(File.read(@data[:client_cert]))
31
+ ssl_context.key = OpenSSL::PKey::RSA.new(File.read(@data[:client_key]))
32
+ end
33
+
34
+ if @data[:proxy]
35
+ request = 'CONNECT ' << @data[:host] << ':' << @data[:port] << Excon::HTTP_1_1
36
+ request << 'Host: ' << @data[:host] << ':' << @data[:port] << Excon::CR_NL
37
+
38
+ if @data[:proxy][:password] || @data[:proxy][:user]
39
+ auth = ['' << @data[:proxy][:user].to_s << ':' << @data[:proxy][:password].to_s].pack('m').delete(Excon::CR_NL)
40
+ request << "Proxy-Authorization: Basic " << auth << Excon::CR_NL
41
+ end
42
+
43
+ request << 'Proxy-Connection: Keep-Alive' << Excon::CR_NL
44
+
45
+ request << Excon::CR_NL
46
+
47
+ # write out the proxy setup request
48
+ @socket.write(request)
49
+
50
+ # eat the proxy's connection response
51
+ Excon::Response.parse(@socket, { :expects => 200, :method => "CONNECT" })
52
+ end
53
+
54
+ # convert Socket to OpenSSL::SSL::SSLSocket
55
+ @socket = OpenSSL::SSL::SSLSocket.new(@socket, ssl_context)
56
+ @socket.sync_close = true
57
+ @socket.connect
58
+
59
+ # Server Name Indication (SNI) RFC 3546
60
+ if @socket.respond_to?(:hostname=)
61
+ @socket.hostname = @data[:host]
62
+ end
63
+
64
+ # verify connection
65
+ if @data[:ssl_verify_peer]
66
+ @socket.post_connection_check(@data[:host])
67
+ end
68
+
69
+ @socket
70
+ end
71
+
72
+ def connect
73
+ check_nonblock_support
74
+ super
75
+ end
76
+
77
+ def read(max_length=nil)
78
+ check_nonblock_support
79
+ super
80
+ end
81
+
82
+ def write(data)
83
+ check_nonblock_support
84
+ super
85
+ end
86
+
87
+ private
88
+
89
+ def check_nonblock_support
90
+ # backwards compatability for things lacking nonblock
91
+ if !DEFAULT_NONBLOCK && @data[:nonblock]
92
+ $stderr.puts("Excon nonblock is not supported by your OpenSSL::SSL::SSLSocket")
93
+ @data[:nonblock] = false
94
+ end
95
+ end
96
+
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,19 @@
1
+ module Skylight
2
+ module Vendor
3
+ module Excon
4
+ class StandardInstrumentor
5
+ def self.instrument(name, params = {}, &block)
6
+ if params.has_key?(:headers) && params[:headers].has_key?('Authorization')
7
+ params = params.dup
8
+ params[:headers] = params[:headers].dup
9
+ params[:headers]['Authorization'] = REDACTED
10
+ end
11
+ $stderr.puts("#{name} #{params.inspect}")
12
+ if block_given?
13
+ yield
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Skylight
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -181,19 +181,20 @@ module Skylight
181
181
  end
182
182
 
183
183
  def http_post(body)
184
- req = http_request(body.bytesize)
185
- req.body = body
184
+ hdrs = {}
185
+ hdrs[CONTENT_TYPE] = body.bytesize
186
+ hdrs[AUTHORIZATION] = config.authentication_token
187
+ hdrs[CONTENT_TYPE] = APPLICATION_JSON
186
188
 
187
- debug "Posting report to server"
188
- http = Net::HTTP.new config.host, config.port
189
- http.use_ssl = true if config.ssl?
189
+ if config.deflate?
190
+ hdrs[CONTENT_ENCODING] = GZIP
191
+ end
190
192
 
191
- http.start do |http|
192
- resp = http.request req
193
+ debug "Posting report to server"
194
+ res = Excon.post(post_url, :headers => hdrs, :body => body)
193
195
 
194
- unless resp.code == '200'
195
- debug "Server responded with #{resp.code}"
196
- end
196
+ unless res.status == 200
197
+ debug "Server responded with #{res.status}"
197
198
  end
198
199
 
199
200
  true
@@ -204,23 +205,23 @@ module Skylight
204
205
  end
205
206
  end
206
207
 
207
- def http_request(length)
208
- hdrs = {}
208
+ DEFAULT_PORT = {
209
+ 'http' => 80,
210
+ 'https' => 443
211
+ }
209
212
 
210
- hdrs[CONTENT_LENGTH] = length.to_s
211
- hdrs[AUTHORIZATION] = config.authentication_token
212
- hdrs[CONTENT_TYPE] = APPLICATION_JSON
213
+ def post_url
214
+ @post_url ||=
215
+ begin
216
+ proto = config.ssl?? 'https' : 'http'
217
+ host = config.host
213
218
 
214
- if config.deflate?
215
- hdrs[CONTENT_ENCODING] = GZIP
216
- end
219
+ if DEFAULT_PORT[proto] != config.port
220
+ host << ":#{config.port}"
221
+ end
217
222
 
218
- Net::HTTPGenericRequest.new \
219
- 'POST', # Request method
220
- true, # There is a request body
221
- true, # There is a response body
222
- ENDPOINT, # Endpoint
223
- hdrs
223
+ "#{proto}://#{host}#{ENDPOINT}"
224
+ end
224
225
  end
225
226
 
226
227
  def debug(msg)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skylight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-29 00:00:00.000000000 Z
12
+ date: 2013-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -55,6 +55,19 @@ files:
55
55
  - lib/skylight/util/queue.rb
56
56
  - lib/skylight/util/uniform_sample.rb
57
57
  - lib/skylight/util/uuid.rb
58
+ - lib/skylight/vendor/excon.rb
59
+ - lib/skylight/vendor/excon/cacert.pem
60
+ - lib/skylight/vendor/excon/connection.rb
61
+ - lib/skylight/vendor/excon/constants.rb
62
+ - lib/skylight/vendor/excon/errors.rb
63
+ - lib/skylight/vendor/excon/middlewares/base.rb
64
+ - lib/skylight/vendor/excon/middlewares/expects.rb
65
+ - lib/skylight/vendor/excon/middlewares/instrumentor.rb
66
+ - lib/skylight/vendor/excon/middlewares/mock.rb
67
+ - lib/skylight/vendor/excon/response.rb
68
+ - lib/skylight/vendor/excon/socket.rb
69
+ - lib/skylight/vendor/excon/ssl_socket.rb
70
+ - lib/skylight/vendor/excon/standard_instrumentor.rb
58
71
  - lib/skylight/version.rb
59
72
  - lib/skylight/worker.rb
60
73
  - README.md