From a00493fd040bf279f1f72b4865a2f95b8bbcf86f Mon Sep 17 00:00:00 2001 From: Talyx Date: Wed, 9 Feb 2022 17:18:36 +0300 Subject: [PATCH] add: Redirect --- src/Client/Response.cpp | 75 ++++++++++++++++++++++++++++++----------- src/Client/Response.hpp | 4 ++- 2 files changed, 59 insertions(+), 20 deletions(-) diff --git a/src/Client/Response.cpp b/src/Client/Response.cpp index ac8b780..6f03a90 100644 --- a/src/Client/Response.cpp +++ b/src/Client/Response.cpp @@ -43,7 +43,7 @@ void Response::setHeaderBlocks(void) setConnection(); setDate(); setCacheControl(); - // setLocation(void); + setLocation(); // setLanguage(void); // setTransferEncoding(void); } @@ -87,6 +87,12 @@ void Response::setCacheControl(void) _cacheControl = "no-store, no-cache, must-revalidate"; } +void Response::setLocation(void) +{ + if (_code == 301) + _locationSTR = _location->redirect[_code]; +} + //-------------------------------------------------File--------------------------------------- void Response::OpenResponseFile(const char *path) @@ -185,13 +191,24 @@ std::string Response::getFullURI(void) std::string tmp; std::string ret = ""; int len = _location->location.size(); - tmp = _request.getURI().substr(len); - - tmp = _location->root + tmp; - + if (_location->location[0] == '*') + { + 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 (_location->directoryFile.empty()) + if (_location->directoryFile.empty() || _Autoindex) ret = tmp; else { @@ -210,7 +227,7 @@ void Response::generateBody(void) { if (_Autoindex) { - _body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost()); + _body = Autoindex::getPage(_request.getURI(), _fullURI, _request.getHost(), _listen.port); if (_body.empty()) _code = 404; } @@ -229,7 +246,6 @@ void Response::generateBody(void) bool Response::allowedMethod(std::string &method) { - DBOUT << "allowedMethod called" << ENDL; std::vector::iterator it; it = _location->methods.begin(); @@ -259,6 +275,8 @@ void Response::generateHeader(void) ss << "Date: " << _date << "\r\n"; if (!_cacheControl.empty()) ss << "Cache-Control: " << _cacheControl << "\r\n"; + if (!_locationSTR.empty()) + ss << "Location: " << _locationSTR << "\r\n"; ss << "\r\n"; _header = ss.str(); } @@ -276,20 +294,27 @@ void Response::generate() methodPost(); } -void Response::generate2(void) +void Response::generate2(serverListen &l) { - _errorPages = _config->getErrorPages(); - _Autoindex = _location->autoindex; - _code = _request.getCode(); - _hostPort.ip = _config->getHost(); - _hostPort.port = _config->getPort(); - _fullURI = getFullURI(); - _method = _request.getMethod(); + if (_config == NULL) + { + _code = 404; + } + else + { + _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 << RED << "code is " << _code << ENDL; - - if (_request.badCode(_code) || !allowedMethod(_method)) + if (_request.badCode(_code) || !allowedMethod(_method) || isRedirect()) { invalidClient(); 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--------------------------------------- void Response::invalidClient(void) { - OpenErrorFile(_code); + if (!isRedirect()) + OpenErrorFile(_code); setHeaderBlocks(); generateHeader(); diff --git a/src/Client/Response.hpp b/src/Client/Response.hpp index ade66cb..e17e776 100644 --- a/src/Client/Response.hpp +++ b/src/Client/Response.hpp @@ -8,6 +8,7 @@ class Response { private: + serverListen _listen; std::string _body; std::string _header; Request _request; @@ -62,6 +63,7 @@ public: std::string getErrorPage(int code); std::string getFullURI(); + bool isRedirect(void); bool allowedMethod(std::string &); void setData(Request, ServerConfig *); void setData(Request &, ServerConfig *, location *location); @@ -70,7 +72,7 @@ public: void OpenErrorFile(int code); void initErrorCode(void); void generate(); - void generate2(); + void generate2(serverListen &); Response(); ~Response();