mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 13:27:59 +03:00
update class client, request
This commit is contained in:
@@ -5,12 +5,14 @@
|
|||||||
Client::Client()
|
Client::Client()
|
||||||
{
|
{
|
||||||
this->_fd = -1;
|
this->_fd = -1;
|
||||||
|
this->_sended = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::Client(char *str)
|
Client::Client(char *str)
|
||||||
{
|
{
|
||||||
this->_fd = -1;
|
this->_fd = -1;
|
||||||
this->_buff = str;
|
this->_buff = str;
|
||||||
|
this->_sended = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,6 +21,7 @@ Client::Client(char *str, ServerConfig *config)
|
|||||||
this->_fd = -1;
|
this->_fd = -1;
|
||||||
this->_config = config;
|
this->_config = config;
|
||||||
this->_buff = str;
|
this->_buff = str;
|
||||||
|
this->_sended = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +34,12 @@ Request Client::getRequest(void)
|
|||||||
|
|
||||||
Response Client::getResponse(void)
|
Response Client::getResponse(void)
|
||||||
{
|
{
|
||||||
return (_Response);
|
return (_response);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Client::getStrToSend(void)
|
||||||
|
{
|
||||||
|
return (_toSend);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Client::getFd(void)
|
int Client::getFd(void)
|
||||||
@@ -49,6 +57,7 @@ void Client::setFd(int fd)
|
|||||||
this->_fd = fd;
|
this->_fd = fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-------------------------------------------------Parsing---------------------------------------
|
//-------------------------------------------------Parsing---------------------------------------
|
||||||
|
|
||||||
int Client::parseRequest(void)
|
int Client::parseRequest(void)
|
||||||
@@ -68,23 +77,55 @@ int Client::sendData(int fd, std::string data)
|
|||||||
{
|
{
|
||||||
return (send(fd, data.c_str(), data.length(), 0));
|
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)
|
int Client::sendResponse(int fd)
|
||||||
{
|
{
|
||||||
_Response.setData(_request, _config);
|
_response.setData(_request, _config);
|
||||||
_Response.generate();
|
_response.generate();
|
||||||
_ClientToSend = _Response.getClient();
|
_headerToSend = _response.getClient();
|
||||||
_bodyToSend = _Response.getBody();
|
_bodyToSend = _response.getBody();
|
||||||
_ret = sendData(fd, _ClientToSend);
|
_ret = sendData(fd, _headerToSend + _bodyToSend);
|
||||||
_ret = sendData(fd, _bodyToSend);
|
|
||||||
|
|
||||||
return (_ret);
|
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---------------------------------------
|
//-------------------------------------------------Error---------------------------------------
|
||||||
|
|
||||||
//-------------------------------------------------Other---------------------------------------
|
//-------------------------------------------------Other---------------------------------------
|
||||||
|
|
||||||
|
bool Client::readyToSend(void)
|
||||||
|
{
|
||||||
|
return(_request.ok());
|
||||||
|
}
|
||||||
|
|
||||||
void Client::printClientInfo(void)
|
void Client::printClientInfo(void)
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string> map;
|
std::map<std::string, std::string> map;
|
||||||
@@ -97,14 +138,15 @@ void Client::printClientInfo(void)
|
|||||||
std::cout << PINK << "host = " << _request.getHost() << 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 query = " << _request.getQuery() << ZERO_C << std::endl;
|
||||||
std::cout << PINK << "request http versioin = " << _request.getVersion() << 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 << YELLOW << "request Client:\n" << _buff << ZERO_C << std::endl;
|
||||||
|
|
||||||
std::cout << TURGUOISE << "Client MAP" << ZERO_C << std::endl;
|
std::cout << TURGUOISE << "Client MAP" << ZERO_C << std::endl;
|
||||||
for ( it = map.begin(); it != map.end() ; it++)
|
for ( it = map.begin(); it != map.end() ; it++)
|
||||||
{
|
{
|
||||||
std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl;
|
std::cout << PINK << it->first << BLUE << it->second << ZERO_C << std::endl;
|
||||||
// std::cout << "1\n";
|
|
||||||
}
|
}
|
||||||
|
std::cout << TURGUOISE << "Client BODY" << ZERO_C << std::endl;
|
||||||
|
std::cout << BLUE << _request.getBody() << ZERO_C << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,6 +158,9 @@ void Client::clear(void)
|
|||||||
_request.clear();
|
_request.clear();
|
||||||
_buff = NULL;
|
_buff = NULL;
|
||||||
_config = NULL;
|
_config = NULL;
|
||||||
|
_bodyToSend = "";
|
||||||
|
_headerToSend = "";
|
||||||
|
_toSend = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::~Client()
|
Client::~Client()
|
||||||
|
|||||||
@@ -11,22 +11,25 @@ class Client
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Request _request;
|
Request _request;
|
||||||
Response _Response;
|
Response _response;
|
||||||
ServerConfig *_config;
|
ServerConfig *_config;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _ret;
|
int _ret;
|
||||||
int _fd;
|
int _fd;
|
||||||
|
unsigned int _sended;
|
||||||
char *_buff;
|
char *_buff;
|
||||||
|
|
||||||
|
|
||||||
std::string _bodyToSend;
|
std::string _bodyToSend;
|
||||||
std::string _ClientToSend;
|
std::string _headerToSend;
|
||||||
|
std::string _toSend;
|
||||||
std::map<std::string, std::string> _errorCode;
|
std::map<std::string, std::string> _errorCode;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Request getRequest(void);
|
Request getRequest(void);
|
||||||
Response getResponse(void);
|
Response getResponse(void);
|
||||||
|
std::string getStrToSend(void);
|
||||||
void setRawData(char *);
|
void setRawData(char *);
|
||||||
void setFd(int);
|
void setFd(int);
|
||||||
int getFd(void);
|
int getFd(void);
|
||||||
@@ -36,9 +39,14 @@ public:
|
|||||||
|
|
||||||
void printClientInfo(void);
|
void printClientInfo(void);
|
||||||
|
|
||||||
|
bool readyToSend(void);
|
||||||
|
bool allSended(void);
|
||||||
|
bool isChunked(void);
|
||||||
int sendResponse(int fd);
|
int sendResponse(int fd);
|
||||||
int sendData(int , std::string data);
|
int sendData(int , std::string data);
|
||||||
void clear(void);
|
void clear(void);
|
||||||
|
void increaseCounter(void);
|
||||||
|
std::string generateRespons(void);
|
||||||
|
|
||||||
Client();
|
Client();
|
||||||
Client(char *);
|
Client(char *);
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ Request::Request()
|
|||||||
{
|
{
|
||||||
_row = 0;
|
_row = 0;
|
||||||
_ret = 200;
|
_ret = 200;
|
||||||
|
_contentLength = 0;
|
||||||
|
_chunked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Request::Request(char *str)
|
Request::Request(char *str)
|
||||||
@@ -13,6 +15,10 @@ Request::Request(char *str)
|
|||||||
_row = 0;
|
_row = 0;
|
||||||
_ret = 200;
|
_ret = 200;
|
||||||
_data = str;
|
_data = str;
|
||||||
|
_head_ok = false;
|
||||||
|
_body_ok = false;
|
||||||
|
_chunked = false;
|
||||||
|
_contentLength = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------Get/Set---------------------------------------
|
//-------------------------------------------------Get/Set---------------------------------------
|
||||||
@@ -59,9 +65,12 @@ int Request::getCode(void)
|
|||||||
}
|
}
|
||||||
std::map<std::string, std::string> Request::getClientFields(void)
|
std::map<std::string, std::string> Request::getClientFields(void)
|
||||||
{
|
{
|
||||||
return (_ClientField);
|
return (_headerField);
|
||||||
|
}
|
||||||
|
bool Request::getChunked(void)
|
||||||
|
{
|
||||||
|
return (_chunked);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::setData(char *str)
|
void Request::setData(char *str)
|
||||||
{
|
{
|
||||||
this->_data = str;
|
this->_data = str;
|
||||||
@@ -118,6 +127,37 @@ int Request::parseStartLine(std::string str)
|
|||||||
return (_ret);
|
return (_ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Request::splitData(char *data)
|
||||||
|
{
|
||||||
|
int pos;
|
||||||
|
std::string str;
|
||||||
|
|
||||||
|
str = data;
|
||||||
|
if (!_head_ok)
|
||||||
|
{
|
||||||
|
pos = str.find("\r\n\r\n");
|
||||||
|
if (pos == -1)
|
||||||
|
{
|
||||||
|
_ret = 400;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_head = str.substr(0, pos) + "\n";
|
||||||
|
str.erase(0, pos + 4);
|
||||||
|
_head_ok = true;
|
||||||
|
parseHeader();
|
||||||
|
if (_contentLength == 0)
|
||||||
|
_body_ok = true;
|
||||||
|
}
|
||||||
|
if (badCode(_ret))
|
||||||
|
return ;
|
||||||
|
else if (!_body_ok)
|
||||||
|
{
|
||||||
|
_body += str.substr(0, str.size());
|
||||||
|
if (_body.size() >= _contentLength)
|
||||||
|
_body_ok = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int Request::parseClientfield(std::string str)
|
int Request::parseClientfield(std::string str)
|
||||||
{
|
{
|
||||||
int distance;
|
int distance;
|
||||||
@@ -130,7 +170,7 @@ int Request::parseClientfield(std::string str)
|
|||||||
key = str.substr(0, distance);
|
key = str.substr(0, distance);
|
||||||
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
|
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
|
||||||
value = str.erase(0, distance + 1);
|
value = str.erase(0, distance + 1);
|
||||||
if (_ClientField.find(key) != _ClientField.end())
|
if (_headerField.find(key) != _headerField.end())
|
||||||
{
|
{
|
||||||
std::cout << RED << "ERROR: double Client-field" << ZERO_C << std::endl;
|
std::cout << RED << "ERROR: double Client-field" << ZERO_C << std::endl;
|
||||||
}
|
}
|
||||||
@@ -138,17 +178,17 @@ int Request::parseClientfield(std::string str)
|
|||||||
{
|
{
|
||||||
value = value.erase(0, value.find_first_not_of(WHITESPACE));
|
value = value.erase(0, value.find_first_not_of(WHITESPACE));
|
||||||
value = value.substr(0, value.find_last_not_of(WHITESPACE) + 1);
|
value = value.substr(0, value.find_last_not_of(WHITESPACE) + 1);
|
||||||
_ClientField[key] = value;
|
_headerField[key] = value;
|
||||||
}
|
}
|
||||||
return 200;
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Request::parseRequest(void)
|
int Request::parseHeader(void)
|
||||||
{
|
{
|
||||||
std::string line;
|
std::string line;
|
||||||
std::stringstream buffStream;
|
std::stringstream buffStream;
|
||||||
|
|
||||||
buffStream << _data;
|
buffStream << _head;
|
||||||
_ret = 200;
|
_ret = 200;
|
||||||
while (std::getline(buffStream, line, '\n') && !badCode(_ret))
|
while (std::getline(buffStream, line, '\n') && !badCode(_ret))
|
||||||
{
|
{
|
||||||
@@ -163,14 +203,39 @@ int Request::parseRequest(void)
|
|||||||
return (_ret);
|
return (_ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Request::parseRequest(void)
|
||||||
|
{
|
||||||
|
if (!_head_ok || !_body_ok)
|
||||||
|
splitData(_data);
|
||||||
|
|
||||||
|
return (_ret);
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------------------------------------Utils---------------------------------------
|
//-------------------------------------------------Utils---------------------------------------
|
||||||
|
|
||||||
|
|
||||||
void Request::copyFromMap()
|
void Request::copyFromMap()
|
||||||
{
|
{
|
||||||
_host = _ClientField.find("host")->second;
|
std::map<std::string, std::string>::iterator it;
|
||||||
|
int pos;
|
||||||
|
//host
|
||||||
|
_host = _headerField.find("host")->second;
|
||||||
|
|
||||||
|
//content-lenght
|
||||||
|
it = _headerField.find("content-length");
|
||||||
|
if (it != _headerField.end())
|
||||||
|
_contentLength = atoi(it->second.c_str());
|
||||||
|
//chunked
|
||||||
|
it = _headerField.find("transfer-encoding");
|
||||||
|
if (it != _headerField.end())
|
||||||
|
{
|
||||||
|
pos = it->second.find("chunked");
|
||||||
|
if ( pos != -1)
|
||||||
|
_chunked = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Request::badCode(int code)
|
bool Request::badCode(int code)
|
||||||
{
|
{
|
||||||
if (code == 200)
|
if (code == 200)
|
||||||
@@ -178,6 +243,11 @@ bool Request::badCode(int code)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Request::ok(void)
|
||||||
|
{
|
||||||
|
return (_head_ok && _body_ok);
|
||||||
|
}
|
||||||
|
|
||||||
int Request::isFile(std::string path)
|
int Request::isFile(std::string path)
|
||||||
{
|
{
|
||||||
struct stat s;
|
struct stat s;
|
||||||
@@ -222,7 +292,10 @@ void Request::clear(void)
|
|||||||
_fullURI = "";
|
_fullURI = "";
|
||||||
_version = "";
|
_version = "";
|
||||||
_location = "";
|
_location = "";
|
||||||
_ClientField.clear();
|
_head = "";
|
||||||
|
_head_ok = false;
|
||||||
|
_body_ok = false;
|
||||||
|
_headerField.clear();
|
||||||
_data = NULL;
|
_data = NULL;
|
||||||
_config = NULL;
|
_config = NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,10 @@ private:
|
|||||||
|
|
||||||
int _ret;
|
int _ret;
|
||||||
int _row;
|
int _row;
|
||||||
|
unsigned int _contentLength;
|
||||||
|
|
||||||
std::string _URI;
|
std::string _URI;
|
||||||
|
std::string _head;
|
||||||
std::string _body;
|
std::string _body;
|
||||||
std::string _host;
|
std::string _host;
|
||||||
std::string _query;
|
std::string _query;
|
||||||
@@ -21,10 +23,12 @@ private:
|
|||||||
std::string _fullURI;
|
std::string _fullURI;
|
||||||
std::string _version;
|
std::string _version;
|
||||||
std::string _location;
|
std::string _location;
|
||||||
std::map<std::string, std::string> _ClientField;
|
std::map<std::string, std::string> _headerField;
|
||||||
|
|
||||||
ServerConfig *_config;
|
ServerConfig *_config;
|
||||||
|
bool _head_ok;
|
||||||
|
bool _body_ok;
|
||||||
|
bool _chunked;
|
||||||
public:
|
public:
|
||||||
std::string getURI(void);
|
std::string getURI(void);
|
||||||
std::string getBody(void);
|
std::string getBody(void);
|
||||||
@@ -37,6 +41,7 @@ public:
|
|||||||
ServerConfig *getConfig(void);
|
ServerConfig *getConfig(void);
|
||||||
int getCode(void);
|
int getCode(void);
|
||||||
std::map<std::string, std::string> getClientFields(void);
|
std::map<std::string, std::string> getClientFields(void);
|
||||||
|
bool getChunked(void);
|
||||||
|
|
||||||
void setConfig(ServerConfig *config);
|
void setConfig(ServerConfig *config);
|
||||||
void setData(char *);
|
void setData(char *);
|
||||||
@@ -49,15 +54,19 @@ public:
|
|||||||
int parseStartLine(std::string);
|
int parseStartLine(std::string);
|
||||||
int parseClientfield(std::string);
|
int parseClientfield(std::string);
|
||||||
int parseRequest(void);
|
int parseRequest(void);
|
||||||
|
int parseHeader(void);
|
||||||
void parseURI(std::string);
|
void parseURI(std::string);
|
||||||
void printClientInfo(void);
|
void printClientInfo(void);
|
||||||
|
|
||||||
|
|
||||||
bool badCode(int);
|
bool badCode(int);
|
||||||
|
bool ok(void);
|
||||||
int isDir(std::string path);
|
int isDir(std::string path);
|
||||||
int isFile(std::string path);
|
int isFile(std::string path);
|
||||||
bool autoindexOn(void);
|
bool autoindexOn(void);
|
||||||
void copyFromMap(void);
|
void copyFromMap(void);
|
||||||
void clear(void);
|
void clear(void);
|
||||||
|
void splitData(char *);
|
||||||
|
|
||||||
~Request();
|
~Request();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user