If Node automatically performed a URL decode on the pathname, you lose data —
e.g., if your application depended on knowing whether someone encoded a space
as a "+" or %20 (it seems silly, but let's say you can contrive an example
where that matters), you'd have no way to tell from that information. URL
parsing and decoding are conceptually separate steps, and the implementation in
Node reflects that.
You should be able to get a correctly-decoded URL using something like:
decodeURIComponent(url.parse("http://host/path/some+thing%20with%20spaces+in+it/bar",
true).path.replace(/\++/g, ' '))
Just make sure you always resolve the +'s first, because if someone
percent-encodes a literal + sign, that would then be translated into a space if
done in the wrong order.
Hope that helps!
-Matt
On Apr 24, 2012, at 4:25 PM, Matt wrote:
> > url.parse("http://host/path/some+thing%20with%20spaces+in+it/bar", true);
> { protocol: 'http:',
> slashes: true,
> host: 'host',
> hostname: 'host',
> href: 'http://host/path/some+thing%20with%20spaces+in+it/bar',
> search: '',
> query: {},
> pathname: '/path/some+thing%20with%20spaces+in+it/bar',
> path: '/path/some+thing%20with%20spaces+in+it/bar' }
>
> I kind of hoped it would set the path to '/path/some thing with spaces in
> it/bar' which would be appropriate for passing to the filesystem. How come it
> doesn't decode the path?
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en