all repos — nimhttpd @ 49edff091c899834d067b77f06b4c9ebd6c048cd

A useful static file web server.

Add support for custom headers
Jake Leahy jake@leahy.dev
Thu, 08 Dec 2022 20:31:14 +1100
commit

49edff091c899834d067b77f06b4c9ebd6c048cd

parent

098f6255cde4b00cf331d0e49b6c3e93161d273f

2 files changed, 19 insertions(+), 4 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,7 +7,8 @@ os,

parseopt, strutils, times, - uri + uri, + strscans from httpcore import HttpMethod, HttpHeaders

@@ -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,13 @@ quit(2)

else: echo "Error: Invalid port: '", val, "'" echo "Running on default port instead." + of "header", "H": + var key, value: string + if val.scanf("$+: $+", key, value): + headers[key] = value + else: + echo "Invalid header ", val, " passed. Should be in the form \"key: value\"" + quit QuitFailure else: discard of cmdArgument:

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

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