From 695bcb34ddabcacb7900edc3f5eb8dc77bf52dc6 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Fri, 4 May 2012 08:03:12 +0000
Subject: [PATCH] metarefresh: Restructure MetaLoader to return early on
 error/not modified.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@3086 44740490-163a-0410-bde0-09ae8108e29a
---
 modules/metarefresh/lib/MetaLoader.php | 102 ++++++++++++-------------
 1 file changed, 51 insertions(+), 51 deletions(-)

diff --git a/modules/metarefresh/lib/MetaLoader.php b/modules/metarefresh/lib/MetaLoader.php
index 2fb532ff2..67c13fe68 100644
--- a/modules/metarefresh/lib/MetaLoader.php
+++ b/modules/metarefresh/lib/MetaLoader.php
@@ -56,68 +56,68 @@ class sspmod_metarefresh_MetaLoader {
 		}
 
 		// We have response headers, so the request succeeded
-		if(isset($responseHeaders)) {
-
-			// 200 OK
-			if(preg_match('@^HTTP/1\.[01]\s200\s@', $responseHeaders[0])) {
-
-				if (isset($source['conditionalGET']) && $source['conditionalGET']) {
-					// Stale or no metadata, so a fresh copy
-					SimpleSAML_Logger::debug('Downloaded fresh copy');
-				}
-
-				$entities = $this->loadXML($data, $source);
-
-				foreach($entities as $entity) {
-
-					if(isset($source['blacklist'])) {
-						if(!empty($source['blacklist']) && in_array($entity->getEntityID(), $source['blacklist'])) {
-							SimpleSAML_Logger::info('Skipping "' .  $entity->getEntityID() . '" - blacklisted.' . "\n");
-							continue;
-						}
-					}
+		if(!isset($responseHeaders)) {
+			// No response headers, this means the request failed in some way, so re-use old data
+			SimpleSAML_Logger::debug('No response from ' . $source['src'] . ' - attempting to re-use cached metadata');
+			$this->addCachedMetadata($source);
+			return;
+		} elseif(preg_match('@^HTTP/1\.[01]\s304\s@', $responseHeaders[0])) {
+			// 304 response
+			SimpleSAML_Logger::debug('Received HTTP 304 (Not Modified) - attempting to re-use cached metadata');
+			$this->addCachedMetadata($source);
+			return;
+		} elseif(!preg_match('@^HTTP/1\.[01]\s200\s@', $responseHeaders[0])) {
+			// Other error.
+			SimpleSAML_Logger::debug('Error from ' . $source['src'] . ' - attempting to re-use cached metadata');
+			$this->addCachedMetadata($source);
+			return;
+		}
 
-					if(isset($source['whitelist'])) {
-						if(!empty($source['whitelist']) && !in_array($entity->getEntityID(), $source['whitelist'])) {
-							SimpleSAML_Logger::info('Skipping "' .  $entity->getEntityID() . '" - not in the whitelist.' . "\n");
-							continue;
-						}
-					}
+		/* Everything OK. Proceed. */
+		if (isset($source['conditionalGET']) && $source['conditionalGET']) {
+			// Stale or no metadata, so a fresh copy
+			SimpleSAML_Logger::debug('Downloaded fresh copy');
+		}
 
-					if(array_key_exists('validateFingerprint', $source) && $source['validateFingerprint'] !== NULL) {
-						if(!$entity->validateFingerprint($source['validateFingerprint'])) {
-							SimpleSAML_Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify signature.' . "\n");
-							continue;
-						}
-					}
+		$entities = $this->loadXML($data, $source);
 
-					$template = NULL;
-					if (array_key_exists('template', $source)) $template = $source['template'];
+		foreach($entities as $entity) {
 
-					$this->addMetadata($source['src'], $entity->getMetadata1xSP(), 'shib13-sp-remote', $template);
-					$this->addMetadata($source['src'], $entity->getMetadata1xIdP(), 'shib13-idp-remote', $template);
-					$this->addMetadata($source['src'], $entity->getMetadata20SP(), 'saml20-sp-remote', $template);
-					$this->addMetadata($source['src'], $entity->getMetadata20IdP(), 'saml20-idp-remote', $template);
-					$attributeAuthorities = $entity->getAttributeAuthorities();
-					if (!empty($attributeAuthorities)) {
-						$this->addMetadata($source['src'], $attributeAuthorities[0], 'attributeauthority-remote', $template);
-					}
+			if(isset($source['blacklist'])) {
+				if(!empty($source['blacklist']) && in_array($entity->getEntityID(), $source['blacklist'])) {
+					SimpleSAML_Logger::info('Skipping "' .  $entity->getEntityID() . '" - blacklisted.' . "\n");
+					continue;
 				}
+			}
 
-				$this->saveState($source, $responseHeaders);
+			if(isset($source['whitelist'])) {
+				if(!empty($source['whitelist']) && !in_array($entity->getEntityID(), $source['whitelist'])) {
+					SimpleSAML_Logger::info('Skipping "' .  $entity->getEntityID() . '" - not in the whitelist.' . "\n");
+					continue;
+				}
 			}
 
-			// 304 response
-			if(preg_match('@^HTTP/1\.[01]\s304\s@', $responseHeaders[0])) {
-				SimpleSAML_Logger::debug('Received HTTP 304 (Not Modified) - attempting to re-use cached metadata');
-				$this->addCachedMetadata($source);
+			if(array_key_exists('validateFingerprint', $source) && $source['validateFingerprint'] !== NULL) {
+				if(!$entity->validateFingerprint($source['validateFingerprint'])) {
+					SimpleSAML_Logger::info('Skipping "' . $entity->getEntityId() . '" - could not verify signature.' . "\n");
+					continue;
+				}
 			}
 
-		} else {
-			// No response headers, this means the request failed in some way, so re-use old data
-			SimpleSAML_Logger::debug('No response from ' . $source['src'] . ' - attempting to re-use cached metadata');
-			$this->addCachedMetadata($source);
+			$template = NULL;
+			if (array_key_exists('template', $source)) $template = $source['template'];
+
+			$this->addMetadata($source['src'], $entity->getMetadata1xSP(), 'shib13-sp-remote', $template);
+			$this->addMetadata($source['src'], $entity->getMetadata1xIdP(), 'shib13-idp-remote', $template);
+			$this->addMetadata($source['src'], $entity->getMetadata20SP(), 'saml20-sp-remote', $template);
+			$this->addMetadata($source['src'], $entity->getMetadata20IdP(), 'saml20-idp-remote', $template);
+			$attributeAuthorities = $entity->getAttributeAuthorities();
+			if (!empty($attributeAuthorities)) {
+				$this->addMetadata($source['src'], $attributeAuthorities[0], 'attributeauthority-remote', $template);
+			}
 		}
+
+		$this->saveState($source, $responseHeaders);
 	}
 
 	/**
-- 
GitLab