ZShogi に If-Modified-Since
2007年9月9日23:22
zope
ZShogi-0.5.1, PyShogi-0.5.5

ZShogi-0.5.1 で HTTP ヘッダ If-Modified-Since を受け取った場合の対応をしてみました。
今まで局面を動的に返すことしか考慮してなかったのですが、
クライアントから棋譜全体を静的に get することもあるので。
以下のようなコードを追加。

# ZShogi.py
...
from App.Common import rfc1123_date
...
def index_html(self, REQUEST=None):
    if REQUEST is not None:
        last = REQUEST.get('HTTP_IF_MODIFIED_SINCE', '')
        mtime = rfc1123_date(self.bobobase_modification_time())
        # 棋譜が更新されてない場合は HTTP ステータス 304 を返す
        if mtime == last:
            REQUEST.RESPONSE.setStatus(304) # Not Modified
            return ''
        # 棋譜データを返す
        _set_header = REQUEST.RESPONSE.setHeader
        _set_header('last-modified', mtime)
        _set_header('X-Zope-Product', 'ZShogi-%s' % __version__)
        _set_header('Content-Type', 'application/xml')
        ...

telnet を使って確認すると、
一回目のアクセスでは Last-Modified が返ってきます。

$ telnet owa.as.wakwak.ne.jp 80
...
Escape character is '^]'.
GET /zope/shogi/Games/Meijin/M63_1 HTTP/1.0

HTTP/1.1 200 OK
Date: Sun, 09 Sep 2007 13:56:27 GMT
Server: Zope/(Zope 2.9.7-final, python 2.4.4, linux2) ZServer/1.1
Last-Modified: Sat, 11 Jun 2005 11:39:15 GMT
Content-Length: 814
Content-Type: application/xml; charset=utf-8
X-Zope-Product: ZShogi-0.5.1.0
Connection: close

<?xml version="1.0" encoding="utf-8" ?>
<ZShogi version="0.5">
...
</ZShogi>

二回目のリクエストで If-Modified-Since をそのままの文字列で設定してみると、
ちゃんとステータス 304 を返します。

$ telnet owa.as.wakwak.ne.jp 80
...
Escape character is '^]'.
GET /zope/shogi/Games/Meijin/M63_1 HTTP/1.0
If-Modified-Since: Sat, 11 Jun 2005 11:39:15 GMT

HTTP/1.1 304 Not Modified
Date: Sun, 09 Sep 2007 13:58:25 GMT
Server: Zope/(Zope 2.9.7-final, python 2.4.4, linux2) ZServer/1.1
Connection: close

と云うことでクライアント側の ZBrowser も If-Modified-Since
を発行するように修正しました。

これで竜王戦を怒られずに観戦できるかなー;;


コメントはありません