about summary refs log tree commit diff stats
path: root/adapter/protocol/ftp.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-12-15 15:35:38 +0100
committerbptato <nincsnevem662@gmail.com>2023-12-15 15:51:49 +0100
commitfc09f0bee88cbbb90a89798a55a1d405f62ce8a7 (patch)
tree3c23ee4ef94491e62772a41cd7392d4bd204eabc /adapter/protocol/ftp.nim
parenta55c1ab95ecb0d79c73bbb82edd99b737c601260 (diff)
downloadchawan-fc09f0bee88cbbb90a89798a55a1d405f62ce8a7.tar.gz
ftp: fix unnecessary slashes being added to path; move bindings/curl
also in ftp: clean up resources before exit
Diffstat (limited to 'adapter/protocol/ftp.nim')
-rw-r--r--adapter/protocol/ftp.nim51
1 files changed, 30 insertions, 21 deletions
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()
f='#n279'>279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444