Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print UTF-8 string from Haskell program for CGI?

I'm trying to make a CGI program with Haskell. (using Apache) But my program cannot print UTF-8 string correctly.

module  Main    where

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

Result checked via telnet is:

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$ 

What's the problem and what should I do to fix this?

PS. The program printed well on command-line console. And apache CGI built with shell script printed UTF-8 string well.

like image 265
eonil Avatar asked Dec 13 '25 14:12

eonil


1 Answers

To make sure putStr uses the right encoding you can call hSetEncoding on stdout to set it to "utf8".

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

like image 112
tibbe Avatar answered Dec 16 '25 08:12

tibbe