hack-middleware-jsonp (empty) → 0.0.0
raw patch · 4 files changed
+113/−0 lines, 4 filesdep +basedep +bytestringdep +bytestring-classsetup-changed
Dependencies added: base, bytestring, bytestring-class, hack, web-encodings
Files
- Hack/Middleware/Jsonp.hs +63/−0
- LICENSE +25/−0
- Setup.lhs +7/−0
- hack-middleware-jsonp.cabal +18/−0
+ Hack/Middleware/Jsonp.hs view
@@ -0,0 +1,63 @@+---------------------------------------------------------+-- |+-- Module : Hack.Middleware.Jsonp+-- Copyright : Michael Snoyman+-- License : BSD3+--+-- Maintainer : Michael Snoyman <michael@snoyman.com>+-- Stability : Unstable+-- Portability : portable+--+-- Automatic wrapping of JSON responses to convert into JSONP.+--+---------------------------------------------------------+module Hack.Middleware.Jsonp (jsonp) where++import Hack+import Web.Encodings (decodeUrlPairs)+import Data.ByteString.Class (toLazyByteString)+import qualified Data.ByteString.Lazy as BS+import Data.Maybe (fromMaybe)+import Data.List (isInfixOf)++-- | Wrap json responses in a jsonp callback.+--+-- Basically, if the user requested a \"text\/javascript\" and supplied a+-- \"callback\" GET parameter, ask the application for an+-- \"application/json\" response, then convern that into a JSONP response,+-- having a content type of \"text\/javascript\" and calling the specified+-- callback function.+jsonp :: Middleware+jsonp app env = do+ let accept = fromMaybe "" $ lookup "HTTP_ACCEPT" $ http env+ let gets = decodeUrlPairs $ queryString env+ let callback :: Maybe String+ callback =+ if "text/javascript" `isInfixOf` accept+ then lookup "callback" gets+ else Nothing+ let env' =+ case callback of+ Nothing -> env+ Just _ -> env+ { http = changeVal "HTTP_ACCEPT"+ "application/json"+ $ http env+ }+ res <- app env'+ case callback of+ Nothing -> return res+ Just c -> return $ res+ { headers = changeVal "Content-type"+ "text/javascript"+ $ headers res+ , body = BS.concat+ [ toLazyByteString c+ , toLazyByteString "("+ , body res+ , toLazyByteString ")"+ ]+ }++changeVal :: String -> String -> [(String, String)] -> [(String, String)]+changeVal key val old = (key, val) : filter (\(k, _) -> k /= key) old
+ LICENSE view
@@ -0,0 +1,25 @@+The following license covers this documentation, and the source code, except+where otherwise indicated.++Copyright 2008, Michael Snoyman. All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+ list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhaskell++> module Main where+> import Distribution.Simple++> main :: IO ()+> main = defaultMain
+ hack-middleware-jsonp.cabal view
@@ -0,0 +1,18 @@+name: hack-middleware-jsonp+version: 0.0.0+license: BSD3+license-file: LICENSE+author: Michael Snoyman <michael@snoyman.com>+maintainer: Michael Snoyman <michael@snoyman.com>+synopsis: Automatic wrapping of JSON responses to convert into JSONP.+category: Web+stability: unstable+cabal-version: >= 1.2+build-type: Simple+homepage: http://github.com/snoyberg/hack-middleware-jsonp/tree/master++library+ build-depends: base >= 3, bytestring >= 0.9.1.4, bytestring-class,+ web-encodings, hack >= 2009.5.19+ exposed-modules: Hack.Middleware.Jsonp+ ghc-options: -Wall