diff --git a/.gitignore b/.gitignore index 424f9507..65431f52 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ !**/*.csv !**/*.cfg !**/*.js +!**/*.php # Specific files and directories to include !Makefile diff --git a/conf/php.conf b/conf/php.conf new file mode 100644 index 00000000..3508b251 --- /dev/null +++ b/conf/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 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/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/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; + } + } +} diff --git a/secretus b/secretus index 77c5dfce..40e6826c 160000 --- a/secretus +++ b/secretus @@ -1 +1 @@ -Subproject commit 77c5dfce82baa6652e7c2324d55f9da336a869d5 +Subproject commit 40e6826c693300ba0e651095e7fa37831f9bdfaf diff --git a/src/CGIHandler.cpp b/src/CGIHandler.cpp index f4e8d0f7..06a5abbd 100644 --- a/src/CGIHandler.cpp +++ b/src/CGIHandler.cpp @@ -52,6 +52,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; @@ -70,6 +71,7 @@ std::vector CGIHandler::createArgvForExecve(const MetaVariables &en { argv.push_back(scriptPath); } + std::cout << "---------- Exiting createArgvForExecve ------------------" << std::endl; return argv; } diff --git a/src/Config.cpp b/src/Config.cpp index 123fe3e7..6d33c8df 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 } 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/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/Router.cpp b/src/Router.cpp index bead8656..780c4cda 100644 --- a/src/Router.cpp +++ b/src/Router.cpp @@ -64,11 +64,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 e4bc5c84..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); @@ -419,7 +421,6 @@ 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; static int sendResponseCounter = 0; - bool isLastSend = false; size_t tmpBufferSize = SEND_BUFFER_SIZE; (void)i; @@ -434,24 +435,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 097f98bd..359e2140 100644 --- a/src/StaticContentHandler.cpp +++ b/src/StaticContentHandler.cpp @@ -38,36 +38,43 @@ 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(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::string requestTarget = path; if (path[path.length() - 1] == '/' && requestTarget[0] == '/') requestTarget = requestTarget.substr(1); request.setRequestTarget(requestTarget); std::cout << "requestTarget: " << request.getRequestTarget() << std::endl; // 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/database.txt b/var/www.php_site.com/database.txt new file mode 100644 index 00000000..e69de29b 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/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.

-
- - - 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 @@ + + + + + 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 diff --git a/var/www.php_site.com/welcome.php b/var/www.php_site.com/welcome.php new file mode 100644 index 00000000..e69de29b