From 036256522a5baafcd31f52280212665f534001b5 Mon Sep 17 00:00:00 2001
From: hugogogo
Date: Mon, 15 Aug 2022 15:28:19 +0200
Subject: [PATCH 1/5] makefile for cgi_scripts
---
Makefile | 23 +++++--
srcs/cgi-bin/Makefile | 86 +++++++++++++++++++++++
srcs/cgi-bin/cgi_cpp.cpp | 90 +++----------------------
srcs/cgi-bin/cgi_cpp_content_length.cpp | 55 +--------------
srcs/cgi-bin/cgi_utils.cpp | 78 +++++++++++++++++++++
srcs/cgi-bin/cgi_utils.hpp | 40 +++++++++++
www/{form_get.html => form_cgi.html} | 0
7 files changed, 232 insertions(+), 140 deletions(-)
create mode 100644 srcs/cgi-bin/Makefile
create mode 100644 srcs/cgi-bin/cgi_utils.cpp
create mode 100644 srcs/cgi-bin/cgi_utils.hpp
rename www/{form_get.html => form_cgi.html} (100%)
diff --git a/Makefile b/Makefile
index a67a053..8ed7131 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ CXXFLAGS = -Wall -Wextra #-Werror
CXXFLAGS += $(HEADERS_D:%=-I%)
CXXFLAGS += -std=c++98
CXXFLAGS += -g
-CXXFLAGS += -fno-limit-debug-info
+#CXXFLAGS += -fno-limit-debug-info
CXXFLAGS += -MMD -MP #header dependencie
#CXXFLAGS += -O3
@@ -18,7 +18,7 @@ HEADERS_D = srcs \
SRCS_D = srcs \
srcs/webserv \
- srcs/config
+ srcs/config \
SRCS = main.cpp \
base.cpp init.cpp close.cpp epoll_update.cpp signal.cpp \
@@ -36,9 +36,12 @@ OBJS_D = builds
OBJS = $(SRCS:%.cpp=$(OBJS_D)/%.o)
DEPS = $(OBJS:.o=.d) #header dependencie
-# --------------------
-# ------ RULES -------
-# --------------------
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
+# . target: prerequisites . $@ : target #
+# RULES . recipe . $< : 1st prerequisite #
+# . @recipe (silent) . $^ : all prerequisites #
+# . target: VAR = assignment . | : order-only prereq. #
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: $(NAME)
@@ -53,6 +56,16 @@ $(NAME): $(OBJS)
$(CXX) $^ -o $(NAME)
echo "$(_GREEN)\r\33[2K\r$(NAME) created 😎$(_END)"
+# CGI
+cgi:
+ make -C srcs/cgi-bin
+cgiclean:
+ make clean -C srcs/cgi-bin
+cgifclean:
+ make fclean -C srcs/cgi-bin
+cgire:
+ make re -C srcs/cgi-bin
+
clean:
rm -rf $(OBJS_D)
diff --git a/srcs/cgi-bin/Makefile b/srcs/cgi-bin/Makefile
new file mode 100644
index 0000000..5f521ed
--- /dev/null
+++ b/srcs/cgi-bin/Makefile
@@ -0,0 +1,86 @@
+# - - - - - - #
+# #
+# COLORS #
+# #
+# - - - - - - #
+
+GRAY = "\e[0;30m"
+RED = "\e[0;31m"
+GREEN = "\e[0;32m"
+YELLOW = "\e[0;33m"
+BLUE = "\e[0;34m"
+PURPLE = "\e[0;35m"
+CYAN = "\e[0;36m"
+WHITE = "\e[0;37m"
+
+B_GRAY = "\e[1;30m"
+B_RED = "\e[1;31m"
+B_GREEN = "\e[1;32m"
+B_YELLOW = "\e[1;33m"
+B_BLUE = "\e[1;34m"
+B_PURPLE = "\e[1;35m"
+B_CYAN = "\e[1;36m"
+B_WHITE = "\e[1;37m"
+
+RESET = "\e[0m"
+
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
+# . name = value \ . += append to a variable #
+# VARIABLES . value . != set result of command #
+# . name is case sensitive . ?= set if not already set #
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
+
+NAME_1 = $(SRCS_1:.cpp=.out)
+NAME_2 = $(SRCS_2:.cpp=.out)
+
+CXX = c++
+CXXFLAGS = -Wall -Wextra #-Werror
+CXXFLAGS += $(HEADERS_D:%=-I%)
+CXXFLAGS += -std=c++98
+CXXFLAGS += -g
+
+VPATH = $(SRCS_D)
+HEADERS_D = .
+SRCS_D = .
+
+SRCS = cgi_utils.cpp
+SRCS_1 = cgi_cpp.cpp
+SRCS_2 = cgi_cpp_content_length.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)
+
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
+# . target: prerequisites . $@ : target #
+# RULES . recipe . $< : 1st prerequisite #
+# . recipe . $^ : all prerequisites #
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
+
+all: cgi_1 cgi_2
+cgi_1: $(NAME_1)
+cgi_2: $(NAME_2)
+
+$(OBJS_D)/%.o: %.cpp | $(OBJS_D)
+ $(CXX) $(CXXFLAGS) -c $< -o $@
+
+$(OBJS_D):
+ mkdir $@
+
+$(NAME_1): $(OBJS) $(OBJS_1)
+$(NAME_2): $(OBJS) $(OBJS_2)
+$(NAME_1) $(NAME_2):
+ $(CXX) $^ -o $@
+
+clean:
+ rm -rf $(OBJS_D)
+
+fclean: clean
+ rm -f $(NAME_1)
+ rm -f $(NAME_2)
+
+re: fclean all
+
+.PHONY : all clean fclean re
+
diff --git a/srcs/cgi-bin/cgi_cpp.cpp b/srcs/cgi-bin/cgi_cpp.cpp
index 8cabc71..7b8b730 100644
--- a/srcs/cgi-bin/cgi_cpp.cpp
+++ b/srcs/cgi-bin/cgi_cpp.cpp
@@ -1,91 +1,20 @@
-# include
-# include
-# include
-# include
-# include // getenv
-# define CR "\r"
-# define LF "\n"
-# define CRLF CR LF
-# define NPOS std::string::npos
+# include "cgi_utils.hpp"
-std::string trim(std::string str, char del)
-{
- size_t pos;
-
- // delete leadings del
- pos = str.find_first_not_of(del);
- if (pos == NPOS)
- pos = str.size();
- str = str.substr(pos);
-
- // delete trailing del
- pos = str.find_last_not_of(del);
- if (pos != NPOS)
- str = str.substr(0, pos + 1);
-
- return str;
-}
-
-std::vector
- split(const std::string & input, std::string delim, char ctrim = '\0')
-{
- std::vector split_str;
- std::string tmp;
- size_t start = 0;
- size_t end = 0;
- size_t len = 0;
-
- while (end != NPOS)
- {
- end = input.find(delim, start);
- len = end - start;
- if (end == NPOS)
- len = end;
- tmp = input.substr(start, len);
- if (ctrim != '\0')
- tmp = trim(tmp, ctrim);
- if (tmp.size() != 0)
- split_str.push_back( tmp );
- start = end + delim.size();
- }
- return split_str;
-}
-
-int main (int ac, char **av, char **en)
+int main ()
{
std::vector split_str;
std::vector sub_split_str;
std::vector::const_iterator it;
- char * tmp;
std::string input;
std::string http_header;
std::string http_body;
- std::ostringstream strs;
- size_t pos;
std::cin >> input;
- http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
- http_header += "Content-Length: ";
+ http_body = HTML_BODY_TOP;
- http_body = "\
- \
- \
- \
- CGI\
- \
- \
- cgi
\
- ";
-
- http_body += "";
- tmp = getenv("REQUEST_METHOD");
- if (tmp != NULL)
- http_body += tmp;
- else
- http_body = "method not foud";
- http_body += "
";
+ http_body += fill_env("REQUEST_METHOD", "h3");
split_str = split(input, "&");
for (it = split_str.begin(); it != split_str.end(); ++it)
@@ -99,14 +28,11 @@ int main (int ac, char **av, char **en)
http_body += "
";
}
- http_body += "\
- \
- \
- ";
+ http_body += HTML_BODY_BOTTOM;
- strs << http_body.size();
- http_header += strs.str();
- http_header += CRLF CRLF;
+ http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
+ http_header += "Content-Length: ";
+ http_header += itos(http_body.size());
std::cout << http_header << CRLF CRLF << http_body;
return 0;
diff --git a/srcs/cgi-bin/cgi_cpp_content_length.cpp b/srcs/cgi-bin/cgi_cpp_content_length.cpp
index 9bb5089..c23cb50 100644
--- a/srcs/cgi-bin/cgi_cpp_content_length.cpp
+++ b/srcs/cgi-bin/cgi_cpp_content_length.cpp
@@ -1,65 +1,14 @@
-# include
-# include
-# include
-# include
-# include // getenv
-# define CR "\r"
-# define LF "\n"
-# define CRLF CR LF
-# define NPOS std::string::npos
+# include "cgi_utils.hpp"
-std::string trim(std::string str, char del)
+int main ()
{
- size_t pos;
-
- // delete leadings del
- pos = str.find_first_not_of(del);
- if (pos == NPOS)
- pos = str.size();
- str = str.substr(pos);
-
- // delete trailing del
- pos = str.find_last_not_of(del);
- if (pos != NPOS)
- str = str.substr(0, pos + 1);
-
- return str;
-}
-
-std::vector
- split(const std::string & input, std::string delim, char ctrim = '\0')
-{
- std::vector split_str;
- std::string tmp;
- size_t start = 0;
- size_t end = 0;
- size_t len = 0;
-
- while (end != NPOS)
- {
- end = input.find(delim, start);
- len = end - start;
- if (end == NPOS)
- len = end;
- tmp = input.substr(start, len);
- if (ctrim != '\0')
- tmp = trim(tmp, ctrim);
- if (tmp.size() != 0)
- split_str.push_back( tmp );
- start = end + delim.size();
- }
- return split_str;
-}
-
-int main (int ac, char **av, char **en) {
std::vector split_str;
std::vector sub_split_str;
std::vector::const_iterator it;
char * tmp;
std::string output;
std::ostringstream strs;
- size_t pos;
std::cout << "Content-Type: text/html; charset=UTF-8" << CRLF CRLF;
diff --git a/srcs/cgi-bin/cgi_utils.cpp b/srcs/cgi-bin/cgi_utils.cpp
new file mode 100644
index 0000000..0c30116
--- /dev/null
+++ b/srcs/cgi-bin/cgi_utils.cpp
@@ -0,0 +1,78 @@
+#include "cgi_utils.hpp"
+
+std::string trim(std::string str, char del)
+{
+ size_t pos;
+
+ // delete leadings del
+ pos = str.find_first_not_of(del);
+ if (pos == NPOS)
+ pos = str.size();
+ str = str.substr(pos);
+
+ // delete trailing del
+ pos = str.find_last_not_of(del);
+ if (pos != NPOS)
+ str = str.substr(0, pos + 1);
+
+ return str;
+}
+
+std::vector
+ split(const std::string & input, std::string delim, char ctrim)
+{
+ std::vector split_str;
+ std::string tmp;
+ size_t start = 0;
+ size_t end = 0;
+ size_t len = 0;
+
+ while (end != NPOS)
+ {
+ end = input.find(delim, start);
+ len = end - start;
+ if (end == NPOS)
+ len = end;
+ tmp = input.substr(start, len);
+ if (ctrim != '\0')
+ tmp = trim(tmp, ctrim);
+ if (tmp.size() != 0)
+ split_str.push_back( tmp );
+ start = end + delim.size();
+ }
+ return split_str;
+}
+
+std::string itos(int n)
+{
+ std::stringstream strs;
+
+ strs << n;
+ return ( strs.str() );
+}
+
+std::string fill_env(std::string env, std::string tag)
+{
+ std::string ret;
+ char * ret_env;
+
+ ret = "<";
+ ret += tag;
+ ret += ">";
+
+ ret_env = getenv(env.c_str());
+ if (ret_env != NULL)
+ ret += ret_env;
+ else
+ {
+ ret += env;
+ ret += " not foud";
+ }
+
+ ret += "";
+ ret += tag;
+ ret += ">";
+
+ return ret;
+}
+
diff --git a/srcs/cgi-bin/cgi_utils.hpp b/srcs/cgi-bin/cgi_utils.hpp
new file mode 100644
index 0000000..eca8b3f
--- /dev/null
+++ b/srcs/cgi-bin/cgi_utils.hpp
@@ -0,0 +1,40 @@
+
+#ifndef CGI_UTILS_HPP
+# define CGI_UTILS_HPP
+
+# include
+# include
+# include
+# include
+# include // getenv
+
+# define CR "\r"
+# define LF "\n"
+# define CRLF CR LF
+# define CRLF_SIZE 2
+# define NPOS std::string::npos
+
+# define HTML_BODY_TOP ""\
+ ""\
+ " "\
+ " CGI"\
+ " "\
+ " "\
+ " cgi
"
+# define HTML_BODY_BOTTOM " "\
+ ""
+
+std::string
+ trim(std::string str, char del);
+
+std::vector
+ split(const std::string & input, std::string delim, char ctrim = '\0');
+
+std::string
+ itos(int n);
+
+std::string
+ fill_env(std::string env, std::string tag);
+
+#endif
+
diff --git a/www/form_get.html b/www/form_cgi.html
similarity index 100%
rename from www/form_get.html
rename to www/form_cgi.html
From 6ad6ec7d637ab2bcaa70a5d31f478d8c6f855139 Mon Sep 17 00:00:00 2001
From: hugogogo
Date: Mon, 15 Aug 2022 16:18:47 +0200
Subject: [PATCH 2/5] wip cgi ouptput
---
srcs/cgi-bin/cgi | 4 +++
srcs/cgi-bin/cgi_cpp.cpp | 63 +++++++++++++++++++++++---------------
srcs/cgi-bin/cgi_utils.cpp | 48 +++++++++++++++++++++++------
srcs/cgi-bin/cgi_utils.hpp | 15 ++++++++-
4 files changed, 94 insertions(+), 36 deletions(-)
create mode 100755 srcs/cgi-bin/cgi
diff --git a/srcs/cgi-bin/cgi b/srcs/cgi-bin/cgi
new file mode 100755
index 0000000..14e0d29
--- /dev/null
+++ b/srcs/cgi-bin/cgi
@@ -0,0 +1,4 @@
+#! /bin/bash
+echo "status: 100\r\n"
+echo "\r\n\r\n"
+echo "hiii"
diff --git a/srcs/cgi-bin/cgi_cpp.cpp b/srcs/cgi-bin/cgi_cpp.cpp
index 7b8b730..7d7ffaa 100644
--- a/srcs/cgi-bin/cgi_cpp.cpp
+++ b/srcs/cgi-bin/cgi_cpp.cpp
@@ -1,40 +1,53 @@
# include "cgi_utils.hpp"
-int main ()
+int main (int ac, char **av, char ** env)
{
- std::vector split_str;
- std::vector sub_split_str;
- std::vector::const_iterator it;
- std::string input;
- std::string http_header;
- std::string http_body;
+ std::string http_req_body;
+ std::string http_resp_header;
+ std::string http_resp_body;
- std::cin >> input;
+ std::string tmp;
+ (void)ac;
+ (void)av;
+ (void)env;
- http_body = HTML_BODY_TOP;
+ /*
+ rq_method = find_method();
+ rq_body = parse_body();
+ rq_query = parse_query();
- http_body += fill_env("REQUEST_METHOD", "h3");
-
- split_str = split(input, "&");
- for (it = split_str.begin(); it != split_str.end(); ++it)
+ method used : GET
+ form query : key=val&key=val
+ form body : EMPTY
+ output :
+ first name: John
+ last name: Doe
+ cgi env variables :
+ CONTENT_TYPE: value
+ ...
+ */
+ tmp = "PRINT ENV _________________
";
+ for (int i = 0; env[i] != NULL; ++i)
{
- sub_split_str = split(*it, "=");
- http_body += "";
- http_body += sub_split_str[0];
- http_body += "
";
- http_body += "";
- http_body += sub_split_str[1];
- http_body += "
";
+ tmp += "";
+ tmp += env[i];
+ tmp += "
";
}
+ tmp += "___________________________
";
- http_body += HTML_BODY_BOTTOM;
+ http_req_body = parse_form_infos();
- http_header = "Content-Type: text/html; charset=UTF-8" CRLF;
- http_header += "Content-Length: ";
- http_header += itos(http_body.size());
+ http_resp_body = HTML_BODY_TOP;
+ http_resp_body += fill_tag("ENV:", "h3") + fill_env("REQUEST_METHOD");
+ http_resp_body += fill_form(http_req_body, "h3", "p");
+ http_resp_body += tmp;
+ http_resp_body += HTML_BODY_BOTTOM;
- std::cout << http_header << CRLF CRLF << http_body;
+ http_resp_header = "Content-Type: text/html; charset=UTF-8" CRLF;
+ http_resp_header += "Content-Length: " + itos(http_resp_body.size());
+
+ std::cout << http_resp_header << CRLF CRLF << http_resp_body;
return 0;
}
diff --git a/srcs/cgi-bin/cgi_utils.cpp b/srcs/cgi-bin/cgi_utils.cpp
index 0c30116..2d5dcb6 100644
--- a/srcs/cgi-bin/cgi_utils.cpp
+++ b/srcs/cgi-bin/cgi_utils.cpp
@@ -51,28 +51,56 @@ std::string itos(int n)
return ( strs.str() );
}
+std::string fill_tag(std::string content, std::string tag)
+{
+ std::string ret;
+
+ ret = "<" + tag + ">" + content + "" + tag + ">";
+ return ret;
+}
+
std::string fill_env(std::string env, std::string tag)
{
std::string ret;
char * ret_env;
- ret = "<";
- ret += tag;
- ret += ">";
+ ret = "<" + tag + ">";
ret_env = getenv(env.c_str());
if (ret_env != NULL)
ret += ret_env;
else
- {
- ret += env;
- ret += " not foud";
- }
+ ret += env + " not foud";
- ret += "";
- ret += tag;
- ret += ">";
+ ret += "" + tag + ">";
return ret;
}
+std::string
+ parse_form_infos()
+{
+ std::string ret;
+
+ std::cin >> ret;
+ return ret;
+}
+
+std::string
+ fill_form(std::string form, std::string tag_key, std::string tag_val)
+{
+ std::vector split_str;
+ std::vector sub_split_str;
+ std::vector::const_iterator it;
+ std::string ret;
+
+ split_str = split(form, "&");
+ for (it = split_str.begin(); it != split_str.end(); ++it)
+ {
+ sub_split_str = split(*it, "=");
+ ret = "<" + tag_key + ">" + sub_split_str[0] + ": " + tag_key + ">";
+ ret = "<" + tag_val + ">" + sub_split_str[1] + "" + tag_val + ">";
+ }
+ return ret;
+}
+
diff --git a/srcs/cgi-bin/cgi_utils.hpp b/srcs/cgi-bin/cgi_utils.hpp
index eca8b3f..a491d39 100644
--- a/srcs/cgi-bin/cgi_utils.hpp
+++ b/srcs/cgi-bin/cgi_utils.hpp
@@ -34,7 +34,20 @@ std::string
itos(int n);
std::string
- fill_env(std::string env, std::string tag);
+ fill_env(std::string env, std::string tag = "p");
+
+std::string
+ fill_tag(std::string env, std::string tag = "p");
+
+std::string
+ fill_form(
+ std::string form,
+ std::string tag_key = "p",
+ std::string tag_val = "p"
+ );
+
+std::string
+ parse_form_infos();
#endif
From 1c13e254d51215def5df882d002c2839623f8869 Mon Sep 17 00:00:00 2001
From: hugogogo
Date: Mon, 15 Aug 2022 18:26:41 +0200
Subject: [PATCH 3/5] basic cgi script test working
---
srcs/cgi-bin/cgi_cpp.cpp | 70 ++++++++++++++++++-------------------
srcs/cgi-bin/cgi_style.css | 3 ++
srcs/cgi-bin/cgi_utils.cpp | 44 +++++++++++------------
srcs/cgi-bin/cgi_utils.hpp | 17 +++++----
srcs/webserv/cgi_script.cpp | 4 +++
srcs/webserv/response.cpp | 11 +++---
6 files changed, 74 insertions(+), 75 deletions(-)
create mode 100644 srcs/cgi-bin/cgi_style.css
diff --git a/srcs/cgi-bin/cgi_cpp.cpp b/srcs/cgi-bin/cgi_cpp.cpp
index 7d7ffaa..10c475a 100644
--- a/srcs/cgi-bin/cgi_cpp.cpp
+++ b/srcs/cgi-bin/cgi_cpp.cpp
@@ -3,51 +3,49 @@
int main (int ac, char **av, char ** env)
{
- std::string http_req_body;
- std::string http_resp_header;
- std::string http_resp_body;
+ 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 tmp;
(void)ac;
(void)av;
- (void)env;
- /*
- rq_method = find_method();
- rq_body = parse_body();
- rq_query = parse_query();
+ rq_method = parse_env("REQUEST_METHOD");
+ rq_body = parse_body();
+ rq_query = parse_env("QUERY_STRING");
- method used : GET
- form query : key=val&key=val
- form body : EMPTY
- output :
- first name: John
- last name: Doe
- cgi env variables :
- CONTENT_TYPE: value
- ...
- */
- tmp = "PRINT ENV _________________
";
- for (int i = 0; env[i] != NULL; ++i)
- {
- tmp += "";
- tmp += env[i];
- tmp += "
";
- }
- tmp += "___________________________
";
+ if (rq_method == "POST")
+ form_infos = rq_body;
+ else if (rq_method == "GET")
+ form_infos = rq_query;
- http_req_body = parse_form_infos();
+ http_body = HTML_BODY_TOP;
- http_resp_body = HTML_BODY_TOP;
- http_resp_body += fill_tag("ENV:", "h3") + fill_env("REQUEST_METHOD");
- http_resp_body += fill_form(http_req_body, "h3", "p");
- http_resp_body += tmp;
- http_resp_body += HTML_BODY_BOTTOM;
+ http_body += "
method used:
";
+ http_body += "" + rq_method + "
";
- http_resp_header = "Content-Type: text/html; charset=UTF-8" CRLF;
- http_resp_header += "Content-Length: " + itos(http_resp_body.size());
+ http_body += "
form body:
";
+ http_body += "" + rq_body + "
";
+
+ http_body += "
form query:
";
+ http_body += "" + rq_query + "
";
+
+ http_body += "
output:
";
+ http_body += print_form(form_infos, "p", "p");
+
+ http_body += "
cgi_env_variables:
";
+ 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);
+
+ std::cout << http_header << CRLF CRLF << http_body;
- std::cout << http_resp_header << CRLF CRLF << http_resp_body;
return 0;
}
diff --git a/srcs/cgi-bin/cgi_style.css b/srcs/cgi-bin/cgi_style.css
new file mode 100644
index 0000000..52c981f
--- /dev/null
+++ b/srcs/cgi-bin/cgi_style.css
@@ -0,0 +1,3 @@
+h1, h2, h3, p {
+ display: inline;
+}
diff --git a/srcs/cgi-bin/cgi_utils.cpp b/srcs/cgi-bin/cgi_utils.cpp
index 2d5dcb6..cea2ca9 100644
--- a/srcs/cgi-bin/cgi_utils.cpp
+++ b/srcs/cgi-bin/cgi_utils.cpp
@@ -51,34 +51,19 @@ std::string itos(int n)
return ( strs.str() );
}
-std::string fill_tag(std::string content, std::string tag)
+std::string parse_env(const std::string & env)
{
- std::string ret;
-
- ret = "<" + tag + ">" + content + "" + tag + ">";
- return ret;
-}
-
-std::string fill_env(std::string env, std::string tag)
-{
- std::string ret;
+ std::string ret = "";
char * ret_env;
- ret = "<" + tag + ">";
-
ret_env = getenv(env.c_str());
if (ret_env != NULL)
- ret += ret_env;
- else
- ret += env + " not foud";
-
- ret += "" + tag + ">";
+ ret = ret_env;
return ret;
}
-std::string
- parse_form_infos()
+std::string parse_body()
{
std::string ret;
@@ -86,20 +71,33 @@ std::string
return ret;
}
+std::string print_env(char **env, std::string tag)
+{
+ std::string ret = "";
+
+ for (int i = 0; env[i] != NULL; ++i)
+ {
+ ret += "<" + tag + ">";
+ ret += env[i];
+ ret += "" + tag + ">
";
+ }
+ return ret;
+}
+
std::string
- fill_form(std::string form, std::string tag_key, std::string tag_val)
+ print_form(std::string form, std::string tag_key, std::string tag_val)
{
std::vector split_str;
std::vector sub_split_str;
std::vector::const_iterator it;
- std::string ret;
+ std::string ret = "";
split_str = split(form, "&");
for (it = split_str.begin(); it != split_str.end(); ++it)
{
sub_split_str = split(*it, "=");
- ret = "<" + tag_key + ">" + sub_split_str[0] + ": " + tag_key + ">";
- ret = "<" + tag_val + ">" + sub_split_str[1] + "" + tag_val + ">";
+ ret += "
<" + tag_key + ">" + sub_split_str[0] + ": " + tag_key + ">";
+ ret += "<" + tag_val + ">" + sub_split_str[1] + "" + tag_val + ">";
}
return ret;
}
diff --git a/srcs/cgi-bin/cgi_utils.hpp b/srcs/cgi-bin/cgi_utils.hpp
index a491d39..00abe7b 100644
--- a/srcs/cgi-bin/cgi_utils.hpp
+++ b/srcs/cgi-bin/cgi_utils.hpp
@@ -17,10 +17,13 @@
# define HTML_BODY_TOP ""\
""\
" "\
+ " "\
+ " "\
" CGI"\
+ " "\
" "\
" "\
- " cgi
"
+ " cgi
"
# define HTML_BODY_BOTTOM " "\
""
@@ -34,20 +37,16 @@ std::string
itos(int n);
std::string
- fill_env(std::string env, std::string tag = "p");
+ parse_env(const std::string & env);
std::string
- fill_tag(std::string env, std::string tag = "p");
+ parse_body();
std::string
- fill_form(
- std::string form,
- std::string tag_key = "p",
- std::string tag_val = "p"
- );
+ print_env(char **env, std::string tag = "p");
std::string
- parse_form_infos();
+ print_form(std::string form, std::string key = "p", std::string val = "p");
#endif
diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp
index e572749..20e9003 100644
--- a/srcs/webserv/cgi_script.cpp
+++ b/srcs/webserv/cgi_script.cpp
@@ -187,9 +187,13 @@ std::string Webserv::_exec_script(Client *client, char **env)
void Webserv::_check_script_output(Client *client, std::string & output)
{
_check_script_status(client, output);
+/*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";
_remove_body_leading_empty_lines(output);
+/*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
_add_script_body_length_header(output);
+/*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
// _check_script_empty_lines(client, output);
// _check_script_space_colons(client, output);
// _check_script_new_lines(client, output);
diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp
index 556ab4d..88906ad 100644
--- a/srcs/webserv/response.cpp
+++ b/srcs/webserv/response.cpp
@@ -85,15 +85,12 @@ void Webserv::_construct_response(Client *client)
if (_is_cgi(client, path))
{
script_output = _exec_cgi(client);
-
-///*DEBUG*/ std::cout << "\n" B_PURPLE "[script output]:" RESET "\n"; ::print_special(script_output); std::cout << B_PURPLE "-----------" RESET "\n\n";
-///*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 "[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;
-
-/*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";
+///*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";
return;
}
From c05536ca0147d226b08abe311fef82cecb9c603d Mon Sep 17 00:00:00 2001
From: Hugo LAMY
Date: Mon, 15 Aug 2022 21:21:40 +0200
Subject: [PATCH 4/5] hugo merge and clean some debug messages
---
srcs/webserv/cgi_script.cpp | 8 ++++----
srcs/webserv/response.cpp | 6 +++---
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp
index 20e9003..8949a77 100644
--- a/srcs/webserv/cgi_script.cpp
+++ b/srcs/webserv/cgi_script.cpp
@@ -187,13 +187,13 @@ std::string Webserv::_exec_script(Client *client, char **env)
void Webserv::_check_script_output(Client *client, std::string & output)
{
_check_script_status(client, output);
-/*DEBUG*/ std::cout << "\n" B_PURPLE "[script status]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
+///*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";
+///*DEBUG*/ std::cout << "\n" B_PURPLE "[script fields]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
_remove_body_leading_empty_lines(output);
-/*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
+///*DEBUG*/ std::cout << "\n" B_PURPLE "[script empty lines]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
_add_script_body_length_header(output);
-/*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
+///*DEBUG*/ std::cout << "\n" B_PURPLE "[script content length]:" RESET "\n"; ::print_special(output); std::cout << B_PURPLE "-----------" RESET "\n\n";
// _check_script_empty_lines(client, output);
// _check_script_space_colons(client, output);
// _check_script_new_lines(client, output);
diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp
index 0cf3356..6d2bcc6 100644
--- a/srcs/webserv/response.cpp
+++ b/srcs/webserv/response.cpp
@@ -90,12 +90,12 @@ void Webserv::_construct_response(Client *client)
if (_is_cgi(client, path))
{
script_output = _exec_cgi(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";
+///*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;
///*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";
+///*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output]:" RESET "\n"; ::print_special(client->response); std::cout << B_PURPLE "-----------" RESET "\n\n";
return;
}
From 48af92b3bc7c780438b6a84b8661b620791d5fc0 Mon Sep 17 00:00:00 2001
From: lperrey
Date: Mon, 15 Aug 2022 22:12:55 +0200
Subject: [PATCH 5/5] fixed _error_html_response() + renamed eval_file_mode()
in eval_file_access() + memo update
---
default_error_pages/404.html | 11 -----------
memo.txt | 27 +++++++++++----------------
srcs/utils.cpp | 6 ++----
srcs/utils.hpp | 2 +-
srcs/webserv/cgi_script.cpp | 2 +-
srcs/webserv/method_delete.cpp | 2 +-
srcs/webserv/method_get.cpp | 2 +-
srcs/webserv/method_post.cpp | 2 +-
srcs/webserv/response.cpp | 14 ++++++++++----
www/error_pages/error_404.html | 4 ++--
10 files changed, 30 insertions(+), 42 deletions(-)
delete mode 100644 default_error_pages/404.html
diff --git a/default_error_pages/404.html b/default_error_pages/404.html
deleted file mode 100644
index df4d27c..0000000
--- a/default_error_pages/404.html
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-
- 404 Not Found
-
-
- 404 Not Found
-
- Le Webserv/0.1
-
-
\ No newline at end of file
diff --git a/memo.txt b/memo.txt
index 7ded14e..6987b8b 100644
--- a/memo.txt
+++ b/memo.txt
@@ -1,19 +1,22 @@
-IN 42 SUBJECT AND/OR PRIORITY :
+----Priorité élevée------------------------
- CGI (TODO HUGO)
- Need to test normal body parsing (Verif avec HUGO)
-- Ecrire des tests !
-- handle redirection (Work, but weird behavior need deeper test)
-
- curl --resolve, for testing hostname
curl -v --resolve server1:4040:127.0.0.1 --resolve server2:4040:127.0.0.1 server1:4040
- test limit de connexions sur listen()
------------------------------
-Si ce n'est pas deja fait :
-peut-être check si ip > 32bits
------------------------------
+- handle redirection (Work, but weird behavior need deeper test)
+- Ecrire des tests !
+
+----Priorité modérée------------------------
+- namespace utils ?
+- change "std::string" to reference "std::string &" in most functions
+and add "const" if apropriate.
+- peut-être check si ip > 32bits
+
+----Priorité faible------------------------
- chunked request (need testing)
- client_body_limit 0 valeur special pour desactiver dans config
- gerer le champ "Accept" du client
@@ -26,11 +29,3 @@ peut-être check si ip > 32bits
- change "std::string" to reference "std::string &" in most functions
and add "const" if apropriate.
- Il faut vérifier le path de la requête, voir si le serveur est bien censé délivrer cette ressource et si le client y a accès, avant d'appeler le CGI.
-
-
-__________________________
---------------------------
-
-----Discord 42------------
-Un truc cool et surtout bien utile ici c'est d'utiliser un proxy entre ton navigateur et ton serveur pour vérifier ce qui est envoyé en raw. Les navigateurs peuvent avoir des comportements différents.
-Vous avez des modules sur vos navigateur ou des logiciels externe. C'est assez rapide et gratuit.
\ No newline at end of file
diff --git a/srcs/utils.cpp b/srcs/utils.cpp
index d85d5d5..4c82845 100644
--- a/srcs/utils.cpp
+++ b/srcs/utils.cpp
@@ -153,10 +153,9 @@ std::string http_methods_to_str(unsigned int methods)
file_type eval_file_type(const std::string &path)
{
- const char *tmp_path = path.c_str(); // variable superflu ?
struct stat s;
- if (stat(tmp_path, &s) != -1)
+ if (stat(path.c_str(), &s) != -1)
{
if (S_ISREG(s.st_mode))
return (IS_FILE);
@@ -171,8 +170,7 @@ file_type eval_file_type(const std::string &path)
return (IS_OTHER);
}
-// rename in "eval_file_access" ?
-size_t eval_file_mode(const std::string &path, int mode)
+size_t eval_file_access(const std::string &path, int mode)
{
if (::access(path.c_str(), F_OK) == -1)
{
diff --git a/srcs/utils.hpp b/srcs/utils.hpp
index 44f5347..7d00e1b 100644
--- a/srcs/utils.hpp
+++ b/srcs/utils.hpp
@@ -67,7 +67,7 @@ std::string trim(std::string str, char del);
http_method str_to_http_method(std::string &str);
std::string http_methods_to_str(unsigned int methods);
file_type eval_file_type(const std::string &path);
-size_t eval_file_mode(const std::string &path, int mode);
+size_t eval_file_access(const std::string &path, int mode);
void replace_all_substr(std::string &str, const std::string &ori_substr, const std::string &new_substr);
std::string str_tolower(std::string str);
std::string extract_line(std::string & str, size_t pos = 0, std::string delim = "\n");
diff --git a/srcs/webserv/cgi_script.cpp b/srcs/webserv/cgi_script.cpp
index 8949a77..815218e 100644
--- a/srcs/webserv/cgi_script.cpp
+++ b/srcs/webserv/cgi_script.cpp
@@ -20,7 +20,7 @@ bool Webserv::_is_cgi(Client *client, std::string path)
continue;
if (file_type == IS_FILE)
{
- file_mode = ::eval_file_mode( script_path, X_OK );
+ file_mode = ::eval_file_access( script_path, X_OK );
if (!file_mode)
return true;
}
diff --git a/srcs/webserv/method_delete.cpp b/srcs/webserv/method_delete.cpp
index 16c7d94..248471e 100644
--- a/srcs/webserv/method_delete.cpp
+++ b/srcs/webserv/method_delete.cpp
@@ -12,7 +12,7 @@ void Webserv::_delete(Client *client, const std::string &path)
void Webserv::_delete_file(Client *client, const std::string &path)
{
std::cout << "_delete_file()\n";
- client->status = ::eval_file_mode(path, W_OK);
+ client->status = ::eval_file_access(path, W_OK);
if (client->status)
return;
diff --git a/srcs/webserv/method_get.cpp b/srcs/webserv/method_get.cpp
index 4649dd2..e84a7bb 100644
--- a/srcs/webserv/method_get.cpp
+++ b/srcs/webserv/method_get.cpp
@@ -55,7 +55,7 @@ void Webserv::_get_file(Client *client, const std::string &path)
std::cout << "_get_file()\n";
- client->status = ::eval_file_mode(path, R_OK);
+ client->status = ::eval_file_access(path, R_OK);
if (client->status)
return;
diff --git a/srcs/webserv/method_post.cpp b/srcs/webserv/method_post.cpp
index ce7309b..8611770 100644
--- a/srcs/webserv/method_post.cpp
+++ b/srcs/webserv/method_post.cpp
@@ -38,7 +38,7 @@ void Webserv::_upload_files(Client *client)
size_t pos;
bool file_existed = false;
- client->status = ::eval_file_mode(client->assigned_location->upload_dir, W_OK);
+ client->status = ::eval_file_access(client->assigned_location->upload_dir, W_OK);
if (client->status)
return;
diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp
index 6d2bcc6..627a868 100644
--- a/srcs/webserv/response.cpp
+++ b/srcs/webserv/response.cpp
@@ -47,7 +47,7 @@ int Webserv::_send_response(Client *client)
if (client->status >= 400)
_error_html_response(client);
-/*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output + headers]:" RESET "\n"; ::print_special(client->response); std::cout << "\n" B_PURPLE "-----------" RESET "\n\n";
+// /*DEBUG*/ std::cout << "\n" B_PURPLE "[response + output + headers]:" RESET "\n"; ::print_special(client->response); std::cout << "\n" B_PURPLE "-----------" RESET "\n\n";
std::cerr << "client->response.size() = " << client->response.size() << "\n"; // DEBUG
ret = ::send(client->get_cl_fd(), client->response.c_str(), client->response.size(), 0);
@@ -131,14 +131,20 @@ void Webserv::_insert_status_line(Client *client)
void Webserv::_error_html_response(Client *client)
{
- if (!client->assigned_server || client->assigned_server->error_pages[client->status].empty())
+ std::cout << "_error_html_response()\n";
+
+ if (client->assigned_server
+ && !client->assigned_server->error_pages[client->status].empty()
+ && ::eval_file_access(client->assigned_server->error_pages[client->status], R_OK) == 0 )
+ {
+ _get_file(client, client->assigned_server->error_pages[client->status]);
+ }
+ else
{
std::string html_page = HTML_ERROR;
::replace_all_substr(html_page, STATUS_PLACEHOLDER, _http_status[client->status]);
_append_body(client, html_page, "html");
}
- else
- _get_file(client, client->assigned_server->error_pages[client->status]);
}
void Webserv::_append_body(Client *client, const std::string &body, const std::string &file_extension)
diff --git a/www/error_pages/error_404.html b/www/error_pages/error_404.html
index fa18a2b..114a656 100644
--- a/www/error_pages/error_404.html
+++ b/www/error_pages/error_404.html
@@ -4,8 +4,8 @@
404 Not Found
- Check it UP 404 Not Found
+ Custom 404 Not Found
- Le Webserv/0.1
+ (° ͜ʖ °) Grosse personnalisation