METAR parsing infinite loop bugfix #599
This commit is contained in:
parent
b0f122a301
commit
ea9ee985e8
@ -4,6 +4,8 @@ namespace App\Services\Metar;
|
||||
|
||||
use App\Contracts\Metar;
|
||||
use App\Support\HttpClient;
|
||||
use function count;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
@ -44,15 +46,31 @@ class AviationWeather extends Metar
|
||||
try {
|
||||
$res = $this->httpClient->get($url, []);
|
||||
$xml = simplexml_load_string($res);
|
||||
if (\count($xml->data->METAR->raw_text) === 0) {
|
||||
|
||||
$attrs = $xml->data->attributes();
|
||||
if (!isset($attrs['num_results'])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$num_results = $attrs['num_results'];
|
||||
if (empty($num_results)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$num_results = (int) $num_results;
|
||||
if ($num_results === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (count($xml->data->METAR->raw_text) === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $xml->data->METAR->raw_text->__toString();
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
Log::error('Error reading METAR: '.$e->getMessage());
|
||||
|
||||
throw $e;
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ class Weather extends Widget
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
/** @var \App\Services\AirportService $airportSvc */
|
||||
$airportSvc = app(AirportService::class);
|
||||
$metar = $airportSvc->getMetar($this->config['icao']);
|
||||
|
||||
|
@ -185,4 +185,15 @@ class MetarTest extends TestCase
|
||||
|
||||
$this->assertNull($airportSvc->getMetar('idk'));
|
||||
}
|
||||
|
||||
public function testHttpCallUnknown()
|
||||
{
|
||||
$this->mockXmlResponse('aviationweather/unknown.xml');
|
||||
|
||||
/** @var AirportService $airportSvc */
|
||||
$airportSvc = app(AirportService::class);
|
||||
|
||||
$metar = $airportSvc->getMetar('7AK4');
|
||||
$this->assertNull($metar);
|
||||
}
|
||||
}
|
||||
|
0
tests/data/aviationweather/unknown.xml
Normal file
0
tests/data/aviationweather/unknown.xml
Normal file
Loading…
Reference in New Issue
Block a user