Browse Source

improved route finding and uri correction

test
nicolas-arnaud 2 years ago
parent
commit
fae8641138
  1. 3
      includes/webserv.hpp
  2. 4
      srcs/load/Route.cpp
  3. 22
      srcs/load/Server.cpp
  4. 5
      srcs/load/Socket.cpp
  5. 10
      srcs/tools.cpp

3
includes/webserv.hpp

@ -11,6 +11,7 @@
#include <stdio.h>
#include <unistd.h>
#include <algorithm>
#include <cctype>
#include <cerrno>
#include <climits>
@ -19,6 +20,7 @@
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <map>
@ -41,6 +43,7 @@ typedef std::vector<JSONNode *> JSONList;
void *ft_memset(void *b, int c, size_t len);
bool isInt(string str);
std::vector<string> split(string str, char delim);
#include "Nodes.hpp"
#include "Token.hpp"

4
srcs/load/Route.cpp

@ -57,6 +57,10 @@ string Route::correctUri(string uri) {
cout << "Correcting request: " << uri
<< " with root: " << root << "\n";
int i = 0;
while (uri.at(i) == '/')
i++;
uri.erase(0, i);
while ((slash_pos = root.find('/')) > 0) {
ret << root.substr(0, slash_pos);
root.erase(0, slash_pos);

22
srcs/load/Server.cpp

@ -42,10 +42,24 @@ void Server::refresh(void) {
}
Route *Server::get_route(string uri) {
for (std::map<string, Route *>::iterator it = _routes.begin();
it != _routes.end(); it++) {
if (uri == (*it).first)
return (*it).second;
cout << uri << "\n";
std::vector<string> req = split(uri, '/');
std::vector<string> root;
for (std::map<string, Route *>::iterator rit = _routes.begin();
rit != _routes.end(); rit++) {
root = split((*rit).first, '/');
std::vector<string>::iterator root_it = root.begin();
for (std::vector<string>::iterator it = req.begin();
it < req.end(); it++) {
if (*it == "")
continue ;
cout << *it << " - " << *root_it << "\n";
if (*it != *(root_it++))
break;
if (root_it == root.end())
return ((*rit).second);
}
}
return this;
}

5
srcs/load/Socket.cpp

@ -108,10 +108,13 @@ void Socket::refresh() {
}
void Socket::answer(int fd, string request) {
string uri = split(request, ' ').at(1);
cout << uri << "\n";
cout << request << "\n|===|===|===|\n";
std::stringstream answer;
answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: ";
string uri = "docs/";
Route *route = _server->get_route(uri);
answer << route->getAutoindex(uri);
cout << answer.str() << "\n|===|===|===|\n";

10
srcs/tools.cpp

@ -17,3 +17,13 @@ bool isInt(string str) {
return false;
return true;
}
std::vector<string> split(string str, char delim) {
std::vector<std::string> tokens;
std::string token;
std::stringstream ss(str);
while (getline(ss, token, delim)){
tokens.push_back(token);
}
return tokens;
}

Loading…
Cancel
Save