Cloud & PGF service: use Content-Type application/json for JSON output

This is in accordance with RFC 4627. http://tools.ietf.org/html/rfc4627
Use application/javascript for JSONP output.
This commit is contained in:
hallgren
2013-07-29 16:05:54 +00:00
parent a675b50423
commit fb61fc4562
2 changed files with 24 additions and 22 deletions

View File

@@ -324,7 +324,7 @@ serveStaticFile' path =
if ext `elem` [".cgi",".fcgi",".sh",".php"] if ext `elem` [".cgi",".fcgi",".sh",".php"]
then return $ resp400 $ "Unsupported file type: "++ext then return $ resp400 $ "Unsupported file type: "++ext
else do b <- doesFileExist path else do b <- doesFileExist path
if b then fmap (ok200' (ct t)) $ rdFile path if b then fmap (ok200' (ct t "")) $ rdFile path
else do cwd <- getCurrentDirectory else do cwd <- getCurrentDirectory
logPutStrLn $ "Not found: "++path++" cwd="++cwd logPutStrLn $ "Not found: "++path++" cwd="++cwd
return (resp404 path) return (resp404 path)
@@ -334,15 +334,16 @@ logPutStrLn s = liftIO . hPutStrLn stderr $ s
-- * JSONP output -- * JSONP output
jsonp qs = json200' $ maybe id apply (lookup "jsonp" qs) jsonp qs = maybe json200 apply (lookup "jsonp" qs)
where where
apply f json = f++"("++json++")" apply f = jsonp200' $ \ json -> f++"("++json++")"
-- * Standard HTTP responses -- * Standard HTTP responses
ok200 = Response 200 [plainUTF8,noCache,xo] . encodeString ok200 = Response 200 [plainUTF8,noCache,xo] . encodeString
ok200' t = Response 200 [t,xo] ok200' t = Response 200 [t,xo]
json200 x = json200' id x json200 x = json200' id x
json200' f = ok200' jsonUTF8 . encodeString . f . encode json200' f = ok200' jsonUTF8 . encodeString . f . encode
jsonp200' f = ok200' jsonpUTF8 . encodeString . f . encode
html200 = ok200' htmlUTF8 . encodeString html200 = ok200' htmlUTF8 . encodeString
resp204 = Response 204 [xo] "" -- no content resp204 = Response 204 [xo] "" -- no content
resp400 msg = Response 400 [plain,xo] $ "Bad request: "++msg++"\n" resp400 msg = Response 400 [plain,xo] $ "Bad request: "++msg++"\n"
@@ -355,11 +356,14 @@ instance Error Response where
strMsg = resp500 strMsg = resp500
-- * Content types -- * Content types
plain = ct "text/plain" plain = ct "text/plain" ""
plainUTF8 = ct "text/plain; charset=UTF-8" plainUTF8 = ct "text/plain" csutf8
jsonUTF8 = ct "text/javascript; charset=UTF-8" jsonUTF8 = ct "application/json" csutf8 -- http://www.ietf.org/rfc/rfc4627.txt
htmlUTF8 = ct "text/html; charset=UTF-8" jsonpUTF8 = ct "application/javascript" csutf8
ct t = ("Content-Type",t) htmlUTF8 = ct "text/html" csutf8
ct t cs = ("Content-Type",t++cs)
csutf8 = "; charset=UTF-8"
xo = ("Access-Control-Allow-Origin","*") -- Allow cross origin requests xo = ("Access-Control-Allow-Origin","*") -- Allow cross origin requests
-- https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS -- https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS

View File

@@ -167,11 +167,11 @@ outputJSONP = outputEncodedJSONP . encode
outputEncodedJSONP :: String -> CGI CGIResult outputEncodedJSONP :: String -> CGI CGIResult
outputEncodedJSONP json = outputEncodedJSONP json =
do mc <- getInput "jsonp" do mc <- getInput "jsonp"
let str = case mc of let (ty,str) = case mc of
Nothing -> json Nothing -> ("json",json)
Just c -> c ++ "(" ++ json ++ ")" Just c -> ("javascript",c ++ "(" ++ json ++ ")")
setHeader "Content-Type" "text/javascript; charset=utf-8" ct = "application/"++ty++"; charset=utf-8"
outputStrict $ UTF8.encodeString str outputStrict ct $ UTF8.encodeString str
outputPNG :: BS.ByteString -> CGI CGIResult outputPNG :: BS.ByteString -> CGI CGIResult
outputPNG x = do outputPNG x = do
@@ -186,18 +186,16 @@ outputBinary x = do
outputFPS x outputFPS x
outputHTML :: String -> CGI CGIResult outputHTML :: String -> CGI CGIResult
outputHTML x = do outputHTML = outputStrict "text/html; charset=utf-8" . UTF8.encodeString
setHeader "Content-Type" "text/html; charset=utf-8"
outputStrict $ UTF8.encodeString x
outputPlain :: String -> CGI CGIResult outputPlain :: String -> CGI CGIResult
outputPlain x = do outputPlain = outputStrict "text/plain; charset=utf-8" . UTF8.encodeString
setHeader "Content-Type" "text/plain; charset=utf-8"
outputStrict $ UTF8.encodeString x
outputStrict :: String -> CGI CGIResult outputStrict :: String -> String -> CGI CGIResult
outputStrict x | x == x = do setXO ; output x outputStrict ct x | x == x = do setHeader "Content-Type" ct
| otherwise = fail "I am the pope." setXO
output x
| otherwise = fail "I am the pope."
setXO = setHeader "Access-Control-Allow-Origin" "*" setXO = setHeader "Access-Control-Allow-Origin" "*"
-- https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS -- https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS