From 757635c7c93fc9f61f11f4396bbfee74257a77ce Mon Sep 17 00:00:00 2001 From: 552020 Date: Fri, 17 May 2024 20:31:50 +0200 Subject: [PATCH 01/11] feat(new PHP website): WIP --- src/HTTPRequest.cpp | 2 +- var/www.php_site/database.txt | 0 var/www.php_site/index.html | 14 -------------- webserv_default.conf | 11 +++++++++++ 4 files changed, 12 insertions(+), 15 deletions(-) create mode 100644 var/www.php_site/database.txt delete mode 100644 var/www.php_site/index.html diff --git a/src/HTTPRequest.cpp b/src/HTTPRequest.cpp index 907d7131..9a1dd7ea 100644 --- a/src/HTTPRequest.cpp +++ b/src/HTTPRequest.cpp @@ -130,7 +130,7 @@ void HTTPRequest::setMethod(std::string method) void HTTPRequest::setRequestTarget(std::string requestTarget) { - if (requestTarget != "/" && requestTarget[requestTarget.size() - 1] == '/') + if (!requestTarget.empty() && requestTarget != "/" && requestTarget[requestTarget.size() - 1] == '/') requestTarget = requestTarget.substr(0, requestTarget.size() - 1); _requestTarget = requestTarget; } diff --git a/var/www.php_site/database.txt b/var/www.php_site/database.txt new file mode 100644 index 00000000..e69de29b diff --git a/var/www.php_site/index.html b/var/www.php_site/index.html deleted file mode 100644 index d85ea5bd..00000000 --- a/var/www.php_site/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - PHP - - -
- -

What is PHP?

-

PHP is a popular general-purpose scripting language that is especially suited to web development. It was created by Rasmus Lerdorf in 1994; PHP originally stood for Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

-
- - - diff --git a/webserv_default.conf b/webserv_default.conf index 1648b483..d4d5d8ab 100644 --- a/webserv_default.conf +++ b/webserv_default.conf @@ -24,4 +24,15 @@ server { allow_methods GET POST DELETE; autoindex off; root var/; +} + +server { + listen 8080; + server_name www.php_site; + allow_methods GET POST DELETE; + index index.php; + autoindex off; + root var/; + cgi_ext .php; + cgi_path /Users/stefano/.brew/bin/php; } \ No newline at end of file From b9db2b165ee200f05119d53faf451809a098ae1e Mon Sep 17 00:00:00 2001 From: 552020 Date: Fri, 17 May 2024 20:44:08 +0200 Subject: [PATCH 02/11] refactor: rename php site folder --- var/www.php_site.com/index.html | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 var/www.php_site.com/index.html diff --git a/var/www.php_site.com/index.html b/var/www.php_site.com/index.html deleted file mode 100644 index d85ea5bd..00000000 --- a/var/www.php_site.com/index.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - PHP - - -
- -

What is PHP?

-

PHP is a popular general-purpose scripting language that is especially suited to web development. It was created by Rasmus Lerdorf in 1994; PHP originally stood for Personal Home Page, but it now stands for the recursive initialism PHP: Hypertext Preprocessor.

