fix: leak in response class

This commit is contained in:
Talyx
2022-02-19 21:49:22 +03:00
parent 11b1985a39
commit 5ec52e4e2c
5 changed files with 62 additions and 72 deletions

View File

@@ -135,40 +135,40 @@ void Client::increaseRecvCounter(unsigned int n)
} }
//Генерирует response. Далее респонс можно получить через функцию getStrToSend() //Генерирует response. Далее респонс можно получить через функцию getStrToSend()
int Client::sendResponse(int fd) // int Client::sendResponse(int fd)
{ // {
_response.setData(_request, _config); // _response.setData(_request, _config);
_response.generate(); // _response.generate();
_headerToSend = _response.getHeader(); // _headerToSend = _response.getHeader();
_bodyToSend = _response.getBody(); // _bodyToSend = _response.getBody();
_ret = sendData(fd, _headerToSend + _bodyToSend); // _ret = sendData(fd, _headerToSend + _bodyToSend);
return (_ret); // return (_ret);
} // }
std::string Client::generateRespons(void) // std::string Client::generateRespons(void)
{ // {
size_t len; // size_t len;
_response.setData(_request, _config); // _response.setData(_request, _config);
_response.generate(); // _response.generate();
_headerToSend = _response.getHeader(); // _headerToSend = _response.getHeader();
_bodyToSend = _response.getBody(); // _bodyToSend = _response.getBody();
*_toSend = _headerToSend + _bodyToSend; // *_toSend = _headerToSend + _bodyToSend;
len = _toSend->size(); // len = _toSend->size();
response_len = len; // response_len = len;
_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);
return (*_toSend); // return (*_toSend);
} // }
std::string Client::generateRespons(std::vector<ServerConfig *> &configs) std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
{ {
size_t len; size_t len;
location *tmp; std::vector<location *> tmp;
_config = Config::getConfig(configs, _request, connected_to); _config = Config::getConfig(configs, _request, connected_to);
tmp = Config::getLocation(_config->getLocations(), _request.getURI()); tmp = Config::getLocation(_config->getLocations(), _request.getURI());
@@ -176,7 +176,7 @@ std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
_response.generate2(connected_to); _response.generate2(connected_to);
_toSend = new std::string; _toSend = new std::string;
*_toSend = (_response.getBody().empty()) ? _response.getHeader() : _response.getHeader() + _response.getBody(); *_toSend = (_response.getBody() == NULL) ? _response.getHeader() : _response.getHeader() + *_response.getBody();
len = _toSend->size(); len = _toSend->size();

View File

@@ -63,7 +63,7 @@ int Config::calcLen(std::string &s1, std::string &s2)
return (len); return (len);
} }
location *Config::getLocation(std::vector<location *> &arr, std::string &URI) std::vector<location *> Config::getLocation(std::vector<location *> &arr, std::string &URI)
{ {
unsigned int tryLen = URI.size(); unsigned int tryLen = URI.size();
std::string tryLocation; std::string tryLocation;
@@ -83,12 +83,7 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
tmp = *it; tmp = *it;
tryLocation = URI.substr(0, tryLen); tryLocation = URI.substr(0, tryLen);
if (tmp->location == tryLocation) if (tmp->location == tryLocation)
{
tmp = new location;
copyLocation(tmp, *it);
step_1.push_back(tmp); step_1.push_back(tmp);
break;
}
it++; it++;
} }
if (!step_1.empty()) if (!step_1.empty())
@@ -102,6 +97,7 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
if (tmp->location == URI || tmp->location.size() > 1) if (tmp->location == URI || tmp->location.size() > 1)
step_2.push_back(tmp); step_2.push_back(tmp);
} }
it = arr.begin(); it = arr.begin();
while (it != arr.end()) while (it != arr.end())
{ {
@@ -110,40 +106,22 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
{ {
suffix1 = tmp->location.substr(2); suffix1 = tmp->location.substr(2);
if (suffix1 == suffix) if (suffix1 == suffix)
{
tmp = new location;
copyLocation(tmp, *it);
step_2.push_back(tmp); step_2.push_back(tmp);
break;
}
} }
it++; it++;
} }
if (step_2.size() == 1) if (step_2.empty())
return (step_2[0]);
else if (step_2.size() == 2)
{ {
if(!step_2[1]->cgi_pass.empty()) it = arr.begin();
while (it != arr.end())
{ {
step_2[0]->cgi_pass = step_2[1]->cgi_pass; tmp = *it;
delete step_2[1]; if (tmp->location == "/")
step_2.push_back(tmp);
it++;
} }
return (step_2[0]);
} }
it = arr.begin(); return (step_2);
while (it != arr.end())
{
tmp = *it;
if (tmp->location == "/")
{
tmp = new location;
copyLocation(tmp, *it);
return (tmp);
}
it++;
}
return (NULL);
} }
ServerConfig *Config::getConfig(std::vector<ServerConfig *> &arr, Request &request, serverListen &data) ServerConfig *Config::getConfig(std::vector<ServerConfig *> &arr, Request &request, serverListen &data)

View File

@@ -16,7 +16,7 @@ public:
~Config(); ~Config();
static int calcLen(std::string &, std::string &); static int calcLen(std::string &, std::string &);
static location *getLocation(std::vector<location *> &, std::string &URI); static std::vector<location *> getLocation(std::vector<location *> &, std::string &URI);
static ServerConfig *getConfig(std::vector<ServerConfig *> &, Request &request, serverListen &data); static ServerConfig *getConfig(std::vector<ServerConfig *> &, Request &request, serverListen &data);
private: private:

