class Api2cart::Daemon::HTTPMessageReader

Constants

READ_BUFFER_SIZE

Attributes

parser[R]

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/api2cart/daemon/http_message_reader.rb, line 8
def initialize(*args)
  super
  initialize_parser!
end

Public Instance Methods

read_http_message() click to toggle source
# File lib/api2cart/daemon/http_message_reader.rb, line 13
def read_http_message
  message = read_entire_message_from_socket!
  host, port = parse_host_and_port(parser.headers['Host'])
  OpenStruct.new message: message, request_host: host, request_port: port, request_url: parser.request_url
end

Protected Instance Methods

complete_http_message_received?() click to toggle source
# File lib/api2cart/daemon/http_message_reader.rb, line 45
def complete_http_message_received?
  !! @complete_http_message_received
end
initialize_parser!() click to toggle source
# File lib/api2cart/daemon/http_message_reader.rb, line 23
def initialize_parser!
  @complete_http_message_received = false

  @parser = HTTP::Parser.new

  parser.on_message_complete = ->() do
    @complete_http_message_received = true
  end
end
parse_host_and_port(host_and_port) click to toggle source
# File lib/api2cart/daemon/http_message_reader.rb, line 49
def parse_host_and_port(host_and_port)
  return nil if host_and_port.nil?

  host, port = host_and_port.split(':')
  port ||= 80
  [host, port]
end
read_entire_message_from_socket!() click to toggle source
# File lib/api2cart/daemon/http_message_reader.rb, line 39
def read_entire_message_from_socket!
  ''.tap do |raw_data|
    raw_data << read_next_chunk until complete_http_message_received?
  end
end
read_next_chunk() click to toggle source
# File lib/api2cart/daemon/http_message_reader.rb, line 33
def read_next_chunk
  socket.readpartial(READ_BUFFER_SIZE).tap do |chunk|
    parser << chunk
  end
end