Parent

Class/Module Index [+]

Quicksearch

Fluent::HttpInput::Handler

Public Class Methods

new(io, km, callback, body_size_limit) click to toggle source
# 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

Public Instance Methods

closing?() click to toggle source
# File lib/fluent/plugin/in_http.rb, line 269
def closing?
  @next_close
end
on_body(chunk) click to toggle source
# 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_close() click to toggle source
# File lib/fluent/plugin/in_http.rb, line 159
def on_close
  @km.delete(self)
end
on_connect() click to toggle source
# File lib/fluent/plugin/in_http.rb, line 163
def on_connect
  @parser = Http::Parser.new(self)
end
on_headers_complete(headers) click to toggle source
# 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/
      expect = v
    when /Content-Length/
      size = v.to_i
    when /Content-Type/
      @content_type = v
    when /Connection/
      if v =~ /close/
        @keep_alive = false
      elsif v =~ /Keep-alive/
        @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_begin() click to toggle source
# File lib/fluent/plugin/in_http.rb, line 176
def on_message_begin
  @body = ''
end
on_message_complete() click to toggle source
# 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) click to toggle source
# 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_complete() click to toggle source
# File lib/fluent/plugin/in_http.rb, line 260
def on_write_complete
  close if @next_close
end
send_response(code, header, body) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_idle() click to toggle source
# File lib/fluent/plugin/in_http.rb, line 155
def step_idle
  @idle += 1
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.