httpray 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/httpray/version.rb +1 -1
- data/lib/httpray.rb +24 -26
- data/test/httpray_test.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46b15f44920b575fdc88fd97c64ddb14d670539a
|
4
|
+
data.tar.gz: 84676027d29c606d4fba8b9e28c32408899621f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b9b2ec5dea26db03552cefbc7b8449f88534f63ff13d2b734656fbb1403cb5819264326d7b7c117a2d993148bb8517bd370eb2c652ce5b653954e176ef8e438
|
7
|
+
data.tar.gz: 84d44dfb708a8c4c3c05b46dc74ec0cca0bab673c64c17d021b41c66f0da8de07356dd4a85fa623f635a00c3c63bd80778d8f558bd137384903266f0b8222055
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# httpray
|
2
2
|
Non-blocking HTTP library for Ruby
|
3
3
|
|
4
|
-
[](https://badge.fury.io/rb/httpray)
|
5
5
|
|
6
6
|
Started out the same as the [fire-and-forget](https://github.com/mattetti/fire-and-forget) gem but with a more exposed interface, TLS support, and a better name. Added ideas from [tcp_timeout](https://github.com/lann/tcp-timeout-ruby) and accidentally ended up creating a light-weight, non-blocking HTTP client.
|
7
7
|
|
@@ -20,7 +20,7 @@ gem "httpray"
|
|
20
20
|
```ruby
|
21
21
|
require 'httpray'
|
22
22
|
|
23
|
-
# def HTTParty.request!(method, uri, headers = {}, body =
|
23
|
+
# def HTTParty.request!(method, uri, headers = {}, body = nil, timeout = 1, ssl_context = nil)
|
24
24
|
|
25
25
|
# send an HTTP request and don't listen for the response
|
26
26
|
HTTPray.request(
|
data/lib/httpray/version.rb
CHANGED
data/lib/httpray.rb
CHANGED
@@ -12,7 +12,7 @@ module HTTPray
|
|
12
12
|
"Accept" => "*/*"
|
13
13
|
}.freeze
|
14
14
|
|
15
|
-
def self.request2!(method, uri, headers = {}, body =
|
15
|
+
def self.request2!(method, uri, headers = {}, body = nil, timeout = 1, ssl_context = nil)
|
16
16
|
uri = URI.parse(uri) unless URI === uri
|
17
17
|
address = Socket.getaddrinfo(uri.host, nil, Socket::AF_INET).first[3]
|
18
18
|
socket_address = Socket.pack_sockaddr_in(uri.port, address)
|
@@ -20,18 +20,15 @@ module HTTPray
|
|
20
20
|
socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
|
21
21
|
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
22
22
|
|
23
|
+
expire_time = Time.now + timeout
|
23
24
|
begin
|
25
|
+
raise Timeout if Time.now > expire_time
|
24
26
|
socket.connect_nonblock(socket_address)
|
25
|
-
rescue
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
# connected
|
31
|
-
end
|
32
|
-
else
|
33
|
-
raise Timeout
|
34
|
-
end
|
27
|
+
rescue IO::WaitReadable, IO::WaitWritable
|
28
|
+
select_timeout = expire_time - Time.now
|
29
|
+
select_timeout = 0 if select_timeout < 0
|
30
|
+
IO.select([socket], [socket], [socket], select_timeout)
|
31
|
+
retry
|
35
32
|
end
|
36
33
|
|
37
34
|
original_socket = socket
|
@@ -40,25 +37,26 @@ module HTTPray
|
|
40
37
|
socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
|
41
38
|
socket.hostname = uri.host
|
42
39
|
socket.sync_close = true
|
43
|
-
|
40
|
+
begin
|
41
|
+
raise Timeout if Time.now > expire_time
|
42
|
+
socket.connect_nonblock
|
43
|
+
rescue IO::WaitReadable, IO::WaitWritable
|
44
|
+
select_timeout = expire_time - Time.now
|
45
|
+
select_timeout = 0 if select_timeout < 0
|
46
|
+
IO.select([socket.io], [socket.io], [socket.io], select_timeout)
|
47
|
+
retry
|
48
|
+
end
|
44
49
|
end
|
45
50
|
|
46
|
-
headers = DEFAULT_HEADERS.merge(headers).merge(
|
47
|
-
|
48
|
-
"Content-Length" => body.bytesize)
|
49
|
-
|
50
|
-
if IO.select(nil, [socket], [socket], 1)
|
51
|
-
socket.puts "#{method} #{uri.request_uri} HTTP/1.0\r\n"
|
52
|
-
headers.each do |header, value|
|
53
|
-
socket.puts "#{header}: #{value}\r\n"
|
54
|
-
end
|
55
|
-
socket.puts "\r\n"
|
56
|
-
socket.puts body
|
51
|
+
headers = DEFAULT_HEADERS.merge(headers).merge("Host" => uri.host)
|
52
|
+
headers["Content-Length"] = body.bytesize if body
|
57
53
|
|
58
|
-
|
59
|
-
|
60
|
-
|
54
|
+
socket.write_nonblock "#{method} #{uri.request_uri} HTTP/1.0\r\n"
|
55
|
+
headers.each do |header, value|
|
56
|
+
socket.write_nonblock "#{header}: #{value}\r\n"
|
61
57
|
end
|
58
|
+
socket.write_nonblock "\r\n"
|
59
|
+
socket.write_nonblock body if body
|
62
60
|
return socket, original_socket
|
63
61
|
end
|
64
62
|
|
data/test/httpray_test.rb
CHANGED
@@ -2,6 +2,11 @@ require 'minitest/autorun'
|
|
2
2
|
require 'lib/httpray'
|
3
3
|
|
4
4
|
class HTTPrayTest < MiniTest::Unit::TestCase
|
5
|
+
def test_request_timesout_with_short_timeout
|
6
|
+
assert_raises HTTPray::Timeout do
|
7
|
+
HTTPray.request("GET", "httppppp://httpbin.org/status/200", {}, nil, 0)
|
8
|
+
end
|
9
|
+
end
|
5
10
|
def test_request_timesout_with_bad_address
|
6
11
|
assert_raises HTTPray::Timeout do
|
7
12
|
HTTPray.request("GET", "httppppp://httpbin.org/status/200")
|
@@ -16,6 +21,11 @@ class HTTPrayTest < MiniTest::Unit::TestCase
|
|
16
21
|
assert_equal "HTTP/1.1 200 OK\r\n", socket.gets
|
17
22
|
end
|
18
23
|
end
|
24
|
+
def test_secure_request_timesout_with_short_timeout
|
25
|
+
assert_raises HTTPray::Timeout do
|
26
|
+
HTTPray.request("GET", "https://httpbin.org/status/200", {}, nil, 0)
|
27
|
+
end
|
28
|
+
end
|
19
29
|
def test_secure_request_sends
|
20
30
|
HTTPray.request("GET", "https://httpbin.org/get")
|
21
31
|
assert true
|