From 28c86e5e437972f93a9c29311b9015bbb9cda1db Mon Sep 17 00:00:00 2001 From: Talyx Date: Thu, 17 Feb 2022 21:15:53 +0300 Subject: [PATCH] add: memory optimization --- src/CGI/CgiHandle.cpp | 5 ++--- src/Client/Client.cpp | 26 +++++++++++---------- src/Client/Client.hpp | 2 +- src/Client/Request.cpp | 17 +++++++------- src/Client/Request.hpp | 4 ++-- src/Client/Response.cpp | 50 ++++++++++++++++++++++++----------------- src/Client/Response.hpp | 5 +++-- 7 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/CGI/CgiHandle.cpp b/src/CGI/CgiHandle.cpp index 2ddfda0..916a262 100644 --- a/src/CGI/CgiHandle.cpp +++ b/src/CGI/CgiHandle.cpp @@ -81,7 +81,6 @@ std::string CgiHandle::executeCgi() int sO; int byte_read = 1; std::string body; - std::string &reqBody = _request.getBody(); argv[0] = const_cast(_response._fullURI.data()); argv[1] = NULL; @@ -100,7 +99,7 @@ std::string CgiHandle::executeCgi() FILE *fOt = tmpfile(); long fdin = fileno(fIn); long fdOut = fileno(fOt); - write(fdin, reqBody.data(), reqBody.size()); + write(fdin, _request.getBody()->data(), _request.getBody()->size()); lseek(fdin, 0, SEEK_SET); pid = fork(); if (pid == -1) @@ -152,7 +151,7 @@ void CgiHandle::initEnvVariables() _variable["AUTH TYPE"] = it->second; else _variable["AUTH TYPE"] = ""; - _variable["CONTENT_LENGTH"] = toString(_request.getBody().size()); + _variable["CONTENT_LENGTH"] = toString(_request.getBody()->size()); it = _request.getClientFields().find("content-type"); _variable["CONTENT_TYPE"] = ""; _variable["GATEWAY_INTERFACE"] = std::string("CGI/1.1"); diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index b1bbcba..7f5d0df 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -111,7 +111,7 @@ bool Client::isChunked(void) // Возвращает истина, если отправлены все данные клиента. bool Client::allSended(void) { - if (_toSend.size() <= _sended) + if (response_len <= _sended) done = true; return (done); } @@ -154,15 +154,15 @@ std::string Client::generateRespons(void) _response.generate(); _headerToSend = _response.getHeader(); _bodyToSend = _response.getBody(); - _toSend = _headerToSend + _bodyToSend; + *_toSend = _headerToSend + _bodyToSend; - len = _toSend.size(); + len = _toSend->size(); response_len = len; _to_send_char = new char[len + 1]; - std::memcpy(_to_send_char, _toSend.c_str(), len + 1); + std::memcpy(_to_send_char, _toSend->c_str(), len + 1); - return (_toSend); + return (*_toSend); } std::string Client::generateRespons(std::vector &configs) @@ -174,17 +174,20 @@ std::string Client::generateRespons(std::vector &configs) tmp = Config::getLocation(_config->getLocations(), _request.getURI()); _response.setData(_request, _config, tmp); _response.generate2(connected_to); - _headerToSend = _response.getHeader(); - _bodyToSend = _response.getBody(); - _toSend = _headerToSend + _bodyToSend; + + _toSend = new std::string; + *_toSend = _response.getHeader() + _response.getBody(); - len = _toSend.size(); + len = _toSend->size(); response_len = len; _to_send_char = new char[len + 1]; - std::memcpy(_to_send_char, _toSend.c_str(), len + 1); + std::memcpy(_to_send_char, _toSend->c_str(), len + 1); - return (_toSend); + delete _request.getBody(); + delete _toSend; + _response.freeData(); + return (_headerToSend); } //-------------------------------------------------Error--------------------------------------- @@ -258,7 +261,6 @@ void Client::clear(void) _config = NULL; _bodyToSend = ""; _headerToSend = ""; - _toSend = ""; if (_to_send_char) delete[] _to_send_char; } diff --git a/src/Client/Client.hpp b/src/Client/Client.hpp index bbf5ddf..975e0d9 100644 --- a/src/Client/Client.hpp +++ b/src/Client/Client.hpp @@ -26,7 +26,7 @@ private: std::string _stringBUF; std::string _bodyToSend; std::string _headerToSend; - std::string _toSend; + std::string *_toSend; char *_to_send_char; std::map _errorCode; diff --git a/src/Client/Request.cpp b/src/Client/Request.cpp index ae553a9..56c5c88 100644 --- a/src/Client/Request.cpp +++ b/src/Client/Request.cpp @@ -13,7 +13,7 @@ Request::Request() _received = 0; _headerSize = 0; _lifeTime = 5; - + _body = new std::string; } Request::Request(char *str) @@ -28,6 +28,8 @@ Request::Request(char *str) _contentLength = 0; _headerSize = 0; _lifeTime = 5; + _body = new std::string; + } //-------------------------------------------------Get/Set--------------------------------------- @@ -36,7 +38,7 @@ std::string &Request::getURI(void) { return (_URI); } -std::string &Request::getBody(void) +std::string *Request::getBody(void) { return (_body); } @@ -230,13 +232,13 @@ void Request::splitData(std::string &data) return ; else if (_chunked) { - _body.insert(_body.end(), str.begin(), str.end()); - if (checkEnd(_body, "0\r\n\r\n") == 0) + _body->insert(_body->end(), str.begin(), str.end()); + if (checkEnd(*_body, "0\r\n\r\n") == 0) _body_ok = true; } else if (_head_ok && !_body_ok) { - _body.insert(_body.end(), str.begin(), str.end()); + _body->insert(_body->end(), str.begin(), str.end()); if ((_received - _headerSize) == _contentLength) { _body_ok = true; @@ -244,7 +246,7 @@ void Request::splitData(std::string &data) } if (_head_ok && _body_ok && _chunked) { - std::string &tmp = _body; + std::string &tmp = *_body; std::string subchunk = tmp.substr(0, 100); std::string newBody = ""; int chunksize = strtol(subchunk.c_str(), NULL, 16); @@ -258,7 +260,7 @@ void Request::splitData(std::string &data) subchunk = tmp.substr(i, 100); chunksize = strtol(subchunk.c_str(), NULL, 16); } - _body = newBody; + *_body = newBody; } } @@ -403,7 +405,6 @@ void Request::clear(void) _ret = 200; _row = 0; _URI = ""; - _body = ""; _host = ""; _query = ""; _method = ""; diff --git a/src/Client/Request.hpp b/src/Client/Request.hpp index 61a740a..18bdbbb 100644 --- a/src/Client/Request.hpp +++ b/src/Client/Request.hpp @@ -20,7 +20,7 @@ private: std::string _URI; std::string _head; - std::string _body; + std::string *_body; std::string _host; std::string _query; std::string _method; @@ -37,7 +37,7 @@ private: bool _chunked; public: std::string &getURI(void); - std::string &getBody(void); + std::string *getBody(void); std::string &getHost(void); std::string &getQuery(void); std::string &getMethod(void); diff --git a/src/Client/Response.cpp b/src/Client/Response.cpp index ba07978..7ae7b02 100644 --- a/src/Client/Response.cpp +++ b/src/Client/Response.cpp @@ -6,18 +6,26 @@ Response::Response() { initErrorCode(); _Autoindex = true; + _body = new std::string; + _header = new std::string; _code = 200; } //-------------------------------------------------GET/SET--------------------------------------- +void Response::freeData(void) +{ + delete _body; + delete _header; +} + std::string Response::getHeader(void) { - return (_header); + return (*_header); } std::string Response::getBody(void) { - return (_body); + return (*_body); } void Response::setData(Request request, ServerConfig *config) @@ -59,7 +67,7 @@ void Response::setContentType(void) void Response::setContentLength() { - _contentLength = _body.size(); + _contentLength = _body->size(); } void Response::setServer(void) @@ -118,7 +126,7 @@ void Response::OpenResponseFile(const char *path) if (file.is_open()) { ss << file.rdbuf(); - _body = ss.str(); + *_body = ss.str(); file.close(); } else @@ -142,14 +150,14 @@ void Response::OpenErrorFile(int code) if (file.is_open()) { ss << file.rdbuf(); - _body = ss.str(); + *_body = ss.str(); file.close(); } else - _body = getErrorPage(code); + *_body = getErrorPage(code); } else - _body = getErrorPage(code); + *_body = getErrorPage(code); } std::string Response::getContentType(void) @@ -239,8 +247,8 @@ void Response::generateBody(void) { if (_Autoindex) { - _body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost(), _listen.port); - if (_body.empty()) + *_body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost(), _listen.port); + if (_body->empty()) _code = 404; } else @@ -292,7 +300,7 @@ void Response::generateHeader(void) if (!_locationSTR.empty()) ss << "Location: " << _locationSTR << "\r\n"; ss << "\r\n"; - _header = ss.str(); + *_header = ss.str(); } void Response::generate() @@ -326,7 +334,7 @@ void Response::generate2(serverListen &l) _method = _request.getMethod(); _maxBodySize = (_location->clientBodySize > 0) ? _location->clientBodySize : _config->getClientBodySize(); if (_maxBodySize > 0) - _code = (_request.getBody().size() > (unsigned long)_maxBodySize) ? 413 : _code; + _code = (_request.getBody()->size() > (unsigned long)_maxBodySize) ? 413 : _code; } if (_request.badCode(_code) || (!allowedMethod(_method) && _location->cgi_pass.empty()) || isRedirect()) @@ -375,19 +383,19 @@ void Response::methodGet(void) { CgiHandle cgi(_request, *this); - _body = cgi.executeCgi(); + *_body = cgi.executeCgi(); - unsigned long pos = _body.find("\r\n\r\n"); + unsigned long pos = _body->find("\r\n\r\n"); if (pos != std::string::npos) { - std::string tmp = _body.substr(0, pos); + std::string tmp = _body->substr(0, pos); unsigned long stat = tmp.find("Status: "); unsigned long type = tmp.find("Content-type: "); if (stat != std::string::npos) _code = atoi(tmp.substr(stat + 8, 3).c_str()); if (type != std::string::npos) _contentType = tmp.substr(type + 14, tmp.find("\r\n", type+14)); - _body.erase(_body.begin(), _body.begin() + pos + 4); + _body->erase(_body->begin(), _body->begin() + pos + 4); } } else @@ -402,19 +410,19 @@ void Response::methodPost(void) { CgiHandle cgi(_request, *this); - _body = cgi.executeCgi(); - DBOUT << "CGI SIZE BODY " << _body.size() << ENDL; - unsigned long pos = _body.find("\r\n\r\n"); + *_body = cgi.executeCgi(); + DBOUT << "CGI SIZE BODY " << _body->size() << ENDL; + unsigned long pos = _body->find("\r\n\r\n"); if (pos != std::string::npos) { - std::string tmp = _body.substr(0, pos); + std::string tmp = _body->substr(0, pos); unsigned long stat = tmp.find("Status: "); unsigned long type = tmp.find("Content-type: "); if (stat != std::string::npos) _code = atoi(tmp.substr(stat + 8, 3).c_str()); if (type != std::string::npos) _contentType = tmp.substr(type + 14, tmp.find("\r\n", type+14)); - _body.erase(_body.begin(), _body.begin() + pos + 4); + _body->erase(_body->begin(), _body->begin() + pos + 4); } } else @@ -434,7 +442,7 @@ void Response::methodPut(void) _code = 403; else { - file.write(_request.getBody().data(), _request.getBody().size()); + file.write(_request.getBody()->data(), _request.getBody()->size()); } file.close(); setHeaderBlocks(); diff --git a/src/Client/Response.hpp b/src/Client/Response.hpp index e1f85ef..ccac86c 100644 --- a/src/Client/Response.hpp +++ b/src/Client/Response.hpp @@ -10,8 +10,8 @@ class Response { private: serverListen _listen; - std::string _body; - std::string _header; + std::string *_body; + std::string *_header; Request _request; ServerConfig *_config; location *_location; @@ -78,6 +78,7 @@ public: void initErrorCode(void); void generate(); void generate2(serverListen &); + void freeData(void); Response(); ~Response();