Skip to content

Commit

Permalink
avoid using set_form_data()
Browse files Browse the repository at this point in the history
- as it doesn't seem to be escaping some of the special characters such as '*' (asterisk). Use CGI.escape instead.
- do not parse/escape request POST body in case if "content-type" request header is specified.
  • Loading branch information
smaeda-ks committed Nov 28, 2019
1 parent 5c05a68 commit 14d455a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/twurl/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def parse_options(args)
arguments = args.dup

Twurl.options = Options.new
Twurl.options.args = arguments
Twurl.options.trace = false
Twurl.options.data = {}
Twurl.options.headers = {}
Expand Down Expand Up @@ -228,9 +229,13 @@ def trace

def data
on('-d', '--data [data]', 'Sends the specified data in a POST request to the HTTP server.') do |data|
data.split('&').each do |pair|
key, value = pair.split('=', 2)
options.data[key] = value
if options.args.count { |item| /content-type: (.*)/i.match(item) } > 0
options.data[data] = nil
else
data.split('&').each do |pair|
key, value = pair.split('=', 2)
options.data[key] = value
end
end
end
end
Expand Down
9 changes: 8 additions & 1 deletion lib/twurl/oauth_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ def perform_request_from_options(options, &block)
elsif request.content_type && options.data
request.body = options.data.keys.first
elsif options.data
request.set_form_data(options.data)
request.content_type = "application/x-www-form-urlencoded"
if options.data.length == 1 && options.data.values.first == nil
request.body = options.data.keys.first
else
request.body = options.data.map do |key, value|
"#{key}=#{CGI.escape value}"
end.join("&")
end
end

request.oauth!(consumer.http, consumer, access_token)
Expand Down

0 comments on commit 14d455a

Please sign in to comment.