Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #28379
| From | Mike Amling <mamling@chaff.us> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: [LONG] java.net.URI encoding weirdness |
| Date | 2014-05-05 11:37 -0500 |
| Message-ID | <bspt15FdlhbU1@mid.individual.net> (permalink) |
| References | <lk82m6$hhi$1@dont-email.me> |
On 5/5/14 8:11 AM, Stanimir Stamenkov wrote:
> This is a long time observation but I wanted to summarize it and give
> heads up to ones which might not have encountered it, yet.
>
> It doesn't appear java.net.URI behaves in undocumented way, but just in
> no useful way. In my experience the java.net.URI is only suitable for
> parsing certain URI parts, and not for constructing URI instances,
> either using the properties of an existing URI or using values obtained
> else way.
>
> My use case is simple: Have an input URI which I want to modify certain
> components/properties of, and produce a new URI. For example, change
> the 'host' or 'path' of an HTTP URL.
>
> The first example behaves pretty much as I expect:
>
> import java.net.URI;
> import java.net.URLEncoder;
>
> public class URITest {
>
> public static void main(String[] args) throws Exception {
> System.out.println(URLEncoder
> .encode("#%&/;=?@", "US-ASCII"));
>
> URI u = URI.create("http://user%40domain@server1:8080"
> + "/path?param=value#fragment");
> System.out.println(u.toASCIIString());
>
> URI v = new URI(u.getScheme(),
> u.getUserInfo(),
> "server2",
> u.getPort(),
> u.getPath(),
> u.getQuery(),
> u.getFragment());
> System.out.println(v.toASCIIString());
>
> URI w = new URI(u.getScheme(),
> u.getRawUserInfo(),
> "server3",
> u.getPort(),
> u.getRawPath(),
> u.getRawQuery(),
> u.getRawFragment());
> System.out.println(w.toASCIIString());
> }
>
> }
>
> It tests the behavior of the URI(scheme, userInfo, host, port, path,
> query, fragment) constructor, and the output is as:
>
> http://user%40domain@server1:8080/path?param=value#fragment
> http://user%40domain@server2:8080/path?param=value#fragment
> http://user%2540domain@server3:8080/path?param=value#fragment
>
> As I would expect the 'userInfo' is encoded properly when given as
> decoded value (and double-encoded if given as a raw, already encoded
> value). The other properties, in this case, don't make a difference
> because their values are the same in raw and decoded form.
>
> ----
>
> Now, I expect the URI(scheme, authority, path, query, fragment)
> constructor would need a raw 'authority' value as it gets parsed into
> 'userInfo', 'host' and 'port' components/properties:
>
> public class URITest2 {
>
> public static void main(String[] args) throws Exception {
> URI u = URI.create("http://user%40domain@server1:8080"
> + "/path?param=value#fragment");
> System.out.println(u.toASCIIString());
>
> URI v = new URI(u.getScheme(),
> u.getAuthority(),
> "/htap",
> u.getQuery(),
> u.getFragment());
> System.out.println(v.toASCIIString());
>
> URI w = new URI(u.getScheme(),
> u.getRawAuthority(),
> "/htap",
> u.getQuery(),
> u.getFragment());
> System.out.println(w.toASCIIString());
> }
>
> }
>
> The output:
>
> http://user%40domain@server1:8080/path?param=value#fragment
> http://user@domain@server1:8080/htap?param=value#fragment
> http://user%2540domain@server1:8080/htap?param=value#fragment
>
> shows there's no way to re-construct a correct URI using it.
>
> ... TL;DR
Looks like you need
URI v = new URI(
u.getScheme(),
u.getAuthority().someKindOfEncodeFunction(),
"/htap",
u.getQuery(),
u.getFragment());
Mike Amling
--
V2hlcmUgaW4gdGhlIHdvcmxkIGlzIFdhbGRvIFNhbmRpZWdvPw==
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
[LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-05 16:11 +0300
Re: [LONG] java.net.URI encoding weirdness Mike Amling <mamling@chaff.us> - 2014-05-05 11:37 -0500
Re: [LONG] java.net.URI encoding weirdness Mike Amling <mamling@chaff.us> - 2014-05-05 11:43 -0500
Re: [LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-05 19:50 +0300
Re: [LONG] java.net.URI encoding weirdness markspace <markspace@nospam.nospam> - 2014-05-05 11:58 -0700
Re: [LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-05 23:59 +0300
Re: [LONG] java.net.URI encoding weirdness markspace <markspace@nospam.nospam> - 2014-05-05 16:51 -0700
Re: [LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-06 10:26 +0300
Re: [LONG] java.net.URI encoding weirdness Steven Simpson <ss@domain.invalid> - 2014-05-06 08:47 +0100
Re: [LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-06 11:02 +0300
Re: [LONG] java.net.URI encoding weirdness markspace <markspace@nospam.nospam> - 2014-05-06 10:24 -0700
Re: [LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-06 22:24 +0300
Re: [LONG] java.net.URI encoding weirdness markspace <markspace@nospam.nospam> - 2014-05-06 12:24 -0700
Re: [LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-06 22:34 +0300
Re: [LONG] java.net.URI encoding weirdness markspace <markspace@nospam.nospam> - 2014-05-06 13:04 -0700
Re: [LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-06 23:07 +0300
Re: [LONG] java.net.URI encoding weirdness Stanimir Stamenkov <s7an10@netscape.net> - 2014-05-06 23:04 +0300
Re: [LONG] java.net.URI encoding weirdness Steven Simpson <ss@domain.invalid> - 2014-05-06 20:29 +0100
Re: [LONG] java.net.URI encoding weirdness markspace <markspace@nospam.nospam> - 2014-05-06 13:01 -0700
csiph-web