C ++ SSL / HTTPS 301重定向循环

时间:2015-10-06 20:14:55

标签: c++ loops ssl

试图缩短这一点。

我正在尝试编写一个可以与加密连接交互的小工具(使用SSL的HTTPS - > OpenSSL)。我在Mac上这样做。

这就是我的联系方式:

BIO * SSL_Connect(std::string host_IPv4, std::string truststore){

SSL_load_error_strings();
ERR_load_BIO_strings();
ERR_load_crypto_strings();

SSL * ssl;
BIO * bio;
SSL_CTX * ctx = SSL_CTX_new(SSLv23_client_method());

std::string host = host_IPv4 + ":443";

if(ctx == NULL){

    //std::cout << ERR_reason_error_string(ERR_get_error()) << std::endl;

}

int load_error = SSL_CTX_load_verify_locations(ctx, truststore.c_str(), NULL);

if(load_error == 0){

    //std::cout << "ERROR loading TrustedStore" << std::endl;

}
else{

    bio = BIO_new_ssl_connect(ctx);
    BIO_get_ssl(bio, &ssl);
    SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);

    BIO_set_conn_hostname(bio, host.c_str());

    if(BIO_do_connect(bio) <= 0){

        //std::cout << "Connection failed!" << std::endl;

    }
    else{

        if(SSL_get_verify_result(ssl) != X509_V_OK){

            //std::cout << "Fatal Cert Error!" << std::endl;

        }
        else{

            //std::cout << "SSL Connection established!!!" << std::endl;

        }

    }

}

return bio;

}

我的代码确实有效,我的问题如下:

我尝试连接到例如:https://en.wikipedia.org/wiki/something 然后我得到了一个永久移动的HTTP / 1.1 301&#39;来自服务器的响应指向https://en.wikipedia.org/wiki/something(完全相同的URL)

这种情况会持续下去..有人可以解释为什么会这样吗?我似乎无法在谷歌上找到类似的东西:/

如果您需要其他信息,我可以提供。

HTTP代码:

http_get_header create_http_get_header(std::string url, std::string cookie, int mode){

http_get_header header;

std::vector<std::string> UA = populate_user_agents();

int random_value;

//parse url, need format: www.xxxx.yyy/zzzzzz
if(url.substr(0, 5) == "https"){ //https://www.google.com / https://google.com

    if(url.substr(0, 11) == "https://www"){ //https://www.google.de

        url = url.substr(8, url.length());

    }
    else{ //https://google.com

        url = url.substr(8, url.length());
        url = "www." + url;

    }

}
else if(url.substr(0, 5) == "http:"){ //http://www.google.com / http://google.com

    if(url.substr(0, 10) == "http://www"){ //http://www.google.com

        url = url.substr(7, url.length());

    }
    else{ //http://google.com

        url = url.substr(7, url.length());
        url = "www." + url;

    }

}
else if(url.substr(0, 4) == "www."){ //www.google.com

    //nothing todo

}
else{ //google.com

    url = "www." + url;

}

//check for at least one '/', if none (www.google.com) append one
if(!(url.find("/")<1800000)){

    url = url + "/";

}

//check if we can last the www. at the beginning
if(mode == 1){

    url = url.substr(4, url.length());

}

header.Host = "Host: " + url.substr(0, url.find("/"));
header.GET = "GET " + url.substr(url.find("/"), url.length()) + " HTTP/1.1";

srand(time(NULL));
random_value = rand() % 16 + 1;

header.UserAgent = "User-Agent: " + UA[random_value-1];
header.Acc = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
header.AccLang = "Accept-Language: de,en-US;q=0.7,en;q=0.3";
header.AccEncoding = "";

//Check for Cookie
if(cookie == ""){

    header.Cookie = "";

}
else{

    header.Cookie = "Cookie: " + cookie;

}

header.Connection = "Connection: close";

return header;

}

这就是我如何创建HTTP标头..我不会重试获取新网址指向&#39;位置:&#39;,我只是看到响应指向完全相同的网址并且想知道为什么..

