Module: Plugin::ResponseHelpers

Extended by:
Stats
Defined in:
lib/unicorn-cuba-base/plugin/response_helpers.rb

Instance Method Summary collapse

Methods included from Stats

def_stats, stats

Instance Method Details

#write(code, content_type, body, headers = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# 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_epilogueObject



85
86
87
# 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 = {}) ⇒ Object



40
41
42
43
44
45
# 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 = {}) ⇒ Object



78
79
80
81
82
83
# 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 = {}) ⇒ Object



47
48
49
# 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 = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# 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 = {}) ⇒ Object



32
33
34
# 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 = {}) ⇒ Object



74
75
76
# 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 = {}) ⇒ Object

Multipart



52
53
54
55
56
57
58
59
60
# 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 = {}) ⇒ Object



27
28
29
30
# 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 = {}) ⇒ Object



36
37
38
# 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