nimhttpd icon indicating copy to clipboard operation
nimhttpd copied to clipboard

nimhttpd hangs with `127.0.0.1` but not `localhost` in URL

Open hamidb80 opened this issue 2 years ago • 2 comments

Problem

when I use localhost in URL everything works fine, but when I use http://127.0.0.1:PORT, nimhttpd just does not respond anymore and hangs

Code Example

the provided code copies a image 50 times into the ./temp folder and creates a index.html file for you to see all of them.

import std/[os, sequtils, strutils, math, strformat]
import karax/[karaxdsl, vdom]


func indexPage(root: string, images: seq[string]): VNode =
  buildHtml body:
    for imageName in images:
      img(src = root & imageName, height = "100")
      span: text imageName
      hr()

proc repeatedCopy(filePath, destDir: string, times: int) =
  ## copies file `filePath` into directory `destDir`
  ## for `times` times
  let
    (_, _, ext) = splitFile filePath
    numberLen = toInt ceil log10 tofloat times
  for n in 1..times:
    copyFile filePath, destDir / align($n, numberLen, '0') & ext

proc extractImages(dir: string): seq[string] =
  for path in walkFiles dir/"*":
    let (_, name, ext) = splitFile path
    case ext.toLowerAscii
    of ".png", ".jpg", ".jpeg":
      result.add name & ext

when isMainModule:
  let
    port = parseint paramStr 1
    imagePathToBeCopied = paramStr 2
    localhost = fmt"http://localhost:{port}/"
    ip = fmt"http://127.0.0.1:{port}/"

  discard existsOrCreateDir "./temp"

  repeatedCopy imagePathToBeCopied, "./temp", 50

  writeFile "./temp/index.html":
    # $indexPage(localhost, extractImages "./temp") # <<------------- works fine
    $indexPage(ip, extractImages "./temp") # does not work for some images

run with

nim r play.nim PORT PATH_TO_IMAGE
nimhttpd ./temp -p:PORT

Result

As you can probably see some images does not load at all and reloading the page does not help

image

Additional Info

windows 10 64bit
nim 1.6.12

hamidb80 avatar Jul 04 '23 06:07 hamidb80

I don't know if its related, but I also had nimhttpd just hang. I am not sure why.

treeform avatar Jan 18 '24 19:01 treeform

Hi, I might have the same issue: https://github.com/planetis-m/naylib/issues/122 In the web console this is printed indefinitely:

index.js:759 still waiting on run dependencies:
(anonymous) @ index.js:759
index.js:761 dependency: wasm-instantiate
(anonymous) @ index.js:761
index.js:764 (end of list)

planetis-m avatar Jul 02 '24 21:07 planetis-m