all repos — nimhttpd @ 7e0342582a912e00bddc03aaaa796b106c5caaeb

A useful static file web server.

Revise app to be dual-stack IPv4+IPv6
Michael Adams unquietwiki@gmail.com
Sun, 15 Jan 2023 18:23:52 -0800
commit

7e0342582a912e00bddc03aaaa796b106c5caaeb

parent

ee386274a8c0491285ac4931fb36175267c42580

4 files changed, 28 insertions(+), 26 deletions(-)

jump to
M LICENSELICENSE

@@ -1,6 +1,6 @@

The MIT License (MIT) -Copyright (c) 2015-2021 Fabio Cevasco +Copyright (c) 2015-2023 Fabio Cevasco Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
M README.mdREADME.md

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

## Usage -**nimhttpd** **[** **-6** **-p:**_port_ **-t:**_title_ **-a:**_address_ **-H**:_"key: val"_ **]** **[** _directory_ **]** +**nimhttpd** **[** **-p:**_port_ **-t:**_title_ **-a:**_address_ **-6:**_ipv6_ **-H**:_"key: val"_ **]** **[** _directory_ **]** Where: - _directory_ is the directory to serve (default: current directory). - _port_ is the port to listen to (default: 1337). If the specified port is unavailable, the number will be incremented until an available port is found. -- _address_ is the address to bind to (default: 0.0.0.0). +- _address_ is the address to bind to (default: localhost; use "0.0.0.0" to accept any IPv4 address). +- _ipv6_ is the IPv6 address to bind to (default: localhost; use "::" to accept any IPv6 address). - _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

@@ -1,7 +1,6 @@

import asyncdispatch, asynchttpserver, - macros, mimetypes, nativesockets, os,

@@ -24,10 +23,8 @@ author = pkgAuthor

addressDefault = "localhost" portDefault = 1337 -var domain = AF_INET - let usage = """ $1 v$2 - $3 - (c) 2014-2022 $4 + (c) 2014-2023 $4 Usage: nimhttpd [-p:port] [directory]

@@ -38,9 +35,9 @@

Options: -t, --title The title to use in index pages (default: Index) -p, --port The port to listen to (default: $5). - -a, --address The address to listen to (default: $6). If the specified port is + -a, --address The IPv4 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. + -6, --ipv6 The IPv6 address to listen to (default: $6). -H --header Add a custom header. Multiple headers can be added """ % [name, version, description, author, $portDefault, $addressDefault]

@@ -56,7 +53,8 @@ directory*: string

mimes*: MimeDb port*: Port title*: string - address*: string + address4*: string + address6*: string name*: string version*: string headers*: HttpHeaders

@@ -159,14 +157,14 @@

setControlCHook(handleCtrlC) proc genMsg(settings: NimHttpSettings): string = - let url = "http://$1:$2/" % [settings.address, $settings.port.int] let t = now() let pid = getCurrentProcessId() result = """$1 v$2 -Address: $3 -Directory: $4 -Current Time: $5 -PID: $6""" % [settings.name, settings.version, url, settings.directory.quoteShell, $t, $pid] +Address4: $3 +Address6: $4 +Directory: $5 +Current Time: $6 +PID: $7""" % [settings.name, settings.version, settings.address4, settings.address6, settings.directory.quoteShell, $t, $pid] proc serve*(settings: NimHttpSettings) = var server = newAsyncHttpServer()

@@ -187,12 +185,14 @@ 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) + asyncCheck server.serve(settings.port, handleHttpRequest, settings.address4, -1, AF_INET) + asyncCheck server.serve(settings.port, handleHttpRequest, settings.address6, -1, AF_INET6) when isMainModule: var port = portDefault - var address = addressDefault + var address4 = addressDefault + var address6 = addressDefault var logging = false var www = getCurrentDir() var title = "Index"

@@ -210,10 +210,10 @@ quit(0)

of "version", "v": echo version quit(0) - of "-ipv6", "6": - domain = AF_INET6 of "address", "a": - address = val + address4 = val + of "ipv6", "6": + address6 = val of "title", "t": title = val of "port", "p":

@@ -245,9 +245,10 @@ quit(1)

else: discard - var addrInfo = getAddrInfo(address, Port(port), domain) - if addrInfo == nil: - echo "Error: Could not resolve address '"&address&"'." + var addrInfo4 = getAddrInfo(address4, Port(port), AF_INET) + var addrInfo6 = getAddrInfo(address6, Port(port), AF_INET6) + if (addrInfo4 == nil) and (addrInfo6 == nil): + echo "Error: Could not resolve given IPv4 or IPv6 addresses." quit(1) var settings: NimHttpSettings

@@ -255,7 +256,8 @@ settings.directory = www

settings.logging = logging settings.mimes = newMimeTypes() settings.mimes.register("htm", "text/html") - settings.address = address + settings.address4 = address4 + settings.address6 = address6 settings.name = name settings.title = title settings.version = version
M src/nimhttpdpkg/config.nimsrc/nimhttpdpkg/config.nim

@@ -1,5 +1,5 @@

const pkgTitle* = "NimHTTPd" - pkgVersion* = "1.4.1" + pkgVersion* = "1.5.0" pkgAuthor* = "Fabio Cevasco & Michael Adams" pkgDescription* = "A tiny static file web server. IPv4 & IPv6 supported!"