From f28bf82e0c1fa8cb9ff97b2681529ee6575d6501 Mon Sep 17 00:00:00 2001 From: Talyx Date: Wed, 19 Jan 2022 17:20:47 +0300 Subject: [PATCH] add Autoindex --- includes/Autoindex.hpp | 20 +++++ includes/Header.hpp | 5 ++ includes/webserv.hpp | 4 + src/Autoindex.cpp | 51 +++++++++++ src/Header.cpp | 46 ++++++++-- www/errorFolder/index1.html | 164 ++++++++++++++++++++++++++++++++++++ 6 files changed, 282 insertions(+), 8 deletions(-) create mode 100644 includes/Autoindex.hpp create mode 100644 src/Autoindex.cpp create mode 100644 www/errorFolder/index1.html diff --git a/includes/Autoindex.hpp b/includes/Autoindex.hpp new file mode 100644 index 0000000..9ed6b9b --- /dev/null +++ b/includes/Autoindex.hpp @@ -0,0 +1,20 @@ +#ifndef AUTOINDEX_HPP +#define AUTOINDEX_HPP + +#include "webserv.hpp" + +class Autoindex +{ +private: + +public: + Autoindex(); + static std::string getPage(std::string path, std::string host); + static std::string getReference(std::string file, std::string dir, std::string host); + ~Autoindex(); +}; + + + + +#endif \ No newline at end of file diff --git a/includes/Header.hpp b/includes/Header.hpp index 0d093f5..2ee99cf 100644 --- a/includes/Header.hpp +++ b/includes/Header.hpp @@ -15,6 +15,7 @@ private: std::string _method; std::string _version; std::string _URI; + std::string _host; std::string _query; std::string _fileToSend; std::map _headerField; @@ -37,6 +38,10 @@ public: int isDir(std::string); void OpenResponsFile(const char *path); + std::string ltrim(std::string); + std::string rtrim(std::string); + std::string trim(std::string); + int parseStartLine(std::string); int parseHeaderfield(std::string); void printHeaderInfo(void); diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 96ed773..ef01b64 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -31,7 +31,11 @@ #include #include #include +#include +#include +#include #include "Socket.hpp" +#include "Autoindex.hpp" #include "Server.hpp" #include "Header.hpp" diff --git a/src/Autoindex.cpp b/src/Autoindex.cpp new file mode 100644 index 0000000..8cf6a96 --- /dev/null +++ b/src/Autoindex.cpp @@ -0,0 +1,51 @@ +#include "Autoindex.hpp" + +Autoindex::Autoindex() +{ +} + +std::string Autoindex::getPage(std::string path, std::string host) +{ + std::string allpath = HOME + path; + DIR *dir = opendir(allpath.c_str()); + struct dirent *dirEnt; + std::string page =\ + "\n\ + \n\ + \n\ + " + allpath + "\n\ + \n\ + \n\ +

" + allpath + "

\n\ +

\n"; + if (dir == NULL) + { + std::cerr << RED << "AutoindexError: could not open \"" + << allpath << "\" directory." << ZERO_C << std::endl; + return ""; + } + // if (allpath[0] != '/') + // path = "/" + path; + for (dirEnt = readdir(dir); dirEnt; dirEnt = readdir(dir)) + { + page = page + getReference(dirEnt->d_name, path, host); + } + page += "

\n\n\n"; + closedir(dir); + return (page); +} + +std::string Autoindex::getReference(std::string file, std::string dir, std::string host) +{ + std::stringstream link; + link << "\t\t

" << file << "

