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

Constructor Details

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

Set up a few things specific to HTTP responses



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

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)



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

def headers
  @headers
end

#statusObject

The HTTP status code



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

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)


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

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.



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

def content_type
	return self.headers[ :content_type ]
end

#content_type=(type) ⇒ Object

Set the current response Content-Type.



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

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.



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

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)


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

def handled?
	return ! @status.nil?
end

#header_dataObject

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



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

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.



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

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)


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

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.



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

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



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

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

	self.set_defaults

	return true
end

#set_defaultsObject

Set up response default headers, etc.



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

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

#status_categoryObject

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



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

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)


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

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)


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

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)


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

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)


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

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)


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

def status_is_successful?
	return self.status_category == 2
end

#status_lineObject

Send the response status to the client



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

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



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

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