diff --git a/src/Client/Client.cpp b/src/Client/Client.cpp index 010342c..57833c3 100644 --- a/src/Client/Client.cpp +++ b/src/Client/Client.cpp @@ -5,7 +5,6 @@ Client::Client() { allRead = false; - _received = 0; this->_fd = -1; this->_sended = 0; } @@ -13,7 +12,6 @@ Client::Client() Client::Client(char *str) { allRead = false; - _received = 0; this->_fd = -1; this->_buff = str; this->_sended = 0; @@ -23,7 +21,6 @@ Client::Client(char *str) Client::Client(char *str, ServerConfig *config) { allRead = false; - _received = 0; this->_fd = -1; this->_config = config; this->_buff = str; @@ -60,7 +57,7 @@ unsigned int Client::getCounter(void) const unsigned int Client::getRecvCounter(void) const { - return _received; + return _request.getRecved(); } void Client::setRawData(char *str) @@ -107,14 +104,8 @@ bool Client::allSended(void) bool Client::allRecved(void) { - if (_request.getContentLength() == _received) - { - std::cout << "contentLength, _received " - << _request.getContentLength() - << " " << - _received << std::endl; + if (_request.getContentLength() == (_request.getRecved() - _request.getHeaderSize())) return (true); - } else return (false); } @@ -126,9 +117,7 @@ void Client::increaseCounter(void) void Client::increaseRecvCounter(unsigned int n) { - if (_received == 0) - _received -= _request.getHeaderSize(); - _received += n; + _request.increaseRecvCounter(n); } //Генерирует response. Далее респонс можно получить через функцию getStrToSend() @@ -170,20 +159,26 @@ void Client::printClientInfo(void) std::map::iterator it1; map = _request.getClientFields(); + std::cout << TURQ << "Parsed request" << 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 << PINK << "content-length = " << _request.getContentLength() << ZERO_C << std::endl; + std::cout << PINK << "connection type = " << _request.getConnection() << ZERO_C << std::endl; + + std::cout << TURQ << "Parsed response" << ZERO_C << std::endl; + std::cout << BLUE << _response.getHeader() << 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 << TURGUOISE << "Client BODY" << ZERO_C << std::endl; - std::cout << GREEN << _request.getBody().size() << 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 << TURGUOISE << "Client BODY" << 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 fcc290f..d5cddd1 100644 --- a/src/Client/Client.hpp +++ b/src/Client/Client.hpp @@ -18,7 +18,6 @@ private: int _ret; int _fd; unsigned int _sended; - unsigned int _received; char *_buff; diff --git a/src/Client/Request.cpp b/src/Client/Request.cpp index 7664df1..b8bbe13 100644 --- a/src/Client/Request.cpp +++ b/src/Client/Request.cpp @@ -10,6 +10,8 @@ Request::Request() _chunked = false; _head_ok = false; _body_ok = false; + _received = 0; + } Request::Request(char *str) @@ -19,6 +21,7 @@ Request::Request(char *str) _data = str; _head_ok = false; _body_ok = false; + _received = 0; _chunked = false; _contentLength = 0; } @@ -57,6 +60,10 @@ std::string Request::getLocation(void) { return (_location); } +std::string Request::getConnection(void) +{ + return (_connection); +} ServerConfig *Request::getConfig(void) { return (_config); @@ -82,7 +89,10 @@ unsigned int Request::getHeaderSize(void) const { return (_headerSize); } - +unsigned int Request::getRecved(void) const +{ + return (_received); +} void Request::setData(char *str) { this->_data = str; @@ -99,7 +109,10 @@ void Request::setConfig(ServerConfig *config) //-------------------------------------------------Parsing--------------------------------------- - +void Request::increaseRecvCounter(unsigned int n) +{ + _received += n; +} void Request::parseURI(std::string str) { std::string tmp; @@ -142,9 +155,10 @@ int Request::parseStartLine(std::string str) void Request::splitData(char *data) { int pos; + std::stringstream ss; std::string str; - str = data; + str = std::string(data); if (!_head_ok) { pos = str.find("\r\n\r\n"); @@ -165,9 +179,11 @@ void Request::splitData(char *data) return ; else if (!_body_ok) { - _body += str.substr(0, str.size()); - if (_body.size() >= _contentLength) + _body += str.substr(0, _received - _headerSize); + if ((_received - _headerSize) == _contentLength) + { _body_ok = true; + } } } @@ -231,8 +247,11 @@ void Request::copyFromMap() { std::map::iterator it; int pos; + //host - _host = _headerField.find("host")->second; + it = _headerField.find("host"); + if (it != _headerField.end()) + _host = it->second; //content-lenght it = _headerField.find("content-length"); @@ -246,6 +265,10 @@ void Request::copyFromMap() if ( pos != -1) _chunked = true; } + //connection + it = _headerField.find("connection"); + if (it != _headerField.end()) + _connection = it->second; } diff --git a/src/Client/Request.hpp b/src/Client/Request.hpp index 56528d4..b0441d5 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 _received; unsigned int _headerSize; std::string _URI; @@ -24,6 +25,7 @@ private: std::string _fullURI; std::string _version; std::string _location; + std::string _connection; std::map _headerField; ServerConfig *_config; @@ -39,10 +41,12 @@ public: std::string getFullUri(void); std::string getVersion(void); std::string getLocation(void); + std::string getConnection(void); ServerConfig *getConfig(void); int getCode(void); unsigned int getContentLength(void) const; unsigned int getHeaderSize(void) const; + unsigned int getRecved(void)const; std::map getClientFields(void); bool getChunked(void); @@ -71,6 +75,7 @@ public: void clear(void); void splitData(char *); + void increaseRecvCounter(unsigned int n); ~Request(); }; diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 7f69826..9b8d0cc 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -82,19 +82,18 @@ void Server::readSocket(int fd, std::map &client_map) return; } client_map[fd].setRawData(buf); - status = client_map[fd].parseRequest(); client_map[fd].increaseRecvCounter(bytes_read); - client_map[fd].printClientInfo(); + status = client_map[fd].parseRequest(); + // 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 << "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::cerr << "allRead " << client_map[fd].allRead << std::endl; std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl; bzero(buf, BUFFSIZE); @@ -171,6 +170,7 @@ void Server::start(void) if (client.readyToSend() && client.allSended()) { + client_map[fd].printClientInfo(); close(client_it->first); std::cerr << RED << "deleting client "