Skip to content

Conversation

@lacostej
Copy link

@lacostej lacostej commented Apr 2, 2016

We keep track of the original quoted value when it is already quoted,
and we avoid consider values to be quoted if they start with a quote.

We keep track of the original quoted value when it is already quoted,
and we avoid consider values to be quoted if they start with a quote.
@lacostej
Copy link
Author

lacostej commented Apr 2, 2016

to_yaml still loses the quoting. I can easily fix it under the encode_with method but have no idea how to make it work with the to_yaml_properties (which is there for ruby 1.8 compatibility, right ?) as it doesn't allow me to control how the properties are mapped

update I can solve it in both cases if I also expose the new @raw_value field

lacostej added 2 commits April 3, 2016 10:25
…t field initialization time, which could cause unexepected results on ruby 1.8.7
@knu
Copy link
Member

knu commented Dec 9, 2016

This may be a necessary change, as I observed that most popular browsers do not consider double quotes at all.

@tylerj
Copy link

tylerj commented Jun 7, 2019

I realize this PR is old, but in case it is still being considered: would it make more sense to modify the HTTP::Cookie::Scanner.quote method to return the original value, if it matches the /^"(.*)"$/ regex?

FWIW, the way I patched it in my project is:

module HTTP
  class Cookie
    class Scanner
      # https://github.com/sparklemotion/http-cookie/blob/master/lib/http/cookie/scanner.rb#L24
      def self.quote(str)
        return str unless str.match(RE_BAD_CHAR)

        # Because double quotes are being added to the beginning and end of the return value,
        # let's go ahead and remove them if the string already begins AND ends in double quotes
        unquoted = str.sub(/^[\\"](.*)[\\"]$/, '\\1')
        '"' + unquoted.gsub(/([\\"])/, '\\\\\\1') + '"'
      end
    end
  end
end

@olegykz
Copy link

olegykz commented Oct 21, 2021

This is how it works for me, mixed with @tylerj 's snippet

module RespectDoubleQuotedCookieValue
  def self.prepended(klass)
    klass.singleton_class.prepend(ClassMethods)
  end

  module ClassMethods
    def quote(str)
      return str unless str.match(HTTP::Cookie::Scanner::RE_BAD_CHAR)

      # Because double quotes are being added to the beginning and end of the return value,
      # let's go ahead and remove them if the string already begins AND ends in double quotes
      unquoted = str.sub(/^[\\"](.*)[\\"]$/, '\\1')
      "\"#{unquoted.gsub(/([\\"])/, '\\\\\\1')}\""
    end
  end

  def scan_dquoted
    unquoted =
      [].tap do |s|
        case # rubocop:disable Style/EmptyCaseCondition
        when skip(/"/)
          break
        when skip(/\\/)
          s << getch
        when scan(/[^"\\]+/)
          s << matched
        end until eos?
      end.join

    "\"#{unquoted}\""
  end
end

HTTP::Cookie::Scanner.prepend(RespectDoubleQuotedCookieValue)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants