diff --git a/testers/cgi_tester b/42_testers/cgi_tester similarity index 100% rename from testers/cgi_tester rename to 42_testers/cgi_tester diff --git a/testers/tester b/42_testers/tester similarity index 100% rename from testers/tester rename to 42_testers/tester diff --git a/README.md b/README.md index a9668a1..56711d5 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,11 @@ SERVER_SOFTWARE : the server software you're using (e.g. Apache 1.3) ``` REDIRECT_STATUS : for exemple, 200 ``` +#### cgi questions : +- when should we use the cgi ? + - execute cgi based on certain file extensions as defined in configuration file + - if the path lead to the cgi script ? + - for certain methods like POST GET or DELETE ? --- ## ressources diff --git a/headers/Webserv.hpp b/headers/Webserv.hpp index fda9751..bef8f0e 100644 --- a/headers/Webserv.hpp +++ b/headers/Webserv.hpp @@ -4,24 +4,23 @@ # include # include -# include // errno -# include // perror +# include // errno +# include // perror # include # include -# include // close -# include // cout, cin -# include // memset - -# include // socket, accept, listen, send, recv, bind, connect, setsockopt, getsockname -# include // sockaddr_in -// # include // usefull for what ? -# include // htonl, htons, ntohl, ntohs, inet_addr - -# include // epoll -# include // fcntl +# include // close +# include // cout, cin +# include // memset +# include // socket, accept, listen, send, recv, bind, connect, setsockopt, getsockname +# include // sockaddr_in +# include // htonl, htons, ntohl, ntohs, inet_addr +# include // epoll +# include // fcntl +# include // waitpid +# include // stringstream #define BUFSIZE 8192 -#define TIMEOUT 3 * 60 * 1000 +#define TIMEOUT 10 * 1000 #define MAX_EVENTS 42 // arbitrary #define MSG_TEST "Le Webserv / 20 =D\n" #define MSG_BOUNCE "bounced properly ;)\n" // placeholder diff --git a/index.html b/index.html new file mode 100644 index 0000000..ae36576 --- /dev/null +++ b/index.html @@ -0,0 +1,13 @@ + + + + + + + +
+ +
+ + + diff --git a/srcs/Webserv.cpp b/srcs/Webserv.cpp index b0bd37f..9c57479 100644 --- a/srcs/Webserv.cpp +++ b/srcs/Webserv.cpp @@ -177,6 +177,73 @@ void Webserv::_read_request(int fd) void Webserv::_send_response(int fd) { + + + // TMP test cgi + // find POST in _buf + std::string tmpstr = _buf; + std::size_t found; + found = tmpstr.find("POST"); + // if "POST" found, execve a cgi + if (found != std::string::npos) + { + int save_stdout; + char** env = new char*[4]; + char * const * nll = NULL; + + // set env + env[0] = strdup("PATH_INFO=/no"); + env[1] = strdup("REQUEST_METHOD=POST"); + env[2] = strdup("SERVER_PROTOCOL=HTTP/1.1"); + env[3] = NULL; + // save STDOUT + save_stdout = dup(STDOUT_FILENO); + // inside chil process + if (fork() == 0) + { + dup2(fd, STDOUT_FILENO); + execve("./srcs/cgi-bin/cgi_cpp.cgi", nll, env); + } + // inside parent process + else + waitpid(-1, NULL, 0); + // restore stdout + dup2(save_stdout, STDOUT_FILENO); + // don't send the rest + ::close(fd); + return; + } + else + found = tmpstr.find("index.html"); + // if "index.html" found, send the page + if (found != std::string::npos) + { + int index_fd; + std::string to_send; + std::string end_header = "\r\n\r\n"; + std::string body; + std::stringstream strs; + char buffer[1]; + + to_send = "HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8\nContent-Length: "; + index_fd = open("./index.html", O_RDONLY); + for (int ret = 1; ret > 0;) + { + ret = read(index_fd, buffer, 1); + body += buffer; + } + strs << body.size(); + to_send += strs.str(); + to_send += end_header; + to_send += body; + ::send(fd, to_send.c_str(), to_send.size(), 0); + // don't send the rest + ::close(fd); + return; + } + // TMP end test cgi + + std::cout << "send()\n"; if (::send(fd, _buf, _read_ret, 0) == -1) std::perror("err send(): "); diff --git a/srcs/cgi-bin/cgi.cpp b/srcs/cgi-bin/cgi.cpp new file mode 100644 index 0000000..3882239 --- /dev/null +++ b/srcs/cgi-bin/cgi.cpp @@ -0,0 +1,41 @@ +# include +# include +# include + +int main (int ac, char **av) { + std::string to_send; + std::string header; + std::string end_header = "\r\n\r\n"; + std::string response; + std::stringstream strs; + + header = "HTTP/1.1 200 OK\n"; + header += "Content-Type: text/html; charset=UTF-8\n"; + header += "Content-Length: "; + + response = "\n"; + response += "\n"; + response += "\n"; + response += "CGI\n"; + response += "\n"; + response += "\n"; + response += "

CGI request :

\n"; + for (int i = 1; i < ac; i++) + { + response += "

"; + response += av[i]; + response += "

\n"; + } + response += "\n"; + response += "\n"; + + strs << response.size(); + header += strs.str(); + header += end_header; + to_send = header; + to_send += response; + + std::cout << to_send; + + return 0; +} diff --git a/srcs/cgi-bin/cgi_cpp.cgi b/srcs/cgi-bin/cgi_cpp.cgi new file mode 100755 index 0000000..95013cb Binary files /dev/null and b/srcs/cgi-bin/cgi_cpp.cgi differ diff --git a/srcs/cgi-bin/php-cgi b/srcs/cgi-bin/php-cgi new file mode 100755 index 0000000..bb78d68 --- /dev/null +++ b/srcs/cgi-bin/php-cgi @@ -0,0 +1,7 @@ +#! /usr/bin/php + +# +