fix: leak in Response class

This commit is contained in:
Talyx
2022-02-18 18:12:01 +03:00
parent 28c86e5e43
commit 1519914e97
5 changed files with 19 additions and 7 deletions

View File

@@ -99,7 +99,9 @@ std::string CgiHandle::executeCgi()
FILE *fOt = tmpfile(); FILE *fOt = tmpfile();
long fdin = fileno(fIn); long fdin = fileno(fIn);
long fdOut = fileno(fOt); long fdOut = fileno(fOt);
write(fdin, _request.getBody()->data(), _request.getBody()->size()); if (_request.getBody() != NULL)
write(fdin, _request.getBody()->data(), _request.getBody()->size());
lseek(fdin, 0, SEEK_SET); lseek(fdin, 0, SEEK_SET);
pid = fork(); pid = fork();
if (pid == -1) if (pid == -1)
@@ -151,7 +153,7 @@ void CgiHandle::initEnvVariables()
_variable["AUTH TYPE"] = it->second; _variable["AUTH TYPE"] = it->second;
else else
_variable["AUTH TYPE"] = ""; _variable["AUTH TYPE"] = "";
_variable["CONTENT_LENGTH"] = toString(_request.getBody()->size()); _variable["CONTENT_LENGTH"] = (_request.getBody() == NULL) ? "0" : toString(_request.getBody()->size());
it = _request.getClientFields().find("content-type"); it = _request.getClientFields().find("content-type");
_variable["CONTENT_TYPE"] = ""; _variable["CONTENT_TYPE"] = "";
_variable["GATEWAY_INTERFACE"] = std::string("CGI/1.1"); _variable["GATEWAY_INTERFACE"] = std::string("CGI/1.1");

View File

@@ -184,8 +184,10 @@ std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
_to_send_char = new char[len + 1]; _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);
delete _request.getBody();
delete _toSend; delete _toSend;
if (_request.getBody() != NULL)
_request.freeData();
_response.freeData(); _response.freeData();
return (_headerToSend); return (_headerToSend);
} }

View File

@@ -13,7 +13,7 @@ Request::Request()
_received = 0; _received = 0;
_headerSize = 0; _headerSize = 0;
_lifeTime = 5; _lifeTime = 5;
_body = new std::string; _body = NULL;
} }
Request::Request(char *str) Request::Request(char *str)
@@ -28,12 +28,17 @@ Request::Request(char *str)
_contentLength = 0; _contentLength = 0;
_headerSize = 0; _headerSize = 0;
_lifeTime = 5; _lifeTime = 5;
_body = new std::string; _body = NULL;
} }
//-------------------------------------------------Get/Set--------------------------------------- //-------------------------------------------------Get/Set---------------------------------------
void Request::freeData(void)
{
delete _body;
}
std::string &Request::getURI(void) std::string &Request::getURI(void)
{ {
return (_URI); return (_URI);
@@ -226,11 +231,13 @@ void Request::splitData(std::string &data)
if ((_contentLength == 0 && !_chunked) || (_method == "GET" if ((_contentLength == 0 && !_chunked) || (_method == "GET"
|| _method == "DELETE" || _method == "HEAD")) || _method == "DELETE" || _method == "HEAD"))
_body_ok = true; _body_ok = true;
else
_body = new std::string;
} }
} }
if (badCode(_ret)) if (badCode(_ret))
return ; return ;
else if (_chunked) else if (_chunked && !_body_ok)
{ {
_body->insert(_body->end(), str.begin(), str.end()); _body->insert(_body->end(), str.begin(), str.end());
if (checkEnd(*_body, "0\r\n\r\n") == 0) if (checkEnd(*_body, "0\r\n\r\n") == 0)

View File

@@ -81,6 +81,7 @@ public:
void copyFromMap(void); void copyFromMap(void);
void clear(void); void clear(void);
void splitData(std::string &); void splitData(std::string &);
void freeData(void);
void increaseRecvCounter(unsigned int n); void increaseRecvCounter(unsigned int n);
~Request(); ~Request();

View File

@@ -333,7 +333,7 @@ void Response::generate2(serverListen &l)
_fullURI = getFullURI(); _fullURI = getFullURI();
_method = _request.getMethod(); _method = _request.getMethod();
_maxBodySize = (_location->clientBodySize > 0) ? _location->clientBodySize : _config->getClientBodySize(); _maxBodySize = (_location->clientBodySize > 0) ? _location->clientBodySize : _config->getClientBodySize();
if (_maxBodySize > 0) if (_maxBodySize > 0 && _request.getBody() != NULL)
_code = (_request.getBody()->size() > (unsigned long)_maxBodySize) ? 413 : _code; _code = (_request.getBody()->size() > (unsigned long)_maxBodySize) ? 413 : _code;
} }