From 1b7d3882312f391a0738e64c4a2c6dfef71e4835 Mon Sep 17 00:00:00 2001 From: Eric LAZO Date: Tue, 16 Aug 2022 20:41:57 +0200 Subject: [PATCH] a few small fixes here and there, more telnet tests, starting Siege load testing --- test1.config => Tester/test1.config | 13 +++-- Tester/test_header.sh | 6 +-- Tester/test_method.sh | 6 +-- Tester/test_path.sh | 3 +- Tester/test_template.sh | 6 +-- Tester/test_valid_uri.sh | 74 ++++++++++++++++++++++++++++ Tester/www/test/index1.html | 2 - compare.txt | 76 ++++++++++++++--------------- default.config | 13 +++-- main_test.sh | 11 +++-- srcs/Client.cpp | 2 + srcs/config/postProcessing.cpp | 22 ++++++--- srcs/webserv/method_get.cpp | 19 +++----- srcs/webserv/response.cpp | 1 + tmp.txt | 1 - tmp1.txt | 1 - urls.txt | 1 + 17 files changed, 163 insertions(+), 94 deletions(-) rename test1.config => Tester/test1.config (92%) create mode 100644 Tester/test_valid_uri.sh delete mode 100644 tmp.txt delete mode 100644 tmp1.txt diff --git a/test1.config b/Tester/test1.config similarity index 92% rename from test1.config rename to Tester/test1.config index fc03ed6..e245aaa 100644 --- a/test1.config +++ b/Tester/test1.config @@ -63,8 +63,12 @@ server { root ./Tester/www/test/; } + location /stylesheet { + root ./stylesheet/; + } + location /test/index1.html { - root ./Tester/www/test/; + root ./Tester/www/test/index1.html; index index1.html subdex.html; } @@ -72,14 +76,9 @@ server { redirect 301 https://berniesanders.com/404/; } - location /stylesheet/ { -# root ./www/../; - root ./styelsheet/; - } - location /test/something.html { # allow_methods DELETE; - root ./Tester/www/test/; + root ./Tester/www/test/something.html; } location /test/test_deeper/ { diff --git a/Tester/test_header.sh b/Tester/test_header.sh index 8f8e5ef..60f7974 100644 --- a/Tester/test_header.sh +++ b/Tester/test_header.sh @@ -2,7 +2,7 @@ test_name="Header Test" -config_file="test1.config" +config_file="./Tester/test1.config" port=4040 host="localhost" @@ -41,10 +41,10 @@ run_this_test() { echo "----- $test_name -----" echo -e "$_RED$request$_END" - } >> telnet.out + } >> telnet.log echo -e "$request" sleep 1 - echo -e "\n\n" >> telnet.out + echo -e "\n\n" >> telnet.log done } diff --git a/Tester/test_method.sh b/Tester/test_method.sh index 076d7eb..ca0fb43 100644 --- a/Tester/test_method.sh +++ b/Tester/test_method.sh @@ -2,7 +2,7 @@ test_name="Method Test" -config_file="test1.config" +config_file="./Tester/test1.config" port=4040 host="localhost" @@ -41,10 +41,10 @@ run_this_test() { echo "----- $test_name -----" echo -e "$_RED$request$_END" - } >> telnet.out + } >> telnet.log echo -e "$request" sleep 1 - echo -e "\n\n" >> telnet.out + echo -e "\n\n" >> telnet.log done } diff --git a/Tester/test_path.sh b/Tester/test_path.sh index 3439313..ffd4417 100644 --- a/Tester/test_path.sh +++ b/Tester/test_path.sh @@ -2,7 +2,7 @@ test_name="Path Test" -config_file="test1.config" +config_file="./Tester/test1.config" port=4040 host="localhost" @@ -31,7 +31,6 @@ run_this_test() echo "----- $test_name -----" echo -e "$_RED$request$_END" } >> telnet.log - # echo -e "$request" > compare.txt >> telnet.out echo -e "$request" sleep 1 echo -e "\n" >> telnet.log diff --git a/Tester/test_template.sh b/Tester/test_template.sh index ffb11e3..99a9821 100644 --- a/Tester/test_template.sh +++ b/Tester/test_template.sh @@ -2,7 +2,7 @@ test_name="Template test all good" -config_file="default.config" +config_file="./Tester/test1.config" port=4040 host="localhost" @@ -35,7 +35,7 @@ run_this_test() { echo "----- $test_name -----" echo -e "$_RED$request$_END" - } >> telnet.out + } >> telnet.log # } &>> test.log # } 2>>&1 # } &>> /dev/stdout @@ -47,7 +47,7 @@ run_this_test() # } > /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" >> telnet.log # echo -e "\n\n------\n\n" >> telnet.out # echo -e "\n------\n" > /dev/stdout # run_a_test diff --git a/Tester/test_valid_uri.sh b/Tester/test_valid_uri.sh new file mode 100644 index 0000000..ba273a5 --- /dev/null +++ b/Tester/test_valid_uri.sh @@ -0,0 +1,74 @@ +#! /bin/bash + +test_name="Valid URIs Test" + +config_file="./Tester/test1.config" +port=4040 +host="localhost" + +paths=("/" "/stylesheet/style.css" "/test/something.html" "/test/something.html/" "/test/" "/list" "/hilarious_404" "/redirect") # you can add many +#paths=("/") # you can add many +#methods=("GET" "POST" "DELETE") +methods=("GET") +httpz=("HTTP/1.1") + + + +l1="${methods[0]} ${paths[0]} ${httpz[0]}" + +header="Host: $host" +body= + + +run_this_test() +{ + + for i in "${paths[@]}" + do + l1="${methods[0]} $i ${httpz[0]}" + request="$l1\n$header\n$body\n" + { + echo "----- $test_name -----" + echo -e "$_RED$request$_END" + } >> telnet.log + echo -e "$request" + sleep 1 + echo -e "\n" >> telnet.log + done + +} + + + +# expected result... + + +files=() + +file="expected_valid_uri_test.txt" +files+=("expected_valid_files_test.txt") +#files+=("expected_path_root_test.txt") +#files+=("expected_path_testnoslash_test.txt") +#files+=("expected_path_testslash_test.txt") +#files+=("expected_path_list_test.txt") +#files+=("expected_path_badlist_test.txt") +#files+=("expected_path_wrong_test.txt") + + +#local_expected_test_file=$file +local_expected_test_files=() +test_path="" + +add_path() +{ + for i in "${files[@]}" + do + local_expected_test_files+=("$test_path$i") + done +} + +#add_path + + + + diff --git a/Tester/www/test/index1.html b/Tester/www/test/index1.html index f976302..e6ee456 100644 --- a/Tester/www/test/index1.html +++ b/Tester/www/test/index1.html @@ -2,9 +2,7 @@ Webserv test index - -

Webserv Test Index

diff --git a/compare.txt b/compare.txt index e09bbf5..2ff687b 100644 --- a/compare.txt +++ b/compare.txt @@ -21,19 +21,26 @@ Content-Length: 193 HTTP/1.1 200 OK Server: Webserv/0.1 Connection: keep-alive +Content-Type: text/css; charset=UTF-8 +Content-Length: 43 + +h1 { + color: red; + text-align: center; +} +HTTP/1.1 200 OK +Server: Webserv/0.1 +Connection: keep-alive Content-Type: text/html; charset=UTF-8 -Content-Length: 290 +Content-Length: 207 - Webserv test index - - - + Webserv test Something -

Webserv Test Index

+

Webserv Test Something


(˚3˚)

@@ -42,15 +49,30 @@ HTTP/1.1 200 OK Server: Webserv/0.1 Connection: keep-alive Content-Type: text/html; charset=UTF-8 -Content-Length: 290 +Content-Length: 207 + + + + + Webserv test Something + + +

Webserv Test Something

+
+

(˚3˚)

+ + +HTTP/1.1 200 OK +Server: Webserv/0.1 +Connection: keep-alive +Content-Type: text/html; charset=UTF-8 +Content-Length: 227 Webserv test index - -

Webserv Test Index

@@ -58,7 +80,7 @@ Content-Length: 290

(˚3˚)

-HTTP/1.1 +HTTP/1.1 200 OK Server: Webserv/0.1 Connection: keep-alive Content-Type: text/html; charset=UTF-8 @@ -80,37 +102,13 @@ Content-Length: 1277 rfc2119.html error_pages rfc2119_files -
HTTP/1.1 404 Not Found +
HTTP/1.1 301 Moved Permanently +Location: https://berniesanders.com/404/ + Server: Webserv/0.1 Connection: keep-alive -Content-Type: text/html; charset=UTF-8 -Content-Length: 210 +HTTP/1.1 307 Temporary Redirect +Location: https://fr.wikipedia.org/wiki/Ketchup - - - - 404 Not Found - - -

Check it UP 404 Not Found

-
-

Le Webserv/0.1

- - -HTTP/1.1 404 Not Found Server: Webserv/0.1 Connection: keep-alive -Content-Type: text/html; charset=UTF-8 -Content-Length: 210 - - - - - 404 Not Found - - -

Check it UP 404 Not Found

-
-

Le Webserv/0.1

- - diff --git a/default.config b/default.config index 8a9fa18..329a127 100644 --- a/default.config +++ b/default.config @@ -63,8 +63,12 @@ server { root ./www/test/; } + location /stylesheet { + root ./stylesheet/; + } + location /test/index1.html { - root ./www/test/; + root ./www/test/index1.html; index index1.html subdex.html; } @@ -72,14 +76,9 @@ server { redirect 301 https://berniesanders.com/404/; } - location /stylesheet/ { -# root ./www/../; - root ./styelsheet/; - } - location /test/something.html { # allow_methods DELETE; - root ./www/test/; + root ./www/test/something.html; } location /test/test_deeper/ { diff --git a/main_test.sh b/main_test.sh index 46a7986..4049bfd 100755 --- a/main_test.sh +++ b/main_test.sh @@ -21,7 +21,8 @@ _END='\033[0m' #test_file_names=("test_template.sh" "test_header.sh" "test_path.sh") -test_file_names=("test_method.sh" "test_header.sh" "test_path.sh") +#test_file_names=("test_method.sh" "test_header.sh" "test_path.sh") +test_file_names=("test_valid_uri.sh") #test_file_names=("test_path.sh") test_files=() @@ -103,10 +104,10 @@ test_all() done -# if [ "$arg" != "diff" ]; -# then -# cat telnet.log -# fi + if [ "$arg" = "" ]; + then + cat telnet.log + fi } diff --git a/srcs/Client.cpp b/srcs/Client.cpp index 06d0de5..c88ff3d 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -361,6 +361,8 @@ void Client::_parse_request_target( std::string target ) else _request.query = ""; _request.abs_path = target.substr(0, pos); + if (_request.abs_path[_request.abs_path.size() - 1] == '/') + _request.abs_path.erase(_request.abs_path.size() - 1); } void Client::_parse_request_fields() diff --git a/srcs/config/postProcessing.cpp b/srcs/config/postProcessing.cpp index 8a4c7c0..63d4861 100644 --- a/srcs/config/postProcessing.cpp +++ b/srcs/config/postProcessing.cpp @@ -50,14 +50,6 @@ void ConfigParser::_post_processing(std::vector *servers) && it_l->path.size() > 1) it_l->path.erase(it_l->path.size() - 1); - std::vector::const_iterator tmp = it_l + 1; - while (tmp != it->locations.end()) - { - if (it_l->path == tmp->path) - throw std::invalid_argument("Duplicate locations in config file"); - ++tmp; - } - if (it_l->root == "") it_l->root = it->root; @@ -71,6 +63,20 @@ void ConfigParser::_post_processing(std::vector *servers) ++it_l; } + + it_l = it->locations.begin(); + while (it_l != it->locations.end()) + { + std::vector::const_iterator tmp = it_l + 1; + while (tmp != it->locations.end()) + { + if (it_l->path == tmp->path) + throw std::invalid_argument("Duplicate locations in config file"); + ++tmp; + } + ++it_l; + } + std::sort(it->locations.begin(), it->locations.end()); std::reverse(it->locations.begin(), it->locations.end()); diff --git a/srcs/webserv/method_get.cpp b/srcs/webserv/method_get.cpp index 35b1d92..5090252 100644 --- a/srcs/webserv/method_get.cpp +++ b/srcs/webserv/method_get.cpp @@ -19,6 +19,7 @@ void Webserv::_get(Client *client, std::string &path) { path.append(client->assigned_location->index[i]); _get_file(client, path); + std::cerr << "Added an index\n"; //debug return ; } } @@ -88,16 +89,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; - // std::cout << "location root: " << client->assigned_location->root << " location path: " << client->assigned_location->path << '\n'; - // std::cout << "Path in auto is: " << path << '\n'; if ( (dir = opendir(path.c_str()) ) != NULL) { @@ -106,20 +101,19 @@ void Webserv::_autoindex(Client *client, const std::string &path) dir_list.append(AUTOINDEX_MID1); dir_list.append(path); dir_list.append(AUTOINDEX_MID2); - /* print all the files and directories within directory */ while ((ent = readdir (dir)) != NULL) { -// std::cout << "ent: " << ent->d_name << '\n'; if (strcmp(".", ent->d_name) == 0) continue ; dir_list.append("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); + if (ent->d_type == DT_DIR) + dir_list.append("/"); dir_list.append(""); dir_list.append("\n"); } @@ -131,15 +125,14 @@ void Webserv::_autoindex(Client *client, const std::string &path) // .. dir_list.append(AUTOINDEX_END); -// std::cout << "\n\n" << dir_list << '\n'; closedir (dir); + client->status = 200; _append_body(client, dir_list, "html"); } else { - // in theory not possible cuz we already checked... - std::cerr << "could not open dir\n"; - // throw? + client->status = 500; + perror("could not open dir"); return ; } } diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index 9e8357e..4da04f5 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -105,6 +105,7 @@ void Webserv::_construct_response(Client *client) void Webserv::_process_method(Client *client, std::string &path) { std::cerr << "allow_methods = " << http_methods_to_str(client->assigned_location->allow_methods) << "\n"; // debug + std::cerr << "Path again: " << path << '\n'; // debug switch (client->get_rq_method()) { diff --git a/tmp.txt b/tmp.txt deleted file mode 100644 index deba01f..0000000 --- a/tmp.txt +++ /dev/null @@ -1 +0,0 @@ -something diff --git a/tmp1.txt b/tmp1.txt deleted file mode 100644 index deba01f..0000000 --- a/tmp1.txt +++ /dev/null @@ -1 +0,0 @@ -something diff --git a/urls.txt b/urls.txt index 334e66b..82ecc7e 100644 --- a/urls.txt +++ b/urls.txt @@ -1,3 +1,4 @@ +http://localhost:4040 http://localhost:4040/test http://localhost:4040/test/test_deeper/ http://localhost:4040/test/test_deeper/super_deep/