~h3rald/nimhttpd

29a358e530a960ea007341b07378af905e6d2565 — Jake Leahy 1 year, 9 months ago 19697d5
Add support for custom headers
2 files changed, 19 insertions(+), 4 deletions(-)

M README.md
M src/nimhttpd.nim
M README.md => README.md +2 -1
@@ 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 @@ Where:
- _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.nim => src/nimhttpd.nim +17 -3
@@ 7,7 7,8 @@ import
  parseopt,
  strutils, 
  times, 
  uri
  uri,
  strscans
  
from httpcore import HttpMethod, HttpHeaders



@@ 40,6 41,7 @@ let usage = """ $1 v$2 - $3
    -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 @@ type
    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 serve*(settings: NimHttpSettings) =
    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 @@ proc serve*(settings: NimHttpSettings) =
      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 @@ when isMainModule:
  var logging = false
  var www = getCurrentDir()
  var title = "Index"
  var headers = newHttpHeaders()
  
  for kind, key, val in getopt():
    case kind


@@ 220,6 226,13 @@ when isMainModule:
          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 @@ when isMainModule:
  settings.title = title
  settings.version = version
  settings.port = Port(port)
  
  settings.headers = headers

  serve(settings)
  runForever()