module Plugin::ResponseHelpers

Public Instance Methods

write(code, content_type, body, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 16
def write(code, content_type, body, headers = {})
        req.body.read # read all remaining upload before we send response so that client will read it
        res.status = code
        res["Content-Type"] = content_type
        headers.each do |key, value|
                res[key] = value
        end
        ResponseHelpers.stats.incr_total_write
        res.write body
end
write_epilogue() click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 85
def write_epilogue
        res.write "--#{@boundary}--\r\n"
end
write_error(code, error, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 40
def write_error(code, error, headers = {})
        msg = error.message
        log.warn "sending #{code} error response: #{msg}"
        ResponseHelpers.stats.incr_total_write_error
        write_plain code, msg, headers
end
write_error_part(code, error, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 78
def write_error_part(code, error, headers = {})
        msg = error.message
        log.warn "sending error in multipart response part: #{msg}"
        ResponseHelpers.stats.incr_total_write_error_part
        write_plain_part msg, headers.merge('Status' => code)
end
write_json(code, obj, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 47
def write_json(code, obj, headers = {})
        write code, 'application/json', obj.to_json, headers
end
write_part(content_type, body, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 62
def write_part(content_type, body, headers = {})
        res.write "--#{@boundary}\r\n"
        res.write "Content-Type: #{content_type}\r\n"
        headers.each_pair do |name, value|
                res.write "#{name}: #{value}\r\n"
        end
        res.write "\r\n"
        ResponseHelpers.stats.incr_total_write_part
        res.write body
        res.write "\r\n"
end
write_plain(code, msg, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 32
def write_plain(code, msg, headers = {})
        write_text code, 'text/plain', msg, headers
end
write_plain_part(msg, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 74
def write_plain_part(msg, headers = {})
        write_part 'text/plain', msg.to_s.gsub("\n", "\r\n"), headers
end
write_preamble(code, headers = {}) click to toggle source

Multipart

# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 52
def write_preamble(code, headers = {})
        res.status = code
        @boundary = SecureRandom.uuid
        res["Content-Type"] = "multipart/mixed; boundary=\"#{@boundary}\""
        headers.each do |key, value|
                res[key] = value
        end
        ResponseHelpers.stats.incr_total_write_multipart
end
write_text(code, content_type, msg, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 27
def write_text(code, content_type, msg, headers = {})
        msg = msg.join("\r\n") if msg.is_a? Array
        write code, content_type, (msg.gsub(/(?<!\r)\n/, "\r\n") + "\r\n"), headers
end
write_url_list(code, msg, headers = {}) click to toggle source
# File lib/unicorn-cuba-base/plugin/response_helpers.rb, line 36
def write_url_list(code, msg, headers = {})
        write_text code, 'text/uri-list', msg, headers
end