Browse Source

save 22-11-9-1

master
nicolas-arnaud 2 years ago
parent
commit
b2d081f548
  1. 2
      Makefile
  2. 10
      includes/Socket.hpp
  3. 10
      includes/webserv.hpp
  4. 13
      srcs/load/Route.cpp
  5. 110
      srcs/load/Socket.cpp

2
Makefile

@ -10,7 +10,7 @@ CXXFLAGS= -g -I includes -Werror -Wextra -Wall -std=c++98
all : $(NAME) all : $(NAME)
$(NAME): $(OBJS) $(NAME): $(OBJS)
$(CXX) -g $(OBJS) -o $(NAME) $(CXX) -g -fsanitize=address $(OBJS) -o $(NAME)
clean: clean:
rm -rf $(OBJS) rm -rf $(OBJS)

10
includes/Socket.hpp

@ -7,15 +7,13 @@ class Socket {
Socket *_parent; Socket *_parent;
std::vector<Socket *> _childs; std::vector<Socket *> _childs;
struct sockaddr_in _address; struct sockaddr_in _address;
string _tmp;
string _header; string _header;
string _content; string _content;
string _method; std::map<string,std::vector<string> > _request;
string _uri;
string _host;
string _extension;
int answer(Env *env, string request); bool getRequest(string paquet);
bool parseHeader();
int answer(Env *env);
void send_answer(string msg); void send_answer(string msg);
bool waitHeader(); bool waitHeader();
public: public:

10
includes/webserv.hpp

@ -32,6 +32,11 @@ using std::cout;
using std::strerror; using std::strerror;
using std::string; using std::string;
typedef struct listen_s {
string ip;
int port;
} listen_t;
class Env; class Env;
class Server; class Server;
class Socket; class Socket;
@ -41,11 +46,6 @@ class JSONNode;
typedef std::map<string, JSONNode *> JSONObject; typedef std::map<string, JSONNode *> JSONObject;
typedef std::vector<JSONNode *> JSONList; typedef std::vector<JSONNode *> JSONList;
typedef struct listen_s {
string ip;
int port;
} listen_t;
void *ft_memset(void *b, int c, size_t len); void *ft_memset(void *b, int c, size_t len);
bool isInt(string str); bool isInt(string str);
std::vector<string> split(string str, char delim); std::vector<string> split(string str, char delim);

13
srcs/load/Route.cpp

@ -60,7 +60,7 @@ string Route::getIndex(string uri, string path) {
std::cerr << "stat() error on " << path << ": " std::cerr << "stat() error on " << path << ": "
<< strerror(errno) << "\n"; << strerror(errno) << "\n";
} }
content << "<ul>"; content << "</ul>";
closedir(dir); closedir(dir);
} }
if (!_autoindex) if (!_autoindex)
@ -76,15 +76,15 @@ string Route::correctUri(string uri) {
std::vector<string>::iterator it; std::vector<string>::iterator it;
std::vector<string>::iterator it2; std::vector<string>::iterator it2;
// cout << "Correcting request: " << uri << " with root: " << _root << "\n"; cout << "Correcting request: " << uri << " with root: " << _root << "\n";
ret << _root; ret << "./" << _root;
std::vector<string> loc_split = split(_location, '/'); std::vector<string> loc_split = split(_location, '/');
std::vector<string> uri_split = split(uri, '/'); std::vector<string> uri_split = split(uri, '/');
it2 = uri_split.begin(); it2 = uri_split.begin();
for (it = loc_split.begin(); it < loc_split.end(); it++) { for (it = loc_split.begin(); it < loc_split.end(); it++) {
while (*it2 == "") while ( it2 < uri_split.end() && *it2 == "")
it2++; it2++;
while (*it == "") while (it < loc_split.end() && *it == "")
it++; it++;
if (it != loc_split.end()) if (it != loc_split.end())
it2++; it2++;
@ -93,6 +93,7 @@ string Route::correctUri(string uri) {
while (it2 < uri_split.end()) { while (it2 < uri_split.end()) {
ret << "/" << *(it2++); ret << "/" << *(it2++);
} }
// cout << "resutlt: " << ret.str() << "\n";
cout << "result: " << ret.str() << "\n";
return ret.str(); return ret.str();
} }

110
srcs/load/Socket.cpp

@ -93,73 +93,83 @@ void Socket::refresh(Env *env) {
_childs.erase(it); _childs.erase(it);
} else { } else {
buffer[valread] = '\0'; buffer[valread] = '\0';
(*it)->answer(env, buffer); if ((*it)->getRequest(buffer))
(*it)->answer(env);
} }
} }
} }
} }
bool Socket::waitHeader() { bool Socket::parseHeader() {
if (_tmp.length() < 1) std::vector<string> lines = split(_header, '\n');
return false; std::vector<string> line;
std::vector<string> lines = split(_tmp, '\n'); if (lines.size() > 0) {
bool is_valid = false; for (std::vector<string>::iterator it = lines.begin() + 1; it < lines.end(); it++) {
for (std::vector<string>::iterator it = lines.begin(); it < lines.end(); line = split(*it, ' ');
it++) { cout << line.at(0) << "scraped from header\n";
if (*it == "\r") _request[line.at(0)] = std::vector<string>(line.begin() + 1, line.end());
is_valid = true; }
} }
if (!is_valid) std::vector<string> method = split(lines.at(0), ' ');
if (method.at(0) == "POST" && _request.find("Content-Length:") == _request.end() && _request.find("Transfer-Encoding:") ==_request.end())
return false; return false;
_header = _tmp; _request["Method:"] = method;
_tmp = "";
return true; return true;
} }
int Socket::answer(Env *env, string request) { bool Socket::getRequest(string paquet) {
_tmp += request; cout << "|===|paquet|===>" << paquet << "|===||\n";
cout << "|===|request|===>" << _tmp << "|===||\n"; if (paquet.length() < 1) //HTTPS?
if (_header == "") { return false;
waitHeader(); std::vector<string> lines = split(paquet, '\n');
long chunk_len = (_content.length() > 0 && _request["Transfer-Encoding:"].size() && _request["Transfer-Encoding:"].at(0) == "chunked") ? std::strtol(lines.at(0).substr(1).c_str(), 0, 16) : -1;
cout << "Chunk length: " << chunk_len << "\n";
for (std::vector<string>::iterator it = lines.begin(); it < lines.end(); it++) {
if (*it == "\r" && _content.length() == 0)
this->parseHeader();
if (*it != "\r" && _content.length() == 0)
_header += *it + "\n";
else if (chunk_len == -1 || it != lines.begin())
_content += *it + "\n";
}
cout << "Header: \n-|" << _header << "|-\n";
cout << "Content: \n-|" << _content << "|-\n";
if (_content.length() > 0) {
_content.resize(_content.length() - 1);
if ( _request["Method:"].at(0) == "GET" || (chunk_len == 0 || std::strtoul(_request["Content-Length:"].at(0).c_str(), 0, 10) <= _content.length() ) ) {
cout << "Request received\n";
return true;
} }
std::vector<string> lines = split(_header, '\n'); } else
std::vector<string> head = split(lines.at(0), ' '); _header.resize(_header.length() - 1);
this->_method = head.at(0); return false;
this->_uri = head.at(1); }
for (std::vector<string>::iterator it = lines.begin(); it < lines.end(); it++)
if (it->find("Host:") != string::npos) int Socket::answer(Env *env) {
this->_host = it->substr(6); cout << "Method: " << _request["Method:"].at(0) << "\n";
cout << "Method: " << this->_method << "\n"; cout << "URI: " << _request["Method:"].at(1) << "\n";
cout << "URI: " << this->_uri << "\n"; cout << "Host: " << _request["Host:"].at(0) << "\n";
cout << "Host: " << this->_host << "\n";
string ret; string ret;
std::stringstream answer; std::stringstream answer;
answer << "HTTP/1.1"; answer << "HTTP/1.1";
Server *server = env->choose_server(_parent, split(lines.at(1), ' ').at(1)); Server *server = env->choose_server(_parent, _request["Host:"].at(0));
Route *route = server->get_route(this->_uri); Route *route = server->get_route(_request["Method:"].at(1));
std::vector<string> headers; string method = _request["Method:"].at(0);
std::vector<string> allowed;
if ((head.at(0) != "GET" && head.at(0) != "POST" && if (method != "GET" && method != "POST" && method != "DELETE")
head.at(0) != "DELETE") ||
head.size() < 2)
send_answer(
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
else if ((headers = route->getHeadersLst()).size() > 0) {
if (std::find(headers.begin(), headers.end(), head.at(0)) ==
headers.end())
send_answer( send_answer(
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); "HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
} else if ((headers = server->getHeadersLst()).size() > 0) { else if ((allowed = route->getHeadersLst()).size() > 0) {
if (std::find(headers.begin(), headers.end(), head.at(0)) == if (std::find(allowed.begin(), allowed.end(), method) == allowed.end())
headers.end()) send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
send_answer( } else if ((allowed = server->getHeadersLst()).size() > 0) {
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); if (std::find(allowed.begin(), allowed.end(), method) == allowed.end())
} send_answer("HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
}
string path = route->correctUri(this->_uri); string path = route->correctUri(_request["Method:"].at(1));
cout << "Path: " << path << "\n"; cout << "Path: " << path << "\n";
ret = route->getIndex(this->_uri, path); ret = route->getIndex(_request["Method:"].at(1), path);
if (ret == "") { if (ret == "") {
cout << "No index: lf file\n"; cout << "No index: lf file\n";
ret = read_file(path); ret = read_file(path);

Loading…
Cancel
Save