Merge remote-tracking branch 'origin/fara' into roman

This commit is contained in:
3lswear
2022-02-08 21:56:55 +03:00
7 changed files with 262 additions and 102 deletions

View File

@@ -73,6 +73,11 @@ void Client::setRawData(char *str)
this->_buff = str; this->_buff = str;
} }
void Client::setRawData(std::string &buf)
{
_stringBUF = buf;
}
void Client::setFd(int fd) void Client::setFd(int fd)
{ {
this->_fd = fd; this->_fd = fd;
@@ -82,7 +87,8 @@ void Client::setFd(int fd)
int Client::parseRequest(void) int Client::parseRequest(void)
{ {
_request.setData(_buff); // _request.setData(_buff);
_request.setData(_stringBUF);
_ret = _request.parseRequest(); _ret = _request.parseRequest();
return (_ret); return (_ret);
@@ -156,6 +162,9 @@ std::string Client::generateRespons(void)
_to_send_char = new char[len + 1]; _to_send_char = new char[len + 1];
std::memcpy(_to_send_char, _toSend.c_str(), len + 1); std::memcpy(_to_send_char, _toSend.c_str(), len + 1);
// DBOUT << "len = " << len << ENDL;
// DBOUT << "strlen = " << strlen(_to_send_char) << ENDL;
// DBOUT << "content_lenth = " << _request.getContentLength() << ENDL;
return (_toSend); return (_toSend);
} }
@@ -167,7 +176,7 @@ std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
_config = Config::getConfig(configs, _request, connected_to); _config = Config::getConfig(configs, _request, connected_to);
tmp = Config::getLocation(_config->getLocations(), _request.getURI()); tmp = Config::getLocation(_config->getLocations(), _request.getURI());
_response.setData(_request, _config, tmp); _response.setData(_request, _config, tmp);
_response.generate(); _response.generate2();
_headerToSend = _response.getHeader(); _headerToSend = _response.getHeader();
_bodyToSend = _response.getBody(); _bodyToSend = _response.getBody();
_toSend = _headerToSend + _bodyToSend; _toSend = _headerToSend + _bodyToSend;

View File

@@ -23,7 +23,7 @@ private:
unsigned int _sended; unsigned int _sended;
char *_buff; char *_buff;
std::string _stringBUF;
std::string _bodyToSend; std::string _bodyToSend;
std::string _headerToSend; std::string _headerToSend;
std::string _toSend; std::string _toSend;
@@ -53,6 +53,7 @@ public:
char *getStrToSend(void); char *getStrToSend(void);
unsigned int getCounter(void) const; unsigned int getCounter(void) const;
void setRawData(char *); void setRawData(char *);
void setRawData(std::string &);
void setFd(int); void setFd(int);
int getFd(void); int getFd(void);
unsigned int getRecvCounter(void) const; unsigned int getRecvCounter(void) const;

View File

@@ -105,6 +105,12 @@ void Request::setData(char *str)
{ {
this->_data = str; this->_data = str;
} }
void Request::setData(std::string &str)
{
_stringBUF = str;
}
void Request::setData(char *str, ServerConfig *config) void Request::setData(char *str, ServerConfig *config)
{ {
_data = str; _data = str;
@@ -152,16 +158,27 @@ int Request::parseStartLine(std::string str)
if (_version != "HTTP/1.1") if (_version != "HTTP/1.1")
_ret = 505; _ret = 505;
else if (_method != "GET" && _method != "POST"
&& _method != "DELETE")
_ret = 405;
return (_ret); return (_ret);
} }
void Request::splitData(char *data) int checkEnd(const std::string& str, const std::string& end)
{
size_t i = str.size();
size_t j = end.size();
while (j > 0)
{
i--;
j--;
if (i < 0 || str[i] != end[j])
return (1);
}
return (0);
}
void Request::splitData(std::string &data)
{ {
int pos; int pos;
std::stringstream ss;
std::string str; std::string str;
str = std::string(data); str = std::string(data);
@@ -175,22 +192,47 @@ void Request::splitData(char *data)
} }
_head = str.substr(0, pos) + "\n"; _head = str.substr(0, pos) + "\n";
_headerSize = _head.size() + 3; _headerSize = _head.size() + 3;
str.erase(0, pos + 4); data.erase(0, pos + 4);
_head_ok = true; _head_ok = true;
parseHeader(); parseHeader();
if (_contentLength == 0) if (_contentLength == 0 && !_chunked)
_body_ok = true; _body_ok = true;
} }
if (badCode(_ret)) if (badCode(_ret))
return ; return ;
else if (_chunked)
{
_body.insert(_body.end(), data.begin(), data.end());
if (checkEnd(_body, "0\r\n\r\n") == 0)
_body_ok = true;
}
else if (!_body_ok) else if (!_body_ok)
{ {
_body += str;
_body.insert(_body.end(), data.begin(), data.end());
if ((_received - _headerSize) == _contentLength) if ((_received - _headerSize) == _contentLength)
{ {
_body_ok = true; _body_ok = true;
} }
} }
if (_head_ok && _body_ok && _chunked)
{
std::string &tmp = _body;
std::string subchunk = tmp.substr(0, 100);
std::string newBody = "";
int chunksize = strtol(subchunk.c_str(), NULL, 16);
size_t i = 0;
while (chunksize)
{
i = tmp.find("\r\n", i) + 2;
newBody += tmp.substr(i, chunksize);
i += chunksize + 2;
subchunk = tmp.substr(i, 100);
chunksize = strtol(subchunk.c_str(), NULL, 16);
}
_body = newBody;
}
} }
int Request::parseClientfield(std::string str) int Request::parseClientfield(std::string str)
@@ -241,7 +283,7 @@ int Request::parseHeader(void)
int Request::parseRequest(void) int Request::parseRequest(void)
{ {
if (!_head_ok || !_body_ok) if (!_head_ok || !_body_ok)
splitData(_data); splitData(_stringBUF);
return (_ret); return (_ret);
} }

