Skip to content

Commit 1bd5aad

Browse files
author
Paul M. Jones
committed
Merge pull request #503 from Tobion/rootless-path
[PSR-7] UriInterface: rootless path, delimiters, consistency
2 parents ac29800 + 8039183 commit 1bd5aad

File tree

2 files changed

+76
-56
lines changed

2 files changed

+76
-56
lines changed

proposed/http-message-meta.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,5 +629,6 @@ used to populate the headers of an HTTP message.
629629
* Michael Dowling
630630
* Larry Garfield
631631
* Evert Pot
632+
- Tobias Schultze
632633
* Phil Sturgeon
633634
* Chris Wilkinson

proposed/http-message.md

Lines changed: 75 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,10 +1176,13 @@ interface StreamInterface
11761176
namespace Psr\Http\Message;
11771177

11781178
/**
1179-
* Value object representing a URI for use in HTTP requests.
1179+
* Value object representing a URI.
11801180
*
1181-
* This interface is meant to represent only URIs for use with HTTP requests,
1182-
* and is not intended as a general-purpose URI implementation.
1181+
* This interface is meant to represent URIs according to RFC 3986 and to
1182+
* provide methods for most common operations. Additional functionality for
1183+
* working with URIs can be provided on top of the interface or externally.
1184+
* Its primary use is for HTTP requests, but may also be used in other
1185+
* contexts.
11831186
*
11841187
* Instances of this interface are considered immutable; all methods that
11851188
* might change state MUST be implemented such that they retain the internal
@@ -1195,23 +1198,28 @@ namespace Psr\Http\Message;
11951198
interface UriInterface
11961199
{
11971200
/**
1198-
* Retrieve the URI scheme.
1199-
*
1200-
* Implementations SHOULD restrict values to "http", "https", or an empty
1201-
* string but MAY accommodate other schemes if required.
1201+
* Retrieve the scheme component of the URI.
12021202
*
12031203
* If no scheme is present, this method MUST return an empty string.
12041204
*
1205-
* The string returned MUST omit the trailing "://" delimiter if present.
1205+
* The value returned MUST be normalized to lowercase, per RFC 3986
1206+
* Section 3.1.
1207+
*
1208+
* The trailing ":" character is not part of the scheme and MUST NOT be
1209+
* added.
12061210
*
1207-
* @return string The scheme of the URI.
1211+
* @see https://tools.ietf.org/html/rfc3986#section-3.1
1212+
* @return string The URI scheme.
12081213
*/
12091214
public function getScheme();
12101215

12111216
/**
1212-
* Retrieve the authority portion of the URI.
1217+
* Retrieve the authority component of the URI.
12131218
*
1214-
* The authority portion of the URI is:
1219+
* If no authority information is present, this method MUST return an empty
1220+
* string.
1221+
*
1222+
* The authority syntax of the URI is:
12151223
*
12161224
* <pre>
12171225
* [user-info@]host[:port]
@@ -1220,35 +1228,38 @@ interface UriInterface
12201228
* If the port component is not set or is the standard port for the current
12211229
* scheme, it SHOULD NOT be included.
12221230
*
1223-
* This method MUST return an empty string if no authority information is
1224-
* present.
1225-
*
1226-
* @return string Authority portion of the URI, in "[user-info@]host[:port]"
1227-
* format.
1231+
* @see https://tools.ietf.org/html/rfc3986#section-3.2
1232+
* @return string The URI authority, in "[user-info@]host[:port]" format.
12281233
*/
12291234
public function getAuthority();
12301235

12311236
/**
1232-
* Retrieve the user information portion of the URI, if present.
1237+
* Retrieve the user information component of the URI.
1238+
*
1239+
* If no user information is present, this method MUST return an empty
1240+
* string.
12331241
*
12341242
* If a user is present in the URI, this will return that value;
12351243
* additionally, if the password is also present, it will be appended to the
12361244
* user value, with a colon (":") separating the values.
12371245
*
1238-
* Implementations MUST NOT return the "@" suffix when returning this value.
1246+
* The trailing "@" character is not part of the user information and MUST
1247+
* NOT be added.
12391248
*
1240-
* @return string User information portion of the URI, if present, in
1241-
* "username[:password]" format.
1249+
* @return string The URI user information, in "username[:password]" format.
12421250
*/
12431251
public function getUserInfo();
12441252

