merge hugo5 with script cgi tests

This commit is contained in:
Hugo LAMY
2022-08-17 18:26:06 +02:00
26 changed files with 628 additions and 133 deletions

View File

@@ -1,3 +1,4 @@
# - - - - - - #
# #
# COLORS #
@@ -30,11 +31,7 @@ RESET = "\e[0m"
# . name is case sensitive . ?= set if not already set #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
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)
NAME = $(SRCS_X:.cpp=.out)
CXX = c++
CXXFLAGS = -Wall -Wextra #-Werror
@@ -47,19 +44,24 @@ HEADERS_D = .
SRCS_D = .
SRCS = cgi_utils.cpp
SRCS_1 = cgi_cpp.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
SRCS_X = \
cgi_cpp.cpp \
cgi_cpp_bad_headers.cpp \
cgi_cpp_empty.cpp \
cgi_cpp_empty_lines.cpp \
cgi_cpp_len.cpp \
cgi_cpp_len_big.cpp \
cgi_cpp_len_small.cpp \
cgi_cpp_no_body.cpp \
cgi_cpp_no_headers.cpp \
cgi_cpp_only_crlf.cpp \
cgi_cpp_sleep.cpp \
cgi_cpp_status.cpp \
cgi_cpp_download.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)
OBJS_X = $(SRCS_X:%.cpp=$(OBJS_D)/%.o)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target #
@@ -67,36 +69,25 @@ OBJS_5 = $(SRCS_5:%.cpp=$(OBJS_D)/%.o)
# . recipe . $^ : all prerequisites #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
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)
all: $(NAME)
$(OBJS_D)/%.o: %.cpp | $(OBJS_D)
@echo $(B_GREEN)"compilation :" $@ $(RESET)
$(CXX) $(CXXFLAGS) -c $< -o $@
$(OBJS_D):
mkdir $@
$(NAME_1): $(OBJS) $(OBJS_1)
$(NAME_2): $(OBJS) $(OBJS_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 $@
$(NAME): $(OBJS) $(OBJS_X)
$(NAME):
@echo $(B_YELLOW)"linkage :" $@ $(RESET)
$(CXX) $(OBJS) $(@:%.out=$(OBJS_D)/%.o) -o $@
clean:
rm -rf $(OBJS_D)
fclean: clean
rm -f $(NAME_1)
rm -f $(NAME_2)
rm -f $(NAME_3)
rm -f $(NAME_4)
rm -f $(NAME_5)
rm -f $(NAME)
re: fclean all

View File

@@ -5,25 +5,20 @@
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string http_header;
std::string http_body;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
// ::sleep(5);
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
fill_response_basic(env, http_body, http_header);
fill_body_basic(env, http_body, rq_body);
std::cout << http_header;
std::flush(std::cout);
::sleep(2);
std::cout << CRLF CRLF;
std::flush(std::cout);
::sleep(2);
std::cout << http_body;
std::flush(std::cout);
::sleep(2);
std::cout << http_header << CRLF << http_body;
return 0;
}

View File

@@ -0,0 +1,24 @@
# include "cgi_utils.hpp"
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string http_status;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
http_header = "Bad-Headers: wrong";
fill_body_basic(env, http_body, rq_body);
std::cout << http_header << CRLF << http_body;
return 0;
}

View File

@@ -0,0 +1,55 @@
# include "cgi_utils.hpp"
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string rq_body;
std::string form_infos;
std::string path;
std::ifstream ifd;
std::stringstream buf;
size_t status;
std::cin >> rq_body;
(void)ac;
(void)av;
(void)env;
http_header = "Content-Type: image/jpeg" CRLF;
form_infos = get_form_infos(rq_body);
path = get_value("file", rq_body);
path = "./www/" + path;
status = ::eval_file_read(path);
if (status)
{
std::cout << "Status: " << status << CRLF CRLF;
return 0;
}
ifd.open(path.c_str());
if (!ifd)
{
std::cout << "Status: " << 500 << CRLF CRLF;
return 0;
}
else
{
buf << ifd.rdbuf();
if (!ifd || !buf)
{
std::cout << "Status: " << 500 << CRLF CRLF;
return 0;
}
}
std::cout << http_header << CRLF << buf.str();
return 0;
}

View File

@@ -0,0 +1,12 @@
# include "cgi_utils.hpp"
int main (int ac, char **av, char ** env)
{
(void)ac;
(void)av;
(void)env;
return 0;
}

View File

@@ -0,0 +1,23 @@
# include "cgi_utils.hpp"
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
fill_body_basic(env, http_body, rq_body);
std::cout << http_header << CRLF CRLF CRLF CRLF CRLF << http_body;
return 0;
}

View File

@@ -5,15 +5,20 @@ int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
fill_response_basic(env, http_body, http_header);
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
http_header += "Content-Length: " + itos(http_body.size()) + CRLF;
http_header += "Content-Length: " + itos(http_body.size());
fill_body_basic(env, http_body, rq_body);
std::cout << http_header << CRLF CRLF << http_body;
std::cout << http_header << CRLF << http_body;
return 0;
}

View File

@@ -3,17 +3,22 @@
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string http_header;
std::string http_body;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
fill_response_basic(env, http_body, http_header);
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
http_header += "Content-Length: " + itos(http_body.size() + 100) + CRLF;
http_header += "Content-Length: " + itos(http_body.size() + 100);
fill_body_basic(env, http_body, rq_body);
std::cout << http_header << CRLF CRLF << http_body;
std::cout << http_header << CRLF << http_body;
return 0;
}

View File

