Skip to content

Commit

Permalink
Merge branch 'main' into message-syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
samih713 committed Apr 27, 2024
2 parents 08153cb + eaf89c7 commit 8cab6e9
Show file tree
Hide file tree
Showing 51 changed files with 889 additions and 313 deletions.
20 changes: 12 additions & 8 deletions .colors.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
RESET=`tput sgr0`

# Regular Colors
BLACK=`tput bold setaf 0` # Black
RED=`tput bold setaf 1` # Red
GREEN=`tput bold setaf 2` # Green
YELLOW=`tput bold setaf 3` # Yellow
BLUE=`tput bold setaf 4` # Blue
PURPLE=`tput bold setaf 5` # Purple
CYAN=`tput bold setaf 6` # Cyan
WHITE=`tput bold setaf 7` # White
BLACK=`tput setaf 0` # Black
RED=`tput setaf 1` # Red
GREEN=`tput setaf 2` # Green
YELLOW=`tput setaf 3` # Yellow
BLUE=`tput setaf 4` # Blue
MAGENTA=`tput setaf 5` # Magenta
CYAN=`tput setaf 6` # Cyan
WHITE=`tput setaf 7` # White

BOLD=`tput bold`

BOLD:=`tput bold`
7 changes: 1 addition & 6 deletions .github/workflows/compile-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: actions/checkout@v3

- name: Set up C++ environment
uses: aminya/setup-cpp@v1
uses: egor-tensin/setup-gcc@v1.3
with:
compiler: gcc

Expand All @@ -30,10 +30,5 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up C++ environment
uses: aminya/setup-cpp@v1
with:
compiler: gcc

- name: Build project
run: make
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.vscode/
*/objects/
objects
*/.cache/
*/compile_commands.json
compile_commands.json
Expand Down
2 changes: 2 additions & 0 deletions .resources/cgi-bin/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/python3
print("hello worled")
3 changes: 3 additions & 0 deletions .resources/cgi-bin/file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
echo "Hello, World Bash!"
sleep 20
2 changes: 2 additions & 0 deletions .resources/notes/CGI_ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://www.oreilly.com/library/view/cgi-programming-on/9781565921689/04_chapter-01.html //good aticla for CGi
http://www.mnuwer.dbasedeveloper.co.uk/dlearn/web/session01.htm
7 changes: 7 additions & 0 deletions .tests/curl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curl -X GET http://localhost:8080/cgi-bin/file.sh?name=hashim \
-H "Host: Linode.com" \
-H "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) Gecko/20091102 Firefox/3.5.5" \
-H "Accept-Encoding: gzip,deflate" \
-H "Accept-Charset: ISO-8859-1,utf-8" \
-H "Cache-Control: no-cache" \
-d $'body\r\n'
21 changes: 21 additions & 0 deletions .tests/test.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
events {
}
http {
# server {
# listen 8080;
# client_max_body_size 1M;
# server_name localhost;

# root ./.resources/sample_pages;
# index index.html index.htm;

# location {
# root ./.resources/sample_pages;
# }

# # error_page 404 /404.html;
# # location = /404.html {
# # root ./.resources/sample_pages;
# # }
# }
}
33 changes: 16 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ all: $(NAME)

$(NAME): $(OBJS)
@$(CXX) $(CXXFLAGS) $(DEPFLAGS) $(INCLUDES) $(OBJS) -o $@
@echo "$(YELLOW)[ EXECUTABLE ]$(RESET) $(NAME) is ready."
@echo "$(YELLOW)$(BOLD)[ EXECUTABLE ]$(RESET) $(NAME) is ready."

$(OBJS_DIR)/%.o: $(SRCS_DIR)/%.cpp | $(OBJS_DIR)
@$(CXX) $(CXXFLAGS) $(DEPFLAGS) $(INCLUDES) -c $< -o $@
@echo "$(GREEN)[ COMPILE ]$(RESET) $<."
@echo "$(GREEN)$(BOLD)[ COMPILE ]$(RESET) $<."

