all repos — nimhttpd @ b18db95716a3b63cbd201dd92de67b6384f7beba

A useful static file web server.

Fixed compilation on nim 0.15.2
Fabio Cevasco h3rald@h3rald.com
Thu, 24 Nov 2016 16:14:54 +0000
commit

b18db95716a3b63cbd201dd92de67b6384f7beba

parent

beb4816e98978043842e78802006ccb2c8112aa8

1 files changed, 7 insertions(+), 8 deletions(-)

jump to
M nimhttpd.nimnimhttpd.nim

@@ -1,10 +1,9 @@

import asynchttpserver, asyncdispatch, asyncnet, os, strutils, mimetypes, times, parseopt2 -from strtabs import StringTableRef, newStringTable const style = "style.css".slurp let appname = "NimHTTPd Web Server" -let appversion = "1.0.0" +let appversion = "1.0.1" let usage = appname & " v" & appversion & " - Tiny Static File Web Server" & """ (c) 2014-2015 Fabio Cevasco

@@ -22,7 +21,7 @@ type

NimHttpResponse* = tuple[ code: HttpCode, content: string, - headers: StringTableRef] + headers: HttpHeaders] NimHttpSettings* = object logging*: bool directory*: string

@@ -72,17 +71,17 @@ return relparent

proc sendNotFound(settings: NimHttpSettings, path: string): NimHttpResponse = var content = "<p>The page you requested cannot be found.<p>" - return (code: Http404, content: h_page(settings, content, $Http404), headers: newStringTable()) + return (code: Http404, content: h_page(settings, content, $Http404), headers: newHttpHeaders()) proc sendNotImplemented(settings: NimHttpSettings, path: string): NimHttpResponse = var content = "<p>This server does not support the functionality required to fulfill the request.</p>" - return (code: Http501, content: h_page(settings, content, $Http501), headers: newStringTable()) + return (code: Http501, content: h_page(settings, content, $Http501), headers: newHttpHeaders()) proc sendStaticFile(settings: NimHttpSettings, path: string): NimHttpResponse = let mimes = settings.mimes let mimetype = mimes.getMimetype(path.splitFile.ext[1 .. ^1]) var file = path.readFile - return (code: Http200, content: file, headers: {"Content-Type": mimetype}.newStringTable) + return (code: Http200, content: file, headers: {"Content-Type": mimetype}.newHttpHeaders) proc sendDirContents(settings: NimHttpSettings, path: string): NimHttpResponse = let cwd = settings.directory

@@ -105,7 +104,7 @@ <ul>

$1 </ul> """ % [files.join("\n")] - res = (code: Http200, content: h_page(settings, ul, title), headers: newStringTable()) + res = (code: Http200, content: h_page(settings, ul, title), headers: newHttpHeaders()) return res proc printReqInfo(settings: NimHttpSettings, req: Request) =

@@ -125,7 +124,7 @@ proc handleHttpRequest(req: Request): Future[void] {.async.} =

printReqInfo(settings, req) let path = settings.directory/req.url.path.replace("%20", " ") var res: NimHttpResponse - if req.reqMethod != "get": + if req.reqMethod != HttpGet: res = sendNotImplemented(settings, path) elif path.existsDir: res = sendDirContents(settings, path)