fatigué
This commit is contained in:
parent
286730b70d
commit
f0127b00a3
4 changed files with 49 additions and 12 deletions
|
|
@ -5,8 +5,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "../utils/string/string.h"
|
// #include "../utils/string/string.h"
|
||||||
#include "bits/getopt_ext.h"
|
// #include "bits/getopt_ext.h"
|
||||||
|
|
||||||
#define ARG_VALID 0
|
#define ARG_VALID 0
|
||||||
#define ARG_INVALID 1
|
#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
|
// == Main functions
|
||||||
|
|
||||||
struct config *parse_configuration(int argc, char *argv[])
|
struct config *parse_configuration(int argc, char *argv[])
|
||||||
|
|
@ -227,6 +240,8 @@ struct config *parse_configuration(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// apply_default_values(config);
|
||||||
|
|
||||||
// Check config validity
|
// Check config validity
|
||||||
if (check_config(config) != 0)
|
if (check_config(config) != 0)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// Default values
|
||||||
|
#define DEFAULT_DF "index.html"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** @brief Enum daemon
|
** @brief Enum daemon
|
||||||
** NO_OPTION if the '--daemon' option is not given
|
** NO_OPTION if the '--daemon' option is not given
|
||||||
|
|
|
||||||
|
|
@ -184,7 +184,9 @@ static void check_req(struct http_request *req, struct http_response *resp)
|
||||||
!= 0)
|
!= 0)
|
||||||
resp->status_code = 400;
|
resp->status_code = 400;
|
||||||
else if (string_compare_strictly_n_str(req->protocol, "HTTP/1.1",
|
else if (string_compare_strictly_n_str(req->protocol, "HTTP/1.1",
|
||||||
strlen("HTTP/1.1") != 0))
|
strlen("HTTP/1.1"))
|
||||||
|
!= 0)
|
||||||
|
|
||||||
resp->status_code = 505;
|
resp->status_code = 505;
|
||||||
|
|
||||||
// printf("%s %d\n", req->protocol->data, resp->status_code);
|
// printf("%s %d\n", req->protocol->data, resp->status_code);
|
||||||
|
|
@ -220,7 +222,10 @@ void handle_request(int client_fd, char *client_ip)
|
||||||
// Parse request
|
// Parse request
|
||||||
struct http_request *req = parse_request(str);
|
struct http_request *req = parse_request(str);
|
||||||
if (req == NULL)
|
if (req == NULL)
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
char *method = get_http_method(req->method);
|
char *method = get_http_method(req->method);
|
||||||
char *target = string_to_charptr(req->target);
|
char *target = string_to_charptr(req->target);
|
||||||
print_log_request(method, target, client_ip);
|
print_log_request(method, target, client_ip);
|
||||||
|
|
@ -230,12 +235,20 @@ void handle_request(int client_fd, char *client_ip)
|
||||||
// Generate response
|
// Generate response
|
||||||
struct http_response *resp = generate_response(req);
|
struct http_response *resp = generate_response(req);
|
||||||
if (resp == NULL)
|
if (resp == NULL)
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
|
free(req);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Format response to string
|
// Format response to string
|
||||||
struct string *res = format_response(resp);
|
struct string *res = format_response(resp);
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
|
{
|
||||||
|
free(str);
|
||||||
|
free(req);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Send response
|
// Send response
|
||||||
ssize_t nsent;
|
ssize_t nsent;
|
||||||
|
|
@ -288,7 +301,10 @@ struct http_request *parse_request(struct string *req)
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
ssize_t nread = parse_reqline(res, req);
|
ssize_t nread = parse_reqline(res, req);
|
||||||
if (nread <= 0)
|
if (nread <= 0)
|
||||||
|
{
|
||||||
|
free(res);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// Split path and query
|
// Split path and query
|
||||||
split_target(res);
|
split_target(res);
|
||||||
|
|
@ -298,7 +314,10 @@ struct http_request *parse_request(struct string *req)
|
||||||
// Headers
|
// Headers
|
||||||
nread = parse_headers(res, req, i);
|
nread = parse_headers(res, req, i);
|
||||||
if (nread <= 0)
|
if (nread <= 0)
|
||||||
|
{
|
||||||
|
free(res);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ executable = "./httpd"
|
||||||
|
|
||||||
def spawn_httpd(stdout_filename, args=[]):
|
def spawn_httpd(stdout_filename, args=[]):
|
||||||
with open(stdout_filename,"w") as f:
|
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)
|
time.sleep(0.2)
|
||||||
|
|
||||||
return httpd_proc
|
return httpd_proc
|
||||||
|
|
@ -21,7 +21,7 @@ def kill_httpd(proc):
|
||||||
#proc.send_signal(sp.SIGINT)
|
#proc.send_signal(sp.SIGINT)
|
||||||
proc.kill()
|
proc.kill()
|
||||||
|
|
||||||
@pytest.mark.timeout(2)
|
# @pytest.mark.timeout(2)
|
||||||
def test_bad_config():
|
def test_bad_config():
|
||||||
proc = spawn_httpd("out.log", ["hello","world"])
|
proc = spawn_httpd("out.log", ["hello","world"])
|
||||||
proc.wait(1)
|
proc.wait(1)
|
||||||
|
|
@ -30,7 +30,7 @@ def test_bad_config():
|
||||||
finally:
|
finally:
|
||||||
kill_httpd(proc)
|
kill_httpd(proc)
|
||||||
|
|
||||||
@pytest.mark.timeout(2)
|
# @pytest.mark.timeout(2)
|
||||||
def test_get_index():
|
def test_get_index():
|
||||||
proc = spawn_httpd("out.log")
|
proc = spawn_httpd("out.log")
|
||||||
req = requests.get(f"http://{host}:{port}/index.html")
|
req = requests.get(f"http://{host}:{port}/index.html")
|
||||||
|
|
@ -41,7 +41,7 @@ def test_get_index():
|
||||||
finally:
|
finally:
|
||||||
kill_httpd(proc)
|
kill_httpd(proc)
|
||||||
|
|
||||||
@pytest.mark.timeout(2)
|
# @pytest.mark.timeout(2)
|
||||||
def test_get_default():
|
def test_get_default():
|
||||||
proc = spawn_httpd("out.log")
|
proc = spawn_httpd("out.log")
|
||||||
req = requests.get(f"http://{host}:{port}/")
|
req = requests.get(f"http://{host}:{port}/")
|
||||||
|
|
@ -52,13 +52,13 @@ def test_get_default():
|
||||||
finally:
|
finally:
|
||||||
kill_httpd(proc)
|
kill_httpd(proc)
|
||||||
|
|
||||||
@pytest.mark.timeout(2)
|
# @pytest.mark.timeout(2)
|
||||||
def test_no_file():
|
def test_no_file():
|
||||||
proc = spawn_httpd("out.log")
|
proc = spawn_httpd("out.log")
|
||||||
req = requests.get(f"http://{host}:{port}/notindex.html")
|
req = requests.get(f"http://{host}:{port}/notindex.html")
|
||||||
assert req.status_code == 404
|
assert req.status_code == 404
|
||||||
|
|
||||||
@pytest.mark.timeout(2)
|
# @pytest.mark.timeout(2)
|
||||||
def test_bad_request():
|
def test_bad_request():
|
||||||
proc = spawn_httpd("out.log")
|
proc = spawn_httpd("out.log")
|
||||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
|
|
@ -76,7 +76,7 @@ def test_bad_request():
|
||||||
finally:
|
finally:
|
||||||
kill_httpd(proc)
|
kill_httpd(proc)
|
||||||
|
|
||||||
@pytest.mark.timeout(2)
|
# @pytest.mark.timeout(2)
|
||||||
def test_invalid_method():
|
def test_invalid_method():
|
||||||
proc = spawn_httpd("out.log")
|
proc = spawn_httpd("out.log")
|
||||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
|
|
@ -95,7 +95,7 @@ def test_invalid_method():
|
||||||
kill_httpd(proc)
|
kill_httpd(proc)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.timeout(2)
|
# @pytest.mark.timeout(2)
|
||||||
def test_invalid_version():
|
def test_invalid_version():
|
||||||
proc = spawn_httpd("out.log")
|
proc = spawn_httpd("out.log")
|
||||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
|
|
@ -113,7 +113,7 @@ def test_invalid_version():
|
||||||
finally:
|
finally:
|
||||||
kill_httpd(proc)
|
kill_httpd(proc)
|
||||||
|
|
||||||
@pytest.mark.timeout(2)
|
# @pytest.mark.timeout(2)
|
||||||
def test_bad_request():
|
def test_bad_request():
|
||||||
proc = spawn_httpd("out.log")
|
proc = spawn_httpd("out.log")
|
||||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue