fluentd 1.9.3 → 1.11.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fluentd might be problematic. Click here for more details.

Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/issue-auto-closer.yml +12 -0
  3. data/CHANGELOG.md +182 -0
  4. data/CONTRIBUTING.md +1 -1
  5. data/README.md +4 -0
  6. data/docs/SECURITY_AUDIT.pdf +0 -0
  7. data/lib/fluent/command/debug.rb +1 -0
  8. data/lib/fluent/command/fluentd.rb +25 -1
  9. data/lib/fluent/config.rb +1 -0
  10. data/lib/fluent/daemonizer.rb +88 -0
  11. data/lib/fluent/log.rb +45 -6
  12. data/lib/fluent/match.rb +1 -1
  13. data/lib/fluent/msgpack_factory.rb +13 -6
  14. data/lib/fluent/plugin/buffer.rb +2 -2
  15. data/lib/fluent/plugin/in_dummy.rb +3 -3
  16. data/lib/fluent/plugin/in_forward.rb +2 -2
  17. data/lib/fluent/plugin/in_gc_stat.rb +16 -0
  18. data/lib/fluent/plugin/in_http.rb +146 -75
  19. data/lib/fluent/plugin/in_monitor_agent.rb +1 -1
  20. data/lib/fluent/plugin/in_syslog.rb +4 -4
  21. data/lib/fluent/plugin/in_tail.rb +40 -31
  22. data/lib/fluent/plugin/in_tail/position_file.rb +23 -6
  23. data/lib/fluent/plugin/in_unix.rb +77 -77
  24. data/lib/fluent/plugin/out_copy.rb +1 -1
  25. data/lib/fluent/plugin/out_file.rb +1 -1
  26. data/lib/fluent/plugin/out_forward.rb +25 -22
  27. data/lib/fluent/plugin/out_forward/handshake_protocol.rb +4 -0
  28. data/lib/fluent/plugin/out_forward/load_balancer.rb +1 -1
  29. data/lib/fluent/plugin/out_http.rb +15 -2
  30. data/lib/fluent/plugin/parser_multiline.rb +1 -1
  31. data/lib/fluent/plugin/parser_syslog.rb +303 -62
  32. data/lib/fluent/plugin/sd_file.rb +1 -0
  33. data/lib/fluent/plugin/sd_srv.rb +135 -0
  34. data/lib/fluent/plugin_helper/cert_option.rb +15 -2
  35. data/lib/fluent/plugin_helper/child_process.rb +3 -2
  36. data/lib/fluent/plugin_helper/record_accessor.rb +14 -0
  37. data/lib/fluent/plugin_helper/server.rb +3 -1
  38. data/lib/fluent/plugin_helper/service_discovery.rb +7 -0
  39. data/lib/fluent/plugin_helper/service_discovery/manager.rb +8 -0
  40. data/lib/fluent/plugin_helper/socket.rb +20 -2
  41. data/lib/fluent/plugin_helper/socket_option.rb +21 -3
  42. data/lib/fluent/supervisor.rb +21 -9
  43. data/lib/fluent/system_config.rb +2 -1
  44. data/lib/fluent/test/filter_test.rb +2 -2
  45. data/lib/fluent/test/output_test.rb +3 -3
  46. data/lib/fluent/version.rb +1 -1
  47. data/test/command/test_fluentd.rb +71 -12
  48. data/test/config/test_system_config.rb +2 -0
  49. data/test/helper.rb +2 -2
  50. data/test/plugin/in_tail/test_fifo.rb +121 -0
  51. data/test/plugin/in_tail/test_io_handler.rb +132 -0
  52. data/test/plugin/in_tail/test_position_file.rb +25 -1
  53. data/test/plugin/out_forward/test_handshake_protocol.rb +10 -1
  54. data/test/plugin/out_forward/test_load_balancer.rb +46 -0
  55. data/test/plugin/test_buf_file.rb +3 -1
  56. data/test/plugin/test_buffer.rb +20 -0
  57. data/test/plugin/test_compressable.rb +7 -4
  58. data/test/plugin/test_in_dummy.rb +12 -14
  59. data/test/plugin/test_in_forward.rb +2 -2
  60. data/test/plugin/test_in_gc_stat.rb +24 -1
  61. data/test/plugin/test_in_http.rb +57 -0
  62. data/test/plugin/test_in_syslog.rb +16 -1
  63. data/test/plugin/test_in_tail.rb +43 -20
  64. data/test/plugin/test_in_unix.rb +128 -73
  65. data/test/plugin/test_out_forward.rb +39 -3
  66. data/test/plugin/test_out_http.rb +38 -0
  67. data/test/plugin/test_out_null.rb +1 -1
  68. data/test/plugin/test_output_as_buffered_retries.rb +12 -4
  69. data/test/plugin/test_output_as_buffered_secondary.rb +9 -1
  70. data/test/plugin/test_parser_syslog.rb +106 -46
  71. data/test/plugin/test_sd_file.rb +17 -0
  72. data/test/plugin/test_sd_srv.rb +230 -0
  73. data/test/plugin_helper/data/cert/cert-with-CRLF.pem +19 -0
  74. data/test/plugin_helper/data/cert/cert_chains/ca-cert-key.pem +27 -0
  75. data/test/plugin_helper/data/cert/cert_chains/ca-cert.pem +20 -0
  76. data/test/plugin_helper/data/cert/cert_chains/cert-key.pem +27 -0
  77. data/test/plugin_helper/data/cert/cert_chains/cert.pem +40 -0
  78. data/test/plugin_helper/data/cert/generate_cert.rb +38 -0
  79. data/test/plugin_helper/http_server/test_app.rb +1 -1
  80. data/test/plugin_helper/http_server/test_route.rb +1 -1
  81. data/test/plugin_helper/test_cert_option.rb +2 -0
  82. data/test/plugin_helper/test_child_process.rb +20 -3
  83. data/test/plugin_helper/test_http_server_helper.rb +2 -2
  84. data/test/plugin_helper/test_record_accessor.rb +41 -0
  85. data/test/plugin_helper/test_server.rb +1 -1
  86. data/test/plugin_helper/test_service_discovery.rb +37 -4
  87. data/test/plugin_helper/test_socket.rb +131 -0
  88. data/test/test_daemonizer.rb +91 -0
  89. data/test/test_log.rb +44 -0
  90. data/test/test_msgpack_factory.rb +18 -0
  91. metadata +28 -2
