fix: endless loading for non txt files

This commit is contained in:
Talyx
2022-02-01 15:58:30 +03:00
parent 88b0e49dc1
commit d3425e88a3
5 changed files with 56 additions and 34 deletions

View File

@@ -5,7 +5,6 @@
Client::Client() Client::Client()
{ {
allRead = false; allRead = false;
_received = 0;
this->_fd = -1; this->_fd = -1;
this->_sended = 0; this->_sended = 0;
} }
@@ -13,7 +12,6 @@ Client::Client()
Client::Client(char *str) Client::Client(char *str)
{ {
allRead = false; allRead = false;
_received = 0;
this->_fd = -1; this->_fd = -1;
this->_buff = str; this->_buff = str;
this->_sended = 0; this->_sended = 0;
@@ -23,7 +21,6 @@ Client::Client(char *str)
Client::Client(char *str, ServerConfig *config) Client::Client(char *str, ServerConfig *config)
{ {
allRead = false; allRead = false;
_received = 0;
this->_fd = -1; this->_fd = -1;
this->_config = config; this->_config = config;
this->_buff = str; this->_buff = str;
@@ -60,7 +57,7 @@ unsigned int Client::getCounter(void) const
unsigned int Client::getRecvCounter(void) const unsigned int Client::getRecvCounter(void) const
{ {
return _received; return _request.getRecved();
} }
void Client::setRawData(char *str) void Client::setRawData(char *str)
@@ -107,14 +104,8 @@ bool Client::allSended(void)
bool Client::allRecved(void) bool Client::allRecved(void)
{ {
if (_request.getContentLength() == _received) if (_request.getContentLength() == (_request.getRecved() - _request.getHeaderSize()))
{
std::cout << "contentLength, _received "
<< _request.getContentLength()
<< " " <<
_received << std::endl;
return (true); return (true);
}
else else
return (false); return (false);
} }
@@ -126,9 +117,7 @@ void Client::increaseCounter(void)
void Client::increaseRecvCounter(unsigned int n) void Client::increaseRecvCounter(unsigned int n)
{ {
if (_received == 0) _request.increaseRecvCounter(n);
_received -= _request.getHeaderSize();
_received += n;
} }
//Генерирует response. Далее респонс можно получить через функцию getStrToSend() //Генерирует response. Далее респонс можно получить через функцию getStrToSend()
@@ -170,20 +159,26 @@ void Client::printClientInfo(void)
std::map<std::string, std::string>::iterator it1; std::map<std::string, std::string>::iterator it1;
map = _request.getClientFields(); 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 method = " << _request.getMethod() << ZERO_C << std::endl;
std::cout << PINK << "request URI = " << _request.getURI() << 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 << "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 << PINK << "content-length = " << _request.getContentLength() << ZERO_C << std::endl;
std::cout << PINK << "connection type = " << _request.getConnection() << ZERO_C << std::endl;
std::cout << TURGUOISE << "Client MAP" << ZERO_C << std::endl; std::cout << TURQ << "Parsed response" << ZERO_C << std::endl;
for ( it = map.begin(); it != map.end() ; it++) std::cout << BLUE << _response.getHeader() << ZERO_C << std::endl;
{
std::cout << PINK << it->first << BLUE << it->second << 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 BODY" << ZERO_C << std::endl; // for ( it = map.begin(); it != map.end() ; it++)
std::cout << GREEN << _request.getBody().size() << 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 << GREEN << _request.getBody().size() << ZERO_C << std::endl;
/* std::cout << BLUE << _request.getBody() << ZERO_C << std::endl; */ /* std::cout << BLUE << _request.getBody() << ZERO_C << std::endl; */
} }

View File

@@ -18,7 +18,6 @@ private:
int _ret; int _ret;
int _fd; int _fd;
unsigned int _sended; unsigned int _sended;
unsigned int _received;
char *_buff; char *_buff;

View File

@@ -10,6 +10,8 @@ Request::Request()
_chunked = false; _chunked = false;
_head_ok = false; _head_ok = false;
_body_ok = false; _body_ok = false;
_received = 0;
} }
Request::Request(char *str) Request::Request(char *str)
@@ -19,6 +21,7 @@ Request::Request(char *str)
_data = str; _data = str;
_head_ok = false; _head_ok = false;
_body_ok = false; _body_ok = false;
_received = 0;
_chunked = false; _chunked = false;
_contentLength = 0; _contentLength = 0;
} }
@@ -57,6 +60,10 @@ std::string Request::getLocation(void)
{ {
return (_location); return (_location);
} }
std::string Request::getConnection(void)
{
return (_connection);
}
ServerConfig *Request::getConfig(void) ServerConfig *Request::getConfig(void)
{ {
return (_config); return (_config);
@@ -82,7 +89,10 @@ unsigned int Request::getHeaderSize(void) const
{ {
return (_headerSize); return (_headerSize);
} }
unsigned int Request::getRecved(void) const
{
return (_received);
}
void Request::setData(char *str) void Request::setData(char *str)
{ {
this->_data = str; this->_data = str;
@@ -99,7 +109,10 @@ void Request::setConfig(ServerConfig *config)
//-------------------------------------------------Parsing--------------------------------------- //-------------------------------------------------Parsing---------------------------------------
void Request::increaseRecvCounter(unsigned int n)
{
_received += n;
}
void Request::parseURI(std::string str) void Request::parseURI(std::string str)
{ {
std::string tmp; std::string tmp;
@@ -142,9 +155,10 @@ int Request::parseStartLine(std::string str)
void Request::splitData(char *data) void Request::splitData(char *data)
{ {
int pos; int pos;
std::stringstream ss;
std::string str; std::string str;
str = data; str = std::string(data);
if (!_head_ok) if (!_head_ok)
{ {
pos = str.find("\r\n\r\n"); pos = str.find("\r\n\r\n");
@@ -165,11 +179,13 @@ void Request::splitData(char *data)
return ; return ;
else if (!_body_ok) else if (!_body_ok)
{ {
_body += str.substr(0, str.size()); _body += str.substr(0, _received - _headerSize);
if (_body.size() >= _contentLength) if ((_received - _headerSize) == _contentLength)
{
_body_ok = true; _body_ok = true;
} }
} }
}
int Request::parseClientfield(std::string str) int Request::parseClientfield(std::string str)
{ {
@@ -231,8 +247,11 @@ void Request::copyFromMap()
{ {
std::map<std::string, std::string>::iterator it; std::map<std::string, std::string>::iterator it;
int pos; int pos;
//host //host
_host = _headerField.find("host")->second; it = _headerField.find("host");
if (it != _headerField.end())
_host = it->second;
//content-lenght //content-lenght
it = _headerField.find("content-length"); it = _headerField.find("content-length");
@@ -246,6 +265,10 @@ void Request::copyFromMap()
if ( pos != -1) if ( pos != -1)
_chunked = true; _chunked = true;
} }
//connection
it = _headerField.find("connection");
if (it != _headerField.end())
_connection = it->second;
} }

View File

@@ -13,6 +13,7 @@ private:
int _ret; int _ret;
int _row; int _row;
unsigned int _contentLength; unsigned int _contentLength;
unsigned int _received;
unsigned int _headerSize; unsigned int _headerSize;
std::string _URI; std::string _URI;
@@ -24,6 +25,7 @@ private:
std::string _fullURI; std::string _fullURI;
std::string _version; std::string _version;
std::string _location; std::string _location;
std::string _connection;
std::map<std::string, std::string> _headerField; std::map<std::string, std::string> _headerField;
ServerConfig *_config; ServerConfig *_config;
@@ -39,10 +41,12 @@ public:
std::string getFullUri(void); std::string getFullUri(void);
std::string getVersion(void); std::string getVersion(void);
std::string getLocation(void); std::string getLocation(void);
std::string getConnection(void);
ServerConfig *getConfig(void); ServerConfig *getConfig(void);
int getCode(void); int getCode(void);
unsigned int getContentLength(void) const; unsigned int getContentLength(void) const;
unsigned int getHeaderSize(void) const; unsigned int getHeaderSize(void) const;
unsigned int getRecved(void)const;
std::map<std::string, std::string> getClientFields(void); std::map<std::string, std::string> getClientFields(void);
bool getChunked(void); bool getChunked(void);
@@ -71,6 +75,7 @@ public:
void clear(void); void clear(void);
void splitData(char *); void splitData(char *);
void increaseRecvCounter(unsigned int n);
~Request(); ~Request();
}; };

View File

@@ -82,19 +82,18 @@ void Server::readSocket(int fd, std::map<int, Client> &client_map)
return; return;
} }
client_map[fd].setRawData(buf); client_map[fd].setRawData(buf);
status = client_map[fd].parseRequest();
client_map[fd].increaseRecvCounter(bytes_read); 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()) if ((bytes_read < BUFFSIZE) && client_map[fd].allRecved())
{ {
client_map[fd].allRead = true; 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 << "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; std::cout << BLUE << "status is " << Response::getReasonPhrase(status) << RESET << std::endl;
bzero(buf, BUFFSIZE); bzero(buf, BUFFSIZE);
@@ -171,6 +170,7 @@ void Server::start(void)
if (client.readyToSend() && client.allSended()) if (client.readyToSend() && client.allSended())
{ {
client_map[fd].printClientInfo();
close(client_it->first); close(client_it->first);
std::cerr << RED << std::cerr << RED <<
"deleting client " "deleting client "