From 2277b19905ae3fb736166eb8f0d2d5c02f29bef9 Mon Sep 17 00:00:00 2001 From: bptato Date: Tue, 19 Sep 2023 21:45:20 +0200 Subject: ftp, file: better dirlist, fix FTP path issue * Dirlist is now unified across ftp and file loaders. It's basically a copycat of w3m's FTP dirlist, because I like how it looks. * We now hack around the cURL FTP path problem by always prepending a slash to the path. This is probably closer aligned with expectations than the default behavior. --- src/loader/dirlist.nim | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/loader/file.nim | 47 +++++++++++++++++++++++++++------------ src/loader/ftp.nim | 58 ++++++++++++++++++++++++++++++------------------ 3 files changed, 130 insertions(+), 35 deletions(-) create mode 100644 src/loader/dirlist.nim (limited to 'src/loader') diff --git a/src/loader/dirlist.nim b/src/loader/dirlist.nim new file mode 100644 index 00000000..5de79084 --- /dev/null +++ b/src/loader/dirlist.nim @@ -0,0 +1,60 @@ +import algorithm + +import utils/twtstr + +type DirlistItemType = enum + ITEM_FILE, ITEM_LINK, ITEM_DIR + +type DirlistItem* = object + name*: string + modified*: string + case t*: DirlistItemType + of ITEM_LINK: + linkto*: string + of ITEM_FILE: + nsize*: int + of ITEM_DIR: + discard + +type NameWidthTuple = tuple[name: string, width: int] + +func makeDirlist*(items: seq[DirlistItem]): string = + var names: seq[NameWidthTuple] + var maxw = 20 + for item in items: + var name = item.name + if item.t == ITEM_LINK: + name &= '@' + elif item.t == ITEM_DIR: + name &= '/' + let w = name.width() + maxw = max(w, maxw) + names.add((name, w)) + names.sort(proc(a, b: NameWidthTuple): int = cmp(a.name, b.name)) + var outs = "[Upper Directory]\n" + for i in 0 ..< items.len: + let item = items[i] + var (name, width) = names[i] + var path = percentEncode(item.name, PathPercentEncodeSet) + if item.t == ITEM_LINK: + if item.linkto.len > 0 and item.linkto[^1] == '/': + # If the target is a directory, treat it as a directory. (For FTP.) + path &= '/' + elif item.t == ITEM_DIR: + path &= '/' + var line = "" + while width <= maxw: + if width mod 2 == 0: + line &= ' ' + else: + line &= '.' + inc width + if line[^1] != ' ': + line &= ' ' + line &= htmlEscape(item.modified) + if item.t == ITEM_FILE: + line &= ' ' & convert_size(item.nsize) + elif item.t == ITEM_LINK: + line &= " -> " & htmlEscape(item.linkto) + outs &= line & '\n' + return outs diff --git a/src/loader/file.nim b/src/loader/file.nim index b627e0c7..40a19981 100644 --- a/src/loader/file.nim +++ b/src/loader/file.nim @@ -2,8 +2,10 @@ import algorithm import os import streams import tables +import times import loader/connecterror +import loader/dirlist import loader/headers import loader/loaderhandle import types/url @@ -29,30 +31,47 @@ proc loadDir(handle: LoaderHandle, url: URL, path: string) =

Directory list of """ & path & """

