Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

flush codec after decode #81

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions lib/logstash/inputs/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class LogStash::Inputs::Http < LogStash::Inputs::Base
# mostly due to rack compliance
REJECTED_HEADERS = ["puma.socket", "rack.hijack?", "rack.hijack", "rack.url_scheme", "rack.after_reply", "rack.version", "rack.errors", "rack.multithread", "rack.multiprocess", "rack.run_once", "SCRIPT_NAME", "QUERY_STRING", "SERVER_PROTOCOL", "SERVER_SOFTWARE", "GATEWAY_INTERFACE"]

public
def register
require "logstash/util/http_compressed_requests"
@server = ::HTTPInputWebServer.new(nil) # we'll set the rack handler later
Expand Down Expand Up @@ -154,12 +153,12 @@ def run(queue)
end
begin
codec = local_codecs[req["content_type"]] || local_codecs[:default]
codec.decode(body.read) do |event|
event.set("host", remote_host)
event.set("headers", req)
decorate(event)
queue << event
end

codec.decode(body.read) { |event| push_decorated_event(queue, event, remote_host, req) }

# since payloads are self-contained and we don't handle multipart we should flush
# the codec after each request.
codec.flush { |event| push_decorated_event(queue, event, remote_host, req) }
ensure
@write_slots.put(local_codecs)
end
Expand Down Expand Up @@ -188,7 +187,16 @@ def run(queue)
@server.run.join
end

def stop
return unless @server
@server.stop(true)
@server.binder.close if @server.binder
rescue IOError
# do nothing
end

private

def lowercase_keys(hash)
new_hash = {}
hash.each_pair do |k,v|
Expand All @@ -197,13 +205,11 @@ def lowercase_keys(hash)
new_hash
end

public
def stop
return unless @server
@server.stop(true)
@server.binder.close if @server.binder
rescue IOError
# do nothing
def push_decorated_event(queue, event, host, headers)
event.set("host", host)
event.set("headers", headers)
decorate(event)
queue << event
end

end # class LogStash::Inputs::Http
1 change: 1 addition & 0 deletions logstash-input-http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ Gem::Specification.new do |s|

s.add_development_dependency 'logstash-devutils'
s.add_development_dependency 'logstash-codec-json'
s.add_development_dependency 'logstash-codec-line'
s.add_development_dependency 'ftw'
end
15 changes: 15 additions & 0 deletions spec/inputs/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def do_post
end
end
end

context "with json codec" do
subject { LogStash::Inputs::Http.new("port" => port, "codec" => "json") }
it "should parse the json body" do
Expand All @@ -145,6 +146,20 @@ def do_post
end
end

context "with json_lines codec without final delimiter" do
subject { LogStash::Inputs::Http.new("port" => port, "codec" => "line") }
let(:line1) { "foo" }
let(:line2) { "bar" }
it "should parse all json_lines in body including last one" do
agent.post!("http://localhost:#{port}/meh.json", :body => "#{line1}\n#{line2}")
expect(queue.size).to eq(2)
event = queue.pop
expect(event.get("message")).to eq("foo")
event = queue.pop
expect(event.get("message")).to eq("bar")
end
end

context "when using a custom codec mapping" do
subject { LogStash::Inputs::Http.new("port" => port,
"additional_codecs" => { "application/json" => "plain" }) }
Expand Down