[hunchentoot-devel] url-encoding file names

Mathias Dahl mathias.dahl at gmail.com
Sat Jun 16 16:16:10 UTC 2007


Hi!

I am encoding file names using the url-encode function in Hunchentoot.
However, it encodes chars such as space and / as well, which makes at
least my browser (Firefox) or web server not understand that they are
indeed links to files I have shared.

So I have this workaround:

(defun replace-all (string part replacement &key (test #'char=))
"Returns a new string in which all the occurences of the part
is replaced with replacement."
    (with-output-to-string (out)
      (loop with part-length = (length part)
            for old-pos = 0 then (+ pos part-length)
            for pos = (search part string
                              :start2 old-pos
                              :test test)
            do (write-string string out
                             :start old-pos
                             :end (or pos (length string)))
            when pos do (write-string replacement out)
            while pos)))

(defun replace-some-encoded (file-name)
  ;; Some chars should not be encoded, apparently...
  (replace-all (replace-all file-name "+" "%20") "%2F" "/"))

(defun url-encode-file-name (file-name)
  (replace-some-encoded (hunchentoot:url-encode file-name :utf-8)))

The above gives URLs like these:

http://myserver/Tommy%20K%C3%B6rberg%2FTommy%20K%C3%B6rberg%20-%20Stad%20i%20ljus.mp3

And without the workaround I get this:

http://klibb.com/muuartist/Tommy+K%C3%B6rberg%2FTommy+K%C3%B6rberg+-+Anthem.mp3

I searched the doc for "file name" to see if there was some special
method suited for encoding file names with path information but could
not find one.

Do I need the above workaround?

Thanks!

/Mathias



More information about the Tbnl-devel mailing list