on master help sign in
Fixed jsonp, added test
commit0b6433ac4c11c0a944d6ee30b8f21788a017c508 authorMichael Snoyman <michael@snoyman.com> dateSun 16 May 2010, 07:08 (16 years ago) parente36942a
4 files changed+30−2
Network/Wai/Middleware/Jsonp.hs+6−1 test/json+1 test/sample.hs+6−1 test/test.html+17
Network/Wai/Middleware/Jsonp.hs+6−1
@@ -27,6 +27,11 @@ takeCallback bs =
2727 then Just y'
2828 else takeCallback $ B8.drop 1 z
2929
30+dropQM :: B8.ByteString -> B8.ByteString
31+dropQM bs
32+ | B8.null bs = bs
33+ | B8.head bs == '?' = B8.tail bs
34+ | otherwise = bs
3035
3136-- | Wrap json responses in a jsonp callback.
3237--
@@ -41,7 +46,7 @@ jsonp app env = do
4146 let callback :: Maybe B8.ByteString
4247 callback =
4348 if B8.pack "text/javascript" `B8.isInfixOf` accept
44− then takeCallback $ queryString env
49+ then takeCallback $ dropQM $ queryString env
4550 else Nothing
4651 let env' =
4752 case callback of
test/jsonnew file+1
@@ -0,0 +1 @@
1+{"data":"this is some data"}
test/sample.hs+6−1
@@ -3,6 +3,7 @@ import Data.ByteString.Lazy (fromChunks)
33import Network.Wai
44import Network.Wai.Enumerator
55import Network.Wai.Middleware.Gzip
6+import Network.Wai.Middleware.Jsonp
67import Network.Wai.Handler.SimpleServer
78
89app :: Application
@@ -15,7 +16,11 @@ app req =
1516 , show i
1617 , "</p>"
1718 ]
19+ "/test.html" -> return $ Response Status200 [] $ Left "test.html"
20+ "/json" -> return $ Response Status200
21+ [(ContentType, pack "application/json")]
22+ $ Left "json"
1823 _ -> return $ Response Status404 [] $ Left "../LICENSE"
1924
2025main :: IO ()
21−main = run 3000 $ gzip app
26+main = run 3000 $ jsonp $ gzip app
test/test.htmlnew file+17
@@ -0,0 +1,17 @@
1+<!DOCTYPE html>
2+<html>
3+ <head>
4+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
5+ <script>
6+ $(function(){
7+ $.getJSON("/json?callback=?", function(o){
8+ $("#dest").html(o.data);
9+ });
10+ });
11+ </script>
12+ </head>
13+ <body>
14+ There should be some content loaded below:
15+ <div id="dest"></div>
16+ </body>
17+</html>