nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.23k stars 1.47k forks source link

httpClient with Uri object #21872

Open zivoslav opened 1 year ago

zivoslav commented 1 year ago

Description

If I use httpClient with Uri object and if I parse url without "http" and set scheme directly, program exit with error.

Run


echo "compile with full url, will be OK"
nim c -r -d:ssl --define:http  ./devel.nim

echo "compile with url and set scheme, will be ERROR"
nim c -r -d:ssl --define:scheme  ./devel.nim

echo "with with full url , request url AS STRING, will be OK"
nim c -r -d:ssl --define:http --define:str  ./devel.nim

echo "compile with url and set scheme, request url AS STRING, will be OK"
nim c -r -d:ssl --define:scheme --define:str ./devel.nim

for 4 variants of this program:


import std/httpcore
import std/uri
import std/httpclient

when defined http:
  var url = parseUri "https://nim-lang.org"
when defined scheme:
  var url = parseUri "nim-lang.org"
  url.scheme = "https"

echo "url is ", $url

let metoda = HttpGet
var client = newHttpClient()

try:
  when defined str:
    let response = client.request(url = $url, httpMethod = metoda)
  else:
    let response = client.request(url = url, httpMethod = metoda)

  echo response.status
finally:
  client.close()

Nim Version

nim -v Nim Compiler Version 1.6.12 [Linux: amd64] Compiled at 2023-03-10 Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 1aa9273640c0c51486cf3a7b67282fe58f360e91 active boot switches: -d:release

------------------------------------------------------------------------- AND TOO ----------------------

nim -v Nim Compiler Version 1.9.3 [Linux: amd64] Compiled at 2023-05-08 Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 71f2e1a502ad231e3356217398e2d7fcd6137967 active boot switches: -d:release

Current Output

~/.choosenim/toolchains/nim-1.6.12/lib/pure/httpclient.nim(1110) request
~/.choosenim/toolchains/nim-1.6.12/lib/pure/httpclient.nim(1011) requestAux
~/.choosenim/toolchains/nim-1.6.12/lib/pure/httpclient.nim(903) newConnection
~/.choosenim/toolchains/nim-1.6.12/lib/pure/net.nim(2012) dial
~/.choosenim/toolchains/nim-1.6.12/lib/pure/nativesockets.nim(308) getAddrInfo
~/.choosenim/toolchains/nim-1.6.12/lib/pure/includes/oserr.nim(95) raiseOSError
Error: unhandled exception: No such file or directory
Additional info: Name or service not known [OSError]
Error: execution of an external program failed: './devel'

Expected Output

200 OK

Possible Solution

No response

Additional Information

No response

bung87 commented 1 year ago

what's expected here? parseUri with a invalid url

z------------- commented 1 year ago
import std/uri except `$`

echo parseUri "https://nim-lang.org"

var url = parseUri "nim-lang.org"
url.scheme = "https"
echo url

Output:

(scheme: "https", username: "", password: "", hostname: "nim-lang.org", port: "", path: "", query: "", anchor: "", opaque: false, isIpv6: false)
(scheme: "https", username: "", password: "", hostname: "", port: "", path: "nim-lang.org", query: "", anchor: "", opaque: false, isIpv6: false)

200 OK would definitely be the wrong result. If anything, httpclient should require that the passed Uri has a hostname.

xTrayambak commented 6 months ago

The parser is working as expected. You can't just expect the user to eventually specify the scheme if it's necessary for the HTTP client to work, you have to ensure that it's there in the first place.

hamidb80 commented 5 months ago

The parser is working as expected

shouldn't at least the parseUri function raise an exception?

xTrayambak commented 5 months ago

Oh, wait, I didn't see that. I thought it was throwing an error when no scheme was provided and the issue poster was talking about that.