Merge branch 'eric_config_parser'

This commit is contained in:
Eric LAZO
2022-08-14 21:42:08 +02:00
13 changed files with 354 additions and 7 deletions

3
1 Normal file
View File

@@ -0,0 +1,3 @@
------

View File

@@ -15,6 +15,28 @@ server {
error_page 404 ./www/error_pages/error_404.html;
# something to do with /upload
location /upload {
root ./www/test/;
index submit_form.html;
# upload_dir ./www/uploaded/;
# cgi_ext php;
}
location /uploaded {
# autoindex on;
root ./www/uploaded/;
upload_dir ./www/uploaded/;
}
# location /srcs/cgi-bin/ {
# root ./srcs/cgi-bin/;
# allow_methods POST;
# cgi_ext php;
# }
location /list {
autoindex on;
}

View File

@@ -268,8 +268,7 @@ void Client::clear_script()
void Client::print_client(std::string message)
{
std::map<std::string, std::string>::iterator it;
std::cout << "\n=== DEBUG PRINT CLIENT ===\n";
std::cout << "\n=== DEBUG PRINT CLIENT ===\n";
std::cout << message << ":\n----------\n\n" << "raw_request:\n__\n";
::print_special(raw_request);
std::cout << "\n__\n"

View File

@@ -0,0 +1,65 @@
#! /bin/bash/php
<?php
// $FileName=$_FILES['myFile']['filename'];
# $TmpName=$_FILES['myFile']['tmp_name'];
# move_uploaded_file($TmpName, $FileName);
# echo("File was uploaded successfully!");
// this part needs to be grabed from POST uri
$target_dir = "./www/uploaded/";
//$target_dir = $_POST["upload_dir"];
//$target_dir = $_GET["upload_dir"];
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$fileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
//if(isset($_POST["submit"])) {
// $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
// if($check !== false) {
// echo "File is an image - " . $check["mime"] . ".";
// $uploadOk = 1;
// } else {
// echo "File is not an image.";
// $uploadOk = 0;
// }
//}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
//if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
//&& $imageFileType != "gif" ) {
// echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
// $uploadOk = 0;
//}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

View File

@@ -194,7 +194,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
{
if (tmp_val[0].find_first_of(":") == NPOS)
{
if (!::isNumeric(tmp_val[0]))
if (!::isNumeric_btw(0, 65535, tmp_val[0]))
throw std::invalid_argument("bad port number");
server->host = "0.0.0.0";
server->port = tmp_val[0];
@@ -211,7 +211,7 @@ void ConfigParser::_set_server_values(ServerConfig *server, \
if (!::isNumeric_btw(0, 255, ip[i]))
throw std::invalid_argument("bad host ip");
}
if (!::isNumeric(tmp2[1]))
if (!::isNumeric_btw(0, 65535, tmp2[1]))
throw std::invalid_argument("bad port number");
server->host = tmp2[0];
server->port = tmp2[1];

View File

@@ -105,6 +105,10 @@ void Webserv::_autoindex(Client *client, const std::string &path)
{
std::cout << "_autoindex()\n";
// std::cout << "client target: " << client->get_rq_target() << '\n';
std::string dir_list;
DIR *dir;
struct dirent *ent;
@@ -122,11 +126,14 @@ void Webserv::_autoindex(Client *client, const std::string &path)
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL)
{
// std::cout << "ent: " << ent->d_name << '\n';
// std::cout << "ent: " << ent->d_name << '\n';
if (strcmp(".", ent->d_name) == 0)
continue ;
dir_list.append("<a href=\"");
dir_list.append(client->get_rq_abs_path() + "/");
// dir_list.append(client->get_rq_target());
dir_list.append(client->get_rq_abs_path());
if (dir_list[dir_list.size() - 1] != '/')
dir_list.push_back('/');
dir_list.append(ent->d_name);
dir_list.append("\">");
dir_list.append(ent->d_name);

View File

@@ -53,6 +53,8 @@ int Webserv::_read_request(Client *client)
// ::print_special(client->raw_request);
// std::cerr << "__raw_request__\n" << client->raw_request << "\n______\n"; // DEBUG
// print_special(client->raw_request);
if (!client->header_complete)
{
client->parse_request_headers(_servers);

View File

@@ -203,12 +203,13 @@ If we get a url that ends in / ignore the last /
*/
std::string uri = path;
if (uri[uri.size() - 1] == '/')
if (uri[uri.size() - 1] == '/' && uri.size() != 1)
uri.erase(uri.size() - 1);
for (std::vector<LocationConfig>::const_iterator it = server.locations.begin(); it != server.locations.end(); it++)
{
// std::cout << it->path << " -- ";
if (it->path.size() > uri.size())
continue ;

143
telnet_test.sh Executable file
View File

@@ -0,0 +1,143 @@
#! /bin/bash
# you need to put absolutely everything in ""
##########
# Colors
##########
_GREY='\033[30m'
_RED='\033[0;31m'
_GREEN='\033[32m'
_YELLOW='\033[33m'
_BLUE='\033[34m'
_PURPLE='\033[35m'
_CYAN='\033[36m'
_WHITE='\033[37m'
_END='\033[0m'
test_file=$1
source $test_file
connect_to_telnet="open $host $port"
start_telnet()
{
echo "open $host $port"
sleep 1
}
run_telnet()
{
echo "$connect_to_telnet"
sleep 1
echo -e "$request"
# echo "$l1"
# echo "$header"
# echo
# echo $body
# echo
# echo
echo
sleep 1
# ret = $(arg | telnet)
}
run()
{
echo "$connect_to_telnet"
sleep 1
run_this_test
}
run_a_test()
{
{
echo "----- $test_name -----"
echo -e "$_RED$request$_END"
} &> test.log
# run_telnet
# echo ${pid}
# echo -e "$_END---"
echo -e "$request" | telnet
# run_telnet | telnet
# run_telnet
# echo
# echo "----- end of test -----"
}
run_all()
{
echo
# ./webserv $config_file 2>&1 > webserv.log &
#while something i change headers
# for i in "${methods[@]}"
# do
# expected_result="something"
# run_a_test
# echo -e "${_GREEN}$expected_result${_END}"
## echo
# done
}
#######
# This is where stuff is launched
#######
rm -rf test.log
rm -rf telnet.out
#./webserv $config_file 2>&1 > webserv.log & run_all
#./webserv $config_file 2>&1 > webserv.log &
#./webserv $config_file &> /dev/null &
./webserv $config_file &> webserv.log &
#./webserv $config_file 1>&1 > webserv.log &
#run_all
#run_this_test
echo -e "${_GREEN}Running Telnet Test on '$test_name'${_END}"
sleep 1
run | telnet >> telnet.out
#run | telnet
pkill webserv

16
telnet_test2.sh Executable file
View File

@@ -0,0 +1,16 @@
#! /bin/bash
echo "open duckduckgo.com 80"
sleep 1
echo "GET / HTTP/1.1"
echo "Host: duckduckgo.com"
echo
echo
sleep 2
#(
#echo open duckduckgo.com 80
#echo "GET / HTTP/1.1"
#echo "Host: duckduckgo.com"
#echo "exit"
#) | telnet

11
test_header.txt Normal file
View File

@@ -0,0 +1,11 @@
config_file=...
port=...
# Host: ...
# the correct response
...

61
test_template.sh Normal file
View File

@@ -0,0 +1,61 @@
#! /bin/bash
test_name="Template test all good"
config_file="default.config"
port=4040
host="localhost"
paths=("/") # you can add many
methods=("GET" "POST" "DELETE")
#methods=("GET")
httpz=("HTTP/1.1")
# the parts we will send to webserv
# let main.sh handle the l1
l1="${methods[0]} ${paths[0]} ${https[0]}"
header="Host: $host"
body=
run_this_test()
{
for i in "${methods[@]}"
do
l1="$i ${paths[0]} ${httpz[0]}"
request="$l1\n$header\n$body\n"
{
echo "----- $test_name -----"
echo -e "$_RED$request$_END"
} >> telnet.out
# } &>> test.log
# } 2>>&1
# } &>> /dev/stdout
echo -e "$request"
sleep 1
# {
# echo -e "\n------\n"
# } > /dev/stdout
# echo -e "\n\n\n------\n\n\n" >> telnet.out
# echo -e "\n\n\n------\n\n\n" &> /dev/stdout
echo -e "\n\n" >> telnet.out
# echo -e "\n\n------\n\n" >> telnet.out
# echo -e "\n------\n" > /dev/stdout
# run_a_test
# echo
done
}
# expected result...

17
www/test/submit_form.html Normal file
View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Webserv test Something</title>
</head>
<body>
<h1 style="text-align:center">Webserv in Test</h1>
<hr>
<p style="text-align:center">Time to submit something:</p>
<!-- <form action="./srcs/cgi-bin/upload_file.php" method="post" enctype="multipart/form-data"> -->
<form action="/uploaded" method="post" enctype="multipart/form-data">
<!-- <input type="hidden" name="upload_dir" value="./www/uploaded/"> -->
<input type="file" id="fileToUpload" name="myFile">
<input type="submit" value="Upload File">
</form>
</body>
</html>