开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 1 Fork 0

C++项目/servertech-chat

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (6)
master
feature/48-mysql-connection-pool
gh-pages
bugfix/52-volume-already-in-use
feature/6-simplify-deployment
async-rewrite
master
分支 (6)
master
feature/48-mysql-connection-pool
gh-pages
bugfix/52-volume-already-in-use
feature/6-simplify-deployment
async-rewrite
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (6)
master
feature/48-mysql-connection-pool
gh-pages
bugfix/52-volume-already-in-use
feature/6-simplify-deployment
async-rewrite
servertech-chat
/
server
/
src
/
http_session.cpp
servertech-chat
/
server
/
src
/
http_session.cpp
http_session.cpp 6.33 KB
一键复制 编辑 原始数据 按行查看 历史
Ruben Perez 提交于 2023年09月26日 23:21 +08:00 . Authentication, login and account creation
//
// Copyright (c) 2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "http_session.hpp"
#include <boost/asio/error.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/tcp_stream.hpp>
#include <boost/beast/http/message_generator.hpp>
#include <boost/beast/http/parser.hpp>
#include <boost/beast/http/read.hpp>
#include <boost/beast/http/status.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/beast/http/verb.hpp>
#include <boost/beast/websocket/error.hpp>
#include <boost/beast/websocket/rfc6455.hpp>
#include <boost/variant2/variant.hpp>
#include <cstddef>
#include <exception>
#include <string_view>
#include <utility>
#include "api/auth.hpp"
#include "api/chat_websocket.hpp"
#include "error.hpp"
#include "request_context.hpp"
#include "shared_state.hpp"
#include "static_files.hpp"
namespace beast = boost::beast;
namespace http = boost::beast::http;
using namespace chat;
static http::message_generator handle_http_request_impl(
request_context& ctx,
shared_state& st,
boost::asio::yield_context yield
)
{
// Attempt to parse the request target
auto ec = ctx.parse_request_target();
if (ec)
return ctx.response().bad_request_text("Invalid request target");
auto target = ctx.request_target();
auto segs = target.segments();
auto method = ctx.request_method();
if (!segs.empty() && segs.front() == "api")
{
// API endpoint. All endpoint handlers have the signature
// http::message_generator (request_context&, shared_state&, boost::asio::yield_context)
auto it = std::next(segs.begin());
auto seg = *it;
++it;
if (seg == "create-account" && it == segs.end())
{
if (method == http::verb::post)
return handle_create_account(ctx, st, yield);
else
return ctx.response().method_not_allowed();
}
else if (seg == "login" && it == segs.end())
{
if (method == http::verb::post)
return handle_login(ctx, st, yield);
else
return ctx.response().method_not_allowed();
}
else
{
return ctx.response().not_found_text();
}
}
else
{
// Static file
return handle_static_file(ctx, st);
}
}
static http::message_generator handle_http_request(
http::request<http::string_body>&& req,
shared_state& st,
boost::asio::yield_context yield
)
{
// Build a request context
request_context ctx(std::move(req));
// We don't communicate regular failures using exceptions, but
// unhandled exceptions shouldn't crash the server.
try
{
return handle_http_request_impl(ctx, st, yield);
}
catch (const std::exception& err)
{
return ctx.response().internal_server_error(errc::uncaught_exception, err.what());
}
}
void chat::run_http_session(
boost::asio::ip::tcp::socket&& socket,
std::shared_ptr<shared_state> state,
boost::asio::yield_context yield
)
{
error_code ec;
// A buffer to read incoming client requests
boost::beast::flat_buffer buff;
// A stream allows us to set quality-of-service parameters for the connection,
// like timeouts.
boost::beast::tcp_stream stream(std::move(socket));
while (true)
{
// Construct a new parser for each message
boost::beast::http::request_parser<boost::beast::http::string_body> parser;
// Apply a reasonable limit to the allowed size
// of the body in bytes to prevent abuse.
parser.body_limit(10000);
// Set the timeout.
stream.expires_after(std::chrono::seconds(30));
// Read a request
http::async_read(stream, buff, parser.get(), yield[ec]);
if (ec == http::error::end_of_stream)
{
// This means they closed the connection
stream.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
return;
}
else if (ec)
{
// An unknown error happened
return log_error(ec, "read");
}
// See if it is a WebSocket Upgrade
if (boost::beast::websocket::is_upgrade(parser.get()))
{
// Create a websocket, transferring ownership of the socket
// and the buffer (we're not using them again here)
websocket ws(stream.release_socket(), parser.release(), std::move(buff));
// Perform the session handshake
ec = ws.accept(yield);
if (ec)
return log_error(ec, "websocket accept");
// Run the websocket session. This will run until the client
// closes the connection or an error occurs.
// We don't use exceptions to communicate regular failures, but an
// unhandled exception in a websocket session shoudn't crash the server.
try
{
auto err = handle_chat_websocket(std::move(ws), state, yield);
if (err.ec && err.ec != boost::beast::websocket::error::closed)
log_error(err, "Running chat websocket session");
}
catch (const std::exception& err)
{
log_error(
errc::uncaught_exception,
"Uncaught exception while running websocket session",
err.what()
);
}
return;
}
// It's a regular HTTP request.
// Attempt to serve it and generate a response
http::message_generator msg = handle_http_request(parser.release(), *state, yield);
// Determine if we should close the connection
bool keep_alive = msg.keep_alive();
// Send the response
beast::async_write(stream, std::move(msg), yield[ec]);
if (ec)
return log_error(ec, "write");
// This means we should close the connection, usually because
// the response indicated the "Connection: close" semantic.
if (!keep_alive)
{
stream.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
return;
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

Web聊天室项目
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cppprojects/servertech-chat.git
git@gitee.com:cppprojects/servertech-chat.git
cppprojects
servertech-chat
servertech-chat
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /