diff --git a/cgi/hello.py b/cgi/hello.py
deleted file mode 100644
index f19d84d..0000000
--- a/cgi/hello.py
+++ /dev/null
@@ -1,8 +0,0 @@
-code = 200
-ct = "text/html"
-content = "
Bonjour!
"
-print(f"HTTP/1.1 {code}")
-print(f"Content-Type: {ct}")
-print(f"Content-Length: {len(content)}")
-print()
-print(content)
diff --git a/cgi/sleep.py b/cgi/sleep.py
new file mode 100644
index 0000000..a36baac
--- /dev/null
+++ b/cgi/sleep.py
@@ -0,0 +1,16 @@
+import time
+time.sleep(5)
+
+code = 200
+ct = "text/html"
+content = "Je dormais!
"
+
+response = f"""
+HTTP/1.1 {code}
+Content-Type: {ct}
+Content-Length: {len(content)}
+
+{content}
+"""
+
+print(response)
diff --git a/http/http_response.c b/http/http_response.c
index 064ecf1..f6f6c67 100644
--- a/http/http_response.c
+++ b/http/http_response.c
@@ -33,14 +33,15 @@ void http_respond(HttpResponse *__res, int clientfd) {
if (__res == NULL)
return;
char response[BUFSIZ];
- // TODO: Handle return
- construct_response(*__res, response);
+ if (!construct_response(*__res, response))
+ return;
+ // Une fois la réponse construite on l'envoie au client.
send(clientfd, response, strlen(response), MSG_EOR);
}
char *read_file(const char *__path) {
- // If the file does not exist
+ // Si le fichier n'existe pas.
if (access(__path, F_OK) != 0) {
return NULL;
}
@@ -49,23 +50,21 @@ char *read_file(const char *__path) {
if (f == NULL) {
return NULL;
}
- // Get the length of the file
+
+ // On trouve la longueur en bytes du fichier.
fseek(f, 0, SEEK_END);
size_t length = ftell(f);
rewind(f);
- // Initialize a value to get the content
+ // Et on initialise un string de taille suffisante.
char *content = malloc(length * sizeof(char));
- if (content == NULL) {
- fclose(f);
- return NULL;
- }
-
- // Read the file into content and set the last char to \0
+ // On lit le contenu du fichier et nous écrivons tout ça dans content.
size_t bytesRead = fread(content, sizeof(char), length, f);
+ // On met le dernier caractère comme étant le null terminator: \0
content[bytesRead] = 0;
+ // On ferme évidemment le fichier.
fclose(f);
return content;
@@ -98,28 +97,30 @@ void free_response(HttpResponse *__res) {
}
HttpServerRunStatus cgi(const char *__path, int clientfd) {
- pid_t pid;
+ size_t length = strlen(__path) + 1;
+ char path[length];
+ sprintf(path, ".%s", __path);
+ if (access(path, F_OK) != 0) {
+ return HTTP_SRS_FILE_NOT_FOUND;
+ }
- pid = fork(); // Create a child process
+ // Création du processus enfant.
+ pid_t pid = fork();
if (pid < 0) {
- // Error handling for fork failure
perror("fork failed");
return HTTP_SRS_FORK_FAILED;
}
if (pid == 0) {
- // Redirect standard input to read from the pipe
+ // Renvoie tout ce qui sort du stdout du processus enfant vers le file
+ // descriptor client. On fait essentiellement un send vers le client.
if (dup2(clientfd, STDOUT_FILENO) == -1) {
perror("dup2");
exit(EXIT_FAILURE);
}
-
- // Close the read end of the pipe (it's no longer needed after
- // redirection)
- char path[1024] = {0};
- sprintf(path, ".%s", __path);
+ // On execute le script python.
execl("/usr/bin/python3", "python3", path, NULL);
perror("Exec failed");
diff --git a/http/http_server.h b/http/http_server.h
index 455b5fc..8246f98 100644
--- a/http/http_server.h
+++ b/http/http_server.h
@@ -22,15 +22,16 @@ typedef enum {
HTTP_SRS_HANDLE_REQUEST_FAILED,
HTTP_SRS_FORK_FAILED,
HTTP_SRS_WONT_HANDLE,
+ HTTP_SRS_FILE_NOT_FOUND,
} HttpServerRunStatus;
// Structure ayant pour objectif de stocker tout ce qui est nécessaire au bon
// fonctionnement du serveur.
typedef struct {
- int port; // Port du host
- int server_fd; // File descriptor du server
- unsigned int backlog; // Le nombre de clients possibles pouvant être en
- // attente.
+ int port; // Port du host
+ int server_fd; // File descriptor du server
+ unsigned int
+ backlog; // Le nombre de clients possibles pouvant être en attente.
struct sockaddr_in *address; // L'adresse décrivant une socket internet.
pid_t pid; // Le pid du parent en attente de requêtes.
} HttpServer;