-
- - - From a04c81980c1347193957802c66f3174b631c2f54 Mon Sep 17 00:00:00 2001 From: dantol29 Date: Sat, 18 May 2024 10:44:17 +0200 Subject: [PATCH 03/11] fix(Router): clean up mess in the Router --- src/Router.cpp | 5 ++-- src/Server.cpp | 18 ++++---------- src/StaticContentHandler.cpp | 41 +++++++++++++++++++------------- var/www.php_site.com/index.html | 42 +++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 32 deletions(-) create mode 100644 var/www.php_site.com/index.html diff --git a/src/Router.cpp b/src/Router.cpp index b3cfe93a..d8274e1f 100644 --- a/src/Router.cpp +++ b/src/Router.cpp @@ -68,11 +68,12 @@ void Router::routeRequest(HTTPRequest &request, HTTPResponse &response) root = "var/"; request.setRoot(root); std::string path = root + request.getSingleHeader("host").second; - std::string requestTarget = request.getRequestTarget(); - std::cout << YELLOW << "requestTarget: " << requestTarget << RESET << std::endl; + std::cout << YELLOW << "requestTarget: " << request.getRequestTarget() << RESET << std::endl; adaptPathForFirefox(request); + std::cout << YELLOW << "requestTarget after firefox: " << request.getRequestTarget() << RESET << std::endl; + std::cout << GREEN << "Routing request to path: " << request.getPath() << RESET << std::endl; // std::cout << request << std::endl; diff --git a/src/Server.cpp b/src/Server.cpp index e88b55f6..9907edd8 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -265,9 +265,8 @@ void Server::writeToClient(Connection &conn, size_t &i, HTTPResponse &response) std::cout << "\033[1;36m" << "Entering writeToClient" << "\033[0m" << std::endl; - std::cout << response << std::endl; + //std::cout << response << std::endl; static int sendResponseCounter = 0; - bool isLastSend = false; size_t tmpBufferSize = SEND_BUFFER_SIZE; (void)i; @@ -282,24 +281,17 @@ void Server::writeToClient(Connection &conn, size_t &i, HTTPResponse &response) { tmpBufferSize = conn.getResponseString().size(); std::cout << GREEN << "Sending last part of the response" << RESET << std::endl; - isLastSend = true; + std::cout << GREEN << "sendResponseCounter: " << sendResponseCounter << RESET << std::endl; + conn.setHasFinishedSending(true); + conn.setCanBeClosed(true); } - std::cout << GREEN << "sendResponseCounter: " << sendResponseCounter << RESET << std::endl; int read = send(conn.getPollFd().fd, conn.getResponseString().c_str(), tmpBufferSize, 0); if (read == -1) - { perror("send"); - } - sendResponseCounter++; + ++sendResponseCounter; conn.setResponseSizeSent(conn.getResponseSizeSent() + tmpBufferSize); - if (isLastSend) - { - conn.setHasFinishedSending(true); - conn.setCanBeClosed(true); - } - conn.setResponseString(conn.getResponseString().substr(tmpBufferSize)); response.getBody().erase(0, SEND_BUFFER_SIZE); } diff --git a/src/StaticContentHandler.cpp b/src/StaticContentHandler.cpp index e0bcf52e..aa97ee87 100644 --- a/src/StaticContentHandler.cpp +++ b/src/StaticContentHandler.cpp @@ -38,34 +38,41 @@ std::string getMimeType(const std::string &filePath) return "application/octet-stream"; // Default binary type } -static bool isDirectory(const std::string &path) -{ - struct stat statbuf; - if (stat(path.c_str(), &statbuf) != 0) - return false; - return S_ISDIR(statbuf.st_mode); -} +// static bool isDirectory(const std::string &path) +// { +// struct stat statbuf; +// if (stat(path.c_str(), &statbuf) != 0) +// return false; +// return S_ISDIR(statbuf.st_mode); +// } void StaticContentHandler::handleRequest(const HTTPRequest &request, HTTPResponse &response) { - std::string requestTarget = request.getRequestTarget(); std::string webRoot = "var/www"; std::string host = request.getHost(); std::string path = request.getPath(); std::cout << "path: " << path << std::endl; - if (requestTarget == "/" || requestTarget == "") - requestTarget = "/index.html"; + + // this is totally wrong + // if (requestTarget == "/" || requestTarget == "") + // requestTarget = "/index.html"; + + // is this necessary? // if the last character of the path is a / and the first character of the request target is a /, we remove the // first character of the request target - std::cout << "requestTarget: " << requestTarget << std::endl; - if (path[path.length() - 1] == '/' && requestTarget[0] == '/') - requestTarget = requestTarget.substr(1); + // std::cout << "requestTarget: " << request.getRequestTarget() << std::endl; + // if (path[path.length() - 1] == '/' && request.getRequestTarget()[0] == '/') + // request.setRequestTarget(request.getRequestTarget().substr(1)); + // TODO: consider streaming the file instead of loading it all in memory for large files - if (isDirectory(path)) - { - path += "index.html"; - } + + // this is totally wrong + // if (isDirectory(path)) + // { + // path += "index.html"; + // } + std::ifstream file(path.c_str()); if (!file) // TODO: this is wrong, it should return a false bool { diff --git a/var/www.php_site.com/index.html b/var/www.php_site.com/index.html new file mode 100644 index 00000000..d70d7631 --- /dev/null +++ b/var/www.php_site.com/index.html @@ -0,0 +1,42 @@ + + + + + + Welcome to Our Web Service + + +
+
+

Welcome to Our Web Service

+
+
+ +
+
+

About This Server

+

