Class: Fluent::HttpInput::Handler
- Inherits:
-
Coolio::Socket
- Object
- Coolio::Socket
- Fluent::HttpInput::Handler
- Defined in:
- lib/fluent/plugin/in_http.rb
Instance Attribute Summary collapse
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
Instance Method Summary collapse
- #closing? ⇒ Boolean
-
#initialize(io, km, callback, body_size_limit, format, log, cors_allow_origins) ⇒ Handler
constructor
A new instance of Handler.
- #on_body(chunk) ⇒ Object
- #on_close ⇒ Object
- #on_connect ⇒ Object
- #on_headers_complete(headers) ⇒ Object
- #on_message_begin ⇒ Object
- #on_message_complete ⇒ Object
- #on_read(data) ⇒ Object
- #on_write_complete ⇒ Object
- #send_response(code, header, body) ⇒ Object
- #send_response_and_close(code, header, body) ⇒ Object
- #send_response_nobody(code, header) ⇒ Object
- #step_idle ⇒ Object
Constructor Details
#initialize(io, km, callback, body_size_limit, format, log, cors_allow_origins) ⇒ Handler
Returns a new instance of Handler.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/fluent/plugin/in_http.rb', line 244 def initialize(io, km, callback, body_size_limit, format, log, cors_allow_origins) super(io) @km = km @callback = callback @body_size_limit = body_size_limit @next_close = false @format = format @log = log @cors_allow_origins = cors_allow_origins @idle = 0 @km.add(self) @remote_port, @remote_addr = *Socket.unpack_sockaddr_in(io.getpeername) rescue nil end |
Instance Attribute Details
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
242 243 244 |
# File 'lib/fluent/plugin/in_http.rb', line 242 def content_type @content_type end |
Instance Method Details
#closing? ⇒ Boolean
394 395 396 |
# File 'lib/fluent/plugin/in_http.rb', line 394 def closing? @next_close end |
#on_body(chunk) ⇒ Object
329 330 331 332 333 334 335 336 337 |
# File 'lib/fluent/plugin/in_http.rb', line 329 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 ⇒ Object
263 264 265 |
# File 'lib/fluent/plugin/in_http.rb', line 263 def on_close @km.delete(self) end |
#on_connect ⇒ Object
267 268 269 |
# File 'lib/fluent/plugin/in_http.rb', line 267 def on_connect @parser = Http::Parser.new(self) end |
#on_headers_complete(headers) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/fluent/plugin/in_http.rb', line 284 def on_headers_complete(headers) expect = nil size = nil if @parser.http_version == [1, 1] @keep_alive = true else @keep_alive = false end @env = {} @content_type = "" 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 /Origin/i @origin = v when /X-Forwarded-For/i @remote_addr = v.split(",").first 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 ⇒ Object
280 281 282 |
# File 'lib/fluent/plugin/in_http.rb', line 280 def @body = '' end |
#on_message_complete ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/fluent/plugin/in_http.rb', line 339 def return if closing? # CORS check # ========== # For every incoming request, we check if we have some CORS # restrictions and white listed origins through @cors_allow_origins. unless @cors_allow_origins.nil? unless @cors_allow_origins.include?(@origin) send_response_and_close("403 Forbidden", {'Connection' => 'close'}, "") return end end @env['REMOTE_ADDR'] = @remote_addr if @remote_addr uri = URI.parse(@parser.request_url) params = WEBrick::HTTPUtils.parse_query(uri.query) if @format != 'default' params[EVENT_RECORD_PARAMETER] = @body elsif @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 = uri.path params.merge!(@env) @env.clear code, header, body = *@callback.call(path_info, params) body = body.to_s header['Access-Control-Allow-Origin'] = @origin if !@cors_allow_origins.nil? && @cors_allow_origins.include?(@origin) 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
271 272 273 274 275 276 277 278 |
# File 'lib/fluent/plugin/in_http.rb', line 271 def on_read(data) @idle = 0 @parser << data rescue @log.warn "unexpected error", error: $!.to_s @log.warn_backtrace close end |
#on_write_complete ⇒ Object
385 386 387 |
# File 'lib/fluent/plugin/in_http.rb', line 385 def on_write_complete close if @next_close end |
#send_response(code, header, body) ⇒ Object
398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/fluent/plugin/in_http.rb', line 398 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
389 390 391 392 |
# File 'lib/fluent/plugin/in_http.rb', line 389 def send_response_and_close(code, header, body) send_response(code, header, body) @next_close = true end |
#send_response_nobody(code, header) ⇒ Object
412 413 414 415 416 417 418 419 |
# File 'lib/fluent/plugin/in_http.rb', line 412 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 ⇒ Object
259 260 261 |
# File 'lib/fluent/plugin/in_http.rb', line 259 def step_idle @idle += 1 end |