Skip to content

Commit

Permalink
X-Request-Id : it is sometimes an array, add repository and project n…
Browse files Browse the repository at this point in the history
…ame in it
  • Loading branch information
Gustry committed Mar 5, 2025
1 parent 5e5bce9 commit edd8f35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lizmap/modules/lizmap/lib/Request/OGCRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,14 @@ protected function logRequestIfError($code, $headers)

$message = 'The HTTP OGC request to QGIS Server ended with an error.';

$xRequestId = $headers['X-Request-Id'] ?? '';
if ($xRequestId !== '') {
$message .= ' The X-Request-Id `'.$xRequestId.'`.';
if (isset($headers['X-Request-Id'])) {
if (is_array($headers['X-Request-Id'])) {
$xRequestId = implode(',', $headers['X-Request-Id']);
} else {
$xRequestId = $headers['X-Request-Id'];
}
} else {
$xRequestId = '';
}

// The master error with MAP parameter
Expand Down
18 changes: 15 additions & 3 deletions lizmap/modules/lizmap/lib/Request/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,14 @@ protected static function buildHeaders($url, $options)
), $options['headers']);

if (strpos($url, self::$services->wmsServerURL) === 0) {
// headers only for QGIS server
// Headers only for QGIS server
parse_str(parse_url($options['headers']['X-Qgis-Service-Url'], PHP_URL_QUERY), $output);
$requestId = $output['repository'].'-'.$output['project'];
$requestId .= '-'.uniqid().'-'.bin2hex(random_bytes(10));
$options['headers'] = array_merge(
self::userHttpHeader(),
self::$services->wmsServerHeaders,
array('X-Request-Id' => uniqid().'-'.bin2hex(random_bytes(10))),
array('X-Request-Id' => $requestId),
$options['headers']
);
}
Expand All @@ -339,7 +342,16 @@ protected static function logRequestIfError($httpCode, $url, $headers = array())
return;
}

$xRequestId = $headers['X-Request-Id'] ?? '';
if (isset($headers['X-Request-Id'])) {
if (is_array($headers['X-Request-Id'])) {
$xRequestId = implode(',', $headers['X-Request-Id']);
$xRequestId = json_encode($headers['X-Request-Id']);
} else {
$xRequestId = $headers['X-Request-Id'];
}
} else {
$xRequestId = '';
}

$lizmapAdmin = 'An HTTP request ended with an error, please check the main error log.';
$lizmapAdmin .= ' HTTP code '.$httpCode.'.';
Expand Down
1 change: 1 addition & 0 deletions tests/units/classes/Request/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ public function testBuildHeaders($options, $expectedHeaders, $expectedBody, $exp
$this->assertEquals($value, $result['headers'][$header]);
}
$this->assertArrayHasKey('X-Request-Id', $result['headers']);
$this->assertIsString($result['headers']['X-Request-Id']);
$this->assertEquals($expectedBody, $result['body']);
if ($expectedUrl) {
$this->assertEquals($expectedUrl, $url);
Expand Down

0 comments on commit edd8f35

Please sign in to comment.