因此,我尝试使用服务和内容创建一个C++ Web服务器。它是alive here,这是在Linux上的普通用户下如何compile in in 3 lines以及here 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中注册用户(意味着发送正确的请求),但是当服务器尝试将其重定向到页面时,我希望它向我显示悲伤的page =(

因此,以图片为例:我们输入凭证:

我们点击提交...并永远被卡在连接中...:


当我们尝试仅使用that URL时,它会尝试转到它,我们看到它获得了“找到的响应”的一部分,但尚未重定向自身...(如果我们尝试使用chrome登录,我们将获得所有正确的信息,如果只需按照chrome中的网址进行操作,我们就会将其重定向到所需的位置,并显示以下图像:

我创建了一个简单的用户:带有传递[email protected]123456,因此您可以尝试...

那么重定向的方式有什么问题?为了使其适用于FF,应在响应中添加什么?

在一天结束时,我做了this:
   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);

    }

让我解释一下:当前的FF不喜欢我的302和303重定向...因此,简单的解决方案-将战斗转移到HTML的另一端-因此,我创建了简单的代码,该代码返回可以自动重定向用户或用户的HTML页面至少要给他提供正确的链接。经过测试-一切都按预期进行(带有短本地链接)。比我添加的解决方案不仅可以发送HTML页面,而且还可以使用重定位 header 。它可以在任何地方工作,并且如果浏览器很聪明,它将根本不会加载重定向页面的内容...)

所以现在所有的作品都可以了。

最佳答案

为了获得最佳的可移植性,位置 header 必须是绝对URL。看来您正在尝试发送相对的亲戚。

即:

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

代替:
Location: image.png

关于c++ - 我的重定向机制有什么问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8888557/

10-11 04:12