@@ -125,7 +125,7 @@ module Fluent
125
125
  end
126
126
 
127
127
  def match(str)
128
- @regex.match(str) != nil
128
+ @regex.match?(str)
129
129
  end
130
130
  end
131
131
 
@@ -23,25 +23,32 @@ module Fluent
23
23
 
24
24
  module Mixin
25
25
  def msgpack_factory
26
- if $log
27
- $log.warn('Deprecated method: this method is going to be deleted. Use Fluent::MessagePackFactory.engine_factory')
26
+ unless @deprecated_log_done
27
+ deprecated_log('Deprecated method: this method is going to be deleted. Use Fluent::MessagePackFactory.engine_factory')
28
28
  end
29
29
  MessagePackFactory.engine_factory
30
30
  end
31
31
 
32
32
  def msgpack_packer(*args)
33
- if $log
34
- $log.warn('Deprecated method: this method is going to be deleted. Use Fluent::MessagePackFactory.msgpack_packer')
33
+ unless @deprecated_log_done
34
+ deprecated_log('Deprecated method: this method is going to be deleted. Use Fluent::MessagePackFactory.msgpack_packer')
35
35
  end
36
36
  MessagePackFactory.msgpack_packer(*args)
37
37
  end
38
38
 
39
39
  def msgpack_unpacker(*args)
40
- if $log
41
- $log.warn('Deprecated method: this method is going to be deleted. Use Fluent::MessagePackFactory.msgpack_unpacker')
40
+ unless @deprecated_log_done
41
+ deprecated_log('Deprecated method: this method is going to be deleted. Use Fluent::MessagePackFactory.msgpack_unpacker')
42
42
  end
43
43
  MessagePackFactory.msgpack_unpacker(*args)
44
44
  end
