Class: Fluent::Plugin::HttpInput::Handler

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

Instance Method Summary collapse

Constructor Details

#initialize(io, km, callback, body_size_limit, format_name, log, cors_allow_origins, cors_allow_credentials, add_query_params, splunk_token) ⇒ Handler

Returns a new instance of Handler.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fluent/plugin/in_http_splunk_hec.rb', line 53

def initialize(io, km, callback, body_size_limit, format_name, log,
               cors_allow_origins, cors_allow_credentials, add_query_params, splunk_token)
  @io = io
  @km = km
  @callback = callback
  @body_size_limit = body_size_limit
  @next_close = false
  @format_name = format_name
  @log = log
  @cors_allow_origins = cors_allow_origins
  @cors_allow_credentials = cors_allow_credentials
  @idle = 0
  @add_query_params = add_query_params
  @splunk_token = splunk_token
  @km.add(self)

  @remote_port, @remote_addr = io.remote_port, io.remote_addr
  @parser = Http::Parser.new(self)
end

Instance Method Details

#on_headers_complete(headers) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fluent/plugin/in_http_splunk_hec.rb', line 73

def on_headers_complete(headers)
  expect = nil
  size = nil
  authorization = nil

  if @parser.http_version == [1, 1]
    @keep_alive = true
  else
    @keep_alive = false
  end
  @env = {}
  @content_type = ""
  @content_encoding = ""
  headers.each_pair {|k,v|
    @env["HTTP_#{k.gsub('-','_').upcase}"] = v
    case k
    when /\AExpect\z/i
      expect = v
    when /\AContent-Length\Z/i
      size = v.to_i
    when /\AContent-Type\Z/i
      @content_type = v
    when /\AContent-Encoding\Z/i
      @content_encoding = v
    when /\AConnection\Z/i
      if v =~ /close/i
        @keep_alive = false
      elsif v =~ /Keep-alive/i
        @keep_alive = true
      end
    when /\AOrigin\Z/i
      @origin  = v
    when /\AX-Forwarded-For\Z/i
      # For multiple X-Forwarded-For headers. Use first header value.
      v = v.first if v.is_a?(Array)
      @remote_addr = v.split(",").first
    when /\AAccess-Control-Request-Method\Z/i
      @access_control_request_method = v
    when /\AAccess-Control-Request-Headers\Z/i
      @access_control_request_headers = v
    when /\AAuthorization\Z/i
      # For Splunk Authrozation header verification only.
      authorization = v
    end
  }
  if expect
    if expect == '100-continue'.freeze
      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
  if authorization
    # For Splunk Authrozation header verification only.
    if "Splunk #{@splunk_token}" != authorization
      send_response_and_close("403 Forbidden", {}, "Authorization Failed")
    end
  end
end