簡體   English   中英

如何使用Beast C ++庫從HTTP重定向到HTTPS?

[英]How to redirect from HTTP to HTTPS with Beast C++ library?

我正在研究Boost.Beast庫。 我嘗試發出一個請求,其響應是:

HTTP/1.1 301 Moved Permanently
Cache-Control: public
Content-Type: text/html; charset=UTF-8
Location: https://www.example.com/target/xxx/

然后,我嘗試使用此位置字段發出請求,但收到錯誤的請求響應。

我該怎么做重定向? 有一個例子嗎?

這是我的代碼:

boost::asio::io_service ios;
tcp::resolver resolver{ios};
tcp::socket socket{ios};
auto const lookup = resolver.resolve( tcp::resolver::query(host, port) );
boost::asio::connect(socket, lookup);

// Set up an HTTP GET request message
http::request<http::string_body> req{http::verb::get, target, 11};
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

// Send the HTTP request to the remote host
http::write(socket, req);

// This buffer is used for reading and must be persisted
boost::beast::flat_buffer buffer;

// Declare a container to hold the response
http::response<http::dynamic_body> res;

// Receive the HTTP response
http::read(socket, buffer, res);

if( res.base().result_int() == 301 ) {
   req.set(http::field::location, res.base()["Location"]);
   http::write(socket, req);
   boost::beast::flat_buffer buffer1;
   http::read(socket, buffer1, res);
}
std::cout << req << std::endl;
std::cout << res << std::endl;

謝謝

重定向時,您不能只“替換”現有請求中的某個位置。 除非在重定向目標位於同一TCP端點的極少數情況下,否則您甚至無法使用相同的套接字。

由於主機名,協議和路徑可能已更改,因此您必須解析位置,獲取方案,主機,路徑部分。 然后,您必須再次進行適當的主機解析,並確保在Host標頭中使用正確的主機名。

這是一個示例,顯示在“錯誤”網址http://boost.org/user/license.html請求Boost許可證,該網址將立即重定向到http://www.boost.org/user/license.html

注意我已經使用network::uri為我們做URI解析: https//github.com/reBass/uri

演示

#include <iostream>
#include <boost/beast.hpp>
#include <boost/beast/http.hpp>
#include <network/uri.hpp>
#include <boost/asio.hpp>
#include <string>

using boost::asio::ip::tcp;
namespace http = boost::beast::http;

struct Requester {
    void do_request(std::string const& url) {
        network::uri u{url};
        auto const lookup = resolver_.resolve( tcp::resolver::query(u.host().to_string(), u.scheme().to_string()) );

        // Set up an HTTP GET request message
        tcp::socket socket{ios};
        boost::asio::connect(socket, lookup);

        http::request<http::string_body> req{http::verb::get, u.path().to_string(), 11};
        req.keep_alive(true);

        req.set(http::field::host, u.host().to_string());
        req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

        std::cout << "Target: " << url << "\n";
        std::cout << req << "\n";

        http::write(socket, req);
        boost::beast::flat_buffer buffer;
        http::response<http::dynamic_body> res;
        http::read(socket, buffer, res);

        switch(res.base().result_int()) {
            case 301: 
                std::cout << "Redirecting.....\n";
                do_request(res.base()["Location"].to_string());
                break;
            case 200:
                std::cout << res << "\n";
                break;
            default:
                std::cout << "Unexpected HTTP status " << res.result_int() << "\n";
                break;
        }
    }
  private:
    boost::asio::io_service ios;
    tcp::resolver resolver_{ios};
};

int main() {
    try {
        Requester requester;
        requester.do_request("http://boost.org/users/license.html"); // redirects to http://www.boost.org/...
    } catch(std::exception const& e) {
        std::cerr << "Exception: " << e.what() << "\n";
    }
}

這打印:

Target: http://boost.org/users/license.html
GET /users/license.html HTTP/1.1
Host: boost.org
User-Agent: Boost.Beast/109


Redirecting.....
Target: http://www.boost.org/users/license.html
GET /users/license.html HTTP/1.1
Host: www.boost.org
User-Agent: Boost.Beast/109


HTTP/1.1 200 OK
Date: Sun, 27 Aug 2017 22:25:20 GMT
Server: Apache/2.2.15 (CentOS)
Accept-Ranges: bytes
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

90fd
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
  <title>Boost Software License</title>
  <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
  <link rel="icon" href="/favicon.ico" type="image/ico" />
  <link rel="stylesheet" type="text/css" href="../style-v2/section-boost.css" />
  <!--[if IE 7]> <style type="text/css"> body { behavior: url(/style-v2/csshover3.htc); } </style> <![endif]-->
</head><!--
Note: Editing website content is documented at:
http://www.boost.org/development/website_updating.html
-->

<body>
    ENTIRE LICENSE BODY SNIPPED
</body>
</html>

0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM