Class: Fluent::HttpInput::Handler

Inherits:
Coolio::Socket
  • Object
show all
Defined in:
lib/fluent/plugin/in_http.rb

Instance Method Summary collapse

Constructor Details

#initialize(io, km, callback, body_size_limit) ⇒ Handler

Returns a new instance of Handler.



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fluent/plugin/in_http.rb', line 141

def initialize(io, km, callback, body_size_limit)
  super(io)
  @km = km
  @callback = callback
  @body_size_limit = body_size_limit
  @content_type = ""
  @next_close = false

  @idle = 0
  @km.add(self)

  @remote_port, @remote_addr = *Socket.unpack_sockaddr_in(io.getpeername)
end

Instance Method Details

#closing?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/fluent/plugin/in_http.rb', line 269

def closing?
  @next_close
end

#on_body(chunk) ⇒ Object



219
220
221
222
223
224
225
226
227
# File 'lib/fluent/plugin/in_http.rb', line 219

def on_body(chunk)
  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

#on_closeObject



159
160
161
# File 'lib/fluent/plugin/in_http.rb', line 159

def on_close
  @km.delete(self)
end

#on_connectObject



163
164
165
# File 'lib/fluent/plugin/in_http.rb', line 163

def on_connect
  @parser = Http::Parser.new(self)
end

#on_headers_complete(headers) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fluent/plugin/in_http.rb', line 180

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
    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_beginObject



176
177
178
# File 'lib/fluent/plugin/in_http.rb', line 176

def on_message_begin
  @body = ''
end

#on_message_completeObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/fluent/plugin/in_http.rb', line 229

def on_message_complete
  return if closing?

  @env['REMOTE_ADDR'] = @remote_addr

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

  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/
    params['json'] = @body
  end
  path_info = @parser.request_path

  params.merge!(@env)
  @env.clear

  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
end

#on_read(data) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/fluent/plugin/in_http.rb', line 167

def on_read(data)
  @idle = 0
  @parser << data
rescue
  $log.warn "unexpected error", :error=>$!.to_s
  $log.warn_backtrace
  close
end

#on_write_completeObject



260
261
262
# File 'lib/fluent/plugin/in_http.rb', line 260

def on_write_complete
  close if @next_close
end

#send_response(code, header, body) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/fluent/plugin/in_http.rb', line 273

def send_response(code, header, body)
  header['Content-length'] ||= body.bytesize
  header['Content-type'] ||= 'text/plain'

  data = %[HTTP/1.1 #{code}\r\n]
  header.each_pair {|k,v|
    data << "#{k}: #{v}\r\n"
  }
  data << "\r\n"
  write data

  write body
end

#send_response_and_close(code, header, body) ⇒ Object



264
265
266
267
# File 'lib/fluent/plugin/in_http.rb', line 264

def send_response_and_close(code, header, body)
  send_response(code, header, body)
  @next_close = true
end

#send_response_nobody(code, header) ⇒ Object



287
288
289
290
291
292
293
294
# File 'lib/fluent/plugin/in_http.rb', line 287

def send_response_nobody(code, header)
  data = %[HTTP/1.1 #{code}\r\n]
  header.each_pair {|k,v|
    data << "#{k}: #{v}\r\n"
  }
  data << "\r\n"
  write data
end

#step_idleObject



155
156
157
# File 'lib/fluent/plugin/in_http.rb', line 155

def step_idle
  @idle += 1
end