45
+
46
+ def deprecated_log(str)
47
+ if $log
48
+ $log.warn(str)
49
+ @deprecated_log_done = true
50
+ end
51
+ end
45
52
  end
46
53
 
47
54
  def self.engine_factory(enable_time_support: false)
@@ -758,13 +758,13 @@ module Fluent
758
758
 
759
759
  def statistics
760
760
  stage_size, queue_size = @stage_size, @queue_size
761
- buffer_space = 1.0 - ((stage_size + queue_size * 1.0) / @total_limit_size).round
761
+ buffer_space = 1.0 - ((stage_size + queue_size * 1.0) / @total_limit_size)
762
762
  stats = {
763
763
  'stage_length' => @stage.size,
764
764
  'stage_byte_size' => stage_size,
765
765
  'queue_length' => @queue.size,
766
766
  'queue_byte_size' => queue_size,
767
- 'available_buffer_space_ratios' => buffer_space * 100,
767
+ 'available_buffer_space_ratios' => (buffer_space * 100).round(1),
768
768
  'total_queued_size' => stage_size + queue_size,
769
769
  }
770
770
 
@@ -37,7 +37,7 @@ module Fluent::Plugin
37
37
  desc "If specified, each generated event has an auto-incremented key field."
38
38
  config_param :auto_increment_key, :string, default: nil
39
39
  desc "The boolean to suspend-and-resume incremental value after restart"
40
- config_param :suspend, :bool, default: false
40
+ config_param :suspend, :bool, default: false,deprecated: 'This parameters is ignored'
41
41
  desc "The dummy data to be generated. An array of JSON hashes or a single JSON hash."
42
42
  config_param :dummy, default: [{"message"=>"dummy"}] do |val|
43
43
  begin
@@ -105,10 +105,10 @@ module Fluent::Plugin
105
105
  begin
106
106
  if @size > 1
107
107
  num.times do
108
- router.emit_array(@tag, Array.new(@size) { [Fluent::Engine.now, generate] })
108
+ router.emit_array(@tag, Array.new(@size) { [Fluent::EventTime.now, generate] })
109
109
  end
110
110
  else
111
- num.times { router.emit(@tag, Fluent::Engine.now, generate) }
111
+ num.times { router.emit(@tag, Fluent::EventTime.now, generate) }
112
112
  end
113
113
  rescue => _
114
114
  # ignore all errors not to stop emits by emit errors
@@ -327,7 +327,7 @@ module Fluent::Plugin
327
327
  record = e[1]
328
328
  next if record.nil?
329
329
  time = e[0]
330
- time = Fluent::Engine.now if time.nil? || time.to_i == 0 # `to_i == 0` for empty EventTime
330
+ time = Fluent::EventTime.now if time.nil? || time.to_i == 0 # `to_i == 0` for empty EventTime
331
331
  es.add(time, record)
332
332
  }
333
333
  es
@@ -347,7 +347,7 @@ module Fluent::Plugin
347
347
  return msg[3] # retry never succeeded so return ack and drop incoming event.
348
348
  end
349
349
  return if record.nil?
350
- time = Fluent::Engine.now if time.to_i == 0
350
+ time = Fluent::EventTime.now if time.to_i == 0
351
351
  if @enable_field_injection
352
352
  record[@source_address_key] = conn.remote_addr if @source_address_key
353
353
  record[@source_hostname_key] = conn.remote_host if @source_hostname_key
@@ -24,13 +24,22 @@ module Fluent::Plugin
24
24
 
25
25
  def initialize
26
26
  super
27
+ @key_map = nil
27
28
  end
28
29
 
29
30
  config_param :emit_interval, :time, default: 60
31
+ config_param :use_symbol_keys, :bool, default: true
30
32
  config_param :tag, :string
31
33
 
32
34
  def configure(conf)
33
35
  super
36
+
37
+ unless @use_symbol_keys
38
+ @key_map = {}
39
+ GC.stat.each_key { |key|
40
+ @key_map[key] = key.to_s
41
+ }
42
+ end
34
43
  end
35
44
 
36
45
  def multi_workers_ready?
@@ -50,6 +59,13 @@ module Fluent::Plugin
50
59
  def on_timer
