class Nastika::HTTP::Response

Attributes

body[RW]
code[RW]
headers[RW]
httpver[RW]
status[RW]

Public Class Methods

new(response) click to toggle source

Takes in a response string and parses it to give response code, body, http version, status and headers.

# File lib/nastika/http_response.rb, line 16
def initialize(response)
  # HTTP REQ LOOKS LIKE THIS:
  # HTTP/HTTPVER CODE STATUS
  # HEADERS FOLLOWED BY CRLF
  # CRLF
  # OPTIONAL_BODY
  if response.include? "\r\n"
    response = response.split "\r\n"
  else
    response = response.split "\n"
  end
  self.httpver = response[0].split(" ")[0].split("/")[1]
  self.code = response[0].split(" ")[1]
  self.status = response[0].split(" ")[2]
  self.headers = {}

  response.each_with_index do |a, i|
    if a == ""
      self.body = response[i+1]
      break
    end
    next unless a.include? ":"
    self.headers[a.split(":")[0].strip] = a.split(":")[1].strip
  end
  
end