[前][次][番号順一覧][スレッド一覧]

ruby-changes:54098

From: naruse <ko1@a...>
Date: Tue, 11 Dec 2018 08:54:17 +0900 (JST)
Subject: [ruby-changes:54098] naruse:r66319 (trunk): Preserve HTTP header key as string [Bug #15394]

naruse	2018-12-11 08:54:14 +0900 (Tue, 11 Dec 2018)

  New Revision: 66319

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66319

  Log:
    Preserve HTTP header key as string [Bug #15394]
    
    to prevent send Host header twice accidentally.
    
    From: Sangyong Sim <sangyong-sim@c...>

  Modified files:
    trunk/lib/net/http/header.rb
    trunk/test/net/http/test_httpheader.rb
Index: lib/net/http/header.rb
===================================================================
--- lib/net/http/header.rb	(revision 66318)
+++ lib/net/http/header.rb	(revision 66319)
@@ -22,7 +22,7 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L22
         if value.count("\r\n") > 0
           raise ArgumentError, "header #{key} has field value #{value.inspect}, this cannot include CR/LF"
         end
-        @header[key.downcase] = [value]
+        @header[key.to_str.downcase] = [value]
       end
     end
   end
@@ -36,14 +36,14 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L36
   # Returns the header field corresponding to the case-insensitive key.
   # For example, a key of "Content-Type" might return "text/html"
   def [](key)
-    a = @header[key.downcase] or return nil
+    a = @header[key.to_str.downcase] or return nil
     a.join(', ')
   end
 
   # Sets the header field corresponding to the case-insensitive key.
   def []=(key, val)
     unless val
-      @header.delete key.downcase
+      @header.delete key.to_str.downcase
       return val
     end
     set_field(key, val)
@@ -65,8 +65,9 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L65
   #   p request.get_fields('X-My-Header')   #=> ["a", "b", "c"]
   #
   def add_field(key, val)
-    if @header.key?(key.downcase)
-      append_field_value(@header[key.downcase], val)
+    stringified_downcased_key = key.to_str.downcase
+    if @header.key?(stringified_downcased_key)
+      append_field_value(@header[stringified_downcased_key], val)
     else
       set_field(key, val)
     end
@@ -77,13 +78,13 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L78
     when Enumerable
       ary = []
       append_field_value(ary, val)
-      @header[key.downcase] = ary
+      @header[key.to_str.downcase] = ary
     else
       val = val.to_s # for compatibility use to_s instead of to_str
       if val.b.count("\r\n") > 0
         raise ArgumentError, 'header field value cannot include CR/LF'
       end
-      @header[key.downcase] = [val]
+      @header[key.to_str.downcase] = [val]
     end
   end
 
@@ -112,8 +113,9 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L113
   #     #=> "session=al98axx; expires=Fri, 31-Dec-1999 23:58:23, query=rubyscript; expires=Fri, 31-Dec-1999 23:58:23"
   #
   def get_fields(key)
-    return nil unless @header[key.downcase]
-    @header[key.downcase].dup
+    stringified_downcased_key = key.to_str.downcase
+    return nil unless @header[stringified_downcased_key]
+    @header[stringified_downcased_key].dup
   end
 
   # Returns the header field corresponding to the case-insensitive key.
@@ -121,7 +123,7 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L123
   # raises an IndexError if there's no header field named +key+
   # See Hash#fetch
   def fetch(key, *args, &block)   #:yield: +key+
-    a = @header.fetch(key.downcase, *args, &block)
+    a = @header.fetch(key.to_str.downcase, *args, &block)
     a.kind_of?(Array) ? a.join(', ') : a
   end
 
@@ -182,12 +184,12 @@ module Net::HTTPHeader https://github.com/ruby/ruby/blob/trunk/lib/net/http/header.rb#L184
 
   # Removes a header field, specified by case-insensitive key.
   def delete(key)
-    @header.delete(key.downcase)
+    @header.delete(key.to_str.downcase)
   end
 
   # true if +key+ header exists.
   def key?(key)
-    @header.key?(key.downcase)
+    @header.key?(key.to_str.downcase)
   end
 
   # Returns a Hash consisting of header names and array of values.
Index: test/net/http/test_httpheader.rb
===================================================================
--- test/net/http/test_httpheader.rb	(revision 66318)
+++ test/net/http/test_httpheader.rb	(revision 66319)
@@ -31,6 +31,11 @@ class HTTPHeaderTest < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/net/http/test_httpheader.rb#L31
     assert_raise(ArgumentError){ @c.initialize_http_header("foo"=>"a\xff") }
   end
 
+  def test_initialize_with_symbol
+    @c.initialize_http_header(foo: "abc")
+    assert_equal "abc", @c["foo"]
+  end
+
   def test_size
     assert_equal 0, @c.size
     @c['a'] = 'a'

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]