diff --git a/assets/index.html b/assets/index.html index f024418..9bc27ed 100644 --- a/assets/index.html +++ b/assets/index.html @@ -262,13 +262,6 @@

Special Files

Archive File - -

GET Method Tests

-
- Get HTML File - Get CSS File - Get Image File -

POST Method Test

@@ -343,13 +336,6 @@

Directory Tests

Empty Directory Test -

Static Files

-
- HTML File - Text File - PDF File - JSON File -
diff --git a/conf/default.conf b/conf/default.conf index 0a05500..4415665 100644 --- a/conf/default.conf +++ b/conf/default.conf @@ -1,5 +1,5 @@ server { - listen 0.0.0.0:8081; + listen 0.0.0.0:8001; server_name localhost; root ./; autoindex on; diff --git a/conf/redirect.conf b/conf/redirect.conf new file mode 100644 index 0000000..76b2e41 --- /dev/null +++ b/conf/redirect.conf @@ -0,0 +1,79 @@ +server { + listen 0.0.0.0:9050; + server_name localhost; + root ./; + autoindex on; + client_max_body_size 1m; + index assets/index.html; + use_chunked_encoding true; + chunk_size 100; + allowed_redirects 301 https://google.com; + +# Custom error pages + error_page 404 /error_page/404.html; + error_page 403 /error_page/403.html; + error_page 500 /error_page/500.html; + +# Main site and directory listing test + location /assets/ { + root ./; + index old_index.html; + allowed_methods GET; + autoindex on; + } + + location /cgi-bin/ { + root ./; + allowed_methods GET POST; + cgi_path /usr/bin/python3 /usr/bin/php; + cgi_ext .py .php; + autoindex off; + } + + location /uploads/ { + root ./; + allowed_methods POST GET DELETE; + } +} + +server { + listen 0.0.0.0:9051; + server_name localhost; + root ./; + autoindex on; + client_max_body_size 1m; + index assets/index.html; + use_chunked_encoding true; + chunk_size 100; + +# Custom error pages + error_page 404 /error_page/404.html; + error_page 403 /error_page/403.html; + error_page 500 /error_page/500.html; + +# Main site and directory listing test + location /assets/ { + root ./; + index old_index.html; + allowed_methods GET; + autoindex on; + } + + location /google/ { + allowed_redirects 301 https://google.com; + } + + location /cgi-bin/ { + root ./; + allowed_methods GET POST; + cgi_path /usr/bin/python3 /usr/bin/php; + cgi_ext .py .php; + autoindex off; + } + + location /uploads/ { + root ./; + allowed_methods POST GET DELETE; + } + +} diff --git a/inc/Config.hpp b/inc/Config.hpp index 0300542..ae0cd77 100644 --- a/inc/Config.hpp +++ b/inc/Config.hpp @@ -35,7 +35,7 @@ class Config { void setServerConf( ServerConf server ); //member fucnitons - void parse( std::string& filename ); + void parse( std::string& filename ); void parseServerConfBlock( ServerConf& server ); void parseLocationConfBlock( ServerConf&, std::stringstream& ); }; diff --git a/inc/HttpHelpers.hpp b/inc/HttpHelpers.hpp deleted file mode 100644 index a82bf16..0000000 --- a/inc/HttpHelpers.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef HTTP_HELPERS_HPP -#define HTTP_HELPERS_HPP - -#include - -std::string urlDecoder(const std::string& encodedUrl); - -#endif \ No newline at end of file diff --git a/inc/HttpRequest.hpp b/inc/HttpRequest.hpp index b15499a..3207dd0 100644 --- a/inc/HttpRequest.hpp +++ b/inc/HttpRequest.hpp @@ -20,6 +20,7 @@ class HttpRequest { //cgi response string std::string _cgiResponseString; std::string _hostname; + int _port; // reference to which serverConf i have matched from request ServerConf _server; @@ -46,6 +47,7 @@ class HttpRequest { ServerConf getServer() const; PathInfo getPathInfo() const; std::string getCgiResponseString() const; + int getPort() const; //setters void setMethod( std::string ); @@ -59,6 +61,7 @@ class HttpRequest { void setServer( ServerConf ); void setPathInfo( PathInfo ); void setCgiResponseString(const std::string& cgiResponseString); + void setPort(const int port); // main handler void handleRequest(const std::string& rawRequest); diff --git a/inc/Response.hpp b/inc/Response.hpp index 6dd7c2a..ac5c9bf 100644 --- a/inc/Response.hpp +++ b/inc/Response.hpp @@ -27,6 +27,8 @@ class Response { std::string _reasonPhrase; const ServerConf* _serverConf; LocationConf* _locationConf; + // for redirects + std::string _redirectDest; // to make cookies work // cookie value we want to have @@ -62,6 +64,10 @@ class Response { bool serveRootIndexfile(HttpRequest& ReqObj, std::string fullPath); bool serveLocationIndex(HttpRequest& ReqObj); + + + // Handlers + void HandleRedirectRequest(HttpRequest& ReqObj, std::map redirect); void HandleDeleteRequest(HttpRequest& ReqObj, PathInfo& pathInfo); void HandlePostRequest(HttpRequest& ReqObj,PathInfo& pathinfo); void genarateUploadSucces(HttpRequest& ReqObj); @@ -80,6 +86,7 @@ class Response { std::string getServerName(); std::string getCurrentDateTime(); std::string getContentType(HttpRequest &reqObj, const std::string& extension); + std::string getRedirectDest(); // Setters void setHttpResponse(const std::string& httpResponse) { _httpResponse = httpResponse; } @@ -91,6 +98,7 @@ class Response { void setFilename(const std::string& filename) { _filename = filename; } void setStatusCode(int statusCode) { m_statusCode = statusCode; } void setReasonPhrase(const std::string &reasonPhrase); + void setRedirectDest(const std::string& redirectDest) { _redirectDest = redirectDest; }; // for cookie setter void setSetCookieValue(std::string value); diff --git a/src/Config/ServerConfAccess.cpp b/src/Config/ServerConfAccess.cpp index 45ac37f..9ffc283 100644 --- a/src/Config/ServerConfAccess.cpp +++ b/src/Config/ServerConfAccess.cpp @@ -32,11 +32,11 @@ void ServerConf::checkAccess( void ){ throw( std::runtime_error( \ "RootDir: " + _rootDir + " not accsessible" ) ); - //check every location - for( std::vector< LocationConf >::iterator it = _locations.begin(); \ - it != _locations.end(); it++ ){ - it->checkAccess( _rootDir ); - } + // //check every location + // for( std::vector< LocationConf >::iterator it = _locations.begin(); + // it != _locations.end(); it++ ){ + // it->checkAccess( _rootDir ); + // } //check every errorpage path for( std::map< int, std::string >::iterator it = _errorPages.begin(); \ diff --git a/src/Http/HttpHelpers.cpp b/src/Http/HttpHelpers.cpp deleted file mode 100644 index 32f8045..0000000 --- a/src/Http/HttpHelpers.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "../inc/HttpHelpers.hpp" - -#include -#include - -std::string urlDecoder(const std::string& encodedUrl) { - std::string decodedUrl; - - for (size_t i = 0; i < encodedUrl.length(); ++i) { - if (encodedUrl[i] == '%' && i + 2 < encodedUrl.length()) { - int value; - std::istringstream is(encodedUrl.substr(i + 1, 2)); - if (is >> std::hex >> value) - decodedUrl += static_cast(value); - i += 2; - } - else if (encodedUrl[i] == '+') - decodedUrl += ' '; - else - decodedUrl += encodedUrl[i]; - } - - return decodedUrl; -} \ No newline at end of file diff --git a/src/Http/HttpRequest.cpp b/src/Http/HttpRequest.cpp index 66de732..486ca84 100644 --- a/src/Http/HttpRequest.cpp +++ b/src/Http/HttpRequest.cpp @@ -3,6 +3,7 @@ HttpRequest::HttpRequest(Config& conf): _conf(conf), _response_code(200), + _port(-1), _pathInfo() { _cgiResponseString = ""; @@ -56,6 +57,10 @@ std::string HttpRequest::getHostname() const { return _hostname; } +int HttpRequest::getPort() const { + return _port; +} + Config& HttpRequest::getConf() const { return _conf; } @@ -139,6 +144,10 @@ std::string HttpRequest::getBody() const { return _body; } +void HttpRequest::setPort(int port) { + _port = port; +} + // overload for printing std::ostream& operator<<(std::ostream& os, HttpRequest& request) { os << "Version: " << request.getVersion() << "\n"; @@ -168,6 +177,7 @@ void HttpRequest::parseHeaders(const std::string& rawRequest) { setResponseCode(400); return; } + std::istringstream request_line(line); std::string method, uri, version; request_line >> method >> uri >> version; @@ -175,6 +185,7 @@ void HttpRequest::parseHeaders(const std::string& rawRequest) { setResponseCode(400); return; } + if (version != "HTTP/1.1") { setResponseCode(505); return; @@ -309,20 +320,23 @@ void HttpRequest::parseBody(const std::string& rawRequest) { } // // match server block from conf +// this doesnt even work lol, only checks for hostname in IPS?? void HttpRequest::matchServerBlock(void) { - std::vector server_list; - server_list = _conf.getServerConfs(); - - // loop over serverConfs and match server names and ips - for (std::vector::iterator it = server_list.begin(); it != server_list.end(); ++it) { - std::vector server_names = it->getServerConfNames(); - std::map > ipPorts = it->getIpPort(); - - if (std::find(server_names.begin(), server_names.end(), _hostname) != server_names.end()) { - _server = (*it); - } - if (ipPorts.find(_hostname) != ipPorts.end()) { - _server = (*it); + std::vector serverTmpConf = _conf.getServerConfs(); + + // Match server based on hostname and port + for (size_t i = 0; i < serverTmpConf.size(); i++) { + // Check server names + const std::vector& serverNames = serverTmpConf[i].getServerConfNames(); + if (std::find(serverNames.begin(), serverNames.end(), _hostname) != serverNames.end()) { + // Check if port matches too + const std::map > ports = serverTmpConf[i].getIpPort(); + for (std::map >::const_iterator it = ports.begin(); it != ports.end(); ++it) { + if (it->second.find(_port) != it->second.end()) { + _server = serverTmpConf[i]; + return; + } + } } } } @@ -346,14 +360,16 @@ void HttpRequest::handleRequest(const std::string& rawRequest) { // extract hostname // hostname = remove port from host if present size_t colon = _hostname.find(":"); - if (colon != std::string::npos) + if (colon != std::string::npos) { + std::stringstream ss(_hostname.substr(colon + 1)); + ss >> _port; + std::cout << "PORT: " << _port << '\n'; _hostname = _hostname.substr(0, colon); + } // match server block from conf matchServerBlock(); - //std::cout << "raw req string :" << rawRequest << ":\n"; - // validate and load into PathInfo obj validateRequestPath(); if (_response_code != 200) return ; @@ -362,10 +378,6 @@ void HttpRequest::handleRequest(const std::string& rawRequest) { void HttpRequest::validateRequestPath(void) { const std::vector& locationConfs = _server.getLocationConfs(); const std::string uri = getUri(); - - // Add null checks and debugging - //std::cout << "Processing URI: " << uri << std::endl; - //std::cout << "Server root: " << _server.getRootDir() << std::endl; // Protect against empty/null values if (_server.getRootDir().empty()) { @@ -411,9 +423,7 @@ void HttpRequest::validateRequestPath(void) { } _pathInfo.parsePath(); - - if ((_response_code = _pathInfo.validatePath()) != 200) - return; + _pathInfo.validatePath(); } @@ -439,94 +449,4 @@ std::string HttpRequest::getConnectionType() const { } return connectionType; -} - - -/* -void HttpRequest::validateRequestPath(void) { - const std::vector& locationConfs = _server.getLocationConfs(); - std::string bestMatch = ""; - const LocationConf* matchedLoc = NULL; - const std::string uri = getUri(); - - // Set PathInfo before location matching - std::string full_path = _server.getRootDir() + uri; - _pathInfo = PathInfo(full_path); - _pathInfo.parsePath(); // Parse the path components regardless of validation - - // Loop over location confs - for (std::vector::const_iterator it = locationConfs.begin(); - it != locationConfs.end(); ++it) { - const LocationConf& loc = *it; - std::string locPath = loc.getPath(); - if (uri.substr(0, locPath.length()) == locPath) { - if (locPath.length() > bestMatch.length()) { - bestMatch = locPath; - matchedLoc = &loc; - } - } - } - - // If no location match, set 404 but keep path info - if (!matchedLoc) { - _response_code = 404; - return; - } - - // Only validate the path exists if we found a matching location - if ((_response_code = _pathInfo.validatePath()) != 200) - return; -} - - - -void HttpRequest::validateRequestPath(void) { - // grab location confs - - // things we need in for loop - const std::vector& locationConfs = _server.getLocationConfs(); - std::string bestMatch = ""; - const LocationConf* matchedLoc = NULL; - const std::string uri = getUri(); - - //std::cout << "Debug: URI to match: " << uri << std::endl; - //std::cout << "Debug: Number of location configs: " << locationConfs.size() << std::endl; - - // loop over location confs - for (std::vector::const_iterator it = locationConfs.begin(); - it != locationConfs.end(); ++it) { - const LocationConf& loc = *it; - std::string locPath = loc.getPath(); - // Check if URI starts with location path - if (uri.substr(0, locPath.length()) == locPath) { - // keep longest match (most specific). Ex: /posts/ or posts/articles - if (locPath.length() > bestMatch.length()) { - bestMatch = locPath; - matchedLoc = &loc; - } - } - } - // if we couldnt match a location from the locationConfs in the server - if (!matchedLoc) { - _response_code = 404; - return; - } - - // oarse full path from reuqest uri - std::string full_path = _server.getRootDir() + uri; - size_t question_mark = full_path.find("?"); - if (question_mark != std::string::npos) - full_path = full_path.substr(0, question_mark); - - // std::cout << "Debug fullPath:" << full_path << '\n'; - // load full path into PathInfo obj - _pathInfo = PathInfo(full_path); - - // validates path and set reponse code to return val - if ((_response_code = _pathInfo.validatePath()) != 200) - return; - // parse path and set reponse code to return val - if ((_response_code = _pathInfo.parsePath()) != 200) - return; -} -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/Http/PathInfo.cpp b/src/Http/PathInfo.cpp index d56f66c..6b1a622 100644 --- a/src/Http/PathInfo.cpp +++ b/src/Http/PathInfo.cpp @@ -89,73 +89,7 @@ PathInfo& PathInfo::operator=(const PathInfo& other) { PathInfo::~PathInfo() { } -/*changed function beacuse i need path info even if path is invalid for indexing xoxo linus -int PathInfo::validatePath() { - - // check for invalid path traversal ie -> "../../" - if (_fullPath.find("..") != std::string::npos) - return 403; - - struct stat statbuf; - if (stat(_fullPath.c_str(), &statbuf) != 0) { - return 404; // path doesn't exist or error accessing it - } - // set file properties - _isDirectory = S_ISDIR(statbuf.st_mode); - _isFile = S_ISREG(statbuf.st_mode); - - // check dir with open dir too - if (_isDirectory) { - DIR* dir = opendir(_fullPath.c_str()); - if (dir == NULL) { - return 404; - } - closedir(dir); - } - - // check permissions - if (_isDirectory) { - // directory needs read and execute permissions - if (!(statbuf.st_mode & S_IRUSR) || !(statbuf.st_mode & S_IXUSR)) - return 403; - } else if (_isFile) { - // regular file needs read permission - if (!(statbuf.st_mode & S_IRUSR)) - return 403; - } - return 200; -} - - - -int PathInfo::parsePath() { - - // find the last directory separator - size_t lastSlash = _fullPath.find_last_of("/\\"); - if (lastSlash != std::string::npos) { - _dirName = _fullPath.substr(0, lastSlash); - _fileName = _fullPath.substr(lastSlash + 1); - } else { - _fileName = _fullPath; - } - - // find the last dot to get the file extension - size_t lastDot = _fileName.find_last_of('.'); - if (lastDot != std::string::npos) { - _extension = _fileName.substr(lastDot + 1); - _fileName = _fileName.substr(0, lastDot); - } - - // additional validation for directory and filename - if (_dirName.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/\\._-") != std::string::npos) - return 400; - if (_fileName.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._-") != std::string::npos) - return 400; - - return 200; -} -*/ int PathInfo::validatePath() { // First parse the path components - do this before validation parsePath(); diff --git a/src/Response/Response.cpp b/src/Response/Response.cpp index e859f98..509c66f 100644 --- a/src/Response/Response.cpp +++ b/src/Response/Response.cpp @@ -5,7 +5,7 @@ Response::~Response(){} Response::Response() - : _serverConf(NULL), _locationConf(NULL), _setCookieValue("") { + : _serverConf(NULL), _locationConf(NULL), _redirectDest("") ,_setCookieValue("") { // Default constructor implementation } @@ -17,8 +17,12 @@ std::string Response::getReasonPhrase() const { return _reasonPhrase; } -Response::Response(HttpRequest& reqObj,ServerConf* serverConf) - : _serverConf(serverConf), _locationConf(NULL), _setCookieValue(""){ +std::string Response::getRedirectDest() { + return _redirectDest; +} + +Response::Response(HttpRequest& reqObj, ServerConf* serverConf) + : _serverConf(serverConf), _locationConf(NULL), _redirectDest(""), _setCookieValue("") { // Get locations from server config std::string uri = reqObj.getUri(); @@ -31,9 +35,6 @@ Response::Response(HttpRequest& reqObj,ServerConf* serverConf) it != locations.end(); ++it) { std::string locPath = it->getPath(); - // DEBUG - // std::cout << "DEBUG - Checking location: '" << it->getPath() << "'" << std::endl; - if (uri.find(locPath) == 0) { // URI starts with location path if (locPath.length() > bestMatch.length()) { bestMatch = locPath; @@ -42,15 +43,6 @@ Response::Response(HttpRequest& reqObj,ServerConf* serverConf) } } - // DEBUG - // std::cout << "URI: " << uri << std::endl; - // std::cout << "Server root: " << _serverConf->getRootDir() << std::endl; - // if (_locationConf) { - // std::cout << "Location path: " << _locationConf->getPath() << std::endl; - // std::cout << "Location root: " << _locationConf->getRootDir() << std::endl; - // } - //std::cout << "Final path being used: " << pathInfo.getFullPath() << std::endl; - //std::cout << reqObj << "\n"; processResponse(reqObj); generateHttpresponse(reqObj); } @@ -70,7 +62,14 @@ void Response::generateHttpresponse(HttpRequest &reqObj) { //header << "Cache-Control: " << headerMap["Cache-Control"] << "\r\n"; // needed for cookies to work - header << "Set-Cookie: " << headerMap["Set-Cookie"] << "\r\n"; + // only if setCookieValue has been changed + if (_setCookieValue != "") { + header << "Set-Cookie: " << headerMap["Set-Cookie"] << "\r\n"; + } + // to set location (esp for redirects) + if (_redirectDest != "") { + header << "Location: " << headerMap["Location"] << "\r\n"; + } // std::cout << "SetCookie found: " << headerMap["Set-Cookie"] << '\n'; //header << "Last-Modified: " << headerMap["Last-Modified"] << "\r\n"; @@ -81,9 +80,7 @@ void Response::generateHttpresponse(HttpRequest &reqObj) { setHttpResponse(responseString); } - - -std::string Response::getServerName(){ +std::string Response::getServerName() { std::vector server = _serverConf->getServerConfNames(); // Check if the vector has any elements @@ -110,12 +107,6 @@ void Response::generateErrorResponse(HttpRequest &reqObj) { // Retrieve the error pages map const std::map& errorPages = _serverConf->getErrorPages(); - // ONLY NEEDED DURING DEBUGGING: COMMENTING OUT - // // Loop over the error pages and print them - // for (std::map::const_iterator it = errorPages.begin(); it != errorPages.end(); ++it) { - // std::cout << "Error Code: " << it->first << ", Error Page: " << it->second << std::endl; - // } - // check request obj for the code int responseCode = reqObj.getResponseCode(); std::map::const_iterator customPageIt = errorPages.find(responseCode); @@ -202,19 +193,36 @@ void Response::processResponse(HttpRequest &ReqObj){ return; } + // redirect request would override all request heirarchy + // for example we could perform a GET, POST or DELETE in an old folder and that request would need to be redirected and passed on + // get map of location redirects + std::map locationRedirect; + if (_locationConf) + locationRedirect = _locationConf->getAllowedRedirects(); + // get map of server redirects + std::map serverRedirects = _serverConf->getAllowedRedirects(); + + // handlers + if (!locationRedirect.empty() || !serverRedirects.empty()) { + std::cout << "REDIRECT" << '\n'; + // ternary operator evaluate which one to pass to handle redirect + std::map redirect = locationRedirect.empty() ? serverRedirects : locationRedirect; + HandleRedirectRequest(ReqObj, redirect); + } + // GET REQUEST HANDLER - if (ReqObj.getMethod() == "GET") { - std::cout << "GET REQUEST" << std::endl; + else if (ReqObj.getMethod() == "GET") { + std::cout << "GET REQUEST" << '\n'; HandleGetRequest(ReqObj,pathInfo); } // POST REQUEST HANDLER else if (ReqObj.getMethod() == "POST") { - std::cout << "POST REQUEST" << std::endl; + std::cout << "POST REQUEST" << '\n'; HandlePostRequest(ReqObj, pathInfo); } // HANDLE DELETE request else if (ReqObj.getMethod() == "DELETE") { - std::cout << "DELETE REQUEST" << std::endl; + std::cout << "DELETE REQUEST" << '\n'; HandleDeleteRequest(ReqObj, pathInfo); } @@ -223,8 +231,6 @@ void Response::processResponse(HttpRequest &ReqObj){ catch(int error) { ReqObj.setResponseCode(error); - // for debugging - // std::cout << "CODE " << ReqObj.getResponseCode() << ":\n"; generateErrorResponse(ReqObj); } @@ -232,4 +238,4 @@ void Response::processResponse(HttpRequest &ReqObj){ void Response::setSetCookieValue(std::string value) { _setCookieValue = value; -} +} \ No newline at end of file diff --git a/src/Response/get_Response.cpp b/src/Response/get_Response.cpp index 7936a0e..130af81 100644 --- a/src/Response/get_Response.cpp +++ b/src/Response/get_Response.cpp @@ -93,7 +93,7 @@ void Response::HandleGetRequest(HttpRequest& ReqObj, PathInfo& pathInfo) { return; } - if(pathInfo.isFile()){ + if(pathInfo.isFile()) { if(serveFileIfExists(fullPath, ReqObj) == true) return; else{ diff --git a/src/Response/helpers_Response.cpp b/src/Response/helpers_Response.cpp index 1dafe4e..fd91cbc 100644 --- a/src/Response/helpers_Response.cpp +++ b/src/Response/helpers_Response.cpp @@ -79,6 +79,11 @@ void Response::generateHeader(HttpRequest &reqObj){ //headerMap["ETag"] = "\"123456789\""; // Example value //headerMap["Location"] = "/new-resource"; // Example value + // for redirects + if (reqObj.getResponseCode() == 301 || reqObj.getResponseCode() == 302) { + headerMap["Location"] = getRedirectDest(); + } + if(reqObj.getResponseCode() == 201){//for Post methode specify headers headerMap["Location"] = reqObj.getUri(); // Stay on same page headerMap["Content-Type"] = "text/html; charset=utf-8"; // Specify charset diff --git a/src/Response/post_Response.cpp b/src/Response/post_Response.cpp index cd98c71..9ebca23 100644 --- a/src/Response/post_Response.cpp +++ b/src/Response/post_Response.cpp @@ -32,7 +32,6 @@ int Response::checkContentLength(HttpRequest& ReqObj){ size_t maxBodySize = _serverConf->getBodySize(); //_locationConf->getClientMaxBodySize() : // If location config exists, use its limit->> dont have that parsed idk if we wna tto include //_serverConf->getClientMaxBodySize(); // Otherwise, use server's default limit - std::cout << "C0ntent size " << contentLength << " and max body len " << maxBodySize << "\n"; if (contentLength > maxBodySize) { throw 413; return 413; diff --git a/src/Response/redirect_response.cpp b/src/Response/redirect_response.cpp new file mode 100644 index 0000000..0aed9c6 --- /dev/null +++ b/src/Response/redirect_response.cpp @@ -0,0 +1,27 @@ +#include "webserv.hpp" + +void Response::HandleRedirectRequest(HttpRequest& ReqObj, std::map redirect) { + int code; + std::string dest; + + // iterator for redirect access + std::map::iterator it; + it = redirect.begin(); + + // convert str to int for red code + std::stringstream ss(it->first); + ss >> code; + + // set dest address + // we skip first character becuase moritz puts in a / at the beginning + dest = it->second.substr(1, it->second.length()); + + // set req code to the first string (301, 302) of redirect vec + ReqObj.setResponseCode(code); + + // set _redirectDest to dest. this gets returned as Location in headers + setRedirectDest(dest); + + // empty body on redirects + setBody(""); +} \ No newline at end of file diff --git a/src/Server/ServerRun.cpp b/src/Server/ServerRun.cpp index b518d83..363c2b4 100644 --- a/src/Server/ServerRun.cpp +++ b/src/Server/ServerRun.cpp @@ -274,15 +274,14 @@ static void checkEvents( Server& server, Client* client, struct epoll_event& ev // check server names const std::vector& serverNames = serverTMPConf[i].getServerConfNames(); if (std::find(serverNames.begin(), serverNames.end(), host) != serverNames.end()) { - selectedServerConf = &serverTMPConf[i]; - break; - } - - // check server ips - const std::map >& ipPorts = serverTMPConf[i].getIpPort(); - if (ipPorts.find(host) != ipPorts.end()) { - selectedServerConf = &serverTMPConf[i]; - break; + // check if port matches too + const std::map > ports = serverTMPConf[i].getIpPort(); + for (std::map >::const_iterator it = ports.begin(); it != ports.end(); ++it) { + if (it->second.find(request.getPort()) != it->second.end()) { + selectedServerConf = &serverTMPConf[i]; + break; + } + } } } diff --git a/uploads/chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default.desktop b/uploads/chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default.desktop new file mode 100644 index 0000000..f3140e1 --- /dev/null +++ b/uploads/chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default.desktop @@ -0,0 +1,9 @@ +#!/usr/bin/env xdg-open +[Desktop Entry] +Version=1.0 +Terminal=false +Type=Application +Name=Docs +Exec=/opt/google/chrome/google-chrome --profile-directory=Default --app-id=mpnpojknpmmopombnjdcgaaiekajbnjb +Icon=chrome-mpnpojknpmmopombnjdcgaaiekajbnjb-Default +StartupWMClass=crx_mpnpojknpmmopombnjdcgaaiekajbnjb