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. 27
      srcs/load/Socket.cpp

8
default.json

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

3
includes/Route.hpp

@ -14,5 +14,6 @@ class Route {
string getRoot(void);
string getReturn(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 refresh();
void answer(int fd, string request);
void send_answer(int fd, string msg);
/*
Socket& operator=(Socket &src) {
_ip = src._ip;

5
includes/webserv.hpp

@ -1,11 +1,14 @@
#pragma once
#include <arpa/inet.h>
#include <dirent.h>
#include <fcntl.h>
#include <netinet/in.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 <stdio.h>
#include <unistd.h>
#include <cctype>

2
srcs/json/Nodes.cpp

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

50
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<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();
}

27
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
}

Loading…
Cancel
Save