From fbd5cda6aad6db01bbca3d893a9970314a1bd52c Mon Sep 17 00:00:00 2001 From: kazu Date: Sat, 18 Feb 2017 05:52:16 +0000 Subject: [PATCH] {lib,test}/cgi: Specify frozen_string_literal: true. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/cgi.rb | 2 +- lib/cgi/cookie.rb | 4 ++-- lib/cgi/core.rb | 16 ++++++++-------- lib/cgi/html.rb | 6 +++--- lib/cgi/session.rb | 2 +- lib/cgi/session/pstore.rb | 2 +- lib/cgi/util.rb | 2 +- test/cgi/test_cgi_cookie.rb | 6 +++--- test/cgi/test_cgi_core.rb | 12 ++++++------ test/cgi/test_cgi_header.rb | 12 ++++++------ test/cgi/test_cgi_modruby.rb | 2 +- test/cgi/test_cgi_multipart.rb | 12 ++++++------ test/cgi/test_cgi_session.rb | 10 +++++----- test/cgi/test_cgi_tag_helper.rb | 2 +- test/cgi/test_cgi_util.rb | 28 ++++++++++++++-------------- 15 files changed, 59 insertions(+), 59 deletions(-) diff --git a/lib/cgi.rb b/lib/cgi.rb index 167b76cef7..0f44a929e4 100644 --- a/lib/cgi.rb +++ b/lib/cgi.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # cgi.rb - cgi support library # diff --git a/lib/cgi/cookie.rb b/lib/cgi/cookie.rb index 4cc050b90d..a2155edb77 100644 --- a/lib/cgi/cookie.rb +++ b/lib/cgi/cookie.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'cgi/util' class CGI # Class representing an HTTP cookie. @@ -143,7 +143,7 @@ class CGI # Convert the Cookie to its string representation. def to_s val = collect{|v| CGI.escape(v) }.join("&") - buf = "#{@name}=#{val}" + buf = "#{@name}=#{val}".dup buf << "; domain=#{@domain}" if @domain buf << "; path=#{@path}" if @path buf << "; expires=#{CGI::rfc1123_date(@expires)}" if @expires diff --git a/lib/cgi/core.rb b/lib/cgi/core.rb index 1a741dcd76..fd9c41aa31 100644 --- a/lib/cgi/core.rb +++ b/lib/cgi/core.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true #-- # Methods for generating HTML, parsing CGI-related parameters, and # generating HTTP responses. @@ -182,7 +182,7 @@ class CGI alias :header :http_header def _header_for_string(content_type) #:nodoc: - buf = '' + buf = ''.dup if nph?() buf << "#{$CGI_ENV['SERVER_PROTOCOL'] || 'HTTP/1.0'} 200 OK#{EOL}" buf << "Date: #{CGI.rfc1123_date(Time.now)}#{EOL}" @@ -198,7 +198,7 @@ class CGI private :_header_for_string def _header_for_hash(options) #:nodoc: - buf = '' + buf = ''.dup ## add charset to option['type'] options['type'] ||= 'text/html' charset = options.delete('charset') @@ -480,7 +480,7 @@ class CGI @files = {} boundary_rexp = /--#{Regexp.quote(boundary)}(#{EOL}|--)/ boundary_size = "#{EOL}--#{boundary}#{EOL}".bytesize - buf = '' + buf = ''.dup bufsize = 10 * 1024 max_count = MAX_MULTIPART_COUNT n = 0 @@ -535,12 +535,12 @@ class CGI body.rewind ## original filename /Content-Disposition:.* filename=(?:"(.*?)"|([^;\r\n]*))/i.match(head) - filename = $1 || $2 || '' + filename = $1 || $2 || ''.dup filename = CGI.unescape(filename) if unescape_filename?() body.instance_variable_set(:@original_filename, filename.taint) ## content type /Content-Type: (.*)/i.match(head) - (content_type = $1 || '').chomp! + (content_type = $1 || ''.dup).chomp! body.instance_variable_set(:@content_type, content_type.taint) ## query parameter name /Content-Disposition:.* name=(?:"(.*?)"|([^;\r\n]*))/i.match(head) @@ -589,7 +589,7 @@ class CGI else begin require 'stringio' - body = StringIO.new("".force_encoding(Encoding::ASCII_8BIT)) + body = StringIO.new("".b) rescue LoadError require 'tempfile' body = Tempfile.new('CGI', encoding: Encoding::ASCII_8BIT) @@ -700,7 +700,7 @@ class CGI if value return value elsif defined? StringIO - StringIO.new("".force_encoding(Encoding::ASCII_8BIT)) + StringIO.new("".b) else Tempfile.new("CGI",encoding: Encoding::ASCII_8BIT) end diff --git a/lib/cgi/html.rb b/lib/cgi/html.rb index 4b9e577b32..02d847ebd3 100644 --- a/lib/cgi/html.rb +++ b/lib/cgi/html.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true class CGI # Base module for HTML-generation mixins. # @@ -26,7 +26,7 @@ class CGI # - O EMPTY def nOE_element(element, attributes = {}) attributes={attributes=>nil} if attributes.kind_of?(String) - s = "<#{element.upcase}" + s = "<#{element.upcase}".dup attributes.each do|name, value| next unless value s << " " @@ -408,7 +408,7 @@ class CGI end pretty = attributes.delete("PRETTY") pretty = " " if true == pretty - buf = "" + buf = "".dup if attributes.has_key?("DOCTYPE") if attributes["DOCTYPE"] diff --git a/lib/cgi/session.rb b/lib/cgi/session.rb index b504f25f15..fbb42986a4 100644 --- a/lib/cgi/session.rb +++ b/lib/cgi/session.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # cgi/session.rb - session support for cgi scripts # diff --git a/lib/cgi/session/pstore.rb b/lib/cgi/session/pstore.rb index 2dfb72bdce..cb0370b619 100644 --- a/lib/cgi/session/pstore.rb +++ b/lib/cgi/session/pstore.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # cgi/session/pstore.rb - persistent storage of marshalled session data # diff --git a/lib/cgi/util.rb b/lib/cgi/util.rb index 66fa54d8e9..d765fd73e6 100644 --- a/lib/cgi/util.rb +++ b/lib/cgi/util.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true class CGI; module Util; end; extend Util; end module CGI::Util @@accept_charset="UTF-8" unless defined?(@@accept_charset) diff --git a/test/cgi/test_cgi_cookie.rb b/test/cgi/test_cgi_cookie.rb index ca81e41133..115a57e4a1 100644 --- a/test/cgi/test_cgi_cookie.rb +++ b/test/cgi/test_cgi_cookie.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'test/unit' require 'cgi' require 'stringio' @@ -15,7 +15,7 @@ class CGICookieTest < Test::Unit::TestCase 'REQUEST_METHOD' => 'GET', 'SCRIPT_NAME' => nil, ) - @str1="\xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93" + @str1="\xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93".dup @str1.force_encoding("UTF-8") if defined?(::Encoding) end @@ -39,7 +39,7 @@ class CGICookieTest < Test::Unit::TestCase def test_cgi_cookie_new_complex t = Time.gm(2030, 12, 31, 23, 59, 59) - value = ['val1', '&<>"', "\xA5\xE0\xA5\xB9\xA5\xAB"] + value = ['val1', '&<>"', "\xA5\xE0\xA5\xB9\xA5\xAB".dup] value[2].force_encoding("EUC-JP") if defined?(::Encoding) cookie = CGI::Cookie.new('name'=>'name1', 'value'=>value, diff --git a/test/cgi/test_cgi_core.rb b/test/cgi/test_cgi_core.rb index 97308a0457..255203dc17 100644 --- a/test/cgi/test_cgi_core.rb +++ b/test/cgi/test_cgi_core.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'test/unit' require 'cgi' require 'stringio' @@ -105,11 +105,11 @@ class CGICoreTest < Test::Unit::TestCase hash={} cgi = CGI.new(:accept_charset=>"UTF-8"){|key,val|hash[key]=val} ## cgi[] - assert_equal("\xBE\xBE\xB9\xBE".force_encoding("UTF-8"), cgi['str']) + assert_equal("\xBE\xBE\xB9\xBE".dup.force_encoding("UTF-8"), cgi['str']) ## cgi.params - assert_equal(["\xBE\xBE\xB9\xBE".force_encoding("UTF-8")], cgi.params['str']) + assert_equal(["\xBE\xBE\xB9\xBE".dup.force_encoding("UTF-8")], cgi.params['str']) ## accept-charset error - assert_equal({"str"=>"\xBE\xBE\xB9\xBE".force_encoding("UTF-8")},hash) + assert_equal({"str"=>"\xBE\xBE\xB9\xBE".dup.force_encoding("UTF-8")},hash) $stdin.rewind assert_raise(CGI::InvalidEncoding) do @@ -119,9 +119,9 @@ class CGICoreTest < Test::Unit::TestCase $stdin.rewind cgi = CGI.new(:accept_charset=>"EUC-JP") ## cgi[] - assert_equal("\xBE\xBE\xB9\xBE".force_encoding("EUC-JP"), cgi['str']) + assert_equal("\xBE\xBE\xB9\xBE".dup.force_encoding("EUC-JP"), cgi['str']) ## cgi.params - assert_equal(["\xBE\xBE\xB9\xBE".force_encoding("EUC-JP")], cgi.params['str']) + assert_equal(["\xBE\xBE\xB9\xBE".dup.force_encoding("EUC-JP")], cgi.params['str']) else assert(true) end diff --git a/test/cgi/test_cgi_header.rb b/test/cgi/test_cgi_header.rb index c420e7978b..bab2d0348a 100644 --- a/test/cgi/test_cgi_header.rb +++ b/test/cgi/test_cgi_header.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'test/unit' require 'cgi' require 'time' @@ -58,7 +58,7 @@ class CGIHeaderTest < Test::Unit::TestCase 'expires' => Time.gm(2000, 1, 23, 12, 34, 56), 'location' => 'http://www.ruby-lang.org/', } - expected = "Status: 302 Found\r\n" + expected = "Status: 302 Found\r\n".dup expected << "Server: webrick\r\n" expected << "Connection: close\r\n" expected << "Content-Type: text/xhtml; charset=utf8\r\n" @@ -116,7 +116,7 @@ class CGIHeaderTest < Test::Unit::TestCase CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true), ] cgi.instance_variable_set('@output_cookies', cookies) - expected = "Content-Type: text/html; charset=utf8\r\n" + expected = "Content-Type: text/html; charset=utf8\r\n".dup expected << "Set-Cookie: name1=abc&123; path=\r\n" expected << "Set-Cookie: name2=value2; path=; secure\r\n" expected << "\r\n" @@ -151,7 +151,7 @@ class CGIHeaderTest < Test::Unit::TestCase actual.sub!(date, "Date: DATE_IS_REMOVED\r\n") end ## assertion - expected = "HTTP/1.1 200 OK\r\n" + expected = "HTTP/1.1 200 OK\r\n".dup expected << "Date: DATE_IS_REMOVED\r\n" expected << "Server: Apache 2.2.0\r\n" expected << "Connection: close\r\n" @@ -163,10 +163,10 @@ class CGIHeaderTest < Test::Unit::TestCase expected.sub!(/^HTTP\/1.1 200 OK\r\n/, "HTTP/1.1 302 Found\r\n") expected.sub!(/\r\n\r\n/, "\r\nlocation: http://www.example.com/\r\n\r\n") assert_equal(expected, actual3) - expected = "Content-Type: text/html\r\n" + expected = "Content-Type: text/html\r\n".dup expected << "\r\n" assert_equal(expected, actual4) - expected = "Status: 302 Found\r\n" + expected = "Status: 302 Found\r\n".dup expected << "Content-Type: text/html\r\n" expected << "location: http://www.example.com/\r\n" expected << "\r\n" diff --git a/test/cgi/test_cgi_modruby.rb b/test/cgi/test_cgi_modruby.rb index 9813a95277..90132962b5 100644 --- a/test/cgi/test_cgi_modruby.rb +++ b/test/cgi/test_cgi_modruby.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'test/unit' require 'cgi' require_relative 'update_env' diff --git a/test/cgi/test_cgi_multipart.rb b/test/cgi/test_cgi_multipart.rb index f63c2e3778..d27b1cb8b6 100644 --- a/test/cgi/test_cgi_multipart.rb +++ b/test/cgi/test_cgi_multipart.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'test/unit' require 'cgi' require 'tempfile' @@ -33,7 +33,7 @@ class MultiPart def initialize(boundary=nil) @boundary = boundary || create_boundary() - @buf = '' + @buf = ''.dup @buf.force_encoding(::Encoding::ASCII_8BIT) if defined?(::Encoding) end attr_reader :boundary @@ -53,7 +53,7 @@ class MultiPart def close buf = @buf - @buf = '' + @buf = ''.dup return buf << "--#{boundary}--\r\n" end @@ -202,7 +202,7 @@ class CGIMultipartTest < Test::Unit::TestCase @boundary = '----WebKitFormBoundaryAAfvAII+YL9102cX' @data = [ {:name=>'hidden1', :value=>'foobar'}, - {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"}, + {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A".dup}, {:name=>'file1', :value=>_read('file1.html'), :filename=>'file1.html', :content_type=>'text/html'}, {:name=>'image1', :value=>_read('small.png'), @@ -218,7 +218,7 @@ class CGIMultipartTest < Test::Unit::TestCase @boundary = '----WebKitFormBoundaryAAfvAII+YL9102cX' @data = [ {:name=>'hidden1', :value=>'foobar'}, - {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"}, + {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A".dup}, {:name=>'file1', :value=>_read('file1.html'), :filename=>'file1.html', :content_type=>'text/html'}, {:name=>'image1', :value=>_read('large.png'), @@ -323,7 +323,7 @@ class CGIMultipartTest < Test::Unit::TestCase @boundary = '(.|\n)*' @data = [ {:name=>'hidden1', :value=>'foobar'}, - {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A"}, + {:name=>'text1', :value=>"\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86\xE3\x81\x88\xE3\x81\x8A".dup}, {:name=>'file1', :value=>_read('file1.html'), :filename=>'file1.html', :content_type=>'text/html'}, {:name=>'image1', :value=>_read('small.png'), diff --git a/test/cgi/test_cgi_session.rb b/test/cgi/test_cgi_session.rb index 59e8d4c15b..b16b69766e 100644 --- a/test/cgi/test_cgi_session.rb +++ b/test/cgi/test_cgi_session.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'test/unit' require 'cgi' require 'cgi/session' @@ -30,7 +30,7 @@ class CGISessionTest < Test::Unit::TestCase 'SERVER_PROTOCOL' => 'HTTP/1.1', ) value1="value1" - value2="\x8F\xBC\x8D]" + value2="\x8F\xBC\x8D]".dup value2.force_encoding("SJIS") if defined?(::Encoding) cgi = CGI.new session = CGI::Session.new(cgi,"tmpdir"=>@session_dir) @@ -66,7 +66,7 @@ class CGISessionTest < Test::Unit::TestCase 'SERVER_PROTOCOL' => 'HTTP/1.1', ) value1="value1" - value2="\x8F\xBC\x8D]" + value2="\x8F\xBC\x8D]".dup value2.force_encoding("SJIS") if defined?(::Encoding) cgi = CGI.new session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"database_manager"=>CGI::Session::PStore) @@ -101,7 +101,7 @@ class CGISessionTest < Test::Unit::TestCase 'SERVER_PROTOCOL' => 'HTTP/1.1', ) value1="value1" - value2="\x8F\xBC\x8D]" + value2="\x8F\xBC\x8D]".dup value2.force_encoding("SJIS") if defined?(::Encoding) cgi = CGI.new session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"session_id"=>"foo") @@ -139,7 +139,7 @@ class CGISessionTest < Test::Unit::TestCase 'SERVER_PROTOCOL' => 'HTTP/1.1', ) value1="value1" - value2="\x8F\xBC\x8D]" + value2="\x8F\xBC\x8D]".dup value2.force_encoding("SJIS") if defined?(::Encoding) cgi = CGI.new session = CGI::Session.new(cgi,"tmpdir"=>@session_dir,"session_key"=>"bar") diff --git a/test/cgi/test_cgi_tag_helper.rb b/test/cgi/test_cgi_tag_helper.rb index a48bafdaec..0b99dfc1bc 100644 --- a/test/cgi/test_cgi_tag_helper.rb +++ b/test/cgi/test_cgi_tag_helper.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'test/unit' require 'cgi' require 'stringio' diff --git a/test/cgi/test_cgi_util.rb b/test/cgi/test_cgi_util.rb index 0765249c4e..9c9067a488 100644 --- a/test/cgi/test_cgi_util.rb +++ b/test/cgi/test_cgi_util.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true require 'test/unit' require 'cgi' require 'stringio' @@ -15,7 +15,7 @@ class CGIUtilTest < Test::Unit::TestCase 'REQUEST_METHOD' => 'GET', 'SCRIPT_NAME' => nil, ) - @str1="&<>\" \xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93" + @str1="&<>\" \xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93".dup @str1.force_encoding("UTF-8") if defined?(::Encoding) end @@ -31,14 +31,14 @@ class CGIUtilTest < Test::Unit::TestCase def test_cgi_escape_with_invalid_byte_sequence assert_nothing_raised(ArgumentError) do - assert_equal('%C0%3C%3C', CGI::escape("\xC0\<\<".force_encoding("UTF-8"))) + assert_equal('%C0%3C%3C', CGI::escape("\xC0\<\<".dup.force_encoding("UTF-8"))) end end def test_cgi_escape_preserve_encoding - assert_equal(Encoding::US_ASCII, CGI::escape("\xC0\<\<".force_encoding("US-ASCII")).encoding) - assert_equal(Encoding::ASCII_8BIT, CGI::escape("\xC0\<\<".force_encoding("ASCII-8BIT")).encoding) - assert_equal(Encoding::UTF_8, CGI::escape("\xC0\<\<".force_encoding("UTF-8")).encoding) + assert_equal(Encoding::US_ASCII, CGI::escape("\xC0\<\<".dup.force_encoding("US-ASCII")).encoding) + assert_equal(Encoding::ASCII_8BIT, CGI::escape("\xC0\<\<".dup.force_encoding("ASCII-8BIT")).encoding) + assert_equal(Encoding::UTF_8, CGI::escape("\xC0\<\<".dup.force_encoding("UTF-8")).encoding) end def test_cgi_unescape @@ -51,9 +51,9 @@ class CGIUtilTest < Test::Unit::TestCase end def test_cgi_unescape_preserve_encoding - assert_equal(Encoding::US_ASCII, CGI::unescape("%C0%3C%3C".force_encoding("US-ASCII")).encoding) - assert_equal(Encoding::ASCII_8BIT, CGI::unescape("%C0%3C%3C".force_encoding("ASCII-8BIT")).encoding) - assert_equal(Encoding::UTF_8, CGI::unescape("%C0%3C%3C".force_encoding("UTF-8")).encoding) + assert_equal(Encoding::US_ASCII, CGI::unescape("%C0%3C%3C".dup.force_encoding("US-ASCII")).encoding) + assert_equal(Encoding::ASCII_8BIT, CGI::unescape("%C0%3C%3C".dup.force_encoding("ASCII-8BIT")).encoding) + assert_equal(Encoding::UTF_8, CGI::unescape("%C0%3C%3C".dup.force_encoding("UTF-8")).encoding) end def test_cgi_unescape_accept_charset @@ -72,7 +72,7 @@ class CGIUtilTest < Test::Unit::TestCase end def test_cgi_escape_html_duplicated - orig = "Ruby".force_encoding("US-ASCII") + orig = "Ruby".dup.force_encoding("US-ASCII") str = CGI::escapeHTML(orig) assert_equal(orig, str) assert_not_same(orig, str) @@ -90,10 +90,10 @@ class CGIUtilTest < Test::Unit::TestCase end def test_cgi_escape_html_preserve_tainted - assert_not_predicate CGI::escapeHTML("'&\"><"), :tainted? - assert_predicate CGI::escapeHTML("'&\"><".taint), :tainted? - assert_not_predicate CGI::escapeHTML("Ruby"), :tainted? - assert_predicate CGI::escapeHTML("Ruby".taint), :tainted? + assert_not_predicate CGI::escapeHTML("'&\"><"), :tainted? + assert_predicate CGI::escapeHTML("'&\"><".dup.taint), :tainted? + assert_not_predicate CGI::escapeHTML("Ruby"), :tainted? + assert_predicate CGI::escapeHTML("Ruby".dup.taint), :tainted? end def test_cgi_escape_html_dont_freeze