Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully fledged PHP website - WIP #368

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
!**/*.csv
!**/*.cfg
!**/*.js
!**/*.php

# Specific files and directories to include
!Makefile
Expand Down
10 changes: 10 additions & 0 deletions conf/php.conf
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions development.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
40 changes: 20 additions & 20 deletions experiments/issues.csv
Original file line number Diff line number Diff line change
@@ -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
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
3 changes: 3 additions & 0 deletions experiments/php/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"phpServer.phpPath": "/Users/stefano/.brew/bin/php"
}
9 changes: 9 additions & 0 deletions experiments/php/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>HTML Test File</title>
</head>
<body>
<h1>It works!</h1>
</body>
</html>
13 changes: 10 additions & 3 deletions ngnix/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;'
36 changes: 36 additions & 0 deletions ngnix/nginx.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion secretus
2 changes: 2 additions & 0 deletions src/CGIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void CGIHandler::handleRequest(HTTPRequest &request, HTTPResponse &response)

std::vector<std::string> CGIHandler::createArgvForExecve(const MetaVariables &env)
{
std ::cout << "------------------createArgvForExecve-------------------" << std::endl;
std::vector<std::string> argv;
std::string scriptName = env.getVar("SCRIPT_NAME");
std::cout << "createArgvForExecve: scriptName: " << scriptName << std::endl;
Expand All @@ -70,6 +71,7 @@ std::vector<std::string> CGIHandler::createArgvForExecve(const MetaVariables &en
{
argv.push_back(scriptPath);
}
std::cout << "---------- Exiting createArgvForExecve ------------------" << std::endl;
return argv;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions src/MetaVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ std::string MetaVariables::getVar(const std::string &key) const

std::vector<std::string> MetaVariables::getForExecve() const
{
std::cout << "MetaVariables::getForExecve: " << std::endl;
std::vector<std::string> result;
for (std::map<std::string, std::string>::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;
}

Expand Down
11 changes: 6 additions & 5 deletions src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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=")
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 6 additions & 12 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
35 changes: 21 additions & 14 deletions src/StaticContentHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Empty file.
Loading
Loading