我要求:

https://de.wikipedia.org/wiki/Kuchen

响应:

HTTP/1.1 301 Moved Permanently
Location: https://de.wikipedia.org/wiki/Kuchen

它是一样的,不是拼写的东西..

2 个答案:

答案 0 :(得分:0)

  

我尝试连接到例如:https://en.wikipedia.org/wiki/something然后我从服务器指向https://en.wikipedia.org/wiki/something(完全相同的URL)获得“HTTP / 1.1 301永久移动”响应

它不是相同的网址。在重定向期间,请仔细查看Location标题。

https://en.wikipedia.org/wiki/something的请求被重定向到https://en.wikipedia.org/wiki/Something。注意案例的区别。对https://en.wikipedia.org/wiki/Something的请求会返回实际内容,而不是其他重定向。

因此,如果您陷入重定向循环,则不会正确请求重定向的URL。但是你没有显示你的HTTP代码。

更新:正如您所说,您没有正确设置Host标头。原因是因为您没有正确解析URL的主机名。特别是,当您不应该添加/删除www时。这是主机名的一部分,对于正确的DNS查找和Host标头分配非常重要。

尝试更类似的内容(有关URL语法的正式定义,请参阅RFC 3986):

http_get_header create_http_get_header(std::string url, std::string cookie, int mode)
{
    http_get_header header;

    std::vector<std::string> UA = populate_user_agents();

    int random_value;

    //parse url

    //std::string scheme;
    std::string host;
    std::string port;
    std::string path;
    std::string query;
    //std::string fragment;

    std::string::size_type n = url.find(":");
    if (n != std::string::npos)
    {
        //scheme = url.substr(0, n);
        url = url.substr(n+1);
    }

    if (url.substr(0, 2) == "//")
    {
        n = url.find_first_of("/?#", 2);
        if (n != std::string::npos)
        {
            host = url.substr(2, n-2);

            if (url[n] == '/')
            {
                path = url.substr(n);

                n = path.find_first_of("?#");
                if (n != std::string::npos)
                {
                    std::string tmp = path.substr(n);
                    path = path.substr(0, n-1);

                    if (tmp[0] == '?')
                    {
                        query = tmp.substr(1);

                        n = query.find("#");
                        if (n != std::string::npos)
                        {
                            //fragment = query.substr(n+1);
                            query = query.substr(0, n-1);
                        }
                    }
                    /*else
                        fragment = tmp.substr(1);*/
                }
            }
            else if (url[n] == '?')
            {
                query = url.substr(n+1);

                n = query.find("#");
                if (n != std::string::npos)
                {
                    //fragment = query.substr(n+1);
                    query = query.substr(0, n-1);
                }
            }
            /*else
                fragment = url.substr(n+1);*/
        }
        else
            host = url;

        n = host.rfind("@");
        if (n != std::string::npos)
            host = host.substr(n+1);

        n = host.find(":");
        if (n != std::string::npos)
        {
            port = host.substr(n+1);
            host = host.substr(0, n-1);
        }
    }

    if (path == "")
        path = "/";

    header.Host = "Host: " + host;
    if (port != "") header.Host += (":" + port);

    header.GET = "GET " + path;
    if (query != "") header.GET += ("?" + query);
    header.GET += " HTTP/1.1";

    srand(time(NULL));
    random_value = rand() % 16 + 1;

    header.UserAgent = "User-Agent: " + UA[random_value-1];
    header.Acc = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    header.AccLang = "Accept-Language: de,en-US;q=0.7,en;q=0.3";
    header.AccEncoding = "";

    //Check for Cookie
    if (cookie != "")
        header.Cookie = "Cookie: " + cookie;
    else
        header.Cookie = "";

    header.Connection = "Connection: close";

    return header;
}

答案 1 :(得分:0)

遇到问题..真的很有线。这是我的GET Header字段'Host:'是:

  

主持人:www.de.wikipedia.org

但必须是:

  

主持人:de.wikipedia.org

解决了它......但感谢你们所有人!