uv-rays 2.2.2 → 2.3.0

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: b98ca3114c943ec05175971d81d65bdfe6572894
4
- data.tar.gz: d92dcde8d7221fb14a75b6c9d8b3e38af49560bd
3
+ metadata.gz: f2071c1ca1785836a027d0c84859a9ab8f8fdd39
4
+ data.tar.gz: b82be2f6611fecd989e85693894ea4996ff16cc1
5
5
  SHA512:
6
- metadata.gz: dd9826d083d991d08c12710c693e8eaefa79d74bfb479f104d0f32574dfc820fa58a48264ee5aae3f0cac84066611a381b727e9f437d01a3dd8d7ca4122d844d
7
- data.tar.gz: 684db01106810c7fe628b1aaceeb917468b25d65f0114350a3fa56f08ad8543a9eea62affe92056ad4da3b8540120d2294063da7d1c29661b7679db62788b1b1
6
+ metadata.gz: d37fadf118df972c3a8e9ddeddc1142e36e3f45b24e67cfc21e54447b9570156e495d0bdf8181e96660b80e105e8cd075e52042f69c5e4337f8de3fa6bb24ab9
7
+ data.tar.gz: d962b2226429fb52d9286745881a5c2100680f5601c203780dbfe306016c1bf369ef5d26aa2bb2a4fc26cccb7f8bc7e06adce3f02cb42337602ae13f7a8e9f21
data/README.md CHANGED
@@ -50,3 +50,14 @@ reactor {
50
50
  }
51
51
 
