modif and added several scipts for testing cgi
+ modif html page for script tests + script output not added to response in case of http error
This commit is contained in:
@@ -32,6 +32,9 @@ RESET = "\e[0m"
|
||||
|
||||
NAME_1 = $(SRCS_1:.cpp=.out)
|
||||
NAME_2 = $(SRCS_2:.cpp=.out)
|
||||
NAME_3 = $(SRCS_3:.cpp=.out)
|
||||
NAME_4 = $(SRCS_4:.cpp=.out)
|
||||
NAME_5 = $(SRCS_5:.cpp=.out)
|
||||
|
||||
CXX = c++
|
||||
CXXFLAGS = -Wall -Wextra #-Werror
|
||||
@@ -45,12 +48,18 @@ SRCS_D = .
|
||||
|
||||
SRCS = cgi_utils.cpp
|
||||
SRCS_1 = cgi_cpp.cpp
|
||||
SRCS_2 = cgi_cpp_content_length.cpp
|
||||
SRCS_2 = cgi_cpp_len.cpp
|
||||
SRCS_3 = cgi_cpp_len_big.cpp
|
||||
SRCS_4 = cgi_cpp_len_small.cpp
|
||||
SRCS_5 = cgi_cpp_status.cpp
|
||||
|
||||
OBJS_D = builds
|
||||
OBJS = $(SRCS:%.cpp=$(OBJS_D)/%.o)
|
||||
OBJS_1 = $(SRCS_1:%.cpp=$(OBJS_D)/%.o)
|
||||
OBJS_2 = $(SRCS_2:%.cpp=$(OBJS_D)/%.o)
|
||||
OBJS_3 = $(SRCS_3:%.cpp=$(OBJS_D)/%.o)
|
||||
OBJS_4 = $(SRCS_4:%.cpp=$(OBJS_D)/%.o)
|
||||
OBJS_5 = $(SRCS_5:%.cpp=$(OBJS_D)/%.o)
|
||||
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
# . target: prerequisites . $@ : target #
|
||||
@@ -58,9 +67,12 @@ OBJS_2 = $(SRCS_2:%.cpp=$(OBJS_D)/%.o)
|
||||
# . recipe . $^ : all prerequisites #
|
||||
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
|
||||
|
||||
all: cgi_1 cgi_2
|
||||
all: cgi_1 cgi_2 cgi_3 cgi_4 cgi_5
|
||||
cgi_1: $(NAME_1)
|
||||
cgi_2: $(NAME_2)
|
||||
cgi_3: $(NAME_3)
|
||||
cgi_4: $(NAME_4)
|
||||
cgi_5: $(NAME_5)
|
||||
|
||||
$(OBJS_D)/%.o: %.cpp | $(OBJS_D)
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
@@ -70,7 +82,10 @@ $(OBJS_D):
|
||||
|
||||
$(NAME_1): $(OBJS) $(OBJS_1)
|
||||
$(NAME_2): $(OBJS) $(OBJS_2)
|
||||
$(NAME_1) $(NAME_2):
|
||||
$(NAME_3): $(OBJS) $(OBJS_3)
|
||||
$(NAME_4): $(OBJS) $(OBJS_4)
|
||||
$(NAME_5): $(OBJS) $(OBJS_5)
|
||||
$(NAME_1) $(NAME_2) $(NAME_3) $(NAME_4) $(NAME_5):
|
||||
$(CXX) $^ -o $@
|
||||
|
||||
clean:
|
||||
@@ -79,6 +94,9 @@ clean:
|
||||
fclean: clean
|
||||
rm -f $(NAME_1)
|
||||
rm -f $(NAME_2)
|
||||
rm -f $(NAME_3)
|
||||
rm -f $(NAME_4)
|
||||
rm -f $(NAME_5)
|
||||
|
||||
re: fclean all
|
||||
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
#! /bin/bash
|
||||
echo "status: 100\r\n"
|
||||
echo "\r\n\r\n"
|
||||
echo "hiii"
|
||||
@@ -3,46 +3,13 @@
|
||||
|
||||
int main (int ac, char **av, char ** env)
|
||||
{
|
||||
std::string rq_method = "not found";
|
||||
std::string rq_body = "";
|
||||
std::string rq_query = "";
|
||||
std::string form_infos = "";
|
||||
std::string http_header = "";
|
||||
std::string http_body = "";
|
||||
std::string http_header;
|
||||
std::string http_body;
|
||||
|
||||
(void)ac;
|
||||
(void)av;
|
||||
|
||||
rq_method = parse_env("REQUEST_METHOD");
|
||||
rq_body = parse_body();
|
||||
rq_query = parse_env("QUERY_STRING");
|
||||
|
||||
if (rq_method == "POST")
|
||||
form_infos = rq_body;
|
||||
else if (rq_method == "GET")
|
||||
form_infos = rq_query;
|
||||
|
||||
http_body = HTML_BODY_TOP;
|
||||
|
||||
http_body += "<br><h3>method used: </h3>";
|
||||
http_body += "<p>" + rq_method + "</p>";
|
||||
|
||||
http_body += "<br><h3>form body: </h3>";
|
||||
http_body += "<p>" + rq_body + "</p>";
|
||||
|
||||
http_body += "<br><h3>form query: </h3>";
|
||||
http_body += "<p>" + rq_query + "</p>";
|
||||
|
||||
http_body += "<br><br><h3>output:</h3><br>";
|
||||
http_body += print_form(form_infos, "p", "p");
|
||||
|
||||
http_body += "<br><br><h3>cgi_env_variables:</h3><br>";
|
||||
http_body += print_env(env, "p");
|
||||
|
||||
http_body += HTML_BODY_BOTTOM;
|
||||
|
||||
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
|
||||
http_header += "Content-Length: " + itos(http_body.size() - 10);
|
||||
fill_response_basic(env, http_body, http_header);
|
||||
|
||||
std::cout << http_header << CRLF CRLF << http_body;
|
||||
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
|
||||
# include "cgi_utils.hpp"
|
||||
|
||||
int main ()
|
||||
{
|
||||
std::vector<std::string> split_str;
|
||||
std::vector<std::string> sub_split_str;
|
||||
std::vector<std::string>::const_iterator it;
|
||||
char * tmp;
|
||||
std::string output;
|
||||
std::ostringstream strs;
|
||||
|
||||
std::cout << "Content-Type: text/html; charset=UTF-8" << CRLF CRLF;
|
||||
|
||||
std::cout
|
||||
<< "<!DOCTYPE html>"
|
||||
<< "<html>"
|
||||
<< "<head>"
|
||||
<< " <title>CGI</title>"
|
||||
<< "</head>"
|
||||
<< "<body>"
|
||||
<< " <h2>cgi</h2>"
|
||||
<< " <h3>";
|
||||
|
||||
tmp = getenv("REQUEST_METHOD");
|
||||
if (tmp != NULL)
|
||||
output = tmp;
|
||||
else
|
||||
output = "method not foud";
|
||||
|
||||
std::cout
|
||||
<< output
|
||||
<< " </h3>"
|
||||
<< " <h3>http-request-body-message content :</h3>";
|
||||
|
||||
|
||||
std::cin >> output;
|
||||
split_str = split(output, "&");
|
||||
output.clear();
|
||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
||||
{
|
||||
sub_split_str = split(*it, "=");
|
||||
|
||||
std::cout
|
||||
<< "<p>"
|
||||
<< sub_split_str[0]
|
||||
<< " : "
|
||||
<< sub_split_str[1]
|
||||
<< "</p>";
|
||||
}
|
||||
|
||||
tmp = getenv("QUERY_STRING");
|
||||
if (tmp == NULL)
|
||||
std::cout << "query not foud";
|
||||
|
||||
std::cout
|
||||
<< " <h3>http-uri-query content :</h3>";
|
||||
|
||||
output = tmp;
|
||||
split_str = split(output, "&");
|
||||
output.clear();
|
||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
||||
{
|
||||
sub_split_str = split(*it, "=");
|
||||
|
||||
std::cout
|
||||
<< "<h3>"
|
||||
<< sub_split_str[0]
|
||||
<< "</h3>"
|
||||
<< "<p>"
|
||||
<< sub_split_str[1]
|
||||
<< "</p>";
|
||||
}
|
||||
|
||||
|
||||
std::cout
|
||||
<< "</body>"
|
||||
<< "</html>";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
20
srcs/cgi-bin/cgi_cpp_len.cpp
Normal file
20
srcs/cgi-bin/cgi_cpp_len.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
# include "cgi_utils.hpp"
|
||||
|
||||
int main (int ac, char **av, char ** env)
|
||||
{
|
||||
std::string http_header;
|
||||
std::string http_body;
|
||||
|
||||
(void)ac;
|
||||
(void)av;
|
||||
|
||||
fill_response_basic(env, http_body, http_header);
|
||||
|
||||
http_header += "Content-Length: " + itos(http_body.size());
|
||||
|
||||
std::cout << http_header << CRLF CRLF << http_body;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
20
srcs/cgi-bin/cgi_cpp_len_big.cpp
Normal file
20
srcs/cgi-bin/cgi_cpp_len_big.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
# include "cgi_utils.hpp"
|
||||
|
||||
int main (int ac, char **av, char ** env)
|
||||
{
|
||||
std::string http_header;
|
||||
std::string http_body;
|
||||
|
||||
(void)ac;
|
||||
(void)av;
|
||||
|
||||
fill_response_basic(env, http_body, http_header);
|
||||
|
||||
http_header += "Content-Length: " + itos(http_body.size() + 100);
|
||||
|
||||
std::cout << http_header << CRLF CRLF << http_body;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
20
srcs/cgi-bin/cgi_cpp_len_small.cpp
Normal file
20
srcs/cgi-bin/cgi_cpp_len_small.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
# include "cgi_utils.hpp"
|
||||
|
||||
int main (int ac, char **av, char ** env)
|
||||
{
|
||||
std::string http_header;
|
||||
std::string http_body;
|
||||
|
||||
(void)ac;
|
||||
(void)av;
|
||||
|
||||
fill_response_basic(env, http_body, http_header);
|
||||
|
||||
http_header += "Content-Length: " + itos(http_body.size() - 100);
|
||||
|
||||
std::cout << http_header << CRLF CRLF << http_body;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
22
srcs/cgi-bin/cgi_cpp_status.cpp
Normal file
22
srcs/cgi-bin/cgi_cpp_status.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
# include "cgi_utils.hpp"
|
||||
|
||||
int main (int ac, char **av, char ** env)
|
||||
{
|
||||
std::string http_header;
|
||||
std::string http_body;
|
||||
std::string http_status;
|
||||
|
||||
(void)ac;
|
||||
(void)av;
|
||||
|
||||
fill_response_basic(env, http_body, http_header);
|
||||
|
||||
http_status = get_value("Status");
|
||||
http_header += "Status: " + http_status;
|
||||
|
||||
std::cout << http_header << CRLF CRLF << http_body;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
#include "cgi_utils.hpp"
|
||||
|
||||
std::string str_tolower(std::string str)
|
||||
{
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
||||
return str;
|
||||
}
|
||||
|
||||
std::string trim(std::string str, char del)
|
||||
{
|
||||
size_t pos;
|
||||
@@ -91,14 +97,99 @@ std::string
|
||||
std::vector<std::string> sub_split_str;
|
||||
std::vector<std::string>::const_iterator it;
|
||||
std::string ret = "";
|
||||
std::string key;
|
||||
|
||||
split_str = split(form, "&");
|
||||
for (it = split_str.begin(); it != split_str.end(); ++it)
|
||||
{
|
||||
sub_split_str = split(*it, "=");
|
||||
ret += "<br><" + tag_key + ">" + sub_split_str[0] + ": </" + tag_key + ">";
|
||||
key = sub_split_str[0];
|
||||
if (key == "fname")
|
||||
key = "first name";
|
||||
else if (key == "lname")
|
||||
key = "last name";
|
||||
ret += "<br><" + tag_key + ">" + key + ": </" + tag_key + ">";
|
||||
ret += "<" + tag_val + ">" + sub_split_str[1] + "</" + tag_val + ">";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string get_form_infos()
|
||||
{
|
||||
std::string form_infos;
|
||||
std::string method;
|
||||
|
||||
method = parse_env("REQUEST_METHOD");
|
||||
|
||||
if (method == "POST")
|
||||
form_infos = parse_body();
|
||||
else if (method == "GET")
|
||||
form_infos = parse_env("QUERY_STRING");
|
||||
|
||||
return form_infos;
|
||||
}
|
||||
|
||||
std::string get_value(std::string key)
|
||||
{
|
||||
std::string infos;
|
||||
std::string ret;
|
||||
size_t pos;
|
||||
size_t end;
|
||||
size_t len;
|
||||
|
||||
infos = get_form_infos();
|
||||
pos = str_tolower(infos).find(str_tolower(key));
|
||||
if (pos == NPOS)
|
||||
return "";
|
||||
pos = infos.find_first_of("=", pos);
|
||||
if (pos == NPOS)
|
||||
return "";
|
||||
pos++;
|
||||
end = infos.find_first_of("&", pos);
|
||||
if (end == NPOS)
|
||||
end = infos.size();
|
||||
len = end - pos;
|
||||
ret = infos.substr(pos, len);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
fill_response_basic(char **env, std::string & http_body, std::string & http_header)
|
||||
{
|
||||
std::string rq_method = "not found";
|
||||
std::string rq_body;
|
||||
std::string rq_query;
|
||||
std::string form_infos;
|
||||
|
||||
rq_method = parse_env("REQUEST_METHOD");
|
||||
rq_body = parse_body();
|
||||
rq_query = parse_env("QUERY_STRING");
|
||||
|
||||
if (rq_method == "POST")
|
||||
form_infos = rq_body;
|
||||
else if (rq_method == "GET")
|
||||
form_infos = rq_query;
|
||||
|
||||
http_body = HTML_BODY_TOP;
|
||||
|
||||
http_body += "<br><h3>method used: </h3>";
|
||||
http_body += "<p>" + rq_method + "</p>";
|
||||
|
||||
http_body += "<br><h3>form body: </h3>";
|
||||
http_body += "<p>" + rq_body + "</p>";
|
||||
|
||||
http_body += "<br><h3>form query: </h3>";
|
||||
http_body += "<p>" + rq_query + "</p>";
|
||||
|
||||
http_body += "<br><br><h3>output:</h3><br>";
|
||||
http_body += print_form(form_infos, "p", "p");
|
||||
|
||||
http_body += "<br><br><h3>cgi_env_variables:</h3><br>";
|
||||
http_body += print_env(env, "p");
|
||||
|
||||
http_body += HTML_BODY_BOTTOM;
|
||||
|
||||
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
# include <string>
|
||||
# include <sstream>
|
||||
# include <vector>
|
||||
# include <stdlib.h> // getenv
|
||||
# include <stdlib.h> // getenv
|
||||
# include <algorithm> // transform
|
||||
|
||||
# define CR "\r"
|
||||
# define LF "\n"
|
||||
@@ -27,6 +28,9 @@
|
||||
# define HTML_BODY_BOTTOM " </body>"\
|
||||
"</html>"
|
||||
|
||||
std::string
|
||||
str_tolower(std::string str);
|
||||
|
||||
std::string
|
||||
trim(std::string str, char del);
|
||||
|
||||
@@ -45,8 +49,17 @@ std::string
|
||||
std::string
|
||||
print_env(char **env, std::string tag = "p");
|
||||
|
||||
std::string
|
||||
get_form_infos();
|
||||
|
||||
std::string
|
||||
get_value(std::string key);
|
||||
|
||||
std::string
|
||||
print_form(std::string form, std::string key = "p", std::string val = "p");
|
||||
|
||||
void
|
||||
fill_response_basic(char **env, std::string & body, std::string & header);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -187,6 +187,8 @@ std::string Webserv::_exec_script(Client *client, char **env)
|
||||
void Webserv::_check_script_output(Client *client, std::string & output)
|
||||
{
|
||||
_check_script_status(client, output);
|
||||
if (client->status >= 400 && client->status < 600)
|
||||
return;
|
||||
///*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||
_check_script_fields(client, output);
|
||||
///*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||
|
||||
@@ -93,7 +93,8 @@ void Webserv::_construct_response(Client *client)
|
||||
///*DEBUG*/ std::cout << "\n" B_PURPLE "[response]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||
///*DEBUG*/ std::cout << "\n" B_PURPLE "[script output]:" RESET "\n"; ::print_special(script_output); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||
_check_script_output(client, script_output);
|
||||
client->response += script_output;
|
||||
if (client->status < 400)
|
||||
client->response += script_output;
|
||||
///*DEBUG*/ std::cout << B_YELLOW "inside cgi" RESET "\n";
|
||||
///*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user