Class: Fluent::ExHttpInput::ExHandler

Inherits:
Handler
  • Object
show all
Defined in:
lib/fluent/plugin/in_http_ex.rb

Instance Method Summary collapse

Instance Method Details

#check_content_type(params, content_type, body, path_info) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/fluent/plugin/in_http_ex.rb', line 225

def check_content_type(params, content_type, body, path_info)
  path = path_info[1..-1] # remove /
  resource, _ = path.split('/')
  case resource
  when /^js$/
    params["resource"] = :js
  when /^ms$/
    params["resource"] = :ms
  end

  if content_type =~ /^application\/x-www-form-urlencoded/
    params.update WEBrick::HTTPUtils.parse_query(body)
  elsif content_type =~ /^multipart\/form-data; boundary=(.+)/
    boundary = WEBrick::HTTPUtils.dequote($1)
    params.update WEBrick::HTTPUtils.parse_form_data(body, boundary)
  elsif content_type =~ /^application\/(json|x-msgpack)/
    params[$1] = body
  end

  params
rescue => ex
  $log.error ex
  $log.error ex.backtrace * "\n"
end

#on_body(chunk) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fluent/plugin/in_http_ex.rb', line 165

def on_body(chunk)
  if @chunked && @content_type =~ /application\/x-msgpack/i
    m = method(:on_read_msgpack)
    @u = MessagePack::Unpacker.new
    (class << self; self; end).module_eval do
      define_method(:on_body, m)
    end
    m.call(chunk)
  else
    if @body.bytesize + chunk.bytesize > @body_size_limit
      unless closing?
        send_response_and_close("413 Request Entity Too Large", {}, "Too large")
      end
      return
    end
    @body << chunk
  end
end

#on_closeObject



117
118
119
120
# File 'lib/fluent/plugin/in_http_ex.rb', line 117

def on_close
  $log.debug "close #{@remote_addr}:#{@remote_port}"
  super
end

#on_headers_complete(headers) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
# File 'lib/fluent/plugin/in_http_ex.rb', line 122

def on_headers_complete(headers)
  expect = nil
  size = nil
  if @parser.http_version == [1, 1]
    @keep_alive = true
  else
    @keep_alive = false
  end
  @env = {}
  headers.each_pair {|k,v|
    @env["HTTP_#{k.gsub('-','_').upcase}"] = v
    case k
    when /Expect/i
      expect = v
    when /Content-Length/i
      size = v.to_i
    when /Content-Type/i
      @content_type = v
    when /Connection/i
      if v =~ /close/i
        @keep_alive = false
      elsif v =~ /Keep-alive/i
        @keep_alive = true
      end
    when /Transfer-Encoding/i
      if v =~ /chunked/i
        @chunked = true
      end
    end
  }
  if expect
    if expect == '100-continue'
      if !size || size < @body_size_limit
        send_response_nobody("100 Continue", {})
      else
        send_response_and_close("413 Request Entity Too Large", {}, "Too large")
      end
    else
      send_response_and_close("417 Expectation Failed", {}, "")
    end
  end
end

#on_message_completeObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/fluent/plugin/in_http_ex.rb', line 198

def on_message_complete
  return if closing?

  @env['REMOTE_ADDR'] = @remote_addr

  params = WEBrick::HTTPUtils.parse_query(@parser.query_string)
  path_info = @parser.request_path

  params = check_content_type(params, @content_type, @body, path_info)
  params.merge!(@env)
  @env.clear

  unless @chunked
    code, header, body = *@callback.call(path_info, params)
    body = body.to_s

    if @keep_alive
      header['Connection'] = 'Keep-Alive'
      send_response(code, header, body)
    else
      send_response_and_close(code, header, body)
    end
  else
    send_response("200 OK", {'Content-type'=>'text/plain', 'Connection'=>'Keep-Alive'}, "")
  end
end

#on_read_msgpack(data) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/fluent/plugin/in_http_ex.rb', line 184

def on_read_msgpack(data)
  params = WEBrick::HTTPUtils.parse_query(@parser.query_string)
  path_info = @parser.request_path
  @u.feed_each(data) do |obj|
    params["chunked"] = obj
    params["REMOTE_ADDR"] = @remote_addr
    @callback.call(path_info, params)
  end
rescue
  $log.error "on_read_msgpack error: #{$!.to_s}"
  $log.error_backtrace
  close
end