Skip to content

Commit f9fa948

Browse files
committed
Fix #29
Changed directory listing XSLT to use EXSLT str:encode-uri in conjunction with five nested str:replace functions to encode directory file listings in a way that will allow links to work. Signed-off-by: Elijah Zupancic <[email protected]>
1 parent 89d919e commit f9fa948

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

common/etc/nginx/include/listing.xsl

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0"?>
2-
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
2+
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://exslt.org/strings" extension-element-prefixes="str">
33
<xsl:output method="html" encoding="utf-8" indent="yes"/>
4+
<xsl:strip-space elements="*" />
45

56
<xsl:template match="/">
67
<xsl:choose>
@@ -81,7 +82,7 @@
8182
select="substring-after(text(), $globalPrefix)"/>
8283
<tr>
8384
<td>
84-
<a href="/{text()}">
85+
<a><xsl:attribute name="href">/<xsl:call-template name="encode-uri"><xsl:with-param name="uri" select="text()"/></xsl:call-template>/</xsl:attribute>
8586
<xsl:value-of select="$dirName"/>
8687
</a>
8788
</td>
@@ -103,7 +104,8 @@
103104
<xsl:variable name="size" select="*[local-name()='Size']/text()"/>
104105
<tr>
105106
<td>
106-
<a href="/{$key}">
107+
<a>
108+
<xsl:attribute name="href">/<xsl:call-template name="encode-uri"><xsl:with-param name="uri" select="$key"/></xsl:call-template></xsl:attribute>
107109
<xsl:value-of select="$fileName"/>
108110
</a>
109111
</td>
@@ -116,4 +118,21 @@
116118
</tr>
117119
</xsl:if>
118120
</xsl:template>
121+
<!-- This template escapes the URI such that symbols or unicode characters are
122+
encoded so that they form a valid link that NGINX can parse -->
123+
<xsl:template name="encode-uri">
124+
<xsl:param name="uri"/>
125+
<xsl:for-each select="str:split($uri, '/')">
126+
<xsl:variable name="encoded" select="str:encode-uri(., 'true', 'UTF-8')" />
127+
<xsl:variable name="more-encoded" select="
128+
str:replace(
129+
str:replace(
130+
str:replace(
131+
str:replace(
132+
str:replace($encoded, '@', '%40'), '(', '%28'),
133+
')', '%29'),
134+
'!', '%21'),
135+
'*', '%2A')" />
136+
<xsl:value-of select="$more-encoded" /><xsl:if test="position() != last()">/</xsl:if></xsl:for-each>
137+
</xsl:template>
119138
</xsl:stylesheet>

0 commit comments

Comments
 (0)