add: Redirect

This commit is contained in:
Talyx
2022-02-09 17:18:36 +03:00
parent 0aeb7b7999
commit a00493fd04
2 changed files with 59 additions and 20 deletions

View File

@@ -43,7 +43,7 @@ void Response::setHeaderBlocks(void)
setConnection(); setConnection();
setDate(); setDate();
setCacheControl(); setCacheControl();
// setLocation(void); setLocation();
// setLanguage(void); // setLanguage(void);
// setTransferEncoding(void); // setTransferEncoding(void);
} }
@@ -87,6 +87,12 @@ void Response::setCacheControl(void)
_cacheControl = "no-store, no-cache, must-revalidate"; _cacheControl = "no-store, no-cache, must-revalidate";
} }
void Response::setLocation(void)
{
if (_code == 301)
_locationSTR = _location->redirect[_code];
}
//-------------------------------------------------File--------------------------------------- //-------------------------------------------------File---------------------------------------
void Response::OpenResponseFile(const char *path) void Response::OpenResponseFile(const char *path)
@@ -185,13 +191,24 @@ std::string Response::getFullURI(void)
std::string tmp; std::string tmp;
std::string ret = ""; std::string ret = "";
int len = _location->location.size(); int len = _location->location.size();
tmp = _request.getURI().substr(len); if (_location->location[0] == '*')
{
tmp = _location->root + tmp; int pos = 0;
pos = _request.getURI().rfind("/");
tmp = _request.getURI().substr(pos);
tmp = _location->root + tmp;
}
else
{
tmp = _request.getURI().substr(len);
tmp = _location->root + tmp;
}
DBOUT << RED << _location->location << ENDL;
DBOUT << len << ENDL;
DBOUT << RED << tmp << ENDL;
if (_request.isDir(tmp) == 0) if (_request.isDir(tmp) == 0)
{ {
if (_location->directoryFile.empty()) if (_location->directoryFile.empty() || _Autoindex)
ret = tmp; ret = tmp;
else else
{ {
@@ -210,7 +227,7 @@ void Response::generateBody(void)
{ {
if (_Autoindex) if (_Autoindex)
{ {
_body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost()); _body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost(), _listen.port);
if (_body.empty()) if (_body.empty())
_code = 404; _code = 404;
} }
@@ -229,7 +246,6 @@ void Response::generateBody(void)
bool Response::allowedMethod(std::string &method) bool Response::allowedMethod(std::string &method)
{ {
DBOUT << "allowedMethod called" << ENDL;
std::vector<std::string>::iterator it; std::vector<std::string>::iterator it;
it = _location->methods.begin(); it = _location->methods.begin();
@@ -259,6 +275,8 @@ void Response::generateHeader(void)
ss << "Date: " << _date << "\r\n"; ss << "Date: " << _date << "\r\n";
if (!_cacheControl.empty()) if (!_cacheControl.empty())
ss << "Cache-Control: " << _cacheControl << "\r\n"; ss << "Cache-Control: " << _cacheControl << "\r\n";
if (!_locationSTR.empty())
ss << "Location: " << _locationSTR << "\r\n";
ss << "\r\n"; ss << "\r\n";
_header = ss.str(); _header = ss.str();
} }
@@ -276,20 +294,27 @@ void Response::generate()
methodPost(); methodPost();
} }
void Response::generate2(void) void Response::generate2(serverListen &l)
{ {
_errorPages = _config->getErrorPages(); if (_config == NULL)
_Autoindex = _location->autoindex; {
_code = _request.getCode(); _code = 404;
_hostPort.ip = _config->getHost(); }
_hostPort.port = _config->getPort(); else
_fullURI = getFullURI(); {
_method = _request.getMethod(); _listen = l;
_errorPages = _config->getErrorPages();
_Autoindex = _location->autoindex;
_code = _request.getCode();
_hostPort.ip = _config->getHost();
_hostPort.port = _config->getPort();
_fullURI = getFullURI();
_method = _request.getMethod();
}
DBOUT << "fullURI " << _fullURI << ENDL; DBOUT << "fullURI " << _fullURI << ENDL;
DBOUT << RED << "code is " << _code << ENDL; DBOUT << RED << "code is " << _code << ENDL;
if (_request.badCode(_code) || !allowedMethod(_method) || isRedirect())
if (_request.badCode(_code) || !allowedMethod(_method))
{ {
invalidClient(); invalidClient();
return; return;
@@ -303,12 +328,24 @@ void Response::generate2(void)
} }
bool Response::isRedirect()
{
if (!_location->redirect.empty())
{
_code = 301;
return (true);
}
else
return (false);
}
//-------------------------------------------------HEADER/BODY--------------------------------------- //-------------------------------------------------HEADER/BODY---------------------------------------
void Response::invalidClient(void) void Response::invalidClient(void)
{ {
OpenErrorFile(_code); if (!isRedirect())
OpenErrorFile(_code);
setHeaderBlocks(); setHeaderBlocks();
generateHeader(); generateHeader();

View File

@@ -8,6 +8,7 @@
class Response class Response
{ {
private: private:
serverListen _listen;
std::string _body; std::string _body;
std::string _header; std::string _header;
Request _request; Request _request;
@@ -62,6 +63,7 @@ public:
std::string getErrorPage(int code); std::string getErrorPage(int code);
std::string getFullURI(); std::string getFullURI();
bool isRedirect(void);
bool allowedMethod(std::string &); bool allowedMethod(std::string &);
void setData(Request, ServerConfig *); void setData(Request, ServerConfig *);
void setData(Request &, ServerConfig *, location *location); void setData(Request &, ServerConfig *, location *location);
@@ -70,7 +72,7 @@ public:
void OpenErrorFile(int code); void OpenErrorFile(int code);
void initErrorCode(void); void initErrorCode(void);
void generate(); void generate();
void generate2(); void generate2(serverListen &);
Response(); Response();
~Response(); ~Response();