增加最大上传大小限制

This commit is contained in:
xx
2024-06-16 13:38:01 +08:00
parent 4542dc5679
commit 282fbd9a99
6 changed files with 107 additions and 94 deletions

View File

@@ -161,11 +161,21 @@ namespace ylib
cdn_config cdn;
// 代理
std::vector<proxy_config> proxy;
// GZIP
bool gzip = false;
};
/// <summary>
/// HTTP服务配置
/// </summary>
struct server_config {
// 最大上传大小限制
uint64 max_upload_size = 0;
// Https
bool https = false;
// 端口
ushort port = 0;
};
/// <summary>
/// 启动信息
/// </summary>
@@ -174,7 +184,10 @@ namespace ylib
std::vector<website_config> website;
// 证书
std::map<std::string, ssl_config> cert;
// 最大上传大小限制
uint64 max_upload_size = 0;
};
class agent;
class website;
// 临时接收

View File

@@ -24,7 +24,7 @@ namespace ylib
public:
server();
~server();
bool create(bool https, ushort port);
bool create(server_config config);
/******************************************************************
* Function启动
* Param
@@ -53,12 +53,17 @@ namespace ylib
* Function每秒请求数
******************************************************************/
network::qps* qps();
inline const ushort port() { return m_port; }
/// <summary>
/// 取配置
/// </summary>
/// <returns></returns>
const server_config& config() { return m_config; }
#if USE_NET_HTTP_AGENT == 1
inline network::http::agent* agent() { return m_agent; }
#endif
public:
bool m_init_ssl;
bool m_init_ssl = false;
private:
friend class http_server_lst;
private:
@@ -66,10 +71,8 @@ namespace ylib
void* m_server;
// HP Listener 指针
network::http::http_server_lst* m_listener;
// https
bool m_https;
// 端口
ushort m_port;
// 配置
server_config m_config;
// QPS
network::qps m_qps;
#if USE_NET_HTTP_AGENT == 1

View File