View File

@@ -24,9 +24,9 @@ std::string Response::getHeader(void)
{ {
return (*_header); return (*_header);
} }
std::string Response::getBody(void) std::string *Response::getBody(void)
{ {
return (*_body); return (_body);
} }
void Response::setData(Request request, ServerConfig *config) void Response::setData(Request request, ServerConfig *config)
@@ -36,12 +36,21 @@ void Response::setData(Request request, ServerConfig *config)
_config = config; _config = config;
} }
void Response::setData(Request &request, ServerConfig *config, location *loc) void Response::setData(Request &request, ServerConfig *config, std::vector<location *> &loc)
{ {
_request = request; _request = request;
_code = request.getCode(); _code = request.getCode();
_config = config; _config = config;
_location = loc; if (loc.empty())
_code = 404;
else
{
_location = loc[0];
if (loc.size() == 2)
_cgi_Pass = loc[1]->cgi_pass;
else
_cgi_Pass = _location->cgi_pass;
}
} }
void Response::setHeaderBlocks(void) void Response::setHeaderBlocks(void)
@@ -68,7 +77,10 @@ void Response::setContentType(void)
void Response::setContentLength() void Response::setContentLength()
{ {
_contentLength = _body->size(); if (_body != NULL)
_contentLength = _body->size();
else
_contentLength = 0;
} }
void Response::setServer(void) void Response::setServer(void)
@@ -109,7 +121,7 @@ serverListen Response::getListen()
std::string Response::getCgiPass(void) std::string Response::getCgiPass(void)
{ {
return (_location->cgi_pass); return (_cgi_Pass);
} }
ssize_t Response::getMaxBodySize(void) ssize_t Response::getMaxBodySize(void)
@@ -283,7 +295,7 @@ bool Response::allowedMethod(std::string &method)
return (true); return (true);
it++; it++;
} }
if (!_location->cgi_pass.empty() && (method == "GET" || method == "POST")) if (!_cgi_Pass.empty() && (method == "GET" || method == "POST"))
return (true); return (true);
_code = 405; _code = 405;
return (false); return (false);
@@ -330,7 +342,7 @@ void Response::generate2(serverListen &l)
{ {
_code = 404; _code = 404;
} }
else else if (!_request.badCode(_code))
{ {
_listen = l; _listen = l;
_errorPages = _config->getErrorPages(); _errorPages = _config->getErrorPages();
@@ -344,7 +356,6 @@ void Response::generate2(serverListen &l)
if (_maxBodySize > 0 && _request.getBody() != NULL) if (_maxBodySize > 0 && _request.getBody() != NULL)
_code = (_request.getBody()->size() > (unsigned long)_maxBodySize) ? 413 : _code; _code = (_request.getBody()->size() > (unsigned long)_maxBodySize) ? 413 : _code;
} }
if (_request.badCode(_code) || !allowedMethod(_method) || isRedirect()) if (_request.badCode(_code) || !allowedMethod(_method) || isRedirect())
{ {
invalidClient(); invalidClient();
@@ -389,7 +400,7 @@ void Response::invalidClient(void)
void Response::methodGet(void) void Response::methodGet(void)
{ {
_body = new std::string; _body = new std::string;
if (!_location->cgi_pass.empty()) if (!_cgi_Pass.empty())
{ {
CgiHandle cgi(_request, *this); CgiHandle cgi(_request, *this);
@@ -416,7 +427,7 @@ void Response::methodGet(void)
} }
void Response::methodPost(void) void Response::methodPost(void)
{ {
if (!_location->cgi_pass.empty()) if (!_cgi_Pass.empty())
{ {
_body = new std::string; _body = new std::string;
CgiHandle cgi(_request, *this); CgiHandle cgi(_request, *this);
@@ -565,5 +576,5 @@ std::string Response::getErrorPage(int code)
Response::~Response() Response::~Response()
{ {
delete _location;
} }

View File

@@ -34,6 +34,7 @@ private:
std::string _contentLanguage; std::string _contentLanguage;
std::string _transferEncoding; std::string _transferEncoding;
std::string _upload_dir; std::string _upload_dir;
std::string _cgi_Pass;
void setHeaderBlocks(void); void setHeaderBlocks(void);
void setContentType(void); void setContentType(void);
@@ -62,7 +63,7 @@ public:
serverListen getListen(void); serverListen getListen(void);
std::string getCgiPass(void); std::string getCgiPass(void);
std::string getHeader(void); std::string getHeader(void);
std::string getBody(void); std::string *getBody(void);
static std::string getReasonPhrase(std::string); static std::string getReasonPhrase(std::string);
static std::string getReasonPhrase(int); static std::string getReasonPhrase(int);
std::string getErrorPage(int code); std::string getErrorPage(int code);
@@ -71,7 +72,7 @@ public:
bool isRedirect(void); bool isRedirect(void);
bool allowedMethod(std::string &); bool allowedMethod(std::string &);
void setData(Request, ServerConfig *); void setData(Request, ServerConfig *);
void setData(Request &, ServerConfig *, location *location); void setData(Request &, ServerConfig *, std::vector<location *> &);
public: public:
std::string _fullURI; std::string _fullURI;
void OpenResponseFile(const char *path); void OpenResponseFile(const char *path);