- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Boost Asio https synchronous call- Error code 400 bad request 中所述的 boost asio 进行 https 同步调用现在我们得到了响应代码,但是在从套接字读取它时,我收到了短读错误:
boost::asio::write(socket, request_);
boost::asio::read_until(socket, response_, "\r\n");
string res=make_string(response_);
// Check that response is OK.
std::istream response_stream(&response_);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
if (!response_stream || http_version.substr(0, 5) != "HTTP/")
{
PBLOG_WARN("Invalid response\n");
}
if (status_code != 200)
{
fast_ostringstream oss;
oss << "Response returned with status code: " << status_code << "\n";
PBLOG_WARN(oss.str());
}
boost::asio::read(socket, response_, boost::asio::transfer_all(), error);
if (error != boost::asio::error::eof)
{
fast_ostringstream oss;
oss << "Error : " << error.message();
PBLOG_WARN(oss.str());
return false;
}
else
{
//parse the original resposne
}
在上述逻辑中,它进入 if 循环并将错误作为 Error : short read。请帮我解决这个问题。
最佳答案
正如我在 the other answer 中解释的那样,当服务器通过简单地关闭连接来实现关闭时,可以预期短读(“stream_truncaed”)。在这种情况下,缺少常规 SSL 关闭,提示“短读”。
我链接回那里的问题描述了如何在 HTTP 响应解析的上下文中处理错误 - 并得出结论,当响应已被完全解析时,忽略它是安全的。
在您的情况下,您没有明确解析响应,因此您无法真正做出明智的决定。这使您容易受到潜在的滥用,您可能会被攻击者接受格式错误的请求。
1.累了(解决方法)
我们天真的解决方法是:
Live On Coliru
std::istream response_stream(&response_);
if (std::getline(response_stream >> http_version >> status_code, status_message)
&& http_version.substr(0, 5) == "HTTP/")
{
if (status_code != 200) {
std::clog << "Response returned with status code: " << status_code << "\n";
}
boost::system::error_code error;
boost::asio::read(socket, response_, boost::asio::transfer_all(), error);
if (error != boost::asio::error::eof &&
error != boost::asio::ssl::error::stream_truncated)
{
std::clog << "Error : " << error.message() << std::endl;
return 1;
}
else
{
//parse the original resposne
}
} else {
std::clog << "Invalid response\n" << std::endl;
}
打印
Payload:
----
<!DOCTYPE html>
<html>
<title>Coliru Viewer</title>
<head>
....
....
</body>
</html>
----
2.连线:正确执行
EOF
(这可能永远不会出现?)并摸不着头脑,为什么会导致短读。
Note: this method also makes a lot of other things less error-prone and way less insecure. Things like encoding header values (which you don't), proper use of CRLF (which was an error in your previous question), correct calculation of content length etc.
On the response parsing you - randomly - check that HTTP version starts with "HTTP/". However, using Beast you will actually check input sanity and change behaviour accordingly. This means that if your server does something you didn't expect (like, you know, actually using HTTP/1.1 features like chunked encoding) you will still read the response, as if you totally knew about that.
Conversely, if someone attacks your server by sending invalid responses, you have a better chance of not crashing or worse.
#include <boost/asio.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/asio/ssl.hpp>
#include <iostream>
#include <iomanip>
namespace http = boost::beast::http;
using boost::asio::ip::tcp;
// https://postman-echo.com/post see https://docs.postman-echo.com/?version=latest
static std::string
verb = "POST",
server_endpoint = "/post",
hostname = "postman-echo.com",
port_no = "443",
authorization_token = "c3RhdGljIGNvbnN0IHN0ZDo6c3RyaW5nIGF1dGhvcml6YXRpb"
"25fdG9rZW4gPSAiQXV0aDogIj"
"sK",
client_name = "demo program 0.01",
req_str = R"(name=blabla&password=bloblo)";
int main() {
boost::asio::io_service io_service;
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
ctx.set_verify_mode(boost::asio::ssl::verify_peer);
//ctx.load_verify_file("ca.pem");
ctx.add_verify_path("/etc/ssl/certs");
ctx.set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::no_sslv3);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>
socket(io_service, ctx);
{
#ifdef COLIRU
verb = "GET";
server_endpoint = "/a/cf2748285fa3343a";
hostname = "coliru.stacked-crooked.com";
socket.set_verify_mode(boost::asio::ssl::verify_none);
socket.lowest_layer().connect({ boost::asio::ip::address::from_string("173.203.57.63"), 443 });
#else
tcp::resolver resolver(io_service);
tcp::resolver::query query(hostname, port_no);
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
boost::system::error_code error = boost::asio::error::host_not_found;
boost::asio::connect(socket.lowest_layer(), endpoint_iterator, error);
#endif
}
{
socket.handshake(boost::asio::ssl::stream_base::client);
using http::field;
http::request<http::string_body> request;
request.method_string(::verb);
request.target(server_endpoint);
request.version(11);
request.set(field::host, hostname);
request.set(field::accept, "*/*");
request.set(field::authorization, authorization_token);
request.set(field::user_agent, client_name);
request.set(field::content_type, "application/x-www-form-urlencoded");
request.set(field::connection, field::close);
request.body() = req_str;
request.prepare_payload();
write(socket, request);
// std::clog << request << "\n"; return 1;
}
{
boost::system::error_code ec;
using http::field;
http::response<http::string_body> response_;
boost::beast::flat_buffer buf;
read(socket, buf, response_, ec);
{
//std::clog << "Response: " << response_ << std::endl;
}
// Check that response is OK.
if (!ec && response_.version() == 11) {
if (response_.result() != http::status::ok) {
std::clog << "Response returned with status code: " << response_.result_int() << "\n";
}
std::clog << "Payload:\n----\n" << response_.body() << "\n----" << std::endl;
} else {
std::clog << "Error: " << ec.message() << "\n";
}
}
}
概括
关于c++ - 短读错误-Boost asio同步https调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63376765/
发起 HTTPS 对话时,会生成一个随机数来为交换创建 key (或类似的东西)。我不明白这是如何防止重放攻击的。 为什么攻击者不能重复真实客户端发出的所有请求? This answer claims
要使这个简单的 HTTP header 与支持 HTTPS 的服务器通信,还需要进行哪些其他更改。 GET /index.php HTTP/1.1 Host: localhost [CR] [CR]
我想弄清楚 HTTPS 是有状态还是无状态?这是关于我构建的 RESTful API。我们最初使用 HTTP。由于 HTTP 本质上是在无状态的 TCP/IP 上工作的,因此 HTTP 是无状态的,但
我从各种来源了解到,HTTPS 握手是使用 HTTPS 中最重要的部分。我在服务器之间内部使用 POST 来传达信息,并希望为此使用 HTTPS。我想知道实际的 HTTPS 握手持续多长时间/“保持打
我想知道HTTPS是如何实现的。是数据加密还是路径加密(数据通过哪个路径)。如果有人向我提供实现细节,我将不胜感激。 最佳答案 很简单,HTTPS 使用安全套接字层来加密客户端和服务器之间传输的数据。
我是 HTTPS 技术的初学者:(。我对 HTTPS 的实现有一些疑问。 假设我有一张注册表 http://www.sitename.com/register.php 如果我想在 HTTPS 中使用它
在带有 Devise 1.51 的 Rails 3.1.1 应用程序中,我希望确认消息中使用的确认链接是 https 而不是 http。因此,在电子邮件中,“确认链接”会指向如下内容: https:/
我对 HTTPS 有疑问。我的一位前辈告诉我,Https 实际上并不使用 SSL/TLS,而只是使用它们的加密算法。他说,证书的握手过程是在传输层完成的,但实际有效负载的安全 key 加密是在应用层完
我建立了一个使用 PHP mail() 的网站。如果我在 http://上点击脚本,我可以让它成功运行,但如果我切换到 https://它就不起作用了!我使用 Godaddy 进行托管,并通过他们购买
我最近更改了域并设置了来自 https://sadlergatestoyou.co.uk 的重定向至https://www.sadlergates.co.uk但是,www.sadlergatestoy
我正在制作一个依赖于设置 http.proxyPort 和 http.proxyHost 的 Java 应用程序。有两个进程:一个是正则程序,一个是代理程序。我有一个在 http.proxyPort(
我正在开发一个 RESTful 应用程序,为此我需要将从 http 地址传入的请求重定向到它的 https 等效项。我似乎无法使用 ring/compojure 启用 https。 有人有一些有用的教
我看过很多关于重写的文章。都好。但没有一个涵盖这种具体情况。所以这是我的问题:希望你能帮忙。因为我无法让它发挥作用。 我们在domain.com(非www)上运行网站 我们已设置 ssl(因此仅限 h
我需要将大量请求自动提交到基于云的数据库接口(interface) (Intelex)。没有任何方法可以批量提交某些操作,但是提交单个请求所必需的只是让经过身份验证的用户尝试打开 Web 链接。因此,
我正在使用 https 设置一个独立的(非嵌入式) jetty 9.2.1。 我在本地机器上使用自签名证书玩了一会儿,一切顺利。 现在我正在设置一个 uat 服务器(类似于我将在生产中获得的服务器),
我对 Web 开发(从今年 1 月开始)和 Web 安全(在不到一周前开始!)都是新手,所以如果我的问题完全没有受过教育、误导或简单愚蠢,请原谅我。 我工作的公司的主要产品是一个很好的老式客户端/服务
HTTPS头是否加密到什么程度(如果有的话)? 最佳答案 它们在通过SSL传输时被加密。没有专门用于 header 的特殊加密,HTTPS对整个消息进行加密。 关于https - HTTPS head
在 HTTPS 安全模型中,最薄弱的部分是浏览器中的可信 CA 列表。有人可以通过多种方式将额外的 CA 添加到用户信任错误的人的列表中。 例如,您公司的公用计算机或 PC。管理员可能会强制您信任自己
我们最近切换到 HTTPS,当提交我们的一个表单时,Firefox 会弹出: Although this page is encrypted, the information you have ent
我知道没有愚蠢的问题,但这是:您能否在完全支持 https 的网站上通过 AdSense 或其他方式转换基于上下文的广告? 最佳答案 更新: We’ve updated the AdSense ad
我是一名优秀的程序员,十分优秀!