Class: FTW::Response

Inherits:
Object
  • Object
show all
Includes:
HTTP::Message, Protocol
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 CRLF

CRLF::CRLF

Constants included from HTTP::Message

HTTP::Message::VALID_VERSIONS

Instance Attribute Summary collapse

Attributes included from HTTP::Message

#headers, #version

Instance Method Summary collapse

Methods included from Protocol

#discard_body, #encode_chunked, #read_body, #read_http_body, #read_http_body_chunked, #read_http_body_length, #read_http_message, #write_all, #write_http_body, #write_http_body_chunked, #write_http_body_normal

Methods included from HTTP::Message

#body, #body=, #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

#reasonObject (readonly)

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



20
21
22
# File 'lib/ftw/response.rb', line 20

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>



16
17
18
# File 'lib/ftw/response.rb', line 16

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

#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)


94
95
96
97
98
# File 'lib/ftw/response.rb', line 94

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