mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 05:17:59 +03:00
add: memory optimization
This commit is contained in:
@@ -81,7 +81,6 @@ std::string CgiHandle::executeCgi()
|
|||||||
int sO;
|
int sO;
|
||||||
int byte_read = 1;
|
int byte_read = 1;
|
||||||
std::string body;
|
std::string body;
|
||||||
std::string &reqBody = _request.getBody();
|
|
||||||
|
|
||||||
argv[0] = const_cast<char *>(_response._fullURI.data());
|
argv[0] = const_cast<char *>(_response._fullURI.data());
|
||||||
argv[1] = NULL;
|
argv[1] = NULL;
|
||||||
@@ -100,7 +99,7 @@ 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, reqBody.data(), reqBody.size());
|
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)
|
||||||
@@ -152,7 +151,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"] = 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");
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ bool Client::isChunked(void)
|
|||||||
// Возвращает истина, если отправлены все данные клиента.
|
// Возвращает истина, если отправлены все данные клиента.
|
||||||
bool Client::allSended(void)
|
bool Client::allSended(void)
|
||||||
{
|
{
|
||||||
if (_toSend.size() <= _sended)
|
if (response_len <= _sended)
|
||||||
done = true;
|
done = true;
|
||||||
return (done);
|
return (done);
|
||||||
}
|
}
|
||||||
@@ -154,15 +154,15 @@ std::string Client::generateRespons(void)
|
|||||||
_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)
|
||||||
@@ -174,17 +174,20 @@ std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
|
|||||||
tmp = Config::getLocation(_config->getLocations(), _request.getURI());
|
tmp = Config::getLocation(_config->getLocations(), _request.getURI());
|
||||||
_response.setData(_request, _config, tmp);
|
_response.setData(_request, _config, tmp);
|
||||||
_response.generate2(connected_to);
|
_response.generate2(connected_to);
|
||||||
_headerToSend = _response.getHeader();
|
|
||||||
_bodyToSend = _response.getBody();
|
_toSend = new std::string;
|
||||||
_toSend = _headerToSend + _bodyToSend;
|
*_toSend = _response.getHeader() + _response.getBody();
|
||||||
|
|
||||||
|
|
||||||
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);
|
delete _request.getBody();
|
||||||
|
delete _toSend;
|
||||||
|
_response.freeData();
|
||||||
|
return (_headerToSend);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------Error---------------------------------------
|
//-------------------------------------------------Error---------------------------------------
|
||||||
@@ -258,7 +261,6 @@ void Client::clear(void)
|
|||||||
_config = NULL;
|
_config = NULL;
|
||||||
_bodyToSend = "";
|
_bodyToSend = "";
|
||||||
_headerToSend = "";
|
_headerToSend = "";
|
||||||
_toSend = "";
|
|
||||||
if (_to_send_char)
|
if (_to_send_char)
|
||||||
delete[] _to_send_char;
|
delete[] _to_send_char;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ private:
|
|||||||
std::string _stringBUF;
|
std::string _stringBUF;
|
||||||
std::string _bodyToSend;
|
std::string _bodyToSend;
|
||||||
std::string _headerToSend;
|
std::string _headerToSend;
|
||||||
std::string _toSend;
|
std::string *_toSend;
|
||||||
char *_to_send_char;
|
char *_to_send_char;
|
||||||
std::map<std::string, std::string> _errorCode;
|
std::map<std::string, std::string> _errorCode;
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ Request::Request()
|
|||||||
_received = 0;
|
_received = 0;
|
||||||
_headerSize = 0;
|
_headerSize = 0;
|
||||||
_lifeTime = 5;
|
_lifeTime = 5;
|
||||||
|
_body = new std::string;
|
||||||
}
|
}
|
||||||
|
|
||||||
Request::Request(char *str)
|
Request::Request(char *str)
|
||||||
@@ -28,6 +28,8 @@ Request::Request(char *str)
|
|||||||
_contentLength = 0;
|
_contentLength = 0;
|
||||||
_headerSize = 0;
|
_headerSize = 0;
|
||||||
_lifeTime = 5;
|
_lifeTime = 5;
|
||||||
|
_body = new std::string;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------Get/Set---------------------------------------
|
//-------------------------------------------------Get/Set---------------------------------------
|
||||||
@@ -36,7 +38,7 @@ std::string &Request::getURI(void)
|
|||||||
{
|
{
|
||||||
return (_URI);
|
return (_URI);
|
||||||
}
|
}
|
||||||
std::string &Request::getBody(void)
|
std::string *Request::getBody(void)
|
||||||
{
|
{
|
||||||
return (_body);
|
return (_body);
|
||||||
}
|
}
|
||||||
@@ -230,13 +232,13 @@ void Request::splitData(std::string &data)
|
|||||||
return ;
|
return ;
|
||||||
else if (_chunked)
|
else if (_chunked)
|
||||||
{
|
{
|
||||||
_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)
|
||||||
_body_ok = true;
|
_body_ok = true;
|
||||||
}
|
}
|
||||||
else if (_head_ok && !_body_ok)
|
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)
|
if ((_received - _headerSize) == _contentLength)
|
||||||
{
|
{
|
||||||
_body_ok = true;
|
_body_ok = true;
|
||||||
@@ -244,7 +246,7 @@ void Request::splitData(std::string &data)
|
|||||||
}
|
}
|
||||||
if (_head_ok && _body_ok && _chunked)
|
if (_head_ok && _body_ok && _chunked)
|
||||||
{
|
{
|
||||||
std::string &tmp = _body;
|
std::string &tmp = *_body;
|
||||||
std::string subchunk = tmp.substr(0, 100);
|
std::string subchunk = tmp.substr(0, 100);
|
||||||
std::string newBody = "";
|
std::string newBody = "";
|
||||||
int chunksize = strtol(subchunk.c_str(), NULL, 16);
|
int chunksize = strtol(subchunk.c_str(), NULL, 16);
|
||||||
@@ -258,7 +260,7 @@ void Request::splitData(std::string &data)
|
|||||||
subchunk = tmp.substr(i, 100);
|
subchunk = tmp.substr(i, 100);
|
||||||
chunksize = strtol(subchunk.c_str(), NULL, 16);
|
chunksize = strtol(subchunk.c_str(), NULL, 16);
|
||||||
}
|
}
|
||||||
_body = newBody;
|
*_body = newBody;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,7 +405,6 @@ void Request::clear(void)
|
|||||||
_ret = 200;
|
_ret = 200;
|
||||||
_row = 0;
|
_row = 0;
|
||||||
_URI = "";
|
_URI = "";
|
||||||
_body = "";
|
|
||||||
_host = "";
|
_host = "";
|
||||||
_query = "";
|
_query = "";
|
||||||
_method = "";
|
_method = "";
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ private:
|
|||||||
|
|
||||||
std::string _URI;
|
std::string _URI;
|
||||||
std::string _head;
|
std::string _head;
|
||||||
std::string _body;
|
std::string *_body;
|
||||||
std::string _host;
|
std::string _host;
|
||||||
std::string _query;
|
std::string _query;
|
||||||
std::string _method;
|
std::string _method;
|
||||||
@@ -37,7 +37,7 @@ private:
|
|||||||
bool _chunked;
|
bool _chunked;
|
||||||
public:
|
public:
|
||||||
std::string &getURI(void);
|
std::string &getURI(void);
|
||||||
std::string &getBody(void);
|
std::string *getBody(void);
|
||||||
std::string &getHost(void);
|
std::string &getHost(void);
|
||||||
std::string &getQuery(void);
|
std::string &getQuery(void);
|
||||||
std::string &getMethod(void);
|
std::string &getMethod(void);
|
||||||
|
|||||||
@@ -6,18 +6,26 @@ Response::Response()
|
|||||||
{
|
{
|
||||||
initErrorCode();
|
initErrorCode();
|
||||||
_Autoindex = true;
|
_Autoindex = true;
|
||||||
|
_body = new std::string;
|
||||||
|
_header = new std::string;
|
||||||
_code = 200;
|
_code = 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------GET/SET---------------------------------------
|
//-------------------------------------------------GET/SET---------------------------------------
|
||||||
|
|
||||||
|
void Response::freeData(void)
|
||||||
|
{
|
||||||
|
delete _body;
|
||||||
|
delete _header;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Response::getHeader(void)
|
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)
|
||||||
@@ -59,7 +67,7 @@ void Response::setContentType(void)
|
|||||||
|
|
||||||
void Response::setContentLength()
|
void Response::setContentLength()
|
||||||
{
|
{
|
||||||
_contentLength = _body.size();
|
_contentLength = _body->size();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::setServer(void)
|
void Response::setServer(void)
|
||||||
@@ -118,7 +126,7 @@ void Response::OpenResponseFile(const char *path)
|
|||||||
if (file.is_open())
|
if (file.is_open())
|
||||||
{
|
{
|
||||||
ss << file.rdbuf();
|
ss << file.rdbuf();
|
||||||
_body = ss.str();
|
*_body = ss.str();
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -142,14 +150,14 @@ void Response::OpenErrorFile(int code)
|
|||||||
if (file.is_open())
|
if (file.is_open())
|
||||||
{
|
{
|
||||||
ss << file.rdbuf();
|
ss << file.rdbuf();
|
||||||
_body = ss.str();
|
*_body = ss.str();
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_body = getErrorPage(code);
|
*_body = getErrorPage(code);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
_body = getErrorPage(code);
|
*_body = getErrorPage(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Response::getContentType(void)
|
std::string Response::getContentType(void)
|
||||||
@@ -239,8 +247,8 @@ void Response::generateBody(void)
|
|||||||
{
|
{
|
||||||
if (_Autoindex)
|
if (_Autoindex)
|
||||||
{
|
{
|
||||||
_body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost(), _listen.port);
|
*_body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost(), _listen.port);
|
||||||
if (_body.empty())
|
if (_body->empty())
|
||||||
_code = 404;
|
_code = 404;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -292,7 +300,7 @@ void Response::generateHeader(void)
|
|||||||
if (!_locationSTR.empty())
|
if (!_locationSTR.empty())
|
||||||
ss << "Location: " << _locationSTR << "\r\n";
|
ss << "Location: " << _locationSTR << "\r\n";
|
||||||
ss << "\r\n";
|
ss << "\r\n";
|
||||||
_header = ss.str();
|
*_header = ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::generate()
|
void Response::generate()
|
||||||
@@ -326,7 +334,7 @@ void Response::generate2(serverListen &l)
|
|||||||
_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)
|
||||||
_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())
|
if (_request.badCode(_code) || (!allowedMethod(_method) && _location->cgi_pass.empty()) || isRedirect())
|
||||||
@@ -375,19 +383,19 @@ void Response::methodGet(void)
|
|||||||
{
|
{
|
||||||
CgiHandle cgi(_request, *this);
|
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)
|
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 stat = tmp.find("Status: ");
|
||||||
unsigned long type = tmp.find("Content-type: ");
|
unsigned long type = tmp.find("Content-type: ");
|
||||||
if (stat != std::string::npos)
|
if (stat != std::string::npos)
|
||||||
_code = atoi(tmp.substr(stat + 8, 3).c_str());
|
_code = atoi(tmp.substr(stat + 8, 3).c_str());
|
||||||
if (type != std::string::npos)
|
if (type != std::string::npos)
|
||||||
_contentType = tmp.substr(type + 14, tmp.find("\r\n", type+14));
|
_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
|
else
|
||||||
@@ -402,19 +410,19 @@ void Response::methodPost(void)
|
|||||||
{
|
{
|
||||||
CgiHandle cgi(_request, *this);
|
CgiHandle cgi(_request, *this);
|
||||||
|
|
||||||
_body = cgi.executeCgi();
|
*_body = cgi.executeCgi();
|
||||||
DBOUT << "CGI SIZE BODY " << _body.size() << ENDL;
|
DBOUT << "CGI SIZE BODY " << _body->size() << ENDL;
|
||||||
unsigned long pos = _body.find("\r\n\r\n");
|
unsigned long pos = _body->find("\r\n\r\n");
|
||||||
if (pos != std::string::npos)
|
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 stat = tmp.find("Status: ");
|
||||||
unsigned long type = tmp.find("Content-type: ");
|
unsigned long type = tmp.find("Content-type: ");
|
||||||
if (stat != std::string::npos)
|
if (stat != std::string::npos)
|
||||||
_code = atoi(tmp.substr(stat + 8, 3).c_str());
|
_code = atoi(tmp.substr(stat + 8, 3).c_str());
|
||||||
if (type != std::string::npos)
|
if (type != std::string::npos)
|
||||||
_contentType = tmp.substr(type + 14, tmp.find("\r\n", type+14));
|
_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
|
else
|
||||||
@@ -434,7 +442,7 @@ void Response::methodPut(void)
|
|||||||
_code = 403;
|
_code = 403;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
file.write(_request.getBody().data(), _request.getBody().size());
|
file.write(_request.getBody()->data(), _request.getBody()->size());
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
setHeaderBlocks();
|
setHeaderBlocks();
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ class Response
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
serverListen _listen;
|
serverListen _listen;
|
||||||
std::string _body;
|
std::string *_body;
|
||||||
std::string _header;
|
std::string *_header;
|
||||||
Request _request;
|
Request _request;
|
||||||
ServerConfig *_config;
|
ServerConfig *_config;
|
||||||
location *_location;
|
location *_location;
|
||||||
@@ -78,6 +78,7 @@ public:
|
|||||||
void initErrorCode(void);
|
void initErrorCode(void);
|
||||||
void generate();
|
void generate();
|
||||||
void generate2(serverListen &);
|
void generate2(serverListen &);
|
||||||
|
void freeData(void);
|
||||||
Response();
|
Response();
|
||||||
~Response();
|
~Response();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user