如何从Haskell程序中为CGI打印UTF-8字符串?

时间:2011-03-07 07:33:40

标签: apache haskell utf-8 cgi

我正在尝试用Haskell制作一个CGI程序。 (使用Apache) 但我的程序无法正确打印UTF-8字符串。

module  Main    where

main    ::  IO()
main    =   do
        {
            putStr("Content-type: text/plain; charset=utf-8\n\n");
            putStr("English한글日本語abc");
        }

通过telnet检查的结果是:

hhmm:appserve Eonil$ telnet localhost 80
Trying ::1...
Connected to localhost.
Escape character is '^]'.
GET http://localhost/cgi-bin/test HTTP\1.0

HTTP/1.1 200 OK
Date: Mon, 07 Mar 2011 07:31:28 GMT
Server: Apache/2.2.15 (Unix) mod_ssl/2.2.15 OpenSSL/0.9.8l DAV/2
Connection: close
Content-Type: text/plain; charset=utf-8

EnglishConnection closed by foreign host.
hhmm:appserve Eonil$ 

问题是什么,我该怎么做才能解决这个问题?

PS。该程序在命令行控制台上打印得很好。用shell脚本构建的apache CGI很好地打印了UTF-8字符串。

1 个答案:

答案 0 :(得分:2)

为确保putStr使用正确的编码,您可以致电hSetEncoding上的stdout将其设为"utf8"

http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.3.1.0/System-IO.html#g:23