View File

@@ -9,6 +9,7 @@ class Request
{ {
private: private:
char *_data; char *_data;
char *_pointerBody;
int _ret; int _ret;
int _row; int _row;
@@ -29,6 +30,7 @@ private:
std::string _connection; std::string _connection;
std::map<std::string, std::string> _headerField; std::map<std::string, std::string> _headerField;
std::string _stringBUF;
ServerConfig *_config; ServerConfig *_config;
bool _head_ok; bool _head_ok;
bool _body_ok; bool _body_ok;
@@ -49,11 +51,14 @@ public:
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; unsigned int getRecved(void)const;
char *getPointerBody(void)const;
std::map<std::string, std::string> getClientFields(void); std::map<std::string, std::string> getClientFields(void);
bool getChunked(void); bool getChunked(void);
void setConfig(ServerConfig *config); void setConfig(ServerConfig *config);
void setData(char *); void setData(char *);
void setData(std::string &);
void setData(char *, ServerConfig *); void setData(char *, ServerConfig *);
public: public:
@@ -75,7 +80,7 @@ public:
bool autoindexOn(void); bool autoindexOn(void);
void copyFromMap(void); void copyFromMap(void);
void clear(void); void clear(void);
void splitData(char *); void splitData(std::string &);
void increaseRecvCounter(unsigned int n); void increaseRecvCounter(unsigned int n);
~Request(); ~Request();

View File

@@ -5,6 +5,7 @@
Response::Response() Response::Response()
{ {
initErrorCode(); initErrorCode();
_Autoindex = true;
_code = 200; _code = 200;
} }
@@ -48,6 +49,11 @@ void Response::setHeaderBlocks(void)
} }
void Response::setContentType(void) void Response::setContentType(void)
{ {
if (_code == 204)
return ;
else if (_request.badCode(_code))
_contentType = "text/html";
else
_contentType = getContentType(); _contentType = getContentType();
} }
@@ -86,7 +92,6 @@ void Response::setCacheControl(void)
void Response::OpenResponseFile(const char *path) void Response::OpenResponseFile(const char *path)
{ {
std::stringstream ss; std::stringstream ss;
// char buf[BUFFSIZE + 1] = {0};
std::ifstream file(path, std::ifstream::in); std::ifstream file(path, std::ifstream::in);
if (file.is_open()) if (file.is_open())
@@ -96,69 +101,34 @@ void Response::OpenResponseFile(const char *path)
file.close(); file.close();
} }
else else
_body = getErrorPage(403);
}
void Response::generate()
{
_fullURI = _request.getFullUri();
if (_request.badCode(_request.getCode()))
invalidClient();
else if (_request.getMethod() == "GET")
methodGet();
// else if (_request.getMethod() == "POST")
// methodPost();
// else
// methodDelete();
}
std::string Response::getFullURI(std::string &str, std::string &str2)
{
std::string line;
unsigned long pos = str.find_last_of("/");
if (pos == str.size() - 1)
line = str.substr(0, pos - 1);
line = line + str2;
return (line);
}
void Response::generate2(void)
{
_errorPages = _config->getErrorPages();
_Autoindex = _location->autoindex;
_code = _request.getCode();
_hostPort.ip = _config->getHost();
_hostPort.port = _config->getPort();
_fullURI = getFullURI(_location->root, _request.getURI());
_method = _request.getMethod();
if (_request.badCode(_code))
{ {
invalidClient(); _code = 403;
return; OpenErrorFile(_code);
} }
if (_method == "GET")
methodGet();
// else if (_method == "POST")
// methodPost();
// else
// methodDelete();
} }
//-------------------------------------------------HEADER/BODY--------------------------------------- void Response::OpenErrorFile(int code)
std::string Response::getTime(void)
{ {
char buff[1337] = {0}; std::map<int, std::string>::iterator it;
struct timeval currTime; std::stringstream ss;
struct tm *t;
gettimeofday(&currTime, NULL); it = _errorPages.find(code);
t = gmtime(&currTime.tv_sec); if (it != _errorPages.end())
strftime(buff, 1337, "%a, %d %b %Y %H:%M:%S GMT", t); {
const char *path = it->second.c_str();
std::ifstream file(path, std::ifstream::in);
return (buff); if (file.is_open())
{
ss << file.rdbuf();
_body = ss.str();
file.close();
}
else
_body = getErrorPage(code);
}
else
_body = getErrorPage(code);
} }
std::string Response::getContentType(void) std::string Response::getContentType(void)
@@ -195,40 +165,98 @@ std::string Response::getContentType(void)
} }
void Response::invalidClient(void) std::string Response::getTime(void)
{ {
std::map<int, std::string>::iterator it; char buff[1337] = {0};
struct timeval currTime;
struct tm *t;
it = _errorPages.find(_code); gettimeofday(&currTime, NULL);
if (it != _errorPages.end()) t = gmtime(&currTime.tv_sec);
OpenResponseFile(it->second.c_str()); strftime(buff, 1337, "%a, %d %b %Y %H:%M:%S GMT", t);
else
_body = getErrorPage(_code);
setHeaderBlocks();
generateHeader();
DBOUT << RED << "Error Method called" << ENDL; return (buff);
}
std::string Response::getFullURI(void)
{
std::string tmp;
bool end = false;
std::string ret = "";
if (!_location->directoryFile.empty())
{
struct dirent *dirEnt;
DIR *dir = opendir(_location->root.c_str());
if (dir == NULL)
{
_code = 404;
return "";
}
for (dirEnt = readdir(dir); !end && dirEnt; dirEnt = readdir(dir))
{
tmp = dirEnt->d_name;
if (tmp == _location->directoryFile)
{
ret = tmp;
end = true;
}
}
}
if (!end)
{
ret = _location->root;
}
return (ret);
} }
void Response::generateBody(void) void Response::generateBody(void)
{ {
if (!_request.badCode(_code) && _request.isDir(_request.getFullUri()) == 0) if (!_request.badCode(_code) && _request.isDir(_fullURI) == 0)
_body = Autoindex::getPage(_request.getURI(), _request.getFullUri(), _request.getHost()); {
else if (!_request.badCode(_code) && _request.isFile(_request.getFullUri()) == 0) if (_Autoindex)
OpenResponseFile(_request.getFullUri().c_str()); {
else if (_request.isFile(_request.getFullUri()) == -1) _body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost());
_body = getErrorPage(404); if (_body.empty())
_code = 404;
}
else else
_body = getErrorPage(_code); _code = 403;
}
else if (!_request.badCode(_code) && _request.isFile(_request.getFullUri()) == 0)
OpenResponseFile(_fullURI.c_str());
else if (_request.isFile(_fullURI) == -1)
_code = 404;
if (_request.badCode(_code))
OpenErrorFile(_code);
} }
bool Response::allowedMethod(std::string &method)
{
std::vector<std::string>::iterator it;
it = _location->methods.begin();
while (it != _location->methods.end())
{
if (*it == method)
return (true);
it++;
}
_code = 405;
return (false);
}
void Response::generateHeader(void) void Response::generateHeader(void)
{ {
std::stringstream ss; std::stringstream ss;
std::string tmp; std::string tmp;
ss << "HTTP/1.1" << " " << _code << " " << getReasonPhrase(_code) << "\r\n"; ss << "HTTP/1.1" << " " << _code << " " << getReasonPhrase(_code) << "\r\n";
if (!_contentType.empty())
ss << "Content-Type: " << _contentType << "\r\n"; ss << "Content-Type: " << _contentType << "\r\n";
ss << "Content-Length: " << _contentLength << "\r\n"; ss << "Content-Length: " << _contentLength << "\r\n";
ss << "Server: " << _server << "\r\n"; ss << "Server: " << _server << "\r\n";
@@ -241,18 +269,89 @@ void Response::generateHeader(void)
_header = ss.str(); _header = ss.str();
} }
void Response::generate()
{
_fullURI = _request.getFullUri();
if (_request.badCode(_request.getCode()))
invalidClient();
else if (_request.getMethod() == "GET")
methodGet();
else if (_request.getMethod() == "DELETE")
methodDelete();
else
methodPost();
}
void Response::generate2(void)
{
_errorPages = _config->getErrorPages();
_Autoindex = _location->autoindex;
_code = _request.getCode();
_hostPort.ip = _config->getHost();
_hostPort.port = _config->getPort();
_fullURI = getFullURI();
_method = _request.getMethod();
if (_request.badCode(_code) || !allowedMethod(_method))
{
invalidClient();
return;
}
if (_method == "GET")
methodGet();
else if (_method == "POST")
methodPost();
else
methodDelete();
}
//-------------------------------------------------HEADER/BODY---------------------------------------
void Response::invalidClient(void)
{
OpenErrorFile(_code);
setHeaderBlocks();
generateHeader();
DBOUT << RED << "Error Method called" << ENDL;
}
void Response::methodGet(void) void Response::methodGet(void)
{ {
generateBody(); generateBody();
setHeaderBlocks(); setHeaderBlocks();
DBOUT << RED << _fullURI << ENDL;
DBOUT << _body.size() << ENDL;
generateHeader(); generateHeader();
std::cout << GREEN << "GET method called\n" << ZERO_C; std::cout << GREEN << "GET method called\n" << ZERO_C;
}
void Response::methodPost(void)
{
std::ofstream outfile(_fullURI.c_str(), std::ios::out | std::ios::binary);
outfile.write(_request.getBody().data(), _request.getBody().size());
_code = 204;
setHeaderBlocks();
generateHeader();
DBOUT << GREEN << "POST method called" << ENDL;
}
void Response::methodDelete(void)
{
if (_request.isFile(_fullURI) == 0)
{
if (remove(_fullURI.c_str()) == 0)
_code = 204;
else
_code = 403;
}
else
_code = 404;
if (_code == 404 || _code == 403)
OpenErrorFile(_code);
setHeaderBlocks();
generateHeader();
DBOUT << GREEN << "Delete method called" << ENDL;
} }
//-------------------------------------------------GET/SET--------------------------------------- //-------------------------------------------------GET/SET---------------------------------------
@@ -332,7 +431,7 @@ std::string Response::getErrorPage(int code)
ss << "<html><head><title>" << code <<" "<< getReasonPhrase(code) <<"</title></head><body>" ss << "<html><head><title>" << code <<" "<< getReasonPhrase(code) <<"</title></head><body>"
<<"<center><h1>" << code <<" " << getReasonPhrase(code) <<"</h1></center> " <<"<center><h1>" << code <<" " << getReasonPhrase(code) <<"</h1></center> "
<< "<hr><center>poheck/1.0.0 (KDE)</center></body></html>"; << "<hr><center>poheck/1.0.0 (genereted)</center></body></html>";
Page = ss.str(); Page = ss.str();
return (Page); return (Page);
} }

