Class: RaptorIO::Protocol::HTTP::Response
- Defined in:
- lib/raptor-io/protocol/http/response.rb
Overview
HTTP Response.
Instance Attribute Summary collapse
-
#code ⇒ Integer
HTTP response status code.
-
#error ⇒ Exception
Exception representing the error that occurred.
-
#message ⇒ String
HTTP response status message.
-
#redirections ⇒ Array<Response>
Automatically followed redirections that eventually led to this response.
- #request ⇒ Request
Attributes inherited from Message
Class Method Summary collapse
-
.inflate(str) ⇒ String
Inflated ‘str`.
- .parse(response) ⇒ Response
-
.unzip(str) ⇒ String
Unziped ‘str`.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Response
constructor
A new instance of Response.
-
#modified? ⇒ Boolean
‘true` if the remote resource has been modified since the date given in the `If-Modified-Since` request header field, `false` otherwise.
-
#redirect? ⇒ Boolean
‘true` if the response is a `3xx` redirect and there is a `Location` header field.
-
#text? ⇒ Bool
‘true` if the response body is textual in nature, `false` otherwise (if binary).
-
#to_s ⇒ String
String representation of the response.
Methods inherited from Message
#http_1_0?, #http_1_1?, #keep_alive?
Constructor Details
#initialize(options = {}) ⇒ Response
This class’ options are in addition to Message#initialize.
Returns a new instance of Response.
39 40 41 42 43 44 45 46 47 |
# File 'lib/raptor-io/protocol/http/response.rb', line 39 def initialize( = {} ) super( ) @body = @body.force_utf8 if text? @code ||= 0 # Holds the redirection responses that eventually led to this one. @redirections ||= [] end |
Instance Attribute Details
#code ⇒ Integer
Returns HTTP response status code.
15 16 17 |
# File 'lib/raptor-io/protocol/http/response.rb', line 15 def code @code end |
#error ⇒ Exception
Returns Exception representing the error that occurred.
28 29 30 |
# File 'lib/raptor-io/protocol/http/response.rb', line 28 def error @error end |
#message ⇒ String
Returns HTTP response status message.
18 19 20 |
# File 'lib/raptor-io/protocol/http/response.rb', line 18 def @message end |
#redirections ⇒ Array<Response>
Returns Automatically followed redirections that eventually led to this response.
25 26 27 |
# File 'lib/raptor-io/protocol/http/response.rb', line 25 def redirections @redirections end |
#request ⇒ Request
Returns HTTP RaptorIO::Protocol::HTTP::Request which triggered this RaptorIO::Protocol::HTTP::Response.
21 22 23 |
# File 'lib/raptor-io/protocol/http/response.rb', line 21 def request @request end |
Class Method Details
.inflate(str) ⇒ String
Returns Inflated ‘str`.
145 146 147 148 149 150 |
# File 'lib/raptor-io/protocol/http/response.rb', line 145 def self.inflate( str ) z = Zlib::Inflate.new s = z.inflate( str ) z.close s end |
.parse(response) ⇒ Response
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/raptor-io/protocol/http/response.rb', line 97 def self.parse( response ) ||= {} # FIXME: The existence of this extra newline at the beginning of a # response suggests a bug somewhere else in the response parsing # code. response = response.gsub(/\A\r\n/, '') headers_string, [:body] = response.split( HEADER_SEPARATOR_PATTERN, 2 ) request_line = headers_string.to_s.lines.first.to_s.chomp [:version], [:code], [:message] = request_line.scan( /HTTP\/([\d.]+)\s+(\d+)\s*(.*)\s*$/ ).flatten .delete(:message) if [:message].to_s.empty? [:code] = [:code].to_i if !headers_string.to_s.empty? [:headers] = Headers.parse( headers_string.split( CRLF_PATTERN )[1..-1].join( "\r\n" ) ) else [:headers] = Headers.new end if ![:body].to_s.empty? # If any encoding has been applied to the body, remove all evidence of it # and adjust the content-length accordingly. case [:headers]['content-encoding'].to_s.downcase when 'gzip', 'x-gzip' [:body] = unzip( [:body] ) when 'deflate', 'compress', 'x-compress' [:body] = inflate( [:body] ) end if [:headers].delete( 'content-encoding' ) || [:headers].delete( 'transfer-encoding' ) [:headers]['content-length'] = [:body].size end end new( ) end |
.unzip(str) ⇒ String
Returns Unziped ‘str`.
154 155 156 157 158 159 160 161 |
# File 'lib/raptor-io/protocol/http/response.rb', line 154 def self.unzip( str ) s = '' s.force_encoding( 'ASCII-8BIT' ) if s.respond_to?( :encoding ) gz = Zlib::GzipReader.new( StringIO.new( str, 'rb' ) ) s << gz.read gz.close s end |
Instance Method Details
#modified? ⇒ Boolean
Depends on the response code.
Returns ‘true` if the remote resource has been modified since the date given in the `If-Modified-Since` request header field, `false` otherwise.
61 62 63 |
# File 'lib/raptor-io/protocol/http/response.rb', line 61 def modified? code != 304 end |
#redirect? ⇒ Boolean
Returns ‘true` if the response is a `3xx` redirect and there is a `Location` header field.
52 53 54 |
# File 'lib/raptor-io/protocol/http/response.rb', line 52 def redirect? code >= 300 && code <= 399 && !!headers['Location'] end |
#text? ⇒ Bool
Returns ‘true` if the response body is textual in nature, `false` otherwise (if binary).
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/raptor-io/protocol/http/response.rb', line 68 def text? return if !@body if (type = headers['content-type']) return true if type.start_with?( 'text/' ) # Non "application/" content types will surely not be text-based # so bail out early. return false if !type.start_with?( 'application/' ) end # Last resort, more resource intensive binary detection. !@body.binary? end |
#to_s ⇒ String
Returns String representation of the response.
85 86 87 88 89 90 91 92 93 |
# File 'lib/raptor-io/protocol/http/response.rb', line 85 def to_s headers['Content-Length'] = body.to_s.size r = "HTTP/#{version} #{code}" r << " #{}" if r << "\r\n" r << "#{headers.to_s}\r\n\r\n" r << body.to_s end |