$(OBJS_DIR):
@mkdir -p $@ $(patsubst $(SRCS_DIR)%,$(OBJS_DIR)%,$(SUB_DIRS))
Expand All @@ -64,56 +64,55 @@ run: re
debug: export CXXFLAGS += $(DEBUGFLAGS)
debug: fclean
@$(MAKE) -se all
@echo "$(MAGENTA)[ DEBUG ]$(RESET) $(NAME) is ready for debugging."
@echo "$(MAGENTA)$(BOLD)[ DEBUG ]$(RESET) $(NAME) is ready for debugging."

clean:
@if [ -d $(OBJS_DIR) ]; then \
$(RM) $(OBJS_DIR); \
echo "$(RED)[ DELETE ]$(RESET) Removed object files."; \
echo "$(RED)$(BOLD)[ DELETE ]$(RESET) Removed object files."; \
fi
@if [ -f $(TEST_PARSER) ] || [ -f $(TEST_HTTP) ] || [ -f $(TEST_SOCKET) ] || [ -f $(TEST_CGI) ]; then \
$(RM) $(TEST_PARSER) $(TEST_HTTP) $(TEST_SOCKET) $(TEST_CGI); \
echo "$(GREEN)[ DELETE ]$(RESET) Removed testers."; \
@if [ -f $(TEST_PARSER) ] || [ -f $(TEST_HTTP) ] || [ -f $(TEST_SOCKET) ] || [ -f $(TEST_CGI) ] || [ -f client ]; then \
$(RM) $(TEST_PARSER) $(TEST_HTTP) $(TEST_SOCKET) $(TEST_CGI) client; \
echo "$(RED)$(BOLD)[ DELETE ]$(RESET) Removed testers."; \
fi

fclean: clean
@if [ -f $(NAME) ]; then \
$(RM) $(NAME); \
echo "$(RED)[ DELETE ]$(RESET) Removed $(NAME)."; \
echo "$(RED)$(BOLD)[ DELETE ]$(RESET) Removed $(NAME)."; \
fi

re: fclean
@$(MAKE) -s all

test_parser:
@$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(PARSER_SRCS) $(TEST_PARSER_SRC) -o $(TEST_PARSER)
@echo "$(BLUE)[ TEST ]$(RESET) Parser ready for testing."
@cd ./$(PARSER_DIR)/$(TESTS_DIR) && ./run_tests.sh && cd -

test_http:
@$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(CGI_SRCS) $(HTTP_SRCS) $(TEST_HTTP_SRC) -o $(TEST_HTTP)
@echo "$(BLUE)[ TEST ]$(RESET) HTTP ready for testing."
@echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) HTTP ready for testing."

#! SOCKET_main.cpp has a compile error so this test has been commented out
# test_socket:
# @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SOCKET_SRCS) $(TEST_CLIENT_SRC) -o $(SERVER_DIR)/client @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SOCKET_SRCS) $(TEST_SOCKET_SRC) -o $(TEST_SOCKET)
# @echo "$(BLUE)[ TEST ]$(RESET) SOCKET ready for testing."
# @echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) SOCKET ready for testing."

client: $(TEST_CLIENT_SRC)
@$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(TEST_CLIENT_SRC) $(SERVER_DIR)/socket/Socket.cpp $(SERVER_DIR)/socket/TCPSocket.cpp -o $@
@echo "$(BLUE)[ TEST ]$(RESET) Client is ready."
@echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) Client is ready."

test_cgi:
@$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SANITIZE) $(HTTP_SRCS) $(CGI_SRCS) $(TEST_CGI_SRC) -o $(TEST_CGI)
@echo "$(BLUE)[ TEST ]$(RESET) CGI ready for testing."
@echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) CGI ready for testing."

format:
@echo "$(BLUE)[ FORMAT ]$(RESET) Formatting code..."
@echo "$(BLUE)$(BOLD)[ FORMAT ]$(RESET) Formatting code..."
@find ./$(SRCS_DIR) -name "*.cpp" -o -name "*.hpp" \
-exec clang-format -i {} +
@find ./includes -name "*.hpp" \
-exec clang-format -i {} +
@echo "$(BLUE)[ FORMAT ]$(RESET) Code has been formatted."
@echo "$(BLUE)$(BOLD)[ FORMAT ]$(RESET) Code has been formatted."

