nicolas-arnaud 2 years ago
parent
commit
d1f114b887
  1. 4
      default.json
  2. 7
      includes/Route.hpp
  3. 2
      includes/webserv.hpp
  4. 1
      public/testsite/basique.html
  5. 8
      public/testsite/index.php
  6. 6
      srcs/load/Route.cpp
  7. 62
      srcs/load/Socket.cpp
  8. 10
      srcs/tools.cpp

4
default.json

@ -13,7 +13,9 @@
"listens": ["192.168.62.61:8080", "localhost", "555"],
"root": "public/testsite",
"indexs": ["basique.html"],
"cgi": "cgi.sh",
"cgi": {
".php": "/usr/bin/php-cgi"
},
"locations":
{
"docs/":

7
includes/Route.hpp

@ -7,11 +7,13 @@ class Route {
string _location;
string _root;
string _ret;
std::vector<string> _indexs;
std::vector<string> _headers;
bool _autoindex;
public:
std::vector<string> _indexs;
std::vector<string> _headers;
std::map<string, string> _cgi;
Route(Server *server, string location, JSONNode *datas);
~Route(void);
string getLocation(void);
@ -22,4 +24,5 @@ class Route {
Server *getServer(void);
string getIndex(string uri, string path);
string correctUri(string uri);
friend class Socket;
};

2
includes/webserv.hpp

@ -8,6 +8,7 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <algorithm>
@ -50,6 +51,7 @@ bool isInt(string str);
std::vector<string> split(string str, char delim);
listen_t get_listen_t(string listen);
string getMime(string path);
string get_extension(string str);
string read_file(string path);
#include "Nodes.hpp"

1
public/testsite/basique.html

@ -26,6 +26,7 @@
<ul>
<li><a href="/docs/">Documentss</a></li>
<li><a href="/img/">Images</a></li>
<li><a href="/index.php">php-cgi test</a></li>
</ul>
<form action="/" method="post" enctype="multipart/form-data">
<input type="text" name="description" value="form-data" />

8
public/testsite/index.php

@ -0,0 +1,8 @@
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>

6
srcs/load/Route.cpp

@ -23,6 +23,12 @@ Route::Route(Server *server, string location, JSONNode *datas)
_headers.push_back((*it)->str());
}
}
if ((tmp = object["cgi"])) {
JSONObject cgis = tmp->obj();
for (JSONObject::iterator it = cgis.begin(); it != cgis.end(); it++) {
_cgi[(*it).first] = (*it).second->str();
}
}
}
Route::~Route(void) {}

62
srcs/load/Socket.cpp

@ -216,29 +216,57 @@ int Socket::answer(Env *env) {
string method = _request["Method:"].at(0);
std::vector<string> allowed;
if (method != "GET" && method != "POST" && method != "DELETE")
send_answer(
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
else if ((allowed = route->getHeadersLst()).size() > 0) {
send_answer("HTTP/1.1 405 Method Not Allowed\r\n\r\n");
else if ((allowed = route->_headers).size() > 0) {
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");
} else if ((allowed = server->getHeadersLst()).size() > 0) {
send_answer("HTTP/1.1 405 Method Not Allowed\r\n\r\n");
} else if ((allowed = server->_headers).size() > 0) {
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");
send_answer("HTTP/1.1 405 Method Not Allowed\r\n\r\n");
}
string path = route->correctUri(_request["Method:"].at(1));
cout << "Path: " << path << "\n";
ret = route->getIndex(_request["Method:"].at(1), path);
if (ret == "") {
cout << "No index: lf file\n";
ret = read_file(path);
}
answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n"
: " 200 OK\r\n")
<< ret;
cout << "|===|Answer|===|\n" << answer.str() << "|===|End of answer|===|\n";
string cgi;
if (route->_cgi.size())
cgi = route->_cgi[get_extension(path)];
cout << "Cgi:" << cgi << "\n";
if (cgi != "") {
int status;
int fd[2];
pipe(fd);
int pid = fork();
if (pid == 0) {
const char **args = new const char *[cgi.length() + path.length() + 2];
args[0] = cgi.c_str();
args[1] = path.c_str();
args[2] = NULL;
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
close(fd[0]);
execve(cgi.c_str(), (char **)args, NULL);
}
close(fd[1]);
waitpid(pid, &status, 0);
cout << "Cgi finished\n";
char buffer[10000];
size_t len = read(fd[0], buffer, 10000);
buffer[len] = 0;
ret = string(buffer);
len = ret.length() - ret.find("\r\n\r\n") - 4;
answer << " 200 OK\r\nContent-length: " << len << "\r\n";
answer << ret;
} else {
ret = route->getIndex(_request["Method:"].at(1), path);
if (ret == "") {
cout << "No index: lf file\n";
ret = read_file(path);
}
answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n"
: " 200 OK\r\n")
<< ret;
}
cout << "|===|Answer|===|\n" << answer.str() << "|===|End of answer|===|\n";
send_answer(answer.str());
_content = "";
_header = "";

10
srcs/tools.cpp

@ -22,9 +22,8 @@ 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)) {
while (getline(ss, token, delim))
tokens.push_back(token);
}
return tokens;
}
@ -210,6 +209,13 @@ string getMime(string path) {
return ("text/plain");
}
string get_extension(string str) {
int dot_pos = str.rfind('.');
string ret = str.substr(dot_pos);
cout << ret << "\n";
return ret;
}
string read_file(string path) {
string str;
string content;

Loading…
Cancel
Save