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 += ""; + + 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