Skip to content

Commit

Permalink
Parser now accepts CGI
Browse files Browse the repository at this point in the history
  • Loading branch information
ratavare committed May 22, 2024
1 parent 1fcb763 commit 92d9066
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 28 deletions.
31 changes: 20 additions & 11 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ server:
error_pages:
403: error/403.html
400: error/400.html
cgi:
.py: /usr/bin/python3
.php: /usr/bin/php
max_cbsize: 2000m # to do: conversion and mapping
max_conn: 10
route /main: # to do: HTTP redirect.
Expand All @@ -28,6 +31,12 @@ server:
root: /uploads
dir_listing: on
http_methods: GET POST DELETE
route /python:
root: /cgi-bin/py
index: index.py
route /php:
root: /cgi-bin/php
index: index.php

server:
listen:
Expand All @@ -47,14 +56,14 @@ server:
dir_listing: on
http_methods: GET

# server:
# listen:
# host: localhost
# port: 1234
# server_name: example2.com
# root: /home
# index: xedni.html
# error_pages:
# 400: error/400.html
# max_cbsize: 1m
# max_conn: 5
server:
listen:
host: localhost
port: 1234
server_name: example2.com
root: /home
index: xedni.html
error_pages:
400: error/400.html
max_cbsize: 1m
max_conn: 5
4 changes: 3 additions & 1 deletion inc/Lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ enum nodeType {
ROUTE,
METHOD,
DIR_LISTING,
REDIRECT
REDIRECT,
CGI,
CGI_PARAM
};