51
60
  now = Fluent::EventTime.now
52
61
  record = GC.stat
62
+ unless @use_symbol_keys
63
+ new_record = {}
64
+ record.each_pair { |k, v|
65
+ new_record[@key_map[k]] = v
66
+ }
67
+ record = new_record
68
+ end
53
69
  router.emit(@tag, now, record)
54
70
  end
55
71
  end
@@ -27,10 +27,28 @@ require 'json'
27
27
  module Fluent::Plugin
28
28
  class InHttpParser < Parser
29
29
  Fluent::Plugin.register_parser('in_http', self)
30
+
31
+ config_set_default :time_key, 'time'
32
+
33
+ def configure(conf)
34
+ super
35
+
36
+ # if no time parser related parameters, use in_http's time convert rule
37
+ @time_parser = if conf.has_key?('time_type') || conf.has_key?('time_format')
38
+ time_parser_create
39
+ else
40
+ nil
41
+ end
42
+ end
43
+
30
44
  def parse(text)
31
45
  # this plugin is dummy implementation not to raise error
32
46
  yield nil, nil
33
47
  end
48
+
49
+ def get_time_parser
50
+ @time_parser
51
+ end
34
52
  end
35
53
 
36
54
  class HttpInput < Input
@@ -60,6 +78,8 @@ module Fluent::Plugin
60
78
  config_param :respond_with_empty_img, :bool, default: false
61
79
  desc 'Respond status code with 204.'
62
80
  config_param :use_204_response, :bool, default: false
81
+ desc 'Dump error log or not'
82
+ config_param :dump_error_log, :bool, default: true
63
83
 
64
84
  config_section :parse do
65
85
  config_set_default :@type, 'in_http'
@@ -67,6 +87,24 @@ module Fluent::Plugin
67
87
 
68
88
  EVENT_RECORD_PARAMETER = '_event_record'
69
89
 
90
+ def initialize
91
+ super
92
+
93
+ @km = nil
94
+ @format_name = nil
95
+ @parser_time_key = nil
96
+
97
+ # default parsers
98
+ @parser_msgpack = nil
99
+ @parser_json = nil
100
+ @default_time_parser = nil
101
+ @default_keep_time_key = nil
102
+ @float_time_parser = nil
103
+
104
+ # <parse> configured parser
105
+ @custom_parser = nil
106
+ end
107
+
70
108
  def configure(conf)
71
109
  compat_parameters_convert(conf, :parser)
72
110
 
@@ -74,20 +112,22 @@ module Fluent::Plugin
74
112
 
75
113
  m = if @parser_configs.first['@type'] == 'in_http'
76
114
  @parser_msgpack = parser_create(usage: 'parser_in_http_msgpack', type: 'msgpack')
115
+ @parser_msgpack.time_key = nil
77
116
  @parser_msgpack.estimate_current_event = false
78
117
  @parser_json = parser_create(usage: 'parser_in_http_json', type: 'json')
118
+ @parser_json.time_key = nil
79
119
  @parser_json.estimate_current_event = false
120
+
121
+ default_parser = parser_create(usage: '')
80
122
  @format_name = 'default'
81
- @parser_time_key = if parser_config = conf.elements('parse').first
82
- parser_config['time_key'] || 'time'
83
- else
84
- 'time'
85
- end
123
+ @parser_time_key = default_parser.time_key
124
+ @default_time_parser = default_parser.get_time_parser
125
+ @default_keep_time_key = default_parser.keep_time_key
86
126
  method(:parse_params_default)
87
127
  else
88
- @parser = parser_create
128
+ @custom_parser = parser_create
89
129
  @format_name = @parser_configs.first['@type']
90
- @parser_time_key = @parser.time_key
130
+ @parser_time_key = @custom_parser.time_key
91
131
  method(:parse_params_with_parser)
92
132
  end
93
133
  self.singleton_class.module_eval do
@@ -142,6 +182,13 @@ module Fluent::Plugin
142
182
  super
143
183
  end
144
184
 
