C++ で記述された HTTP サーバーがあります。ある時点で、クライアントへの応答として送信する必要がある html ファイルを処理し、定義済みのラベルを他のラベルに置き換えたいと考えています。プロセスは次のようになります。
do {
size = read(page_fd, buffer, 1024);
/* processing buffer - replacing variables */
std::string tmp = std::string(buffer);
unsigned int start = 0, end;
do {
start = tmp.find("#{", start);
if (start != std::string::npos && start < tmp.length()) {
end = tmp.find("}", start + 1);
if (end != std::string::npos && end < tmp.length()) {
std::string tmp2 = tmp.substr(start + 2, end - start - 2);
tmp = tmp.replace(start, end - start + 1, params[tmp2.c_str()]);
start = end + 1;
}
}
} while (start != std::string::npos && start > end && start < tmp.length());
char buff[512];
memcpy(buff, tmp.c_str(), tmp.length());
std::cout << buff << "\n\n";
/* end of processing - writing to socket */
write(_conn_fd, buff, tmp.length());
} while (size > 0);
クライアントに送信したい html ページは次のとおりです。
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>It works!</h1>
<h2>#{custom}</h2>
<p>This page was served through a C++ HTTP server!</p>
</body>
</html>
クライアントが受信したものを確認すると、次のように html コードは常に不完全です。
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>It works!</h1>
<h2>replaced message</h2>
<p>This page was served through a C++ HTTP server!</p>
コード内のstd::cout
行は、正しい html 文字列を出力します。
クライアントが完全な html を受信しないのはなぜですか、または完全に受信したとしても、それがブラウザーから見えないのはなぜですか?