Skip to content

Commit 8db1632

Browse files
tvdijenmonkeyiqdependabot[bot]
authored
Fix to work with Symfony's http-client (#63)
* Fix to work with Symfony's http-client * Fix * Fix handling response codes * Repair test * Fix path to autoloader * Suppress errors from the http-clients * Properly deal with transport issues * Add warning * add underlying error to the info message * If we are using TransportException then maybe include it here too * This appears to be the only place that is causing the entityid issue (#66) * Bump the all-actions group with 3 updates (#65) Bumps the all-actions group with 3 updates: [simplesamlphp/simplesamlphp-test-framework/.github/workflows/reusable_phplinter.yml](https://github.com/simplesamlphp/simplesamlphp-test-framework), [simplesamlphp/simplesamlphp-test-framework/.github/workflows/reusable_linter.yml](https://github.com/simplesamlphp/simplesamlphp-test-framework) and [geekyeggo/delete-artifact](https://github.com/geekyeggo/delete-artifact). Updates `simplesamlphp/simplesamlphp-test-framework/.github/workflows/reusable_phplinter.yml` from 1.11.1 to 1.11.3 - [Commits](simplesamlphp/simplesamlphp-test-framework@v1.11.1...v1.11.3) Updates `simplesamlphp/simplesamlphp-test-framework/.github/workflows/reusable_linter.yml` from 1.11.1 to 1.11.3 - [Commits](simplesamlphp/simplesamlphp-test-framework@v1.11.1...v1.11.3) Updates `geekyeggo/delete-artifact` from 5 to 6 - [Release notes](https://github.com/geekyeggo/delete-artifact/releases) - [Changelog](https://github.com/GeekyEggo/delete-artifact/blob/main/CHANGELOG.md) - [Commits](GeekyEggo/delete-artifact@v5...v6) --- updated-dependencies: - dependency-name: simplesamlphp/simplesamlphp-test-framework/.github/workflows/reusable_phplinter.yml dependency-version: 1.11.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all-actions - dependency-name: simplesamlphp/simplesamlphp-test-framework/.github/workflows/reusable_linter.yml dependency-version: 1.11.3 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: all-actions - dependency-name: geekyeggo/delete-artifact dependency-version: '6' dependency-type: direct:production update-type: version-update:semver-major dependency-group: all-actions ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * This appears to be the only place that is causing the entityid issue This was mentioned here #64 --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Ben Martin <monkeyiq@users.sourceforge.net> Co-authored-by: monkeyiq <monkeyiq@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent 546c709 commit 8db1632

4 files changed

Lines changed: 29 additions & 24 deletions

File tree

bin/metarefresh.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$baseDir = dirname(__FILE__, 4);
1313

1414
// Add library autoloader.
15-
require_once($baseDir . '/lib/_autoload.php');
15+
require_once($baseDir . '/src/_autoload.php');
1616

1717
if (!\SimpleSAML\Module::isModuleEnabled('metarefresh')) {
1818
echo "You need to enable the metarefresh module before this script can be used.\n";

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"simplesamlphp/simplesamlphp": "~2.5@dev",
4343
"simplesamlphp/xml-common": "~2.7",
4444
"symfony/http-foundation": "~7.4",
45+
"symfony/http-client": "~7.4",
4546
"symfony/var-exporter": "~7.4"
4647
},
4748
"require-dev": {

src/MetaLoader.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use SimpleSAML\Metadata;
1111
use SimpleSAML\Utils;
1212
use SimpleSAML\XML\DOMDocumentFactory;
13+
use Symfony\Component\HttpClient\Exception\TransportException;
1314
use Symfony\Component\VarExporter\VarExporter;
1415

1516
/**
@@ -108,27 +109,29 @@ public function loadSource(array $source): void
108109

109110
$httpUtils = new Utils\HTTP();
110111
$data = null;
112+
111113
// GET!
114+
$client = $httpUtils->createHttpClient($context);
115+
$response = $client->request('GET', $source['src'], $context);
112116
try {
113-
/** @var array $response We know this because we set the third parameter to `true` */
114-
$response = $httpUtils->fetch($source['src'], $context, true);
115-
list($data, $responseHeaders) = $response;
116-
} catch (Exception $e) {
117-
Logger::warning('metarefresh: ' . $e->getMessage());
117+
$statusCode = $response->getStatusCode();
118+
} catch (TransportException $e) {
119+
Logger::info('No response from ' . $source['src'] . ' - attempting to re-use cached metadata.'
120+
. ' The error was: ' . $e->getMessage());
121+
$this->addCachedMetadata($source);
122+
return;
118123
}
119124