185
+ RES_TEXT_HEADER = {'Content-Type' => 'text/plain'}.freeze
186
+ RESPONSE_200 = ["200 OK".freeze, RES_TEXT_HEADER, "".freeze].freeze
187
+ RESPONSE_204 = ["204 No Content".freeze, {}.freeze].freeze
188
+ RESPONSE_IMG = ["200 OK".freeze, {'Content-Type'=>'image/gif; charset=utf-8'}.freeze, EMPTY_GIF_IMAGE].freeze
189
+ RES_400_STATUS = "400 Bad Request".freeze
190
+ RES_500_STATUS = "500 Internal Server Error".freeze
191
+
145
192
  def on_request(path_info, params)
146
193
  begin
147
194
  path = path_info[1..-1] # remove /
@@ -152,83 +199,77 @@ module Fluent::Plugin
152
199
  if record.nil?
153
200
  log.debug { "incoming event is invalid: path=#{path_info} params=#{params.to_json}" }
154
201
  if @respond_with_empty_img
155
- return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE]
202
+ return RESPONSE_IMG
156
203
  else
157
204
  if @use_204_response
158
- return ["204 No Content", {}]
205
+ return RESPONSE_204
159
206
  else
160
- return ["200 OK", {'Content-Type'=>'text/plain'}, ""]
207
+ return RESPONSE_200
161
208
  end
162
209
  end
163
210
  end
164
211
 
165
- unless record.is_a?(Array)
166
- if @add_http_headers
167
- params.each_pair { |k,v|
168
- if k.start_with?("HTTP_")
169
- record[k] = v
170
- end
171
- }
172
- end
173
- if @add_remote_addr
174
- record['REMOTE_ADDR'] = params['REMOTE_ADDR']
175
- end
176
- end
177
- time = if param_time = params['time']
178
- param_time = param_time.to_f
179
- param_time.zero? ? Fluent::Engine.now : @float_time_parser.parse(param_time)
180
- else
181
- record_time.nil? ? Fluent::Engine.now : record_time
182
- end
183
- rescue
184
- return ["400 Bad Request", {'Content-Type'=>'text/plain'}, "400 Bad Request\n#{$!}\n"]
185
- end
186
-
187
- # TODO server error
188
- begin
212
+ mes = nil
189
213
  # Support batched requests
190
214
  if record.is_a?(Array)
191
215
  mes = Fluent::MultiEventStream.new
192
216
  record.each do |single_record|
193
- if @add_http_headers
194
- params.each_pair { |k,v|
195
- if k.start_with?("HTTP_")
196
- single_record[k] = v
197
- end
198
- }
199
- end
200
- if @add_remote_addr
201
- single_record['REMOTE_ADDR'] = params['REMOTE_ADDR']
202
- end
203
-
204
- if defined? @parser
205
- single_time = @parser.parse_time(single_record)
206
- single_time, single_record = @parser.convert_values(single_time, single_record)
217
+ add_params_to_record(single_record, params)
218
+
219
+ if param_time = params['time']
220
+ param_time = param_time.to_f
221
+ single_time = param_time.zero? ? Fluent::EventTime.now : @float_time_parser.parse(param_time)
222
+ elsif @custom_parser
223
+ single_time = @custom_parser.parse_time(single_record)
224
+ single_time, single_record = @custom_parser.convert_values(single_time, single_record)
207
225
  else
208
- single_time = if t = single_record.delete(@parser_time_key)
209
- Fluent::EventTime.from_time(Time.at(t))
210
- else
211
- time
212
- end
226
+ single_time = convert_time_field(single_record)
213
227
  end
214
228
 
215
229
  mes.add(single_time, single_record)
216
230
  end
231
+ else
232
+ add_params_to_record(record, params)
233
+
234
+ time = if param_time = params['time']
235
+ param_time = param_time.to_f
236
+ param_time.zero? ? Fluent::EventTime.now : @float_time_parser.parse(param_time)
237
+ else
238
+ if record_time.nil?
239
+ convert_time_field(record)
240
+ else
241
+ record_time
242
+ end
243
+ end
244
+ end
245
+ rescue => e
246
+ if @dump_error_log
247
+ log.error "failed to process request", error: e
248
+ end
249
+ return [RES_400_STATUS, RES_TEXT_HEADER, "400 Bad Request\n#{e}\n"]
250
+ end
251
+
252
+ # TODO server error
253
+ begin
254
+ if mes
217
255
  router.emit_stream(tag, mes)
