Clozure CL + HunchentootのPOST時の文字化け対策

Clozure CL(Linux amd64)上でHunchentootを使用したときに、少しハマったので、メモしておく。

現象

POSTのパラメータに日本語を含めると、日本語部分だけが丸ごと消えてしまう。

原因

URLデコード処理のせいかもしれないと思って調べたら、やっぱりそうだった。

Clozure CL
 ? (hunchentoot::url-encode "あ" :utf-8)
"%C3%A3%C2%81%C2%82"  ; あれ?UTF-8じゃないぞ。
 ? (hunchentoot::form-url-encoded-list-to-alist '("abc=%C3%A3%C2%81%C2%82") :utf-8)
(("abc" . "あ"))
 ? (hunchentoot::form-url-encoded-list-to-alist '("abc=%E3%81%82") :utf-8)
(("abc" . ""))        ; 正しいUTF-8のコードはうまくデコードできない模様。
 ? (hunchentoot::form-url-encoded-list-to-alist '("abc=%E3%81%82") :latin-1)
(("abc" . "あ"))      ; external-formatをlatin-1にするとなぜかうまくいった。
SBCLだと正しく変換される
 * (hunchentoot::url-encode "あ" :utf-8)
"%E3%81%82"
 * (hunchentoot::form-url-encoded-list-to-alist '("abc=%E3%81%82") :utf-8)
(("abc" . "あ"))

対処

ハンドラの先頭で、external-format=latin-1でリクエストを再計算してやると、うまく取れる模様。

(defun some-handler ()
  #+ccl
  (progn
    (hunchentoot:no-cache)
    (hunchentoot:recompute-request-parameters :external-format :latin-1))
  ... ;以下、普通のハンドラの処理。
  )