2012-01-17 4 views
3

私はサービスやものでC++ Webサーバーを作成しようとします。これはalive hereです。これはLinuxの正規ユーザの場合はcompile in in 3 lineshere is it svnの場合です。私のリダイレクト・マカニズムで何が問題になっていますか?

void http_utils::send_found_302(std::string redirect_lication, boost::shared_ptr<boost::asio::ip::tcp::socket> socket, boost::shared_ptr<http_response> response) 
{ 
    response->status = 302; 
    response->description = "Found"; 
    response->headers.insert(std::pair<std::string, std::string>("Location", redirect_lication)); 
    response->send(*socket); 
} 

そしてChromeとSafaryでとIEで:私はそのような機能を使用するユーザーをリダイレクトする

。私は自分のサーバーに登録してログインすることができます。しかし、FF ...私はそれが私の悲しいページを表示したい場合は、FFの私はDBのユーザーを登録することができます(正しい要求を送信する意味)が、私はそれが私の悲しいページを表示したいページをリダイレクトしようとする=(

私は信じて: enter image description here

私たちは、提出にヒット...そして...これまでの接続で立ち往生されています。 enter image description here

我々だけthat URLを使用しようとすると、それは我々はそれが一部を得たことを見に行くことにしよう「見つかったレスポンス」のうち、リダイレクトされていない...(もし私たちがクロムでログインしようとすれば、クロームで、私たちは必要な場所にリダイレクトして、そのような画像を参照してくださいになるだろう: enter image description here

私はシンプルなユーザー作成:だから何が道と間違っているパス123456[email protected]ので、あなたはそれを試してみることができます...

を私はリダイレクトしますか?それはFFのために働くために応答に何が加えられるでしょうか?

void http_utils::send_found_302(const std::string & redirect_lication, boost::shared_ptr<boost::asio::ip::tcp::socket> socket, boost::shared_ptr<http_response> response) 
    { 
      /* if there was no Fire Fox and probably other dull browsers we would do this (Chrome, IE, Safari tested): 
      * 
      *\code 
        response->status = 302; 
        response->description = "Found"; 
        response->headers.insert(std::pair<std::string, std::string>("Location", redirect_lication)); 
        response->send(*socket); 
      * \endcode 
      * 
      * but indeed there are. 
      * 
      * We could also create simple HTML redirection page - would work anywhere. 
      * \code 
      std::ostringstream data_stream; 
      data_stream << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head><script type=\"text/javascript\">location.replace(\"" 
        << redirect_lication << "\");</script><noscript><meta http-equiv=\"refresh\" content=\"0; url= " 
        << redirect_lication << "\"></noscript></head><body><p>Please turn on JavaScript!</p><a href=\"" 
        << redirect_lication << "\"><h1>Content awaits you here.</h1></a></body></html>"; 
      response->headers.insert(std::pair<std::string, std::string>("Cache-Control", "max-age=0")); 
      response->headers.insert(std::pair<std::string, std::string>("Pragma", "no-cache")); 
      http_utils::send(data_stream.str(), socket, response); 
      * \endcode 
      * 
      * so we decided to mix - html page and HTTP redirection 
      */ 

      response->description = "Found"; 
      response->headers.insert(std::pair<std::string, std::string>("Location", redirect_lication)); 
      response->headers.insert(std::pair<std::string, std::string>("Content-Location", redirect_lication)); 
      response->headers.insert(std::pair<std::string, std::string>("Refresh", std::string("0; url=" + redirect_lication))); 
      response->headers.insert(std::pair<std::string, std::string>("Cache-Control", "max-age=0")); 
      response->headers.insert(std::pair<std::string, std::string>("Pragma", "no-cache")); 
      std::ostringstream data_stream; 
      data_stream << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head><script type=\"text/javascript\">location.replace(\"" 
        << redirect_lication << "\");</script><noscript><meta http-equiv=\"refresh\" content=\"0; url= " 
        << redirect_lication << "\"></noscript></head><body><p>Please turn on JavaScript!</p><a href=\"" 
        << redirect_lication << "\"><h1>Content awaits you here.</h1></a></body></html>"; 

      http_utils::send(302, data_stream.str(), socket, response); 

    } 

私に説明してみましょう:私はthisをした一日の終わりに


現在のFFは私の302と303リダイレクトを好きではなかった...ので、簡単な解決策は - 戦いを移動することでしたHTMLの反対側に移動するので、ユーザーを自動リダイレクトするか、少なくとも正しいリンクを提示するHTMLページを返す簡単なコードを作成しました。それをテストしました。(短くローカルなリンクを使って)すべてがうまくいきました。私はHTMLページを送信するだけでなく、再配置ヘッダーも送信するソリューションを追加しました。これはどこでも動作し、ブラウザがスマートならリダイレクションページの内容を読み込まない...)

KayEssお返事ありがとうございます。 )

+0

残念ながら、より多くのコードなしで答えるのは難しいだろう。しかし、単純なルールは、ヘッダが何よりも先に送られなければならず、 'Location'ヘッダを複製することは無視されるかもしれない、つまり、2番目の' Location'が無視されるかもしれないということです。 – hiobs

+0

非関連のアドバイス:std :: stringと(とりわけ)std :: shared_ptrを値渡ししないでください。可能であればconst参照でそれらを渡してください。値でshared_ptrを渡すと、同期操作を実行して参照カウントをインクリメントする必要があります。(これはパフォーマンス重視の方法だと思います)。 –

答えて

1

最高の移植性のために、ロケーションヘッダーは絶対URLである必要があります。それはあなたが相対的なものを送信しようとしているように見えます。

すなわち:

Location: http://example.com/path/image.png 

の代わりに:

Location: image.png 
関連する問題