123456789_123456789_123456789_123456789_123456789_

Class: WEBrick::HTTPResponse

Relationships & Source Files
Namespace Children
Exceptions:
Inherits: Object
Defined in: lib/webrick/httpresponse.rb

Overview

An HTTP response. This is filled in by the service or do_* methods of a ::WEBrick HTTP Servlet.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(config) ⇒ HTTPResponse

Creates a new HTTP response object. Config::HTTP is the default configuration.

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 97

def initialize(config)
  @config = config
  @buffer_size = config[:OutputBufferSize]
  @logger = config[:Logger]
  @header = Hash.new
  @status = HTTPStatus::RC_OK
  @reason_phrase = nil
  @http_version = HTTPVersion::convert(@config[:HTTPVersion])
  @body = ''
  @keep_alive = true
  @cookies = []
  @request_method = nil
  @request_uri = nil
  @request_http_version = @http_version  # temporary
  @chunked = false
  @filename = nil
  @sent_size = 0
end

Instance Attribute Details

#body (rw)

Body may be a String or IO-like object that responds to #read and #readpartial.

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 55

attr_accessor :body

#chunked=(val) (rw)

Enables chunked transfer encoding.

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 192

def chunked=(val)
  @chunked = val ? true : false
end

#chunked?Boolean (rw)

Will this response body be returned using chunked transfer-encoding?

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 185

def chunked?
  @chunked
end

#config (readonly)

Configuration for this response

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 86

attr_reader :config

#content_length (rw)

The content-length header

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 148

def content_length
  if len = self['content-length']
    return Integer(len)
  end
end

#content_length=(len) (rw)

Sets the content-length header to len

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 157

def content_length=(len)
  self['content-length'] = len.to_s
end

#content_type (rw)

The content-type header

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 164

def content_type
  self['content-type']
end

#content_type=(type) (rw)

Sets the content-type header to type

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 171

def content_type=(type)
  self['content-type'] = type
end

#cookies (readonly)

Response cookies

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 44

attr_reader :cookies

#filename (rw)

Filename of the static file in this response. Only used by the FileHandler servlet.

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 76

attr_accessor :filename

#header (readonly)

Response header

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 39

attr_reader :header

#http_version (readonly)

HTTP Response version

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 29

attr_reader :http_version

#keep_alive (rw)

Is this a keep-alive response?

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 81

attr_accessor :keep_alive

#keep_alive?Boolean (rw)

Will this response's connection be kept alive?

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 199

def keep_alive?
  @keep_alive
end

#reason_phrase (rw)

Response reason phrase (“OK”)

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 49

attr_accessor :reason_phrase

#request_http_version (rw)

Request HTTP version for this response

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 70

attr_accessor :request_http_version

#request_method (rw)

Request method for this response

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 60

attr_accessor :request_method

#request_uri (rw)

Request URI for this response

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 65

attr_accessor :request_uri

#sent_size (readonly)

Bytes sent in this response

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 91

attr_reader :sent_size

#status (rw)

Response status code (200)

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 34

attr_reader :status

#status=(status) (rw)

Sets the response's status to the #status code

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 126

def status=(status)
  @status = status
  @reason_phrase = HTTPStatus::reason_phrase(status)
end

Instance Method Details

#[](field)

Retrieves the response header field

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 134

def [](field)
  @header[field.downcase]
end

#[]=(field, value)

Sets the response header field to value

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 141

def []=(field, value)
  @header[field.downcase] = value.to_s
end

#check_header(header_value) (private)

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 368

def check_header(header_value)
  if header_value =~ /\r\n/
    raise InvalidHeader
  else
    header_value
  end
end

#each

Iterates over each header in the response

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 178

def each
  @header.each{|field, value|  yield(field, value) }
end

#set_error(ex, backtrace = false)

Creates an error page for exception ex with an optional backtrace

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 341

def set_error(ex, backtrace=false)
  case ex
  when HTTPStatus::Status
    @keep_alive = false if HTTPStatus::error?(ex.code)
    self.status = ex.code
  else
    @keep_alive = false
    self.status = HTTPStatus::RC_INTERNAL_SERVER_ERROR
  end
  @header['content-type'] = "text/html; charset=ISO-8859-1"

  if respond_to?(:create_error_page)
    create_error_page()
    return
  end

  if @request_uri
    host, port = @request_uri.host, @request_uri.port
  else
    host, port = @config[:ServerName], @config[:Port]
  end

  error_body(backtrace, ex, host, port)
end

#set_redirect(status, url)

Redirects to url with a HTTPStatus::Redirect #status.

Example:

res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect
[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 332

def set_redirect(status, url)
  @body = "<HTML><A HREF=\"#{url}\">#{url}</A>.</HTML>\n"
  @header['location'] = url.to_s
  raise status
end

#status_line

The response's HTTP status line

[ GitHub ]

  
# File 'lib/webrick/httpresponse.rb', line 119

def status_line
  "HTTP/#@http_version #@status #@reason_phrase #{CRLF}"
end