Skip to content

Commit 8790d49

Browse files
authored
Merge pull request #59 from paluh/paluh-htaccess
FileServer: add simple content type detection
2 parents b23b661 + cbdb7ca commit 8790d49

File tree

1 file changed

+100
-3
lines changed

1 file changed

+100
-3
lines changed

src/Hyper/Node/FileServer.purs

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module Hyper.Node.FileServer (fileServer) where
22

33
import Prelude
4-
import Node.Buffer as Buffer
5-
import Node.Path as Path
4+
65
import Control.IxMonad (ibind, (:>>=))
76
import Control.Monad.Aff.Class (liftAff, class MonadAff)
87
import Control.Monad.Eff.Class (liftEff)
8+
import Data.Array (last)
9+
import Data.Map (Map, fromFoldable, lookup)
10+
import Data.Maybe (maybe)
11+
import Data.String (Pattern(..), split)
912
import Data.Tuple (Tuple(Tuple))
1013
import Hyper.Conn (Conn)
1114
import Hyper.Middleware (Middleware, lift')
@@ -14,10 +17,101 @@ import Hyper.Request (class Request, getRequestData)
1417
import Hyper.Response (class ResponseWritable, class Response, ResponseEnded, StatusLineOpen, end, headers, send, toResponse, writeStatus)
1518
import Hyper.Status (statusOK)
1619
import Node.Buffer (BUFFER, Buffer)
20+
import Node.Buffer as Buffer
1721
import Node.FS (FS)
1822
import Node.FS.Aff (readFile, stat, exists)
1923
import Node.FS.Stats (isDirectory, isFile)
2024
import Node.Path (FilePath)
25+
import Node.Path as Path
26+
27+
28+
htaccess :: Map String String
29+
htaccess = fromFoldable $
30+
[ Tuple "aab" "application/x-authorware-bin"
31+
, Tuple "aam" "application/x-authorware-map"
32+
, Tuple "aas" "application/x-authorware-seg"
33+
, Tuple "asc" "text/plain"
34+
, Tuple "asf" "video/x-ms-asf"
35+
, Tuple "asp" "text/html"
36+
, Tuple "asx" "video/x-ms-asf"
37+
, Tuple "avi" "application/octet-stream"
38+
, Tuple "awk" "text/plain"
39+
, Tuple "bash" "text/plain"
40+
, Tuple "bsh" "text/plain"
41+
, Tuple "bz2" "application/octet-stream"
42+
, Tuple "c" "text/plain"
43+
, Tuple "cgi" "text/plain"
44+
, Tuple "chm" "application/octet-stream"
45+
, Tuple "class" "application/x-java-applet"
46+
, Tuple "csh" "text/plain"
47+
, Tuple "css" "text/css"
48+
, Tuple "csv" "application/vnd.ms-excel"
49+
, Tuple "dcr" "application/x-director"
50+
, Tuple "dir" "application/x-director"
51+
, Tuple "dmg" "application/octet-stream"
52+
, Tuple "dxr" "application/x-director"
53+
, Tuple "exe" "application/octet-stream"
54+
, Tuple "fgd" "application/x-director"
55+
, Tuple "fh" "image/x-freehand"
56+
, Tuple "fh4" "image/x-freehand"
57+
, Tuple "fh5" "image/x-freehand"
58+
, Tuple "fh7" "image/x-freehand"
59+
, Tuple "fhc" "image/x-freehand"
60+
, Tuple "flv" "video/x-flv"
61+
, Tuple "gawk" "text/plain"
62+
, Tuple "gtar" "application/x-gtar"
63+
, Tuple "gz" "application/x-gzip"
64+
, Tuple "h" "text/plain"
65+
, Tuple "ico" "image/vnd.microsoft.icon"
66+
, Tuple "in" "text/plain"
67+
, Tuple "ini" "text/plain"
68+
, Tuple "m3u" "audio/x-mpegurl"
69+
, Tuple "md5" "text/plain"
70+
, Tuple "mov" "application/octet-stream"
71+
, Tuple "mov" "video/quicktime"
72+
, Tuple "mp4" "application/octet-stream"
73+
, Tuple "mpg" "application/octet-stream"
74+
, Tuple "msi" "application/octet-stream"
75+
, Tuple "nawk" "text/plain"
76+
, Tuple "pdb" "application/x-pilot"
77+
, Tuple "pdf" "application/pdf"
78+
, Tuple "phps" "application/x-httpd-php-source"
79+
, Tuple "pl" "text/plain"
80+
, Tuple "prc" "application/x-pilot"
81+
, Tuple "py" "text/plain"
82+
, Tuple "qt" "video/quicktime"
83+
, Tuple "ra" "audio/vnd.rn-realaudio"
84+
, Tuple "ram" "audio/vnd.rn-realaudio"
85+
, Tuple "rar" "application/x-rar-compressed"
86+
, Tuple "rm" "application/vnd.rn-realmedia"
87+
, Tuple "rpm" "audio/x-pn-realaudio-plugin"
88+
, Tuple "rv" "video/vnd.rn-realvideo"
89+
, Tuple "sh" "text/plain"
90+
, Tuple "sha" "text/plain"
91+
, Tuple "sha1" "text/plain"
92+
, Tuple "shtml" "text/html"
93+
, Tuple "svg" "image/svg+xml"
94+
, Tuple "svgz" "image/svg+xml"
95+
, Tuple "swf" "application/x-shockwave-flash"
96+
, Tuple "tgz" "application/octet-stream"
97+
, Tuple "torrent" "application/x-bittorrent"
98+
, Tuple "var" "text/plain"
99+
, Tuple "wav" "audio/x-wav"
100+
, Tuple "wax" "audio/x-ms-wax"
101+
, Tuple "wm" "video/x-ms-wm"
102+
, Tuple "wma" "audio/x-ms-wma"
103+
, Tuple "wmd" "application/x-ms-wmd"
104+
, Tuple "wmv" "video/x-ms-wmv"
105+
, Tuple "wmx" "video/x-ms-wmx"
106+
, Tuple "wmz" "application/x-ms-wmz"
107+
, Tuple "wvx" "video/x-ms-wvx"
108+
, Tuple "xbm" "image/x-xbitmap"
109+
, Tuple "xhtml" "application/xhtml+xml"
110+
, Tuple "xls" "application/octet-stream"
111+
, Tuple "xml" "text/xml"
112+
, Tuple "xrdf" "application/xrds+xml"
113+
, Tuple "zip" "application/zip"
114+
]
21115

22116
serveFile
23117
:: forall m e req res c b
@@ -32,10 +126,13 @@ serveFile
32126
(Conn req (res ResponseEnded) c)
33127
Unit
34128
serveFile path = do
129+
let
130+
ext = last $ split (Pattern ".") path
131+
contentType = maybe "*/*" id (ext >>= flip lookup htaccess)
35132
buf <- lift' (liftAff (readFile path))
36133
contentLength <- liftEff (Buffer.size buf)
37134
_ <- writeStatus statusOK
38-
_ <- headers [ Tuple "Content-Type" "*/*; charset=utf-8"
135+
_ <- headers [ Tuple "Content-Type" (contentType <> "; charset=utf-8")
39136
, Tuple "Content-Length" (show contentLength)
40137
]
41138
response <- toResponse buf

0 commit comments

Comments
 (0)