Skip to content

Commit

Permalink
fix: custom headers handling by local server
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Nov 20, 2024
1 parent 8e3286d commit d492822
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
21 changes: 7 additions & 14 deletions resources/server/router.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,19 @@
$headersFile = $_SERVER['DOCUMENT_ROOT'] . '/../' . SERVER_TMP_DIR . '/headers.ini';
if (file_exists($headersFile)) {
$headersArray = parse_ini_file($headersFile, true);
// joker path tree
$pathParts = explode('/', pathinfo($path, PATHINFO_DIRNAME));
$previous = '';
for ($i = 0; $i < \count($pathParts); $i++) {
$headersSectionKey = $previous . \sprintf('/%s/*', trim($pathParts[$i], '\\'));
$previous .= '/' . $pathParts[$i];
$headersSectionKey = '/' . trim($headersSectionKey, '/');
if (\array_key_exists($headersSectionKey, $headersArray)) {
foreach ($headersArray[$headersSectionKey] as $key => $value) {
header("$key: " . $value);
// path with wildcard
foreach ($headersArray as $source => $headers) {
list($match) = sscanf($path, str_replace('*', '%s', $source));
if ($match !== null) {
foreach ($headers as $key => $value) {
header("$key: $value");
}
}
if (empty($pathParts[1])) {
break;
}
}
// exact file path
if (\array_key_exists($path, $headersArray)) {
foreach ($headersArray[$path] as $key => $value) {
header("$key: " . $value);
header("$key: $value");
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Command/Serve.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ private function buildSuccess(OutputInterface $output): void
// writes `headers.ini` file
if (null !== $headers = $this->getBuilder()->getConfig()->get('headers')) {
$output->writeln('Writing headers file...');
Util\File::getFS()->remove(Util::joinFile($this->getPath(), self::TMP_DIR, 'headers.ini'));
foreach ($headers as $header) {
Util\File::getFS()->appendToFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'headers.ini'), "[{$header['source']}]\n");
foreach ($header['headers'] as $h) {
Expand Down

0 comments on commit d492822

Please sign in to comment.