Commit 72309b2 1 parent 67a6cb3 commit 72309b2 Copy full SHA for 72309b2
File tree 2 files changed +36
-2
lines changed
2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -378,12 +378,31 @@ public function getUserInfo()
378
378
public function withUserInfo ($ user , $ password = null )
379
379
{
380
380
$ clone = clone $ this ;
381
- $ clone ->user = $ user ;
382
- $ clone ->password = $ password ? $ password : '' ;
381
+ $ clone ->user = $ this ->filterUserInfo ($ user );
382
+ if ($ clone ->user ) {
383
+ $ clone ->password = $ password ? $ this ->filterUserInfo ($ password ) : '' ;
384
+ }
383
385
384
386
return $ clone ;
385
387
}
386
388
389
+ /**
390
+ * Filters the user info string.
391
+ *
392
+ * @param string $query The raw uri query string.
393
+ * @return string The percent-encoded query string.
394
+ */
395
+ protected function filterUserInfo ($ query )
396
+ {
397
+ return preg_replace_callback (
398
+ '/(?:[^a-zA-Z0-9_\-\.~!\$& \'\(\)\*\+,;=]+|%(?![A-Fa-f0-9]{2}))/u ' ,
399
+ function ($ match ) {
400
+ return rawurlencode ($ match [0 ]);
401
+ },
402
+ $ query
403
+ );
404
+ }
405
+
387
406
/**
388
407
* Retrieve the host component of the URI.
389
408
*
Original file line number Diff line number Diff line change @@ -180,6 +180,13 @@ public function testGetUserInfoNone()
180
180
$ this ->assertEquals ('' , $ uri ->getUserInfo ());
181
181
}
182
182
183
+ public function testGetUserInfoWithUsernameAndPasswordEncodesCorrectly ()
184
+ {
185
+ $ uri = Uri::
createFromString (
'https://bob%40example.com:pass%[email protected] :443/foo/bar?abc=123#section3 ' );
186
+
187
+ $ this ->assertEquals ('bob%40example.com:pass%3Aword ' , $ uri ->getUserInfo ());
188
+ }
189
+
183
190
public function testWithUserInfo ()
184
191
{
185
192
$ uri = $ this ->uriFactory ()->withUserInfo ('bob ' , 'pass ' );
@@ -188,6 +195,14 @@ public function testWithUserInfo()
188
195
$ this ->assertAttributeEquals ('pass ' , 'password ' , $ uri );
189
196
}
190
197
198
+ public function testWithUserInfoEncodesCorrectly ()
199
+ {
200
+ $ uri =
$ this ->
uriFactory ()->
withUserInfo (
'[email protected] ' ,
'pass:word ' );
201
+
202
+ $ this ->assertAttributeEquals ('bob%40example.com ' , 'user ' , $ uri );
203
+ $ this ->assertAttributeEquals ('pass%3Aword ' , 'password ' , $ uri );
204
+ }
205
+
191
206
public function testWithUserInfoRemovesPassword ()
192
207
{
193
208
$ uri = $ this ->uriFactory ()->withUserInfo ('bob ' );
You can’t perform that action at this time.
0 commit comments