From 9b920a0b734b8aba37d5cb147ea7bb1b13c5ef11 Mon Sep 17 00:00:00 2001 From: bjorn Date: Fri, 17 Oct 2008 10:49:34 +0000 Subject: [PATCH] Added JSON-P support to fastcgi server. --- src/server/MainFastCGI.hs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/server/MainFastCGI.hs b/src/server/MainFastCGI.hs index b36014840..059b26bb9 100644 --- a/src/server/MainFastCGI.hs +++ b/src/server/MainFastCGI.hs @@ -39,7 +39,7 @@ cgiMain pgf = "/translate" -> return (doTranslate pgf) `ap` getText `ap` getCat `ap` getFrom `ap` getTo "/grammar" -> return (doGrammar pgf) `ap` requestAcceptLanguage _ -> throwCGIError 404 "Not Found" ["Resource not found: " ++ path] - outputJSON json + outputJSONP json where getText :: CGI String getText = liftM (maybe "" (urlDecodeUnicode . UTF8.decodeString)) $ getInput "input" @@ -148,9 +148,14 @@ langCodeLanguage pgf code = listToMaybe [l | l <- PGF.languages pgf, PGF.languag -- * General CGI and JSON stuff -outputJSON :: JSON a => a -> CGI CGIResult -outputJSON x = do setHeader "Content-Type" "text/json; charset=utf-8" - outputStrict $ UTF8.encodeString $ encode x +outputJSONP :: JSON a => a -> CGI CGIResult +outputJSONP x = + do mc <- getInput "jsonp" + let str = case mc of + Nothing -> encode x + Just c -> c ++ "(" ++ encode x ++ ")" + setHeader "Content-Type" "text/json; charset=utf-8" + outputStrict $ UTF8.encodeString str outputStrict :: String -> CGI CGIResult outputStrict x | x == x = output x