Class: Bijou::HttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/bijou/httpresponse.rb

Constant Summary collapse

ContentType =
'Content-Type'
SetCookie =
'Set-Cookie'
Location =
'Location'
DefaultContentType =
'text/html'
DefaultCharset =
'iso-8859-1'
CR =
"\015"
LF =
"\012"
EOL =
CR + LF
@@status_map =
{
  200 => "200 OK",
  206 => "206 Partial Content",
  300 => "300 Multiple Choices",
  301 => "301 Moved Permanently",
  302 => "302 Found",
  304 => "304 Not Modified",
  400 => "400 Bad Request",
  401 => "401 Authorization Required",
  403 => "403 Forbidden",
  404 => "404 Not Found",
  405 => "405 Method Not Allowed",
  406 => "406 Not Acceptable",
  411 => "411 Length Required",
  412 => "412 Precondition Failed",
  500 => "500 Internal Server Error",
  501 => "501 Method Not Implemented",
  502 => "502 Bad Gateway",
  506 => "506 Variant Also Negotiates",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttpResponse

Returns a new instance of HttpResponse.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bijou/httpresponse.rb', line 33

def initialize
  @buffer_output = true
  @content_type = DefaultContentType
  @charset = DefaultCharset
  @headers = {}
  @error_headers = {}
  @cookies = []
  @output = ''
  @log = ''
  @suppress_headers = false
  @suppress_content = false

  @status = 200
  update_content_type_header
end

Instance Attribute Details

#buffer_outputObject

Returns the value of attribute buffer_output.



21
22
23
# File 'lib/bijou/httpresponse.rb', line 21

def buffer_output
  @buffer_output
end

#statusObject

Returns the value of attribute status.



31
32
33
# File 'lib/bijou/httpresponse.rb', line 31

def status
  @status
end

#suppress_contentObject

A flag indicating whether to suppress content rendering to the client.



29
30
31
# File 'lib/bijou/httpresponse.rb', line 29

def suppress_content
  @suppress_content
end

#suppress_headersObject

Some environments generate their own headers. Headers should be suppressed in such cases. Note, however that response header attributes, such as content_type, will have no effect.



26
27
28
# File 'lib/bijou/httpresponse.rb', line 26

def suppress_headers
  @suppress_headers
end

Instance Method Details



107
108
109
# File 'lib/bijou/httpresponse.rb', line 107

def append_cookie(cookie)
  @cookies.push(cookie)
end

#charsetObject



103
104
105
# File 'lib/bijou/httpresponse.rb', line 103

def charset
  @charset
end

#charset=(str) ⇒ Object



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

def charset=(str)
  @charset = str
  update_content_type_header
end

#clearObject



80
81
82
83
# File 'lib/bijou/httpresponse.rb', line 80

def clear
  clear_headers
  clear_content
end

#clear_contentObject



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

def clear_content
  @output = ''
end

#clear_header(header) ⇒ Object

Removes a header having the specified name. Case is significant.



132
133
134
135
136
137
# File 'lib/bijou/httpresponse.rb', line 132

def clear_header(header)
  if header.downcase == ContentType.downcase
    @content_type = nil
  end
  @headers.delete(header)
end

#clear_headersObject



70
71
72
73
74
# File 'lib/bijou/httpresponse.rb', line 70

def clear_headers
  @headers = {}
  @content_type = nil
  @charset = nil
end

#content_typeObject

Gets the HTTP MIME type of the response. The default is "text/html"



94
95
96
# File 'lib/bijou/httpresponse.rb', line 94

def content_type
  @content_type
end

#content_type=(str) ⇒ Object

Sets the HTTP MIME type of the response. The default is "text/html"



87
88
89
90
# File 'lib/bijou/httpresponse.rb', line 87

def content_type=(str)
  @content_type = str
  update_content_type_header
end

#end_requestObject

Called to terminate a request before the render cycle is complete. This is useful for redirecting from init or render handlers, for example.

Raises:



181
182
183
# File 'lib/bijou/httpresponse.rb', line 181

def end_request
  raise EndRequest
end

#get_header(header) ⇒ Object

Returns the specified header value. Case is significant so, for example Content-type is not valid, whereas Content-Type is valid.



127
128
129
# File 'lib/bijou/httpresponse.rb', line 127

def get_header(header)
  @headers[header]
end

#get_logObject



223
224
225
# File 'lib/bijou/httpresponse.rb', line 223

def get_log
  @log
end

#html_encode(string) ⇒ Object



207
208
209
# File 'lib/bijou/httpresponse.rb', line 207

def html_encode(string)
  ::CGI::escapeHTML(string)
end

#location(value) ⇒ Object



185
186
187
# File 'lib/bijou/httpresponse.rb', line 185

def location(value)
  set_header("Location", value)
end

#log(text) ⇒ Object



219
220
221
# File 'lib/bijou/httpresponse.rb', line 219

def log(text)
  @log << text
end

#redirect(value, status = 302) ⇒ Object



189
190
191
192
193
194
# File 'lib/bijou/httpresponse.rb', line 189

def redirect(value, status = 302)
  @status = status
  @suppress_content = true
  location(value)
  end_request
end

#renderObject



196
197
198
199
200
201
# File 'lib/bijou/httpresponse.rb', line 196

def render
  result = ''
  result << render_headers unless @suppress_headers
  result << @output
  result
end

#render_headersObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/bijou/httpresponse.rb', line 139

def render_headers
  result = '';

  if @@status_map.has_key? @status
    status = @@status_map[@status]
  else
    status = @@status_map[200]
  end

  # status line, e.g.: "Status: 200 OK\r\n"
  result = "Status: " + status + EOL

  if @status < 300
    headers = @headers
  else
    headers = @error_headers
  end

  headers.each { |key,value|
    if value && !value.empty?
      #result << "#{key}: #{value}" + EOL
      result << "#{key}: #{value}\n"
    end
  }
  if @cookies
    @cookies.each {|cookie|
      result << "Set-Cookie: " + cookie.to_s + EOL
    }
  end

  # BUGBUG: The output contains \n and after print stdio translates to
  # \r\n, which increases length. We can't use content-length until fixed.
  #result << "Content-Length: #{@output.length}\n"

  result << EOL
  
  #print "Content-type: text/plain; charset=iso-8859-1\n\n"
  result
end

#set_header(header, value) ⇒ Object

Sets the value of the specified header. Case is significant so, for example Content-type is not valid, whereas Content-Type is valid.



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bijou/httpresponse.rb', line 113

def set_header(header, value)
  if header.downcase == ContentType.downcase
    set_header_content_type(value)
  elsif header.downcase == Location.downcase
    @error_headers[header] = value
  elsif header.downcase == SetCookie.downcase
    # We include cookies for 302 Found (redirect)
    @error_headers[header] = value
  end
  @headers[header] = value
end

#url_encode(string) ⇒ Object



203
204
205
# File 'lib/bijou/httpresponse.rb', line 203

def url_encode(string)
  ::CGI::escape(string)
end

#write(str) ⇒ Object



211
212
213
# File 'lib/bijou/httpresponse.rb', line 211

def write(str)
  @output << str
end

#writeline(str) ⇒ Object



215
216
217
# File 'lib/bijou/httpresponse.rb', line 215

def writeline(str)
  @output << str + "\n"
end