@@ -3,17 +3,21 @@
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string http_header;
std::string http_body;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
fill_response_basic(env, http_body, http_header);
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
http_header += "Content-Length: " + itos(http_body.size() - 100) + CRLF;
http_header += "Content-Length: " + itos(http_body.size() - 100);
fill_body_basic(env, http_body, rq_body);
std::cout << http_header << CRLF CRLF << http_body;
std::cout << http_header << CRLF << http_body;
return 0;
}

View File

@@ -0,0 +1,22 @@
# include "cgi_utils.hpp"
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_status;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
(void)env;
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
std::cout << http_header << CRLF;
return 0;
}

View File

@@ -0,0 +1,24 @@
# include "cgi_utils.hpp"
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string http_status;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
http_header = CRLF;
fill_body_basic(env, http_body, rq_body);
std::cout << CRLF << http_body;
return 0;
}

View File

@@ -0,0 +1,15 @@
# include "cgi_utils.hpp"
int main (int ac, char **av, char ** env)
{
(void)ac;
(void)av;
(void)env;
std::cout << CRLF CRLF;
return 0;
}

View File

@@ -0,0 +1,29 @@
# include "cgi_utils.hpp"
int main (int ac, char **av, char ** env)
{
std::string http_header;
std::string http_body;
std::string rq_body;
size_t time;
std::stringstream ss;
std::cin >> rq_body;
(void)ac;
(void)av;
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
fill_body_basic(env, http_body, rq_body);
ss << get_value("sleep", rq_body);
ss >> time;
sleep(time);
std::cout << http_header << CRLF << http_body;
return 0;
}

View File

@@ -6,14 +6,19 @@ int main (int ac, char **av, char ** env)
std::string http_header;
std::string http_body;
std::string http_status;
std::string rq_body;
std::cin >> rq_body;
(void)ac;
(void)av;
fill_response_basic(env, http_body, http_header);
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
http_status = get_value("Status");
http_header += "Status: " + http_status;
fill_body_basic(env, http_body, rq_body);
http_status = get_value("Status", rq_body);
http_header += "Status: " + http_status + CRLF;
std::cout << http_header << CRLF CRLF << http_body;

View File

@@ -69,14 +69,6 @@ std::string parse_env(const std::string & env)
return ret;
}
std::string parse_body()
{
std::string ret;
std::cin >> ret;
return ret;
}
std::string print_env(char **env, std::string tag)
{
std::string ret = "";
@@ -114,7 +106,7 @@ std::string
return ret;
}
std::string get_form_infos()
std::string get_form_infos(const std::string & rq_body)
{
std::string form_infos;
std::string method;
@@ -122,14 +114,14 @@ std::string get_form_infos()
method = parse_env("REQUEST_METHOD");
if (method == "POST")
form_infos = parse_body();
form_infos = rq_body;
else if (method == "GET")
form_infos = parse_env("QUERY_STRING");
return form_infos;
}
std::string get_value(std::string key)
std::string get_value(const std::string & key, const std::string & rq_body)
{
std::string infos;
std::string ret;
@@ -137,7 +129,7 @@ std::string get_value(std::string key)
size_t end;
size_t len;
infos = get_form_infos();
infos = get_form_infos(rq_body);
pos = str_tolower(infos).find(str_tolower(key));
if (pos == NPOS)
return "";
@@ -155,15 +147,13 @@ std::string get_value(std::string key)
}
void
fill_response_basic(char **env, std::string & http_body, std::string & http_header)
fill_body_basic(char **env, std::string & http_body, const std::string & rq_body)
{
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")
@@ -189,7 +179,21 @@ void
http_body += print_env(env, "p");
http_body += HTML_BODY_BOTTOM;
http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
}
size_t eval_file_read(const std::string &path)
{
if (::access(path.c_str(), F_OK) == -1)
{
std::perror("err access()");
return 404; // NOT_FOUND, file doesn't exist
}
if (::access(path.c_str(), R_OK) == -1)
{
std::perror("err access()");
return 403; // FORBIDDEN, file doesn't have access permission
}
return 0;
}

View File

@@ -5,9 +5,11 @@
# include <iostream>
# include <string>
# include <sstream>
# include <fstream>
# include <vector>
# include <stdlib.h> // getenv
# include <algorithm> // transform
# include <unistd.h> // sleep, close, access
# define CR "\r"
# define LF "\n"
@@ -24,42 +26,43 @@
" <link href=\"./cgi_style.css\" type=\"text/css\" rel=\"stylesheet\">"\
" </head>"\
" <body>"\
" <h1>cgi</h1><br>"
# define HTML_BODY_BOTTOM " </body>"\
" <h1>CGI</h1><br>"
# define HTML_BODY_BOTTOM " <br><h1>END CGI</h1>"\
" </body>"\
"</html>"
std::string
str_tolower(std::string str);
str_tolower(std::string str);
std::string
trim(std::string str, char del);
trim(std::string str, char del);
std::vector<std::string>
split(const std::string & input, std::string delim, char ctrim = '\0');
split(const std::string & input, std::string delim, char ctrim = '\0');
std::string
itos(int n);
itos(int n);
std::string
parse_env(const std::string & env);
parse_env(const std::string & env);
std::string
parse_body();
print_env(char **env, std::string tag = "p");
std::string
print_env(char **env, std::string tag = "p");
get_form_infos(const std::string & rq_body);
std::string
get_form_infos();
get_value(const std::string & key, const std::string & rq_body);
std::string
get_value(std::string key);
std::string
print_form(std::string form, std::string key = "p", std::string val = "p");
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);
fill_body_basic(char **env, std::string & http_body, const std::string & rq_body);
size_t
eval_file_read(const std::string &path);
#endif