Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uri.EscapeUriString with square braces

Something of a strange question but let's see what kind of response it gets...

If I code a console app (VS 2013, .NET 4.5.1) and execute this line of code:

Uri.EscapeUriString("[")

I get this:

[

However if I execute the same thing (well, technically Uri.EscapeUriString("[").Dump()) in LINQPad on my machine I get this:

%5B

To further complicate things, according to this post Uri.EscapeUriString("[") should indeed return %5B.The post was written on 27/06/2012.

I'm thinking that perhaps LINQPad is referencing an older DLL than that used by VS, but that would imply that EscapeUriString has changed relatively recently, which I cannot find any record of. Does anyone have any ideas as to what could be causing this behaviour?

like image 334
Levi Botelho Avatar asked Nov 15 '13 14:11

Levi Botelho


2 Answers

This changed between .Net 4 and .Net 4.5, which you can verify by retargeting the framework version to .Net 4 and running the test program.

.Net 4 -> outputs "%5B" .net 4.5 (or later) -> outputs "["

This is mentioned here: Application Compatibility in the .NET Framework 4.5

under the section for Uri.EscapeDataString , Uri.EscapeUriString, and Uri.UnescapeDataString, which states that (with .Net 4.5):

The list of reserved and unreserved characters now supports RFC 3986.

Specific changes:

Unreserved escaped characters are un-escaped.
EscapeDataString escapes reserved characters based on RFC 3986.
EscapeUriString does not escape reserved characters.
UnescapeDataString does not throw an exception if it encounters an invalid escape sequence.

In particular, it is the EscapeUriString does not escape reserved characters which is significant.

like image 200
Matthew Watson Avatar answered Oct 19 '22 19:10

Matthew Watson


The new behavior seems to be the correct one according to RFC 2396. In line 566 it states:

566.    Other characters are excluded because gateways and other transport
567.    agents are known to sometimes modify such characters, or they are
568.    used as delimiters.
569. 
570.    unwise      = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
571. 
572.    Data corresponding to excluded characters must be escaped in order to
573.    be properly represented within a URI.

In Uri.EscapeUriString's documentation it states that

By default, the EscapeUriString method converts all characters, except RFC 2396 unreserved characters, to their hexadecimal representation

So it would seem that there was a bug up to .NET 4.0 that was fixed in 4.5.1

like image 40
Panagiotis Kanavos Avatar answered Oct 19 '22 21:10

Panagiotis Kanavos