Skip to content
Open
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
28 changes: 25 additions & 3 deletions lib/cgi/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@ def initialize_query()
boundary = $1.dup
@multipart = true
@params = read_multipart(boundary, Integer(content_length))
append_query_string(@params)
else
@multipart = false
@params = CGI.parse(
Expand All @@ -860,10 +861,21 @@ def initialize_query()
end
when "POST"
stdinput.binmode if defined? stdinput.binmode
if content_length.nil?
stdinput.read or ''
body = if content_length.nil?
stdinput.read || ''
else
stdinput.read(Integer(content_length)) or ''
stdinput.read(Integer(content_length)) || ''
end
# The query string is independent of the method (cgi#57).
# Body values stay first, so cgi[key] is unchanged when
# the same name is in both places.
query = env_table['QUERY_STRING'] || ""
if query.empty?
body
elsif body.empty?
query
else
"#{body}&#{query}"
end
else
read_from_cmdline
Expand All @@ -888,6 +900,16 @@ def initialize_query()
end
private :initialize_query

def append_query_string(params)
query = env_table['QUERY_STRING'] || ""
return params if query.empty?
CGI.parse(query).each do |key, values|
(params[key] ||= []).concat(values)
end
params
end
private :append_query_string

# Returns whether the form contained multipart/form-data
def multipart?
@multipart
Expand Down
17 changes: 17 additions & 0 deletions test/cgi/test_cgi_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ def test_cgi_core_params_POST
$stdin = STDIN
end

def test_cgi_core_params_POST_reads_the_query_string
body = 'body=1'
update_env(
'REQUEST_METHOD' => 'POST',
'CONTENT_LENGTH' => body.bytesize.to_s,
'QUERY_STRING' => 'q=2&body=from-query',
)
$stdin = StringIO.new(body)
cgi = CGI.new
assert_equal('1', cgi['body'])
assert_equal(['1', 'from-query'], cgi.params['body'])
assert_equal('2', cgi['q'])
assert_equal(['2'], cgi.params['q'])
ensure
$stdin = STDIN
end

def test_cgi_core_params_POST_without_content_length
update_env(
'REQUEST_METHOD' => 'POST',
Expand Down