merge from roman

This commit is contained in:
Talyx
2022-01-30 17:51:19 +03:00
6 changed files with 103 additions and 11 deletions

View File

@@ -4,12 +4,16 @@
Client::Client() Client::Client()
{ {
allRead = false;
_received = 0;
this->_fd = -1; this->_fd = -1;
this->_sended = 0; this->_sended = 0;
} }
Client::Client(char *str) Client::Client(char *str)
{ {
allRead = false;
_received = 0;
this->_fd = -1; this->_fd = -1;
this->_buff = str; this->_buff = str;
this->_sended = 0; this->_sended = 0;
@@ -18,6 +22,8 @@ Client::Client(char *str)
Client::Client(char *str, ServerConfig *config) Client::Client(char *str, ServerConfig *config)
{ {
allRead = false;
_received = 0;
this->_fd = -1; this->_fd = -1;
this->_config = config; this->_config = config;
this->_buff = str; this->_buff = str;
@@ -52,6 +58,11 @@ unsigned int Client::getCounter(void) const
return _sended; return _sended;
} }
unsigned int Client::getRecvCounter(void) const
{
return _received;
}
void Client::setRawData(char *str) void Client::setRawData(char *str)
{ {
this->_buff = str; this->_buff = str;
@@ -93,11 +104,33 @@ bool Client::allSended(void)
return (true); return (true);
return (false); return (false);
} }
bool Client::allRecved(void)
{
if (_request.getContentLength() == _received)
{
std::cout << "contentLength, _received "
<< _request.getContentLength()
<< " " <<
_received << std::endl;
return (true);
}
else
return (false);
}
// Функция увеличивает счетчик на количество BUFFERSIZE. Счетчик - количество байтов отправленных клиенту. // Функция увеличивает счетчик на количество BUFFERSIZE. Счетчик - количество байтов отправленных клиенту.
void Client::increaseCounter(void) void Client::increaseCounter(void)
{ {
_sended += BUFFSIZE; _sended += BUFFSIZE;
} }
void Client::increaseRecvCounter(unsigned int n)
{
if (_received == 0)
_received -= _request.getHeaderSize();
_received += n;
}
//Генерирует response. Далее респонс можно получить через функцию getStrToSend() //Генерирует response. Далее респонс можно получить через функцию getStrToSend()
int Client::sendResponse(int fd) int Client::sendResponse(int fd)
{ {
@@ -150,7 +183,8 @@ void Client::printClientInfo(void)
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 << TURGUOISE << "Client BODY" << ZERO_C << std::endl; std::cout << TURGUOISE << "Client BODY" << ZERO_C << std::endl;
std::cout << BLUE << _request.getBody() << ZERO_C << std::endl; std::cout << GREEN << _request.getBody().size() << ZERO_C << std::endl;
/* std::cout << BLUE << _request.getBody() << ZERO_C << std::endl; */
} }

View File

@@ -18,6 +18,7 @@ private:
int _ret; int _ret;
int _fd; int _fd;
unsigned int _sended; unsigned int _sended;
unsigned int _received;
char *_buff; char *_buff;
@@ -26,6 +27,9 @@ private:
std::string _toSend; std::string _toSend;
std::map<std::string, std::string> _errorCode; std::map<std::string, std::string> _errorCode;
public:
bool allRead;
public: public:
Request getRequest(void); Request getRequest(void);
Response getResponse(void); Response getResponse(void);
@@ -34,6 +38,8 @@ public:
void setRawData(char *); void setRawData(char *);
void setFd(int); void setFd(int);
int getFd(void); int getFd(void);
unsigned int getRecvCounter(void) const;
public: public:
int parseRequest(void); int parseRequest(void);
@@ -42,11 +48,13 @@ public:
bool readyToSend(void); bool readyToSend(void);
bool allSended(void); bool allSended(void);
bool allRecved(void);
bool isChunked(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); void increaseCounter(void);
void increaseRecvCounter(unsigned int n);
std::string generateRespons(void); std::string generateRespons(void);
Client(); Client();

View File

@@ -10,7 +10,6 @@ Request::Request()
_chunked = false; _chunked = false;
_head_ok = false; _head_ok = false;
_body_ok = false; _body_ok = false;
} }
Request::Request(char *str) Request::Request(char *str)
@@ -74,6 +73,16 @@ bool Request::getChunked(void)
{ {
return (_chunked); return (_chunked);
} }
unsigned int Request::getContentLength(void) const
{
return (_contentLength);
}
unsigned int Request::getHeaderSize(void) const
{
return (_headerSize);
}
void Request::setData(char *str) void Request::setData(char *str)
{ {
this->_data = str; this->_data = str;
@@ -145,6 +154,7 @@ void Request::splitData(char *data)
return; return;
} }
_head = str.substr(0, pos) + "\n"; _head = str.substr(0, pos) + "\n";
_headerSize = _head.size() + 3;
str.erase(0, pos + 4); str.erase(0, pos + 4);
_head_ok = true; _head_ok = true;
parseHeader(); parseHeader();

View File

