packages feed

http-accept (empty) → 0.1

raw patch · 5 files changed

+95/−0 lines, 5 filesdep +basedep +bytestringsetup-changed

Dependencies added: base, bytestring

Files

+ COPYING view
@@ -0,0 +1,13 @@+Copyright © 2012, Stephen Paul Weber <singpolyma.net>++Permission to use, copy, modify, and/or distribute this software for any+purpose with or without fee is hereby granted, provided that the above+copyright notice and this permission notice appear in all copies.++THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ Network/HTTP/Accept.hs view
@@ -0,0 +1,40 @@+module Network.HTTP.Accept (selectAcceptType) where++import Data.Char (isAscii)+import Data.Maybe (mapMaybe, listToMaybe)+import Control.Arrow (first)+import Data.ByteString (ByteString)+import qualified Data.ByteString as BS (singleton, split)+import qualified Data.ByteString.Char8 as Char8++data Pattern a = PatternAny | PatternExactly a++instance (Eq a) => Eq (Pattern a) where+	PatternAny == _ = True+	_ == PatternAny = True+	(PatternExactly a) == (PatternExactly b) = a == b++-- | Select which Accept type to use+selectAcceptType ::+	[String]        -- ^ List of supported MIME types, in preferred order+	-> [ByteString] -- ^ List of types from Accept, pre-sorted with no q+	-> Maybe String -- ^ Just the selected supported type, or else Nothing+selectAcceptType supported accept =+	listToMaybe $ mapMaybe (`lookup` supported') accept'+	where+	accept' = map (Just . parseAccept) accept+	supported' = map (first $ fmap parseAccept . stringAscii)+		(zip supported supported)+	parseAccept s = let (t:sub:_) = BS.split 0x2f s in+		(parsePattern t, parsePattern sub)++parsePattern :: ByteString -> Pattern ByteString+parsePattern s+	| s == BS.singleton 0x2a = PatternAny+	| otherwise = PatternExactly s++-- | Safely convert an ASCII 'String' to 'ByteString'+stringAscii :: String -> Maybe ByteString+stringAscii s+	| all isAscii s = Just $ Char8.pack s+	| otherwise     = Nothing
+ README view
@@ -0,0 +1,4 @@+Functions for working with HTTP Accept headers.  You may also want+check out parseHttpAccept from wai-extra:Network.Wai.Parse (it is+not tied to WAI in any way and some similar may make it into this+package eventually).
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ http-accept.cabal view
@@ -0,0 +1,35 @@+name:            http-accept+version:         0.1+cabal-version:   >= 1.8+license:         OtherLicense+license-file:    COPYING+category:        Web+copyright:       © 2012 Stephen Paul Weber+author:          Stephen Paul Weber <singpolyma@singpolyma.net>+maintainer:      Stephen Paul Weber <singpolyma@singpolyma.net>+stability:       experimental+tested-with:     GHC == 7.4.1+synopsis:        Functions for working with HTTP Accept headers+homepage:        https://github.com/singpolyma/http-accept+bug-reports:     https://github.com/singpolyma/http-accept/issues+build-type:      Simple+description:+        Functions for working with HTTP Accept headers.  You may also want+        check out parseHttpAccept from wai-extra:Network.Wai.Parse (it is+        not tied to WAI in any way and some similar may make it into this+        package eventually).++extra-source-files:+        README++library+        exposed-modules:+                Network.HTTP.Accept++        build-depends:+                base == 4.*,+                bytestring++source-repository head+        type:     git+        location: git://github.com/singpolyma/http-accept.git