File tree Expand file tree Collapse file tree 2 files changed +25
-4
lines changed
Expand file tree Collapse file tree 2 files changed +25
-4
lines changed Original file line number Diff line number Diff line change @@ -109,10 +109,17 @@ public function setAbsoluteUrl(string $url): void
109109 public function getAbsoluteUrl (): string
110110 {
111111 if ((null === $ this ->absoluteUrl ) || ('' === $ this ->absoluteUrl )) {
112- // Guessing we're a http endpoint.
113- $ this ->absoluteUrl = 'http:// ' .
114- ($ this ->getHeader ('Host ' ) ?? 'localhost ' ).
115- $ this ->getUrl ();
112+ $ url = $ this ->getUrl ();
113+ if (parse_url ($ url , PHP_URL_SCHEME )) {
114+ // It's already an absolute URL
115+ $ this ->absoluteUrl = $ url ;
116+ } else {
117+ $ host = $ this ->getHeader ('Host ' )
118+ ?? parse_url ($ url , PHP_URL_HOST )
119+ ?? 'localhost ' ;
120+ // Guessing we're a http endpoint.
121+ $ this ->absoluteUrl = "http:// $ host$ url " ;
122+ }
116123 }
117124
118125 return $ this ->absoluteUrl ;
Original file line number Diff line number Diff line change @@ -134,4 +134,18 @@ public function testToStringAuthorization(): void
134134 .'foo ' ;
135135 self ::assertEquals ($ expected , (string ) $ request );
136136 }
137+
138+ public function testAbsoluteUrlHttp (): void
139+ {
140+ $ request = new Request ('GET ' , 'http://example.com/foo/bar?a=b&c=d ' );
141+ self ::assertEquals ('http://example.com/foo/bar?a=b&c=d ' , $ request ->getAbsoluteUrl ());
142+ }
143+
144+ public function testAbsoluteUrlHttpHostPrevalence (): void
145+ {
146+ $ request = new Request ('GET ' , 'http://example.com/foo/bar?a=b&c=d ' , [
147+ 'Host ' => 'example.org ' ,
148+ ]);
149+ self ::assertEquals ('http://example.com/foo/bar?a=b&c=d ' , $ request ->getAbsoluteUrl ());
150+ }
137151}
You can’t perform that action at this time.
0 commit comments