Merge remote-tracking branch 'origin/fara' into roman

This commit is contained in:
3lswear
2022-01-29 20:32:30 +03:00
5 changed files with 170 additions and 36 deletions

View File

@@ -5,12 +5,14 @@
Client::Client()
{
this->_fd = -1;
this->_sended = 0;
}
Client::Client(char *str)
{
this->_fd = -1;
this->_buff = str;
this->_sended = 0;
}
@@ -19,6 +21,7 @@ Client::Client(char *str, ServerConfig *config)
this->_fd = -1;
this->_config = config;
this->_buff = str;
this->_sended = 0;
}
@@ -31,7 +34,12 @@ Request Client::getRequest(void)
Response Client::getResponse(void)
{
return (_Response);
return (_response);
}
std::string Client::getStrToSend(void)
{
return (_toSend);
}
int Client::getFd(void)
@@ -68,23 +76,55 @@ int Client::sendData(int fd, std::string data)
{
return (send(fd, data.c_str(), data.length(), 0));
}
//Возвращает истина, если запрос chunked.
bool Client::isChunked(void)
{
return (_request.getChunked());
}
// Возвращает истина, если отправлены все данные клиента.
bool Client::allSended(void)
{
if (_toSend.size() <= _sended)
return (true);
return (false);
}
// Функция увеличивает счетчик на количество BUFFERSIZE. Счетчик - количество байтов отправленных клиенту.
void Client::increaseCounter(void)
{
_sended += BUFFSIZE;
}
//Генерирует response. Далее респонс можно получить через функцию getStrToSend()
int Client::sendResponse(int fd)
{
_Response.setData(_request, _config);
_Response.generate();
_ClientToSend = _Response.getClient();
_bodyToSend = _Response.getBody();
_ret = sendData(fd, _ClientToSend);
_ret = sendData(fd, _bodyToSend);
_response.setData(_request, _config);
_response.generate();
_headerToSend = _response.getClient();
_bodyToSend = _response.getBody();
_ret = sendData(fd, _headerToSend + _bodyToSend);
return (_ret);
}
std::string Client::generateRespons(void)
{
_response.setData(_request, _config);
_response.generate();
_headerToSend = _response.getClient();
_bodyToSend = _response.getBody();
_toSend = _headerToSend + _bodyToSend;
return (_toSend);
}
//-------------------------------------------------Error---------------------------------------
//-------------------------------------------------Other---------------------------------------
bool Client::readyToSend(void)
{
return(_request.ok());
}
void Client::printClientInfo(void)
{
std::map<std::string, std::string> map;
@@ -92,19 +132,20 @@ void Client::printClientInfo(void)
std::map<std::string, std::string>::iterator it1;
map = _request.getClientFields();
/* std::cout << PINK << "request method = " << _request.getMethod() << ZERO_C << std::endl; */
/* std::cout << PINK << "request URI = " << _request.getURI() << ZERO_C << std::endl; */
/* std::cout << PINK << "host = " << _request.getHost() << ZERO_C << std::endl; */
/* std::cout << PINK << "request query = " << _request.getQuery() << ZERO_C << std::endl; */
/* std::cout << PINK << "request http versioin = " << _request.getVersion() << ZERO_C << std::endl; */
std::cout << YELLOW << "Client request dump:\n" << _buff << ZERO_C << std::endl;
std::cout << PINK << "request method = " << _request.getMethod() << ZERO_C << std::endl;
std::cout << PINK << "request URI = " << _request.getURI() << ZERO_C << std::endl;
std::cout << PINK << "host = " << _request.getHost() << ZERO_C << std::endl;
std::cout << PINK << "request query = " << _request.getQuery() << ZERO_C << std::endl;
std::cout << PINK << "request http versioin = " << _request.getVersion() << ZERO_C << std::endl;
// std::cout << YELLOW << "request Client:\n" << _buff << ZERO_C << std::endl;
/* std::cout << TURGUOISE << "Client MAP" << ZERO_C << std::endl; */
/* for ( it = map.begin(); it != map.end() ; it++) */
/* { */
/* std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl; */
/* // std::cout << "1\n"; */
/* } */
std::cout << TURGUOISE << "Client MAP" << ZERO_C << std::endl;
for ( it = map.begin(); it != map.end() ; it++)
{
std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl;
}
std::cout << TURGUOISE << "Client BODY" << ZERO_C << std::endl;
std::cout << BLUE << _request.getBody() << ZERO_C << std::endl;
}
@@ -116,6 +157,9 @@ void Client::clear(void)
_request.clear();
_buff = NULL;
_config = NULL;
_bodyToSend = "";
_headerToSend = "";
_toSend = "";
}
Client::~Client()