Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-10305

Spring URI template class does not encode a URI correctly

    XMLWordPrintable

    Details

    • Last commented by a User:
      true

      Description

      When using the Spring template class (org.springframework.web.util.UriTemplate), the expand method does not encode a URI correctly. Some reserved characters are not encoded ever, others only sometimes.

      For example, the slash character / should be encoded as %2f.

      I've tried every permutation I could think of. Templating, not templating, parameters, et al.

      Results as follows.

      List of strings which contain reserved characters:

      List<String> paths = Arrays.asList(
          "foo%boo"
          ,"foo/boo"
          ,"foo?boo"
          ,"foo=boo"
          ,"foo&boo"
          ,"foo#boo"
          ,"foo$boo"
          ,"foo+boo"
          ,"foo,boo"
          ,"foo:boo"
          ,"foo;boo"
          ,"[email protected]"
      );
      

      Java to utilize the URI template class:

      for (String path : paths) {
          System.out.println("path: \"" + path + "\"");
          System.out.println("UriTemplate#expand: \"" + (new UriTemplate("{path}")).expand(path).toString() + "\"");
          System.out.println("UriTemplate#expand, path: \"" + (new UriTemplate("http://domain.com/{path}")).expand(path).toString() + "\"");
          System.out.println("UriTemplate#expand, query: \"" + (new UriTemplate("http://domain.com/?{path}")).expand(path).toString() + "\"");
          System.out.println("UriTemplate#expand, query param: \"" + (new UriTemplate("http://domain.com/?param={path}")).expand(path).toString() + "\"");
      }
      

      Result:

      path: "foo%boo"
      UriTemplate#expand: "foo%25boo"
      UriTemplate#expand, path: "http://domain.com/foo%25boo"
      UriTemplate#expand, query: "http://domain.com/?foo%25boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo%25boo"
      
      path: "foo/boo"
      UriTemplate#expand: "foo/boo"
      UriTemplate#expand, path: "http://domain.com/foo/boo"
      UriTemplate#expand, query: "http://domain.com/?foo/boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo/boo"
      
      path: "foo?boo"
      UriTemplate#expand: "foo%3Fboo"
      UriTemplate#expand, path: "http://domain.com/foo%3Fboo"
      UriTemplate#expand, query: "http://domain.com/?foo?boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo?boo"
      
      path: "foo=boo"
      UriTemplate#expand: "foo=boo"
      UriTemplate#expand, path: "http://domain.com/foo=boo"
      UriTemplate#expand, query: "http://domain.com/?foo%3Dboo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo%3Dboo"
      
      path: "foo&boo"
      UriTemplate#expand: "foo&boo"
      UriTemplate#expand, path: "http://domain.com/foo&boo"
      UriTemplate#expand, query: "http://domain.com/?foo%26boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo%26boo"
      
      path: "foo#boo"
      UriTemplate#expand: "foo%23boo"
      UriTemplate#expand, path: "http://domain.com/foo%23boo"
      UriTemplate#expand, query: "http://domain.com/?foo%23boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo%23boo"
      
      path: "foo$boo"
      UriTemplate#expand: "foo$boo"
      UriTemplate#expand, path: "http://domain.com/foo$boo"
      UriTemplate#expand, query: "http://domain.com/?foo$boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo$boo"
      
      path: "foo+boo"
      UriTemplate#expand: "foo+boo"
      UriTemplate#expand, path: "http://domain.com/foo+boo"
      UriTemplate#expand, query: "http://domain.com/?foo%2Bboo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo%2Bboo"
      
      path: "foo,boo"
      UriTemplate#expand: "foo,boo"
      UriTemplate#expand, path: "http://domain.com/foo,boo"
      UriTemplate#expand, query: "http://domain.com/?foo,boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo,boo"
      
      path: "foo:boo"
      UriTemplate#expand: "foo:boo"
      UriTemplate#expand, path: "http://domain.com/foo:boo"
      UriTemplate#expand, query: "http://domain.com/?foo:boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo:boo"
      
      path: "foo;boo"
      UriTemplate#expand: "foo;boo"
      UriTemplate#expand, path: "http://domain.com/foo;boo"
      UriTemplate#expand, query: "http://domain.com/?foo;boo"
      UriTemplate#expand, query param: "http://domain.com/?param=foo;boo"
      
      path: "[email protected]"
      UriTemplate#expand: "[email protected]"
      UriTemplate#expand, path: "http://domain.com/[email protected]"
      UriTemplate#expand, query: "http://domain.com/[email protected]"
      UriTemplate#expand, query param: "http://domain.com/[email protected]"
      

      Any ideas?

      Thanks.

      -AH

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              rstoya05-aop Rossen Stoyanchev
              Reporter:
              hawleyal Alexander Hawley
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 37 weeks ago