src/nimhttpd.nim
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
import asynchttpserver, asyncdispatch, asyncnet, os, strutils, mimetypes, times, parseopt, parsecfg, streams, strutils from httpcore import HttpMethod, HttpHeaders import nimhttpdpkg/config const name = pkgTitle version = pkgVersion style = "style.css".slurp description = pkgDescription author = pkgAuthor let usage = """ $1 v$2 - $3 (c) 2014-2018 $4 Usage: nimhttpd [-p:port] [directory] Arguments: directory The directory to serve (default: current directory). Options: -p, --port The port to listen to (default: 1337). """ % [name, version, description, author] type NimHttpResponse* = tuple[ code: HttpCode, content: string, headers: HttpHeaders] NimHttpSettings* = object logging*: bool directory*: string mimes*: MimeDb port*: Port address*: string name: string version*: string proc h_page(settings:NimHttpSettings, content: string, title=""): string = var footer = """<div id="footer">$1 v$2</div>""" % [settings.name, settings.version] result = """ <!DOCTYPE html> <html> <head> <title>$1</title> <style type="text/css">$2</style> <meta charset="UTF-8"> </head> <body> <h1>$1</h1> $3 $4 </body> </html> """ % [title, style, content, footer] proc relativePath(path, cwd: string): string = var path2 = path if cwd == "/": return path else: path2.delete(0, cwd.len-1) var relpath = path2.replace("\\", "/") if (not relpath.endsWith("/")) and (not path.existsFile): relpath = relpath&"/" if not relpath.startsWith("/"): relpath = "/"&relpath return relpath proc relativeParent(path, cwd: string): string = var relparent = path.parentDir.relativePath(cwd) if relparent == "": return "/" else: 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: 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: newHttpHeaders()) proc sendStaticFile(settings: NimHttpSettings, path: string): NimHttpResponse = let mimes = settings.mimes var ext = path.splitFile.ext if ext == "": ext = ".txt" ext = ext[1 .. ^1] let mimetype = mimes.getMimetype(ext.toLowerAscii) var file = path.readFile return (code: Http200, content: file, headers: {"Content-Type": mimetype}.newHttpHeaders) proc sendDirContents(settings: NimHttpSettings, path: string): NimHttpResponse = let cwd = settings.directory var res: NimHttpResponse var files = newSeq[string](0) if path != cwd and path != cwd&"/" and path != cwd&"\\": files.add """<li class="i-back entypo"><a href="$1">..</a></li>""" % [path.relativeParent(cwd)] var title = "Index of " & path.relativePath(cwd) for i in walkDir(path): let name = i.path.extractFilename let relpath = i.path.relativePath(cwd) if name == "index.html" or name == "index.htm": return sendStaticFile(settings, i.path) if i.path.existsDir: files.add """<li class="i-folder entypo"><a href="$1">$2</a></li>""" % [relpath, name] else: files.add """<li class="i-file entypo"><a href="$1">$2</a></li>""" % [relpath, name] let ul = """ <ul> $1 </ul> """ % [files.join("\n")] res = (code: Http200, content: h_page(settings, ul, title), headers: newHttpHeaders()) return res proc printReqInfo(settings: NimHttpSettings, req: Request) = if not settings.logging: return echo getTime().local, " - ", req.hostname, " ", req.reqMethod, " ", req.url.path proc handleCtrlC() {.noconv.} = echo "\nExiting..." quit() setControlCHook(handleCtrlC) proc serve*(settings: NimHttpSettings) = var server = newAsyncHttpServer() 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 != HttpGet: res = sendNotImplemented(settings, path) elif path.existsDir: res = sendDirContents(settings, path) elif path.existsFile: res = sendStaticFile(settings, path) else: res = sendNotFound(settings, path) await req.respond(res.code, res.content, res.headers) echo settings.name, " v", settings.version, " started on port ", int(settings.port), "." echo "Serving directory ", settings.directory asyncCheck server.serve(settings.port, handleHttpRequest, settings.address) when isMainModule: var port = Port(1337) var address = "" var logging = false var www = getCurrentDir() for kind, key, val in getopt(): case kind of cmdLongOption, cmdShortOption: case key of "log", "l": logging = true of "help", "h": echo usage quit(0) of "version", "v": echo version quit(0) of "port", "p": try: port = Port(val.parseInt) except: if val == "": echo "Port not set." quit(2) else: echo "Error: Invalid port: '", val, "'" echo "Running on default port instead." else: discard of cmdArgument: var dir: string if key.isAbsolute: dir = key else: dir = www/key if dir.existsDir: www = expandFilename dir else: echo "Error: Directory '"&dir&"' does not exist." quit(1) else: discard var settings: NimHttpSettings settings.directory = www settings.logging = logging settings.mimes = newMimeTypes() settings.address = address settings.name = name settings.version = version settings.port = port serve(settings) runForever() |