Welcome to our multi-faceted web server. Here, you can find a variety of web applications and sites ranging from development tools to language-specific sites. Whether you're here to explore our Perl, PHP, or Python projects, or to delve into our development tools, there's something for everyone.

+

Please use the navigation above to explore our sites.

+
+
+
+ + +
+ +
+

Upload Multiple Files

+
+ +
+ +
+ + From 2e7ae7731a4c20a8ec7a9b0e5e97e7fa5d43ee22 Mon Sep 17 00:00:00 2001 From: 552020 Date: Sat, 18 May 2024 14:55:15 +0200 Subject: [PATCH 04/11] chore(php): add utils for php development --- experiments/php/.vscode/settings.json | 3 +++ experiments/php/index.html | 9 +++++++ ngnix/Dockerfile | 13 +++++++--- ngnix/nginx.conf | 36 +++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 experiments/php/.vscode/settings.json create mode 100644 experiments/php/index.html create mode 100644 ngnix/nginx.conf diff --git a/experiments/php/.vscode/settings.json b/experiments/php/.vscode/settings.json new file mode 100644 index 00000000..c74b0248 --- /dev/null +++ b/experiments/php/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "phpServer.phpPath": "/Users/stefano/.brew/bin/php" +} diff --git a/experiments/php/index.html b/experiments/php/index.html new file mode 100644 index 00000000..a3436c08 --- /dev/null +++ b/experiments/php/index.html @@ -0,0 +1,9 @@ + + + + HTML Test File + + +

It works!

