Class: Waitress::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/waitress/response.rb

Overview

The response class is used to cook responses to be served to the client. This class contains things like response headers, status codes and the response body itself

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



7
8
9
10
11
12
# File 'lib/waitress/response.rb', line 7

def initialize
  @headers = {}
  status 200
  default_headers
  @isdone = false
end

Instance Method Details

#append(obj) ⇒ Object

Append something to the Body IO. If the Body IO is a StringIO, this will usually be a String. This is mostly used for the ‘echo’ function



71
72
73
# File 'lib/waitress/response.rb', line 71

def append obj
  @io.write obj
end

#body(str) ⇒ Object

Set the body to be a String. This will replace the BodyIO with a StringIO containing the string

str

The new string to replace the BodyIO with



78
79
80
# File 'lib/waitress/response.rb', line 78

def body str
  body_io StringIO.new(str)
end

#body_io(io = :get) ⇒ Object

Set the Body IO object for the response. This IO object will be read from when the webpage is served, so usually this is a File reference or a StringIO

io

The io object to use. Not required if you just want to get the IO object



64
65
66
67
# File 'lib/waitress/response.rb', line 64

def body_io io=:get
  @io = io unless io == :get
  @io
end

#default_headersObject

Apply the default headers to this response



25
26
27
# File 'lib/waitress/response.rb', line 25

def default_headers
  header "Server", "Waitress #{Waitress::VERSION} (#{RUBY_PLATFORM})"
end

#done(state = true) ⇒ Object

Mark the response as done (already sent to the client)



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

def done state=true
  @isdone = state
end

#done?Boolean

Returns true if the response has already been sent to the client

Returns:

  • (Boolean)


15
16
17
# File 'lib/waitress/response.rb', line 15

def done?
  @isdone
end

#header(header, data) ⇒ Object

Set a header for the response. This header will be encoded to the http response Params:

header

The name of the header. e.g. “Content-Type”

data

The data to be encoded into the header. e.g. “text/html”



57
58
59
# File 'lib/waitress/response.rb', line 57

def header header, data
  @headers[header] = data
end

#mime(filext) ⇒ Object

Set the mimetype (Content-Type header) of this response to the one matching the given file extension as matched by the Waitress::Util class

filext

The file extension to match, e.g. .html, .css, .js



40
41
42
43
# File 'lib/waitress/response.rb', line 40

def mime filext
  m = Waitress::Util.mime filext
  header "Content-Type", m
end

#mime_raw(type) ⇒ Object

Set the mimetype (Content-Type header) of this response to the one given.

type

The mime type to use, e.g. application/json, text/html,

application/scon



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

def mime_raw type
  header "Content-Type", type
end

#serve(sock) ⇒ Object

Serve the response to the given socket. This will write the Headers, Response Code and Body.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/waitress/response.rb', line 84

def serve sock
  unless done?
    sock.write "HTTP/1.1 #{@status} #{@status_msg}\r\n"
    @headers.each do |k, v|
      sock.write "#{k}: #{v}\r\n"
    end
    sock.write "\r\n"
    unless @io.nil?
      @io.pos = 0
      until @io.eof?
        s = @io.read(4096)
        sock.write s
      end
    end
    done
    sock.close rescue nil
    @io.close rescue nil
  end
end

#status(status_code) ⇒ Object

Apply the given Status code to the response, such as 200, 404, 500 or any other code listed in the HTTP protocol specification



31
32
33
34
35
# File 'lib/waitress/response.rb', line 31

def status status_code
  @status = status_code
  @status_msg = Waitress::Util.status @status
  header "Status", "#{@status} #{@status_msg}"
end