29

I have a resource that is a . This means my url looks like this: http://myapp/index/. And i need to add query parameters so that it looks like this: http://myapp/index/.?type=xml I use Freemarker for the presentation of my resources and made a percent-encoding hack for this case:

<#if key?matches("\\.")>
<li><a href="${contextPath}/index/%2E">${key}</a></li>
</#if>

This works fine for Firefox. But all other Browsers like IE, Safari, Chrom, Opera just ignore my url encoded dot (http://myapp/index/%2E).

Any suggestions?

cuh
  • 3,544
  • 4
  • 27
  • 45

2 Answers2

24

It's actually not really clearly stated in the standard (RFC 3986) whether a percent-encoded version of . or .. is supposed to have the same this-folder/up-a-folder meaning as the unescaped version. Section 3.3 only talks about “The path segments . and ..”, without clarifying whether they match . and .. before or after pct-encoding.

Personally I find Firefox's interpretation that %2E does not mean . most practical, but unfortunately all the other browsers disagree. This would mean that you can't have a path component containing only . or ...

I think the only possible suggestion is “don't do that”! There are other path components that are troublesome too, typically due to server limitations: %2F, %00 and %5C sequences in paths may also be blocked by some web servers, and the empty path segment can also cause problems. So in general it's not possible to fit all possible byte sequences into a path component.

bobince
  • 498,320
  • 101
  • 621
  • 807
  • 1
    I have found that even if %2e is part of a URL, e.g. `http://localhost/index%2ehtml`, Firefox (14) and Chrome convert it to a `.`. This is specified in section 2.3. Later in section 3.3, it says that `.` and `..` are for relative reference within the pathname. So, `http://localhost/%2e` would essentially mean `http://localhost/`. – slowpoison Jul 20 '12 at 03:59
  • 1
    Good point, @slowpoison -- "URIs that differ in the replacement of an unreserved character with its corresponding percent-encoded US-ASCII octet are equivalent", and "." is an unreserved character. Ah well. – treat your mods well Jul 18 '14 at 22:00
13

It is not possible. §2.3 says that "." is an unreserved character and that "URIs that differ in the replacement of an unreserved character with its corresponding percent-encoded US-ASCII octet are equivalent". Therefore, /%2E%2E/ is the same as /../, and that will get normalized away.

(This is a combination of an answer by bobince and a comment by slowpoison.)

treat your mods well
  • 2,556
  • 1
  • 23
  • 33