/tools/Ruby/lib/ruby/1.8/webrick/cookie.rb

http://github.com/agross/netopenspace · Ruby · 110 lines · 87 code · 11 blank · 12 comment · 13 complexity · 8624655c3e12df1714d278bd10ed1cd1 MD5 · raw file

  1. #
  2. # cookie.rb -- Cookie class
  3. #
  4. # Author: IPR -- Internet Programming with Ruby -- writers
  5. # Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
  6. # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
  7. # reserved.
  8. #
  9. # $IPR: cookie.rb,v 1.16 2002/09/21 12:23:35 gotoyuzo Exp $
  10. require 'time'
  11. require 'webrick/httputils'
  12. module WEBrick
  13. class Cookie
  14. attr_reader :name
  15. attr_accessor :value, :version
  16. attr_accessor :domain, :path, :secure
  17. attr_accessor :comment, :max_age
  18. #attr_accessor :comment_url, :discard, :port
  19. def initialize(name, value)
  20. @name = name
  21. @value = value
  22. @version = 0 # Netscape Cookie
  23. @domain = @path = @secure = @comment = @max_age =
  24. @expires = @comment_url = @discard = @port = nil
  25. end
  26. def expires=(t)
  27. @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s)
  28. end
  29. def expires
  30. @expires && Time.parse(@expires)
  31. end
  32. def to_s
  33. ret = ""
  34. ret << @name << "=" << @value
  35. ret << "; " << "Version=" << @version.to_s if @version > 0
  36. ret << "; " << "Domain=" << @domain if @domain
  37. ret << "; " << "Expires=" << @expires if @expires
  38. ret << "; " << "Max-Age=" << @max_age.to_s if @max_age
  39. ret << "; " << "Comment=" << @comment if @comment
  40. ret << "; " << "Path=" << @path if @path
  41. ret << "; " << "Secure" if @secure
  42. ret
  43. end
  44. # Cookie::parse()
  45. # It parses Cookie field sent from the user agent.
  46. def self.parse(str)
  47. if str
  48. ret = []
  49. cookie = nil
  50. ver = 0
  51. str.split(/[;,]\s+/).each{|x|
  52. key, val = x.split(/=/,2)
  53. val = val ? HTTPUtils::dequote(val) : ""
  54. case key
  55. when "$Version"; ver = val.to_i
  56. when "$Path"; cookie.path = val
  57. when "$Domain"; cookie.domain = val
  58. when "$Port"; cookie.port = val
  59. else
  60. ret << cookie if cookie
  61. cookie = self.new(key, val)
  62. cookie.version = ver
  63. end
  64. }
  65. ret << cookie if cookie
  66. ret
  67. end
  68. end
  69. def self.parse_set_cookie(str)
  70. cookie_elem = str.split(/;/)
  71. first_elem = cookie_elem.shift
  72. first_elem.strip!
  73. key, value = first_elem.split(/=/, 2)
  74. cookie = new(key, HTTPUtils.dequote(value))
  75. cookie_elem.each{|pair|
  76. pair.strip!
  77. key, value = pair.split(/=/, 2)
  78. if value
  79. value = HTTPUtils.dequote(value.strip)
  80. end
  81. case key.downcase
  82. when "domain" then cookie.domain = value
  83. when "path" then cookie.path = value
  84. when "expires" then cookie.expires = value
  85. when "max-age" then cookie.max_age = Integer(value)
  86. when "comment" then cookie.comment = value
  87. when "version" then cookie.version = Integer(value)
  88. when "secure" then cookie.secure = true
  89. end
  90. }
  91. return cookie
  92. end
  93. def self.parse_set_cookies(str)
  94. return str.split(/,(?=[^;,]*=)|,$/).collect{|c|
  95. parse_set_cookie(c)
  96. }
  97. end
  98. end
  99. end