125+
$responseHeaders = $response->getHeaders(false);
126+
$data = $response->getContent(false);
127+
120128
// We have response headers, so the request succeeded
121-
if (!isset($responseHeaders)) {
122-
// No response headers, this means the request failed in some way, so re-use old data
123-
Logger::info('No response from ' . $source['src'] . ' - attempting to re-use cached metadata');
124-
$this->addCachedMetadata($source);
125-
return;
126-
} elseif (preg_match('@^HTTP/(2\.0|1\.[01])\s304\s@', $responseHeaders[0])) {
129+
if ($statusCode === 304) {
127130
// 304 response
128131
Logger::debug('Received HTTP 304 (Not Modified) - attempting to re-use cached metadata');
129132
$this->addCachedMetadata($source);
130133
return;
131-
} elseif (!preg_match('@^HTTP/(2\.0|1\.[01])\s200\s@', $responseHeaders[0])) {
134+
} elseif ($statusCode !== 200) {
132135
// Other error
133136
Logger::info('Error from ' . $source['src'] . ' - attempting to re-use cached metadata');
134137
$this->addCachedMetadata($source);
@@ -137,7 +140,7 @@ public function loadSource(array $source): void
137140
} else {
138141
// Local file.
139142
$data = file_get_contents($source['src']);
140-
$responseHeaders = null;
143+
$responseHeaders = [];
141144
}
142145

143146
// Everything OK. Proceed.
@@ -199,7 +202,7 @@ public function loadSource(array $source): void
199202
if (count($attributeAuthorities) && !empty($attributeAuthorities[0])) {
200203
$this->addMetadata(
201204
$source['src'],
202-
$attributeAuthorities,
205+
$attributeAuthorities[0],
203206
'attributeauthority-remote',
204207
$template,
205208
);
@@ -384,23 +387,25 @@ private function createContext(array $source): array
384387
$name = $config->getOptionalString('technicalcontact_name', null);
385388
$mail = $config->getOptionalString('technicalcontact_email', null);
386389

387-
$rawheader = "User-Agent: SimpleSAMLphp metarefresh, run by $name <$mail>\r\n";
390+
$headers = [
391+
'User-Agent' => "SimpleSAMLphp metarefresh, run by $name <$mail>",
392+
];
388393

389394
if (isset($source['conditionalGET']) && $source['conditionalGET']) {
390395
if (array_key_exists($source['src'], $this->state)) {
391396
$sourceState = $this->state[$source['src']];
392397

393398
if (isset($sourceState['last-modified'])) {
394-
$rawheader .= 'If-Modified-Since: ' . $sourceState['last-modified'] . "\r\n";
399+
$headers['If-Modified-Since'] = $sourceState['last-modified'];
395400
}
396401

397402
if (isset($sourceState['etag'])) {
398-
$rawheader .= 'If-None-Match: ' . $sourceState['etag'] . "\r\n";
403+
$headers['If-None-Match'] = $sourceState['etag'];
399404
}
400405
}
401406
}
402407

403-
return ['http' => ['header' => $rawheader]];
408+
return ['headers' => $headers];
404409
}
405410

406411

@@ -427,13 +432,13 @@ private function addCachedMetadata(array $source): void
427432
* Store caching state data for a source
428433
*
429434
* @param array $source
430-
* @param array|null $responseHeaders
435+
* @param array $responseHeaders
431436
*/
432-
private function saveState(array $source, ?array $responseHeaders): void
437+
private function saveState(array $source, array $responseHeaders): void
433438
{
434439
if (isset($source['conditionalGET']) && $source['conditionalGET']) {
435440
// Headers section
436-
if ($responseHeaders !== null) {
441+
if ($responseHeaders !== []) {
437442
$candidates = ['last-modified', 'etag'];
438443

439444
foreach ($candidates as $candidate) {

tests/src/Controller/MetaRefreshTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected function setUp(): void
5252
'cron' => ['hourly'],
5353
'sources' => [
5454
[
55-
'src' => 'https://example.org/simplesaml/module.php/aggregator/?id=kalmarcentral&set=saml2&exclude=norway',
55+
'src' => 'https://nexus.microsoftonline-p.com/federationmetadata/saml20/federationmetadata.xml',
5656
],
5757
],
5858
'outputFormat' => 'flatfile',
@@ -104,6 +104,5 @@ public function testMetaRefresh(): void
104104
$contents = $response->getContents();
105105
$this->assertStringContainsString('[metarefresh]: Executing set [example]', $contents);
106106
$this->assertStringContainsString('In set [example] loading source', $contents);
107-
$this->assertStringContainsString('attempting to re-use cached metadata', $contents);
108107
}
109108
}

0 commit comments

Comments
 (0)