mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-28 21:07:59 +03:00
Merge remote-tracking branch 'origin/fara' into fara
This commit is contained in:
@@ -135,40 +135,40 @@ void Client::increaseRecvCounter(unsigned int n)
|
||||
}
|
||||
|
||||
//Генерирует response. Далее респонс можно получить через функцию getStrToSend()
|
||||
int Client::sendResponse(int fd)
|
||||
{
|
||||
_response.setData(_request, _config);
|
||||
_response.generate();
|
||||
_headerToSend = _response.getHeader();
|
||||
_bodyToSend = _response.getBody();
|
||||
_ret = sendData(fd, _headerToSend + _bodyToSend);
|
||||
// int Client::sendResponse(int fd)
|
||||
// {
|
||||
// _response.setData(_request, _config);
|
||||
// _response.generate();
|
||||
// _headerToSend = _response.getHeader();
|
||||
// _bodyToSend = _response.getBody();
|
||||
// _ret = sendData(fd, _headerToSend + _bodyToSend);
|
||||
|
||||
return (_ret);
|
||||
}
|
||||
// return (_ret);
|
||||
// }
|
||||
|
||||
std::string Client::generateRespons(void)
|
||||
{
|
||||
size_t len;
|
||||
// std::string Client::generateRespons(void)
|
||||
// {
|
||||
// size_t len;
|
||||
|
||||
_response.setData(_request, _config);
|
||||
_response.generate();
|
||||
_headerToSend = _response.getHeader();
|
||||
_bodyToSend = _response.getBody();
|
||||
*_toSend = _headerToSend + _bodyToSend;
|
||||
// _response.setData(_request, _config);
|
||||
// _response.generate();
|
||||
// _headerToSend = _response.getHeader();
|
||||
// _bodyToSend = _response.getBody();
|
||||
// *_toSend = _headerToSend + _bodyToSend;
|
||||
|
||||
|
||||
len = _toSend->size();
|
||||
response_len = len;
|
||||
_to_send_char = new char[len + 1];
|
||||
std::memcpy(_to_send_char, _toSend->c_str(), len + 1);
|
||||
// len = _toSend->size();
|
||||
// response_len = len;
|
||||
// _to_send_char = new char[len + 1];
|
||||
// std::memcpy(_to_send_char, _toSend->c_str(), len + 1);
|
||||
|
||||
return (*_toSend);
|
||||
}
|
||||
// return (*_toSend);
|
||||
// }
|
||||
|
||||
std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
|
||||
{
|
||||
size_t len;
|
||||
location *tmp;
|
||||
std::vector<location *> tmp;
|
||||
|
||||
_config = Config::getConfig(configs, _request, connected_to);
|
||||
tmp = Config::getLocation(_config->getLocations(), _request.getURI());
|
||||
@@ -176,7 +176,7 @@ std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
|
||||
_response.generate2(connected_to);
|
||||
|
||||
_toSend = new std::string;
|
||||
*_toSend = _response.getHeader() + _response.getBody();
|
||||
*_toSend = (_response.getBody() == NULL) ? _response.getHeader() : _response.getHeader() + *_response.getBody();
|
||||
|
||||
|
||||
len = _toSend->size();
|
||||
@@ -184,7 +184,10 @@ std::string Client::generateRespons(std::vector<ServerConfig *> &configs)
|
||||
_to_send_char = new char[len + 1];
|
||||
std::memcpy(_to_send_char, _toSend->c_str(), len + 1);
|
||||
|
||||
|
||||
std::cout << PINK << "\n[["<< YELLOW << "Request header\n{" << ENDL;
|
||||
std::cout << BLUE << _request.getHeader() << YELLOW << "},\n" << ENDL;
|
||||
std::cout << GREEN << "Response Header\n{" << ENDL;
|
||||
std::cout << BLUE << _response.getHeader() << GREEN << "}" << PINK << "]]\n"<< ENDL;
|
||||
delete _toSend;
|
||||
if (_request.getBody() != NULL)
|
||||
_request.freeData();
|
||||
|
||||
@@ -63,7 +63,7 @@ int Config::calcLen(std::string &s1, std::string &s2)
|
||||
return (len);
|
||||
}
|
||||
|
||||
location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
|
||||
std::vector<location *> Config::getLocation(std::vector<location *> &arr, std::string &URI)
|
||||
{
|
||||
unsigned int tryLen = URI.size();
|
||||
std::string tryLocation;
|
||||
@@ -83,12 +83,7 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
|
||||
tmp = *it;
|
||||
tryLocation = URI.substr(0, tryLen);
|
||||
if (tmp->location == tryLocation)
|
||||
{
|
||||
tmp = new location;
|
||||
copyLocation(tmp, *it);
|
||||
step_1.push_back(tmp);
|
||||
break;
|
||||
}
|
||||
it++;
|
||||
}
|
||||
if (!step_1.empty())
|
||||
@@ -102,6 +97,7 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
|
||||
if (tmp->location == URI || tmp->location.size() > 1)
|
||||
step_2.push_back(tmp);
|
||||
}
|
||||
|
||||
it = arr.begin();
|
||||
while (it != arr.end())
|
||||
{
|
||||
@@ -110,40 +106,22 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
|
||||
{
|
||||
suffix1 = tmp->location.substr(2);
|
||||
if (suffix1 == suffix)
|
||||
{
|
||||
tmp = new location;
|
||||
copyLocation(tmp, *it);
|
||||
step_2.push_back(tmp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
it++;
|
||||
}
|
||||
if (step_2.size() == 1)
|
||||
return (step_2[0]);
|
||||
else if (step_2.size() == 2)
|
||||
if (step_2.empty())
|
||||
{
|
||||
if(!step_2[1]->cgi_pass.empty())
|
||||
it = arr.begin();
|
||||
while (it != arr.end())
|
||||
{
|
||||
step_2[0]->cgi_pass = step_2[1]->cgi_pass;
|
||||
delete step_2[1];
|
||||
tmp = *it;
|
||||
if (tmp->location == "/")
|
||||
step_2.push_back(tmp);
|
||||
it++;
|
||||
}
|
||||
return (step_2[0]);
|
||||
}
|
||||
it = arr.begin();
|
||||
while (it != arr.end())
|
||||
{
|
||||
tmp = *it;
|
||||
if (tmp->location == "/")
|
||||
{
|
||||
tmp = new location;
|
||||
copyLocation(tmp, *it);
|
||||
return (tmp);
|
||||
}
|
||||
it++;
|
||||
}
|
||||
|
||||
return (NULL);
|
||||
return (step_2);
|
||||
}
|
||||
|
||||
ServerConfig *Config::getConfig(std::vector<ServerConfig *> &arr, Request &request, serverListen &data)
|
||||
|
||||
@@ -16,7 +16,7 @@ public:
|
||||
~Config();
|
||||
|
||||
static int calcLen(std::string &, std::string &);
|
||||
static location *getLocation(std::vector<location *> &, std::string &URI);
|
||||
static std::vector<location *> getLocation(std::vector<location *> &, std::string &URI);
|
||||
static ServerConfig *getConfig(std::vector<ServerConfig *> &, Request &request, serverListen &data);
|
||||
private:
|
||||
|
||||
|
||||
@@ -108,6 +108,10 @@ ssize_t Request::getRecved(void) const
|
||||
{
|
||||
return (_received);
|
||||
}
|
||||
std::string &Request::getHeader(void)
|
||||
{
|
||||
return _head;
|
||||
}
|
||||
void Request::setData(char *str)
|
||||
{
|
||||
this->_data = str;
|
||||
@@ -226,7 +230,6 @@ void Request::splitData(std::string &data)
|
||||
str = _head.substr(_headerSize);
|
||||
_head = tmp;
|
||||
_head_ok = true;
|
||||
|
||||
parseHeader();
|
||||
if ((_contentLength == 0 && !_chunked) || (_method == "GET"
|
||||
|| _method == "DELETE" || _method == "HEAD"))
|
||||
|
||||
@@ -82,6 +82,7 @@ public:
|
||||
void clear(void);
|
||||
void splitData(std::string &);
|
||||
void freeData(void);
|
||||
std::string &getHeader(void);
|
||||
|
||||
void increaseRecvCounter(unsigned int n);
|
||||
~Request();
|
||||
|
||||
@@ -6,8 +6,8 @@ Response::Response()
|
||||
{
|
||||
initErrorCode();
|
||||
_Autoindex = true;
|
||||
_body = new std::string;
|
||||
_header = new std::string;
|
||||
_body = NULL;
|
||||
_header = NULL;
|
||||
_code = 200;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ Response::Response()
|
||||
|
||||
void Response::freeData(void)
|
||||
{
|
||||
delete _body;
|
||||
if (_body != NULL)
|
||||
delete _body;
|
||||
delete _header;
|
||||
}
|
||||
|
||||
@@ -23,9 +24,9 @@ std::string Response::getHeader(void)
|
||||
{
|
||||
return (*_header);
|
||||
}
|
||||
std::string Response::getBody(void)
|
||||
std::string *Response::getBody(void)
|
||||
{
|
||||
return (*_body);
|
||||
return (_body);
|
||||
}
|
||||
|
||||
void Response::setData(Request request, ServerConfig *config)
|
||||
@@ -35,12 +36,21 @@ void Response::setData(Request request, ServerConfig *config)
|
||||
_config = config;
|
||||
}
|
||||
|
||||
void Response::setData(Request &request, ServerConfig *config, location *loc)
|
||||
void Response::setData(Request &request, ServerConfig *config, std::vector<location *> &loc)
|
||||
{
|
||||
_request = request;
|
||||
_code = request.getCode();
|
||||
_config = config;
|
||||
_location = loc;
|
||||
if (loc.empty())
|
||||
_code = 404;
|
||||
else
|
||||
{
|
||||
_location = loc[0];
|
||||
if (loc.size() == 2)
|
||||
_cgi_Pass = loc[1]->cgi_pass;
|
||||
else
|
||||
_cgi_Pass = _location->cgi_pass;
|
||||
}
|
||||
}
|
||||
|
||||
void Response::setHeaderBlocks(void)
|
||||
@@ -67,7 +77,10 @@ void Response::setContentType(void)
|
||||
|
||||
void Response::setContentLength()
|
||||
{
|
||||
_contentLength = _body->size();
|
||||
if (_body != NULL)
|
||||
_contentLength = _body->size();
|
||||
else
|
||||
_contentLength = 0;
|
||||
}
|
||||
|
||||
void Response::setServer(void)
|
||||
@@ -108,7 +121,7 @@ serverListen Response::getListen()
|
||||
|
||||
std::string Response::getCgiPass(void)
|
||||
{
|
||||
return (_location->cgi_pass);
|
||||
return (_cgi_Pass);
|
||||
}
|
||||
|
||||
ssize_t Response::getMaxBodySize(void)
|
||||
@@ -282,7 +295,7 @@ bool Response::allowedMethod(std::string &method)
|
||||
return (true);
|
||||
it++;
|
||||
}
|
||||
if (!_location->cgi_pass.empty() && (method == "GET" || method == "POST"))
|
||||
if (!_cgi_Pass.empty() && (method == "GET" || method == "POST"))
|
||||
return (true);
|
||||
_code = 405;
|
||||
return (false);
|
||||
@@ -290,6 +303,7 @@ bool Response::allowedMethod(std::string &method)
|
||||
}
|
||||
void Response::generateHeader(void)
|
||||
{
|
||||
_header = new std::string;
|
||||
std::stringstream ss;
|
||||
std::string tmp;
|
||||
|
||||
@@ -298,8 +312,8 @@ void Response::generateHeader(void)
|
||||
ss << "Content-Type: " << _contentType << "\r\n";
|
||||
ss << "Content-Length: " << _contentLength << "\r\n";
|
||||
ss << "Server: " << _server << "\r\n";
|
||||
if (!_keepAlive.empty())
|
||||
ss << "Keep-Alive: " <<_keepAlive << "\r\n";
|
||||
// if (!_keepAlive.empty())
|
||||
// ss << "Keep-Alive: " <<_keepAlive << "\r\n";
|
||||
ss << "Date: " << _date << "\r\n";
|
||||
if (!_cacheControl.empty())
|
||||
ss << "Cache-Control: " << _cacheControl << "\r\n";
|
||||
@@ -328,7 +342,7 @@ void Response::generate2(serverListen &l)
|
||||
{
|
||||
_code = 404;
|
||||
}
|
||||
else
|
||||
else if (!_request.badCode(_code))
|
||||
{
|
||||
_listen = l;
|
||||
_errorPages = _config->getErrorPages();
|
||||
@@ -342,7 +356,6 @@ void Response::generate2(serverListen &l)
|
||||
if (_maxBodySize > 0 && _request.getBody() != NULL)
|
||||
_code = (_request.getBody()->size() > (unsigned long)_maxBodySize) ? 413 : _code;
|
||||
}
|
||||
|
||||
if (_request.badCode(_code) || !allowedMethod(_method) || isRedirect())
|
||||
{
|
||||
invalidClient();
|
||||
@@ -374,6 +387,7 @@ bool Response::isRedirect()
|
||||
|
||||
void Response::invalidClient(void)
|
||||
{
|
||||
_body = new std::string;
|
||||
if (!isRedirect())
|
||||
OpenErrorFile(_code);
|
||||
setHeaderBlocks();
|
||||
@@ -385,7 +399,8 @@ void Response::invalidClient(void)
|
||||
|
||||
void Response::methodGet(void)
|
||||
{
|
||||
if (!_location->cgi_pass.empty())
|
||||
_body = new std::string;
|
||||
if (!_cgi_Pass.empty())
|
||||
{
|
||||
CgiHandle cgi(_request, *this);
|
||||
|
||||
@@ -408,12 +423,13 @@ void Response::methodGet(void)
|
||||
generateBody();
|
||||
setHeaderBlocks();
|
||||
generateHeader();
|
||||
std::cout << GREEN << "GET method called\n" << ZERO_C;
|
||||
DBOUT << GREEN << "GET method called\n" << ZERO_C;
|
||||
}
|
||||
void Response::methodPost(void)
|
||||
{
|
||||
if (!_location->cgi_pass.empty())
|
||||
if (!_cgi_Pass.empty())
|
||||
{
|
||||
_body = new std::string;
|
||||
CgiHandle cgi(_request, *this);
|
||||
|
||||
*_body = cgi.executeCgi();
|
||||
@@ -560,5 +576,5 @@ std::string Response::getErrorPage(int code)
|
||||
|
||||
Response::~Response()
|
||||
{
|
||||
delete _location;
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ private:
|
||||
std::string _contentLanguage;
|
||||
std::string _transferEncoding;
|
||||
std::string _upload_dir;
|
||||
std::string _cgi_Pass;
|
||||
|
||||
void setHeaderBlocks(void);
|
||||
void setContentType(void);
|
||||
@@ -62,7 +63,7 @@ public:
|
||||
serverListen getListen(void);
|
||||
std::string getCgiPass(void);
|
||||
std::string getHeader(void);
|
||||
std::string getBody(void);
|
||||
std::string *getBody(void);
|
||||
static std::string getReasonPhrase(std::string);
|
||||
static std::string getReasonPhrase(int);
|
||||
std::string getErrorPage(int code);
|
||||
@@ -71,7 +72,7 @@ public:
|
||||
bool isRedirect(void);
|
||||
bool allowedMethod(std::string &);
|
||||
void setData(Request, ServerConfig *);
|
||||
void setData(Request &, ServerConfig *, location *location);
|
||||
void setData(Request &, ServerConfig *, std::vector<location *> &);
|
||||
public:
|
||||
std::string _fullURI;
|
||||
void OpenResponseFile(const char *path);
|
||||
|
||||
@@ -132,7 +132,7 @@ void Server::readSocket(Client &client, int fd)
|
||||
// client.setRawData(buf);
|
||||
client.increaseRecvCounter(bytes_read);
|
||||
status = client.parseRequest();
|
||||
(void)status; //JOPA !!!!!
|
||||
(void)status;
|
||||
// client_map[fd].printClientInfo();
|
||||
|
||||
if (client.allRecved())
|
||||
@@ -253,7 +253,7 @@ void Server::start(void)
|
||||
{
|
||||
|
||||
ready_num = epoll_wait(_epoll_fd, _events, MAX_CLIENT, 5000);
|
||||
// DBOUT << TURQ << "ready_num " << ready_num << ENDL;F
|
||||
// DBOUT << TURQ << "ready_num " << ready_num << ENDL;
|
||||
|
||||
if (ready_num < 0)
|
||||
throw std::logic_error("epoll_ret");
|
||||
|
||||
@@ -383,6 +383,8 @@ void ServerConfig::checkConfig(void)
|
||||
|
||||
it = _locations.begin();
|
||||
location *tmp;
|
||||
if (it == _locations.end())
|
||||
throw ConfigException("Required routing settings are missing!");
|
||||
while (it != _locations.end())
|
||||
{
|
||||
tmp = *it;
|
||||
@@ -402,43 +404,43 @@ void ServerConfig::printFields(void)
|
||||
|
||||
it1 = _errorPages.begin();
|
||||
it = _locations.begin();
|
||||
std::cout << RED << "-------------------------Server-Start----------------------------------\n" << ZERO_C;
|
||||
std::cout << GREEN << "name" << " " << BLUE << _serverName << std::endl;
|
||||
std::cout << GREEN << "host" << " " << BLUE << _host << std::endl;
|
||||
std::cout << GREEN << "port" << " " << BLUE << _port << std::endl;
|
||||
std::cout << GREEN << "client_body_size" << " " << BLUE << _clientBodySize << std::endl;
|
||||
std::cout << GREEN << "location" << std::endl;
|
||||
DBOUT << RED << "-------------------------Server-Start----------------------------------\n" << ZERO_C;
|
||||
DBOUT << GREEN << "name" << " " << BLUE << _serverName << std::endl;
|
||||
DBOUT << GREEN << "host" << " " << BLUE << _host << std::endl;
|
||||
DBOUT << GREEN << "port" << " " << BLUE << _port << std::endl;
|
||||
DBOUT << GREEN << "client_body_size" << " " << BLUE << _clientBodySize << std::endl;
|
||||
DBOUT << GREEN << "location" << std::endl;
|
||||
while (it != _locations.end())
|
||||
{
|
||||
std::cout << PINK << "------------------------------------------------\n";
|
||||
std::cout << YELLOW << "location " << BLUE << (*it)->location <<std::endl;
|
||||
std::cout << YELLOW << "root " << BLUE << (*it)->root <<std::endl;
|
||||
std::cout << YELLOW << "directoryFile " << BLUE << (*it)->directoryFile <<std::endl;
|
||||
std::cout << YELLOW << "uploadDir " << BLUE << (*it)->uploadDir <<std::endl;
|
||||
std::cout << YELLOW << "autoindex " << BLUE << (*it)->autoindex <<std::endl;
|
||||
std::cout << YELLOW << "uploadAccept " << BLUE << (*it)->uploadAccept <<std::endl;
|
||||
std::cout << YELLOW << "cgi_pass " << BLUE << (*it)->cgi_pass <<std::endl;
|
||||
std::cout << YELLOW << "client_body_size " << BLUE << (*it)->clientBodySize <<std::endl;
|
||||
std::cout << YELLOW << "methods " << std::endl;
|
||||
DBOUT << PINK << "------------------------------------------------\n";
|
||||
DBOUT << YELLOW << "location " << BLUE << (*it)->location <<std::endl;
|
||||
DBOUT << YELLOW << "root " << BLUE << (*it)->root <<std::endl;
|
||||
DBOUT << YELLOW << "directoryFile " << BLUE << (*it)->directoryFile <<std::endl;
|
||||
DBOUT << YELLOW << "uploadDir " << BLUE << (*it)->uploadDir <<std::endl;
|
||||
DBOUT << YELLOW << "autoindex " << BLUE << (*it)->autoindex <<std::endl;
|
||||
DBOUT << YELLOW << "uploadAccept " << BLUE << (*it)->uploadAccept <<std::endl;
|
||||
DBOUT << YELLOW << "cgi_pass " << BLUE << (*it)->cgi_pass <<std::endl;
|
||||
DBOUT << YELLOW << "client_body_size " << BLUE << (*it)->clientBodySize <<std::endl;
|
||||
DBOUT << YELLOW << "methods " << std::endl;
|
||||
it2 = (*it)->methods.begin();
|
||||
while (it2 != (*it)->methods.end())
|
||||
{
|
||||
std::cout << BLUE << *it2 << " ";
|
||||
DBOUT << BLUE << *it2 << " ";
|
||||
it2++;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
DBOUT << std::endl;
|
||||
it3 = (*it)->redirect.begin();
|
||||
std::cout << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl;
|
||||
DBOUT << YELLOW << "redirection" << RED << " " << it3->first << " " << BLUE << it3->second << std::endl;
|
||||
++it;
|
||||
std::cout << PINK << "------------------------------------------------\n";
|
||||
DBOUT << PINK << "------------------------------------------------\n";
|
||||
}
|
||||
std::cout << GREEN << "error pages" << std::endl;
|
||||
DBOUT << GREEN << "error pages" << std::endl;
|
||||
while (it1 != _errorPages.end())
|
||||
{
|
||||
std::cout << YELLOW << it1->first << " " << BLUE << it1->second << std::endl;
|
||||
DBOUT << YELLOW << it1->first << " " << BLUE << it1->second << std::endl;
|
||||
++it1;
|
||||
}
|
||||
std::cout << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C;
|
||||
DBOUT << RED << "-------------------------Server-End------------------------------------\n" << ZERO_C;
|
||||
}
|
||||
|
||||
ServerConfig::~ServerConfig()
|
||||
|
||||
@@ -23,7 +23,7 @@ int main(int argc, char **argv)
|
||||
catch(const ConfigException& e)
|
||||
{
|
||||
server.end();
|
||||
std::cerr << RED << e.getMessage() << '\n' << ENDL;
|
||||
std::cerr << RED << "\n" << e.getMessage() << '\n' << ENDL;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user