Skip to content

Instantly share code, notes, and snippets.

@rust
Created July 15, 2010 19:52
Show Gist options
  • Save rust/477436 to your computer and use it in GitHub Desktop.
Save rust/477436 to your computer and use it in GitHub Desktop.
diff --git a/lib/jpmobile/hook_action_controller.rb b/lib/jpmobile/hook_action_controller.rb
index cca89d1..59d89f1 100644
--- a/lib/jpmobile/hook_action_controller.rb
+++ b/lib/jpmobile/hook_action_controller.rb
@@ -29,3 +29,26 @@ module ActionController
end
ActionController::Request.send :include, Jpmobile::Encoding
+
+module ActionDispatch
+ module Routing
+ module UrlFor
+ def url_for_with_jpmobile(options = nil)
+ if respond_to?(:request)
+ case options
+ when Hash
+ options.keys.each do |key|
+ if options[key].kind_of?(String)
+ options[key] = request.mobile.to_external_encoding(options[key])
+ end
+ end
+ end
+ end
+
+ url_for_without_jpmobile(options)
+ end
+
+ alias_method_chain :url_for, :jpmobile
+ end
+ end
+end
diff --git a/lib/jpmobile/mobile/abstract_mobile.rb b/lib/jpmobile/mobile/abstract_mobile.rb
index 926618a..8ce5383 100644
--- a/lib/jpmobile/mobile/abstract_mobile.rb
+++ b/lib/jpmobile/mobile/abstract_mobile.rb
@@ -53,6 +53,12 @@ module Jpmobile::Mobile
end
# エンコーディング変換用
+ def to_internal_encoding(str)
+ str
+ end
+ def to_external_encoding(str)
+ str
+ end
def to_internal(str)
str
end
diff --git a/lib/jpmobile/mobile/au.rb b/lib/jpmobile/mobile/au.rb
index be56578..3575c47 100644
--- a/lib/jpmobile/mobile/au.rb
+++ b/lib/jpmobile/mobile/au.rb
@@ -101,11 +101,18 @@ module Jpmobile::Mobile
end
# 文字コード変換
+ def to_internal_encoding(str)
+ # 文字コードを UTF-8 に変換
+ str = Jpmobile::Util.sjis_to_utf8(str)
+ end
+ def to_external_encoding(str)
+ str = Jpmobile::Util.utf8_to_sjis(str)
+ end
def to_internal(str)
# 絵文字を数値参照に変換
str = Jpmobile::Emoticon.external_to_unicodecr_au(Jpmobile::Util.sjis(str))
# 文字コードを UTF-8 に変換
- str = Jpmobile::Util.sjis_to_utf8(str)
+ str = to_internal_encoding(str)
# 数値参照を UTF-8 に変換
Jpmobile::Emoticon::unicodecr_to_utf8(str)
# 半角->全角変換
@@ -115,7 +122,7 @@ module Jpmobile::Mobile
str = Jpmobile::Emoticon.utf8_to_unicodecr(str)
# 文字コードを Shift_JIS に変換
if [nil, "text/html", "application/xhtml+xml"].include?(content_type)
- str = Jpmobile::Util.utf8_to_sjis(str)
+ str = to_external_encoding(str)
charset = default_charset unless str.empty?
end
# 数値参照を絵文字コードに変換
diff --git a/lib/jpmobile/mobile/docomo.rb b/lib/jpmobile/mobile/docomo.rb
index 894bdc6..feb612e 100644
--- a/lib/jpmobile/mobile/docomo.rb
+++ b/lib/jpmobile/mobile/docomo.rb
@@ -81,11 +81,18 @@ module Jpmobile::Mobile
end
# 文字コード変換
+ def to_internal_encoding(str)
+ # 文字コードを UTF-8 に変換
+ str = Jpmobile::Util.sjis_to_utf8(str)
+ end
+ def to_external_encoding(str)
+ str = Jpmobile::Util.utf8_to_sjis(str)
+ end
def to_internal(str)
# 絵文字を数値参照に変換
str = Jpmobile::Emoticon.external_to_unicodecr_docomo(Jpmobile::Util.sjis(str))
# 文字コードを UTF-8 に変換
- str = Jpmobile::Util.sjis_to_utf8(str)
+ str = to_internal_encoding(str)
# 数値参照を UTF-8 に変換
Jpmobile::Emoticon.unicodecr_to_utf8(str)
end
@@ -94,7 +101,7 @@ module Jpmobile::Mobile
str = Jpmobile::Emoticon.utf8_to_unicodecr(str)
# 文字コードを Shift_JIS に変換
if [nil, "text/html", "application/xhtml+xml"].include?(content_type)
- str = Jpmobile::Util.utf8_to_sjis(str)
+ str = to_external_encoding(str)
charset = default_charset unless str.empty?
end
# 数値参照を絵文字コードに変換
diff --git a/lib/jpmobile/util.rb b/lib/jpmobile/util.rb
index 5f9a124..7b07154 100644
--- a/lib/jpmobile/util.rb
+++ b/lib/jpmobile/util.rb
@@ -65,7 +65,12 @@ module Jpmobile
def utf8_to_sjis(utf8_str)
if utf8_str.respond_to?(:encode)
- utf8_str.encode("Shift_JIS")
+ if utf8_str.encoding == ::Encoding::Shift_JIS or
+ utf8_str.encoding != ::Encoding::UTF_8
+ utf8_str
+ else
+ utf8_str.encode("Shift_JIS")
+ end
else
NKF.nkf("-m0 -x -Ws", utf8_str)
end
@@ -73,7 +78,12 @@ module Jpmobile
def sjis_to_utf8(sjis_str)
if sjis_str.respond_to?(:encode)
- sjis_str.encode("UTF-8")
+ if sjis_str.encoding == ::Encoding::UTF_8 or
+ sjis_str.encoding != ::Encoding::Shift_JIS
+ sjis_str
+ else
+ sjis_str.encode("UTF-8")
+ end
else
NKF.nkf("-m0 -x -Sw", sjis_str)
end
diff --git a/lib/tasks/jpmobile_tasks.rake b/lib/tasks/jpmobile_tasks.rake
index 72f6ab1..acd6b2e 100644
--- a/lib/tasks/jpmobile_tasks.rake
+++ b/lib/tasks/jpmobile_tasks.rake
@@ -82,6 +82,7 @@ END
cd rails_root
ruby "-S rake db:migrate test"
ruby "-S rake spec"
+ # ruby "-S rspec -b --color -e 'jpmobile integration spec HankakuFilterController DoCoMo SH902i からのアクセス は半角Shift_JISで渡されたパラメタがparamsに全角UTF-8に変換されて格納されること' spec/requests/filter_spec.rb"
cd relative_root
end
diff --git a/test/rails/overrides/app/controllers/filter_controller_base.rb b/test/rails/overrides/app/controllers/filter_controller_base.rb
index 2565188..89a0ae2 100644
--- a/test/rails/overrides/app/controllers/filter_controller_base.rb
+++ b/test/rails/overrides/app/controllers/filter_controller_base.rb
@@ -16,4 +16,7 @@ class FilterControllerBase < ApplicationController
def rawdata
send_data "アブラカダブラ", :type => 'application/octet-stream'
end
+ def urlfor
+ render :text => url_for(:action => :index, :q => "パラメータ")
+ end
end
diff --git a/test/rails/overrides/spec/requests/filter_spec.rb b/test/rails/overrides/spec/requests/filter_spec.rb
index c7c3cae..e241562 100644
--- a/test/rails/overrides/spec/requests/filter_spec.rb
+++ b/test/rails/overrides/spec/requests/filter_spec.rb
@@ -168,4 +168,11 @@ describe "jpmobile integration spec" do
end
it_should_behave_like "UTF-8で通信する端末との通信(半角変換付き)"
end
+
+ context "url_for で" do
+ it "UTF-8 文字列をパラメータの値として指定したときもエンコーディング変換されてエスケープされること" do
+ get "/filter/urlfor", {}, {"HTTP_USER_AGENT" => "DoCoMo/2.0 SH902i(c100;TB;W24H12)"}
+ body.should match(CGI.escape(utf8_to_sjis("パラメータ")))
+ end
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment