Skip to content
This repository was archived by the owner on Aug 6, 2023. It is now read-only.

Commit 573e8a6

Browse files
committed
Merge pull request #125 from addiks/bugfix/123
bugfix #123; resolve http redirects in REST.php
2 parents bfe9921 + 452cc5d commit 573e8a6

File tree

5 files changed

+58
-28
lines changed

5 files changed

+58
-28
lines changed

src/Pyrus/Channel/RemoteCategories.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function __construct(\Pyrus\ChannelInterface $channelinfo)
4242
function offsetGet($var)
4343
{
4444
$url = $this->parent->protocols->rest['REST1.1']->baseurl . 'c/' . urlencode($var) . '/packagesinfo.xml';
45-
$info = $this->rest->retrieveCacheFirst($url);
45+
$info = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
4646
return new RemoteCategory($this->parent, $var, $info);
4747
}
4848

@@ -70,7 +70,7 @@ function current()
7070
{
7171
$category = $this->key();
7272
$url = $this->parent->protocols->rest['REST1.1']->baseurl . 'c/' . urlencode($category) . '/packagesinfo.xml';
73-
$info = $this->rest->retrieveCacheFirst($url);
73+
$info = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
7474
return new RemoteCategory($this->parent, $category, $info);
7575
}
7676

@@ -88,7 +88,7 @@ function next()
8888
function rewind()
8989
{
9090
$url = $this->parent->protocols->rest['REST1.1']->baseurl . 'c/categories.xml';
91-
$this->categoryList = $this->rest->retrieveCacheFirst($url);
91+
$this->categoryList = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
9292
$this->categoryList = $this->categoryList['c'];
9393
if (!isset($this->categoryList[0])) {
9494
$this->categoryList = array($this->categoryList);

src/Pyrus/Channel/RemoteMaintainers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function __construct(\Pyrus\ChannelFileInterface $channelinfo)
4343
function offsetGet($var)
4444
{
4545
$url = $this->parent->protocols->rest['REST1.1']->baseurl . 'm/' . urlencode($var) . '/info.xml';
46-
$info = $this->rest->retrieveCacheFirst($url);
46+
$info = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
4747
return $info;
4848
}
4949

@@ -76,7 +76,7 @@ function current()
7676
{
7777
$handle = $this->key();
7878
$url = $this->parent->protocols->rest['REST1.1']->baseurl . 'm/' . urlencode($handle) . '/info.xml';
79-
$info = $this->rest->retrieveCacheFirst($url);
79+
$info = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
8080
return $info['m'];
8181
}
8282

@@ -94,7 +94,7 @@ function next()
9494
function rewind()
9595
{
9696
$url = $this->parent->protocols->rest['REST1.1']->baseurl . 'm/allmaintainers.xml';
97-
$this->maintainerList = $this->rest->retrieveCacheFirst($url);
97+
$this->maintainerList = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
9898
$this->maintainerList = $this->maintainerList['h'];
9999
if (!isset($this->maintainerList[0])) {
100100
$this->maintainerList = array($this->maintainerList);

src/Pyrus/Channel/RemotePackage.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,14 @@ function setRawVersion($var, $value)
293293
if (isset($this->parent->protocols->rest['REST1.3'])) {
294294
$a = $this->remoteAbridgedInfo = $this->rest->retrieveCacheFirst(
295295
$this->parent->protocols->rest['REST1.3']->baseurl .
296-
'r/' . strtolower($this->name) . '/v2.' . $value['release'] . '.xml');
296+
'r/' . strtolower($this->name) . '/v2.' . $value['release'] . '.xml',
297+
false, false, 'text/xml');
297298
$this->packageInfo['version']['api'] = $a['a'];
298299
} else {
299300
$a = $this->remoteAbridgedInfo = $this->rest->retrieveCacheFirst(
300301
$this->parent->protocols->rest['REST1.0']->baseurl .
301-
'r/' . strtolower($this->name) . '/' . $value['release'] . '.xml');
302+
'r/' . strtolower($this->name) . '/' . $value['release'] . '.xml',
303+
false, false, 'text/xml');
302304
}
303305

304306
$this->packageInfo['version'] = $value;
@@ -462,10 +464,12 @@ function offsetGet($var)
462464
$ok = \Pyrus\Installer::betterStates($this->releaseList, true);
463465
if (isset($this->parent->protocols->rest['REST1.3'])) {
464466
$rinfo = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.3']->baseurl .
465-
'r/' . $lowerpackage . '/allreleases2.xml');
467+
'r/' . $lowerpackage . '/allreleases2.xml',
468+
false, false, 'text/xml');
466469
} else {
467470
$rinfo = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.0']->baseurl .
468-
'r/' . $lowerpackage . '/allreleases.xml');
471+
'r/' . $lowerpackage . '/allreleases.xml',
472+
false, false, 'text/xml');
469473
}
470474

471475
if (!isset($rinfo['r'][0])) {
@@ -562,10 +566,12 @@ function rewind()
562566

563567
if (isset($this->parent->protocols->rest['REST1.3'])) {
564568
$info = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.3']->baseurl .
565-
'r/' . strtolower($this->name) . '/allreleases2.xml');
569+
'r/' . strtolower($this->name) . '/allreleases2.xml',
570+
false, false, 'text/xml');
566571
} else {
567572
$info = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.0']->baseurl .
568-
'r/' . strtolower($this->name) . '/allreleases.xml');
573+
'r/' . strtolower($this->name) . '/allreleases.xml',
574+
false, false, 'text/xml');
569575
}
570576

571577
$this->releaseList = $info['r'];
@@ -609,7 +615,8 @@ function getPackageInfo($var)
609615
{
610616
$lowerpackage = strtolower($var);
611617
return $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.0']->baseurl .
612-
'p/' . $lowerpackage . '/info.xml');
618+
'p/' . $lowerpackage . '/info.xml',
619+
false, false, 'text/xml');
613620
}
614621

615622
function getPackagefileObject()
@@ -638,7 +645,8 @@ function grabEntirePackagexml()
638645

639646
$pxml = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.0']->baseurl .
640647
'r/' . strtolower($this->name) . '/package.' .
641-
$this->version['release'] . '.xml');
648+
$this->version['release'] . '.xml',
649+
false, false, 'text/xml');
642650
$this->fromArray(array('package' => $pxml));
643651
$this->fullPackagexml = true;
644652
}
@@ -767,7 +775,8 @@ function figureOutBestVersion(\Pyrus\PackageFile\v2\Dependencies\Package $compos
767775
if (isset($this->parent->protocols->rest['REST1.3'])) {
768776
$api = $this->rest->retrieveCacheFirst(
769777
$this->parent->protocols->rest['REST1.3']->baseurl .
770-
'r/' . strtolower($this->name) . '/v2.' . $versioninfo['v'] . '.xml');
778+
'r/' . strtolower($this->name) . '/v2.' . $versioninfo['v'] . '.xml',
779+
false, false, 'text/xml');
771780
} else {
772781
throw new Exception('Channel ' . $this->channel .
773782
' does not support ' .

src/Pyrus/Channel/RemotePackages.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function current()
9494
$lowerpackage = strtolower($lowerpackage);
9595

9696
$url = $this->parent->protocols->rest['REST1.0']->baseurl . 'p/' . $lowerpackage . '/info.xml';
97-
$info = $this->rest->retrieveCacheFirst($url);
97+
$info = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
9898

9999
if (isset($releases)) {
100100
$pxml = new RemotePackage($this->parent, $releases);
@@ -115,10 +115,12 @@ function getPackage($package)
115115
$lowerpackage = strtolower($package);
116116
if (isset($this->parent->protocols->rest['REST1.3'])) {
117117
$info = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.3']->baseurl .
118-
'r/' . $lowerpackage . '/allreleases2.xml');
118+
'r/' . $lowerpackage . '/allreleases2.xml',
119+
false, false, 'text/xml');
119120
} else {
120121
$info = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.0']->baseurl .
121-
'r/' . $lowerpackage . '/allreleases.xml');
122+
'r/' . $lowerpackage . '/allreleases.xml',
123+
false, false, 'text/xml');
122124
}
123125

124126
if (!isset($info['r'][0])) {
@@ -143,7 +145,7 @@ function getPackage($package)
143145
}
144146

145147
$url = $this->parent->protocols->rest['REST1.0']->baseurl . 'p/' . $lowerpackage . '/info.xml';
146-
$info = $this->rest->retrieveCacheFirst($url);
148+
$info = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
147149

