From 036256522a5baafcd31f52280212665f534001b5 Mon Sep 17 00:00:00 2001
From: hugogogo
Date: Mon, 15 Aug 2022 15:28:19 +0200
Subject: [PATCH 1/3] 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/3] 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/3] 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;
}