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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40bd4048bb72f0169b178246b13b20f86504eba4
4
- data.tar.gz: 28642af4d6aeb959c948d24e6fb122e17cccb197
3
+ metadata.gz: 46b15f44920b575fdc88fd97c64ddb14d670539a
4
+ data.tar.gz: 84676027d29c606d4fba8b9e28c32408899621f8
5
5
  SHA512:
6
- metadata.gz: 5ea297d70ed2aaf3acf4dda9ad65248a0158a49340d509394c3ce7631497486ad930a035ad43a9b0686e26a6edae6886a20b36fa0828cbfdf7c37682bbb3bc3f
7
- data.tar.gz: bb5d0f9279feeb73b69d2a13eb54ad0516ef7ff8bef36437a701761176cd66dec66c286daac11184e795bb747a0534e001a13335446f7dab6a1495cfd2b2d522
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
- [![Gem Version](https://badge.fury.io/rb/HTTPray.svg)](https://badge.fury.io/rb/HTTPray)
4
+ [![Gem Version](https://badge.fury.io/rb/httpray.svg)](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 = "", timeout = 1, ssl_context = nil)
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(
@@ -1,3 +1,3 @@
1
1
  module HTTPray
2
- VERSION = "1.0.2".freeze
2
+ VERSION = "1.0.4".freeze
3
3
  end
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 = "", timeout = 1, ssl_context = nil)
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 Errno::EINPROGRESS
26
- if IO.select(nil, [socket], [socket], timeout)
27
- begin
28
- socket.connect_nonblock(socket_address)
29
- rescue Errno::EISCONN
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
- socket.connect
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
- "Host" => uri.host,
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
- yield(socket) if block_given?
59
- else
60
- raise Timeout
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httpray
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - G Gordon Worley III