makefile for cgi_scripts

This commit is contained in:
hugogogo
2022-08-15 15:28:19 +02:00
parent a284e400c1
commit 036256522a
7 changed files with 232 additions and 140 deletions

View File

@@ -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)

86
srcs/cgi-bin/Makefile Normal file
View File

@@ -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

View File

@@ -1,91 +1,20 @@
# include <iostream>
# include <string>
# include <sstream>
# include <vector>
# include <stdlib.h> // 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<std::string>
split(const std::string & input, std::string delim, char ctrim = '\0')
{
std::vector<std::string> 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<std::string> split_str;
std::vector<std::string> sub_split_str;
std::vector<std::string>::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 = "\
<!DOCTYPE html>\
<html>\
<head>\
<title>CGI</title>\
</head>\
<body>\
<h2>cgi</h2>\
";
http_body += "<h3>";
tmp = getenv("REQUEST_METHOD");
if (tmp != NULL)
http_body += tmp;
else
http_body = "method not foud";
http_body += "</h3>";
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 += "</p>";
}
http_body += "\
</body>\
</html>\
";
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;

View File

@@ -1,65 +1,14 @@
# include <iostream>
# include <string>
# include <sstream>
# include <vector>
# include <stdlib.h> // 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<std::string>
split(const std::string & input, std::string delim, char ctrim = '\0')
{
std::vector<std::string> 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<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;
size_t pos;
std::cout << "Content-Type: text/html; charset=UTF-8" << CRLF CRLF;

View File

@@ -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<std::string>
split(const std::string & input, std::string delim, char ctrim)
{
std::vector<std::string> 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;
}

View File

@@ -0,0 +1,40 @@
#ifndef CGI_UTILS_HPP
# define CGI_UTILS_HPP
# include <iostream>
# include <string>
# include <sstream>
# include <vector>
# include <stdlib.h> // getenv
# define CR "\r"
# define LF "\n"
# define CRLF CR LF
# define CRLF_SIZE 2
# define NPOS std::string::npos
# define HTML_BODY_TOP "<!DOCTYPE html>"\
"<html>"\
" <head>"\
" <title>CGI</title>"\
" </head>"\
" <body>"\
" <h2>cgi</h2>"
# define HTML_BODY_BOTTOM " </body>"\
"</html>"
std::string
trim(std::string str, char del);
std::vector<std::string>
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