Skip to content

Commit

Permalink
Merge pull request #40 from samih713/CGI-Parser
Browse files Browse the repository at this point in the history
Cgi parser
  • Loading branch information
Taanviir authored Apr 15, 2024
2 parents 2f6f321 + 996092c commit 8f0a202
Show file tree
Hide file tree
Showing 12 changed files with 488 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/compile-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches:
- main
- server/*
- conf-parser
- CGI-Parser
- handle_connection
- parsing

Expand Down
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ TESTS_DIR:= .tests
PARSER_DIR:= $(SRCS_DIR)/parser
HTTP_DIR:= $(SRCS_DIR)/http
SERVER_DIR:= $(SRCS_DIR)/server
CGI_DIR:= $(SRCS_DIR)/CGI

### EXECUTABLE ###
NAME:= webserv

### MODULES & INCLUDES ###
MODULES:= $(PARSER_DIR) $(HTTP_DIR) $(SERVER_DIR)
MODULES:= $(PARSER_DIR) $(HTTP_DIR) $(SERVER_DIR) $(CGI_DIR)
INCLUDES:= -I./includes/ $(patsubst %,-I./%,$(MODULES))

### SOURCES ###
SRCS:= $(SRCS_DIR)/main.cpp

### OBJECTS & SUBDIRS ###
include $(patsubst %,%/module.mk,$(MODULES))
OBJS += $(patsubst $(SRCS_DIR)%.cpp,$(OBJS_DIR)/%.o,$(SRCS))
OBJS += $(patsubst $(SRCS_DIR)%.cpp,$(OBJS_DIR)%.o,$(SRCS))
SUB_DIRS:= $(patsubst $(SRCS_DIR)%,$(OBJS_DIR)%,$(shell find $(SRCS_DIR) -type d))

all: $(NAME)
Expand Down Expand Up @@ -70,8 +71,8 @@ clean:
$(RM) $(OBJS_DIR); \
echo "$(RED)[ DELETE ]$(RESET) Removed object files."; \
fi
@if [ -f $(TEST_PARSER) ] || [ -f $(TEST_HTTP) ] || [ -f $(TEST_SOCKET) ]; then \
$(RM) $(TEST_PARSER) $(TEST_HTTP) $(TEST_SOCKET); \
@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."; \
fi

Expand All @@ -98,6 +99,10 @@ test_http:
# @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SOCKET_SRCS) $(TEST_SOCKET_SRC) -o $(TEST_SOCKET)
# @echo "$(BLUE)[ TEST ]$(RESET) SOCKET ready for testing."

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."

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

.PHONY: clean fclean all re debug run test_parser test_http test_socket
.PHONY: clean fclean all re debug run test_parser test_http test_socket test_cgi
2 changes: 2 additions & 0 deletions sources/CGI/.tests/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/python3
print("hello worled")
2 changes: 2 additions & 0 deletions sources/CGI/.tests/file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
echo "Hello, World Bash!"
96 changes: 96 additions & 0 deletions sources/CGI/.tests/test_cgi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

// #include <iostream>
// # include <stdio.h>
// # include <unistd.h>
// # include <fcntl.h>
// # include <signal.h>
// # include <sys/stat.h>
// # include <sys/wait.h>
// # include <stdexcept>
#include "../Cgi.hpp"


//using namespace webserv::http;


// int main(int ac, char **av, char **env)
// {
// (void)ac;
// //(void)av;
// int fd[2];
// int id;

// pipe(fd);
// id = fork();
// if (id == 0)
// {
// //dup2(fd[0], STDIN_FILENO);
// close(fd[0]);
// close(fd[1]);
// if (execve("/bin/ls", ++av, env))
// write(2, "error\n", 6);
// }
// close(fd[0]);
// close(fd[1]);

// return (0);
// }



int main(int argc, char** argv, char** envp)
{
(void)argc;
(void)argv;
(void)envp;
vsp headers;

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

//const std::string outputFile;

try
{
Request request(sample_request_cgi);
// headers = request.get_headers();
// cout<< endl << request.get_resource()<< endl;

// // Print headers
// cout << "Headers:" << endl;
// for (vsp::iterator it = headers.begin();
// it != headers.end(); ++it)
// {
// cout << it->first << ": " << it->second << endl;
// }

Cgi cgi(request);

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

// const char *pythonScriptPath = "/Users/hashim/Desktop/42curses/webserv/CGI/tester/file.sh";
// const char *pythonInterpreterPath = "/Users/hashim/Desktop/42curses/webserv/CGI/tester/file.sh";

// // 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;
// return 1;
// }

// // Create an instance of the CGI class
// char* scriptArguments[] = { const_cast<char*>(pythonScriptPath), nullptr };
// Cgi cgi(const_cast<char*>(pythonInterpreterPath), scriptArguments, envp);

// // Execute the Python script
// cgi.execute("std");
return 0;
}
Loading

0 comments on commit 8f0a202

Please sign in to comment.