@@ -195,30 +195,68 @@ public function createHttpRequest()
195195 $ usingTrustedProxy = $ remoteAddr && array_filter ($ this ->proxies , function ($ proxy ) use ($ remoteAddr ) {
196196 return Helpers::ipMatch ($ remoteAddr , $ proxy );
197197 });
198-
199198 if ($ usingTrustedProxy ) {
200- if (!empty ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ])) {
201- $ url ->setScheme (strcasecmp ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ], 'https ' ) === 0 ? 'https ' : 'http ' );
202- }
199+ if (!empty ($ _SERVER ['HTTP_FORWARDED ' ])) {
200+ $ forwardParams = preg_split ('/[,;]/ ' , $ _SERVER ['HTTP_FORWARDED ' ]);
201+ foreach ($ forwardParams as $ forwardParam ) {
202+ list ($ key , $ value ) = explode ('= ' , $ forwardParam , 2 ) + [1 => NULL ];
203+ $ proxyParams [strtolower (trim ($ key ))][] = trim ($ value , " \t\"" );
204+ }
203205
204- if (!empty ($ _SERVER ['HTTP_X_FORWARDED_PORT ' ])) {
205- $ url ->setPort ((int ) $ _SERVER ['HTTP_X_FORWARDED_PORT ' ]);
206- }
206+ if (isset ($ proxyParams ['for ' ])) {
207+ $ address = $ proxyParams ['for ' ][0 ];
208+ if (strpos ($ address , '[ ' ) === FALSE ) { //IPv4
209+ $ remoteAddr = explode (': ' , $ address )[0 ];
210+ } else { //IPv6
211+ $ remoteAddr = substr ($ address , 1 , strpos ($ address , '] ' ) - 1 );
212+ }
213+ }
214+
215+ if (isset ($ proxyParams ['host ' ]) && count ($ proxyParams ['host ' ]) === 1 ) {
216+ $ host = $ proxyParams ['host ' ][0 ];
217+ $ startingDelimiterPosition = strpos ($ host , '[ ' );
218+ if ($ startingDelimiterPosition === FALSE ) { //IPv4
219+ $ remoteHostArr = explode (': ' , $ host );
220+ $ remoteHost = $ remoteHostArr [0 ];
221+ if (isset ($ remoteHostArr [1 ])) {
222+ $ url ->setPort ((int ) $ remoteHostArr [1 ]);
223+ }
224+ } else { //IPv6
225+ $ endingDelimiterPosition = strpos ($ host , '] ' );
226+ $ remoteHost = substr ($ host , strpos ($ host , '[ ' ) + 1 , $ endingDelimiterPosition - 1 );
227+ $ remoteHostArr = explode (': ' , substr ($ host , $ endingDelimiterPosition ));
228+ if (isset ($ remoteHostArr [1 ])) {
229+ $ url ->setPort ((int ) $ remoteHostArr [1 ]);
230+ }
231+ }
232+ }
233+
234+ $ scheme = (isset ($ proxyParams ['scheme ' ]) && count ($ proxyParams ['scheme ' ]) === 1 ) ? $ proxyParams ['scheme ' ][0 ] : 'http ' ;
235+ $ url ->setScheme (strcasecmp ($ scheme , 'https ' ) === 0 ? 'https ' : 'http ' );
236+ } else {
237+ if (!empty ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ])) {
238+ $ url ->setScheme (strcasecmp ($ _SERVER ['HTTP_X_FORWARDED_PROTO ' ], 'https ' ) === 0 ? 'https ' : 'http ' );
239+ }
240+
241+ if (!empty ($ _SERVER ['HTTP_X_FORWARDED_PORT ' ])) {
242+ $ url ->setPort ((int ) $ _SERVER ['HTTP_X_FORWARDED_PORT ' ]);
243+ }
207244
208- if (!empty ($ _SERVER ['HTTP_X_FORWARDED_FOR ' ])) {
209- $ xForwardedForWithoutProxies = array_filter (explode (', ' , $ _SERVER ['HTTP_X_FORWARDED_FOR ' ]), function ($ ip ) {
210- return !array_filter ($ this ->proxies , function ($ proxy ) use ($ ip ) {
211- return Helpers::ipMatch (trim ($ ip ), $ proxy );
245+ if (!empty ($ _SERVER ['HTTP_X_FORWARDED_FOR ' ])) {
246+ $ xForwardedForWithoutProxies = array_filter (explode (', ' , $ _SERVER ['HTTP_X_FORWARDED_FOR ' ]), function ($ ip ) {
247+ return !array_filter ($ this ->proxies , function ($ proxy ) use ($ ip ) {
248+ return Helpers::ipMatch (trim ($ ip ), $ proxy );
249+ });
212250 });
213- });
214- $ remoteAddr = trim (end ($ xForwardedForWithoutProxies ));
215- $ xForwardedForRealIpKey = key ($ xForwardedForWithoutProxies );
216- }
251+ $ remoteAddr = trim (end ($ xForwardedForWithoutProxies ));
252+ $ xForwardedForRealIpKey = key ($ xForwardedForWithoutProxies );
253+ }
217254
218- if (isset ($ xForwardedForRealIpKey ) && !empty ($ _SERVER ['HTTP_X_FORWARDED_HOST ' ])) {
219- $ xForwardedHost = explode (', ' , $ _SERVER ['HTTP_X_FORWARDED_HOST ' ]);
220- if (isset ($ xForwardedHost [$ xForwardedForRealIpKey ])) {
221- $ remoteHost = trim ($ xForwardedHost [$ xForwardedForRealIpKey ]);
255+ if (isset ($ xForwardedForRealIpKey ) && !empty ($ _SERVER ['HTTP_X_FORWARDED_HOST ' ])) {
256+ $ xForwardedHost = explode (', ' , $ _SERVER ['HTTP_X_FORWARDED_HOST ' ]);
257+ if (isset ($ xForwardedHost [$ xForwardedForRealIpKey ])) {
258+ $ remoteHost = trim ($ xForwardedHost [$ xForwardedForRealIpKey ]);
259+ }
222260 }
223261 }
224262 }
0 commit comments