148150
$pxml = new RemotePackage($this->parent, $releases);
149151

@@ -168,7 +170,7 @@ function next()
168170
function rewind()
169171
{
170172
$url = $this->parent->protocols->rest['REST1.0']->baseurl . 'p/packages.xml';
171-
$this->packageList = $this->rest->retrieveCacheFirst($url);
173+
$this->packageList = $this->rest->retrieveCacheFirst($url, false, false, 'text/xml');
172174
$this->packageList = $this->packageList['p'];
173175
if (!is_array($this->packageList)) {
174176
$this->packageList = array($this->packageList);
@@ -181,10 +183,12 @@ function rewind()
181183
foreach ($this->packageList as $lowerpackage) {
182184
if (isset($this->parent->protocols->rest['REST1.3'])) {
183185
$info = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.3']->baseurl .
184-
'r/' . $lowerpackage . '/allreleases2.xml');
186+
'r/' . $lowerpackage . '/allreleases2.xml',
187+
false, false, 'text/xml');
185188
} else {
186189
$info = $this->rest->retrieveCacheFirst($this->parent->protocols->rest['REST1.0']->baseurl .
187-
'r/' . $lowerpackage . '/allreleases.xml');
190+
'r/' . $lowerpackage . '/allreleases.xml',
191+
false, false, 'text/xml');
188192
}
189193

190194
if (!isset($info['r'][0])) {

src/Pyrus/REST.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ function __construct()
4848
*
4949
* @return string|array
5050
*/
51-
function retrieveCacheFirst($url, $accept = false, $forcestring = false)
51+
function retrieveCacheFirst($url, $accept = false, $forcestring = false, $forceContentType=false)
5252
{
5353
$cachefile = $this->config->cache_dir . DIRECTORY_SEPARATOR .
5454
md5($url) . 'rest.cachefile';
5555
if (file_exists($cachefile)) {
5656
return unserialize(implode('', file($cachefile)));
5757
}
5858

59-
return $this->retrieveData($url, $accept, $forcestring);
59+
return $this->retrieveData($url, $accept, $forcestring, $forceContentType);
6060
}
6161

6262
/**
@@ -66,12 +66,13 @@ function retrieveCacheFirst($url, $accept = false, $forcestring = false)
6666
* @param array|false contents of the accept-encoding header
6767
* @param boolean if true, xml will be returned as a string, otherwise, xml will be
6868
* parsed using Pyrus\XMLParser
69+
* @param string|false if string, this will override the recieved content-type and can enforce a parser.
6970
*
7071
* @return string|array
7172
*
7273
* @throws Pyrus\REST\Exception If the xml cannot be parsed
7374
*/
74-
function retrieveData($url, $accept = false, $forcestring = false)
75+
function retrieveData($url, $accept = false, $forcestring = false, $forceContentType=false)
7576
{
7677
$cacheId = $this->getCacheId($url);
7778
if ($ret = $this->useLocalCache($url, $cacheId)) {
@@ -109,14 +110,30 @@ function retrieveData($url, $accept = false, $forcestring = false)
109110
$headers = array();
110111
}
111112

113+
// handle HTTP-redirects
114+
if(isset($headers['location']) && $headers['location'] !== $url){
115+
$content = $this->retrieveData($headers['location'], $accept, $forcestring, $forceContentType);
116+
}
117+
112118
if ($forcestring) {
113119
$this->saveCache($url, $content, $lastmodified, false, $cacheId);
114120
return $content;
115121
}
122+
123+
if(isset($headers['location']) && $headers['location'] !== $url){
124+
return $content;
125+
}
116126

117-
// Default to XML if no content-type is provided
118-
//TODO: Deal with text as well, look at PEAR 1.9/1.8
119-
$ct = isset($headers['content-type']) ? $headers['content-type'] : 'text/xml';
127+
if(is_string($forceContentType)){
128+
$ct = $forceContentType;
129+
130+
}else{
131+
132+
// Default to XML if no content-type is provided
133+
//TODO: Deal with text as well, look at PEAR 1.9/1.8
134+
$ct = isset($headers['content-type']) ? $headers['content-type'] : 'text/xml';
135+
}
136+
120137
switch ($ct) {
121138
case 'text/xml' :
122139
case 'application/xml' :

0 commit comments

Comments
 (0)