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 getRoot(void);
string getReturn(void);
std::vector<string> getIndexs(void);
string getAutoindex(string path);
std::vector<string> getIndexsLst(void);
string getIndex(string path);
string read_file(string path);
string correctUri(string uri);
};

30
srcs/load/Route.cpp

@ -22,35 +22,44 @@ Route::~Route(void) {}
string Route::getLocation(void) { return _location; }
string Route::getRoot(void) { return _root; }
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) {
if (!_autoindex)
return "";
std::stringstream page;
string Route::getIndex(string path) {
std::stringstream content;
std::stringstream ret;
DIR *dir;
struct dirent *entry;
struct stat info;
std::vector<string> indexs = getIndexsLst();
std::vector<string>::iterator it;
if ((dir = opendir(path.c_str())) == NULL)
return "";
else {
page << path << " files :\n";
content << path << " files :\n";
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.')
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)
std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n";
}
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 str;
string content;
std::stringstream ret;
struct stat info;
if (stat(path.c_str(), &info) != 0) {
@ -61,8 +70,11 @@ string Route::read_file(string path) {
std::ifstream file(path.c_str());
while (file) {
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());
}

13
srcs/load/Socket.cpp

@ -111,21 +111,14 @@ int Socket::answer(Env *env, int fd, string request) {
string ret;
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));
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);
if ((ret = route->getAutoindex(path)) == "")
if ((ret = route->getIndex(path)) == "")
ret = route->read_file(path);
answer << ret.length() << "\n\n" << ret;
answer << ret;
cout << answer.str() << "\n|===|===|===|\n";
send_answer(fd, answer.str());
return EXIT_SUCCESS;

Loading…
Cancel
Save