Class: CarbonCopy::Request
- Inherits:
-
Object
- Object
- CarbonCopy::Request
- Defined in:
- lib/carbon-copy/request.rb
Instance Attribute Summary collapse
-
#header_str ⇒ Object
Returns the value of attribute header_str.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#request ⇒ Object
Returns the value of attribute request.
-
#request_str ⇒ Object
Returns the value of attribute request_str.
-
#uri ⇒ Object
Returns the value of attribute uri.
-
#url ⇒ Object
Returns the value of attribute url.
-
#verb ⇒ Object
Returns the value of attribute verb.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(session) ⇒ Request
constructor
A new instance of Request.
- #parse ⇒ Object
- #parse_headers(request) ⇒ Object
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_str ⇒ Object
Returns the value of attribute header_str.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def header_str @header_str end |
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def host @host end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def port @port end |
#request ⇒ Object
Returns the value of attribute request.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def request @request end |
#request_str ⇒ Object
Returns the value of attribute request_str.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def request_str @request_str end |
#uri ⇒ Object
Returns the value of attribute uri.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def uri @uri end |
#url ⇒ Object
Returns the value of attribute url.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def url @url end |
#verb ⇒ Object
Returns the value of attribute verb.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def verb @verb end |
#version ⇒ Object
Returns the value of attribute version.
5 6 7 |
# File 'lib/carbon-copy/request.rb', line 5 def version @version end |
Instance Method Details
#parse ⇒ Object
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 |