Browse Source

started to add autoindex

test
nicolas-arnaud 2 years ago
parent
commit
2a4c13ae1f
  1. 8
      default.json
  2. 3
      includes/Route.hpp
  3. 1
      includes/Socket.hpp
  4. 5
      includes/webserv.hpp
  5. 2
      srcs/json/Nodes.cpp
  6. 50
      srcs/load/Route.cpp
  7. 25
      srcs/load/Socket.cpp

8
default.json

@ -14,14 +14,14 @@
"indexs": ["index.html"], "indexs": ["index.html"],
"locations": "locations":
{ {
"/docs": "docs/":
{ {
"root": "./documents", "root": "documents/",
"autoindex": true "autoindex": true
}, },
"/img": "img/":
{ {
"root": "./images" "root": "images/"
} }
} }

3
includes/Route.hpp

@ -14,5 +14,6 @@ class Route {
string getRoot(void); string getRoot(void);
string getReturn(void); string getReturn(void);
std::vector<string> getIndexs(void); std::vector<string> getIndexs(void);
bool getAutoindex(void); string getAutoindex(string uri);
string correctUri(string uri);
}; };

1
includes/Socket.hpp

@ -21,6 +21,7 @@ class Socket {
void set_fds(); void set_fds();
void refresh(); void refresh();
void answer(int fd, string request); void answer(int fd, string request);
void send_answer(int fd, string msg);
/* /*
Socket& operator=(Socket &src) { Socket& operator=(Socket &src) {
_ip = src._ip; _ip = src._ip;

5
includes/webserv.hpp

@ -1,11 +1,14 @@
#pragma once #pragma once
#include <arpa/inet.h> #include <arpa/inet.h>
#include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros #include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <cctype> #include <cctype>

2
srcs/json/Nodes.cpp

@ -1,7 +1,5 @@
#include "webserv.hpp" #include "webserv.hpp"
#define DEBUG 0
JSONObject JSONNode::obj() { JSONObject JSONNode::obj() {
if (type == OBJECT) if (type == OBJECT)
return *values.object; return *values.object;

50
srcs/load/Route.cpp

@ -18,7 +18,51 @@ Route::Route(JSONNode *datas) {
Route::~Route(void) {} Route::~Route(void) {}
string getRoot(void); string Route::getRoot(void) { return _root; }
string getReturn(void); string Route::getReturn(void) { return _ret; }
std::vector<string> Route::getIndexs(void) { return _indexs; } std::vector<string> 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();
}

25
srcs/load/Socket.cpp

@ -33,6 +33,9 @@ int Socket::launch() {
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
if (_ip.at(0) == '[')
_address.sin_family = AF_INET6;
else
_address.sin_family = AF_INET; _address.sin_family = AF_INET;
_address.sin_addr.s_addr = inet_addr(_ip.c_str()); _address.sin_addr.s_addr = inet_addr(_ip.c_str());
_address.sin_port = htons(_port); _address.sin_port = htons(_port);
@ -97,23 +100,27 @@ void Socket::refresh() {
close(*it); close(*it);
_clients.erase(it); _clients.erase(it);
} else { } else {
buffer[valread]='\0'; buffer[valread] = '\0';
this->answer(*it, buffer); answer(*it, buffer);
} }
} }
} }
} }
void Socket::answer(int fd, string request) { 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"; 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__ #ifdef __linux__
send(fd, r404, strlen(r404), MSG_NOSIGNAL); send(fd, msg.c_str(), msg.length(), MSG_NOSIGNAL);
#elif __APPLE__ #elif __APPLE__
send(fd, r404, strlen(r404), 0); send(fd, msg.c_str(), msg.length(), 0);
#endif #endif
} }

Loading…
Cancel
Save