Class: AjpRailsResponse

Inherits:
ActionController::AbstractResponse
  • Object
show all
Defined in:
lib/ajp-rails/rails-wrapper.rb

Instance Method Summary collapse

Instance Method Details

#to_ajp_response(extra_cookies = nil) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/ajp-rails/rails-wrapper.rb', line 331

def to_ajp_response(extra_cookies = nil)
  raise "Unrecognized status line #{headers['Status']}" unless
    md = /\A(\d{3})(?: (.+))?/.match(headers['Status'])

  res = md[2] ?
    Net::AJP13::Response.new(md[1].to_i, :reason_phrase => md[2]) :
    Net::AJP13::Response.new(md[1])

  @headers.each do |key, value|
    case key.downcase
    when 'status'
      # do nothing
    when 'expires'
      res.add_field(key, CGI::rfc1123_date(options.delete(key)))
    when 'cookie'
      case value
      when String
        res.add_field('Set-Cookie', value)
      when CGI::Cookie
        res.add_field('Set-Cookie', value.to_s)
      when Array
        value.each {|cookie| res.add_field('Set-Cookie', cookie) }
      when Hash
        value.each_value {|cookie| res.add_field('Set-Cookie', cookie) }
      end
    else
      res.add_field(key, value.to_s) if key != 'Status'
    end
  end
  res['content-type'] ||= 'text/html'
  if extra_cookies
    extra_cookies.each do |cookie|
      res.add_field('Set-Cookie', cookie.to_s)
    end
  end
  
  if @body.respond_to?(:call)
    buf = ''
    @body.call(self, StringIO.new(buf))
    res.body = buf
  else
    res.body = self.body
  end

  return res
end