Skip to content

Commit cdae411

Browse files
authored
Merge pull request #783 from rhett-inbox/clear-body-on-redirect
Clear body when redirecting to a GET
2 parents 36c31e0 + b4f6dff commit cdae411

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

Diff for: lib/httparty/request.rb

+33-18
Original file line numberDiff line numberDiff line change
@@ -295,24 +295,7 @@ def assume_utf16_is_big_endian
295295

296296
def handle_response(raw_body, &block)
297297
if response_redirects?
298-
options[:limit] -= 1
299-
if options[:logger]
300-
logger = HTTParty::Logger.build(options[:logger], options[:log_level], options[:log_format])
301-
logger.format(self, last_response)
302-
end
303-
self.path = last_response['location']
304-
self.redirect = true
305-
if last_response.class == Net::HTTPSeeOther
306-
unless options[:maintain_method_across_redirects] && options[:resend_on_redirect]
307-
self.http_method = Net::HTTP::Get
308-
end
309-
elsif last_response.code != '307' && last_response.code != '308'
310-
unless options[:maintain_method_across_redirects]
311-
self.http_method = Net::HTTP::Get
312-
end
313-
end
314-
capture_cookies(last_response)
315-
perform(&block)
298+
handle_redirection(&block)
316299
else
317300
raw_body ||= last_response.body
318301

@@ -331,6 +314,30 @@ def handle_response(raw_body, &block)
331314
end
332315
end
333316

317+
def handle_redirection(&block)
318+
options[:limit] -= 1
319+
if options[:logger]
320+
logger = HTTParty::Logger.build(options[:logger], options[:log_level], options[:log_format])
321+
logger.format(self, last_response)
322+
end
323+
self.path = last_response['location']
324+
self.redirect = true
325+
if last_response.class == Net::HTTPSeeOther
326+
unless options[:maintain_method_across_redirects] && options[:resend_on_redirect]
327+
self.http_method = Net::HTTP::Get
328+
end
329+
elsif last_response.code != '307' && last_response.code != '308'
330+
unless options[:maintain_method_across_redirects]
331+
self.http_method = Net::HTTP::Get
332+
end
333+
end
334+
if http_method == Net::HTTP::Get
335+
clear_body
336+
end
337+
capture_cookies(last_response)
338+
perform(&block)
339+
end
340+
334341
def handle_host_redirection
335342
check_duplicate_location_header
336343
redirect_path = options[:uri_adapter].parse(last_response['location']).normalize
@@ -362,6 +369,14 @@ def parse_response(body)
362369
parser.call(body, format)
363370
end
364371

372+
# Some Web Application Firewalls reject incoming GET requests that have a body
373+
# if we redirect, and the resulting verb is GET then we will clear the body that
374+
# may be left behind from the initiating request
375+
def clear_body
376+
options[:body] = nil
377+
@raw_request.body = nil
378+
end
379+
365380
def capture_cookies(response)
366381
return unless response['Set-Cookie']
367382
cookies_hash = HTTParty::CookieHash.new

Diff for: spec/httparty/request_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,14 @@
928928
expect(@request.http_method).to eq(Net::HTTP::Delete)
929929
end
930930

931+
it 'should clear the body before resulting GET requests' do
932+
@request.http_method = Net::HTTP::Post
933+
@request.options[:body] = { text: 'something' }
934+
expect(@request.perform.parsed_response).to eq({"hash" => {"foo" => "bar"}})
935+
expect(@request.http_method).to eq(Net::HTTP::Get)
936+
expect(@request.options[:body]).to be_nil
937+
end
938+
931939
it 'should log the redirection' do
932940
logger_double = double
933941
expect(logger_double).to receive(:info).twice

0 commit comments

Comments
 (0)