This commit is contained in:
cdricms
2024-11-20 18:29:11 +01:00
parent cdae291dd8
commit e24c207bdc
4 changed files with 42 additions and 32 deletions

View File

@@ -1,8 +0,0 @@
code = 200
ct = "text/html"
content = "<!DOCTYPE html><html><body><h1>Bonjour!</h1></body></html>"
print(f"HTTP/1.1 {code}")
print(f"Content-Type: {ct}")
print(f"Content-Length: {len(content)}")
print()
print(content)

16
cgi/sleep.py Normal file
View File

@@ -0,0 +1,16 @@
import time
time.sleep(5)
code = 200
ct = "text/html"
content = "<!DOCTYPE html><html><body><h1>Je dormais!</h1></body></html>"
response = f"""
HTTP/1.1 {code}
Content-Type: {ct}
Content-Length: {len(content)}
{content}
"""
print(response)

View File

@@ -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");

View File

@@ -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;