Browse Source

save 22-11-7-2

master
nicolas-arnaud 2 years ago
parent
commit
cfd3269196
  1. 4
      includes/Route.hpp
  2. 30
      srcs/load/Route.cpp
  3. 13
      srcs/load/Socket.cpp

4
includes/Route.hpp

@ -15,8 +15,8 @@ class Route {
string getLocation(void); string getLocation(void);
string getRoot(void); string getRoot(void);
string getReturn(void); string getReturn(void);
std::vector<string> getIndexs(void); std::vector<string> getIndexsLst(void);
string getAutoindex(string path); string getIndex(string path);
string read_file(string path); string read_file(string path);
string correctUri(string uri); string correctUri(string uri);
}; };

30
srcs/load/Route.cpp

@ -22,35 +22,44 @@ Route::~Route(void) {}
string Route::getLocation(void) { return _location; } string Route::getLocation(void) { return _location; }
string Route::getRoot(void) { return _root; } string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; } string Route::getReturn(void) { return _ret; }
std::vector<string> Route::getIndexs(void) { return _indexs; } std::vector<string> Route::getIndexsLst(void) { return _indexs; }
string Route::getAutoindex(string path) { string Route::getIndex(string path) {
if (!_autoindex) std::stringstream content;
return ""; std::stringstream ret;
std::stringstream page;
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
struct stat info; struct stat info;
std::vector<string> indexs = getIndexsLst();
std::vector<string>::iterator it;
if ((dir = opendir(path.c_str())) == NULL) if ((dir = opendir(path.c_str())) == NULL)
return ""; return "";
else { else {
page << path << " files :\n"; content << path << " files :\n";
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.') if (entry->d_name[0] == '.')
continue; continue;
page << "- " << entry->d_name << "\n"; for (it = indexs.begin(); it < indexs.end(); it++) {
if (entry->d_name == *it)
return (read_file(path + "/" + *it));
}
content << "- " << entry->d_name << "\n";
if (stat(path.c_str(), &info) != 0) if (stat(path.c_str(), &info) != 0)
std::cerr << "stat() error on " << path << ": " std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n"; << strerror(errno) << "\n";
} }
closedir(dir); closedir(dir);
} }
return page.str(); ret << "Content-type: text/html \n";
ret << "Content-length: "<< content.str().length();
ret << "\n\n" << content;
return ret.str();
} }
string Route::read_file(string path) { string Route::read_file(string path) {
string str; string str;
string content;
std::stringstream ret; std::stringstream ret;
struct stat info; struct stat info;
if (stat(path.c_str(), &info) != 0) { if (stat(path.c_str(), &info) != 0) {
@ -61,8 +70,11 @@ string Route::read_file(string path) {
std::ifstream file(path.c_str()); std::ifstream file(path.c_str());
while (file) { while (file) {
std::getline(file, str); std::getline(file, str);
ret << str << "\n"; content += str + "\n";
} }
ret << "Content-type: " << getMime(path) << "\n";
ret << "Content-length: " << content.length();
ret << "\n\n" << content;
return (ret.str()); return (ret.str());
} }

13
srcs/load/Socket.cpp

@ -111,21 +111,14 @@ int Socket::answer(Env *env, int fd, string request) {
string ret; string ret;
std::stringstream answer; std::stringstream answer;
answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; answer << "HTTP/1.1 200 OK\n";
Server *server = env->choose_server(this, split(lines.at(1), ' ').at(1)); Server *server = env->choose_server(this, split(lines.at(1), ' ').at(1));
Route *route = server->get_route(uri); Route *route = server->get_route(uri);
/*
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); string path = route->correctUri(uri);
if ((ret = route->getAutoindex(path)) == "") if ((ret = route->getIndex(path)) == "")
ret = route->read_file(path); ret = route->read_file(path);
answer << ret.length() << "\n\n" << ret; answer << ret;
cout << answer.str() << "\n|===|===|===|\n"; cout << answer.str() << "\n|===|===|===|\n";
send_answer(fd, answer.str()); send_answer(fd, answer.str());
return EXIT_SUCCESS; return EXIT_SUCCESS;

Loading…
Cancel
Save