クラス用の HTTP サーバーの作成に取り組んでいます。しかし、返されたファイルは Firefox で表示されません。応答ヘッダーを作成し、送信するファイルの右側content-length
に配置してから、 への呼び出しで応答ヘッダーを送信しますsend()
。次にsend()
、ファイルを送信するためにもう一度使用しますが、Firefox でファイルが表示されないのはなぜですか? 以下に関連するコードを掲載します。
error_response
コードを見るとわかるように、404 エラーで ,を送信できません。ライブ http ヘッダーを使用すると、Firefox が正しい応答ヘッダーを受信し、正しい応答を出力することがわかりますcout << error_response << endl;
。Firefox で表示されないのはなぜですか? send()
ヘッダーと応答パケットを含む呼び出しを 1 回だけ行う必要がありますか?
// send response headers
response->Print( buffer, 2000 );
if ( send( acc_tcp_sock, buffer, sizeof(buffer), 0 ) < 0 ) {
cerr << "Unable to send response headers to client." << endl;
return 5;
}
// if 200 and GET then send file
if ( response->Get_code() == 200 && method == "GET" ) {
// send file
}
// close file, if it has been opened
if ( response->Get_code() == 200 ) {
fclose( returned_file );
}
else if ( method == "GET" ) {
if ( send( acc_tcp_sock, error_response.c_str(), error_response.length(), 0 ) < 0 ) {
cerr << "Unable to send data to client." << endl;
return 6;
}
cout << error_response << endl;
}
close( acc_tcp_sock ); // close the connection
}
return 0;
}