Browse Source

added read files (text only)

master
nicolas-arnaud 2 years ago
parent
commit
1fe7d6279a
  1. 10
      default.json
  2. 3
      includes/Route.hpp
  3. 2
      includes/Socket.hpp
  4. 16
      srcs/load/Env.cpp
  5. 44
      srcs/load/Route.cpp
  6. 33
      srcs/load/Socket.cpp

10
default.json

@ -4,24 +4,24 @@
"servers": [
{
"server_name": "localhost",
"listens": ["localhost:8080", "localhost"],
"listens": ["localhost:8080"],
"return": "301 https://$host$uri"
},
{
"server_name": "localhost",
"listens": ["192.168.62.61:8080"],
"root": "./public",
"listens": ["192.168.62.61:8080", "localhost", "555"],
"root": "public/",
"indexs": ["index.html"],
"locations":
{
"docs/":
{
"root": "documents/",
"root": "public/documents/",
"autoindex": true
},
"img/":
{
"root": "images/",
"root": "public/images/",
"autoindex": true
}
}

3
includes/Route.hpp

@ -16,6 +16,7 @@ class Route {
string getRoot(void);
string getReturn(void);
std::vector<string> getIndexs(void);
string getAutoindex(string uri);
string getAutoindex(string path);
string read_file(string path);
string correctUri(string uri);
};

2
includes/Socket.hpp

@ -18,6 +18,6 @@ class Socket {
int launch(void);
void set_fds(void);
void refresh(Env *env);
void answer(Env *env, int fd, string request);
int answer(Env *env, int fd, string request);
void send_answer(int fd, string msg);
};

16
srcs/load/Env.cpp

@ -25,14 +25,14 @@ Env::Env(JSONNode *conf) {
Server *Env::choose_server(Socket *sock, string host) {
std::vector<Server *> exact;
std::vector<Server *> inrange;
std::vector<string> ip_list;
std::vector<string> ip_requ;
std::vector<string> ip_listen;
std::vector<string> ip_required;
// string ip = inet_ntoa(sock->_address.sin_addr);
// int port = ntohs(sock->_address.sin_port);
// cout << "Which server for " << ip << ":" << port << "?\n";
cout << "Socket: " << sock->_listen.ip << ":" << sock->_listen.port << "\n";
ip_requ = split(sock->_listen.ip, '.');
ip_required = split(sock->_listen.ip, '.');
for (std::vector<Server *>::iterator sit = _servers.begin();
sit < _servers.end(); sit++) {
@ -40,7 +40,6 @@ Server *Env::choose_server(Socket *sock, string host) {
for (std::vector<listen_t>::iterator it = serv_listens.begin();
it < serv_listens.end(); it++) {
ip_list = split((*it).ip, '.');
if (sock->_listen.port != (*it).port)
continue;
if (sock->_listen.ip == (*it).ip) {
@ -48,16 +47,15 @@ Server *Env::choose_server(Socket *sock, string host) {
continue;
}
bool is_inrange = true;
ip_list = split((*it).ip, '.');
std::vector<string>::iterator r = ip_requ.begin();
for (std::vector<string>::iterator l = ip_list.begin();
l < ip_list.end(); l++) {
ip_listen = split((*it).ip, '.');
std::vector<string>::iterator r = ip_required.begin();
for (std::vector<string>::iterator l = ip_listen.begin();
l < ip_listen.end(); l++) {
if (*l != *r && *l != "0")
is_inrange = false;
}
if (is_inrange == true)
inrange.push_back(*sit);
// else if (is_ip_into(sock->_listen.ip, (*it).ip))
}
}
if (exact.size() == 0) {

44
srcs/load/Route.cpp

@ -24,18 +24,16 @@ string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; }
std::vector<string> Route::getIndexs(void) { return _indexs; }
string Route::getAutoindex(string uri) {
string Route::getAutoindex(string path) {
if (!_autoindex)
return "4\n\n404!";
return "";
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.";
return "";
else {
page << path << " files :\n";
while ((entry = readdir(dir)) != NULL) {
@ -48,15 +46,28 @@ string Route::getAutoindex(string uri) {
}
closedir(dir);
}
ret << page.str().length() << "\n\n" << page.str();
return ret.str();
return page.str();
}
string Route::read_file(string path) {
string str;
std::stringstream ret;
struct stat info;
if (stat(path.c_str(), &info) != 0) {
std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n";
return "";
}
std::ifstream file(path.c_str());
while (file) {
std::getline(file, str);
ret << str << "\n";
}
return (ret.str());
}
string Route::correctUri(string uri) {
std::stringstream ret;
// int slash_pos;
// string root = _root;
// int i = 0;
std::vector<string>::iterator it;
std::vector<string>::iterator it2;
@ -76,19 +87,6 @@ string Route::correctUri(string uri) {
while (it2 < uri_split.end()) {
ret << "/" << *(it2++);
}
/*
int i = 0;
while (uri.at(i) == '/')
i++;
uri.erase(0, i);
while ((slash_pos = root.find('/')) > 0) {
ret << root.substr(0, slash_pos);
root.erase(0, slash_pos);
if (uri.find('/'))
uri = uri.substr(uri.find('/'), uri.length());
}
ret << uri;
*/
cout << "resutlt: " << ret.str() << "\n";
return ret.str();
}

33
srcs/load/Socket.cpp

@ -1,9 +1,7 @@
#include "webserv.hpp"
Socket::Socket(listen_t listen) : _listen(listen) {
_clients_amount = 0;
}
Socket::Socket(listen_t listen) : _listen(listen) { _clients_amount = 0; }
Socket::~Socket(void) {
close(_master_socket);
cout << "Socket destroyed!\n";
@ -91,27 +89,46 @@ void Socket::refresh(Env *env) {
_clients.erase(it);
} else {
buffer[valread] = '\0';
answer(env, *it, buffer);
if (answer(env, *it, buffer) == EXIT_FAILURE) {
_clients.erase(it);
close(*it);
}
}
}
}
}
void Socket::answer(Env *env, int fd, string request) {
int Socket::answer(Env *env, int fd, string request) {
cout << request << "\n|===|===|===|\n";
std::vector<string> lines = split(request, '\n');
string uri = split(lines.at(0), ' ').at(1);
std::vector<string> head = split(lines.at(0), ' ');
string uri;
if (head.size() < 2)
return EXIT_FAILURE;
uri = head.at(1);
cout << uri << "\n";
string ret;
cout << request << "\n|===|===|===|\n";
std::stringstream answer;
answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: ";
Server *server = env->choose_server(this, split(lines.at(1), ' ').at(1));
Route *route = server->get_route(uri);
cout << "Route find: " << route->getLocation() << "\n";
/*
cout << "Route find: " << route->getLocation() << "->" << route->getRoot() << "\n";
if ((ret = route->read_file(uri)) == "")
answer << route->getAutoindex(uri);
else
answer << ret;
*/
string path = route->correctUri(uri);
if ((ret = route->getAutoindex(path)) == "")
ret = route->read_file(path);
answer << ret.length() << "\n\n" << ret;
cout << answer.str() << "\n|===|===|===|\n";
send_answer(fd, answer.str());
return EXIT_SUCCESS;
}
void Socket::send_answer(int fd, string msg) {

Loading…
Cancel
Save