+ + diff --git a/ngnix/Dockerfile b/ngnix/Dockerfile index 01602bc0..79ec66bc 100644 --- a/ngnix/Dockerfile +++ b/ngnix/Dockerfile @@ -1,9 +1,13 @@ # Use a base image FROM ubuntu:latest +# Set environment variables to avoid interactive prompts +ENV DEBIAN_FRONTEND=noninteractive +ENV TZ=Etc/UTC + # Install dependencies RUN apt-get update && \ - apt-get install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev + apt-get install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev php-fpm php-mysql tzdata # Copy the Nginx source code from your repository to the image COPY nginx-1.24.0 /usr/src/nginx-1.24.0 @@ -24,5 +28,8 @@ RUN ln -sf /dev/stdout /usr/local/nginx/logs/access.log \ # Expose port 80 EXPOSE 80 -# Start Nginx in the foreground -CMD ["nginx", "-g", "daemon off;"] +# Copy Nginx config file +COPY nginx.conf /usr/local/nginx/conf/nginx.conf + +# Start Nginx and PHP-FPM +CMD service php7.4-fpm start && nginx -g 'daemon off;' diff --git a/ngnix/nginx.conf b/ngnix/nginx.conf new file mode 100644 index 00000000..51278f0a --- /dev/null +++ b/ngnix/nginx.conf @@ -0,0 +1,36 @@ +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + include mime.types; + default_type application/octet-stream; + + sendfile on; + keepalive_timeout 65; + + server { + listen 80; + server_name localhost; + + root /usr/share/nginx/html; + index index.php index.html index.htm; + + location / { + try_files $uri $uri/ =404; + } + + location ~ \.php$ { + include fastcgi_params; + fastcgi_pass 127.0.0.1:9000; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + } + + location ~ /\.ht { + deny all; + } + } +} From 66437ad8481304607f4e0b3402986294e7826ec2 Mon Sep 17 00:00:00 2001 From: dantol29 Date: Sun, 19 May 2024 10:33:59 +0200 Subject: [PATCH 05/11] fix(config): change error message and development.conf --- development.conf | 11 +++++++++++ src/Config.cpp | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 development.conf diff --git a/development.conf b/development.conf new file mode 100644 index 00000000..2aa7859a --- /dev/null +++ b/development.conf @@ -0,0 +1,11 @@ +server { + listen 8080; + server_name www.development_site; + allow_methods GET POST DELETE; + root /var/; + cgi_ext .cgi; + location /cgi-bin/ { + cgi_ext .cgi .py; + autoindex off; + } +} diff --git a/src/Config.cpp b/src/Config.cpp index c4accced..1fe412a7 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -206,7 +206,7 @@ bool Config::parse(std::ifstream &config) if (isLocation(line)) // start of location block parseLocation(line, config); else if (!saveDirective(line)) // variables outside of location - return (setError("Config file: Syntax error (invalid var in the root)")); + return (setError("Config file: Syntax error (invalid directive)")); } _tmpServerBlock.deleteData(); // delete saved data } From 6f61ccfe81d1a734bff0c14de13994d9cfbdedb3 Mon Sep 17 00:00:00 2001 From: Stefano Lombardo Date: Tue, 21 May 2024 06:21:21 +0200 Subject: [PATCH 06/11] build(fix): compilation error --- src/StaticContentHandler.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/StaticContentHandler.cpp b/src/StaticContentHandler.cpp index 2f54a63f..359e2140 100644 --- a/src/StaticContentHandler.cpp +++ b/src/StaticContentHandler.cpp @@ -61,6 +61,7 @@ void StaticContentHandler::handleRequest(HTTPRequest &request, HTTPResponse &res // is this necessary? // if the last character of the path is a / and the first character of the request target is a /, we remove the // first character of the request target + std::string requestTarget = path; if (path[path.length() - 1] == '/' && requestTarget[0] == '/') requestTarget = requestTarget.substr(1); From 0208c3599001f46470f0fe2ab549c482290f1202 Mon Sep 17 00:00:00 2001 From: Stefano Lombardo Date: Tue, 21 May 2024 06:57:02 +0200 Subject: [PATCH 07/11] debuh(cgi_path): add prints --- experiments/issues.csv | 40 +++++++++++++++---------------- src/CGIHandler.cpp | 23 ++++++++++++++++++ src/MetaVariables.cpp | 2 ++ var/www.php_site.com/index.html | 42 --------------------------------- 4 files changed, 45 insertions(+), 62 deletions(-) delete mode 100644 var/www.php_site.com/index.html diff --git a/experiments/issues.csv b/experiments/issues.csv index 1ae038b5..ceb9bf8d 100644 --- a/experiments/issues.csv +++ b/experiments/issues.csv @@ -1,21 +1,21 @@ Summary,Issue key,Issue id,Issue Type,Status,Project key,Project name,Creator,Created,Updated -Fix the issue,Issue11,11,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue12,12,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue13,13,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue14,14,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue15,15,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue16,16,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue17,17,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue18,18,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue19,19,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue20,20,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue21,21,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue22,22,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue23,23,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue24,24,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue25,25,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue26,26,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue27,27,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue28,28,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue29,29,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 -Fix the issue,Issue30,30,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 \ No newline at end of file +Fix the issue,Issue31,31,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue32,32,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue33,33,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue34,34,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue35,35,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue36,36,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue37,37,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue38,38,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue39,39,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue40,40,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue41,41,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue42,42,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue43,43,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue44,44,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue45,45,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue46,46,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue47,47,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue48,48,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue49,49,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 +Fix the issue,Issue50,50,Bug,Backlog,GH123,ExampleProject,slombard,2024-05-10,2024-05-10 diff --git a/src/CGIHandler.cpp b/src/CGIHandler.cpp index 03373ea6..576f89be 100644 --- a/src/CGIHandler.cpp +++ b/src/CGIHandler.cpp @@ -53,6 +53,7 @@ void CGIHandler::handleRequest(HTTPRequest &request, HTTPResponse &response) std::vector CGIHandler::createArgvForExecve(const MetaVariables &env) { + std ::cout << "------------------createArgvForExecve-------------------" << std::endl; std::vector argv; std::string scriptName = env.getVar("SCRIPT_NAME"); std::cout << "createArgvForExecve: scriptName: " << scriptName << std::endl; @@ -71,6 +72,7 @@ std::vector CGIHandler::createArgvForExecve(const MetaVariables &en { argv.push_back(scriptPath); } + std::cout << "---------- Exiting createArgvForExecve ------------------" << std::endl; return argv; } @@ -143,6 +145,7 @@ void handleTimeout(int sig) std::string CGIHandler::executeCGI(const MetaVariables &env) { + std::cout << "------------------executeCGI-------------------" << std::endl; std::string cgiOutput; std::vector argv = createArgvForExecve(env); std::vector envp = env.getForExecve(); @@ -178,6 +181,26 @@ std::string CGIHandler::executeCGI(const MetaVariables &env) perror("access"); _exit(EXIT_FAILURE); } + if (argvPointers[0] != NULL) + std::cerr << "argvPointers[0] " << argvPointers[0] << std::endl; + if (argvPointers[1] != NULL) + std::cerr << "argvPointers[1] " << argvPointers[1] << std::endl; + std::cerr << "Printing argvPointers" << std::endl; + for (size_t i = 0; i < argvPointers.size(); i++) + { + if (argvPointers[i] != NULL) + std::cout << argvPointers[i] << std::endl; + else + break; + } + std::cerr << "Printing envpPointers" << std::endl; + for (size_t i = 0; i < envpPointers.size(); i++) + { + if (envpPointers[i] != NULL) + std::cout << envpPointers[i] << std::endl; + else + break; + } execve(argvPointers[0], argvPointers.data(), envpPointers.data()); perror("execve"); diff --git a/src/MetaVariables.cpp b/src/MetaVariables.cpp index a927f169..7e8d7395 100644 --- a/src/MetaVariables.cpp +++ b/src/MetaVariables.cpp @@ -42,12 +42,14 @@ std::string MetaVariables::getVar(const std::string &key) const std::vector MetaVariables::getForExecve() const { + std::cout << "MetaVariables::getForExecve: " << std::endl; std::vector result; for (std::map::const_iterator it = metaVars.begin(); it != metaVars.end(); ++it) { std::string env = it->first + "=" + it->second; result.push_back(env); } + std::cout << "Exiting MetaVariables::getForExecve: " << std::endl; return result; } diff --git a/var/www.php_site.com/index.html b/var/www.php_site.com/index.html deleted file mode 100644 index d70d7631..00000000 --- a/var/www.php_site.com/index.html +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - Welcome to Our Web Service - - -
-
-

