]> andersk Git - scripts-static-cat.git/commitdiff
Clean up range outputting.
authorAnders Kaseorg <andersk@mit.edu>
Tue, 9 Mar 2010 05:51:48 +0000 (00:51 -0500)
committerAnders Kaseorg <andersk@mit.edu>
Tue, 9 Mar 2010 05:51:48 +0000 (00:51 -0500)
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
StaticCat.hs

index f7e4ad80228580deac09e575f0ce945de3cfc12a..25be38899a5b007ca43395beb61afa3138e57145 100644 (file)
@@ -108,6 +108,23 @@ checkRange mTime size = do
       Just (a, b) | a <= b -> return $ Just (a, b)
       _ -> throwExceptionCGI BadRange
 
+outputAll :: Handle -> FileOffset -> CGI CGIResult
+outputAll h size = do
+  setHeader "Content-Length" $ show size
+  outputFPS =<< liftIO (B.hGetContents h)
+
+outputRange :: Handle -> FileOffset -> Maybe (FileOffset, FileOffset) -> CGI CGIResult
+outputRange h size Nothing = outputAll h size
+outputRange h size (Just (a, b)) = do
+  let len = b - a + 1
+
+  setStatus 206 "Partial Content"
+  setHeader "Content-Range" $
+   "bytes " ++ show a ++ "-" ++ show b ++ "/" ++ show size
+  setHeader "Content-Length" $ show len
+  liftIO $ hSeek h AbsoluteSeek (fromIntegral a)
+  outputFPS =<< liftIO (B.hGet h (fromIntegral len))
+
 serveFile :: FilePath -> CGI CGIResult
 serveFile file = (`catchExceptionCGI` outputMyError) $ do
   checkExtension file
@@ -127,19 +144,8 @@ serveFile file = (`catchExceptionCGI` outputMyError) $ do
       size = fileSize status
   checkModified mTime
 
-  checkRange mTime size >>= maybe
-    (do
-       setHeader "Content-Length" $ show size
-       outputFPS =<< (liftIO $ B.hGetContents h))
-    (\(a, b) -> do
-       let len = b - a + 1
-
-       setStatus 206 "Partial Content"
-       setHeader "Content-Range" $
-        "bytes " ++ show a ++ "-" ++ show b ++ "/" ++ show size
-       setHeader "Content-Length" $ show len
-       liftIO $ hSeek h AbsoluteSeek (fromIntegral a)
-       outputFPS =<< (liftIO $ B.hGet h (fromIntegral len)))
+  range <- checkRange mTime size
+  outputRange h size range
 
 main :: IO ()
 main = runCGI $ handleErrors $ serveFile =<< pathTranslated
This page took 0.67815 seconds and 5 git commands to generate.