Class: FTW::Response

Inherits:
Object
  • Object
show all
Includes:
HTTP::Message
Defined in:
lib/ftw/response.rb

Overview

An HTTP Response.

See RFC2616 section 6: <tools.ietf.org/html/rfc2616#section-6>

Constant Summary collapse

STATUS_REASON_MAP =

Translated from the recommendations listed in RFC2616 section 6.1.1 See RFC2616 section 6.1.1: <tools.ietf.org/html/rfc2616#section-6.1.1>

{
  100 => "Continue",
  101 => "Switching Protocols",
  200 => "OK",
  201 => "Created",
  202 => "Accepted",
  203 => "Non-Authoritative Information",
  204 => "No Content",
  205 => "Reset Content",
  206 => "Partial Content",
  300 => "Multiple Choices",
  301 => "Moved Permanently",
  302 => "Found",
  303 => "See Other",
  304 => "Not Modified",
  305 => "Use Proxy",
  307 => "Temporary Redirect",
  400 => "Bad Request",
  401 => "Unauthorized",
  402 => "Payment Required",
  403 => "Forbidden",
  404 => "Not Found",
  405 => "Method Not Allowed",
  406 => "Not Acceptable"
}

Constants included from HTTP::Message

HTTP::Message::VALID_VERSIONS

Constants included from CRLF

CRLF::CRLF

Instance Attribute Summary collapse

Attributes included from HTTP::Message

#headers, #version

Instance Method Summary collapse

Methods included from HTTP::Message

#body?, #content?, #to_s

Constructor Details

#initializeResponse

Create a new Response.



53
54
55
56
57
# File 'lib/ftw/response.rb', line 53

def initialize
  super
  @logger = Cabin::Channel.get
  @reason = "" # Empty reason string by default. It is not required.
end

Instance Attribute Details

#bodyObject

STATUS_REASON_MAP



48
49
50
# File 'lib/ftw/response.rb', line 48

def body
  @body
end

#reasonObject (readonly)

The reason phrase (RFC2616 6.1.1) See RFC2616 section 6.1.1: <tools.ietf.org/html/rfc2616#section-6.1.1>



18
19
20
# File 'lib/ftw/response.rb', line 18

def reason
  @reason
end

#statusObject

The http status code (RFC2616 6.1.1) See RFC2616 section 6.1.1: <tools.ietf.org/html/rfc2616#section-6.1.1>



14
15
16
# File 'lib/ftw/response.rb', line 14

def status
  @status
end

Instance Method Details

#error?Boolean

Is this response an error?

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/ftw/response.rb', line 66

def error?
  # 4xx and 5xx are errors
  return @status >= 400 && @status < 600
end

#read_body(&block) ⇒ Object

Read the body of this Response. The block is called with chunks of the response as they are read in.

This method is generally only called by http clients, not servers.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ftw/response.rb', line 97

def read_body(&block)
  if @body.respond_to?(:read)
    if headers.include?("Content-Length") and headers["Content-Length"].to_i > 0
      @logger.debug("Reading body with Content-Length")
      read_body_length(headers["Content-Length"].to_i, &block)
    elsif headers["Transfer-Encoding"] == "chunked"
      @logger.debug("Reading body with chunked encoding")
      read_body_chunked(&block)
    end

    # If this is a poolable resource, release it (like a FTW::Connection)
    @body.release if @body.respond_to?(:release)
  elsif !@body.nil?
    yield @body
  end
end

#redirect?Boolean

Is this response a redirect?

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/ftw/response.rb', line 60

def redirect?
  # redirects are 3xx
  return @status >= 300 && @status < 400
end

#status_lineObject Also known as: start_line

Get the status-line string, like “HTTP/1.0 200 OK”



83
84
85
86
87
88
# File 'lib/ftw/response.rb', line 83

def status_line
  # First line is 'Status-Line' from RFC2616 section 6.1
  # Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
  # etc...
  return "HTTP/#{version} #{status} #{reason}"
end

#upgrade?Boolean

Is this Response the result of a successful Upgrade request?

Returns:

  • (Boolean)


154
155
156
157
158
# File 'lib/ftw/response.rb', line 154

def upgrade?
  return false unless status == 101 # "Switching Protocols"
  return false unless headers["Connection"] == "Upgrade"
  return true
end