Skip to content

Commit ef0a21e

Browse files
committed
Update documentation for URI parsing types
See gh-33639
1 parent 6c62965 commit ef0a21e

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

Diff for: framework-docs/modules/ROOT/partials/web/web-uris.adoc

+28
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,34 @@ Kotlin::
234234
======
235235

236236

237+
[[uri-parsing]]
238+
= URI Parsing
239+
[.small]#Spring MVC and Spring WebFlux#
240+
241+
`UriComponentsBuilder` supports two URI parser types:
242+
243+
1. RFC parser -- this parser type expects URI strings to conform to RFC 3986 syntax,
244+
and treats deviations from the syntax as illegal.
245+
246+
2. WhatWG parser -- this parser is based on the
247+
https://github.com/web-platform-tests/wpt/tree/master/url[URL parsing algorithm] in the
248+
https://url.spec.whatwg.org[WhatWG URL Living standard]. It provides lenient handling of
249+
a wide range of cases of unexpected input. Browsers implement this in order to handle
250+
leniently user typed URL's. For more details, see the URL Living Standard and URL parsing
251+
https://github.com/web-platform-tests/wpt/tree/master/url[test cases].
252+
253+
By default, `RestClient`, `WebClient`, and `RestTemplate` use the RFC parser type, and
254+
expect applications to provide with URL templates that conform to RFC syntax. To change
255+
that you can customize the `UriBuilderFactory` on any of the clients.
256+
257+
Applications and frameworks may further rely on `UriComponentsBuilder` for their own needs
258+
to parse user provided URL's in order to inspect and possibly validated URI components
259+
such as the scheme, host, port, path, and query. Such components can decide to use the
260+
WhatWG parser type in order to handle URL's more leniently, and to align with the way
261+
browsers parse URI's, in case of a redirect to the input URL or if it is included in a
262+
response to a browser.
263+
264+
237265
[[uri-encoding]]
238266
= URI Encoding
239267
[.small]#Spring MVC and Spring WebFlux#

Diff for: spring-web/src/main/java/org/springframework/web/util/DefaultUriBuilderFactory.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ public final boolean hasBaseUri() {
9797

9898
/**
9999
* Set the {@link UriComponentsBuilder.ParserType} to use.
100-
* <p>By default, if the parser type is not specified,
101-
* {@link UriComponentsBuilder} uses {@link UriComponentsBuilder.ParserType#RFC}.
100+
* <p>By default, {@link UriComponentsBuilder} uses the
101+
* {@link UriComponentsBuilder.ParserType#RFC parser type}.
102102
* @param parserType the parser type
103103
* @since 6.2
104+
* @see UriComponentsBuilder.ParserType
105+
* @see UriComponentsBuilder#fromUriString(String, UriComponentsBuilder.ParserType)
104106
*/
105107
public void setParserType(UriComponentsBuilder.ParserType parserType) {
106108
this.parserType = parserType;

Diff for: spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

+32-32
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,23 @@
4242
import org.springframework.web.util.UriComponents.UriTemplateVariables;
4343

4444
/**
45-
* Builder for {@link UriComponents}.
46-
*
47-
* <p>Typical usage involves:
45+
* Builder for {@link UriComponents}. Use as follows:
4846
* <ol>
49-
* <li>Create a {@code UriComponentsBuilder} with one of the static factory methods
50-
* (such as {@link #fromPath(String)} or {@link #fromUri(URI)})</li>
51-
* <li>Set the various URI components through the respective methods ({@link #scheme(String)},
52-
* {@link #userInfo(String)}, {@link #host(String)}, {@link #port(int)}, {@link #path(String)},
53-
* {@link #pathSegment(String...)}, {@link #queryParam(String, Object...)}, and
54-
* {@link #fragment(String)}.</li>
55-
* <li>Build the {@link UriComponents} instance with the {@link #build()} method.</li>
47+
* <li>Create a builder through a factory method, e.g. {@link #fromUriString(String)}.
48+
* <li>Set URI components (e.g. scheme, host, path, etc) through instance methods.
49+
* <li>Build the {@link UriComponents}.</li>
50+
* <li>Expand URI variables from a map or array or variable values.
51+
* <li>Encode via {@link UriComponents#encode()}.</li>
52+
* <li>Use {@link UriComponents#toUri()} or {@link UriComponents#toUriString()}.
5653
* </ol>
5754
*
55+
* <p>By default, URI parsing is based on the {@link ParserType#RFC RFC parser type},
56+
* which expects input strings to conform to RFC 3986 syntax. The alternative
57+
* {@link ParserType#WHAT_WG WhatWG parser type}, based on the algorithm from
58+
* the WhatWG <a href="https://url.spec.whatwg.org">URL Living Standard</a>
59+
* provides more lenient handling of a wide range of cases that occur in user
60+
* types URL's.
61+
*
5862
* @author Arjen Poutsma
5963
* @author Rossen Stoyanchev
6064
* @author Phillip Webb
@@ -785,43 +789,39 @@ public UriComponentsBuilder cloneBuilder() {
785789
}
786790

787791

788-
private interface PathComponentBuilder {
789-
790-
@Nullable
791-
PathComponent build();
792-
793-
PathComponentBuilder cloneBuilder();
794-
}
795-
796-
797792
/**
798-
* Enum to represent different URI parsing mechanisms.
793+
* Enum to provide a choice of URI parsers to use in {@link #fromUriString(String, ParserType)}.
794+
* @since 6.2
799795
*/
800796
public enum ParserType {
801797

802798
/**
803-
* Parser that expects URI's conforming to RFC 3986 syntax.
799+
* This parser type expects URI's to conform to RFC 3986 syntax.
804800
*/
805801
RFC,
806802

807803
/**
808-
* Parser based on algorithm defined in the WhatWG URL Living standard.
809-
* Browsers use this algorithm to align on lenient parsing of user typed
810-
* URL's that may deviate from RFC syntax.
811-
* <p>For more details, see:
812-
* <ul>
813-
* <li><a href="https://url.spec.whatwg.org">URL Living Standard</a>
814-
* <li><a href="https://url.spec.whatwg.org/#url-parsing">Section 4.4: URL parsing</a>
815-
* <li><a href="https://github.com/web-platform-tests/wpt/tree/master/url">web-platform-tests</a>
816-
* </ul>
817-
* <p>Use this if you need to leniently handle URL's that don't conform
818-
* to RFC syntax, or for alignment with browser parsing.
804+
* This parser follows the
805+
* <a href="https://url.spec.whatwg.org/#url-parsing">URL parsing algorithm</a>
806+
* in the WhatWG URL Living standard that browsers implement to align on
807+
* lenient handling of user typed URL's that may not conform to RFC syntax.
808+
* @see <a href="https://url.spec.whatwg.org">URL Living Standard</a>
809+
* @see <a href="https://github.com/web-platform-tests/wpt/tree/master/url">URL tests</a>
819810
*/
820811
WHAT_WG
821812

822813
}
823814

824815

816+
private interface PathComponentBuilder {
817+
818+
@Nullable
819+
PathComponent build();
820+
821+
PathComponentBuilder cloneBuilder();
822+
}
823+
824+
825825
private static class CompositePathComponentBuilder implements PathComponentBuilder {
826826

827827
private final Deque<PathComponentBuilder> builders = new ArrayDeque<>();

0 commit comments

Comments
 (0)