diff --git a/httpd/.gitignore b/httpd/.gitignore index 035927b..85e1c00 100644 --- a/httpd/.gitignore +++ b/httpd/.gitignore @@ -7,3 +7,4 @@ *.core httpd __pycache__ +env/ diff --git a/httpd/src/config/config.c b/httpd/src/config/config.c index d910424..c8edde6 100644 --- a/httpd/src/config/config.c +++ b/httpd/src/config/config.c @@ -5,8 +5,8 @@ #include #include -#include "../utils/string/string.h" -#include "bits/getopt_ext.h" +// #include "../utils/string/string.h" +// #include "bits/getopt_ext.h" #define ARG_VALID 0 #define ARG_INVALID 1 @@ -180,6 +180,19 @@ static void print_arg_error(int err, char **argv, struct option options[], } } +// static void apply_default_values(struct config *cfg) +// { +// // Default file +// if (cfg->servers->default_file == NULL) +// { +// char *default_df = DEFAULT_DF; +// cfg->servers->default_file = +// malloc((strlen(default_df) + 1) * sizeof(char)); +// // TODO handle error +// strcpy(cfg->servers->default_file, default_df); +// } +// } + // == Main functions struct config *parse_configuration(int argc, char *argv[]) @@ -227,10 +240,12 @@ struct config *parse_configuration(int argc, char *argv[]) } } + // apply_default_values(config); + // Check config validity if (check_config(config) != 0) { - printf("%s: Missing mandatory flags, cannot continue.", argv[0]); + printf("%s: Missing mandatory flags, cannot continue.\n", argv[0]); config_destroy(config); return NULL; } diff --git a/httpd/src/config/config.h b/httpd/src/config/config.h index c1ba197..72be5a8 100644 --- a/httpd/src/config/config.h +++ b/httpd/src/config/config.h @@ -6,6 +6,9 @@ #include +// Default values +#define DEFAULT_DF "index.html" + /* ** @brief Enum daemon ** NO_OPTION if the '--daemon' option is not given diff --git a/httpd/src/http/http.c b/httpd/src/http/http.c index a7c7198..237f312 100644 --- a/httpd/src/http/http.c +++ b/httpd/src/http/http.c @@ -2,7 +2,7 @@ #include #include -#include +// #include #include #include #include @@ -245,7 +245,10 @@ void handle_request(int client_fd, char *client_ip) // Parse request struct http_request *req = parse_request(str); if (req == NULL) + { + free(str); return; + } char *method = get_http_method(req->method); char *target = string_to_charptr(req->target); print_log_request(method, target, client_ip); @@ -255,12 +258,20 @@ void handle_request(int client_fd, char *client_ip) // Generate response struct http_response *resp = generate_response(req); if (resp == NULL) + { + free(str); + free(req); return; + } // Format response to string struct string *res = format_response(resp); if (res == NULL) + { + free(str); + free(req); return; + } // Send response ssize_t nsent; diff --git a/httpd/tests/test_suite.py b/httpd/tests/test_suite.py index 41352dd..504f05a 100644 --- a/httpd/tests/test_suite.py +++ b/httpd/tests/test_suite.py @@ -12,7 +12,7 @@ executable = "./httpd" def spawn_httpd(stdout_filename, args=[]): with open(stdout_filename,"w") as f: - httpd_proc = sp.Popen([executable,"--pid_file","/tmp/HTTPd.pid","--ip",host,"--port", port, "--root_dir","./test_root_dir/","--server_name","httpd"] if args == [] else [executable] + args, stdout=f,stderr=sp.PIPE,bufsize=0) + httpd_proc = sp.Popen([executable,"--pid_file","/tmp/HTTPd.pid","--ip",host,"--port", port, "--root_dir","test_root_dir/","--server_name","httpd"] if args == [] else [executable] + args, stdout=f,stderr=sp.PIPE,bufsize=0) time.sleep(0.2) return httpd_proc @@ -21,7 +21,7 @@ def kill_httpd(proc): #proc.send_signal(sp.SIGINT) proc.kill() -@pytest.mark.timeout(2) +# @pytest.mark.timeout(2) def test_bad_config(): proc = spawn_httpd("out.log", ["hello","world"]) proc.wait(1) @@ -30,7 +30,7 @@ def test_bad_config(): finally: kill_httpd(proc) -@pytest.mark.timeout(2) +# @pytest.mark.timeout(2) def test_get_index(): proc = spawn_httpd("out.log") req = requests.get(f"http://{host}:{port}/index.html") @@ -41,7 +41,7 @@ def test_get_index(): finally: kill_httpd(proc) -@pytest.mark.timeout(2) +# @pytest.mark.timeout(2) def test_get_default(): proc = spawn_httpd("out.log") req = requests.get(f"http://{host}:{port}/") @@ -52,13 +52,13 @@ def test_get_default(): finally: kill_httpd(proc) -@pytest.mark.timeout(2) +# @pytest.mark.timeout(2) def test_no_file(): proc = spawn_httpd("out.log") req = requests.get(f"http://{host}:{port}/notindex.html") assert req.status_code == 404 -@pytest.mark.timeout(2) +# @pytest.mark.timeout(2) def test_bad_request(): proc = spawn_httpd("out.log") sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) @@ -76,7 +76,7 @@ def test_bad_request(): finally: kill_httpd(proc) -@pytest.mark.timeout(2) +# @pytest.mark.timeout(2) def test_invalid_method(): proc = spawn_httpd("out.log") sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) @@ -95,7 +95,7 @@ def test_invalid_method(): kill_httpd(proc) -@pytest.mark.timeout(2) +# @pytest.mark.timeout(2) def test_invalid_version(): proc = spawn_httpd("out.log") sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) @@ -113,7 +113,7 @@ def test_invalid_version(): finally: kill_httpd(proc) -@pytest.mark.timeout(2) +# @pytest.mark.timeout(2) def test_bad_request(): proc = spawn_httpd("out.log") sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)