snap-stream (empty) → 0.1
raw patch · 4 files changed
+191/−0 lines, 4 filesdep +attoparsecdep +basedep +bytestringsetup-changed
Dependencies added: attoparsec, base, bytestring, io-streams, snap-core
Files
- LICENSE +12/−0
- Setup.hs +2/−0
- snap-stream.cabal +23/−0
- src/Snap/Util/FileServe/Stream.hs +154/−0
+ LICENSE view
@@ -0,0 +1,12 @@+Copyright (c) 2018, Obsidian Systems LLC+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. 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.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 HOLDER OR CONTRIBUTORS 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.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ snap-stream.cabal view
@@ -0,0 +1,23 @@+Name: snap-stream+Version: 0.1+Synopsis: Streaming Snap handlers+Description: Snap handlers for streaming access with range requests+License: BSD3+License-file: LICENSE+Author: Obsidian Systems LLC+Maintainer: maintainer@obsidian.systems+Stability: Experimental+Category: Web+Build-type: Simple+Cabal-version: >= 1.6++library+ hs-source-dirs: src+ build-depends: base >= 4.9 && < 4.12+ , attoparsec == 0.13.*+ , bytestring == 0.10.*+ , io-streams >= 1.4 && < 1.6+ , snap-core == 1.0.*+ exposed-modules: Snap.Util.FileServe.Stream++ ghc-options: -Wall -fno-warn-unused-do-bind -fwarn-tabs -funbox-strict-fields -O2
+ src/Snap/Util/FileServe/Stream.hs view
@@ -0,0 +1,154 @@+{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE CPP #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE ScopedTypeVariables #-}++-- | Contains web handlers to stream files+module Snap.Util.FileServe.Stream where++------------------------------------------------------------------------------+import Control.Applicative+import Control.Monad+import Control.Monad.IO.Class+import Data.Attoparsec.ByteString.Char8 hiding (char8)+import Data.ByteString.Builder+import Data.ByteString.Char8 (ByteString)+import qualified Data.ByteString.Char8 as S+import qualified Data.ByteString.Lazy.Char8 as L+import Data.Maybe (fromMaybe, isNothing)+import Data.Word (Word64)+import Prelude+import Snap.Core+import Snap.Internal.Parsing (fullyParse, parseNum)+import System.IO.Streams (OutputStream)+++------------------------------------------------------------------------------+-- | Same as 'serveFile', with control over the MIME mapping used and streamed+serveStreamAs :: MonadSnap m+ => ByteString -- ^ MIME type+ -> Word64+ -> (Word64 -> Word64 -> OutputStream Builder -> IO ())+ -> (OutputStream Builder -> IO ())+ -> m ()+serveStreamAs mime sz stream streamAll = do+ reqOrig <- getRequest++ -- If-Range header must be ignored if there is no Range: header in the+ -- request (RFC 2616 section 14.27)+ let req = if isNothing $ getHeader "range" reqOrig+ then deleteHeader "if-range" reqOrig+ else reqOrig++ -- ok, at this point we know the last-modified time and the+ -- content-type. set those.+ modifyResponse $ setHeader "Accept-Ranges" "bytes"+ . setContentType mime++ -- checkRangeReq checks for a Range: header in the request and sends a+ -- partial response if it matches.+ wasRange <- liftSnap $ checkRangeReq req stream sz++ -- if we didn't have a range request, we just do normal sendfile+ unless wasRange $ do+ modifyResponse $ setResponseCode 200+ . setContentLength sz+ addToOutput $ \str -> liftIO (streamAll str) >> return str++------------------------------------------------------------------------------+data RangeReq = RangeReq !Word64 !(Maybe Word64)+ | SuffixRangeReq !Word64+++------------------------------------------------------------------------------+rangeParser :: Parser RangeReq+rangeParser = string "bytes=" *>+ (byteRangeSpec <|> suffixByteRangeSpec) <*+ endOfInput+ where+ byteRangeSpec = do+ start <- fromIntegral <$> parseNum+ void $! char '-'+ end <- option Nothing $ liftM Just parseNum++ return $! RangeReq start (fromIntegral <$> end)++ suffixByteRangeSpec =+ liftM (SuffixRangeReq . fromIntegral) $ char '-' *> parseNum+++------------------------------------------------------------------------------+checkRangeReq :: (MonadSnap m)+ => Request+ -> (Word64 -> Word64 -> OutputStream Builder -> IO ())+ -> Word64+ -> m Bool+checkRangeReq req stream sz = do+ -- TODO/FIXME: multiple ranges+ maybe (return False)+ (\s -> either (const $ return False)+ withRange+ (fullyParse s rangeParser))+ (getHeader "range" req)++ where+ withRange (RangeReq start mend) = do+ let end = fromMaybe (sz-1) mend++ if start < 0 || end < start || start >= sz || end >= sz+ then send416+ else send206 start end++ withRange (SuffixRangeReq nbytes) = do+ let end = sz-1+ let start = sz - nbytes++ if start < 0 || end < start || start >= sz || end >= sz+ then send416+ else send206 start end++ -- note: start and end INCLUSIVE here+ send206 start end = do+ let !len = end-start+1+ let crng = S.concat . L.toChunks $+ toLazyByteString $+ mconcat [ byteString "bytes "+ , fromShow start+ , char8 '-'+ , fromShow end+ , char8 '/'+ , fromShow sz ]++ modifyResponse $ setResponseCode 206+ . setHeader "Content-Range" crng+ . setContentLength len++ -- end here was inclusive, sendFilePartial is exclusive+ addToOutput $ \str -> liftIO (stream start (end+1) str) >> return str+ return True+++ send416 = do+ -- if there's an "If-Range" header in the request, then we just send+ -- back 200+ if getHeader "If-Range" req /= Nothing+ then return False+ else do+ let crng = S.concat . L.toChunks $+ toLazyByteString $+ mconcat [ byteString "bytes */"+ , fromShow sz ]++ modifyResponse $ setResponseCode 416+ . setHeader "Content-Range" crng+ . setContentLength 0+ . deleteHeader "Content-Type"+ . deleteHeader "Content-Encoding"+ . deleteHeader "Transfer-Encoding"+ . setResponseBody (return . id)++ return True++------------------------------------------------------------------------------+fromShow :: Show a => a -> Builder+fromShow = stringUtf8 . show