on master help sign in
Fixed jsonp, added test
0b6433aMichael Snoyman
Network/Wai/Middleware/Jsonp.hs+6−1
@@ -27,6 +27,11 @@ takeCallback bs =
27 then Just y'
27 then Just y'
28 else takeCallback $ B8.drop 1 z
28 else takeCallback $ B8.drop 1 z
29
29
30+dropQM :: B8.ByteString -> B8.ByteString
31+dropQM bs
32+ | B8.null bs = bs
33+ | B8.head bs == '?' = B8.tail bs
34+ | otherwise = bs
30
35
31-- | Wrap json responses in a jsonp callback.
36-- | Wrap json responses in a jsonp callback.
32--
37--
@@ -41,7 +46,7 @@ jsonp app env = do
41 let callback :: Maybe B8.ByteString
46 let callback :: Maybe B8.ByteString
42 callback =
47 callback =
43 if B8.pack "text/javascript" `B8.isInfixOf` accept
48 if B8.pack "text/javascript" `B8.isInfixOf` accept
44− then takeCallback $ queryString env
49+ then takeCallback $ dropQM $ queryString env
45 else Nothing
50 else Nothing
46 let env' =
51 let env' =
47 case callback of
52 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)
3import Network.Wai
3import Network.Wai
4import Network.Wai.Enumerator
4import Network.Wai.Enumerator
5import Network.Wai.Middleware.Gzip
5import Network.Wai.Middleware.Gzip
6+import Network.Wai.Middleware.Jsonp
6import Network.Wai.Handler.SimpleServer
7import Network.Wai.Handler.SimpleServer
7
8
8app :: Application
9app :: Application
@@ -15,7 +16,11 @@ app req =
15 , show i
16 , show i
16 , "</p>"
17 , "</p>"
17 ]
18 ]
19+ "/test.html" -> return $ Response Status200 [] $ Left "test.html"
20+ "/json" -> return $ Response Status200
21+ [(ContentType, pack "application/json")]
22+ $ Left "json"
18 _ -> return $ Response Status404 [] $ Left "../LICENSE"
23 _ -> return $ Response Status404 [] $ Left "../LICENSE"
19
24
20main :: IO ()
25main :: 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>