From 2a4c13ae1f68818b869aa6d70bb7671e463d007d Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Mon, 7 Nov 2022 00:45:16 +0100 Subject: [PATCH] started to add autoindex --- default.json | 8 +++---- includes/Route.hpp | 3 ++- includes/Socket.hpp | 1 + includes/webserv.hpp | 5 ++++- srcs/json/Nodes.cpp | 2 -- srcs/load/Route.cpp | 50 +++++++++++++++++++++++++++++++++++++++++--- srcs/load/Socket.cpp | 27 +++++++++++++++--------- 7 files changed, 75 insertions(+), 21 deletions(-) diff --git a/default.json b/default.json index a0d802c..a9333b4 100644 --- a/default.json +++ b/default.json @@ -14,14 +14,14 @@ "indexs": ["index.html"], "locations": { - "/docs": + "docs/": { - "root": "./documents", + "root": "documents/", "autoindex": true }, - "/img": + "img/": { - "root": "./images" + "root": "images/" } } diff --git a/includes/Route.hpp b/includes/Route.hpp index b6a7133..9c277f4 100644 --- a/includes/Route.hpp +++ b/includes/Route.hpp @@ -14,5 +14,6 @@ class Route { string getRoot(void); string getReturn(void); std::vector getIndexs(void); - bool getAutoindex(void); + string getAutoindex(string uri); + string correctUri(string uri); }; diff --git a/includes/Socket.hpp b/includes/Socket.hpp index 5c9ac15..dde61b4 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -21,6 +21,7 @@ class Socket { void set_fds(); void refresh(); void answer(int fd, string request); + void send_answer(int fd, string msg); /* Socket& operator=(Socket &src) { _ip = src._ip; diff --git a/includes/webserv.hpp b/includes/webserv.hpp index a4cda2e..24ccbdf 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -1,11 +1,14 @@ #pragma once #include +#include #include #include #include -#include //FD_SET, FD_ISSET, FD_ZERO macros +#include +#include #include +#include #include #include diff --git a/srcs/json/Nodes.cpp b/srcs/json/Nodes.cpp index ac1d8e6..5858411 100644 --- a/srcs/json/Nodes.cpp +++ b/srcs/json/Nodes.cpp @@ -1,7 +1,5 @@ #include "webserv.hpp" -#define DEBUG 0 - JSONObject JSONNode::obj() { if (type == OBJECT) return *values.object; diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index 2834279..5bdad8c 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -18,7 +18,51 @@ Route::Route(JSONNode *datas) { Route::~Route(void) {} -string getRoot(void); -string getReturn(void); +string Route::getRoot(void) { return _root; } +string Route::getReturn(void) { return _ret; } std::vector Route::getIndexs(void) { return _indexs; } -bool getAutoindex(void); + +string Route::getAutoindex(string uri) { + if (!_autoindex) + return "4\n\n404!"; + std::stringstream page; + std::stringstream ret; + string path = correctUri(uri); + DIR *dir; + struct dirent *entry; + struct stat info; + + if ((dir = opendir(path.c_str())) == NULL) + ret << " 19\n\nFolder unaccesible."; + else { + page << path << " files :\n"; + while ((entry = readdir(dir)) != NULL) { + if (entry->d_name[0] == '.') + continue; + page << "- " << entry->d_name << "\n"; + if (stat(path.c_str(), &info) != 0) + std::cerr << "stat() error on " << path << ": " + << strerror(errno) << "\n"; + } + closedir(dir); + } + ret << page.str().length() << "\n\n" << page.str(); + return ret.str(); +} + +string Route::correctUri(string uri) { + std::stringstream ret; + int slash_pos; + string root = _root; + + cout << "Correcting request: " << uri + << "with root: " << root << "\n"; + while ((slash_pos = root.find('/')) > 0) { + ret << root.substr(0, slash_pos); + root.erase(0, slash_pos); + uri = uri.substr(uri.find('/'), uri.length()); + } + ret << uri; + cout << "resutlt: " << ret.str() << "\n"; + return ret.str(); +} diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index e3c5c06..1fd0828 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -33,7 +33,10 @@ int Socket::launch() { return (EXIT_FAILURE); } - _address.sin_family = AF_INET; + if (_ip.at(0) == '[') + _address.sin_family = AF_INET6; + else + _address.sin_family = AF_INET; _address.sin_addr.s_addr = inet_addr(_ip.c_str()); _address.sin_port = htons(_port); @@ -97,23 +100,27 @@ void Socket::refresh() { close(*it); _clients.erase(it); } else { - buffer[valread]='\0'; - this->answer(*it, buffer); + buffer[valread] = '\0'; + answer(*it, buffer); } } } } void Socket::answer(int fd, string request) { - string uri = "path/to/page/"; - Route *route = _server->get_route(uri); - (void)route; - char r404[72] = - "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!"; cout << request << "\n|===|===|===|\n"; + std::stringstream answer; + answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; + string uri = "docs/"; + Route *route = _server->get_route(uri); + answer << route->getAutoindex(uri); + cout << answer.str() << "\n|===|===|===|\n"; + send_answer(fd, answer.str()); +} +void Socket::send_answer(int fd, string msg) { #ifdef __linux__ - send(fd, r404, strlen(r404), MSG_NOSIGNAL); + send(fd, msg.c_str(), msg.length(), MSG_NOSIGNAL); #elif __APPLE__ - send(fd, r404, strlen(r404), 0); + send(fd, msg.c_str(), msg.length(), 0); #endif }