Skip to content

Commit

Permalink
[bug fix] Stop manual URI parsing/escaping and use CGI::parse (#119)
Browse files Browse the repository at this point in the history
* Stop manual URI parsing/escaping

and use URI::HTTP standard library.

* Update .travis.yml

* bump minimum Ruby version

* Use CGI::parse

- URI::decode_www_form can't handle multibyte characters

* avoid using set_form_data()

- 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.

* Update .travis.yml

- follow latest Ruby releases
  • Loading branch information
smaeda-ks committed Dec 4, 2019
1 parent 7eb9a13 commit 5b03096
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
27 changes: 18 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
bundler_args: --without development
dist: xenial
language: ruby
rvm:
- 1.9.3
- 2.0.0
- jruby-head
- rbx-2
- ruby-head

before_install:
- bundle config without 'development'

branches:
only:
- master

matrix:
fast_finish: true
allow_failures:
- rvm: jruby-head
- rvm: ruby-head
fast_finish: true
sudo: false
- rvm: rbx-2
include:
- rvm: 2.6.5
- rvm: 2.5.7
- rvm: 2.4.9
- rvm: jruby-head
- rvm: ruby-head
- rvm: rbx-2
19 changes: 11 additions & 8 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 @@ -159,11 +160,9 @@ def extract_path!(arguments)
end

def escape_params(params)
split_params = params.split("&").map do |param|
key, value = param.split('=', 2)
CGI::escape(key) + '=' + CGI::escape(value)
end
split_params.join("&")
CGI::parse(params).map do |key, value|
"#{CGI.escape key}=#{CGI.escape value.first}"
end.join("&")
end
end

Expand Down Expand Up @@ -230,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
3 changes: 1 addition & 2 deletions twurl.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require 'twurl/version'

Gem::Specification.new do |spec|
spec.add_dependency 'oauth', '~> 0.4'
spec.add_development_dependency 'bundler', '~> 1.0'
spec.authors = ["Marcel Molina", "Erik Michaels-Ober"]
spec.description = %q{Curl for the Twitter API}
spec.email = ['marcel@twitter.com']
Expand All @@ -17,7 +16,7 @@ Gem::Specification.new do |spec|
spec.name = 'twurl'
spec.rdoc_options = ['--title', 'twurl -- OAuth-enabled curl for the Twitter API', '--main', 'README', '--line-numbers', '--inline-source']
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 1.9.3'
spec.required_ruby_version = '>= 2.4.0'
spec.rubyforge_project = 'twurl'
spec.summary = spec.description
spec.version = Twurl::Version
Expand Down

0 comments on commit 5b03096

Please sign in to comment.