Class: Mongrel2::HTTPResponse

Inherits:
Response
  • Object
show all
Extended by:
Loggability
Includes:
Constants
Defined in:
lib/mongrel2/httpresponse.rb

Overview

The Mongrel2 HTTP Response class.

Direct Known Subclasses

WebSocket::ServerHandshake

Constant Summary collapse

STATUS_LINE_FORMAT =

The format for building valid HTTP responses

"HTTP/1.1 %03d %s".freeze
EOL =

A network End-Of-Line

"\r\n".freeze
DEFAULT_CONTENT_TYPE =

The default content type

'application/octet-stream'.freeze

Constants included from Constants

Constants::DATA_DIR, Constants::DEFAULT_CONFIG_SCRIPT, Constants::DEFAULT_CONFIG_URI, Constants::DEFAULT_CONTROL_SOCKET, Constants::MAX_BROADCAST_IDENTS

Constants inherited from Response

Response::DEFAULT_CHUNKSIZE

Instance Attribute Summary collapse

Attributes inherited from Response

#body, #chunksize, #conn_id, #extended_reply_data, #extended_reply_filter, #request, #sender_id

Instance Method Summary collapse

Methods inherited from Response

#<<, #each_chunk, #extend_reply_with, #extended_reply?, from_request, #inspect, #puts, #socket_id

Constructor Details

#initialize(sender_id, conn_id, body = '', headers = {}) ⇒ HTTPResponse

Set up a few things specific to HTTP responses

[View source]

31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongrel2/httpresponse.rb', line 31

def initialize( sender_id, conn_id, body='', headers={} ) # :notnew:
	if body.is_a?( Hash )
		headers = body
		body = ''
	end

	super( sender_id, conn_id, body )

	@headers = Mongrel2::Table.new
	@status = nil
	self.set_defaults

	@headers.merge!( headers )
end

Instance Attribute Details

#headersObject (readonly) Also known as: header

The response headers (a Mongrel2::Table)


52
53
54
# File 'lib/mongrel2/httpresponse.rb', line 52

def headers
  @headers
end

#statusObject

The HTTP status code


56
57
58
# File 'lib/mongrel2/httpresponse.rb', line 56

def status
  @status
end

Instance Method Details

#bodiless?Boolean

Returns true if the response status means the response shouldn’t have a body.

Returns:

  • (Boolean)
[View source]

91
92
93
94
95
# File 'lib/mongrel2/httpresponse.rb', line 91

def bodiless?
	return self.extended_reply? ||
		self.body.nil? ||
		HTTP::BODILESS_HTTP_RESPONSE_CODES.include?( self.status )
end

#content_typeObject

Return the current response Content-Type.

[View source]

136
137
138
# File 'lib/mongrel2/httpresponse.rb', line 136

def content_type
	return self.headers[ :content_type ]
end

#content_type=(type) ⇒ Object

Set the current response Content-Type.

[View source]

142
143
144
# File 'lib/mongrel2/httpresponse.rb', line 142

def content_type=( type )
	return self.headers[ :content_type ] = type
end

#get_content_lengthObject

Get the length of the body IO. If the IO’s offset is somewhere other than the beginning or end, the size of the remainder is used.

[View source]

187
188
189
190
191
192
193
194
195
196
197
# File 'lib/mongrel2/httpresponse.rb', line 187

def get_content_length
	return 0 if self.bodiless?

	if self.body.pos.nonzero? && !self.body.eof?
		self.log.info "Calculating content length based on an offset of %d" % [ self.body.pos ]
		return self.body.size - self.body.pos
	else
		self.log.debug "Calculating body size via %p" % [ self.body.method(:size) ]
		return self.body.size
	end
end

#handled?Boolean Also known as: is_handled?

Returns true if the response is ready to be sent to the client.

Returns:

  • (Boolean)
[View source]

83
84
85
# File 'lib/mongrel2/httpresponse.rb', line 83

def handled?
	return ! @status.nil?
end

#header_dataObject

Return the current response header as a valid HTTP string after normalizing them.

[View source]

