4

I noticed that xdg-open doesn't handle percent encoded urls. For example, these lines will succeed (provided the files exist):

xdg-open "/home/sashoalm/Has Spaces.txt"
xdg-open file:///home/sashoalm/NoSpaces.txt

But this one will fail:

xdg-open file:///home/sashoalm/Has%20Spaces.txt

Edit: This is my version of xdg-utils

 sashoalm@aspire:~$ apt-cache policy xdg-utils
xdg-utils:
  Installed: 1.1.0~rc1+git20111210-6+deb7u1
  Candidate: 1.1.0~rc1+git20111210-6+deb7u1
  Version table:
 *** 1.1.0~rc1+git20111210-6+deb7u1 0
        500 http://ftp.bg.debian.org/debian/ wheezy/main amd64 Packages
        100 /var/lib/dpkg/status

Edit 2: This is the trace:

sashoalm@aspire:~$ bash -x xdg-open file:///home/sashoalm/Has%20Spaces.txt 
+ check_common_commands file:///home/sashoalm/Has%20Spaces.txt
+ '[' 1 -gt 0 ']'
+ parm=file:///home/sashoalm/Has%20Spaces.txt
+ shift
+ case "$parm" in
+ '[' 0 -gt 0 ']'
+ '[' -z '' ']'
+ unset XDG_UTILS_DEBUG_LEVEL
+ '[' 0 -lt 1 ']'
+ xdg_redirect_output=' > /dev/null 2> /dev/null'
+ '[' xfile:///home/sashoalm/Has%20Spaces.txt '!=' x ']'
+ url=
+ '[' 1 -gt 0 ']'
+ parm=file:///home/sashoalm/Has%20Spaces.txt
+ shift
+ case "$parm" in
+ '[' -n '' ']'
+ url=file:///home/sashoalm/Has%20Spaces.txt
+ '[' 0 -gt 0 ']'
+ '[' -z file:///home/sashoalm/Has%20Spaces.txt ']'
+ detectDE
+ unset GREP_OPTIONS
+ '[' -n LXDE ']'
+ case "${XDG_CURRENT_DESKTOP}" in
+ DE=lxde
+ '[' xlxde = x ']'
+ '[' xlxde = x ']'
+ '[' xlxde = x ']'
+ '[' xlxde = xgnome ']'
+ '[' xlxde = x ']'
+ DEBUG 2 'Selected DE lxde'
+ '[' -z '' ']'
+ return 0
+ '[' x = x ']'
+ BROWSER=www-browser:links2:elinks:links:lynx:w3m
+ '[' -n :0 ']'
+ BROWSER=x-www-browser:firefox:seamonkey:mozilla:epiphany:konqueror:chromium-browser:google-chrome:www-browser:links2:elinks:links:lynx:w3m
+ case "$DE" in
+ open_lxde file:///home/sashoalm/Has%20Spaces.txt
+ echo file:///home/sashoalm/Has%20Spaces.txt
+ grep -q '^file://'
++ echo file:///home/sashoalm/Has%20Spaces.txt
++ sed 's%^file://%%'
+ local file=/home/sashoalm/Has%20Spaces.txt
+ echo /home/sashoalm/Has%20Spaces.txt
+ grep -q '^/'
+ pcmanfm /home/sashoalm/Has%20Spaces.txt
+ '[' 0 -eq 0 ']'
+ exit_success
+ '[' 0 -gt 0 ']'
+ exit 0
jofel
  • 26,513
  • 6
  • 65
  • 92
sashoalm
  • 5,760
  • 11
  • 32
  • 47
  • I could not reproduce this. Which version of the xdg-utils package are you actually using? You can get it with `apt-cache policy xdg-utils`. – jofel Nov 21 '14 at 14:50
  • I could really not reproduce this. For me, this works with exactly the same package version. I suggest you to fill a [Debian bug report](https://www.debian.org/Bugs/Reporting). – jofel Nov 21 '14 at 22:50
  • @jofel You mean that xdg-open works for you with URLs that have %20 (that encodes a space)? – sashoalm Nov 22 '14 at 08:30
  • Yes, this is what I mean. – jofel Nov 23 '14 at 00:45
  • @jofel I added the trace, if that's any help. – sashoalm Nov 30 '14 at 11:39
  • thanks, this was the needed information. xdg-open behaves differently depending on the used desktop environment. – jofel Dec 01 '14 at 11:19

1 Answers1

4

If you are using an LXDE desktop environment, xdg-open opens file:// URLS with the pcmanfm program. It strips the file:// part of the URL and calls pcmanfm with the remaining part, since pcmanfm supports only normal paths as arguments, not URLs.

xdg-open does not do any other replacements, so %20 is not translated into a space. This is a bug (feel free to open a bug report for this in Debian). A fix is described below.

For other desktop environments, the open programs support correctly file:// URLs.


Workaround: Unset some environment variables, so that xdg-open uses the generic open handler which supports all needed replacements:

XDG_CURRENT_DESKTOP= DESKTOP_SESSION= xdg-open "/home/sashoalm/Has Spaces.txt"

Bugfix: Copy the xdg-open script to /usr/local/bin (so that it is not overwritten by upgrading your system) and add the line

file="$(printf "$(echo "$file" | sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"

to the xdg-open script above the # handle relative paths comment line.

Bugfix 2:

Or simply replace replace detectDE() with:

detectDE()
{
    DE=gnome
}
sashoalm
  • 5,760
  • 11
  • 32
  • 47
jofel
  • 26,513
  • 6
  • 65
  • 92