Browse Source

save 22-11-8-2

master
nicolas-arnaud 2 years ago
parent
commit
2850bc83ce
  1. 6
      includes/Route.hpp
  2. 21
      includes/Socket.hpp
  3. 9
      srcs/load/Route.cpp
  4. 4
      srcs/load/Server.cpp
  5. 29
      srcs/load/Socket.cpp
  6. 8
      srcs/tools.cpp

6
includes/Route.hpp

@ -3,19 +3,23 @@
class Route {
protected:
Server *_server;
string _location;
string _root;
string _ret;
std::vector<string> _indexs;
std::vector<string> _headers;
bool _autoindex;
public:
Route(string location, JSONNode *datas);
Route(Server *server, string location, JSONNode *datas);
~Route(void);
string getLocation(void);
string getRoot(void);
string getReturn(void);
std::vector<string> getIndexsLst(void);
std::vector<string> getHeadersLst(void);
Server *getServer(void);
string getIndex(string uri, string path);
string correctUri(string uri);
};

21
includes/Socket.hpp

@ -6,21 +6,26 @@ class Socket {
//int _clients_amount;
Socket *_parent;
std::vector<Socket *> _childs;
string _request;
public:
struct sockaddr_in _address;
string tmp;
string _header;
string _content;
int answer(Env *env, string request);
void send_answer(string msg);
bool waitHeader();
public:
Socket(listen_t listen);
Socket(int fd, Socket *parent);
~Socket(void);
listen_t _listen;
static fd_set _readfds;
static int _max_fd;
static int _min_fd;
static int _amount;
Socket(listen_t listen);
Socket(int fd, Socket *parent);
~Socket(void);
int launch(void);
void set_fds(void);
void refresh(Env *env);
bool isRequestValid(string request);
int answer(Env *env, string request);
void send_answer(string msg);
};

9
srcs/load/Route.cpp

@ -1,6 +1,6 @@
#include "webserv.hpp"
Route::Route(string location, JSONNode *datas) : _location(location) {
Route::Route(Server *server, string location, JSONNode *datas) : _server(server), _location(location){
JSONObject object = datas->obj();
JSONNode *tmp;
if ((tmp = object["root"]))
@ -15,6 +15,12 @@ Route::Route(string location, JSONNode *datas) : _location(location) {
_indexs.push_back((*it)->str());
}
}
if ((tmp = object["add_header"])) {
JSONList headers = tmp->lst();
for (JSONList::iterator it = headers.begin(); it < headers.end(); it++) {
_headers.push_back((*it)->str());
}
}
}
Route::~Route(void) {}
@ -23,6 +29,7 @@ string Route::getLocation(void) { return _location; }
string Route::getRoot(void) { return _root; }
string Route::getReturn(void) { return _ret; }
std::vector<string> Route::getIndexsLst(void) { return _indexs; }
std::vector<string> Route::getHeadersLst(void) { return _headers; }
string Route::getIndex(string uri, string path) {
std::stringstream content;

4
srcs/load/Server.cpp

@ -1,6 +1,6 @@
#include "webserv.hpp"
Server::Server(JSONNode *server) : Route("/", server) {
Server::Server(JSONNode *server) : Route(NULL, "/", server) {
JSONObject datas = server->obj();
if (datas["server_name"])
_name = datas["server_name"]->str();
@ -8,7 +8,7 @@ Server::Server(JSONNode *server) : Route("/", server) {
JSONObject locations = datas["locations"]->obj();
for (JSONObject::iterator it = locations.begin(); it != locations.end();
it++) {
Route *route = new Route((*it).first, (*it).second);
Route *route = new Route(this, (*it).first, (*it).second);
_routes[(*it).first] = route;
}
}

29
srcs/load/Socket.cpp

@ -99,15 +99,15 @@ void Socket::refresh(Env *env) {
}
}
bool Socket::isRequestValid(string request) {
_request += request;
if (_request.length() < 1)
bool Socket::waitHeader() {
if (_header.length() < 1)
return false;
std::vector<string> lines = split(_request, '\n');
std::vector<string> lines = split(header, '\r\n');
bool is_valid = false;
for (std::vector<string>::iterator it = lines.begin();
it < lines.end(); it++) {
if (*it == "\r" || *it == "") is_valid = true;
if (*it == "")
is_valid = true;
}
if (!is_valid || lines.at(0) == "")
return false;
@ -115,14 +115,16 @@ bool Socket::isRequestValid(string request) {
if ((head.at(0) != "GET" && head.at(0) != "POST" && head.at(0) != "DELETE")
|| head.size() < 2)
return false;
return true;
}
int Socket::answer(Env *env, string request) {
cout << "|===|Request|===|\n"<< request << "\n|===|===|===|\n";
if (!isRequestValid(request)) {
tmp += request;
cout << "|===|request|===>"<< _request << "|===||\n";
if (_header == "") {
waitHeader();
cout << "Bad request recieved\n";
send_answer("HTTP/1.1 400 Bad Request\n\n");
return EXIT_FAILURE;
}
std::vector<string> lines = split(_request, '\n');
@ -135,6 +137,15 @@ int Socket::answer(Env *env, string request) {
Server *server = env->choose_server(_parent, split(lines.at(1), ' ').at(1));
Route *route = server->get_route(uri);
std::vector<string> headers;
if ((headers = route->getHeadersLst()).size() > 0) {
if (std::find(headers.begin(), headers.end(), head.at(0)) == headers.end())
send_answer("HTTP/1.1 405 Method Not Allowed");
} else if ((headers = server->getHeadersLst()).size() > 0) {
if (std::find(headers.begin(), headers.end(), head.at(0)) == headers.end())
send_answer("HTTP/1.1 405 Method Not Allowed");
}
string path = route->correctUri(uri);
cout << "Path: " << path << "\n";
ret = route->getIndex(uri, path);
@ -143,7 +154,7 @@ int Socket::answer(Env *env, string request) {
ret = read_file(path);
}
answer << (ret == "" ? " 404 Not Found\nContent-length: 0\n\n" : " 200 OK\n") << ret;
cout << "|===|Answer|===|\n" << answer.str() << "\n|===|===|===|\n";
cout << "|===|Answer|===>" << answer.str() << "|===||\n";
send_answer(answer.str());
_request = "";
return EXIT_SUCCESS;

8
srcs/tools.cpp

@ -207,14 +207,14 @@ string read_file(string path) {
string content;
std::stringstream ret;
struct stat info;
if (stat(path.c_str(), &info) != 0) {
if (stat(path.c_str(), &info) != 0 || S_ISDIR(info.st_mode)) {
std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n";
return "";
}
std::ifstream file(path.c_str());
if (file.fail())
return "";
//if (!file.good())
// return "";
while (file) {
std::getline(file, str);
content += str + "\n";
@ -224,5 +224,3 @@ string read_file(string path) {
ret << "\n\n" << content;
return (ret.str());
}

Loading…
Cancel
Save