質問

クラスのHTTPサーバーの作成に取り組んでいます。ただし、返されたファイルはFirefoxで表示されていません。応答ヘッダーを作成し、送信するファイルの右側にcontent-lengthを挿入し、send()を呼び出して応答ヘッダーを送信します。次にerror_responseを再度使用してファイルを送信しますが、Firefoxがファイルを表示しないのはなぜですか?以下に関連するコードを投稿します。

コードを見るとわかるように、404エラー中にcout << error_response << endl;を送信できません。ライブHTTPヘッダーを使用すると、Firefoxが正しい応答ヘッダーを受信し、<=>が正しい応答を出力することがわかります。 Firefoxで表示されないのはなぜですか?ヘッダーと応答パケットを含む<=>を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;
}
役に立ちましたか?

解決

同じsendコマンドで応答ヘッダーを送信しようとしましたか? firefoxが受信しているものを確認できる方法はありますか。 HTTPRequesterクラスを使用して、サーバーにHTTPリクエストを送信するコンソールアプリを作成し、その結果を確認することができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top