2008-10-11
Apache Tomcat 5.5.26仕様変更対応(案)
イコール等を含むクッキーを使用しているWebアプリケーションはTomcat 5.5.26以降で正常に動作しないようになります。改修可能な場合はまだマシなのですが、改修不可能なパッケージ等の場合は深刻な影響が考えられます。
http://d.hatena.ne.jp/int128/20081008/1223480443
上記に対応するApache Tomcatモジュールを作ってみました。このモジュールはWebアプリケーションの処理前にHTTPヘッダを書き換える処理を行います。
public class TomcatCookieHeaderValve extends ValveBase { private Logger logger = Logger.getLogger(TomcatCookieHeaderValve.class.getName()); public void invoke(Request req, Response res) throws IOException, ServletException { String requestCookie = req.getHeader("Cookie"); if(requestCookie != null) { req.clearCookies(); for(String part : requestCookie.split("; *")) { String key = part.substring(0, part.indexOf('=')); String value = part.substring(part.indexOf('=')+1); Cookie cookie = new Cookie(key, value); req.addCookie(cookie); logger.info("Original request Cookie: " + requestCookie); logger.info("Fixed request Cookie: " + key + "=" + value); } } getNext().invoke(req, res); String responseCookie = res.getHeader("Set-Cookie"); if(responseCookie != null) { String fixed = responseCookie.replace("\"", ""); res.setHeader("Set-Cookie", fixed); logger.info("Original response Cookie: " + responseCookie); logger.info("Fixed response Cookie: " + fixed); } } }http://coderepos.org/share/browser/lang/java/misc/http-cookie-parser/trunk/src/org/hidetake/sandbox/TomcatCookieHeaderValve.java
プロジェクト全体をチェックアウトすると動作確認できます。server.xmlには下記を追記してください。
<Server port="8005" shutdown="SHUTDOWN"> <Service name="Catalina"> <Engine defaultHost="localhost" name="Catalina"> <!-- 追記 --> <Valve className="org.hidetake.sandbox.TomcatCookieHeaderValve"/> </Engine> </Service> </Server>
このモジュールは Valve を実装しているのですが、気になる一文が。
A Valve MUST NOT do any of the following things:
http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/Valve.html#invoke%28org.apache.catalina.connector.Request,%20org.apache.catalina.connector.Response%29
- Change request properties that have already been used to direct the flow of processing control for this request (for instance, trying to change the virtual host to which a Request should be sent from a pipeline attached to a Host or Context in the standard implementation).
- Create a completed Response AND pass this Request and Response on to the next Valve in the pipeline.
- Consume bytes from the input stream associated with the Request, unless it is completely generating the response, or wrapping the request before passing it on.
- Modify the HTTP headers included with the Response after the invokeNext() method has returned.
- Perform any actions on the output stream associated with the specified Response after the invokeNext() method has returned.
invoke()の中で、Webアプリケーション処理後のHTTPヘッダを書き換えてはいけないと書いてあります。現状は動いてるけどルール違反だから無理なのかな。根の深い問題ですね。
トラックバック - http://d.hatena.ne.jp/int128/20081011/1223656778
リンク元
- 45 http://secure.ddo.jp/~kaku/tdiary/
- 31 http://reader.livedoor.com/reader/
- 30 http://www.tdtsh.com/blog/archives/386
- 26 http://webcache.googleusercontent.com/search?q=cache:LaGxVNCsGJcJ:d.hatena.ne.jp/int128/20081008/1223480443+java+cookie+ダブルクォート&cd=1&hl=ja&ct=clnk&gl=jp&client=firefox-a
- 11 http://b.hatena.ne.jp/hotentry?mode=daily&date=20081009
- 8 http://www.google.co.jp/search?sourceid=navclient&hl=ja&ie=UTF-8&rlz=1T4ADBF_jaJP293JP293&q=保守性 評価
- 7 http://b.hatena.ne.jp/add?mode=confirm&title=Apache Tomcat%u3068HTTP%u30AF%u30C3%u30AD%u30FC%u306B%u307E%u3064%u308F%u308B%u9A12%u52D5 - int128 Geek Not Found&url=http://d.hatena.ne.jp/int128/20081008/1223480443
- 5 http://d.hatena.ne.jp/
- 5 http://www.google.co.jp/reader/view/
- 4 http://www.google.co.jp/search?hl=ja&lr=lang_ja&tbs=lr:lang_1ja&q=tomcat+ヘッダ+書き換え&aq=f&aqi=&aql=&oq=
