Class: WEBrick::HTTPResponse (Ruby 2.2.2)
Description: Class : WEBrick::HTTPResponse - Ruby 2.2.2

- Home
- Core 2.2.2
- Std-lib 2.2.2
- Downloads

Support for the Ruby 2.2 series has ended. See here for reference.

### Home Classes Methods

### In Files

- webrick/httpresponse.rb

### Parent

Object

### Methods

- ::new
- #[]
- #[]=
- #chunked=
- #chunked?
- #content_length
- #content_length=
- #content_type
- #content_type=
- #each
- #keep_alive?
- #set_error
- #set_redirect
- #status=
- #status_line

### Class/Module Index

Quicksearch

- Dir
- Errno
- Errno::ECONNABORTED
- Errno::ECONNRESET
- Errno::EPROTO
- File
- Object
- WEBrick
- WEBrick::AccessLog
- WEBrick::AccessLog::AccessLogError
- WEBrick::BasicLog
- WEBrick::CGI
- WEBrick::Config
- WEBrick::Config::OpenSSL
- WEBrick::Cookie
- WEBrick::Daemon
- WEBrick::GenericServer
- WEBrick::HTMLUtils
- WEBrick::HTTPAuth
- WEBrick::HTTPAuth::Authenticator
- WEBrick::HTTPAuth::BasicAuth
- WEBrick::HTTPAuth::DigestAuth
- WEBrick::HTTPAuth::Htdigest
- WEBrick::HTTPAuth::Htgroup
- WEBrick::HTTPAuth::Htpasswd
- WEBrick::HTTPAuth::ProxyAuthenticator
- WEBrick::HTTPAuth::ProxyBasicAuth
- WEBrick::HTTPAuth::ProxyDigestAuth
- WEBrick::HTTPAuth::UserDB
- WEBrick::HTTPProxyServer
- WEBrick::HTTPRequest
- WEBrick::HTTPResponse
- WEBrick::HTTPServer
- WEBrick::HTTPServerError
- WEBrick::HTTPServlet
- WEBrick::HTTPServlet::AbstractServlet
- WEBrick::HTTPServlet::CGIHandler
- WEBrick::HTTPServlet::DefaultFileHandler
- WEBrick::HTTPServlet::ERBHandler
- WEBrick::HTTPServlet::FileHandler
- WEBrick::HTTPServlet::HTTPServletError
- WEBrick::HTTPServlet::ProcHandler
- WEBrick::HTTPStatus
- WEBrick::HTTPStatus::ClientError
- WEBrick::HTTPStatus::EOFError
- WEBrick::HTTPStatus::Error
- WEBrick::HTTPStatus::Info
- WEBrick::HTTPStatus::Redirect
- WEBrick::HTTPStatus::ServerError
- WEBrick::HTTPStatus::Status
- WEBrick::HTTPStatus::Success
- WEBrick::HTTPUtils
- WEBrick::HTTPUtils::FormData
- WEBrick::HTTPVersion
- WEBrick::Log
- WEBrick::ServerError
- WEBrick::SimpleServer
- WEBrick::Utils
- WEBrick::Utils::TimeoutHandler
- WEBrick::Utils::TimeoutHandler::Thread

No matching classes.

# WEBrick::HTTPResponse

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

### Attributes

body[RW]

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

config[R]

Configuration for this response

cookies[R]

Response cookies

filename[RW]

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

header[R]

Response header

http_version[R]

HTTP Response version

keep_alive[RW]

Is this a keep-alive response?

reason_phrase[RW]

Response reason phrase (“OK”)

request_http_version[RW]

Request HTTP version for this response

request_method[RW]

Request method for this response

request_uri[RW]

Request URI for this response

sent_size[R]

Bytes sent in this response

status[R]

Response status code (200)

### Public Class Methods

new(config) click to toggle source

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

                   # File webrick/httpresponse.rb, line 95
    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

### Public Instance Methods

[](field) click to toggle source

Retrieves the response header `field`

                   # File webrick/httpresponse.rb, line 132
    def [](field)
      @header[field.downcase]
    end

[]=(field, value) click to toggle source

Sets the response header `field` to `value`

                   # File webrick/httpresponse.rb, line 139
    def []=(field, value)
      @header[field.downcase] = value.to_s
    end

chunked=(val) click to toggle source

Enables chunked transfer encoding.

                   # File webrick/httpresponse.rb, line 190
    def chunked=(val)
      @chunked = val ? true : false
    end

chunked?() click to toggle source

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

                   # File webrick/httpresponse.rb, line 183
    def chunked?
      @chunked
    end

content_length() click to toggle source

The content-length header

                   # File webrick/httpresponse.rb, line 146
    def content_length
      if len = self['content-length']
        return Integer(len)
      end
    end

content_length=(len) click to toggle source

Sets the content-length header to `len`

                   # File webrick/httpresponse.rb, line 155
    def content_length=(len)
      self['content-length'] = len.to_s
    end

content_type() click to toggle source

The content-type header

                   # File webrick/httpresponse.rb, line 162
    def content_type
      self['content-type']
    end

content_type=(type) click to toggle source

Sets the content-type header to `type`

                   # File webrick/httpresponse.rb, line 169
    def content_type=(type)
      self['content-type'] = type
    end

each() click to toggle source

Iterates over each header in the response

                   # File webrick/httpresponse.rb, line 176
    def each
      @header.each{|field, value|  yield(field, value) }
    end

keep_alive?() click to toggle source

Will this response's connection be kept alive?

                   # File webrick/httpresponse.rb, line 197
    def keep_alive?
      @keep_alive
    end

set_error(ex, backtrace=false) click to toggle source

Creates an error page for exception `ex` with an optional `backtrace`

                   # File webrick/httpresponse.rb, line 332
        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

          @body = ''
          @body << <<-_end_of_html_
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
    <HTML>
      <HEAD><TITLE>#{HTMLUtils::escape(@reason_phrase)}</TITLE></HEAD>
      <BODY>
        <H1>#{HTMLUtils::escape(@reason_phrase)}</H1>
        #{HTMLUtils::escape(ex.message)}
        <HR>
          _end_of_html_

          if backtrace && $DEBUG
            @body << "backtrace of `#{HTMLUtils::escape(ex.class.to_s)}' "
            @body << "#{HTMLUtils::escape(ex.message)}"
            @body << "<PRE>"
            ex.backtrace.each{|line| @body << "\t#{line}\n"}
            @body << "</PRE><HR>"
          end

          @body << <<-_end_of_html_
        <ADDRESS>
         #{HTMLUtils::escape(@config[:ServerSoftware])} at
         #{host}:#{port}
        </ADDRESS>
      </BODY>
    </HTML>
          _end_of_html_
        end

set_redirect(status, url) click to toggle source

Redirects to `url` with a WEBrick::HTTPStatus::Redirect `status`.

Example:

    res.set_redirect WEBrick::HTTPStatus::TemporaryRedirect

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

status=(status) click to toggle source

Sets the response's status to the `status` code

                   # File webrick/httpresponse.rb, line 124
    def status=(status)
      @status = status
      @reason_phrase = HTTPStatus::reason_phrase(status)
    end

status_line() click to toggle source

The response's HTTP status line

                   # File webrick/httpresponse.rb, line 117
    def status_line
      "HTTP/#@http_version #@status #@reason_phrase #{CRLF}"
    end