about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--adapter/protocol/curl.nim (renamed from src/bindings/curl.nim)1
-rw-r--r--adapter/protocol/curlerrors.nim2
-rw-r--r--adapter/protocol/curlwrap.nim2
-rw-r--r--adapter/protocol/ftp.nim51
-rw-r--r--adapter/protocol/gopher.nim4
-rw-r--r--adapter/protocol/http.nim2
7 files changed, 39 insertions, 29 deletions
diff --git a/Makefile b/Makefile
index fb5acfc3..ca9d67e3 100644
--- a/Makefile
+++ b/Makefile
@@ -72,7 +72,7 @@ $(OUTDIR_CGI_BIN)/cha-finger: adapter/protocol/cha-finger
 	cp adapter/protocol/cha-finger $(OUTDIR_CGI_BIN)
 
 $(OUTDIR_CGI_BIN)/http: adapter/protocol/http.nim adapter/protocol/curlwrap.nim \
-		adapter/protocol/curlerrors.nim src/bindings/curl.nim \
+		adapter/protocol/curlerrors.nim adapter/protocol/curl.nim \
 		src/utils/twtstr.nim
 	@mkdir -p "$(OUTDIR_CGI_BIN)"
 	$(NIMC) $(FLAGS) --nimcache:"$(OBJDIR)/$(TARGET)/http" -d:curlLibName:$(CURLLIBNAME) \
@@ -95,14 +95,14 @@ $(OUTDIR_CGI_BIN)/file: adapter/protocol/file.nim adapter/protocol/dirlist.nim \
 $(OUTDIR_CGI_BIN)/ftp: adapter/protocol/ftp.nim adapter/protocol/dirlist.nim \
 		src/utils/twtstr.nim src/utils/strwidth.nim src/data/charwidth.nim \
 		res/map/EastAsianWidth.txt src/loader/connecterror.nim \
-		src/types/opt.nim src/bindings/curl.nim
+		src/types/opt.nim adapter/protocol/curl.nim
 	@mkdir -p "$(OUTDIR_CGI_BIN)"
 	$(NIMC) $(FLAGS) -d:curlLibName:$(CURLLIBNAME) --nimcache:"$(OBJDIR)/$(TARGET)/ftp" \
 		-o:"$(OUTDIR_CGI_BIN)/ftp" adapter/protocol/ftp.nim
 
 $(OUTDIR_CGI_BIN)/gopher: adapter/protocol/gopher.nim adapter/protocol/curlwrap.nim \
 		adapter/protocol/curlerrors.nim adapter/gophertypes.nim \
-		src/bindings/curl.nim src/loader/connecterror.nim \
+		adapter/protocol/curl.nim src/loader/connecterror.nim \
 		src/utils/twtstr.nim
 	@mkdir -p "$(OUTDIR_CGI_BIN)"
 	$(NIMC) $(FLAGS) -d:curlLibName:$(CURLLIBNAME) --nimcache:"$(OBJDIR)/$(TARGET)/gopher" \
diff --git a/src/bindings/curl.nim b/adapter/protocol/curl.nim
index e8d86cb2..a12e81c4 100644
--- a/src/bindings/curl.nim
+++ b/adapter/protocol/curl.nim
@@ -381,6 +381,7 @@ proc `==`*(a: CURLM, b: typeof(nil)): bool {.borrow.}
 
 proc curl_global_init*(flags: clong): CURLcode
 proc curl_global_cleanup*()
+proc curl_free*(p: pointer)
 
 proc curl_easy_init*(): CURL
 proc curl_easy_cleanup*(handle: CURL)
diff --git a/adapter/protocol/curlerrors.nim b/adapter/protocol/curlerrors.nim
index 3bb6cf6d..ce50cd82 100644
--- a/adapter/protocol/curlerrors.nim
+++ b/adapter/protocol/curlerrors.nim
@@ -1,4 +1,4 @@
-import bindings/curl
+import curl
 import loader/connecterror
 
 func curlErrorToChaError*(res: CURLcode): ConnectErrorCode =
diff --git a/adapter/protocol/curlwrap.nim b/adapter/protocol/curlwrap.nim
index 41ef5355..523c260f 100644
--- a/adapter/protocol/curlwrap.nim
+++ b/adapter/protocol/curlwrap.nim
@@ -1,4 +1,4 @@
-import bindings/curl
+import curl
 
 template setopt*(curl: CURL, opt: CURLoption, arg: typed) =
   discard curl_easy_setopt(curl, opt, arg)