161
162
163
# File 'lib/mongrel2/httpresponse.rb', line 161

def header_data
	return self.normalized_headers.to_s
end

#keepalive=(value) ⇒ Object Also known as: pipelining_enabled=

Set the Connection header to allow pipelined HTTP.

[View source]

201
202
203
# File 'lib/mongrel2/httpresponse.rb', line 201

def keepalive=( value )
	self.headers[:connection] = value ? 'keep-alive' : 'close'
end

#keepalive?Boolean Also known as: pipelining_enabled?

Returns true if the response has pipelining enabled.

Returns:

  • (Boolean)
[View source]

208
209
210
211
212
# File 'lib/mongrel2/httpresponse.rb', line 208

def keepalive?
	ka_header = self.headers[:connection]
	return !ka_header.nil? && ka_header =~ /keep-alive/i
	return false
end

#normalized_headersObject

Get a copy of the response headers table with any auto-generated or calulated headers set.

[View source]

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/mongrel2/httpresponse.rb', line 168

def normalized_headers
	headers = self.headers.dup

	headers[:date] ||= Time.now.httpdate

	if self.bodiless? && !self.extended_reply?
		headers.delete( :content_length )
		headers.delete( :content_type )
	else
		headers[:content_length] ||= self.get_content_length
		headers[:content_type]   ||= DEFAULT_CONTENT_TYPE.dup
	end

	return headers
end

#resetObject

Clear any existing headers and body and restore them to their defaults

[View source]

148
149
150
151
152
153
154
155
156
# File 'lib/mongrel2/httpresponse.rb', line 148

def reset
	@headers.clear
	@body.truncate( 0 )
	@status = nil

	self.set_defaults

	return true
end

#set_defaultsObject

Set up response default headers, etc.

[View source]

60
61
62
# File 'lib/mongrel2/httpresponse.rb', line 60

def set_defaults
	@headers[:server] = Mongrel2.version_string( true )
end

#status_categoryObject

Return the numeric category of the response’s status code (1-5)

[View source]

99
100
101
102
# File 'lib/mongrel2/httpresponse.rb', line 99

def status_category
	return 0 if self.status.nil?
	return (self.status / 100).ceil
end

#status_is_clienterror?Boolean

Return true if response is in the 4XX range

Returns:

  • (Boolean)
[View source]

124
125
126
# File 'lib/mongrel2/httpresponse.rb', line 124

def status_is_clienterror?
	return self.status_category == 4
end

#status_is_informational?Boolean

Return true if response is in the 1XX range

Returns:

  • (Boolean)
[View source]

106
107
108
# File 'lib/mongrel2/httpresponse.rb', line 106

def status_is_informational?
	return self.status_category == 1
end

#status_is_redirect?Boolean

Return true if response is in the 3XX range

Returns:

  • (Boolean)
[View source]

118
119
120
# File 'lib/mongrel2/httpresponse.rb', line 118

def status_is_redirect?
	return self.status_category == 3
end

#status_is_servererror?Boolean

Return true if response is in the 5XX range

Returns:

  • (Boolean)
[View source]

130
131
132
# File 'lib/mongrel2/httpresponse.rb', line 130

def status_is_servererror?
	return self.status_category == 5
end

#status_is_successful?Boolean

Return true if response is in the 2XX range

Returns:

  • (Boolean)
[View source]

112
113
114
# File 'lib/mongrel2/httpresponse.rb', line 112

def status_is_successful?
	return self.status_category == 2
end

#status_lineObject

Send the response status to the client

[View source]

76
77
78
79
# File 'lib/mongrel2/httpresponse.rb', line 76

def status_line
	st = self.status || self.derived_status_code
	return STATUS_LINE_FORMAT % [ st, HTTP::STATUS_NAME[st] ]
end

#to_sObject

Stringify the response

[View source]

66
67
68
69
70
71
72
# File 'lib/mongrel2/httpresponse.rb', line 66

def to_s
	return [
		self.status_line,
		self.header_data,
		self.bodiless? ? '' : super
	].join( "\r\n" )
end