@@ -34,66 +34,42 @@ def connect_ssl
3434 end
3535 end
3636
37- # NIO with exceptions
38- if RUBY_VERSION < "2.1.0"
39- # Read data from the socket
40- def readpartial ( size , buffer = nil )
41- rescue_readable do
42- @socket . read_nonblock ( size , buffer )
43- end
44- rescue EOFError
45- :eof
46- end
47-
48- # Write data to the socket
49- def write ( data )
50- rescue_writable do
51- @socket . write_nonblock ( data )
52- end
53- rescue EOFError
54- :eof
55- end
56-
57- # NIO without exceptions
58- else
59- # Read data from the socket
60- def readpartial ( size , buffer = nil )
61- timeout = false
62- loop do
63- result = @socket . read_nonblock ( size , buffer , :exception => false )
64-
65- return :eof if result . nil?
66- return result if result != :wait_readable
67-
68- raise TimeoutError , "Read timed out after #{ @read_timeout } seconds" if timeout
69-
70- # marking the socket for timeout. Why is this not being raised immediately?
71- # it seems there is some race-condition on the network level between calling
72- # #read_nonblock and #wait_readable, in which #read_nonblock signalizes waiting
73- # for reads, and when waiting for x seconds, it returns nil suddenly without completing
74- # the x seconds. In a normal case this would be a timeout on wait/read, but it can
75- # also mean that the socket has been closed by the server. Therefore we "mark" the
76- # socket for timeout and try to read more bytes. If it returns :eof, it's all good, no
77- # timeout. Else, the first timeout was a proper timeout.
78- # This hack has to be done because io/wait#wait_readable doesn't provide a value for when
79- # the socket is closed by the server, and HTTP::Parser doesn't provide the limit for the chunks.
80- timeout = true unless @socket . to_io . wait_readable ( @read_timeout )
81- end
37+ # Read data from the socket
38+ def readpartial ( size , buffer = nil )
39+ timeout = false
40+ loop do
41+ result = @socket . read_nonblock ( size , buffer , :exception => false )
42+
43+ return :eof if result . nil?
44+ return result if result != :wait_readable
45+
46+ raise TimeoutError , "Read timed out after #{ @read_timeout } seconds" if timeout
47+
48+ # marking the socket for timeout. Why is this not being raised immediately?
49+ # it seems there is some race-condition on the network level between calling
50+ # #read_nonblock and #wait_readable, in which #read_nonblock signalizes waiting
51+ # for reads, and when waiting for x seconds, it returns nil suddenly without completing
52+ # the x seconds. In a normal case this would be a timeout on wait/read, but it can
53+ # also mean that the socket has been closed by the server. Therefore we "mark" the
54+ # socket for timeout and try to read more bytes. If it returns :eof, it's all good, no
55+ # timeout. Else, the first timeout was a proper timeout.
56+ # This hack has to be done because io/wait#wait_readable doesn't provide a value for when
57+ # the socket is closed by the server, and HTTP::Parser doesn't provide the limit for the chunks.
58+ timeout = true unless @socket . to_io . wait_readable ( @read_timeout )
8259 end
60+ end
8361
84- # Write data to the socket
85- def write ( data )
86- timeout = false
87- loop do
88- result = @socket . write_nonblock ( data , :exception => false )
89- return result unless result == :wait_writable
62+ # Write data to the socket
63+ def write ( data )
64+ timeout = false
65+ loop do
66+ result = @socket . write_nonblock ( data , :exception => false )
67+ return result unless result == :wait_writable
9068
91- raise TimeoutError , "Write timed out after #{ @write_timeout } seconds" if timeout
69+ raise TimeoutError , "Write timed out after #{ @write_timeout } seconds" if timeout
9270
93- timeout = true unless @socket . to_io . wait_writable ( @write_timeout )
94- end
71+ timeout = true unless @socket . to_io . wait_writable ( @write_timeout )
9572 end
96-
9773 end
9874 end
9975 end
0 commit comments