You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.8 KiB

2 years ago
/**
* @file Route.cpp
* @brief A location class which handle
* @author Narnaud
* @version 0.1
* @date 2023-01-12
*/
#include "webserv.hpp"
2 years ago
/**
* @brief Constructor
*
2 years ago
* A route is an object which define how to handle a request. Each Server is Route inherited and each location block
* lead to a new Route object.
*
* @param server The Server parent of the route. NULL if the object is the server.
* @param location The uri associatied to the route.
* @param datas The JSONNode giving configuration.
*/
2 years ago
Route::Route(Server *server, string location, JSONNode *datas) : _server(server), _location(location) {
2 years ago
JSONObject object = datas->obj();
2 years ago
JSONNode *tmp;
2 years ago
_autoindex = false;
_client_max_body_size = -1;
2 years ago
if ((tmp = object["root"])) _root = tmp->str();
if ((tmp = object["return"])) _ret = tmp->str();
if ((tmp = object["autoindex"])) _autoindex = tmp->boo();
2 years ago
if ((tmp = object["indexs"])) {
JSONList indexs = tmp->lst();
2 years ago
for (JSONList::iterator it = indexs.begin(); it < indexs.end(); it++) _indexs.push_back((*it)->str());
2 years ago
}
2 years ago
if ((tmp = object["allowed_methods"])) {
2 years ago
JSONList headers = tmp->lst();
2 years ago
for (JSONList::iterator it = headers.begin(); it < headers.end(); it++) _allowed_methods.push_back((*it)->str());
2 years ago
}
2 years ago
if ((tmp = object["cgi"])) {
2 years ago
JSONObject cgis = tmp->obj();
2 years ago
for (JSONObject::iterator it = cgis.begin(); it != cgis.end(); it++) _cgi[(*it).first] = (*it).second->str();
2 years ago
}
2 years ago
if ((tmp = object["client_max_body_size"])) _client_max_body_size = tmp->nbr();
}
2 years ago
/// @brief Destructor
Route::~Route(void) {}
2 years ago
// Getters
string Route::getLocation(void) { return _location; }
string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; }
2 years ago
/**
* @brief Search for an index while generating autoindex
*
* @param uri The uri requested by client.
* @param path The correct path associated with uri.
*
2 years ago
* @return The index content to give to client or an empty string if there is nothing for him.
*/
string Route::getIndex(string uri, string path) {
2 years ago
std::stringstream body, ret;
DIR *dir;
struct dirent *entry;
struct stat info;
2 years ago
vec_string::iterator it;
2 years ago
if ((dir = opendir(path.c_str()))) {
if (DEBUG) cout << "get index(): path=" << path << "\n";
body << "<h3 style=\"text-align: center;\">" << path << " files :</h3>\n<ul>\n";
2 years ago
while ((entry = readdir(dir)) != NULL) {
2 years ago
if (entry->d_name[0] == '.') continue;
for (it = _indexs.begin(); it < _indexs.end(); it++) {
2 years ago
if (entry->d_name == *it) return (read_file(path + "/" + *it));
2 years ago
}
2 years ago
body << "<li><a href=\"" << uri + "/" + entry->d_name << "\">" << entry->d_name << "</a></li>\n";
if (stat(path.c_str(), &info) != 0) std::cerr << "stat() error on " << path << ": " << strerror(errno) << "\n";
2 years ago
}
2 years ago
body << "</ul>";
2 years ago
closedir(dir);
}
2 years ago
if (!dir || !_autoindex) return "";
if (DEBUG) cout << "Getting autoindex\n";
2 years ago
ret << "Content-type: text/html \r\n";
2 years ago
ret << "Content-length: " << body.str().length() << "\r\n";
ret << "\r\n" << body.str();
2 years ago
return ret.str();
}
2 years ago
/**
* @brief Find the local path corresponding to the uri asked by te client.
*
* @param uri The uri asked by the client.
*
2 years ago
* @return The local path.
* @deprecated Not used by nginx until config use rewrite keyword.
*/
string Route::correctUri(string uri) {
2 years ago
std::stringstream ret;
2 years ago
vec_string::iterator loc_word, uri_word;
2 years ago
vec_string loc_words = split(_location, "/");
vec_string uri_words = split(uri, "/");
uri_word = uri_words.begin();
for (loc_word = loc_words.begin(); loc_word < loc_words.end(); loc_word++) {
2 years ago
while (uri_word < uri_words.end() && *uri_word == "") uri_word++;
while (loc_word < loc_words.end() && *loc_word == "") loc_word++;
if (loc_word != loc_words.end()) uri_word++;
2 years ago
}
2 years ago
ret << "./" << _root;
2 years ago
while (uri_word < uri_words.end()) ret << "/" << *(uri_word++);
2 years ago
return ret.str();
}