\n"; + return link.str(); +} + +Autoindex::~Autoindex() +{ +} \ No newline at end of file diff --git a/src/Header.cpp b/src/Header.cpp index a933455..90549e8 100644 --- a/src/Header.cpp +++ b/src/Header.cpp @@ -1,5 +1,7 @@ #include "Header.hpp" +#define WHITESPACE " \n\r\t\f\v" + Header::Header() { this->_row = 0; @@ -81,8 +83,8 @@ int Header::parseStartLine(std::string str) return (405); else if (checkURI() < 0) { - if (isDir(_URI) == 0) - return (403); + if (isDir(HOME + _URI) == 0) + return (200); else return (404); } @@ -99,6 +101,7 @@ int Header::parseHeaderfield(std::string str) if (distance < 0 && str != "\r") return 400; key = str.substr(0, distance); + std::transform(key.begin(), key.end(), key.begin(), ::tolower); value = str.erase(0, distance + 1); if (_headerField.find(key) != _headerField.end()) { @@ -106,8 +109,12 @@ int Header::parseHeaderfield(std::string str) } else { - _headerField[key] = value.erase(value.find_last_not_of(" \n\r\t") + 1); + value = value.erase(0, value.find_first_not_of(WHITESPACE)); + value = value.substr(0, value.find_last_not_of(WHITESPACE) + 1); + _headerField[key] = value; } + if (key == "host") + _host = value; return 200; } @@ -135,6 +142,7 @@ int Header::checkURI(void) { const char *path; std::string tmp; + std::string str; int pos; tmp = _URI; @@ -144,9 +152,9 @@ int Header::checkURI(void) _URI = tmp.substr(0, pos); _query = tmp.erase(0, pos + 1); } - _URI = HOME + _URI; - path = _URI.c_str(); - if (isFile(_URI) < 0) + str = HOME + _URI; + path = str.c_str(); + if (isFile(str) < 0) return (-1); return (0); } @@ -222,9 +230,14 @@ int Header::sendHeader(int fd) int Header::sendRespons(int fd) { + std::string path; + + path = HOME + _URI; sendHeader(fd); - if (_ret == 200) - OpenResponsFile(_URI.c_str()); + if (_ret == 200 && isDir(path) == 0) + _fileToSend = Autoindex::getPage(_URI, _host); + else if (_ret == 200) + OpenResponsFile(path.c_str()); else _fileToSend = getErrorPage(_ret); send(fd, _fileToSend.c_str(), _fileToSend.length(), 0); @@ -301,12 +314,29 @@ std::string Header::getReasonPhrase(int code) //-------------------------------------------------Other--------------------------------------- +std::string ltrim(std::string s) +{ + size_t start = s.find_first_not_of(WHITESPACE); + return (start == std::string::npos) ? "" : s.substr(start); +} + +std::string rtrim(std::string s) +{ + size_t end = s.find_last_not_of(WHITESPACE); + return (end == std::string::npos) ? "" : s.substr(0, end + 1); +} + +std::string trim(std::string s) { + return rtrim(ltrim(s)); +} + void Header::printHeaderInfo(void) { std::map::iterator it; std::cout << PINK << "request method = " << _method << ZERO_C << std::endl; std::cout << PINK << "request URI = " << _URI << ZERO_C << std::endl; + std::cout << PINK << "host = " << _host << ZERO_C << std::endl; std::cout << PINK << "request query = " << _query << ZERO_C << std::endl; std::cout << PINK << "request http versioin = " << _version << ZERO_C << std::endl; std::cout << PINK << "request rows = " << _row << ZERO_C << std::endl; diff --git a/www/errorFolder/index1.html b/www/errorFolder/index1.html new file mode 100644 index 0000000..8e63e83 --- /dev/null +++ b/www/errorFolder/index1.html @@ -0,0 +1,164 @@ + + + + +Название сайта + + + + + + + + + +
+ + + + + + + + + + +
+ +

Название сайта (организации)

+

Описание сайта

+ +
+ + + + + + + + + + + + + + + + + + + + + +
+

Страница

+ +

+Здравствуйте уважаемые будущие веб-мастера! +Мне 55 лет и я рад приветствовать Вас на своём сайте. +Этот сайт первый, который я разработал самостоятельно, +а до этого умел только входить в интернет.

+ +

Почему я решил его сделать? +За те 3 месяца, пока разбирался в +сайтостроении и создавал этот ресурс обнаружилось, +что авторы руководств по созданию +сайтов считают многие нюансы само собой разумеющимися +и не обращают на них внимание +А мне, учитывая возраст и «опыт», было не просто +понять как раз эти нюансы, они отнимали больше всего +времени.

+ +
+

Меню

+ +

+ + + + + +Страница + +

+

+ + +Страница 1 +

+

+ + +Страница 2 +

+ +
+

Общая информация

+

Текст общей информации или реклама

+ +
+ + + + + + + + + + +
+

Подвал

+ +
+ +
+ + \ No newline at end of file