From 732f0c40a644866adf7dab1363c4dcc76ab8f128 Mon Sep 17 00:00:00 2001 From: Michael Adams Date: Sun, 15 Jan 2023 18:23:52 -0800 Subject: [PATCH] Revise app to be dual-stack IPv4+IPv6 --- LICENSE | 2 +- README.md | 6 +++--- src/nimhttpd.nim | 44 ++++++++++++++++++++------------------ src/nimhttpdpkg/config.nim | 2 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/LICENSE b/LICENSE index e50bfb8..ac8c6b8 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md index 038df26..14dc735 100644 --- a/README.md +++ b/README.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) diff --git a/src/nimhttpd.nim b/src/nimhttpd.nim index 1946b90..691f832 100644 --- a/src/nimhttpd.nim +++ b/src/nimhttpd.nim @@ -1,7 +1,6 @@ import asyncdispatch, asynchttpserver, - macros, mimetypes, nativesockets, os, @@ -24,10 +23,8 @@ const 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 @@ let usage = """ $1 v$2 - $3 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 @@ type mimes*: MimeDb port*: Port title*: string - address*: string + address4*: string + address6*: string name*: string version*: string headers*: HttpHeaders @@ -159,14 +157,14 @@ proc handleCtrlC() {.noconv.} = 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 @@ proc serve*(settings: NimHttpSettings) = 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 @@ when isMainModule: 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 @@ when isMainModule: 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 @@ when isMainModule: 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 diff --git a/src/nimhttpdpkg/config.nim b/src/nimhttpdpkg/config.nim index 557b991..f1c1523 100644 --- a/src/nimhttpdpkg/config.nim +++ b/src/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!" -- 2.45.2