-include $(OBJS:.o=.d)

.PHONY: clean fclean all re debug run test_parser test_http test_socket test_cgi format
.PHONY: clean fclean all re debug run test_http test_socket test_cgi format
19 changes: 19 additions & 0 deletions configs/cgitest.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
http {
server {
listen 8080;
client_max_body_size 1M;
server_name localhost;

root ./.resources;
index index.html index.htm;

location / {
root ./.resources/sample_pages;
}

error_page 404 /404.html;
location = /404.html {
root ./.resources/sample_pages;
}
}
}
35 changes: 14 additions & 21 deletions includes/webserv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@
// [ ] different strategies
// [ ] testing (with a testing framework)

/* -------------------------------- WARNINGS -------------------------------- */

// PARSER
// [ ] maybe there's stuff in the global context that need to be parsed

/* --------------------------------- MACROS --------------------------------- */
#define ws_tostr(name) #name
#define ws_itoa(number) \
static_cast<const std::ostringstream&>((std::ostringstream() << std::dec << number)) \
.str()

/* -------------------------------- INCLUDES -------------------------------- */
#include "./debug.hpp"
#include <cerrno>
Expand Down Expand Up @@ -88,22 +77,26 @@ typedef map<string, string> HeaderMap;
// clang-format on
// socket_descriptor type
typedef int fd;
/* --------------------------------- MACROS --------------------------------- */
#define ws_tostr(name) #name
#define ws_itoa(number) \
static_cast<const ostringstream&>((ostringstream() << std::dec << number)).str()
/* ----------------------------- ERROR MESSAGES ----------------------------- */
#define THROW_EXCEPTION_WITH_INFO(msg) \
do { \
std::ostringstream oss; \
ostringstream oss; \
oss << __FILE__ << ":" << __LINE__ << ": " << R << "error: " << RE << (msg); \
throw std::runtime_error(oss.str()); \
throw runtime_error(oss.str()); \
} while (0)

static std::string ERR_NULL("Socket: null ptr error");
static std::string ERR_CREAT("Socket: creation failed");
static std::string ERR_BIND("Socket: binding failed");
static std::string ERR_NBIND("Socket: not bound to a port");
static std::string ERR_LIST("Socket: listen failed");
static std::string ERR_NLIST("Socket: not listening");
static std::string ERR_ACCP("Socket: accept failed");
static string ERR_NULL("Socket: null ptr error");
static string ERR_CREAT("Socket: creation failed");
static string ERR_BIND("Socket: binding failed");
static string ERR_NBIND("Socket: not bound to a port");
static string ERR_LIST("Socket: listen failed");
static string ERR_NLIST("Socket: not listening");
static string ERR_ACCP("Socket: accept failed");

static std::string ERR_MEMORY_ALLOC("Memory: allocation failed");
static string ERR_MEMORY_ALLOC("Memory: allocation failed");

#endif // WEBSERV_HPP
10 changes: 5 additions & 5 deletions sources/CGI/.tests/test_cgi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ int main(int argc, char** argv, char** envp)

// for (int i = 0; envp[i] != NULL; ++i)
// {
// std::cout << envp[i] << std::endl;
// cout << envp[i] << endl;
// }
// cout<< endl<<endl;

//const std::string outputFile;
//const string outputFile;

string res;

Expand All @@ -76,9 +76,9 @@ int main(int argc, char** argv, char** envp)

//cgi.execute("stds");
}
catch (std::runtime_error &e)
catch (runtime_error &e)
{
cerr << e.what() << std::endl;
cerr << e.what() << endl;
}

// const char *pythonScriptPath = "/Users/hashim/Desktop/42curses/webserv/CGI/tester/file.sh";
Expand All @@ -87,7 +87,7 @@ int main(int argc, char** argv, char** envp)
// // Check if the Python script exists
// if (access(pythonScriptPath, X_OK) == -1)
// {
// std::cerr << "Error: Python script not found or does not have execution permission." << std::endl;
// cerr << "Error: Python script not found or does not have execution permission." << endl;
// return 1;
// }

Expand Down
Loading

0 comments on commit 8cab6e9

Please sign in to comment.