Final
This commit is contained in:
@@ -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
16
cgi/sleep.py
Normal 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)
|
||||||
@@ -33,14 +33,15 @@ void http_respond(HttpResponse *__res, int clientfd) {
|
|||||||
if (__res == NULL)
|
if (__res == NULL)
|
||||||
return;
|
return;
|
||||||
char response[BUFSIZ];
|
char response[BUFSIZ];
|
||||||
// TODO: Handle return
|
if (!construct_response(*__res, response))
|
||||||
construct_response(*__res, response);
|
return;
|
||||||
|
|
||||||
|
// Une fois la réponse construite on l'envoie au client.
|
||||||
send(clientfd, response, strlen(response), MSG_EOR);
|
send(clientfd, response, strlen(response), MSG_EOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *read_file(const char *__path) {
|
char *read_file(const char *__path) {
|
||||||
// If the file does not exist
|
// Si le fichier n'existe pas.
|
||||||
if (access(__path, F_OK) != 0) {
|
if (access(__path, F_OK) != 0) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -49,23 +50,21 @@ char *read_file(const char *__path) {
|
|||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
// Get the length of the file
|
|
||||||
|
// On trouve la longueur en bytes du fichier.
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
size_t length = ftell(f);
|
size_t length = ftell(f);
|
||||||
rewind(f);
|
rewind(f);
|
||||||
|
|
||||||
// Initialize a value to get the content
|
// Et on initialise un string de taille suffisante.
|
||||||
char *content = malloc(length * sizeof(char));
|
char *content = malloc(length * sizeof(char));
|
||||||
|
|
||||||
if (content == NULL) {
|
// On lit le contenu du fichier et nous écrivons tout ça dans content.
|
||||||
fclose(f);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the file into content and set the last char to \0
|
|
||||||
size_t bytesRead = fread(content, sizeof(char), length, f);
|
size_t bytesRead = fread(content, sizeof(char), length, f);
|
||||||
|
// On met le dernier caractère comme étant le null terminator: \0
|
||||||
content[bytesRead] = 0;
|
content[bytesRead] = 0;
|
||||||
|
|
||||||
|
// On ferme évidemment le fichier.
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
@@ -98,28 +97,30 @@ void free_response(HttpResponse *__res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
HttpServerRunStatus cgi(const char *__path, int clientfd) {
|
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) {
|
if (pid < 0) {
|
||||||
// Error handling for fork failure
|
|
||||||
perror("fork failed");
|
perror("fork failed");
|
||||||
return HTTP_SRS_FORK_FAILED;
|
return HTTP_SRS_FORK_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pid == 0) {
|
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) {
|
if (dup2(clientfd, STDOUT_FILENO) == -1) {
|
||||||
perror("dup2");
|
perror("dup2");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
// On execute le script python.
|
||||||
// Close the read end of the pipe (it's no longer needed after
|
|
||||||
// redirection)
|
|
||||||
char path[1024] = {0};
|
|
||||||
sprintf(path, ".%s", __path);
|
|
||||||
execl("/usr/bin/python3", "python3", path, NULL);
|
execl("/usr/bin/python3", "python3", path, NULL);
|
||||||
|
|
||||||
perror("Exec failed");
|
perror("Exec failed");
|
||||||
|
|||||||
@@ -22,15 +22,16 @@ typedef enum {
|
|||||||
HTTP_SRS_HANDLE_REQUEST_FAILED,
|
HTTP_SRS_HANDLE_REQUEST_FAILED,
|
||||||
HTTP_SRS_FORK_FAILED,
|
HTTP_SRS_FORK_FAILED,
|
||||||
HTTP_SRS_WONT_HANDLE,
|
HTTP_SRS_WONT_HANDLE,
|
||||||
|
HTTP_SRS_FILE_NOT_FOUND,
|
||||||
} HttpServerRunStatus;
|
} HttpServerRunStatus;
|
||||||
|
|
||||||
// Structure ayant pour objectif de stocker tout ce qui est nécessaire au bon
|
// Structure ayant pour objectif de stocker tout ce qui est nécessaire au bon
|
||||||
// fonctionnement du serveur.
|
// fonctionnement du serveur.
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int port; // Port du host
|
int port; // Port du host
|
||||||
int server_fd; // File descriptor du server
|
int server_fd; // File descriptor du server
|
||||||
unsigned int backlog; // Le nombre de clients possibles pouvant être en
|
unsigned int
|
||||||
// attente.
|
backlog; // Le nombre de clients possibles pouvant être en attente.
|
||||||
struct sockaddr_in *address; // L'adresse décrivant une socket internet.
|
struct sockaddr_in *address; // L'adresse décrivant une socket internet.
|
||||||
pid_t pid; // Le pid du parent en attente de requêtes.
|
pid_t pid; // Le pid du parent en attente de requêtes.
|
||||||
} HttpServer;
|
} HttpServer;
|
||||||
|
|||||||
Reference in New Issue
Block a user