218
256
  else
219
257
  router.emit(tag, time, record)
220
258
  end
221
- rescue
222
- return ["500 Internal Server Error", {'Content-Type'=>'text/plain'}, "500 Internal Server Error\n#{$!}\n"]
259
+ rescue => e
260
+ if @dump_error_log
261
+ log.error "failed to emit data", error: e
262
+ end
263
+ return [RES_500_STATUS, RES_TEXT_HEADER, "500 Internal Server Error\n#{e}\n"]
223
264
  end
224
265
 
225
266
  if @respond_with_empty_img
226
- return ["200 OK", {'Content-Type'=>'image/gif; charset=utf-8'}, EMPTY_GIF_IMAGE]
267
+ return RESPONSE_IMG
227
268
  else
228
269
  if @use_204_response
229
- return ["204 No Content", {}]
270
+ return RESPONSE_204
230
271
  else
231
- return ["200 OK", {'Content-Type'=>'text/plain'}, ""]
272
+ return RESPONSE_200
232
273
  end
233
274
  end
234
275
  end
@@ -267,7 +308,7 @@ module Fluent::Plugin
267
308
 
268
309
  def parse_params_with_parser(params)
269
310
  if content = params[EVENT_RECORD_PARAMETER]
270
- @parser.parse(content) { |time, record|
311
+ @custom_parser.parse(content) { |time, record|
271
312
  raise "Received event is not #{@format_name}: #{content}" if record.nil?
272
313
  return time, record
273
314
  }
@@ -276,6 +317,32 @@ module Fluent::Plugin
276
317
  end
277
318
  end
278
319
 
320
+ def add_params_to_record(record, params)
321
+ if @add_http_headers
322
+ params.each_pair { |k, v|
323
+ if k.start_with?("HTTP_".freeze)
324
+ record[k] = v
325
+ end
326
+ }
327
+ end
328
+
329
+ if @add_remote_addr
330
+ record['REMOTE_ADDR'] = params['REMOTE_ADDR']
331
+ end
332
+ end
333
+
334
+ def convert_time_field(record)
335
+ if t = @default_keep_time_key ? record[@parser_time_key] : record.delete(@parser_time_key)
336
+ if @default_time_parser
337
+ @default_time_parser.parse(t)
338
+ else
339
+ Fluent::EventTime.from_time(Time.at(t))
340
+ end
341
+ else
342
+ Fluent::EventTime.now
343
+ end
344
+ end
345
+
279
346
  class Handler
280
347
  attr_reader :content_type
281
348
 
@@ -358,7 +425,7 @@ module Fluent::Plugin
358
425
  end
359
426
  }
360
427
  if expect
361
- if expect == '100-continue'
428
+ if expect == '100-continue'.freeze
362
429
  if !size || size < @body_size_limit
363
430
  send_response_nobody("100 Continue", {})
364
431
  else
@@ -380,17 +447,20 @@ module Fluent::Plugin
380
447
  @body << chunk
381
448
  end
382
449
 
450
+ RES_200_STATUS = "200 OK".freeze
451
+ RES_403_STATUS = "403 Forbidden".freeze
452
+
383
453
  # Web browsers can send an OPTIONS request before performing POST
384
454
  # to check if cross-origin requests are supported.
385
455
  def handle_options_request
386
456
  # Is CORS enabled in the first place?
387
457
  if @cors_allow_origins.nil?
388
- return send_response_and_close("403 Forbidden", {}, "")
458
+ return send_response_and_close(RES_403_STATUS, {}, "")
389
459
  end
390
460
 
391
461
  # in_http does not support HTTP methods except POST
392
462
  if @access_control_request_method != 'POST'
393
- return send_response_and_close("403 Forbidden", {}, "")
463
+ return send_response_and_close(RES_403_STATUS, {}, "")
394
464
  end
395
465
 
