From 4d684b37900ef87e952522f4163ee620fa55ba8e Mon Sep 17 00:00:00 2001 From: Olaf Conradi Date: Wed, 26 Dec 2012 02:17:43 +0100 Subject: Fix whitespace --- include/network.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/network.php b/include/network.php index 333e0e83f..7050dc6ac 100644 --- a/include/network.php +++ b/include/network.php @@ -340,18 +340,18 @@ function z_post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0 } if($http_code == 301 || $http_code == 302 || $http_code == 303) { - $matches = array(); - preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); - $newurl = trim(array_pop($matches)); + $matches = array(); + preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); + $newurl = trim(array_pop($matches)); if(strpos($newurl,'/') === 0) $newurl = $url . $newurl; - $url_parsed = @parse_url($newurl); - if (isset($url_parsed)) { - $redirects++; + $url_parsed = @parse_url($newurl); + if (isset($url_parsed)) { + $redirects++; curl_close($ch); - return z_post_url($newurl,$params,$headers,$redirects,$timeout); - } - } + return z_post_url($newurl,$params,$headers,$redirects,$timeout); + } + } $rc = intval($http_code); $ret['return_code'] = $rc; $ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false); -- cgit v1.2.3 From e15683175e0582757746534044e5893ab70f4724 Mon Sep 17 00:00:00 2001 From: Olaf Conradi Date: Wed, 26 Dec 2012 03:25:13 +0100 Subject: More whitespace fixes --- include/network.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/network.php b/include/network.php index 7050dc6ac..0f1420d61 100644 --- a/include/network.php +++ b/include/network.php @@ -162,17 +162,17 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) } if($http_code == 301 || $http_code == 302 || $http_code == 303) { - $matches = array(); - preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); - $newurl = trim(array_pop($matches)); + $matches = array(); + preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); + $newurl = trim(array_pop($matches)); if(strpos($newurl,'/') === 0) $newurl = $url . $newurl; - $url_parsed = @parse_url($newurl); - if (isset($url_parsed)) { - $redirects++; - return fetch_url($newurl,false,$redirects,$timeout); - } - } + $url_parsed = @parse_url($newurl); + if (isset($url_parsed)) { + $redirects++; + return fetch_url($newurl,false,$redirects,$timeout); + } + } $a->set_curl_code($http_code); $body = substr($s,strlen($header)); -- cgit v1.2.3 From bb16186fabdbd4b49ad4ff13552d97a0462e298a Mon Sep 17 00:00:00 2001 From: Olaf Conradi Date: Wed, 26 Dec 2012 04:03:12 +0100 Subject: Add permanent redirect (308) http code Add 308 http code next to 301, 302, 303 and 307. Close curl session before returning on redirect. Let post_url redirect using post_url, not fetch_url (which is actually 308 behavior). --- include/network.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/include/network.php b/include/network.php index 0f1420d61..5571285b4 100644 --- a/include/network.php +++ b/include/network.php @@ -72,7 +72,7 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_ $base = substr($base,strlen($chunk)); } - if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) { + if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307 || $http_code == 308) { $matches = array(); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); $newurl = trim(array_pop($matches)); @@ -81,6 +81,7 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_ $url_parsed = @parse_url($newurl); if (isset($url_parsed)) { $redirects++; + @curl_close($ch); return fetch_url($newurl,$binary,$redirects,$timeout); } } @@ -161,7 +162,7 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) $base = substr($base,strlen($chunk)); } - if($http_code == 301 || $http_code == 302 || $http_code == 303) { + if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307 || $http_code == 308) { $matches = array(); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); $newurl = trim(array_pop($matches)); @@ -170,7 +171,8 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) $url_parsed = @parse_url($newurl); if (isset($url_parsed)) { $redirects++; - return fetch_url($newurl,false,$redirects,$timeout); + @curl_close($ch); + return post_url($newurl,false,$redirects,$timeout); } } $a->set_curl_code($http_code); @@ -247,7 +249,7 @@ function z_fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accep $base = substr($base,strlen($chunk)); } - if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) { + if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307 || $http_code == 308) { $matches = array(); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); $newurl = trim(array_pop($matches)); @@ -339,7 +341,7 @@ function z_post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0 $base = substr($base,strlen($chunk)); } - if($http_code == 301 || $http_code == 302 || $http_code == 303) { + if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307 || $http_code == 308) { $matches = array(); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); $newurl = trim(array_pop($matches)); -- cgit v1.2.3 From 414f7ccc0085a5bec40323847b6a8819fc6eefca Mon Sep 17 00:00:00 2001 From: Olaf Conradi Date: Wed, 26 Dec 2012 04:22:43 +0100 Subject: Include zot for magic --- mod/magic.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mod/magic.php b/mod/magic.php index b48e2b8c9..b74130005 100644 --- a/mod/magic.php +++ b/mod/magic.php @@ -1,5 +1,7 @@ Date: Wed, 26 Dec 2012 16:08:21 +0100 Subject: POST redirects to POST on 308 only POST redirects to POST on 308 only, 301-307 to GET. Side-effect is that POST params get dropped for GET. --- include/network.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/network.php b/include/network.php index 5571285b4..2180e44aa 100644 --- a/include/network.php +++ b/include/network.php @@ -172,7 +172,11 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) if (isset($url_parsed)) { $redirects++; @curl_close($ch); - return post_url($newurl,false,$redirects,$timeout); + if($http_code == 308) { + return post_url($newurl,$params,$redirects,$timeout); + } else { + return fetch_url($newurl,false,$redirects,$timeout); + } } } $a->set_curl_code($http_code); @@ -351,7 +355,11 @@ function z_post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0 if (isset($url_parsed)) { $redirects++; curl_close($ch); - return z_post_url($newurl,$params,$headers,$redirects,$timeout); + if($http_code == 308) { + return z_post_url($newurl,$params,$headers,$redirects,$timeout); + } else { + return z_fetch_url($newurl,false,$headers,$redirects,$timeout); + } } } $rc = intval($http_code); -- cgit v1.2.3 From 7f13baf367e53a4222c068d81557d6618e6292ff Mon Sep 17 00:00:00 2001 From: Olaf Conradi Date: Wed, 26 Dec 2012 16:30:19 +0100 Subject: Same applies for 307 308 spec mentions 307 must not rewrite POST to GET --- include/network.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/network.php b/include/network.php index 2180e44aa..6ef1c2605 100644 --- a/include/network.php +++ b/include/network.php @@ -172,7 +172,7 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) if (isset($url_parsed)) { $redirects++; @curl_close($ch); - if($http_code == 308) { + if($http_code == 307 || $http_code == 308) { return post_url($newurl,$params,$redirects,$timeout); } else { return fetch_url($newurl,false,$redirects,$timeout); @@ -355,9 +355,10 @@ function z_post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0 if (isset($url_parsed)) { $redirects++; curl_close($ch); - if($http_code == 308) { + if($http_code == 307 || $http_code == 308) { return z_post_url($newurl,$params,$headers,$redirects,$timeout); } else { + logger("params=".print_r($params, true)); return z_fetch_url($newurl,false,$headers,$redirects,$timeout); } } -- cgit v1.2.3 From a5cbea038e1f4aff61e3c761ca8b25f8dd674609 Mon Sep 17 00:00:00 2001 From: Olaf Conradi Date: Wed, 26 Dec 2012 18:44:30 +0100 Subject: Tweak behavior of redirect for http codes Not every webserver supports 308 redirects with POST correctly. Most sites are already configured using 301 or 302 redirects. This only effects POST, GET behavior is unmodified. Most calls to POST are under control by friendica not the end user, so should not be a problem ni general. GET with 301, 302, 303, 307 and 308 are redirected as GET POST with 301, 302, 307 and 308 are redirected as POST POST with 303 is redirected as GET --- include/network.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/include/network.php b/include/network.php index 6ef1c2605..321eb375f 100644 --- a/include/network.php +++ b/include/network.php @@ -172,10 +172,10 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) if (isset($url_parsed)) { $redirects++; @curl_close($ch); - if($http_code == 307 || $http_code == 308) { - return post_url($newurl,$params,$redirects,$timeout); - } else { + if($http_code == 303) { return fetch_url($newurl,false,$redirects,$timeout); + } else { + return post_url($newurl,$params,$redirects,$timeout); } } } @@ -355,11 +355,10 @@ function z_post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0 if (isset($url_parsed)) { $redirects++; curl_close($ch); - if($http_code == 307 || $http_code == 308) { - return z_post_url($newurl,$params,$headers,$redirects,$timeout); - } else { - logger("params=".print_r($params, true)); + if($http_code == 303) { return z_fetch_url($newurl,false,$headers,$redirects,$timeout); + } else { + return z_post_url($newurl,$params,$headers,$redirects,$timeout); } } } -- cgit v1.2.3