From 307e0abeac24a1e2df3d59aa92e1d8007497e7a3 Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sun, 30 Jan 2022 13:54:03 +0300 Subject: [PATCH 1/5] feat: allRead --- src/Client/Client.cpp | 6 +++++- src/Client/Client.hpp | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index 7fa2eb6..af1e0b9 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -4,12 +4,14 @@ Client::Client() { + allRead = false; this->_fd = -1; this->_sended = 0; } Client::Client(char *str) { + allRead = false; this->_fd = -1; this->_buff = str; this->_sended = 0; @@ -18,6 +20,7 @@ Client::Client(char *str) Client::Client(char *str, ServerConfig *config) { + allRead = false; this->_fd = -1; this->_config = config; this->_buff = str; @@ -150,7 +153,8 @@ void Client::printClientInfo(void) 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; + std::cout << BLUE << _request.getBody().size() << ZERO_C << std::endl; + /* std::cout << BLUE << _request.getBody() << ZERO_C << std::endl; */ } diff --git a/src/Client/Client.hpp b/src/Client/Client.hpp index 503bf6f..6c74706 100644 --- a/src/Client/Client.hpp +++ b/src/Client/Client.hpp @@ -25,6 +25,9 @@ private: std::string _headerToSend; std::string _toSend; std::map _errorCode; + +public: + bool allRead; public: Request getRequest(void); @@ -35,6 +38,7 @@ public: void setFd(int); int getFd(void); + public: int parseRequest(void); From a3abe505eb1a37a7af0539500337d3b2f7aeff15 Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sun, 30 Jan 2022 13:54:47 +0300 Subject: [PATCH 2/5] feat: add readSocket --- src/Server/Server.cpp | 34 +++++++++++++++++++++++++++------- src/Server/Server.hpp | 1 + 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 316c140..3797c84 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -66,6 +66,28 @@ void Server::sendData(Client &client, int fd) } +void Server::readSocket(int fd, std::map &client_map) +{ + + int status; + int bytes_read; + char buf[BUFFSIZE + 1] = {0}; + + bytes_read = recv(fd, buf, BUFFSIZE, 0); + if (bytes_read < BUFFSIZE) + { + client_map[fd].allRead = true; + if (bytes_read == 0) + return; + } + client_map[fd].setRawData(buf); + status = client_map[fd].parseRequest(); + client_map[fd].printClientInfo(); + + std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl; + bzero(buf, BUFFSIZE); +} + void Server::setupConfig(void) { this->_ip = "127.0.0.1"; @@ -96,11 +118,9 @@ void Server::add_to_epoll_list(int fd, unsigned int ep_events) void Server::start(void) { Socket server_sock(AF_INET, SOCK_STREAM, 0, _port, "127.0.0.1"); - char buf[BUFFSIZE + 1] = {0}; std::map client_map; std::map::iterator client_it; int fd; - int status; int ready_num = 0; unsigned int client_events = EPOLLIN | EPOLLOUT | EPOLLET; @@ -123,6 +143,10 @@ void Server::start(void) std::cout << TURQ << "IN SEND LOOP" << RESET << std::endl; Client &client = client_it->second; + if (!client.allRead) + { + readSocket(client_it->first, client_map); + } if (client.readyToSend()) { epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, client_it->first, NULL); @@ -169,12 +193,8 @@ void Server::start(void) else { 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--; */ + readSocket(fd, client_map); } } ready_num = 0; diff --git a/src/Server/Server.hpp b/src/Server/Server.hpp index 4ec4316..e418a9d 100644 --- a/src/Server/Server.hpp +++ b/src/Server/Server.hpp @@ -28,6 +28,7 @@ private: void sendResponse(Client head, int); void setNonBlock(int fd); void sendData(Client &client, int fd); + void readSocket(int fd, std::map &client_map); public: Server(); Server(std::string path); From 0d8b0e76368a87a695a301a04e80617aeda78228 Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sun, 30 Jan 2022 15:26:01 +0300 Subject: [PATCH 3/5] feat: add recvCounter, allRecved --- src/Client/Client.cpp | 32 +++++++++++++++++++++++++++++++- src/Client/Client.hpp | 4 ++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index af1e0b9..5fe9c94 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -5,6 +5,7 @@ Client::Client() { allRead = false; + _received = 0; this->_fd = -1; this->_sended = 0; } @@ -12,6 +13,7 @@ Client::Client() Client::Client(char *str) { allRead = false; + _received = 0; this->_fd = -1; this->_buff = str; this->_sended = 0; @@ -21,6 +23,7 @@ Client::Client(char *str) Client::Client(char *str, ServerConfig *config) { allRead = false; + _received = 0; this->_fd = -1; this->_config = config; this->_buff = str; @@ -55,6 +58,11 @@ unsigned int Client::getCounter(void) const return _sended; } +unsigned int Client::getRecvCounter(void) const +{ + return _received; +} + void Client::setRawData(char *str) { this->_buff = str; @@ -96,11 +104,33 @@ bool Client::allSended(void) return (true); 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. Счетчик - количество байтов отправленных клиенту. void Client::increaseCounter(void) { _sended += BUFFSIZE; } + +void Client::increaseRecvCounter(unsigned int n) +{ + if (_received == 0) + _received -= _request.getHeaderSize(); + _received += n; +} + //Генерирует response. Далее респонс можно получить через функцию getStrToSend() int Client::sendResponse(int fd) { @@ -153,7 +183,7 @@ void Client::printClientInfo(void) 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().size() << ZERO_C << std::endl; + std::cout << GREEN << _request.getBody().size() << ZERO_C << std::endl; /* std::cout << BLUE << _request.getBody() << ZERO_C << std::endl; */ } diff --git a/src/Client/Client.hpp b/src/Client/Client.hpp index 6c74706..fcc290f 100644 --- a/src/Client/Client.hpp +++ b/src/Client/Client.hpp @@ -18,6 +18,7 @@ private: int _ret; int _fd; unsigned int _sended; + unsigned int _received; char *_buff; @@ -37,6 +38,7 @@ public: void setRawData(char *); void setFd(int); int getFd(void); + unsigned int getRecvCounter(void) const; public: @@ -46,11 +48,13 @@ public: bool readyToSend(void); bool allSended(void); + bool allRecved(void); bool isChunked(void); int sendResponse(int fd); int sendData(int , std::string data); void clear(void); void increaseCounter(void); + void increaseRecvCounter(unsigned int n); std::string generateRespons(void); Client(); From cba4c8adf3e3cb9c3cff578062779df600c14221 Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sun, 30 Jan 2022 15:27:10 +0300 Subject: [PATCH 4/5] feat: add headersize --- src/Client/Request.cpp | 13 +++++++++++++ src/Client/Request.hpp | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Client/Request.cpp b/src/Client/Request.cpp index b4a60fd..7664df1 100644 --- a/src/Client/Request.cpp +++ b/src/Client/Request.cpp @@ -8,6 +8,8 @@ Request::Request() _ret = 200; _contentLength = 0; _chunked = false; + _head_ok = false; + _body_ok = false; } Request::Request(char *str) @@ -71,6 +73,16 @@ bool Request::getChunked(void) { return (_chunked); } + +unsigned int Request::getContentLength(void) const +{ + return (_contentLength); +} +unsigned int Request::getHeaderSize(void) const +{ + return (_headerSize); +} + void Request::setData(char *str) { this->_data = str; @@ -142,6 +154,7 @@ void Request::splitData(char *data) return; } _head = str.substr(0, pos) + "\n"; + _headerSize = _head.size() + 3; str.erase(0, pos + 4); _head_ok = true; parseHeader(); diff --git a/src/Client/Request.hpp b/src/Client/Request.hpp index 850704e..56528d4 100644 --- a/src/Client/Request.hpp +++ b/src/Client/Request.hpp @@ -13,6 +13,7 @@ private: int _ret; int _row; unsigned int _contentLength; + unsigned int _headerSize; std::string _URI; std::string _head; @@ -40,6 +41,8 @@ public: std::string getLocation(void); ServerConfig *getConfig(void); int getCode(void); + unsigned int getContentLength(void) const; + unsigned int getHeaderSize(void) const; std::map getClientFields(void); bool getChunked(void); @@ -73,4 +76,4 @@ public: -#endif \ No newline at end of file +#endif From 7ce1c43758129d0d21fc20de6950d2a444b44a23 Mon Sep 17 00:00:00 2001 From: 3lswear Date: Sun, 30 Jan 2022 15:28:24 +0300 Subject: [PATCH 5/5] fix: readSocket --- src/Server/Server.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 3797c84..7f69826 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -10,6 +10,7 @@ //----------------------------------------------Constructors----------------------------------------------------------------------------------- Server::Server() { + bzero(_events, sizeof(_events)); _client = 0; } @@ -73,17 +74,28 @@ void Server::readSocket(int fd, std::map &client_map) 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 < BUFFSIZE) + if (bytes_read == 0) { client_map[fd].allRead = true; - if (bytes_read == 0) - return; + 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); } @@ -157,7 +169,7 @@ void Server::start(void) } - if (client.allSended()) + if (client.readyToSend() && client.allSended()) { close(client_it->first); std::cerr << RED << @@ -180,6 +192,10 @@ void Server::start(void) /* if (_events[i].events == 0) */ /* continue; */ fd = _events[i].data.fd; + /* if (_events[i].data.fd == 0) */ + /* { */ + /* continue; */ + /* } */ if (fd == server_sock.getSocketFd()) {