@@ -46,7 +46,11 @@ bool ylib::network::http::center::create(const start_config& config)
//std::cout<<"ListenPort:"<<ports[i]<<std::endl;
auto server = new network::http::server;
server->center(this);
server->create(port_have_ssl(ports[i]),ports[i]);
server_config sc;
sc.https = port_have_ssl(ports[i]);
sc.port = ports[i];
sc.max_upload_size = m_config.max_upload_size;
server->create(sc);
m_server.push_back(server);
}
}
@@ -75,7 +79,7 @@ bool ylib::network::http::center::start()
{
if (m_server[i]->start() == false)
{
m_lastErrorDesc = "listen "+std::to_string(m_server[i]->port()) + " failed." + m_server[i]->last_error();
m_lastErrorDesc = "listen "+std::to_string(m_server[i]->config().port) + " failed." + m_server[i]->last_error();
return false;
}
}
@@ -115,7 +119,7 @@ ylib::network::http::server* ylib::network::http::center::server(ushort port)
for (size_t i = 0; i < m_server.size(); i++)
{
if (m_server[i]->port() == port)
if (m_server[i]->config().port == port)
{
return m_server[i];
}

View File

@@ -35,8 +35,6 @@ ylib::network::http::server::server()
{
m_server = nullptr;
m_listener = nullptr;
m_https = false;
m_port = 0;
m_init_ssl = false;
#if USE_NET_HTTP_AGENT == 1
@@ -55,7 +53,7 @@ bool ylib::network::http::server::start()
//HPSERVER->SetAcceptSocketCount(10000);
//HPSERVER->SetMaxConnectionCount(100000);
HPSERVER->SetWorkerThreadCount(5);
if (HPSERVER->Start("0.0.0.0", m_port) == false)
if (HPSERVER->Start("0.0.0.0", m_config.port) == false)
{
#ifndef _WIN32
this->m_lastErrorDesc = "start failed, error code:" + std::string(SYS_GetLastErrorStr());
@@ -75,13 +73,12 @@ bool ylib::network::http::server::start()
return true;
}
bool ylib::network::http::server::create(bool https, ushort port)
bool ylib::network::http::server::create(server_config config)
{
m_listener = new http_server_lst(this);
m_listener->m_server = this;
m_https = https;
m_port = port;
if (m_https)
m_config = config;
if (m_config.https)
{
m_server = HP_Create_HttpsServer(m_listener);
}
@@ -89,6 +86,7 @@ bool ylib::network::http::server::create(bool https, ushort port)
{
m_server = HP_Create_HttpServer(m_listener);
}
return true;
}
@@ -113,7 +111,7 @@ bool ylib::network::http::server::close()
}
HPSERVER->Wait(-1);
//销毁释放
if(m_https)
if(m_config.https)
HP_Destroy_HttpsServer(HPSERVER);
else
HP_Destroy_HttpServer(HPSERVER);

View File

@@ -80,7 +80,19 @@ EnHandleResult ylib::network::http::http_server_lst::OnReceive(ITcpServer* pSend
#if HTTP_SERVER_DEBUG_PRINT == 1
ylib::log->info("OnReceive ("+std::to_string((uint64)dwConnID)+")","http_server");
#endif
//if (m_server->config().max_upload_size > 0)
//{
// PVOID extra = 0;
// if (pSender->GetConnectionExtra(dwConnID, &extra))
// {
// if (extra != 0)
// {
// temp_recv* tr = (temp_recv*)extra;
// tr
// }
// }
//}
return HR_OK;
}
@@ -235,6 +247,15 @@ EnHttpParseResult ylib::network::http::http_server_lst::OnBody(IHttpServer* pSen
{
if (extra != 0)
((temp_recv*)extra)->data.append((char*)pData, iLength);
if (m_server->config().max_upload_size > 0)
{
if (((temp_recv*)extra)->data.length() > m_server->config().max_upload_size*1024*1024)
{
const char *error_body = "exceeded maximum upload limit";
pSender->SendResponse(dwConnID,500,"Internal Server Error",nullptr,0,(const BYTE*)error_body,strlen(error_body));
return HPR_ERROR;
}
}
}
#endif
return HPR_OK;

View File

@@ -38,90 +38,64 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
#endif
#include <cmath>
#include <string.h>
#include <unordered_map>
#include "util/strutils.h"
#ifdef _WIN32
#pragma warning(disable: 4996)
#endif
void ylib::network::content_type(const std::string& extName, std::string& type)
{
if (extName == "html")
static const std::unordered_map<std::string, std::string> mimeTypes = {
{"html", "text/html"},
{"htm", "text/html"},
{"css", "text/css"},
{"text", "text/plain"},
{"txt", "text/plain"},
{"icon", "image/x-icon"},
{"ico", "image/x-icon"},
{"jpeg", "image/jpeg"},
{"jpg", "image/jpeg"},
{"mp3", "audio/mpeg"},
{"pdf", "application/pdf"},
{"swf", "application/x-shockwave-flash"},
{"wav", "audio/x-wav"},
{"zip", "application/zip"},
{"mp4", "video/mpeg4"},
{"png", "image/png"},
{"gif", "image/gif"},
{"bmp", "image/bmp"},
{"svg", "image/svg+xml"},
{"rmvb", "application/vnd.rn-realmedia-vbr"},
{"js", "application/javascript"},
{"json", "application/json"},
{"xml", "application/xml"},
{"ttf", "application/octet-stream"},
{"woff", "application/font-woff"},
{"woff2", "application/font-woff2"},
{"eot", "application/vnd.ms-fontobject"},
{"otf", "font/otf"},
{"webp", "image/webp"},
{"avi", "video/x-msvideo"},
{"mov", "video/quicktime"},
{"flv", "video/x-flv"},
{"mkv", "video/x-matroska"},
{"doc", "application/msword"},
{"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{"ppt", "application/vnd.ms-powerpoint"},
{"pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
{"xls", "application/vnd.ms-excel"},
{"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}
};
auto it = mimeTypes.find(extName);
if (it != mimeTypes.end())
{
type = "text/html";
}
else if (extName == "htm")
{
type = "text/html";
}
else if (extName == "css")
{
type = "text/css";
}
else if (extName == "text")
{
type = "text/plain";
}
else if (extName == "icon")
{
type = "image/x-icon";
}
else if (extName == "jpeg")
{
type = "image/jpeg";
}
else if (extName == "mp3")
{
type = "audio/mpeg";
}
else if (extName == "pdf")
{
type = "application/pdf";
}
else if (extName == "swf")
{
type = "application/x-shockwave-flash";
}
else if (extName == "wav")
{
type = "audio/x-wav";
}
else if (extName == "zip")
{
type = "application/zip";
}
else if (extName == "mp4")
{
type = "video/mpeg4";
}
else if (extName == "png")
{
type = "image/png";
}
else if (extName == "rmvb")
{
type = "application/vnd.rn-realmedia-vbr";
}
else if (extName == "js")
{
type = "application/x-javascript";
}
else if (extName == "ttf")
{
type = "application/octet-stream";
}
else if (extName == "woff")
{
type = "application/x-font-woff";
}
else if (extName == "woff2")
{
type = "application/x-font-woff";
type = it->second;
}
else
{
type = extName;
type = "application/octet-stream"; // default MIME type for unknown extensions
}
}
bool ylib::network::parse_url(const std::string& url, std::string& httpType,std::string&host, std::string& ipaddress, ushort& port, std::string& urlField)