Skip to content

Commit

Permalink
Mimetypes are now looked up.
Browse files Browse the repository at this point in the history
  • Loading branch information
dom96 committed May 13, 2012
1 parent 6f63791 commit 7f17455
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
12 changes: 7 additions & 5 deletions jester.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import httpserver, sockets, strtabs, re, tables, parseutils, os, strutils, uri,
scgi, cookies, times
scgi, cookies, times, mimetypes

import patterns, errorpages, utils

Expand All @@ -15,7 +15,8 @@ type
scgiServer: TScgiState
routes*: seq[tuple[meth: TReqMeth, m: PMatch, c: TCallback]]
options: TOptions

mimes*: TMimeDb

TOptions = object
staticDir: string # By default ./public
appName: string
Expand Down Expand Up @@ -72,6 +73,7 @@ proc initOptions(j: var TJester) =
var j: TJester
j.routes = @[]
j.initOptions()
j.mimes = newMimetypes()

proc statusContent(c: TSocket, status, content: string, headers: PStringTable, http: bool) =
var strHeaders = ""
Expand Down Expand Up @@ -215,9 +217,9 @@ proc handleRequest(client: TSocket, path, query, body,
# TODO: Caching.
if existsFile(j.options.staticDir / req.pathInfo):
var file = readFile(j.options.staticDir / req.pathInfo)
# TODO: Mimetypes
client.statusContent($Http200, file,
{"Content-type": "text/plain"}.newStringTable, isHttp)
let mimetype = j.mimes.getMimetype(req.pathinfo.splitFile.ext[1 .. -1])
client.statusContent($Http200, file,
{"Content-type": mimetype}.newStringTable, isHttp)
else:
client.statusContent($Http404, error($Http404, jesterVer),
{"Content-type": "text/html"}.newStringTable, isHttp)
Expand Down
2 changes: 1 addition & 1 deletion tests/testapp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ var http = true
if paramCount() > 0:
if paramStr(1) == "scgi":
http = false
run("/jester", port = TPort(9999), http=http)
run(if not http: "/jester" else: "", port = TPort(9999), http=http)


1 change: 0 additions & 1 deletion todo.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

## Important

* Mime types
* More information for the TRequest object.
* request.accepts "text/xml"
* Anything else?
Expand Down

0 comments on commit 7f17455

Please sign in to comment.