Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add frozen_string_literal everywhere and fix string mutation with force_encoding #592

Closed
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
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ Style/RegexpLiteral:
Style/NumericLiterals:
MinDigits: 6

# Offense count: 0
# Cop supports --auto-correct.
Style/FrozenStringLiteralComment:
EnforcedStyle: always

# Offense count: 4
# Cop supports --auto-correct.
Lint/UnusedMethodArgument:
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'
gemspec

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

begin
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
Expand Down
2 changes: 2 additions & 0 deletions bin/httparty
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env ruby

# frozen_string_literal: true

require "optparse"
require "pp"

Expand Down
2 changes: 2 additions & 0 deletions examples/aaws.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rubygems'
require 'active_support'

Expand Down
2 changes: 2 additions & 0 deletions examples/basic.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions examples/crack.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rubygems'
require 'crack'

Expand Down
2 changes: 2 additions & 0 deletions examples/custom_parsers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions examples/delicious.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions examples/google.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions examples/headers_and_user_agents.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# To send custom user agents to identify your application to a web service (or mask as a specific browser for testing), send "User-Agent" as a hash to headers as shown below.

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
Expand Down
2 changes: 2 additions & 0 deletions examples/logging.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'logger'
Expand Down
2 changes: 2 additions & 0 deletions examples/nokogiri_html_parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rubygems'
require 'nokogiri'

Expand Down
2 changes: 2 additions & 0 deletions examples/rescue_json.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')

Expand Down
2 changes: 2 additions & 0 deletions examples/rubyurl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions examples/stackexchange.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions examples/stream_download.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions examples/tripit_sign_in.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')

Expand Down
2 changes: 2 additions & 0 deletions examples/twitter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions examples/whoismyrep.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
Expand Down
2 changes: 2 additions & 0 deletions features/steps/env.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'mongrel'
require './lib/httparty'
require 'rspec/expectations'
Expand Down
2 changes: 2 additions & 0 deletions features/steps/httparty_response_steps.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Not needed anymore in ruby 2.0, but needed to resolve constants
# in nested namespaces. This is taken from rails :)
def constantize(camel_cased_word)
Expand Down
2 changes: 2 additions & 0 deletions features/steps/httparty_steps.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

