mirror of
https://github.com/3lswear/webserv.git
synced 2025-10-29 05:17:59 +03:00
add Autoindex
This commit is contained in:
20
includes/Autoindex.hpp
Normal file
20
includes/Autoindex.hpp
Normal file
@@ -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
|
||||||
@@ -15,6 +15,7 @@ private:
|
|||||||
std::string _method;
|
std::string _method;
|
||||||
std::string _version;
|
std::string _version;
|
||||||
std::string _URI;
|
std::string _URI;
|
||||||
|
std::string _host;
|
||||||
std::string _query;
|
std::string _query;
|
||||||
std::string _fileToSend;
|
std::string _fileToSend;
|
||||||
std::map<std::string, std::string> _headerField;
|
std::map<std::string, std::string> _headerField;
|
||||||
@@ -37,6 +38,10 @@ public:
|
|||||||
int isDir(std::string);
|
int isDir(std::string);
|
||||||
void OpenResponsFile(const char *path);
|
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 parseStartLine(std::string);
|
||||||
int parseHeaderfield(std::string);
|
int parseHeaderfield(std::string);
|
||||||
void printHeaderInfo(void);
|
void printHeaderInfo(void);
|
||||||
|
|||||||
@@ -31,7 +31,11 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cctype>
|
||||||
#include "Socket.hpp"
|
#include "Socket.hpp"
|
||||||
|
#include "Autoindex.hpp"
|
||||||
#include "Server.hpp"
|
#include "Server.hpp"
|
||||||
#include "Header.hpp"
|
#include "Header.hpp"
|
||||||
|
|
||||||
|
|||||||
51
src/Autoindex.cpp
Normal file
51
src/Autoindex.cpp
Normal file
@@ -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 =\
|
||||||
|
"<!DOCTYPE html>\n\
|
||||||
|
<html>\n\
|
||||||
|
<head>\n\
|
||||||
|
<title>" + allpath + "</title>\n\
|
||||||
|
</head>\n\
|
||||||
|
<body>\n\
|
||||||
|
<h1>" + allpath + "</h1>\n\
|
||||||
|
<p>\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 += "</p>\n</body>\n</html>\n";
|
||||||
|
closedir(dir);
|
||||||
|
return (page);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Autoindex::getReference(std::string file, std::string dir, std::string host)
|
||||||
|
{
|
||||||
|
std::stringstream link;
|
||||||
|
link << "\t\t<p><a href=\"http://" << host
|
||||||
|
<< dir;
|
||||||
|
if (dir[dir.length() - 1] != '/')
|
||||||
|
link << "/";
|
||||||
|
link << file << "\">" << file << "</a></p>\n";
|
||||||
|
return link.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
Autoindex::~Autoindex()
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "Header.hpp"
|
#include "Header.hpp"
|
||||||
|
|
||||||
|
#define WHITESPACE " \n\r\t\f\v"
|
||||||
|
|
||||||
Header::Header()
|
Header::Header()
|
||||||
{
|
{
|
||||||
this->_row = 0;
|
this->_row = 0;
|
||||||
@@ -81,8 +83,8 @@ int Header::parseStartLine(std::string str)
|
|||||||
return (405);
|
return (405);
|
||||||
else if (checkURI() < 0)
|
else if (checkURI() < 0)
|
||||||
{
|
{
|
||||||
if (isDir(_URI) == 0)
|
if (isDir(HOME + _URI) == 0)
|
||||||
return (403);
|
return (200);
|
||||||
else
|
else
|
||||||
return (404);
|
return (404);
|
||||||
}
|
}
|
||||||
@@ -99,6 +101,7 @@ int Header::parseHeaderfield(std::string str)
|
|||||||
if (distance < 0 && str != "\r")
|
if (distance < 0 && str != "\r")
|
||||||
return 400;
|
return 400;
|
||||||
key = str.substr(0, distance);
|
key = str.substr(0, distance);
|
||||||
|
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
|
||||||
value = str.erase(0, distance + 1);
|
value = str.erase(0, distance + 1);
|
||||||
if (_headerField.find(key) != _headerField.end())
|
if (_headerField.find(key) != _headerField.end())
|
||||||
{
|
{
|
||||||
@@ -106,8 +109,12 @@ int Header::parseHeaderfield(std::string str)
|
|||||||
}
|
}
|
||||||
else
|
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;
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,6 +142,7 @@ int Header::checkURI(void)
|
|||||||
{
|
{
|
||||||
const char *path;
|
const char *path;
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
|
std::string str;
|
||||||
int pos;
|
int pos;
|
||||||
|
|
||||||
tmp = _URI;
|
tmp = _URI;
|
||||||
@@ -144,9 +152,9 @@ int Header::checkURI(void)
|
|||||||
_URI = tmp.substr(0, pos);
|
_URI = tmp.substr(0, pos);
|
||||||
_query = tmp.erase(0, pos + 1);
|
_query = tmp.erase(0, pos + 1);
|
||||||
}
|
}
|
||||||
_URI = HOME + _URI;
|
str = HOME + _URI;
|
||||||
path = _URI.c_str();
|
path = str.c_str();
|
||||||
if (isFile(_URI) < 0)
|
if (isFile(str) < 0)
|
||||||
return (-1);
|
return (-1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@@ -222,9 +230,14 @@ int Header::sendHeader(int fd)
|
|||||||
|
|
||||||
int Header::sendRespons(int fd)
|
int Header::sendRespons(int fd)
|
||||||
{
|
{
|
||||||
|
std::string path;
|
||||||
|
|
||||||
|
path = HOME + _URI;
|
||||||
sendHeader(fd);
|
sendHeader(fd);
|
||||||
if (_ret == 200)
|
if (_ret == 200 && isDir(path) == 0)
|
||||||
OpenResponsFile(_URI.c_str());
|
_fileToSend = Autoindex::getPage(_URI, _host);
|
||||||
|
else if (_ret == 200)
|
||||||
|
OpenResponsFile(path.c_str());
|
||||||
else
|
else
|
||||||
_fileToSend = getErrorPage(_ret);
|
_fileToSend = getErrorPage(_ret);
|
||||||
send(fd, _fileToSend.c_str(), _fileToSend.length(), 0);
|
send(fd, _fileToSend.c_str(), _fileToSend.length(), 0);
|
||||||
@@ -301,12 +314,29 @@ std::string Header::getReasonPhrase(int code)
|
|||||||
|
|
||||||
//-------------------------------------------------Other---------------------------------------
|
//-------------------------------------------------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)
|
void Header::printHeaderInfo(void)
|
||||||
{
|
{
|
||||||
std::map<std::string, std::string>::iterator it;
|
std::map<std::string, std::string>::iterator it;
|
||||||
|
|
||||||
std::cout << PINK << "request method = " << _method << ZERO_C << std::endl;
|
std::cout << PINK << "request method = " << _method << ZERO_C << std::endl;
|
||||||
std::cout << PINK << "request URI = " << _URI << 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 query = " << _query << ZERO_C << std::endl;
|
||||||
std::cout << PINK << "request http versioin = " << _version << 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;
|
std::cout << PINK << "request rows = " << _row << ZERO_C << std::endl;
|
||||||
|
|||||||
164
www/errorFolder/index1.html
Normal file
164
www/errorFolder/index1.html
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>Название сайта</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!--Создаём таблицу контейнер, которой задаём следующее
|
||||||
|
оформление:
|
||||||
|
border="1" - рамка вокруг контейнера. Увеличив число, можно увеличить толщину рамки.
|
||||||
|
align="center" - размещаем контейнер по центру экрана.
|
||||||
|
rules="rows" - убираем двойную рамку.
|
||||||
|
style="width:60%;" - добавляем стилевое свойства, делающее
|
||||||
|
контейнер и весь сайт "резиновым".
|
||||||
|
Сделать полноценный адаптивный дизайн, этим способом невозможно.-->
|
||||||
|
<table
|
||||||
|
border="1"
|
||||||
|
align="center"
|
||||||
|
rules="rows"
|
||||||
|
style="width:60%;">
|
||||||
|
<!--Создаём строку-->
|
||||||
|
<tr>
|
||||||
|
<!--Создаём ячейку строки-->
|
||||||
|
<td>
|
||||||
|
|
||||||
|
<!--ШАПКА САЙТА-->
|
||||||
|
|
||||||
|
<!--В ячейке строки создаём ещё одну таблицу для шапки сайта.
|
||||||
|
Оформление:
|
||||||
|
border="1" - двойная рамка толщиной в 1px
|
||||||
|
background="images/168.png" - картинка в шапке сайта, если требуется.
|
||||||
|
Адрес картинки вы должны вставить свой.
|
||||||
|
bgcolor="#7FFFD4" - фоновый цвет в шапке, если нет картинки.
|
||||||
|
cellpadding="10" - отступ содержимого от рамки не менее 10px.
|
||||||
|
style="width:100%; border-radius:5px;" - добавляем "резиновость"
|
||||||
|
и закругляем уголки рамки-->
|
||||||
|
<table
|
||||||
|
border="1"
|
||||||
|
background="images/168.png"
|
||||||
|
bgcolor="#7FFFD4"
|
||||||
|
cellpadding="10"
|
||||||
|
style="width:100%; border-radius:5px;">
|
||||||
|
<!--Создаём строку таблицы-->
|
||||||
|
<tr>
|
||||||
|
<!--Создаём столбец таблицы-->
|
||||||
|
<th>
|
||||||
|
<!--Содержание ячейки столбца-->
|
||||||
|
<h1>Название сайта (организации)</h1>
|
||||||
|
<h3>Описание сайта</h3>
|
||||||
|
<!--Закрываем таблицу-->
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<!--ОСНОВНОЙ КОНТЕНТ-->
|
||||||
|
|
||||||
|
<!--В этой же ячейке контейнера создаём ещё одну таблицу
|
||||||
|
для основного контента.
|
||||||
|
Оформление как и в предыдущей таблице-->
|
||||||
|
|
||||||
|
<table
|
||||||
|
border="1"
|
||||||
|
bgcolor="#e6e6fa"
|
||||||
|
cellpadding="10"
|
||||||
|
style="width:100%; border-radius:5px;">
|
||||||
|
<!--Создаём строку-->
|
||||||
|
<tr>
|
||||||
|
<!--Создаём ячейку
|
||||||
|
Оформление:
|
||||||
|
rowspan="2" - объединяем две ячейки в одну.
|
||||||
|
Число объединяемых ячеек по числу ячеек в сайдбаре.
|
||||||
|
style="width:80%" - основной контент занимает 80% всей площади,
|
||||||
|
оставшиеся 20% для сайдбара-->
|
||||||
|
<td
|
||||||
|
rowspan="2"
|
||||||
|
style="width:80%">
|
||||||
|
<h2>Страница</h2>
|
||||||
|
<!--Начинаем абзац с красной строки-->
|
||||||
|
<p style="text-indent:20px">
|
||||||
|
Здравствуйте уважаемые будущие веб-мастера!
|
||||||
|
Мне 55 лет и я рад приветствовать Вас на своём сайте.
|
||||||
|
Этот сайт первый, который я разработал самостоятельно,
|
||||||
|
а до этого умел только входить в интернет.</p>
|
||||||
|
|
||||||
|
<p style="text-indent:20px">Почему я решил его сделать?
|
||||||
|
За те 3 месяца, пока разбирался в
|
||||||
|
сайтостроении и создавал этот ресурс обнаружилось,
|
||||||
|
что авторы руководств по созданию
|
||||||
|
сайтов считают многие нюансы само собой разумеющимися
|
||||||
|
и не обращают на них внимание
|
||||||
|
А мне, учитывая возраст и «опыт», было не просто
|
||||||
|
понять как раз эти нюансы, они отнимали больше всего
|
||||||
|
времени.</p>
|
||||||
|
<!--Закрываем ячейку-->
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<!--САЙДБАР-->
|
||||||
|
|
||||||
|
<!--Создаём ячейку сайдбара-->
|
||||||
|
<td bgcolor="#e6e6fa">
|
||||||
|
<h3>Меню</h3>
|
||||||
|
<!--Абзац для ссылки на страницу сайта-->
|
||||||
|
<p>
|
||||||
|
<!--Ссылка на страницу сайта-->
|
||||||
|
<a href="">
|
||||||
|
<!--Картинка маркера перед названием страницы-->
|
||||||
|
<img src="http://trueimages.ru/img/00/06/f4fffdb5.png">
|
||||||
|
<!--Название страницы
|
||||||
|
style="margin-left:5px;" - отступ названия от маркера-->
|
||||||
|
<span style="margin-left:5px;">Страница</span></a>
|
||||||
|
<!--Закрываем абзац-->
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="">
|
||||||
|
<img src="http://trueimages.ru/img/31/ab/4dcb087c2ae4305edcd15171696.jpg">
|
||||||
|
<span style="margin-left:5px;">Страница 1</span;></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="">
|
||||||
|
<img src="http://trueimages.ru/img/31/ab/4dcb087c2ae4305edcd15171696.jpg">
|
||||||
|
<span style="margin-left:5px;">Страница 2</span></a>
|
||||||
|
</p>
|
||||||
|
<!--Закрываем строку Меню-->
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<!--Создаём строку с дополнительной информацией-->
|
||||||
|
<tr>
|
||||||
|
<!--Ячейка с дополнительной информацией-->
|
||||||
|
<td
|
||||||
|
bgcolor="#e6e6fa"
|
||||||
|
align="center">
|
||||||
|
<h3>Общая информация</h3>
|
||||||
|
<p>Текст общей информации или реклама</p>
|
||||||
|
<!--Закрываем ячейку с общей информацией
|
||||||
|
и таблицу основного контента-->
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<!--ПОДВАЛ-->
|
||||||
|
|
||||||
|
<!--Создаём таблицу подвала-->
|
||||||
|
<table
|
||||||
|
border="1"
|
||||||
|
bgcolor="#7FFFD4"
|
||||||
|
height="100"
|
||||||
|
cellpadding="10"
|
||||||
|
style="width:100%; border-radius:5px;">
|
||||||
|
<!--Создаём строку.-->
|
||||||
|
<tr>
|
||||||
|
<!--Создаём столбец-->
|
||||||
|
<th>
|
||||||
|
<h3>Подвал</h3>
|
||||||
|
<!--Закрываем таблицу подвала. При желании в подвале можно
|
||||||
|
сделать несколько строк и столбцов-->
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<!--Закрываем таблицу контейнера-->
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user