Permalink
Browse files

When requesting a folder from the static directory, and that folder h…

…as an index.html; it will be responded

with.
  • Loading branch information...
1 parent 1d10971 commit d844fabe5c2fcbb87e034f009e384046c8098126 @dom96 committed Dec 31, 2012
Showing with 23 additions and 9 deletions.
  1. +20 −7 jester.nim
  2. +0 −1 tests/asynctest.nim
  3. +1 −1 tests/nimrod.cfg
  4. +1 −0 tests/public/root/index.html
  5. +1 −0 todo.markdown
View
@@ -206,6 +206,21 @@ template routeReq(): stmt {.dirty.} =
of TCActionNothing:
assert(false)
+proc sendStaticIfExists[Sock: TSocket | PAsyncSocket](client: Sock, isHttp: bool,
+ paths: varargs[string]) =
+ for p in paths:
+ if existsFile(p):
+ var file = readFile(p)
+ # TODO: Check file permissions
+ let mimetype = j.mimes.getMimetype(p.splitFile.ext[1 .. -1])
+ client.statusContent($Http200, file,
+ {"Content-type": mimetype}.newStringTable, isHttp)
+ return
+
+ # If we get to here then no match could be found.
+ client.statusContent($Http404, error($Http404, jesterVer),
+ {"Content-type": "text/html"}.newStringTable, isHttp)
+
template setMatches(req: expr) = req.matches = matches # Workaround.
proc handleRequest[Sock: TSocket | PAsyncSocket](client: Sock, path, query, body, ip,
reqMethod: string, headers: PStringTable, isHttp: bool) =
@@ -246,14 +261,12 @@ proc handleRequest[Sock: TSocket | PAsyncSocket](client: Sock, path, query, body
if not matched:
# Find static file.
# TODO: Caching.
- if existsFile(j.options.staticDir / req.pathInfo):
- var file = readFile(j.options.staticDir / req.pathInfo)
- let mimetype = j.mimes.getMimetype(req.pathinfo.splitFile.ext[1 .. -1])
- client.statusContent($Http200, file,
- {"Content-type": mimetype}.newStringTable, isHttp)
+ let publicRequested = j.options.staticDir / req.pathInfo
+ if existsDir(publicRequested):
+ client.sendStaticIfExists(isHttp, publicRequested / "index.html",
+ publicRequested / "index.htm")
else:
- client.statusContent($Http404, error($Http404, jesterVer),
- {"Content-type": "text/html"}.newStringTable, isHttp)
+ client.sendStaticIfExists(isHttp, publicRequested)
client.close()
View
@@ -11,4 +11,3 @@ while true:
if not d.poll():
echo("All sockets closed.")
break
-
View
@@ -1,2 +1,2 @@
-#path=".."
+path=".."
#path="."
@@ -0,0 +1 @@
+This should be available at /root/.
View
@@ -5,6 +5,7 @@
* More information for the TRequest object.
* request.accepts "text/xml"
* Anything else?
+* Check file permissions for static files
## Less important

0 comments on commit d844fab

Please sign in to comment.