diff --git a/adapter/protocol/ftp.nim b/adapter/protocol/ftp.nim
index 1a406743..064c0396 100644
--- a/adapter/protocol/ftp.nim
+++ b/adapter/protocol/ftp.nim
@@ -2,11 +2,11 @@ import std/envvars
 import std/options
 import std/strutils
 
+import curl
 import curlerrors
 import curlwrap
 import dirlist
 
-import bindings/curl
 import loader/connecterror
 import types/opt
 import utils/twtstr
@@ -154,6 +154,10 @@ proc main() =
   doAssert curl != nil
   let opath = getEnv("MAPPED_URI_PATH")
   let path = percentDecode(opath)
+  let op = FtpHandle(
+    curl: curl,
+    dirmode: path.len > 0 and path[^1] == '/'
+  )
   let url = curl_url()
   const flags = cuint(CURLU_PATH_AS_IS)
   url.set(CURLUPART_SCHEME, getEnv("MAPPED_URI_SCHEME"), flags)
@@ -171,39 +175,44 @@ proc main() =
   # necessary to specify absolute paths.
   # This is incredibly confusing, and probably not what the user wanted.
   # So we work around it by adding the extra slash ourselves.
+  #
+  # But before that, we take the serialized URL without the path for
+  # setting the base URL:
+  url.set(CURLUPART_PATH, opath, flags)
+  if op.dirmode:
+    let surl = url.get(CURLUPART_URL, cuint(CURLU_PUNY2IDN))
+    if surl == nil:
+      stdout.write("Cha-Control: ConnectionError " & $int(ERROR_INVALID_URL))
+      curl_url_cleanup(url)
+      curl_easy_cleanup(curl)
+      return
+    op.base = $surl
+    op.path = path
+    curl_free(surl)
+  # Now for the workaround:
   url.set(CURLUPART_PATH, '/' & opath, flags)
+  # Set opts for the request
   curl.setopt(CURLOPT_CURLU, url)
-  let dirmode = path.len > 0 and path[^1] == '/'
-  let op = FtpHandle(
-    curl: curl,
-    dirmode: dirmode
-  )
   curl.setopt(CURLOPT_HEADERDATA, op)
   curl.setopt(CURLOPT_HEADERFUNCTION, curlWriteHeader)
   curl.setopt(CURLOPT_WRITEDATA, op)
   curl.setopt(CURLOPT_WRITEFUNCTION, curlWriteBody)
   curl.setopt(CURLOPT_FTP_FILEMETHOD, CURLFTPMETHOD_SINGLECWD)
-  if dirmode:
-    const flags = cuint(CURLU_PUNY2IDN)
-    let surl = url.get(CURLUPART_URL, flags)
-    if surl == nil:
-      stdout.write("Cha-Control: ConnectionError " & $int(ERROR_INVALID_URL))
-      return
-    op.base = $surl
-    op.path = path
   let purl = getEnv("ALL_PROXY")
   if purl != "":
     curl.setopt(CURLOPT_PROXY, purl)
   if getEnv("REQUEST_METHOD") != "GET":
+    # fail
     let code = $int(ERROR_INVALID_METHOD)
     stdout.write("Cha-Control: ConnectionError " & $code & "\n")
-    return
-  let res = curl_easy_perform(curl)
-  if res != CURLE_OK:
-    if not op.statusline:
-      stdout.write(getCurlConnectionError(res))
-  elif op.dirmode:
-    op.finish()
+  else:
+    let res = curl_easy_perform(curl)
+    if res != CURLE_OK:
+      if not op.statusline:
+        stdout.write(getCurlConnectionError(res))
+    elif op.dirmode:
+      op.finish()
+  curl_url_cleanup(url)
   curl_easy_cleanup(curl)
 
 main()
diff --git a/adapter/protocol/gopher.nim b/adapter/protocol/gopher.nim
index 7ee7b625..7742613c 100644
--- a/adapter/protocol/gopher.nim
+++ b/adapter/protocol/gopher.nim
@@ -1,11 +1,11 @@
 import std/envvars
 
-import curlwrap
+import curl
 import curlerrors
+import curlwrap
 
 import ../gophertypes
 
-import bindings/curl
 import loader/connecterror
 import utils/twtstr
 
diff --git a/adapter/protocol/http.nim b/adapter/protocol/http.nim
index 45a78c46..d973bd1d 100644
--- a/adapter/protocol/http.nim
+++ b/adapter/protocol/http.nim
@@ -1,10 +1,10 @@
 import std/envvars
 import std/strutils
 
+import curl
 import curlerrors
 import curlwrap
 
-import bindings/curl
 import utils/twtstr
 
 type