Sha256: f20b861c65dd71becb435d46051a69ec78de1802fb15f80f3ec9ac0852f2514a

Contents?: true

Size: 1.14 KB

Versions: 27

Compression:

Stored size: 1.14 KB

Contents

#This class holds various methods for encoding, decoding and parsing of HTTP-related stuff.
class Http2::Utils
  #URL-encodes a string.
  def self.urlenc(string)
    #Thanks to CGI framework
    string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/) do
      '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
    end.tr(' ', '+')
  end
  
  #URL-decodes a string.
  def self.urldec(string)
    #Thanks to CGI framework
    str = string.to_s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) do
      [$1.delete('%')].pack('H*')
    end
  end
  
  #Parses a cookies-string and returns them in an array.
  def self.parse_set_cookies(str)
    str = String.new(str.to_s)
    return [] if str.length <= 0
    args = {}
    cookie_start_regex = /^(.+?)=(.*?)(;\s*|$)/
    
    match = str.match(cookie_start_regex)
    raise "Could not match cookie: '#{str}'." if !match
    str.gsub!(cookie_start_regex, "")
    
    args["name"] = self.urldec(match[1].to_s)
    args["value"] = self.urldec(match[2].to_s)
    
    while match = str.match(/(.+?)=(.*?)(;\s*|$)/)
      str = str.gsub(match[0], "")
      args[match[1].to_s.downcase] = match[2].to_s
    end
    
    return [args]
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
http2-0.0.28 include/utils.rb
http2-0.0.27 include/utils.rb
http2-0.0.26 include/utils.rb
http2-0.0.25 include/utils.rb
http2-0.0.24 include/utils.rb
http2-0.0.23 include/utils.rb
http2-0.0.22 include/utils.rb
http2-0.0.21 include/utils.rb
http2-0.0.20 include/utils.rb
http2-0.0.19 include/utils.rb
http2-0.0.18 include/utils.rb
http2-0.0.17 include/utils.rb
http2-0.0.16 include/utils.rb
http2-0.0.15 include/utils.rb
http2-0.0.14 include/utils.rb
http2-0.0.13 include/utils.rb
http2-0.0.12 include/utils.rb
http2-0.0.11 include/utils.rb
http2-0.0.10 include/utils.rb
http2-0.0.9 include/utils.rb