all repos — nimhttpd @ 2be0bf1541f8e22c67f18326dd2f1554520b6146

A useful static file web server.

IPv6 support
Michael Adams unquietwiki@gmail.com
Thu, 12 May 2022 21:21:45 -0700
commit

2be0bf1541f8e22c67f18326dd2f1554520b6146

parent

cac12c55b7fdf1da406e52494d8f40b7b7cb8a7d

2 files changed, 25 insertions(+), 32 deletions(-)

jump to
M src/nimhttpd.nimsrc/nimhttpd.nim

@@ -1,30 +1,32 @@

import + asyncdispatch, asynchttpserver, - asyncdispatch, + mimetypes, nativesockets, - os, strutils, - mimetypes, + os, + parseopt, + strutils, times, - parseopt, uri - + from httpcore import HttpMethod, HttpHeaders import nimhttpdpkg/config - -const +const name = pkgTitle version = pkgVersion style = "style.css".slurp description = pkgDescription author = pkgAuthor - addressDefault = "0.0.0.0" + addressDefault = "localhost" portDefault = 1337 + +var domain = AF_INET let usage = """ $1 v$2 - $3 - (c) 2014-2021 $4 + (c) 2014-2022 $4 Usage: nimhttpd [-p:port] [directory]

@@ -37,7 +39,8 @@ -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 unavailable, the number will be incremented until an available port is found. -""" % [name, version, description, author, $portDefault, addressDefault] + -6, --ipv6 Listen to IPv6 addresses. +""" % [name, version, description, author, $portDefault, $addressDefault] type

@@ -55,19 +58,6 @@ address*: string

name*: string version*: string -proc bindAvailablePort*(handle: SocketHandle, p: int): Port = - var check = -1 - var port = p - 1 - while check < 0'i32: - port += 1 - var name: Sockaddr_in - name.sin_family = typeof(name.sin_family)(toInt(AF_INET)) - name.sin_port = htons(uint16(port)) - name.sin_addr.s_addr = htonl(INADDR_ANY) - check = bindAddr(handle, cast[ptr SockAddr](addr(name)), sizeof(name).Socklen) - result = getLocalAddr(handle, AF_INET)[1] - - proc h_page(settings:NimHttpSettings, content, title, subtitle: string): string = var footer = """<div id="footer">$1 v$2</div>""" % [settings.name, settings.version] result = """

@@ -192,7 +182,7 @@ else:

res = sendNotFound(settings, path) await req.respond(res.code, res.content, res.headers) echo genMsg(settings) - asyncCheck server.serve(settings.port, handleHttpRequest, settings.address) + asyncCheck server.serve(settings.port, handleHttpRequest, settings.address, -1, domain) when isMainModule:

@@ -214,6 +204,8 @@ quit(0)

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

@@ -244,9 +236,10 @@ quit(1)

else: discard - let socket = createAsyncNativeSocket() - let availablePort = bindAvailablePort(socket.SocketHandle, port) - socket.closeSocket() + var addrInfo = getAddrInfo(address, Port(port), domain) + if addrInfo == nil: + echo "Error: Could not resolve address '"&address&"'." + quit(1) var settings: NimHttpSettings settings.directory = www

@@ -257,7 +250,7 @@ settings.address = address

settings.name = name settings.title = title settings.version = version - settings.port = availablePort - + settings.port = Port(port) + serve(settings) runForever()
M src/nimhttpdpkg/config.nimsrc/nimhttpdpkg/config.nim

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

const pkgTitle* = "NimHTTPd" - pkgVersion* = "1.2.0" - pkgAuthor* = "Fabio Cevasco" - pkgDescription* = "A tiny static file web server." + pkgVersion* = "1.3.0" + pkgAuthor* = "Fabio Cevasco & Michael Adams" + pkgDescription* = "A tiny static file web server for IPv6."