Welcome to Our Web Service

-
-
- -
-
-

About This Server

-

Welcome to our multi-faceted web server. Here, you can find a variety of web applications and sites ranging from development tools to language-specific sites. Whether you're here to explore our Perl, PHP, or Python projects, or to delve into our development tools, there's something for everyone.

-

Please use the navigation above to explore our sites.

-
-
-
- - -
- -
-

Upload Multiple Files

-
- -
- -
- - From 3eae362fb675d524ccbb391451b76378ab9fd25e Mon Sep 17 00:00:00 2001 From: Stefano Lombardo Date: Tue, 21 May 2024 06:59:23 +0200 Subject: [PATCH 08/11] chore(config): add config file for php --- config/php.conf | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 config/php.conf diff --git a/config/php.conf b/config/php.conf new file mode 100644 index 00000000..3508b251 --- /dev/null +++ b/config/php.conf @@ -0,0 +1,10 @@ +server { + listen 8080; + server_name www.php_site.com; + allow_methods GET POST DELETE; + autoindex off; + root /var/; + error_page 404 404.html; + cgi_ext .cgi .php; + cgi_path /usr/bin/php-cgi; +} \ No newline at end of file From 8de55898aa0aed76ffcb337ba43e7e2415f10bda Mon Sep 17 00:00:00 2001 From: Stefano Lombardo Date: Tue, 21 May 2024 07:51:20 +0200 Subject: [PATCH 09/11] feat(php site): add php pages to the php website --- .gitignore | 1 + var/www.php_site.com/index.php | 94 ++++++++++++++++++++++++++++++++ var/www.php_site.com/logout.php | 6 ++ var/www.php_site.com/welcome.php | 0 4 files changed, 101 insertions(+) create mode 100755 var/www.php_site.com/index.php create mode 100644 var/www.php_site.com/logout.php create mode 100644 var/www.php_site.com/welcome.php diff --git a/.gitignore b/.gitignore index d826b3d1..120d6ff8 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ !**/*.jpg !**/*.yml !**/*.csv +!**/*.php # Specific files and directories to include !Makefile diff --git a/var/www.php_site.com/index.php b/var/www.php_site.com/index.php new file mode 100755 index 00000000..b56f10eb --- /dev/null +++ b/var/www.php_site.com/index.php @@ -0,0 +1,94 @@ +#!/usr/bin/php + + + + + + + Sign Up and Sign In + + +

Sign Up

+ ' . $error . '

'; ?> + ' . $message . '

'; ?> +
+ + +
+ + +
+ +
+ +

Sign In

