divive HTTP::Cookie

master
Kanezoh 2021-08-09 06:19:44 +09:00
parent 51480129d0
commit 031f112eb3
2 changed files with 39 additions and 37 deletions

38
src/mechanize/cookie.cr Normal file
View File

@ -0,0 +1,38 @@
# TODO: want to add methods with safe way like Ruby's refinement.
# open HTTP::Cookie class to add origin property.
# origin property represents the origin of the resource.
# if cookie's domain attribute isn't designated,
# this property is used to send cookies to same origin resource.
class ::HTTP::Cookie
property origin : String?
def initialize(name : String, value : String, @path : String? = nil,
@expires : Time? = nil, @domain : String? = nil,
@secure : Bool = false, @http_only : Bool = false,
@samesite : SameSite? = nil, @extension : String? = nil,
@origin : String? = nil)
validate_name(name)
@name = name
validate_value(value)
@value = value
@origin = origin
end
def valid_cookie?(uri)
host = uri.host
if path
bool = uri.path.try &.=~(/^#{path}.*/)
return false if bool.nil?
end
if secure
return false if uri.scheme == "http"
end
if domain
host.try &.=~(/.*#{domain.try &.gsub(".", "\.")}$/)
else
origin == host
end
end
end

View File

@ -1,5 +1,6 @@
require "uri"
require "http/client"
require "../cookie"
module MechanizeCr
module HTTP
@ -138,40 +139,3 @@ module MechanizeCr
end
end
end
# open HTTP::Cookie class to add origin property.
# origin property represents the origin of the resource.
# if cookie's domain attribute isn't designated,
# this property is used to send cookies to same origin resource.
class HTTP::Cookie
property origin : String?
def initialize(name : String, value : String, @path : String? = nil,
@expires : Time? = nil, @domain : String? = nil,
@secure : Bool = false, @http_only : Bool = false,
@samesite : SameSite? = nil, @extension : String? = nil,
@origin : String? = nil)
validate_name(name)
@name = name
validate_value(value)
@value = value
@origin = origin
end
def valid_cookie?(uri)
host = uri.host
if path
bool = uri.path.try &.=~(/^#{path}.*/)
return false if bool.nil?
end
if secure
return false if uri.scheme == "http"
end
if domain
host.try &.=~(/.*#{domain.try &.gsub(".", "\.")}$/)
else
origin == host
end
end
end