52
52
  ```
53
+
54
+ # Integrations
55
+
56
+ UV-Rays works with many existing GEMs by integrating into common HTTP abstraction libraries
57
+
58
+ * [Faraday](https://github.com/lostisland/faraday)
59
+ * [HTTPI](https://github.com/savonrb/httpi)
60
+ * [Handsoap](https://github.com/unwire/handsoap)
61
+
62
+
63
+
@@ -34,6 +34,15 @@ module Faraday
34
34
  opts[:inactivity_timeout] = (req[:timeout] * 1000) if req[:timeout]
35
35
  end
36
36
 
37
+ if proxy = env[:request][:proxy]
38
+ opts[:proxy] = {
39
+ host: proxy[:uri].host,
40
+ port: proxy[:uri].port,
41
+ username: proxy[:user],
42
+ password: proxy[:password]
43
+ }
44
+ end
45
+
37
46
  error = nil
38
47
  thread = reactor
39
48
  if thread.running?
@@ -21,6 +21,15 @@ class HTTPI::Adapter::Libuv < HTTPI::Adapter::Base
21
21
  body: @request.body
22
22
  }
23
23
 
24
+ if proxy = @request.proxy
25
+ req[:proxy] = {
26
+ host: proxy.host,
27
+ port: proxy.port,
28
+ username: proxy.user,
29
+ password: proxy.password
30
+ }
31
+ end
32
+
24
33
  # Apply authentication settings
25
34
  auth = @request.auth
26
35
  type = auth.type
@@ -44,8 +44,7 @@ module UV
44
44
 
45
45
  def encode_request(method, uri, query)
46
46
  query = encode_query(uri, query)
47
-
48
- HTTP_REQUEST_HEADER % [method.to_s.upcase, query]
47
+ String.new(HTTP_REQUEST_HEADER % [method.to_s.upcase, query])
49
48
  end
50
49
 
51
50
  def encode_query(uri, query)
@@ -33,6 +33,7 @@ module UV
33
33
 
34
34
  @host = endpoint.host
35
35
  @port = endpoint.port
36
+ @http_proxy = endpoint.http_proxy? ? endpoint.proxy : nil
36
37
  @encoded_host = endpoint.encoded_host
37
38
 
38
39
  path = options[:path]
@@ -46,6 +47,7 @@ module UV
46
47
  @cookiejar = endpoint.cookiejar
47
48
  @middleware = endpoint.middleware
48
49
  @uri = "#{endpoint.scheme}://#{@encoded_host}#{@path}"
50
+ @path = @uri if @http_proxy
49
51
  endpoint = nil
50
52
 
51
53
  @options = options
@@ -139,7 +141,10 @@ module UV
139
141
  head['content-type'] = 'application/x-www-form-urlencoded'
140
142
  end
141
143
 
142
- request_header = encode_request(method, path, query)
144
+ request_header = encode_request(method, @path, query)
145
+ if @http_proxy && (@http_proxy[:username] || @http_proxy[:password])
146
+ request_header << encode_auth('Proxy-Authorization', [@http_proxy[:username], @http_proxy[:password]])
147
+ end
143
148
  request_header << encode_headers(head)
144
149
  request_header << CRLF
145
150
 
@@ -33,33 +33,76 @@ module UV
33
33
  end
34
34
  end # CookieJar
35
35
 
36
+ # HTTPS Proxy - connect to proxy
37
+ # CONNECT #{target_host}:#{target_port} HTTP/1.0\r\n"
38
+ # Proxy-Authorization: Basic #{encoded_credentials}\r\n
39
+ # \r\n
40
+ # Parse response =~ %r{\AHTTP/1\.[01] 200 .*\r\n\r\n}m
41
+ # use_tls
42
+ # send requests as usual
43
+
44
+ # HTTP Proxy - connect to proxy
45
+ # GET #{url_with_host} HTTP/1.1\r\n"
46
+ # Proxy-Authorization: Basic #{encoded_credentials}\r\n
47
+ # \r\n
48
+
36
49
  class HttpEndpoint
37
50
  class Connection < OutboundConnection
38
- def initialize(host, port, tls, client)
51
+ def initialize(host, port, tls, proxy, client)
52
+ @target_host = host
39
53
  @client = client
40
54
  @request = nil
41
- super(host, port)
42
55
 
43
- if tls
44
- opts = {host_name: host}.merge(client.tls_options)
45
- use_tls(opts)
56
+ if proxy
57
+ super(proxy[:host], proxy[:port])
58
+ connect_send_handshake(host, port, proxy) if tls
59
+ else
60
+ super(host, port)
61
+ start_tls if tls
46
62
  end
47
63
  end
48
64
 
65
+ def start_tls
66
+ opts = {host_name: @target_host}.merge(@client.tls_options)
67
+ use_tls(opts)
68
+ end
69
+
70
+ def connect_send_handshake(target_host, target_port, proxy)
71
+ @negotiating = true
72
+ header = String.new("CONNECT #{target_host}:#{target_port} HTTP/1.0\r\n")
73
+ if proxy[:username] || proxy[:password]
74
+ encoded_credentials = Base64.strict_encode64([proxy[:username], proxy[:password]].join(":"))
75
+ header << "Proxy-Authorization: Basic #{encoded_credentials}\r\n"
76
+ end
77
+ header << "\r\n"
78
+ write(header)
79
+ end
80
+
49
81
  attr_accessor :request, :reason
50
82
 
51
- def on_read(data, *args) # user to define
52
- @client.data_received(data)
83
+ def on_read(data, *args)
84
+ if @negotiating
85
+ @negotiating = false
86
+ if data =~ %r{\AHTTP/1\.[01] 200 .*\r\n\r\n}m
87
+ start_tls
88
+ @client.connection_ready
89
+ else
90
+ @reason = "Unexpected response from proxy: #{data}"
91
+ close_connection
92
+ end
93
+ else
94
+ @client.data_received(data)
95
+ end
53
96
  end
54
97
 
55
98
  def post_init(*args)
56
99
  end
57
100
 
58
- def on_connect(transport) # user to define
59
- @client.connection_ready
101
+ def on_connect(transport)
102
+ @client.connection_ready unless @negotiating
60
103
  end
61
104
 
62
- def on_close # user to define
105
+ def on_close
63
106
  @client.connection_closed(@request, @reason)
64
107
  ensure
65
108
  @request = nil
@@ -100,6 +143,7 @@ module UV
100
143
 
101
144
  default_port = uri.port == uri.default_port
102
145
  @encoded_host = default_port ? @host : "#{@host}:#{@port}"
146
+ @proxy = @options[:proxy]
103
147
 
104
148
  @scheme = uri.scheme
105
149
  @tls = @scheme == 'https'
@@ -110,7 +154,7 @@ module UV
110
154
 
111
155
  attr_accessor :inactivity_timeout
112
156
  attr_reader :tls_options, :port, :host, :tls, :scheme, :encoded_host
113
- attr_reader :cookiejar, :middleware, :thread
157
+ attr_reader :cookiejar, :middleware, :thread, :proxy
114
158
 
115
159
 
116
160
  def get(options = {}); request(:get, options); end
@@ -191,6 +235,10 @@ module UV
191
235
  close_connection
192
236
  end
193
237
 
238
+ def http_proxy?
239
+ @proxy && !@tls
240
+ end
241
+
194
242
 
195
243
  private
196
244
 
@@ -211,7 +259,7 @@ module UV
211
259
 
212
260
  def new_connection
213
261
  if @queue.length > 0 && @connection.nil?
214
- @connection = Connection.new(@host, @port, @tls, self)
262
+ @connection = Connection.new(@host, @port, @tls, @proxy, self)
215
263
  start_timer
216
264
  end
217
265
  @connection
@@ -259,7 +259,7 @@ module UV
259
259
  # @param schedule [String] a standard CRON job line.
260
260
  # @param callback [Proc] a block or method to execute when the event triggers
261
261
  # @return [::UV::Repeat]
262
- def cron(schedule, callback = nil, timezone: nil , &block)
262
+ def cron(schedule, callback = nil, timezone: nil, &block)
263
263
  callback ||= block
264
264
  ms = Scheduler.parse_cron(schedule, timezone: timezone)
265
265
  event = Repeat.new(self, ms)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UV
4
- VERSION = '2.2.2'
4
+ VERSION = '2.3.0'
5
5
  end
@@ -186,7 +186,7 @@ describe UV::HttpEndpoint do
186
186
  @reactor.stop
187
187
  @general_failure << "test timed out"
188
188
  end
189
- @timeout.start(5000)
189
+ @timeout.start(10000)
190
190
 
191
191
  @request_failure = proc { |err|
192
192
  @general_failure << err
@@ -579,4 +579,58 @@ describe UV::HttpEndpoint do
579
579
  expect(@error2).to eq(:connection_failure)
580
580
  end
581
581
  end
582
+
583
+ =begin
584
+ describe 'proxy support' do
585
+ it "should work with a HTTP proxy server" do
586
+ @reactor.run { |reactor|
587
+ server = UV::HttpEndpoint.new 'http://www.whatsmyip.org', {
588
+ #inactivity_timeout: 1000,
589
+ proxy: {
590
+ host: '212.47.252.49',
591
+ port: 3128
592
+ }
593
+ }
594
+
595
+ @response = nil
596
+
597
+ request = server.get(:path => '/', headers: {accept: 'text/html'})
598
+ request.then(proc { |response|
599
+ @response = response
600
+ @reactor.stop
601
+ }, proc { |error|
602
+ @error = error
603
+ })
604
+ }
605
+
606
+ expect(@general_failure).to eq([])
607
+ expect(@response.status).to eq(200)
608
+ end
609
+
610
+ it "should work with a HTTPS proxy server" do
611
+ @reactor.run { |reactor|
612
+ server = UV::HttpEndpoint.new 'https://www.google.com.au', {
613
+ #inactivity_timeout: 1000,
614
+ proxy: {
615
+ host: '212.47.252.49',
616
+ port: 3128
617
+ }
618
+ }
619
+
620
+ @response = nil
621
+
622
+ request = server.get(:path => '/', headers: {accept: 'text/html'})
623
+ request.then(proc { |response|
624
+ @response = response
625
+ @reactor.stop
626
+ }, proc { |error|
627
+ @error = error
628
+ })
629
+ }
630
+
631
+ expect(@general_failure).to eq([])
632
+ expect(@response.status).to eq(200)
633
+ end
634
+ end
635
+ =end
582
636
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uv-rays
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen von Takach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-09 00:00:00.000000000 Z
11
+ date: 2017-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: libuv
@@ -280,7 +280,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
280
280
  version: '0'
281
281
  requirements: []
282
282
  rubyforge_project:
283
- rubygems_version: 2.6.12
283
+ rubygems_version: 2.6.14
284
284
  signing_key:
285
285
  specification_version: 4
286
286
  summary: Abstractions for working with Libuv