[英]What could be the cause of unexpected characters served by the PHP internal web server?
我在基於 Alpine 3.8 的 Docker 容器中運行 PHP 內部 Web 服務器。
這是一個簡單的頁面,它是:
<!DOCTYPE html>
<html>
<head>
<title>Test page</title>
</head>
<body>
<div>
This is a 'hello world' for my CD container.
</div>
<p>
Version: <?php echo getenv('CD_DEMO_VERSION') ?>
</p>
<p>
GUID: <?php echo file_get_contents(__DIR__ . '/guid.txt') ?>
</p>
</body>
</html>
我在 CircleCI 構建過程中有這個,PHPUnit 使用simplexml_load_string()
讀取 HTML 以檢查它是否正常工作。 大多數情況下它沒問題,但偶爾會在 HTML 的開頭或結尾添加控制字符,導致 HTML 解析失敗。 我會說這種情況發生的概率為 10%,因此很難復制。 它似乎不會發生在本地。
在這個構建過程中,我正在等待 Web 服務器啟動,所以我認為這里不會發生競爭條件。
如果 cruft 出現在開始時,它看起來像這樣(我的輸出交換回車到⏎
符號,這不會被 XML 處理器看到):
^@^@<html>⏎ <head>⏎ <title>Test page</title>⏎ </head>⏎ <body>⏎ <div>⏎ This is a 'hello world' for my CD container.⏎ </div>⏎ <p>⏎ Version: </p>⏎ <p>⏎ GUID: 47294362a6154e9a5397a9f9b72cefe1d349ec2e31de12e6084746a084e4e4ef </p>⏎ </body>⏎</html>
如果 cruft 出現在最后,它看起來像這樣:
<!DOCTYPE html>⏎<html>⏎ <head>⏎ <title>Test page</title>⏎ </head>⏎ <body>⏎ <div>⏎ This is a 'hello world' for my CD container.⏎ </div>⏎ <p>⏎ Version: </p>⏎ <p>⏎ GUID: a721f44d05e93debb593eef135fc664fb6b0d3aa6a04d50af7f17339d0d75399 </p>⏎ </body>⏎</html>⏎^@^@
我不知道這些是否是實際的抑揚符和符號,或者這是否只是隨機內容呈現到我的屏幕上的方式。
看起來 Web 服務器正在提供 UTF-8 字符集標頭。
我認為我的 PHPUnit 測試是相關的,所以我會在這里添加它。 由於 CircleCI 的工作方式,我無法在容器外部發布 Docker 端口,因此我使用docker exec
從內部執行 HTTP 操作:
public function testApp()
{
// Get the page and convert it to an XML object
$html = $this->getWebPage();
// CI is giving me some parsing issues, so let's see it
echo str_replace("\n", "⏎", $html);
$doc = simplexml_load_string($html);
$message = trim((string) $doc->body->div);
$this->assertEquals(
"This is a 'hello world' for my CD container.",
$message
);
}
protected function getWebPage()
{
$output = $return = null;
$command = sprintf(
'docker exec -ti %s php -r "echo file_get_contents(\'http://localhost\');"',
escapeshellarg($this->getRepoName())
);
exec($command, $output, $return);
// Check return value first
if ($return)
{
throw new RuntimeException(
sprintf(
'Returned a failure exit code %d on Docker operation',
$return
)
);
}
$imploded = implode(PHP_EOL, $output);
return $imploded;
}
我已經在 Alpine 3.8 中更新到 Apache 2.4.x,令我驚訝的是,我遇到了完全相同的問題。 因此,結果證明它不是 PHP Web 服務器。
我也試過 Alpine 3.7,同樣的結果。 我再次嘗試了 Ubuntu 18.10。
我試過php -r
與file_get_contents()
和wget
作為容器內的 HTTP 設備。
我現在認為從docker exec
捕獲標准輸出是錯誤的。 我假設如果我在另一個鏡像中設置我的測試並使用 Docker Compose 運行容器,它可能會工作。 或者,我可以使用docker exec
,寫入一個容器文件,然后使用docker cp
取出文件。
不過,知道出了什么問題仍然很有趣。
正如我在問題中推測的那樣,這是通過執行docker exec
並將標准輸出重定向到容器中的臨時文件來解決的。 然后我使用docker cp
將文件從容器中取出回主機。
我沒有發現是什么導致了奇怪的文本偽影,並想知道它是否可能特定於 CircleCI。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.