Closed dmaasland closed 8 months ago
Thanks Donny, nice catch - seems like this would be worth a quick PR.
There is a closely related issue... If you use verb => "HEAD"
and the response headers set the content type to application/json then body is nil and it blows up a few lines earlier in the other branch of the content-type test. The error logged is "JSON parsing error {:message=>"class org.jruby.RubyNil cannot be cast to class org.jruby.RubyIO"
^^^ is correct, the plugin is not handling HEAD requests (or simply nil
bodies) gracefully.
in those cases the whole logic should be skipped e.g.
def process_response(body, headers, event)
content_type, _ = headers.fetch("content-type", "").split(";")
event.set(@target_headers, headers)
return if body.nil? # new line added
if content_type == "application/json"
# ...
this would need some tests - worth a PR if anyone is up for one.
Duplicate #39 I will close my issue
Hi! Would it be unseemly of me to "bump" this issue?
We're experiencing that this plugin crashes with an exception (crashing logstash) when we make an http
request that returns an HTTP 204. It seems that Ruby does not populate the response body in this case, but the plugin unconditionally accesses the response body in process_response
, which is called for all HTTP statuses in the 200
to 299
range.
Just for reference, a workaround for those who stuck, is to use ruby plugin like this example
ruby {
# POST http://bonita:8080/bonita/loginservice
# returns 204 empty body and X-Bonita-API-Token and JSESSIONID cookies
code => "
require 'uri'
require 'net/http'
uri = URI('http://bonita:8080/bonita/loginservice')
res = Net::HTTP.post_form(uri, 'username' => 'admin', 'password' => 'admin')
cookies = res['set-cookie'].split(';')
cookies.each do |cookie|
if cookie.include? 'JSESSIONID'
event.set('[@metadata][JSESSIONID]', cookie.split('=')[2])
end
if cookie.include? 'X-Bonita-API-Token'
event.set('[@metadata][X-Bonita-API-Token]', cookie.split('=')[2])
end
end
"
}
the plugin is not handling HEAD requests (or simply
nil
bodies) gracefully. in those cases the whole logic should be skipped e.g.
This was implemented in PR 40.
Hi! Sorry, but PR #40 does not address the issue described. The PR skips attempting to process the response body when the request verb is HEAD. However, there are other situations in the HTTP protocol where we do not expect a response body, for example, HTTP 204.
If a GET is performed and the response is HTTP 204, the code will continue to try and access the response body, which causes an unhandled exception in ruby.
You are right, PR 40 implements "return if @verb == 'HEAD'", which handles one case, but "return if body.nil?" which @kares suggested would handle more. It is a one line change if anyone wants to do it...
If a web service responds with
HTTP/1.0 204 No Content
and an empty response body the plugin crashes. This is due to the invocation ofbody.strip
on this line.