Class: CarbonCopy::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/carbon-copy/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Request

Returns a new instance of Request.



8
9
10
# File 'lib/carbon-copy/request.rb', line 8

def initialize(session)
  @session = session
end

Instance Attribute Details

#header_strObject

Returns the value of attribute header_str.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def header_str
  @header_str
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def host
  @host
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def path
  @path
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def port
  @port
end

#requestObject

Returns the value of attribute request.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def request
  @request
end

#request_strObject

Returns the value of attribute request_str.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def request_str
  @request_str
end

#uriObject

Returns the value of attribute uri.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def uri
  @uri
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def url
  @url
end

#verbObject

Returns the value of attribute verb.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def verb
  @verb
end

#versionObject

Returns the value of attribute version.



5
6
7
# File 'lib/carbon-copy/request.rb', line 5

def version
  @version
end

Instance Method Details

#parseObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/carbon-copy/request.rb', line 12

def parse
  request = @session.readline

  #---  Initial host/uri information  -------------------------------------
  @verb    = request.slice!(/^\w+\s/).strip
  @host    = request.slice!(/^\/[^\/: ]+/)[1..-1]
  @port    = request.slice!(/^:(\d+)/)

  @port    = ( @port.nil? ) ? '80' : @port[1..-1] # Remove the colon

  @path    = request.slice!(/^(\S)+/)
  @version = request[/HTTP\/(1\.\d)\s*$/, 1]
  @url     = "#{@host}#{@path}"
  @uri     = "#{@path || '/'}"

  uri = URI::parse(@uri)
  @request_str = "#{@verb} #{uri.path}?#{uri.query} HTTP/#{@version}\r"

  #---  Header and final response text  -----------------------------------
  @headers = parse_headers(@session)
  
  #---  Update header info  -----------------------------------------------
  @headers["Host"] = @host

  @header_str = @headers.map{|a, b| "#{a}: #{b}"}.join("\r\n")
  @request = "#{@request_str}\n#{@header_str}\r\n\r\n"

  self
end

#parse_headers(request) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/carbon-copy/request.rb', line 42

def parse_headers(request)
  header = {}
  unless request.eof?
    loop do
      line = request.readline
      if line.strip.empty?
        break
      end

      /^(\S+): ([^\r\n]+)/.match(line)
      header[$1] = $2
    end
  end
  header
end