12451253
/**
12461254
* Retrieve the host component of the URI.
12471255
*
1248-
* This method MUST return a string; if no host component is present, an
1249-
* empty string MUST be returned.
1256+
* If no host is present, this method MUST return an empty string.
1257+
*
1258+
* The value returned MUST be normalized to lowercase, per RFC 3986
1259+
* Section 3.2.2.
12501260
*
1251-
* @return string Host component of the URI.
1261+
* @see http://tools.ietf.org/html/rfc3986#section-3.2.2
1262+
* @return string The URI host.
12521263
*/
12531264
public function getHost();
12541265

@@ -1265,14 +1276,16 @@ interface UriInterface
12651276
* If no port is present, but a scheme is present, this method MAY return
12661277
* the standard port for that scheme, but SHOULD return null.
12671278
*
1268-
* @return null|int The port for the URI.
1279+
* @return null|int The URI port.
12691280
*/
12701281
public function getPort();
12711282

12721283
/**
12731284
* Retrieve the path component of the URI.
12741285
*
1275-
* This method MUST return a string.
1286+
* The path can either be empty or absolute (starting with a slash) or
1287+
* rootless (not starting with a slash). Implementations MUST support all
1288+
* three syntaxes.
12761289
*
12771290
* Normally, the empty path "" and absolute path "/" are considered equal as
12781291
* defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically
@@ -1285,30 +1298,30 @@ interface UriInterface
12851298
* RFC 3986, Sections 2 and 3.3.
12861299
*
12871300
* As an example, if the value should include a slash ("/") not intended as
1288-
* delimiter between path segments, that value MUST be encoded (e.g., "%2F")
1289-
* when passed to the instance.
1301+
* delimiter between path segments, that value MUST be passed in encoded
1302+
* form (e.g., "%2F") to the instance.
12901303
*
12911304
* @see https://tools.ietf.org/html/rfc3986#section-2
12921305
* @see https://tools.ietf.org/html/rfc3986#section-3.3
1293-
* @return string The path component of the URI.
1306+
* @return string The URI path.
12941307
*/
12951308
public function getPath();
12961309

