c++ - 我的重定向机制有什么问题?

标签 c++ http firefox redirect

因此,我尝试创建一个包含服务和内容的 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 允许我在数据库中注册用户(意思是发送正确的请求),但是当服务器尝试将其重定向到页面时我希望它显示悲伤的页面=(

以图片为例:我们输入凭据: enter image description here

我们点击提交...并且永远卡在连接中...: enter image description here

当我们尝试只使用 that URL 时,它会尝试去我们看到它得到了“找到的响应”的一部分,但没有重定向自己......(如果我们尝试使用 chrome 登录,我们会得到所有正确的,也如果我们只是在 chrome 中跟随那个 url,我们会被重定向到需要的地方并看到这样的图像: enter image description here

我创建了一个简单的用户:demo_user@gmail.com,通行证为 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 。这应该在任何地方都有效,如果浏览器是智能的,它根本不会加载重定向页面的内容......)

所以现在一切正常。=)感谢 KayEss 的回答,我现在将所有链接设为绝对链接......为什么不呢?)

最佳答案

为了获得最佳的可移植性,位置 header 需要是绝对 URL。看起来你正在尝试发送一个亲戚。

即:

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

代替:

Location: image.png

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

相关文章:

http - 如何在nifi中使用invoke http来执行GET请求?

html - Firefox 49.0.1 需要旧的 FireBug

ios - 在 iOS 中监听 HTTP 请求

django - Access-Control-Allow-Headers 不允许 Access-Control-Allow-Origin

javascript - nsISocketTransportService 使用 Firefox 插件 sdk

python / Selenium /火狐 : Can't start firefox with specified profile path

c# - 在编程语言中扩展预增量运算符

c++ - 快速排序错误 : stack around the variable was corrupted?

c++ - 为什么要在 C++ 中按值传递对象

c++ - 配置比较函数以使用 std::sort