@@ -13,6 +13,7 @@ private:
int _ret; int _ret;
int _row; int _row;
unsigned int _contentLength; unsigned int _contentLength;
unsigned int _headerSize;
std::string _URI; std::string _URI;
std::string _head; std::string _head;
@@ -40,6 +41,8 @@ public:
std::string getLocation(void); std::string getLocation(void);
ServerConfig *getConfig(void); ServerConfig *getConfig(void);
int getCode(void); int getCode(void);
unsigned int getContentLength(void) const;
unsigned int getHeaderSize(void) const;
std::map<std::string, std::string> getClientFields(void); std::map<std::string, std::string> getClientFields(void);
bool getChunked(void); bool getChunked(void);

View File

@@ -10,6 +10,7 @@
//----------------------------------------------Constructors----------------------------------------------------------------------------------- //----------------------------------------------Constructors-----------------------------------------------------------------------------------
Server::Server() Server::Server()
{ {
bzero(_events, sizeof(_events));
_client = 0; _client = 0;
} }
@@ -66,6 +67,39 @@ void Server::sendData(Client &client, int fd)
} }
void Server::readSocket(int fd, std::map<int, Client> &client_map)
{
int status;
int bytes_read;
char buf[BUFFSIZE + 1] = {0};
std::cout << TURQ << "IN readSocket" << RESET << std::endl;
bytes_read = recv(fd, buf, BUFFSIZE, 0);
if (bytes_read == 0)
{
client_map[fd].allRead = true;
return;
}
client_map[fd].setRawData(buf);
status = client_map[fd].parseRequest();
client_map[fd].increaseRecvCounter(bytes_read);
client_map[fd].printClientInfo();
if ((bytes_read < BUFFSIZE) && client_map[fd].allRecved())
{
client_map[fd].allRead = true;
}
std::cerr << "bytes_read " << bytes_read << std::endl;;
std::cerr << "recvCounter " << client_map[fd].getRecvCounter() << std::endl;;
std::cerr << "contentLength " << client_map[fd].getRequest().getContentLength() << std::endl;
std::cerr << "allRead " << client_map[fd].allRead << std::endl;;
std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
bzero(buf, BUFFSIZE);
}
void Server::setupConfig(void) void Server::setupConfig(void)
{ {
this->_ip = "127.0.0.1"; this->_ip = "127.0.0.1";
@@ -96,11 +130,9 @@ void Server::add_to_epoll_list(int fd, unsigned int ep_events)
void Server::start(void) void Server::start(void)
{ {
Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1"); Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1");
char buf[BUFFSIZE + 1] = {0};
std::map<int, Client> client_map; std::map<int, Client> client_map;
std::map<int, Client>::iterator client_it; std::map<int, Client>::iterator client_it;
int fd; int fd;
int status;
int ready_num = 0; int ready_num = 0;
unsigned int client_events = EPOLLIN | EPOLLOUT | EPOLLET; unsigned int client_events = EPOLLIN | EPOLLOUT | EPOLLET;
@@ -123,6 +155,10 @@ void Server::start(void)
std::cout << TURQ << "IN SEND LOOP" << RESET << std::endl; std::cout << TURQ << "IN SEND LOOP" << RESET << std::endl;
Client &client = client_it->second; Client &client = client_it->second;
if (!client.allRead)
{
readSocket(client_it->first, client_map);
}
if (client.readyToSend()) if (client.readyToSend())
{ {
epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, client_it->first, NULL); epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, client_it->first, NULL);
@@ -133,7 +169,7 @@ void Server::start(void)
} }
if (client.allSended()) if (client.readyToSend() && client.allSended())
{ {
close(client_it->first); close(client_it->first);
std::cerr << RED << std::cerr << RED <<
@@ -156,6 +192,10 @@ void Server::start(void)
/* if (_events[i].events == 0) */ /* if (_events[i].events == 0) */
/* continue; */ /* continue; */
fd = _events[i].data.fd; fd = _events[i].data.fd;
/* if (_events[i].data.fd == 0) */
/* { */
/* continue; */
/* } */
if (fd == server_sock.getSocketFd()) if (fd == server_sock.getSocketFd())
{ {
@@ -169,12 +209,8 @@ void Server::start(void)
else else
{ {
std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl; std::cout << TURQ << "IN FOR LOOP" << RESET << std::endl;
assert(recv(fd, buf, BUFFSIZE, 0) >= 0);
client_map[fd].setRawData(buf);
status = client_map[fd].parseRequest();
std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
bzero(buf, BUFFSIZE);
/* _client--; */ /* _client--; */
readSocket(fd, client_map);
} }
} }
ready_num = 0; ready_num = 0;

View File

@@ -28,6 +28,7 @@ private:
void sendResponse(Client head, int); void sendResponse(Client head, int);
void setNonBlock(int fd); void setNonBlock(int fd);
void sendData(Client &client, int fd); void sendData(Client &client, int fd);
void readSocket(int fd, std::map<int, Client> &client_map);
public: public:
Server(); Server();
Server(std::string path); Server(std::string path);