diff --git a/lib/typhoeus/easy/options.rb b/lib/typhoeus/easy/options.rb index 329a62b3..80d66789 100644 --- a/lib/typhoeus/easy/options.rb +++ b/lib/typhoeus/easy/options.rb @@ -107,9 +107,13 @@ def set_option(option, value) def set_headers @header_list = nil - headers.each {|key, value| @header_list = Curl.slist_append(@header_list, "#{key}: #{value}") } + headers.each {|key, value| @header_list = Curl.slist_append(@header_list, "#{key}: #{escape_zero_byte(value)}") } set_option(:httpheader, @header_list) unless headers.empty? end + + def escape_zero_byte(value) + value.gsub(0.chr, '\\\0') + end end end end diff --git a/spec/typhoeus/easy/options_spec.rb b/spec/typhoeus/easy/options_spec.rb new file mode 100644 index 00000000..89cef3dc --- /dev/null +++ b/spec/typhoeus/easy/options_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe Typhoeus::EasyFu::Options do + describe "#set_headers" do + let(:headers) { { 'User-Agent' => "fubar\0" } } + let(:request) { Typhoeus::Request.get("http://localhost:3001", { :headers => headers }) } + + it "sends them" do + request.body.should include("fubar") + end + + it "removes zero bytes from values" do + request.body.should include("fubar\\\\0") + end + end +end