From: Wakou Aoyama Date: 2000-05-24T14:41:22+09:00 Subject: [ruby-list:22877] Re: CGI::escape 青山です。 On Wed, 24 May 2000 01:08:00 +0900, Ryunosuke Ohshima wrote: > cgi.rbのCGI::escapeは空白(" ")を"+"に置き換えてからescapeしてしまう > ので、unescapeしても空白に戻りません。 > > RFC2616(HTTP/1.1)、RFC2396(URI)を読んだ限りでは空白を特別扱いして"+" > に置き換える必要はなさそう("%20"へのescapeで良い)と思われます。 > (大抵のブラウザは空白を"+"に置き換えていますが。) 大抵のというか、form field のエンコードに関する定義が RFC 1866 以降で 置き換えられていない限りは、そのようになります。 RFC 1866 > 1. The form field names and values are escaped: space > characters are replaced by `+', and then reserved characters > are escaped as per [URL]; that is, non-alphanumeric > characters are replaced by `%HH', a percent sign and two > hexadecimal digits representing the ASCII code of the > character. Line breaks, as in multi-line text field values, > are represented as CR LF pairs, i.e. `%0D%0A'. とはいえ、二重に処理してしまっていたのは確かに問題でした。こんな感じで しょうかね。 def CGI::escape(string) string.gsub(/([^a-zA-Z0-9_.-])/n) do if " " == $1 "+" else sprintf("%%%02X", $1.unpack("C")[0]) end end end また、Cookie の方は () 忘れですね。これではまともに機能していませんでした。 ありがとうございます。 -- 青山 和光 Wakou Aoyama