396
466
  header = {
@@ -401,19 +471,19 @@ module Fluent::Plugin
401
471
  # Check the origin and send back a CORS response
402
472
  if @cors_allow_origins.include?('*')
403
473
  header["Access-Control-Allow-Origin"] = "*"
404
- send_response_and_close("200 OK", header, "")
474
+ send_response_and_close(RES_200_STATUS, header, "")
405
475
  elsif include_cors_allow_origin
406
476
  header["Access-Control-Allow-Origin"] = @origin
407
- send_response_and_close("200 OK", header, "")
477
+ send_response_and_close(RES_200_STATUS, header, "")
408
478
  else
409
- send_response_and_close("403 Forbidden", {}, "")
479
+ send_response_and_close(RES_403_STATUS, {}, "")
410
480
  end
411
481
  end
412
482
 
413
483
  def on_message_complete
414
484
  return if closing?
415
485
 
416
- if @parser.http_method == 'OPTIONS'
486
+ if @parser.http_method == 'OPTIONS'.freeze
417
487
  return handle_options_request()
418
488
  end
419
489
 
@@ -423,7 +493,7 @@ module Fluent::Plugin
423
493
  # restrictions and white listed origins through @cors_allow_origins.
424
494
  unless @cors_allow_origins.nil?
425
495
  unless @cors_allow_origins.include?('*') or include_cors_allow_origin
426
- send_response_and_close("403 Forbidden", {'Connection' => 'close'}, "")
496
+ send_response_and_close(RES_403_STATUS, {'Connection' => 'close'}, "")
427
497
  return
428
498
  end
429
499
  end
@@ -433,14 +503,14 @@ module Fluent::Plugin
433
503
  # Decode payload according to the "Content-Encoding" header.
434
504
  # For now, we only support 'gzip' and 'deflate'.
435
505
  begin
436
- if @content_encoding == 'gzip'
506
+ if @content_encoding == 'gzip'.freeze
437
507
  @body = Zlib::GzipReader.new(StringIO.new(@body)).read
438
- elsif @content_encoding == 'deflate'
508
+ elsif @content_encoding == 'deflate'.freeze
439
509
  @body = Zlib::Inflate.inflate(@body)
440
510
  end
441
511
  rescue
442
512
  @log.warn 'fails to decode payload', error: $!.to_s
443
- send_response_and_close("400 Bad Request", {}, "")
513
+ send_response_and_close(RES_400_STATUS, {}, "")
444
514
  return
445
515
  end
446
516
 
@@ -466,8 +536,9 @@ module Fluent::Plugin
466
536
  params.merge!(@env)
467
537
  @env.clear
468
538
 
469
- code, header, body = *@callback.call(path_info, params)
539
+ code, header, body = @callback.call(path_info, params)
470
540
  body = body.to_s
541
+ header = header.dup if header.frozen?
471
542
 
472
543
  unless @cors_allow_origins.nil?
473
544
  if @cors_allow_origins.include?('*')
@@ -478,7 +549,7 @@ module Fluent::Plugin
478
549
  end
479
550
 
480
551
  if @keep_alive
481
- header['Connection'] = 'Keep-Alive'
552
+ header['Connection'] = 'Keep-Alive'.freeze
482
553
  send_response(code, header, body)
483
554
  else
484
555
  send_response_and_close(code, header, body)
@@ -504,13 +575,13 @@ module Fluent::Plugin
504
575
 
505
576
  def send_response(code, header, body)
506
577
  header['Content-Length'] ||= body.bytesize
507
- header['Content-Type'] ||= 'text/plain'
578
+ header['Content-Type'] ||= 'text/plain'.freeze
508
579
 
509
580
  data = %[HTTP/1.1 #{code}\r\n]
510
581
  header.each_pair {|k,v|
511
582
  data << "#{k}: #{v}\r\n"
512
583
  }
513
- data << "\r\n"
584
+ data << "\r\n".freeze
514
585
  @io.write(data)
515
586
 
516
587
  @io.write(body)
@@ -521,7 +592,7 @@ module Fluent::Plugin
521
592
  header.each_pair {|k,v|
522
593
  data << "#{k}: #{v}\r\n"
523
594
  }
524
- data << "\r\n"
595
+ data << "\r\n".freeze
525
596
  @io.write(data)
526
597
  end
527
598