-[DIR] 
../
+
 """)
   var fs: seq[(PathComponent, string)]
   for pc, file in walkDir(path, relative = true):
     fs.add((pc, file))
   fs.sort(cmp = proc(a, b: (PathComponent, string)): int = cmp(a[1], b[1]))
+  var items: seq[DirlistItem]
   for (pc, file) in fs:
+    let fullpath = path / file
+    var info: FileInfo
+    try:
+      info = getFileInfo(fullpath, followSymlink = false)
+    except OSError:
+      continue
+    let modified = $info.lastWriteTime.local().format("MMM/dd/yyyy HH:MM")
     case pc
     of pcDir:
-      t handle.sendData("[DIR]  ")
+      items.add(DirlistItem(
+        t: ITEM_DIR,
+        name: file,
+        modified: modified
+      ))
     of pcFile:
-      t handle.sendData("[FILE] ")
+      items.add(DirlistItem(
+        t: ITEM_FILE,
+        name: file,
+        modified: modified,
+        nsize: info.size
+      ))
     of pcLinkToDir, pcLinkToFile:
-      t handle.sendData("[LINK] ")
-    var fn = file
-    if pc == pcDir:
-      fn &= '/'
-    t handle.sendData("" & fn & "")
-    if pc in {pcLinkToDir, pcLinkToFile}:
-      discard handle.sendData(" -> " & expandSymlink(path / file))
-    t handle.sendData("
") - t handle.sendData(""" - -""") + var target = expandSymlink(fullpath) + if pc == pcLinkToDir: + target &= '/' + items.add(DirlistItem( + t: ITEM_LINK, + name: file, + modified: modified, + linkto: target + )) + t handle.sendData(makeDirlist(items)) + t handle.sendData("\n
\n\n\n") proc loadSymlink(handle: LoaderHandle, path: string) = template t(body: untyped) = diff --git a/src/loader/ftp.nim b/src/loader/ftp.nim index 84693cb3..f380e5e9 100644 --- a/src/loader/ftp.nim +++ b/src/loader/ftp.nim @@ -4,6 +4,7 @@ import bindings/curl import loader/connecterror import loader/curlhandle import loader/curlwrap +import loader/dirlist import loader/headers import loader/loaderhandle import loader/request @@ -58,8 +59,7 @@ proc curlWriteHeader(p: cstring, size: csize_t, nitems: csize_t,

Index of """ & htmlEscape(op.path) & """

-
-[Upper Directory]"""):
+"""):
           return 0
       return nitems
     elif line.startsWith("530"): # login incorrect
@@ -76,7 +76,9 @@ proc curlWriteHeader(p: cstring, size: csize_t, nitems: csize_t,
 
 Unauthorized
 
-
""" & htmlEscape(line))
+
+
+""" & htmlEscape(line))
       return 0
   return nitems
 
@@ -97,6 +99,7 @@ proc curlWriteBody(p: cstring, size: csize_t, nmemb: csize_t,
 
 proc finish(op: CurlHandle) =
   let op = cast[FtpHandle](op)
+  var items: seq[DirlistItem]
   for line in op.buffer.split('\n'):
     if line.len == 0: continue
     var i = 10 # permission
@@ -123,52 +126,65 @@ proc finish(op: CurlHandle) =
     let nsize = parseInt64(sizes).get(-1)
     # date
     i = line.skipBlanks(i)
+    let datestarti = i
     skip_till_space # m
     i = line.skipBlanks(i)
     skip_till_space # d
     i = line.skipBlanks(i)
     skip_till_space # y
+    let dates = line.substr(datestarti, i)
     inc i
     let name = line.substr(i)
+    if name == "." or name == "..": continue
     case line[0]
     of 'l': # link
       let x = " -> "
       let linki = name.find(x)
       let linkfrom = name.substr(0, linki - 1)
       let linkto = name.substr(linki + 4) # you?
-      let path = percentEncode(linkfrom, PathPercentEncodeSet)
-      discard op.handle.sendData("""
-
-""" & htmlEscape(linkfrom) & """@ (-> """ & htmlEscape(linkto) & """)""")
+      items.add(DirlistItem(
+        t: ITEM_LINK,
+        name: linkfrom,
+        modified: dates,
+        linkto: linkto
+      ))
     of 'd': # directory
-      let path = percentEncode(name, PathPercentEncodeSet)
-      discard op.handle.sendData("""
-
-""" & htmlEscape(name) & """/""")
+      items.add(DirlistItem(
+        t: ITEM_DIR,
+        name: name,
+        modified: dates
+      ))
     else: # file
-      let path = percentEncode(name, PathPercentEncodeSet)
-      discard op.handle.sendData("""
-
-""" & htmlEscape(name) & """ (""" & $nsize & """)""")
-  discard op.handle.sendData("""
-
- - -""") + items.add(DirlistItem( + t: ITEM_FILE, + name: name, + modified: dates, + nsize: nsize + )) + discard op.handle.sendData(makeDirlist(items)) + discard op.handle.sendData("\n
\n\n\n") proc loadFtp*(handle: LoaderHandle, curlm: CURLM, request: Request): CurlHandle = let curl = curl_easy_init() doAssert curl != nil let surl = request.url.serialize() - curl.setopt(CURLOPT_URL, surl) let path = request.url.path.serialize_unicode() + # By default, cURL CWD's into relative paths, and an extra slash is + # 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. + let hackurl = newURL(request.url) + hackurl.setPathname('/' & request.url.pathname) + let csurl = hackurl.serialize() + curl.setopt(CURLOPT_URL, csurl) let dirmode = path.len > 0 and path[^1] == '/' let handleData = curl.newFtpHandle(request, handle, dirmode) curl.setopt(CURLOPT_HEADERDATA, handleData) curl.setopt(CURLOPT_HEADERFUNCTION, curlWriteHeader) curl.setopt(CURLOPT_WRITEDATA, handleData) curl.setopt(CURLOPT_WRITEFUNCTION, curlWriteBody) + curl.setopt(CURLOPT_FTP_FILEMETHOD, CURLFTPMETHOD_SINGLECWD) if dirmode: handleData.finish = finish handleData.base = surl -- cgit