When /^I set my HTTParty timeout option to (\d+)$/ do |timeout|
@request_options[:timeout] = timeout.to_i
end
Expand Down
8 changes: 5 additions & 3 deletions features/steps/mongrel_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'base64'
class BasicMongrelHandler < Mongrel::HttpHandler
attr_accessor :content_type, :custom_headers, :response_body, :response_code, :preprocessor, :username, :password
Expand Down Expand Up @@ -95,19 +97,19 @@ module DigestAuthenticationUsingMD5Sess
def self.extended(base)
base.custom_headers["WWW-Authenticate"] = %(Digest realm="#{REALM}",qop="#{QOP}",algorithm="MD5-sess",nonce="#{NONCE}",opaque="opaque"')
end

def process(request, response)
if authorized?(request)
super
else
reply_with(response, 401, "Incorrect. You have 20 seconds to comply.")
end
end

def md5(str)
Digest::MD5.hexdigest(str)
end

def authorized?(request)
auth = request.params["HTTP_AUTHORIZATION"]
params = {}
Expand Down
2 changes: 2 additions & 0 deletions features/steps/remote_service_steps.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Given /a remote service that returns '(.*)'/ do |response_body|
@handler = BasicMongrelHandler.new
step "the response from the service has a body of '#{response_body}'"
Expand Down
2 changes: 2 additions & 0 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'pathname'
require 'net/http'
require 'net/https'
Expand Down
6 changes: 4 additions & 2 deletions lib/httparty/connection_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module HTTParty
# Default connection adapter that returns a new Net::HTTP each time
#
Expand Down Expand Up @@ -38,12 +40,12 @@ module HTTParty
# in the #options attribute. It is up to you to interpret them within your
# connection adapter. Take a look at the implementation of
# HTTParty::ConnectionAdapter#connection for examples of how they are used.
# The keys used in options are
# The keys used in options are
# * :+timeout+: timeout in seconds
# * :+open_timeout+: http connection open_timeout in seconds, overrides timeout if set
# * :+read_timeout+: http connection read_timeout in seconds, overrides timeout if set
# * :+debug_output+: see HTTParty::ClassMethods.debug_output.
# * :+cert_store+: contains certificate data. see method 'attach_ssl_certificates'
# * :+cert_store+: contains certificate data. see method 'attach_ssl_certificates'
# * :+pem+: contains pem client certificate data. see method 'attach_ssl_certificates'
# * :+p12+: contains PKCS12 client client certificate data. see method 'attach_ssl_certificates'
# * :+verify+: verify the server’s certificate against the ca certificate.
Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/cookie_hash.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class HTTParty::CookieHash < Hash #:nodoc:
CLIENT_COOKIES = %w(path expires domain path secure httponly)

Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module HTTParty
# @abstact Exceptions raised by HTTParty inherit from Error
class Error < StandardError; end
Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/hash_conversions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'erb'

module HTTParty
Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/logger/apache_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module HTTParty
module Logger
class ApacheFormatter #:nodoc:
Expand Down
6 changes: 4 additions & 2 deletions lib/httparty/logger/curl_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# frozen_string_literal: true

module HTTParty
module Logger
class CurlFormatter #:nodoc:
TAG_NAME = HTTParty.name
OUT = '>'.freeze
IN = '<'.freeze
OUT = '>'
IN = '<'

attr_accessor :level, :logger

Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/logger/logger.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'httparty/logger/apache_formatter'
require 'httparty/logger/curl_formatter'

Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/module_inheritable_attributes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module HTTParty
module ModuleInheritableAttributes #:nodoc:
def self.included(base)
Expand Down
10 changes: 6 additions & 4 deletions lib/httparty/net_digest_auth.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'digest/md5'
require 'net/http'

Expand All @@ -14,11 +16,11 @@ def digest_auth(username, password, response)

authenticator.authorization_header.each do |v|
add_field('Authorization', v)
end
end

authenticator.cookie_header.each do |v|
add_field('Cookie', v)
end
end
end

class DigestAuthenticator
Expand Down Expand Up @@ -113,11 +115,11 @@ def md5(str)
def algorithm_present?
@response.key?('algorithm') && !@response['algorithm'].empty?
end

def use_md5_sess?
algorithm_present? && @response['algorithm'] == 'MD5-sess'
end

def a1
a1_user_realm_pwd = [@username, @response['realm'], @password].join(':')
if use_md5_sess?
Expand Down
4 changes: 3 additions & 1 deletion lib/httparty/parser.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module HTTParty
# The default parser used by HTTParty, supports xml, json, html, csv and
# plain text.
Expand Down Expand Up @@ -119,7 +121,7 @@ def xml
MultiXml.parse(body)
end

UTF8_BOM = "\xEF\xBB\xBF".freeze
UTF8_BOM = "\xEF\xBB\xBF"

def json
JSON.parse(body, :quirks_mode => true, :allow_nan => true)
Expand Down
12 changes: 7 additions & 5 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'erb'
require 'httparty/request/body'

Expand Down Expand Up @@ -286,7 +288,7 @@ def encode_with_ruby_encoding(body, charset)
# NOTE: This will raise an argument error if the
# charset does not exist
encoding = Encoding.find(charset)
body.force_encoding(encoding.to_s)
body.dup.force_encoding(encoding.to_s)
rescue ArgumentError
body
end
Expand All @@ -298,16 +300,16 @@ def assume_utf16_is_big_endian
def encode_utf_16(body)
if body.bytesize >= 2
if body.getbyte(0) == 0xFF && body.getbyte(1) == 0xFE
return body.force_encoding("UTF-16LE")
return body.dup.force_encoding("UTF-16LE")
elsif body.getbyte(0) == 0xFE && body.getbyte(1) == 0xFF
return body.force_encoding("UTF-16BE")
return body.dup.force_encoding("UTF-16BE")
end
end

if assume_utf16_is_big_endian
body.force_encoding("UTF-16BE")
body.dup.force_encoding("UTF-16BE")
else
body.force_encoding("UTF-16LE")
body.dup.force_encoding("UTF-16LE")
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/request/body.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative 'multipart_boundary'

module HTTParty
Expand Down
2 changes: 2 additions & 0 deletions lib/httparty/request/multipart_boundary.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'securerandom'

module HTTParty
Expand Down
10 changes: 6 additions & 4 deletions lib/httparty/response.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module HTTParty
class Response < Object
def self.underscore(string)
Expand Down Expand Up @@ -59,10 +61,10 @@ def nil?
response.nil? || response.body.nil? || response.body.empty?
end

def to_s
def to_s
if !response.nil? && !response.body.nil? && response.body.respond_to?(:to_s)
response.body.to_s
else
else
inspect
end
end
Expand All @@ -80,7 +82,7 @@ def display(port=$>)
parsed_response.display(port)
elsif !response.nil? && !response.body.nil? && response.body.respond_to?(:display)
response.body.display(port)
else
else
port.write(inspect)
end
end
Expand All @@ -89,7 +91,7 @@ def respond_to_missing?(name, *args)
return true if super
parsed_response.respond_to?(name) || response.respond_to?(name)
end

protected

def method_missing(name, *args, &block)
Expand Down
Loading