Skip to content

Add Custom Headers To File Server #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Hyper/Node/FileServer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ serveFile
=> ResponseWritable b m Buffer
=> Response res m b
=> FilePath
-> Array (Tuple String String)
-> Middleware
m
(Conn req (res StatusLineOpen) c)
(Conn req (res ResponseEnded) c)
Unit
serveFile path = do
serveFile path userProvidedHeaders = do
buf <- lift' (liftAff (readFile path))
contentLength <- liftEff (Buffer.size buf)
_ <- writeStatus statusOK
_ <- headers [ Tuple "Content-Type" "*/*; charset=utf-8"
let h = [ Tuple "Content-Type" "*/*; charset=utf-8"
, Tuple "Content-Length" (show contentLength)
]
_ <- writeStatus statusOK
_ <- headers $ h <> userProvidedHeaders
response <- toResponse buf
_ <- send response
end
Expand All @@ -57,18 +59,19 @@ fileServer
(Conn req (res StatusLineOpen) c)
(Conn req (res ResponseEnded) c)
Unit
-> Array (Tuple String String)
-> Middleware
m
(Conn req (res StatusLineOpen) c)
(Conn req (res ResponseEnded) c)
Unit
fileServer dir on404 = do
fileServer dir on404 headers = do
conn ← getConn
{ url } <- getRequestData
serve (Path.concat [dir, url])
where
serveStats absolutePath stats
| isFile stats = serveFile absolutePath
| isFile stats = serveFile absolutePath headers
| isDirectory stats = serve (Path.concat [absolutePath, "index.html"])
| otherwise = on404

Expand Down
2 changes: 1 addition & 1 deletion test/Hyper/Node/FileServerSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ serveFilesAndGet path =
# evalMiddleware app
# testServer
where
app = fileServer "test/Hyper/Node/FileServerSpec" on404
app = fileServer "test/Hyper/Node/FileServerSpec" on404 []

on404 = do
body <- liftEff (Buffer.fromString "Not Found" UTF8)
Expand Down