Replace the parsing cookie logic instead of cookiejar gem

This commit is contained in:
Yusei Yamanaka 2015-08-27 18:16:40 +09:00
parent 259ad9bc5a
commit f065acbd63
3 changed files with 40 additions and 9 deletions

View File

@ -4,7 +4,6 @@ gem 'rails'
gem 'rack' gem 'rack'
gem 'rack-proxy' gem 'rack-proxy'
gem 'addressable' gem 'addressable'
gem 'cookiejar', '~> 0.3.2'
# Add dependencies to develop your gem here. # Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc. # Include everything needed to run rake, tests, features, etc.

View File

@ -39,7 +39,6 @@ GEM
addressable (2.3.8) addressable (2.3.8)
arel (6.0.0) arel (6.0.0)
builder (3.2.2) builder (3.2.2)
cookiejar (0.3.2)
descendants_tracker (0.0.4) descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1) thread_safe (~> 0.3, >= 0.3.1)
docile (1.1.5) docile (1.1.5)
@ -149,7 +148,6 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
addressable addressable
bundler (~> 1.0) bundler (~> 1.0)
cookiejar (~> 0.3.2)
jeweler (~> 2.0.1) jeweler (~> 2.0.1)
rack rack
rack-proxy rack-proxy
@ -157,3 +155,6 @@ DEPENDENCIES
rdoc (~> 3.12) rdoc (~> 3.12)
shoulda shoulda
simplecov simplecov
BUNDLED WITH
1.10.6

View File

@ -1,6 +1,5 @@
require 'rack' require 'rack'
require 'rack-proxy' require 'rack-proxy'
require 'cookiejar'
module ReverseProxy module ReverseProxy
class Client class Client
@ -92,11 +91,8 @@ module ReverseProxy
set_cookies_hash = {} set_cookies_hash = {}
set_cookie_headers.each do |set_cookie_header| set_cookie_headers.each do |set_cookie_header|
set_cookie_hash = CookieJar::CookieValidation.parse_set_cookie(set_cookie_header) set_cookie_hash = parse_cookie(set_cookie_header)
set_cookie_hash[:value] = CGI.unescape(set_cookie_hash[:value]) name = set_cookie_hash[:name]
name = set_cookie_hash.delete(:name)
set_cookies_hash[name] = set_cookie_hash set_cookies_hash[name] = set_cookie_hash
end end
@ -140,5 +136,40 @@ module ReverseProxy
def reconstruct_header_name(name) def reconstruct_header_name(name)
name.sub(/^HTTP_/, "").gsub("_", "-") name.sub(/^HTTP_/, "").gsub("_", "-")
end end
COOKIE_PARAM_PATTERN = /\A([^(),\/<>@;:\\\"\[\]?={}\s]+)(?:=([^;]*))?\Z/
COOKIE_SPLIT_PATTERN = /;\s*/
def parse_cookie(cookie_str)
params = cookie_str.split(COOKIE_SPLIT_PATTERN)
info = params.shift.match(COOKIE_PARAM_PATTERN)
return {} unless info
cookie = {
name: info[1],
value: CGI.unescape(info[2]),
}
params.each do |param|
result = param.match(COOKIE_PARAM_PATTERN)
next unless result
key = result[1].downcase.to_sym
value = result[2]
case key
when :expires
begin
cookie[:expires] = Time.parse(value)
rescue ArgumentError
end
when *[:httponly, :secure]
cookie[key] = true
else
cookie[key] = value
end
end
cookie
end
end end
end end