all repos — nimhttpd @ de7bbba7a6038154c144acf2e920d85be16c9993

A useful static file web server.

Merge pull request #13 from ire4ever1190/custom-headers

Fabio Cevasco h3rald@h3rald.com
Thu, 08 Dec 2022 19:24:03 +0100
commit

de7bbba7a6038154c144acf2e920d85be16c9993

parent

098f6255cde4b00cf331d0e49b6c3e93161d273f

2 files changed, 16 insertions(+), 5 deletions(-)

jump to
M README.mdREADME.md

@@ -9,7 +9,7 @@ _NimHTTPd_ is a minimal web server that can be used to serve static files.

## Usage -**nimhttpd** **[** **-6** **-p:**_port_ **-t:**_title_ **-a:**_address_ **]** **[** _directory_ **]** +**nimhttpd** **[** **-6** **-p:**_port_ **-t:**_title_ **-a:**_address_ **-H**:_"key: val"_ **]** **[** _directory_ **]** Where:

@@ -19,3 +19,4 @@ unavailable, the number will be incremented until an available port is found.

- _address_ is the address to bind to (default: 0.0.0.0). - _title_ is the title to use when listing the contents of a directory. - _-6_ enables IPv6 support +- _-H_ is a custom header (Specified like in curl)
M src/nimhttpd.nimsrc/nimhttpd.nim

@@ -7,9 +7,10 @@ os,

parseopt, strutils, times, - uri + uri, + strscans -from httpcore import HttpMethod, HttpHeaders +from httpcore import HttpMethod, HttpHeaders, parseHeader import nimhttpdpkg/config

@@ -40,6 +41,7 @@ -p, --port The port to listen to (default: $5).

-a, --address The address to listen to (default: $6). If the specified port is unavailable, the number will be incremented until an available port is found. -6, --ipv6 Listen to IPv6 addresses. + -H --header Add a custom header. Multiple headers can be added """ % [name, version, description, author, $portDefault, $addressDefault]

@@ -57,7 +59,7 @@ title*: string

address*: string name*: string version*: string - + headers*: HttpHeaders proc h_page(settings:NimHttpSettings, content, title, subtitle: string): string = var footer = """<div id="footer">$1 v$2</div>""" % [settings.name, settings.version] result = """

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

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

@@ -180,6 +183,8 @@ elif path.fileExists:

res = sendStaticFile(settings, path) else: res = sendNotFound(settings, path) + for key, value in settings.headers: + res.headers[key] = value await req.respond(res.code, res.content, res.headers) echo genMsg(settings) asyncCheck server.serve(settings.port, handleHttpRequest, settings.address, -1, domain)

@@ -191,6 +196,7 @@ var address = addressDefault

var logging = false var www = getCurrentDir() var title = "Index" + var headers = newHttpHeaders() for kind, key, val in getopt(): case kind

@@ -220,6 +226,9 @@ quit(2)

else: echo "Error: Invalid port: '", val, "'" echo "Running on default port instead." + of "header", "H": + let (key, values) = parseHeader(val) + headers[key] = values else: discard of cmdArgument:

@@ -251,6 +260,7 @@ settings.name = name

settings.title = title settings.version = version settings.port = Port(port) - + settings.headers = headers + serve(settings) runForever()