From f065acbd63c64d58e810a96d1d1289cb677bae7f Mon Sep 17 00:00:00 2001 From: Yusei Yamanaka Date: Thu, 27 Aug 2015 18:16:40 +0900 Subject: [PATCH] Replace the parsing cookie logic instead of cookiejar gem --- Gemfile | 1 - Gemfile.lock | 5 +++-- lib/reverse_proxy/client.rb | 43 +++++++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 7e5012c..b618d4d 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,6 @@ gem 'rails' gem 'rack' gem 'rack-proxy' gem 'addressable' -gem 'cookiejar', '~> 0.3.2' # Add dependencies to develop your gem here. # Include everything needed to run rake, tests, features, etc. diff --git a/Gemfile.lock b/Gemfile.lock index ed4e7e3..2b1890a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,7 +39,6 @@ GEM addressable (2.3.8) arel (6.0.0) builder (3.2.2) - cookiejar (0.3.2) descendants_tracker (0.0.4) thread_safe (~> 0.3, >= 0.3.1) docile (1.1.5) @@ -149,7 +148,6 @@ PLATFORMS DEPENDENCIES addressable bundler (~> 1.0) - cookiejar (~> 0.3.2) jeweler (~> 2.0.1) rack rack-proxy @@ -157,3 +155,6 @@ DEPENDENCIES rdoc (~> 3.12) shoulda simplecov + +BUNDLED WITH + 1.10.6 diff --git a/lib/reverse_proxy/client.rb b/lib/reverse_proxy/client.rb index 1be21aa..0916da9 100644 --- a/lib/reverse_proxy/client.rb +++ b/lib/reverse_proxy/client.rb @@ -1,6 +1,5 @@ require 'rack' require 'rack-proxy' -require 'cookiejar' module ReverseProxy class Client @@ -92,11 +91,8 @@ module ReverseProxy set_cookies_hash = {} set_cookie_headers.each do |set_cookie_header| - set_cookie_hash = CookieJar::CookieValidation.parse_set_cookie(set_cookie_header) - set_cookie_hash[:value] = CGI.unescape(set_cookie_hash[:value]) - - name = set_cookie_hash.delete(:name) - + set_cookie_hash = parse_cookie(set_cookie_header) + name = set_cookie_hash[:name] set_cookies_hash[name] = set_cookie_hash end @@ -140,5 +136,40 @@ module ReverseProxy def reconstruct_header_name(name) name.sub(/^HTTP_/, "").gsub("_", "-") 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