+
+ + +
+ + +
+ +
+ + diff --git a/var/www.php_site.com/logout.php b/var/www.php_site.com/logout.php new file mode 100644 index 00000000..7e7a48f0 --- /dev/null +++ b/var/www.php_site.com/logout.php @@ -0,0 +1,6 @@ + Date: Thu, 23 May 2024 00:08:37 +0200 Subject: [PATCH 10/11] chore(config): move php config to conf dir --- {config => conf}/php.conf | 0 config/webserv_default.conf | 27 --------------------------- secretus | 2 +- 3 files changed, 1 insertion(+), 28 deletions(-) rename {config => conf}/php.conf (100%) delete mode 100644 config/webserv_default.conf diff --git a/config/php.conf b/conf/php.conf similarity index 100% rename from config/php.conf rename to conf/php.conf diff --git a/config/webserv_default.conf b/config/webserv_default.conf deleted file mode 100644 index a139ea13..00000000 --- a/config/webserv_default.conf +++ /dev/null @@ -1,27 +0,0 @@ -server { - listen 8080; - server_name localho:8080 localhost:8080 www.localhost:8080; - allow_methods GET POST DELETE; - autoindex off; - root /var/; - error_page 404 404.html; - cgi_ext .cgi; - - location /admin { - return http://google.com; - index admin.html; - allow_methods GET POST; - } - location /cgi-bin/ { - cgi_ext .cgi .py; - autoindex off; - } -} - -server { - listen 8080; - server_name www.example.com; - allow_methods GET POST DELETE; - autoindex off; - root var/; -} diff --git a/secretus b/secretus index 77c5dfce..40e6826c 160000 --- a/secretus +++ b/secretus @@ -1 +1 @@ -Subproject commit 77c5dfce82baa6652e7c2324d55f9da336a869d5 +Subproject commit 40e6826c693300ba0e651095e7fa37831f9bdfaf From 46a7b84a095e1e7e20ef28a2166538c349281fc4 Mon Sep 17 00:00:00 2001 From: Stefano Lombardo Date: Thu, 23 May 2024 01:09:11 +0200 Subject: [PATCH 11/11] chore(php site); continue development --- src/Parser.cpp | 11 +++-- src/Parser.hpp | 4 +- src/Server.cpp | 2 + var/www.php_site.com/docs/session_start.md | 30 ++++++++++++ var/www.php_site.com/server.php | 53 ++++++++++++++++++++++ 5 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 var/www.php_site.com/docs/session_start.md create mode 100755 var/www.php_site.com/server.php diff --git a/src/Parser.cpp b/src/Parser.cpp index 1868604c..3541611b 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -177,7 +177,7 @@ void Parser::parseHeaders(const char *request, HTTPRequest &req, HTTPResponse &r if (!hasCRLF(request, i, 0)) return (res.setStatusCode(400, "No CRLF after headers")); if (!hasMandatoryHeaders(req, res)) - return ; + return; _headersAreParsed = true; saveCokies(req); } @@ -223,7 +223,7 @@ void Parser::parseFileUpload(const std::string &request, HTTPRequest &req, HTTPR // ----------------UTILS---------------------------- -bool Parser::hasMandatoryHeaders(HTTPRequest &req, HTTPResponse& res) +bool Parser::hasMandatoryHeaders(HTTPRequest &req, HTTPResponse &res) { _isChunked = false; int isHost = 0; @@ -255,6 +255,8 @@ bool Parser::hasMandatoryHeaders(HTTPRequest &req, HTTPResponse& res) } else if (it->first == "content-type") { + // print content-type + std::cout << "content-type: " << it->second << std::endl; if (!isValidContentType(it->second)) return (res.setStatusCode(415, "Not supported content-type"), false); if (it->second.substr(0, 30) == "multipart/form-data; boundary=") @@ -624,9 +626,8 @@ bool Parser::isOrigForm(std::string &requestTarget, int &queryStart) bool Parser::isValidContentType(std::string type) { -if (type == "text/plain" || type == "text/html" || \ - type.substr(0, 30) == "multipart/form-data; boundary=" \ - || type == "application/octet-stream") + if (type == "text/plain" || type == "text/html" || type.substr(0, 30) == "multipart/form-data; boundary=" || + type == "application/octet-stream" || type == "application/x-www-form-urlencoded") return (true); return (false); } diff --git a/src/Parser.hpp b/src/Parser.hpp index 69de30f3..ee84e3d9 100644 --- a/src/Parser.hpp +++ b/src/Parser.hpp @@ -50,7 +50,7 @@ class Parser // PARSING INTERNAL FUNC void parseRequestLine(const char *request, HTTPRequest &req, HTTPResponse &res); void parseHeaders(const char *request, HTTPRequest &req, HTTPResponse &res); - bool saveFile(const std::string& data, HTTPRequest &req); + bool saveFile(const std::string &data, HTTPRequest &req); // UTILS bool saveFileHeaders(const std::string &headers, HTTPRequest &req, unsigned int &i); int fileHeaderParametrs(const std::string &headers, struct File &file, unsigned int i); @@ -63,7 +63,7 @@ class Parser bool isOrigForm(std::string &requestTarget, int &queryStart); void skipRequestLine(const char *request, unsigned int &i); void skipHeader(const char *request, unsigned int &i); - bool hasMandatoryHeaders(HTTPRequest &req, HTTPResponse& res); + bool hasMandatoryHeaders(HTTPRequest &req, HTTPResponse &res); void saveCokies(HTTPRequest &req); std::string extractValue(std::string &variables, int &i); std::string extractKey(std::string &variables, int &i, int startPos); diff --git a/src/Server.cpp b/src/Server.cpp index baa70bf4..a5dd9d1e 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -287,6 +287,8 @@ void Server::handlePostRequest(Connection &conn, Parser &parser, HTTPRequest &re void Server::buildResponse(Connection &conn, size_t &i, HTTPRequest &request, HTTPResponse &response) { + std::cout << "Entering buildResponse" << std::endl; + std::cout << "Request: " << request << std::endl; (void)i; Debug::log("Entering buildResponse", Debug::NORMAL); Debug::log("Request method: " + request.getMethod(), Debug::NORMAL); diff --git a/var/www.php_site.com/docs/session_start.md b/var/www.php_site.com/docs/session_start.md new file mode 100644 index 00000000..3cc1878c --- /dev/null +++ b/var/www.php_site.com/docs/session_start.md @@ -0,0 +1,30 @@ + +# `session_start()` Function in PHP + +## Functionality of `session_start()` + +- **Session Initialization**: `session_start()` checks if a session already exists. If not, it creates a new session and generates a unique session identifier (session ID). If a session exists, it resumes that session. + +- **Session ID Handling**: The session ID is a unique number that identifies the session, usually stored in a cookie on the user's browser and sent with each request to the server. This ID maps to session data stored on the server. + +- **Session Data Storage**: Once a session is started, you can store and retrieve data from the `$_SESSION` superglobal array. PHP reads session data from the server's session storage and populates the `$_SESSION` array with this data. When the script ends, PHP saves any changes back to the session storage. + +## Common Uses of `session_start()` + +- **User Authentication**: Used in login scripts to store user-specific data (like user ID, roles) in the `$_SESSION` array, accessible across different pages. + +- **Preserving User Preferences**: Sessions can store user-selected themes or language preferences and apply them across the site. + +- **Flash Messages**: Store temporary messages in sessions to be displayed on web pages after interactions, then cleared from the session. + +## Best Practices + +- **Security**: Configure server to use secure cookies (`session.cookie_secure=1` and `session.cookie_httponly=1`). + +- **Session Regeneration**: Regenerate the session ID after a successful login with `session_regenerate_id()` to avoid session fixation attacks. + +- **Session Termination**: Properly manage logging out with `session_unset()` and `session_destroy()` to clear session data. + +- **Performance**: Consider the impact of starting sessions on every page. Start sessions selectively where needed to minimize performance overhead. + +By using `session_start()` properly, you can enhance user experience while maintaining security and efficiency in handling user sessions. \ No newline at end of file diff --git a/var/www.php_site.com/server.php b/var/www.php_site.com/server.php new file mode 100755 index 00000000..222fc54b --- /dev/null +++ b/var/www.php_site.com/server.php @@ -0,0 +1,53 @@ +#!/usr/bin/php + + + + + + Server Variables + + + + +

$_SERVER Variables

+ + + + + + $value) { + echo ""; + echo ""; // Cast key to string to avoid errors + if (is_array($value)) { + $value = implode(', ', $value); // Convert array to comma-separated string + } + echo ""; // Cast value to string to avoid errors + echo ""; + } + ?> +
KeyValue
" . htmlspecialchars((string) $key) . "" . htmlspecialchars((string) $value) . "
+ + + \ No newline at end of file