public function testMetaTagExtraction() { $this->assertFalse(AmberNetworkUtils::find_meta_redirect("")); $this->assertFalse(AmberNetworkUtils::find_meta_redirect("bogus string")); $this->assertFalse(AmberNetworkUtils::find_meta_redirect(<<<EOD <html> <head><title>bad man</title></head> <body> Use META tags like this: <meta http-equiv="refresh" content="30; URL=http://www.example.org/login"> </body> </html> EOD )); $this->assertEquals("http://www.example.org/login", AmberNetworkUtils::find_meta_redirect(<<<EOD <html> <head><title>bad man</title> <meta http-equiv="refresh" content="30; URL=http://www.example.org/login"> </head> <body> Use META tags like this: <meta http-equiv="refresh" content="30; URL=http://www.example.org/login"> </body> </html> EOD )); $this->assertEquals("http://www.example.org/login", AmberNetworkUtils::find_meta_redirect(<<<EOD <html> <head><title>bad man</title> <meta http-equiv="REFRESH" content="0; url=http://www.example.org/login"> </head> <body> Use META tags like this: <meta http-equiv="refresh" content="30; URL=http://www.example.org/login"> </body> </html> EOD )); $this->assertFalse(AmberNetworkUtils::find_meta_redirect(<<<EOD <html> <head><title>bad man</title> <meta http-equiv="refresh" content="5"> </head> <body> Use META tags like this: <meta http-equiv="refresh" content="30; URL=http://www.example.org/login"> </body> </html> EOD )); $this->assertEquals("http://www.example.org/login", AmberNetworkUtils::find_meta_redirect(<<<EOD <html> <head><title>bad man</title> <meta http-equiv="REFRESH" content='0;url = http://www.example.org/login'> </head> <body> Use META tags like this: <meta http-equiv="refresh" content="30; URL=http://www.example.org/login"> </body> </html> EOD )); }
/** * Look at the results from a lookup using curl_multi, and identify urls that we need * to query again, because a redirect is needed but was not followed * @param $urls associative array of url lookups, keyed by url * @return associative array of the subset of items that need to be queried again */ public static function find_urls_requiring_redirects($urls) { $result = array(); foreach ($urls as $url => $data) { if ($data['info']['http_code'] == 301 || $data['info']['http_code'] == 302) { $result[$url] = $data; } else { if (AmberNetworkUtils::find_meta_redirect($data['body'])) { $result[$url] = $data; } } } return $result; }