diff options
author | Joas Schilling <coding@schilljs.com> | 2017-01-09 14:56:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-09 14:56:20 +0100 |
commit | fee18d6c789eff0805667a59c501afc7cd9605f5 (patch) | |
tree | 009764b3c809049f6df69e320017d576b905419e /lib/private/App | |
parent | 4f9ff96f79749e5d2cb6283a52475faf7145c71d (diff) | |
parent | fc04779a26ac4119cc5c63dd89bb6039b461d1d9 (diff) | |
download | nextcloud-server-fee18d6c789eff0805667a59c501afc7cd9605f5.tar.gz nextcloud-server-fee18d6c789eff0805667a59c501afc7cd9605f5.zip |
Merge pull request #2814 from nextcloud/appstore_etag_validation
Add ETag validation to appstore requests
Diffstat (limited to 'lib/private/App')
-rw-r--r-- | lib/private/App/AppStore/Fetcher/AppFetcher.php | 7 | ||||
-rw-r--r-- | lib/private/App/AppStore/Fetcher/Fetcher.php | 39 |
2 files changed, 40 insertions, 6 deletions
diff --git a/lib/private/App/AppStore/Fetcher/AppFetcher.php b/lib/private/App/AppStore/Fetcher/AppFetcher.php index 9ebc12dbc27..7c5efafc92f 100644 --- a/lib/private/App/AppStore/Fetcher/AppFetcher.php +++ b/lib/private/App/AppStore/Fetcher/AppFetcher.php @@ -59,11 +59,14 @@ class AppFetcher extends Fetcher { /** * Only returns the latest compatible app release in the releases array * + * @param string $ETag + * @param string $content + * * @return array */ - protected function fetch() { + protected function fetch($ETag, $content) { /** @var mixed[] $response */ - $response = parent::fetch(); + $response = parent::fetch($ETag, $content); $ncVersion = $this->config->getSystemValue('version'); $ncMajorVersion = explode('.', $ncVersion)[0]; diff --git a/lib/private/App/AppStore/Fetcher/Fetcher.php b/lib/private/App/AppStore/Fetcher/Fetcher.php index 2067242e817..dab79e11821 100644 --- a/lib/private/App/AppStore/Fetcher/Fetcher.php +++ b/lib/private/App/AppStore/Fetcher/Fetcher.php @@ -21,6 +21,7 @@ namespace OC\App\AppStore\Fetcher; +use OCP\AppFramework\Http; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\IAppData; use OCP\Files\NotFoundException; @@ -62,15 +63,37 @@ abstract class Fetcher { /** * Fetches the response from the server * + * @param string $ETag + * @param string $content + * * @return array */ - protected function fetch() { + protected function fetch($ETag, $content) { + $options = []; + + if ($ETag !== '') { + $options['headers'] = [ + 'If-None-Match' => $ETag, + ]; + } + $client = $this->clientService->newClient(); - $response = $client->get($this->endpointUrl); + $response = $client->get($this->endpointUrl, $options); + $responseJson = []; - $responseJson['data'] = json_decode($response->getBody(), true); + if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) { + $responseJson['data'] = json_decode($content, true); + } else { + $responseJson['data'] = json_decode($response->getBody(), true); + $ETag = $response->getHeader('ETag'); + } + $responseJson['timestamp'] = $this->timeFactory->getTime(); $responseJson['ncversion'] = $this->config->getSystemValue('version'); + if ($ETag !== '') { + $responseJson['ETag'] = $ETag; + } + return $responseJson; } @@ -82,6 +105,9 @@ abstract class Fetcher { public function get() { $rootFolder = $this->appData->getFolder('/'); + $ETag = ''; + $content = ''; + try { // File does already exists $file = $rootFolder->getFile($this->fileName); @@ -95,6 +121,11 @@ abstract class Fetcher { isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->config->getSystemValue('version', '0.0.0')) { return $jsonBlob['data']; } + + if (isset($jsonBlob['ETag'])) { + $ETag = $jsonBlob['ETag']; + $content = json_encode($jsonBlob['data']); + } } } catch (NotFoundException $e) { // File does not already exists @@ -103,7 +134,7 @@ abstract class Fetcher { // Refresh the file content try { - $responseJson = $this->fetch(); + $responseJson = $this->fetch($ETag, $content); $file->putContent(json_encode($responseJson)); return json_decode($file->getContent(), true)['data']; } catch (\Exception $e) { |