Webserv
Loading...
Searching...
No Matches
Route.cpp
Go to the documentation of this file.
1
8#include "webserv.hpp"
9
20Route::Route(Server *server, string location, JSONNode *datas) : _server(server), _location(location) {
21 JSONObject object = datas->obj();
22 JSONNode *tmp;
23 _autoindex = false;
25 if ((tmp = object["root"]))
26 _root = tmp->str();
27 if ((tmp = object["return"]))
28 _ret = tmp->str();
29 if ((tmp = object["autoindex"]))
30 _autoindex = tmp->boo();
31 if ((tmp = object["indexs"])) {
32 JSONList indexs = tmp->lst();
33 for (JSONList::iterator it = indexs.begin(); it < indexs.end(); it++)
34 _indexs.push_back((*it)->str());
35 }
36 if ((tmp = object["allowed_methods"])) {
37 JSONList headers = tmp->lst();
38 for (JSONList::iterator it = headers.begin(); it < headers.end(); it++)
39 _allowed_methods.push_back((*it)->str());
40 }
41 if ((tmp = object["cgi"])) {
42 JSONObject cgis = tmp->obj();
43 for (JSONObject::iterator it = cgis.begin(); it != cgis.end(); it++)
44 _cgi[(*it).first] = (*it).second->str();
45 }
46 if ((tmp = object["client_max_body_size"]))
48}
49
52
53// Getters
54string Route::getLocation(void) { return _location; }
55string Route::getRoot(void) { return _root; }
56string Route::getReturn(void) { return _ret; }
57
66string Route::getIndex(string uri, string path) {
67 std::stringstream body, ret;
68 DIR *dir;
69 struct dirent *entry;
70 struct stat info;
71 vec_string::iterator it;
72
73 if ((dir = opendir(path.c_str()))) {
74 if (DEBUG)
75 cout << "get index(): path=" << path << "\n";
76 body << "<h3 style=\"text-align: center;\">" << path << " files :</h3>\n<ul>\n";
77 while ((entry = readdir(dir)) != NULL) {
78 if (entry->d_name[0] == '.')
79 continue;
80 for (it = _indexs.begin(); it < _indexs.end(); it++) {
81 if (entry->d_name == *it)
82 return (read_file(path + "/" + *it));
83 }
84 body << "<li><a href=\"" << uri + "/" + entry->d_name << "\">" << entry->d_name << "</a></li>\n";
85 if (stat(path.c_str(), &info) != 0)
86 std::cerr << "stat() error on " << path << ": " << strerror(errno) << "\n";
87 }
88 body << "</ul>";
89 closedir(dir);
90 }
91 if (!dir || !_autoindex)
92 return "";
93 if (DEBUG)
94 cout << "Getting autoindex\n";
95 ret << "Content-type: text/html \r\n";
96 ret << "Content-length: " << body.str().length() << "\r\n";
97 ret << "\r\n" << body.str();
98 return ret.str();
99}
100/* |==========|
101 * Correct the uri the client asked
102 *
103 * Input: The uri
104 * Output: The local path corresponding to that uri in the route
105 */
106
115string Route::correctUri(string uri) {
116 std::stringstream ret;
117 vec_string::iterator loc_word, uri_word;
118
119 vec_string loc_words = split(_location, "/");
120 vec_string uri_words = split(uri, "/");
121 uri_word = uri_words.begin();
122 for (loc_word = loc_words.begin(); loc_word < loc_words.end(); loc_word++) {
123 while (uri_word < uri_words.end() && *uri_word == "")
124 uri_word++;
125 while (loc_word < loc_words.end() && *loc_word == "")
126 loc_word++;
127 if (loc_word != loc_words.end())
128 uri_word++;
129 }
130 ret << "./" << _root;
131 while (uri_word < uri_words.end())
132 ret << "/" << *(uri_word++);
133 return ret.str();
134}
int nbr()
Definition: Nodes.cpp:18
JSONObject obj()
Definition: Nodes.cpp:3
JSONList lst()
Definition: Nodes.cpp:8
string str()
Definition: Nodes.cpp:13
bool boo()
Definition: Nodes.cpp:23
std::map< string, string > _cgi
Definition: Route.hpp:12
vec_string _allowed_methods
Definition: Route.hpp:11
Route(Server *server, string location, JSONNode *datas)
Constructor.
Definition: Route.cpp:20
int _client_max_body_size
Definition: Route.hpp:13
string _root
Definition: Route.hpp:7
string correctUri(string uri)
Find the local path corresponding to the uri asked by te client.
Definition: Route.cpp:115
vec_string _indexs
Definition: Route.hpp:11
~Route(void)
Destructor.
Definition: Route.cpp:51
string _location
Definition: Route.hpp:7
string getLocation(void)
Definition: Route.cpp:54
bool _autoindex
Definition: Route.hpp:8
string getIndex(string uri, string path)
Search for an index while generating autoindex.
Definition: Route.cpp:66
string _ret
Definition: Route.hpp:7
string getReturn(void)
Definition: Route.cpp:56
string getRoot(void)
Definition: Route.cpp:55
Definition: Server.hpp:4
string read_file(string path)
Definition: tools.cpp:58
vec_string split(string str, string delim)
Definition: tools.cpp:20
std::vector< JSONNode * > JSONList
Definition: webserv.hpp:49
std::vector< string > vec_string
Definition: webserv.hpp:50
#define DEBUG
Definition: webserv.hpp:2
std::map< string, JSONNode * > JSONObject
Definition: webserv.hpp:48