/
FileServe.hs
264 lines (234 loc) · 11.4 KB
/
FileServe.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | Contains web handlers to serve files from a directory.
module Snap.Util.FileServe
(
getSafePath
, fileServe
, fileServe'
, fileServeSingle
, fileServeSingle'
, defaultMimeTypes
, MimeMap
) where
------------------------------------------------------------------------------
import Control.Monad
import Control.Monad.Trans
import qualified Data.ByteString.Char8 as S
import Data.ByteString.Char8 (ByteString)
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
import System.Directory
import System.FilePath
import System.PosixCompat.Files
------------------------------------------------------------------------------
import Snap.Types
------------------------------------------------------------------------------
-- | A type alias for MIME type
type MimeMap = Map FilePath ByteString
------------------------------------------------------------------------------
-- | The default set of mime type mappings we use when serving files. Its
-- value:
--
-- > Map.fromList [
-- > ( ".asc" , "text/plain" ),
-- > ( ".asf" , "video/x-ms-asf" ),
-- > ( ".asx" , "video/x-ms-asf" ),
-- > ( ".avi" , "video/x-msvideo" ),
-- > ( ".bz2" , "application/x-bzip" ),
-- > ( ".c" , "text/plain" ),
-- > ( ".class" , "application/octet-stream" ),
-- > ( ".conf" , "text/plain" ),
-- > ( ".cpp" , "text/plain" ),
-- > ( ".css" , "text/css" ),
-- > ( ".cxx" , "text/plain" ),
-- > ( ".dtd" , "text/xml" ),
-- > ( ".dvi" , "application/x-dvi" ),
-- > ( ".gif" , "image/gif" ),
-- > ( ".gz" , "application/x-gzip" ),
-- > ( ".hs" , "text/plain" ),
-- > ( ".htm" , "text/html" ),
-- > ( ".html" , "text/html" ),
-- > ( ".jar" , "application/x-java-archive" ),
-- > ( ".jpeg" , "image/jpeg" ),
-- > ( ".jpg" , "image/jpeg" ),
-- > ( ".js" , "text/javascript" ),
-- > ( ".log" , "text/plain" ),
-- > ( ".m3u" , "audio/x-mpegurl" ),
-- > ( ".mov" , "video/quicktime" ),
-- > ( ".mp3" , "audio/mpeg" ),
-- > ( ".mpeg" , "video/mpeg" ),
-- > ( ".mpg" , "video/mpeg" ),
-- > ( ".ogg" , "application/ogg" ),
-- > ( ".pac" , "application/x-ns-proxy-autoconfig" ),
-- > ( ".pdf" , "application/pdf" ),
-- > ( ".png" , "image/png" ),
-- > ( ".ps" , "application/postscript" ),
-- > ( ".qt" , "video/quicktime" ),
-- > ( ".sig" , "application/pgp-signature" ),
-- > ( ".spl" , "application/futuresplash" ),
-- > ( ".swf" , "application/x-shockwave-flash" ),
-- > ( ".tar" , "application/x-tar" ),
-- > ( ".tar.bz2" , "application/x-bzip-compressed-tar" ),
-- > ( ".tar.gz" , "application/x-tgz" ),
-- > ( ".tbz" , "application/x-bzip-compressed-tar" ),
-- > ( ".text" , "text/plain" ),
-- > ( ".tgz" , "application/x-tgz" ),
-- > ( ".torrent" , "application/x-bittorrent" ),
-- > ( ".txt" , "text/plain" ),
-- > ( ".wav" , "audio/x-wav" ),
-- > ( ".wax" , "audio/x-ms-wax" ),
-- > ( ".wma" , "audio/x-ms-wma" ),
-- > ( ".wmv" , "video/x-ms-wmv" ),
-- > ( ".xbm" , "image/x-xbitmap" ),
-- > ( ".xml" , "text/xml" ),
-- > ( ".xpm" , "image/x-xpixmap" ),
-- > ( ".xwd" , "image/x-xwindowdump" ),
-- > ( ".zip" , "application/zip" ) ]
--
defaultMimeTypes :: MimeMap
defaultMimeTypes = Map.fromList [
( ".asc" , "text/plain" ),
( ".asf" , "video/x-ms-asf" ),
( ".asx" , "video/x-ms-asf" ),
( ".avi" , "video/x-msvideo" ),
( ".bz2" , "application/x-bzip" ),
( ".c" , "text/plain" ),
( ".class" , "application/octet-stream" ),
( ".conf" , "text/plain" ),
( ".cpp" , "text/plain" ),
( ".css" , "text/css" ),
( ".cxx" , "text/plain" ),
( ".dtd" , "text/xml" ),
( ".dvi" , "application/x-dvi" ),
( ".gif" , "image/gif" ),
( ".gz" , "application/x-gzip" ),
( ".hs" , "text/plain" ),
( ".htm" , "text/html" ),
( ".html" , "text/html" ),
( ".jar" , "application/x-java-archive" ),
( ".jpeg" , "image/jpeg" ),
( ".jpg" , "image/jpeg" ),
( ".js" , "text/javascript" ),
( ".log" , "text/plain" ),
( ".m3u" , "audio/x-mpegurl" ),
( ".mov" , "video/quicktime" ),
( ".mp3" , "audio/mpeg" ),
( ".mpeg" , "video/mpeg" ),
( ".mpg" , "video/mpeg" ),
( ".ogg" , "application/ogg" ),
( ".pac" , "application/x-ns-proxy-autoconfig" ),
( ".pdf" , "application/pdf" ),
( ".png" , "image/png" ),
( ".ps" , "application/postscript" ),
( ".qt" , "video/quicktime" ),
( ".sig" , "application/pgp-signature" ),
( ".spl" , "application/futuresplash" ),
( ".swf" , "application/x-shockwave-flash" ),
( ".tar" , "application/x-tar" ),
( ".tar.bz2" , "application/x-bzip-compressed-tar" ),
( ".tar.gz" , "application/x-tgz" ),
( ".tbz" , "application/x-bzip-compressed-tar" ),
( ".text" , "text/plain" ),
( ".tgz" , "application/x-tgz" ),
( ".torrent" , "application/x-bittorrent" ),
( ".ttf" , "application/x-font-truetype" ),
( ".txt" , "text/plain" ),
( ".wav" , "audio/x-wav" ),
( ".wax" , "audio/x-ms-wax" ),
( ".wma" , "audio/x-ms-wma" ),
( ".wmv" , "video/x-ms-wmv" ),
( ".xbm" , "image/x-xbitmap" ),
( ".xml" , "text/xml" ),
( ".xpm" , "image/x-xpixmap" ),
( ".xwd" , "image/x-xwindowdump" ),
( ".zip" , "application/zip" ) ]
------------------------------------------------------------------------------
-- | Gets a path from the 'Request' using 'rqPathInfo' and makes sure it is
-- safe to use for opening files. A path is safe if it is a relative path
-- and has no ".." elements to escape the intended directory structure.
getSafePath :: Snap FilePath
getSafePath = do
req <- getRequest
let p = S.unpack $ rqPathInfo req
-- check that we don't have any sneaky .. paths
let dirs = splitDirectories p
when (elem ".." dirs) pass
return p
------------------------------------------------------------------------------
-- | Serves files out of the given directory. The relative path given in
-- 'rqPathInfo' is searched for the given file, and the file is served with the
-- appropriate mime type if it is found. Absolute paths and \"@..@\" are prohibited
-- to prevent files from being served from outside the sandbox.
--
-- Uses 'defaultMimeTypes' to determine the @Content-Type@ based on the file's
-- extension.
fileServe :: FilePath -- ^ root directory
-> Snap ()
fileServe = fileServe' defaultMimeTypes
{-# INLINE fileServe #-}
------------------------------------------------------------------------------
-- | Same as 'fileServe', with control over the MIME mapping used.
fileServe' :: MimeMap -- ^ MIME type mapping
-> FilePath -- ^ root directory
-> Snap ()
fileServe' mm root = do
sp <- getSafePath
let fp = root </> sp
-- check that the file exists
liftIO (doesFileExist fp) >>= flip unless pass
let fn = takeFileName fp
let mime = fileType mm fn
fileServeSingle' mime fp
{-# INLINE fileServe' #-}
------------------------------------------------------------------------------
-- | Serves a single file specified by a full or relative path. The
-- path restrictions on fileServe don't apply to this function since
-- the path is not being supplied by the user.
fileServeSingle :: FilePath -- ^ path to file
-> Snap ()
fileServeSingle fp =
fileServeSingle' (fileType defaultMimeTypes (takeFileName fp)) fp
{-# INLINE fileServeSingle #-}
------------------------------------------------------------------------------
-- | Same as 'fileServeSingle', with control over the MIME mapping used.
fileServeSingle' :: ByteString -- ^ MIME type mapping
-> FilePath -- ^ path to file
-> Snap ()
fileServeSingle' mime fp = do
req <- getRequest
let mbH = getHeader "if-modified-since" req
mbIfModified <- liftIO $ case mbH of
Nothing -> return Nothing
(Just s) -> liftM Just $ parseHttpTime s
-- check modification time and bug out early if the file is not modified.
filestat <- liftIO $ getFileStatus fp
let mt = modificationTime filestat
maybe (return ()) (chkModificationTime mt) mbIfModified
let sz = fromEnum $ fileSize filestat
lm <- liftIO $ formatHttpTime mt
modifyResponse $ setHeader "Last-Modified" lm
. setContentType mime
. setContentLength sz
sendFile fp
where
--------------------------------------------------------------------------
chkModificationTime mt lt = when (mt <= lt) notModified
--------------------------------------------------------------------------
notModified = finishWith $
setResponseStatus 304 "Not Modified" emptyResponse
------------------------------------------------------------------------------
fileType :: MimeMap -> FilePath -> ByteString
fileType mm f =
if null ext
then defaultMimeType
else fromMaybe (fileType mm (drop 1 ext))
mbe
where
ext = takeExtensions f
mbe = Map.lookup ext mm
------------------------------------------------------------------------------
defaultMimeType :: ByteString
defaultMimeType = "application/octet-stream"