Compare commits
6 commits
submission
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
df45cf75db | ||
|
|
50891b21f2 | ||
|
|
221e801307 | ||
|
|
f7af2c3850 | ||
|
|
f0127b00a3 | ||
|
|
286730b70d |
32 changed files with 217 additions and 34 deletions
1
httpd/.gitignore → .gitignore
vendored
1
httpd/.gitignore → .gitignore
vendored
|
|
@ -7,3 +7,4 @@
|
|||
*.core
|
||||
httpd
|
||||
__pycache__
|
||||
env/
|
||||
|
|
@ -5,8 +5,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
@ -6,6 +6,9 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
// Default values
|
||||
#define DEFAULT_DF "index.html"
|
||||
|
||||
/*
|
||||
** @brief Enum daemon
|
||||
** NO_OPTION if the '--daemon' option is not given
|
||||
|
|
@ -17,7 +17,7 @@ void daemon_init(struct config *cfg)
|
|||
config = cfg;
|
||||
}
|
||||
|
||||
int get_pid()
|
||||
int get_pid(void)
|
||||
{
|
||||
FILE *stream = fopen(config->pid_file, "r");
|
||||
if (stream == NULL)
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
int get_pid();
|
||||
int get_pid(void);
|
||||
|
||||
/* @brief
|
||||
*/
|
||||
|
|
@ -42,6 +42,12 @@ ssize_t read_value(struct string *str, size_t offset, struct string **res)
|
|||
if (str->size <= offset + nread || str->data[offset + nread] != '\n')
|
||||
return ERR_HTTP_INVALID_INPUT;
|
||||
|
||||
// Trim trailing \r
|
||||
if ((*res)->size > 0 && (*res)->data[(*res)->size - 1] == '\r')
|
||||
{
|
||||
(*res)->size--;
|
||||
}
|
||||
|
||||
return nread;
|
||||
}
|
||||
|
||||
|
|
@ -53,7 +59,8 @@ ssize_t parse_headers(struct http_request *res, struct string *req,
|
|||
|
||||
// Yes I know I do one useless allocation but I really don't care at this
|
||||
// point
|
||||
while (req->data[i] != '\n') // ! Blank line
|
||||
while (i < req->size && req->data[i] != '\n'
|
||||
&& req->data[i] != '\r') // ! Blank line
|
||||
{
|
||||
if (header == NULL)
|
||||
{
|
||||
|
|
@ -73,21 +80,26 @@ ssize_t parse_headers(struct http_request *res, struct string *req,
|
|||
return ERR_HTTP_OUT_OF_MEMORY;
|
||||
|
||||
// Read field
|
||||
ssize_t nread = read_field(req, offset, &header->field);
|
||||
ssize_t nread = read_field(req, i, &header->field);
|
||||
if (nread <= 0)
|
||||
return nread; // Contains error code when negative
|
||||
|
||||
i += nread;
|
||||
i += nread + 1;
|
||||
|
||||
// Read value
|
||||
nread = read_value(req, offset, &header->value);
|
||||
nread = read_value(req, i, &header->value);
|
||||
if (nread <= 0)
|
||||
return nread; // Contains error code when negative
|
||||
|
||||
i += nread + 1;
|
||||
}
|
||||
|
||||
return i + 1;
|
||||
if (i < req->size && req->data[i] == '\r')
|
||||
i++;
|
||||
if (i < req->size && req->data[i] == '\n')
|
||||
i++;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
struct http_header *get_header(struct http_header *headers, const char *field)
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
// #include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
|
@ -68,9 +68,15 @@ static ssize_t parse_reqline(struct http_request *res, struct string *req)
|
|||
return ERR_HTTP_INVALID_INPUT;
|
||||
i += skipped;
|
||||
|
||||
// CRLF (EOL)
|
||||
if (req->data[i++] != '\r' && req->data[i++] != '\n')
|
||||
// CRLF (EOL) oh qu'il est casse couilles celui-là
|
||||
ssize_t req_size = req->size; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah
|
||||
if (i < req_size && req->data[i] == '\r')
|
||||
i++;
|
||||
if (i >= req_size || req->data[i] != '\n')
|
||||
return ERR_HTTP_INVALID_INPUT;
|
||||
i++;
|
||||
// Donc 2h de debug pour ça là ? Plutot envie de me tirer une balle si vous
|
||||
// voulez mon avis
|
||||
|
||||
return i;
|
||||
}
|
||||
|
|
@ -184,10 +190,35 @@ static void check_req(struct http_request *req, struct http_response *resp)
|
|||
!= 0)
|
||||
resp->status_code = 400;
|
||||
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;
|
||||
|
||||
printf("%s %d\n", req->protocol->data, resp->status_code);
|
||||
// Host
|
||||
if (resp->status_code != 400 && resp->status_code != 505)
|
||||
{
|
||||
int host_count = 0;
|
||||
struct http_header *cur = req->headers;
|
||||
while (cur != NULL)
|
||||
{
|
||||
if (cur->field->size == 4
|
||||
&& string_compare_n_str(cur->field, "host", 4) == 0)
|
||||
{
|
||||
host_count++;
|
||||
if (cur->value == NULL || cur->value->size == 0)
|
||||
{
|
||||
resp->status_code = 400;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
if (host_count != 1)
|
||||
resp->status_code = 400;
|
||||
}
|
||||
|
||||
// printf("%s %d\n", req->protocol->data, resp->status_code);
|
||||
}
|
||||
|
||||
// === Functions
|
||||
|
|
@ -215,12 +246,16 @@ void handle_request(int client_fd, char *client_ip)
|
|||
{
|
||||
string_concat_str(str, buffer, nread);
|
||||
}
|
||||
string_concat_str(str, buffer, nread);
|
||||
if (nread > 0)
|
||||
string_concat_str(str, buffer, nread);
|
||||
|
||||
// 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);
|
||||
|
|
@ -230,12 +265,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;
|
||||
|
|
@ -288,7 +331,19 @@ struct http_request *parse_request(struct string *req)
|
|||
size_t i = 0;
|
||||
ssize_t nread = parse_reqline(res, req);
|
||||
if (nread <= 0)
|
||||
return NULL;
|
||||
{
|
||||
if (nread == ERR_HTTP_NOT_IMPLEMENTED)
|
||||
res->status_code = 501;
|
||||
else
|
||||
res->status_code = 400;
|
||||
|
||||
if (res->target == NULL)
|
||||
res->target = string_create("", 0);
|
||||
if (res->protocol == NULL)
|
||||
res->protocol = string_create("HTTP/1.1", 8);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// Split path and query
|
||||
split_target(res);
|
||||
|
|
@ -298,7 +353,10 @@ struct http_request *parse_request(struct string *req)
|
|||
// Headers
|
||||
nread = parse_headers(res, req, i);
|
||||
if (nread <= 0)
|
||||
return NULL;
|
||||
{
|
||||
res->status_code = 400;
|
||||
return res;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
@ -317,8 +375,11 @@ struct http_response *generate_response(struct http_request *req)
|
|||
res->protocol = string_create(protocol, strlen(protocol));
|
||||
|
||||
// Target
|
||||
str_concat_string(config->servers->root_dir,
|
||||
strlen(config->servers->root_dir), req->target);
|
||||
if (req->status_code == 0)
|
||||
{
|
||||
str_concat_string(config->servers->root_dir,
|
||||
strlen(config->servers->root_dir), req->target);
|
||||
}
|
||||
|
||||
// Status code
|
||||
if (req->status_code == 0)
|
||||
|
|
@ -340,7 +401,8 @@ struct http_response *generate_response(struct http_request *req)
|
|||
res->status_code = req->status_code;
|
||||
|
||||
// Check protocol and method
|
||||
check_req(req, res);
|
||||
if (req->status_code == 0)
|
||||
check_req(req, res);
|
||||
|
||||
// Headers
|
||||
char *time = get_time();
|
||||
|
|
@ -362,6 +424,10 @@ struct http_response *generate_response(struct http_request *req)
|
|||
res->status_code = 403;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
append_header(&res->headers, create_header("Content-Length", "0"));
|
||||
}
|
||||
append_header(&res->headers, create_header("Connection", "close"));
|
||||
|
||||
// Status msg
|
||||
|
|
@ -26,7 +26,7 @@ void errlog_init(bool enabled, int logfile_fd, struct server_config *serv_cfg)
|
|||
config.server_cfg = serv_cfg;
|
||||
}
|
||||
|
||||
void print_err()
|
||||
void print_err(void)
|
||||
{
|
||||
print_log_err("%s", get_err());
|
||||
}
|
||||
|
|
@ -55,7 +55,7 @@ void print_log_err(char *format, ...)
|
|||
fprintf(stderr, "Error: %s", get_err());
|
||||
}
|
||||
|
||||
char *get_err()
|
||||
char *get_err(void)
|
||||
{
|
||||
return strerror(errno);
|
||||
}
|
||||
|
|
@ -14,7 +14,7 @@ void errlog_init(bool enabled, int logfile_fd, struct server_config *serv_cfg);
|
|||
/* @brief Retrieves the last error with errno and prints the corresponding
|
||||
* error message in the logs and stderr
|
||||
*/
|
||||
void print_err();
|
||||
void print_err(void);
|
||||
|
||||
/* @brief Prints error logs, just like print_log() but for errors
|
||||
*/
|
||||
|
|
@ -22,6 +22,6 @@ void print_log_err(char *format, ...);
|
|||
|
||||
/* @brief Returns the string corresponding to the last error that happened
|
||||
*/
|
||||
char *get_err();
|
||||
char *get_err(void);
|
||||
|
||||
#endif // ! ERRORS_H
|
||||
|
|
@ -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)
|
||||
|
|
@ -130,3 +130,49 @@ def test_bad_request():
|
|||
assert response.status == 400
|
||||
finally:
|
||||
kill_httpd(proc)
|
||||
|
||||
@pytest.mark.timeout(2)
|
||||
def test_head_index():
|
||||
proc = spawn_httpd("out.log")
|
||||
try:
|
||||
req = requests.head(f"http://{host}:{port}/index.html")
|
||||
assert req.status_code == 200
|
||||
assert req.text == ""
|
||||
finally:
|
||||
kill_httpd(proc)
|
||||
|
||||
@pytest.mark.timeout(2)
|
||||
def test_missing_host():
|
||||
proc = spawn_httpd("out.log")
|
||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
sock.connect((host,int(port)))
|
||||
|
||||
request = f"GET /index.html HTTP/1.1\r\nConnection: close\r\n\r\n"
|
||||
|
||||
sock.sendall(request.encode())
|
||||
|
||||
response = http.client.HTTPResponse(sock)
|
||||
response.begin()
|
||||
|
||||
try:
|
||||
assert response.status == 400
|
||||
finally:
|
||||
kill_httpd(proc)
|
||||
|
||||
@pytest.mark.timeout(2)
|
||||
def test_directory_traversal():
|
||||
proc = spawn_httpd("out.log")
|
||||
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
sock.connect((host,int(port)))
|
||||
|
||||
request = f"GET /../test_suite.py HTTP/1.1\r\nHOST: {host}:{port}\r\nConnection: close\r\n\r\n"
|
||||
|
||||
sock.sendall(request.encode())
|
||||
|
||||
response = http.client.HTTPResponse(sock)
|
||||
response.begin()
|
||||
|
||||
try:
|
||||
assert response.status in [400, 403, 404]
|
||||
finally:
|
||||
kill_httpd(proc)
|
||||
40
tests/tests_mieux.sh
Executable file
40
tests/tests_mieux.sh
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
#!/bin/sh
|
||||
|
||||
# Simple test script for HTTP/1.1 Host header compliance
|
||||
# Usage: ./test_host_compliance.sh [IP] [PORT]
|
||||
|
||||
IP=${1:-"127.0.0.1"}
|
||||
PORT=${2:-"6996"}
|
||||
|
||||
echo "Targeting server at $IP:$PORT"
|
||||
|
||||
test_req() {
|
||||
NAME="$1"
|
||||
PAYLOAD="$2"
|
||||
EXPECTED="$3"
|
||||
|
||||
echo -n "Test: $NAME ... "
|
||||
# Send payload, wait max 1s for response
|
||||
RESP=$(printf "$PAYLOAD" | nc -w 1 $IP $PORT 2>/dev/null | head -n 1)
|
||||
|
||||
if echo "$RESP" | grep -q "$EXPECTED"; then
|
||||
echo "PASS"
|
||||
else
|
||||
echo "FAIL (Expected '$EXPECTED', got '$RESP')"
|
||||
fi
|
||||
}
|
||||
|
||||
# 1. Valid Request
|
||||
test_req "Valid Request" "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n" "200 OK"
|
||||
|
||||
# 2. Missing Host Header
|
||||
test_req "Missing Host" "GET / HTTP/1.1\r\n\r\n" "400 Bad Request"
|
||||
|
||||
# 3. Empty Host Header
|
||||
test_req "Empty Host" "GET / HTTP/1.1\r\nHost:\r\n\r\n" "400 Bad Request"
|
||||
|
||||
# 4. Multiple Host Headers
|
||||
test_req "Multiple Hosts" "GET / HTTP/1.1\r\nHost: a\r\nHost: b\r\n\r\n" "400 Bad Request"
|
||||
|
||||
# 5. Bad Protocol Version (Should be 505 now with the fix)
|
||||
test_req "Bad Protocol (HTTP/1.0)" "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n" "505"
|
||||
Loading…
Add table
Add a link
Reference in a new issue