12971310
/**
12981311
* Retrieve the query string of the URI.
12991312
*
1300-
* This method MUST return a string; if no query string is present, it MUST
1301-
* return an empty string.
1313+
* If no query string is present, this method MUST return an empty string.
13021314
*
1303-
* The string returned MUST omit the leading "?" character.
1315+
* The leading "?" character is not part of the query and MUST NOT be
1316+
* added.
13041317
*
13051318
* The value returned MUST be percent-encoded, but MUST NOT double-encode
13061319
* any characters. To determine what characters to encode, please refer to
13071320
* RFC 3986, Sections 2 and 3.4.
13081321
*
13091322
* As an example, if a value in a key/value pair of the query string should
13101323
* include an ampersand ("&") not intended as a delimiter between values,
1311-
* that value MUST be encoded (e.g., "%26") when passed to the instance.
1324+
* that value MUST be passed in encoded form (e.g., "%26") to the instance.
13121325
*
13131326
* @see https://tools.ietf.org/html/rfc3986#section-2
13141327
* @see https://tools.ietf.org/html/rfc3986#section-3.4
@@ -1319,10 +1332,10 @@ interface UriInterface
13191332
/**
13201333
* Retrieve the fragment component of the URI.
13211334
*
1322-
* This method MUST return a string; if no fragment is present, it MUST
1323-
* return an empty string.
1335+
* If no fragment is present, this method MUST return an empty string.
13241336
*
1325-
* The string returned MUST omit the leading "#" character.
1337+
* The leading "#" character is not part of the fragment and MUST NOT be
1338+
* added.
13261339
*
13271340
* The value returned MUST be percent-encoded, but MUST NOT double-encode
13281341
* any characters. To determine what characters to encode, please refer to
@@ -1338,11 +1351,10 @@ interface UriInterface
13381351
* Return an instance with the specified scheme.
13391352
*
13401353
* This method MUST retain the state of the current instance, and return
1341-
* an instance that contains the specified scheme. If the scheme
1342-
* provided includes the "://" delimiter, it MUST be removed.
1354+
* an instance that contains the specified scheme.
13431355
*
1344-
* Implementations SHOULD restrict values to "http", "https", or an empty
1345-
* string but MAY accommodate other schemes if required.
1356+
* Implementations MUST support the schemes "http" and "https" case
1357+
* insensitively, and MAY accommodate other schemes if required.
13461358
*
13471359
* An empty scheme is equivalent to removing the scheme.
13481360
*
@@ -1362,8 +1374,8 @@ interface UriInterface
13621374
* user; an empty string for the user is equivalent to removing user
13631375
* information.
13641376
*
1365-
* @param string $user User name to use for authority.
1366-
* @param null|string $password Password associated with $user.
1377+
* @param string $user The user name to use for authority.
1378+
* @param null|string $password The password associated with $user.
13671379
* @return self A new instance with the specified user information.
13681380
*/
13691381
public function withUserInfo($user, $password = null);
@@ -1376,7 +1388,7 @@ interface UriInterface
13761388
*
13771389
* An empty host value is equivalent to removing the host.
13781390
*
1379-
* @param string $host Hostname to use with the new instance.
1391+
* @param string $host The hostname to use with the new instance.
13801392
* @return self A new instance with the specified host.
13811393
* @throws \InvalidArgumentException for invalid hostnames.
13821394
*/
@@ -1394,7 +1406,7 @@ interface UriInterface
13941406
* A null value provided for the port is equivalent to removing the port
13951407
* information.
13961408
*
1397-
* @param null|int $port Port to use with the new instance; a null value
1409+
* @param null|int $port The port to use with the new instance; a null value
13981410
* removes the port information.
13991411
* @return self A new instance with the specified port.
14001412
* @throws \InvalidArgumentException for invalid ports.
@@ -1407,10 +1419,12 @@ interface UriInterface
14071419
* This method MUST retain the state of the current instance, and return
14081420
* an instance that contains the specified path.
14091421
*
1410-
* The path MUST be prefixed with "/"; if not, the implementation MAY
1411-
* provide the prefix itself.
1422+
* The path can either be empty or absolute (starting with a slash) or
1423+
* rootless (not starting with a slash). Implementations MUST support all
1424+
* three syntaxes.
14121425
*
1413-
* An empty path value is equivalent to removing the path.
1426+
* Users can provide both encoded and decoded path characters.
1427+
* Implementations ensure the correct encoding as outlined in getPath().
14141428
*
14151429
* @param string $path The path to use with the new instance.
14161430
* @return self A new instance with the specified path.
@@ -1424,9 +1438,8 @@ interface UriInterface
14241438
* This method MUST retain the state of the current instance, and return
14251439
* an instance that contains the specified query string.
14261440
*
1427-
* If the query string is prefixed by "?", that character MUST be removed.
1428-
* Additionally, the query string SHOULD be parseable by parse_str() in
1429-
* order to be valid.
1441+
* Users can provide both encoded and decoded query characters.
1442+
* Implementations ensure the correct encoding as outlined in getQuery().
14301443
*
14311444
* An empty query string value is equivalent to removing the query string.
14321445
*
@@ -1442,28 +1455,34 @@ interface UriInterface
14421455
* This method MUST retain the state of the current instance, and return
14431456
* an instance that contains the specified URI fragment.
14441457
*
1445-
* If the fragment is prefixed by "#", that character MUST be removed.
1458+
* Users can provide both encoded and decoded fragment characters.
1459+
* Implementations ensure the correct encoding as outlined in getFragment().
14461460
*
14471461
* An empty fragment value is equivalent to removing the fragment.
14481462
*
1449-
* @param string $fragment The URI fragment to use with the new instance.
1450-
* @return self A new instance with the specified URI fragment.
1463+
* @param string $fragment The fragment to use with the new instance.
1464+
* @return self A new instance with the specified fragment.
14511465
*/
14521466
public function withFragment($fragment);
14531467

14541468
/**
1455-
* Return the string representation of the URI.
1469+
* Return the string representation as a URI reference.
14561470
*
1457-
* Concatenates the various components of the URI, using the appropriate
1458-
* delimiters:
1471+
* Depending on which components of the URI are present, the resulting
1472+
* string is either a full URI or relative reference according to RFC 3985,
1473+
* Section 4.1. The method concatenates the various components of the URI,
1474+
* using the appropriate delimiters:
14591475
*
14601476
* - If a scheme is present, "://" MUST append the value.
14611477
* - If the authority information is present, that value will be
14621478
* concatenated.
1463-
* - If a path is present, it MUST start with a "/" character.
1479+
* - If the path is rootless and the authority information is present, the
1480+
* path MUST be prefixed by a "/" character. Otherwise, the path can be
1481+
* concatenated without additional delimiters.
14641482
* - If a query string is present, it MUST be prefixed by a "?" character.
14651483
* - If a URI fragment is present, it MUST be prefixed by a "#" character.
14661484
*
1485+
* @see http://tools.ietf.org/html/rfc3986#section-4.1
14671486
* @return string
14681487
*/
14691488
public function __toString();

0 commit comments

Comments
 (0)