代码之家  ›  专栏  ›  技术社区  ›  halfer Jatin Pandey

PHP内部web服务器提供意外字符的原因是什么?

  •  0
  • halfer Jatin Pandey  · 技术社区  · 7 年前

    <!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的开头或结尾添加控制字符cruft,导致HTML解析失败。我想说这种情况有10%的时候会发生,所以很难复制。这似乎不是在当地发生的。

    在这个构建过程中,我正在等待web服务器启动,所以我认为这里不会发生竞争情况。

    如果在开始时发生积垢,看起来是这样的(我的输出交换车厢返回到 ⏎ 符号,这是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>
    

    如果最后出现积垢,它看起来是这样的:

    <!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

    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;
    }
    

    我也尝试了高山3.7,同样的结果再次。我试过ubuntu18.10,同样的。

    php -r 具有 file_get_contents() wget 作为容器内的HTTP设备。

    我现在想从 他有过错。我假设如果我在另一个映像中设置测试并使用Docker Compose运行容器,它可能会工作。或者,我可以用 ,写入容器文件,然后用 docker cp

    不过,知道出了什么问题还是很有意思的。

    1 回复  |  直到 7 年前
        1
  •  0
  •   halfer Jatin Pandey    7 年前

    正如我在问题中猜测的那样,这是通过做一个 docker exec 以及将标准输出重定向到容器中的临时文件。然后我用 docker cp 将文件从容器中取出并返回到主机。

    我没有发现是什么导致了这些奇怪的文字艺术品,不知道它是否是CircleCI特有的。

    推荐文章