class Rakie::HttpRequest

Constants

PARSE_CONTENT
PARSE_HEAD
PARSE_HEADERS

Attributes

content[RW]
head[RW]
headers[RW]

Public Class Methods

new() click to toggle source
# File lib/rakie/http_proto.rb, line 27
def initialize
  @head = Head.new
  @headers = {}
  @content = ''
end

Public Instance Methods

deserialize(source) click to toggle source

@param [String] source @return [Integer]

# File lib/rakie/http_proto.rb, line 121
def deserialize(source)
  current_state = parse_state

  case current_state
  when PARSE_HEAD
    return parse_source_head(source)

  when PARSE_HEADERS
    return parse_source_headers(source)

  when PARSE_CONTENT
    return parse_source_content(source)
  end
end
parse_source_content(source) click to toggle source

@param [String] source

# File lib/rakie/http_proto.rb, line 97
def parse_source_content(source)
  len = headers["content-length"]
  offset = parse_offset

  if len == nil
    return ParseStatus::COMPLETE
  end

  len = len.to_i

  if source.length >= len + offset
    self.content = source[offset .. (offset + len - 1)]
    self.parse_state = PARSE_HEAD
    self.parse_offset = offset + len

    Log.debug("Rakie::HttpRequest parse source content ok")
    return ParseStatus::COMPLETE
  end

  return ParseStatus::PENDING
end
parse_source_head(source) click to toggle source

@param [String] source

# File lib/rakie/http_proto.rb, line 34
def parse_source_head(source)
  offset = parse_offset

  if eol_offset = source.index("\r\n")
    head_s = source[0 .. eol_offset]
    head_method, head_path, head_version = head_s.split(' ')

    head.method = head_method
    head.path = head_path
    head.version = head_version

    self.parse_state = PARSE_HEADERS
    self.parse_offset = offset + 2

    Log.debug("Rakie::HttpRequest parse source head ok")
    return ParseStatus::CONTINUE
  end

  return ParseStatus::PENDING
end
parse_source_header_item(header) click to toggle source
# File lib/rakie/http_proto.rb, line 55
def parse_source_header_item(header)
  if semi_offset = header.index(':')
    return [
      header[0 .. (semi_offset - 1)].downcase,
      header[(semi_offset + 1) .. -1].strip
    ]
  end
  
  return nil
end
parse_source_headers(source) click to toggle source

@param [String] source

# File lib/rakie/http_proto.rb, line 67
def parse_source_headers(source)
  offset = parse_offset

  while eol_offset = source.index("\r\n", offset)
    header = source[offset .. (eol_offset - 1)]

    if header.length == 0
      self.parse_state = PARSE_CONTENT
      self.parse_offset = eol_offset + 2

      Log.debug("Rakie::HttpRequest parse source header done")
      return ParseStatus::CONTINUE
    end

    header_key, header_value = self.parse_source_header_item(header)

    if header_key
      headers[header_key] = header_value
      Log.debug("Rakie::HttpRequest parse source #{header_key}, #{header_value} header ok")
    end

    offset = eol_offset + 2
  end

  self.parse_offset = offset

  return ParseStatus::PENDING
end
serialize() click to toggle source

@return [String]

# File lib/rakie/http_proto.rb, line 137
def serialize
  data = ""

  data += "#{head.method} #{head.path} #{head.version}"
  data += "\r\n"

  headers_list = []
  headers.each do |k, v|
    headers_list << "#{k}: #{v}"
  end

  data += headers_list.join("\r\n")
  data += "\r\n\r\n"

  data += content
end