View File

@@ -60,13 +60,14 @@ public:
static std::string getReasonPhrase(std::string); static std::string getReasonPhrase(std::string);
static std::string getReasonPhrase(int); static std::string getReasonPhrase(int);
std::string getErrorPage(int code); std::string getErrorPage(int code);
std::string getFullURI(std::string &, std::string &); std::string getFullURI();
bool allowedMethod(std::string &);
void setData(Request, ServerConfig *); void setData(Request, ServerConfig *);
void setData(Request &, ServerConfig *, location *location); void setData(Request &, ServerConfig *, location *location);
public: public:
void OpenResponseFile(const char *path); void OpenResponseFile(const char *path);
void OpenErrorFile(int code);
void initErrorCode(void); void initErrorCode(void);
void generate(); void generate();
void generate2(); void generate2();

View File

@@ -88,18 +88,21 @@ void Server::readSocket(Client &client, int fd)
int status; int status;
int bytes_read; int bytes_read;
char buf[BUFFSIZE + 1]; // char buf[BUFFSIZE + 1];
std::string stringBUF(BUFFSIZE, 0);
DBOUT << TURQ << "IN readSocket" << ENDL; DBOUT << TURQ << "IN readSocket" << ENDL;
DBOUT << "client in readSocket "<< &client << ENDL; DBOUT << "client in readSocket "<< &client << ENDL;
bytes_read = recv(fd, buf, BUFFSIZE, 0); bytes_read = recv(fd, &stringBUF[0], BUFFSIZE, 0);
if (bytes_read == 0) if (bytes_read == 0)
{ {
client.allRead = true; client.allRead = true;
return; return;
} }
buf[bytes_read + 1] = '\0'; // buf[bytes_read + 1] = '\0';
client.setRawData(buf); stringBUF.erase(bytes_read, stringBUF.size());
client.setRawData(stringBUF);
// client.setRawData(buf);
client.increaseRecvCounter(bytes_read); client.increaseRecvCounter(bytes_read);
status = client.parseRequest(); status = client.parseRequest();
// client_map[fd].printClientInfo(); // client_map[fd].printClientInfo();