diff --git a/include/net/http_define.h b/include/net/http_define.h index b21784a..9c1b8a4 100644 --- a/include/net/http_define.h +++ b/include/net/http_define.h @@ -161,11 +161,21 @@ namespace ylib cdn_config cdn; // 代理 std::vector proxy; - + // GZIP bool gzip = false; }; - + /// + /// HTTP服务配置 + /// + struct server_config { + // 最大上传大小限制 + uint64 max_upload_size = 0; + // Https + bool https = false; + // 端口 + ushort port = 0; + }; /// /// 启动信息 /// @@ -174,7 +184,10 @@ namespace ylib std::vector website; // 证书 std::map cert; + // 最大上传大小限制 + uint64 max_upload_size = 0; }; + class agent; class website; // 临时接收 diff --git a/include/net/http_server.h b/include/net/http_server.h index 9941903..e6aeeeb 100644 --- a/include/net/http_server.h +++ b/include/net/http_server.h @@ -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; } + + /// + /// 取配置 + /// + /// + 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 diff --git a/src/net/http_center.cpp b/src/net/http_center.cpp index faa0b53..2aa4861 100644 --- a/src/net/http_center.cpp +++ b/src/net/http_center.cpp @@ -46,7 +46,11 @@ bool ylib::network::http::center::create(const start_config& config) //std::cout<<"ListenPort:"<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]; } diff --git a/src/net/http_server.cpp b/src/net/http_server.cpp index 73b9c57..02aa68d 100644 --- a/src/net/http_server.cpp +++ b/src/net/http_server.cpp @@ -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); diff --git a/src/net/http_server_lst.cpp b/src/net/http_server_lst.cpp index 190a30e..23a6b6e 100644 --- a/src/net/http_server_lst.cpp +++ b/src/net/http_server_lst.cpp @@ -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; diff --git a/src/net/util.cpp b/src/net/util.cpp index 6e8c7cb..ebb0379 100644 --- a/src/net/util.cpp +++ b/src/net/util.cpp @@ -38,90 +38,64 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web #endif #include #include - +#include #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 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)