typedef struct token {
Expand Down
3 changes: 2 additions & 1 deletion inc/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ typedef struct s_route {
s_route();
std::string path;
std::string redir;
std::list<std::string> httpMethods;
std::list<std::string> httpMethods; // TODO: HTTP REDIRECT
std::list<std::string> index;
std::string rroot;
int dirListing;
Expand All @@ -22,6 +22,7 @@ typedef struct s_server {
std::list<std::string> httpMethods;
std::list<std::string> index;
std::list<std::pair<int, std::string> > errorPages;
std::list<std::pair<std::string, std::string> > cgi;
long long maxCBSize;
int maxConn;
int dirListing;
Expand Down
8 changes: 4 additions & 4 deletions srcs/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ int main(void)
{
Lexer *lex = new Lexer;
lex->tokenize("config.yml");
// Lexer::printTokens(lex->getTokens()); // * Print tokens.
Lexer::printTokens(lex->getTokens()); // * Print tokens.
Parser *par = new Parser;
par->parse(lex->getTokens());
delete lex;
// par->printServerNodes(par->getServerNodesIt()); // * Print server nodes.
WebServer server(par->getServerNodes());
server.listen();
par->printServerNodes(par->getServerNodesIt()); // * Print server nodes.
// WebServer server(par->getServerNodes());
// server.listen();
delete par;
}
catch (const std::exception& e)
Expand Down
6 changes: 4 additions & 2 deletions srcs/parser/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Lexer::Lexer() {
std::string tmp[] = {"server", "listen", "host", "port", "server_name",
"root", "index", "error_pages", "max_cbsize", "max_conn", "route", "http_methods",
"dir_listing", "redirect"};
for (int i = 0; i < 14; i++)
"dir_listing", "redirect", "cgi"};
for (int i = 0; i < 15; i++)
types.insert(std::make_pair(tmp[i], i));
}

Expand Down Expand Up @@ -64,6 +64,8 @@ void Lexer::trimIdent(std::string& content) {
int Lexer::identifyToken(token& token) {
if (!std::strncmp(token.content.c_str(), "route ", 6))
return 10;
if (token.content[0] == '.' && token.identLevel == 2)
return 15;
size_t i = token.content.find_first_of(":");
std::string tmp = token.content.substr(0, i);
for (std::map<std::string, int>::iterator it = types.begin();
Expand Down
49 changes: 42 additions & 7 deletions srcs/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,15 @@ bool Parser::directivesCase2() {
if (it == end)
return false;
// std::cout << "Entered directivesCase2 with: " << it->content << " | type: " << it->type << std::endl;
if (it->type != LISTEN && it->type != ERROR_PAGE_BLOCK)
return false;
int flag = it->type == LISTEN ? LISTEN : ERROR_PAGE_BLOCK;
if (it->type != LISTEN && it->type != ERROR_PAGE_BLOCK && it->type != CGI)
return false;
int flag = -1;
if (it->type == LISTEN)
flag = LISTEN;
else if (it->type == ERROR_PAGE_BLOCK)
flag = ERROR_PAGE_BLOCK;
else if (it->type == CGI)
flag = CGI;
it++;
if (!blockDirs(flag)) { // !!!!!!!!!
// std::cout << "DEBUG1" << std::endl;
Expand Down Expand Up @@ -203,9 +209,15 @@ bool Parser::directivesCase5() {
if (it == end)
return false;
// std::cout << "Entered directivesCase5 with: " << it->content << " | type: " << it->type << std::endl;
if (it->type != LISTEN && it->type != ERROR_PAGE_BLOCK)
return false;
int flag = it->type == LISTEN ? LISTEN : ERROR_PAGE_BLOCK;
if (it->type != LISTEN && it->type != ERROR_PAGE_BLOCK && it->type != CGI)
return false;
int flag = -1;
if (it->type == LISTEN)
flag = LISTEN;
else if (it->type == ERROR_PAGE_BLOCK)
flag = ERROR_PAGE_BLOCK;
else if (it->type == CGI)
flag = CGI;
it++;
if (!blockDirs(flag))
return false;
Expand Down Expand Up @@ -258,6 +270,9 @@ bool Parser::blockDirsCase1(int flag) {
if (flag == ERROR_PAGE_BLOCK)
if (!parameterLst(serverNodes.back().errorPages))
return false;
if (flag == CGI)
if (!parameterLst(serverNodes.back().cgi))
return false;
if (!blockDirs(flag)) {
std::list<token>::iterator tmp = it;
tmp = tmp == end ? getLastTokenIt(tokens) : --tmp;
Expand All @@ -283,6 +298,9 @@ bool Parser::blockDirsCase2(int flag) {
if (flag == ERROR_PAGE_BLOCK)
if (!parameterLst(serverNodes.back().errorPages))
return false;
if (flag == CGI)
if (!parameterLst(serverNodes.back().cgi))
return false;
return true;
}

Expand Down Expand Up @@ -369,7 +387,6 @@ bool Parser::parameterLstCase1<std::list<t_route> >(std::list<t_route>& containe

template <>
bool Parser::parameterLstCase1<std::list<std::pair<int, std::string> > >(std::list<std::pair<int, std::string> >& container) {
(void)container;
if (it == end)
return false;
// std::cout << "\033[32m" << "Entered parameterLstCase1 ERROR_PAGE with: " << it->content << " | type: " << it->type << "\033[0m" << std::endl;
Expand All @@ -382,3 +399,21 @@ bool Parser::parameterLstCase1<std::list<std::pair<int, std::string> > >(std::li
it++;
return true;
}

// [parameters] CGI

template <>
bool Parser::parameterLstCase1<std::list<std::pair<std::string, std::string> > >(std::list<std::pair<std::string, std::string> >& container) {
if (it == end)
return false;
// std::cout << "\033[32m" << "Entered parameterLstCase1 CGI with: " << it->content << " | type: " << it->type << "\033[0m" << std::endl;
size_t i = it->content.find_first_of(':');
if (i == std::string::npos)
return false;
std::string extension = it->content.substr(0, i);
if (container.back().first == extension)
return false;
container.push_back(std::make_pair(extension, getParam(*it)));
it++;
return true;
}
9 changes: 7 additions & 2 deletions srcs/parser/ParserUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ void Parser::printServerNodes(std::list<t_server>::iterator it) {
static int i;
if (i == 0)
std::cout << std::endl << "============Parser============" << std::endl;
std::cout << "Server: " << i++ << std::endl;
std::cout << "Server: " << i++ + 1 << std::endl;
std::cout << " Host: " << it->host << std::endl;
std::cout << " Port: " << it->port << std::endl;
std::cout << " Server name: " << it->serverName << std::endl;
Expand All @@ -23,6 +23,9 @@ void Parser::printServerNodes(std::list<t_server>::iterator it) {
std::cout << std::endl << " Error pages:" << std::endl;
for (std::list<std::pair<int, std::string> > ::iterator tmp = it->errorPages.begin(); tmp != it->errorPages.end(); tmp++)
std::cout << " "<< tmp->first << " " << tmp->second << std::endl;
std::cout << " CGI:" << std::endl;
for (std::list<std::pair<std::string, std::string> > ::iterator tmp = it->cgi.begin(); tmp != it->cgi.end(); tmp++)
std::cout << " "<< tmp->first << " " << tmp->second << std::endl;
for (std::list<t_route>::iterator tmp = it->route.begin(); tmp != it->route.end(); tmp++) {
std::cout << " Route: " << tmp->path << std::endl;
std::cout << " Root: " << tmp->rroot << std::endl;
Expand Down Expand Up @@ -60,7 +63,7 @@ void Parser::resetParam(int type, int identLevel) {
if (identLevel == 1)
serverNodes.back().index.clear();
if (identLevel == 2)
serverNodes.back().route.back().index.clear(); // ! Might need a clear instead of pop_back
serverNodes.back().route.back().index.clear();
}
if (type == METHOD) {
if (identLevel == 1)
Expand Down Expand Up @@ -98,6 +101,8 @@ void Parser::resetParam(int type, int identLevel) {
serverNodes.back().maxConn = -1;
if (type == REDIRECT)
serverNodes.back().route.back().redir = std::string();
if (type == CGI_PARAM)
serverNodes.back().cgi.pop_back();
}

std::list<token>::iterator Parser::getLastTokenIt(std::list<token> tokens) {
Expand Down

0 comments on commit 92d9066

Please sign in to comment.