lib/microget.rb
def perform_get(uri, request_headers: {}, chunk_size: 1024 * 1024 * 5)
    status_code, header_hash, socket = get_status_headers_and_body_socket(uri, request_headers: request_headers)
    body_bytes_received = 0
    
    # Yield the status and headers once with an empty response
    # so that the client can bail out of the request even before the body
    # starts to arrive
    return body_bytes_received unless yield(status_code, header_hash, '')
    
    # We are using read_nonblock, and it allows a buffer to be passed in.
    # The advantage of passing a buffer is that the same Ruby string is
    # reused for all the reads, and only the string contents gets reallocated.
    # We can reduce GC pressure this way.
    body_buf = ''
    
    # ...and then just read the body, without any buffering, using a non-blocking read
    while !socket.eof?
      begin
        data = socket.read_nonblock(chunk_size, body_buf)
        body_bytes_received += data.bytesize
        continue_reading = yield(status_code, header_hash, body_buf)
        return body_bytes_received unless continue_reading 
      rescue IO::WaitReadable
        IO.select([socket], [], SOCKET_TIMEOUT)
        retry
      end
    end
    
    body_bytes_received
  ensure
    socket.close if socket && !socket.closed?
  end
# Executes a GET request to the given URI. Will yield the status, header hash and a chunk of the body
# to the given block.
#
# The socket will be read from as long as the block given to the method yields a truthy value.
# Once the block returns a truthy value (or the HTTP response is read completely) the method
# will return the number of bytes of the body it did read and terminate.
#
# @param uri[String] the full URI of the request
# @param request_headers[Hash] all the request headers to send with the request
# @param chunk_size[Numeric] what size to feed to read() when reading the response from the socket
# @yield [Array<Numeric, Hash, String>] the status code, the header hash and the chunk of the body data read.
# @return [Numeric] the total number of body bytes read from the socket 
Executes a GET request to the given URI. Will yield the status, header hash and a chunk of the body
to the given block.

The socket will be read from as long as the block given to the method yields a truthy value.
Once the block returns a truthy value (or the HTTP response is read completely) the method
will return the number of bytes of the body it did read and terminate.
Executes a GET request to the given URI . Will yield the status header hash and a chunk of the body to the given block . The socket will be read from as long as the block given to the method yields a truthy value . Once the block returns a truthy value ( or the HTTP response is read completely ) the method will return the number of bytes of the body it did read and terminate .
def perform_get(uri, request_headers: {}, chunk_size: 1024 * 1024 * 5)
    status_code, header_hash, socket = get_status_headers_and_body_socket(uri, request_headers: request_headers)
    body_bytes_received = 0
    return body_bytes_received unless yield(status_code, header_hash, '')
    body_buf = ''
    while !socket.eof?
      begin
        data = socket.read_nonblock(chunk_size, body_buf)
        body_bytes_received += data.bytesize
        continue_reading = yield(status_code, header_hash, body_buf)
        return body_bytes_received unless continue_reading 
      rescue IO::WaitReadable
        IO.select([socket], [], SOCKET_TIMEOUT)
        retry
      end
    end
    body_bytes_received
  ensure
    socket.close if socket && !socket.closed?
  end
def perform_get ( uri , request_headers : { } , chunk_size : 1024 * 1024 * 5 ) status_code , header_hash , socket = get_status_headers_and_body_socket ( uri , request_headers : request_headers ) body_bytes_received = 0 return body_bytes_received unless yield ( status_code , header_hash , '' ) body_buf = '' while ! socket . eof? begin data = socket . read_nonblock ( chunk_size , body_buf ) body_bytes_received += data . bytesize continue_reading = yield ( status_code , header_hash , body_buf ) return body_bytes_received unless continue_reading rescue IO :: WaitReadable IO . select ( [ socket ] , [ ] , SOCKET_TIMEOUT ) retry end end body_bytes_received ensure socket . close if socket && ! socket . closed? end
Executes a GET request to the given URI.
Executes a GET request to the given URI .
# Yield the status and headers once with an empty response # so that the client can bail out of the request even before the body # starts to arrive # We are using read_nonblock, and it allows a buffer to be passed in. # The advantage of passing a buffer is that the same Ruby string is # reused for all the reads, and only the string contents gets reallocated. # We can reduce GC pressure this way. # ...and then just read the body, without any buffering, using a non-blocking read
the total number of body bytes read from the socket
the total number of body bytes read from the socket
Numeric
uri
the full URI of the request
the full URI of the request
request_headers
all the request headers to send with the request
all the request headers to send with the request
chunk_size
None
]
the status code, the header hash and the chunk of the body data read.
the status code the header hash and the chunk of the body data read .