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. 44
      srcs/load/Socket.cpp
  8. 10
      srcs/tools.cpp

4
default.json

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

7
includes/Route.hpp

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

2
includes/webserv.hpp

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

1
public/testsite/basique.html

@ -26,6 +26,7 @@
<ul> <ul>
<li><a href="/docs/">Documentss</a></li> <li><a href="/docs/">Documentss</a></li>
<li><a href="/img/">Images</a></li> <li><a href="/img/">Images</a></li>
<li><a href="/index.php">php-cgi test</a></li>
</ul> </ul>
<form action="/" method="post" enctype="multipart/form-data"> <form action="/" method="post" enctype="multipart/form-data">
<input type="text" name="description" value="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()); _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) {} Route::~Route(void) {}

44
srcs/load/Socket.cpp

@ -216,20 +216,47 @@ int Socket::answer(Env *env) {
string method = _request["Method:"].at(0); string method = _request["Method:"].at(0);
std::vector<string> allowed; std::vector<string> allowed;
if (method != "GET" && method != "POST" && method != "DELETE") if (method != "GET" && method != "POST" && method != "DELETE")
send_answer( send_answer("HTTP/1.1 405 Method Not Allowed\r\n\r\n");
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); else if ((allowed = route->_headers).size() > 0) {
else if ((allowed = route->getHeadersLst()).size() > 0) {
if (std::find(allowed.begin(), allowed.end(), method) == allowed.end()) if (std::find(allowed.begin(), allowed.end(), method) == allowed.end())
send_answer( send_answer("HTTP/1.1 405 Method Not Allowed\r\n\r\n");
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n"); } else if ((allowed = server->_headers).size() > 0) {
} else if ((allowed = server->getHeadersLst()).size() > 0) {
if (std::find(allowed.begin(), allowed.end(), method) == allowed.end()) if (std::find(allowed.begin(), allowed.end(), method) == allowed.end())
send_answer( send_answer("HTTP/1.1 405 Method Not Allowed\r\n\r\n");
"HTTP/1.1 405 Method Not Allowed\r\nContent-length: 0\r\n\r\n");
} }
string path = route->correctUri(_request["Method:"].at(1)); string path = route->correctUri(_request["Method:"].at(1));
cout << "Path: " << path << "\n"; cout << "Path: " << path << "\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); ret = route->getIndex(_request["Method:"].at(1), path);
if (ret == "") { if (ret == "") {
cout << "No index: lf file\n"; cout << "No index: lf file\n";
@ -238,6 +265,7 @@ int Socket::answer(Env *env) {
answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n" answer << (ret == "" ? " 404 Not Found\r\nContent-length: 0\r\n\r\n"
: " 200 OK\r\n") : " 200 OK\r\n")
<< ret; << ret;
}
cout << "|===|Answer|===|\n" << answer.str() << "|===|End of answer|===|\n"; cout << "|===|Answer|===|\n" << answer.str() << "|===|End of answer|===|\n";
send_answer(answer.str()); send_answer(answer.str());
_content = ""; _content = "";

10
srcs/tools.cpp

@ -22,9 +22,8 @@ std::vector<string> split(string str, char delim) {
std::vector<std::string> tokens; std::vector<std::string> tokens;
std::string token; std::string token;
std::stringstream ss(str); std::stringstream ss(str);
while (getline(ss, token, delim)) { while (getline(ss, token, delim))
tokens.push_back(token); tokens.push_back(token);
}
return tokens; return tokens;
} }
@ -210,6 +209,13 @@ string getMime(string path) {
return ("text/plain"); 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 read_file(string path) {
string str; string str;
string content; string content;

Loading…
Cancel
Save