This commit is contained in:
cdricms
2024-11-19 16:28:54 +01:00
parent a4f779d5d7
commit 7b52f5de7d
7 changed files with 71 additions and 33 deletions

View File

@@ -1,6 +1,5 @@
#include "http_response.h"
#include "http_content_type.h"
#include "http_status.h"
#include "http_server.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@@ -18,15 +17,13 @@ bool construct_response(HttpResponse __res, char *out) {
return false;
}
char status_message[256];
http_status_message(__res.status_code, status_message, 256);
// char status_message[256];
// http_status_message(__res.status_code, status_message, 256);
char content_type[256];
http_content_type(__res.content_type, content_type, 256);
sprintf(
out,
"HTTP/1.1 %d %s\r\nContent-Type: %s\r\nContent-Length: %lu\r\n\r\n%s",
__res.status_code, status_message, content_type, __res.content_length,
__res.body);
sprintf(out,
"HTTP/1.1 %d\r\nContent-Type: %s\r\nContent-Length: %lu\r\n\r\n%s",
__res.status_code, content_type, __res.content_length, __res.body);
return true;
}
@@ -38,7 +35,7 @@ void http_respond(HttpResponse *__res, int clientfd) {
// TODO: Handle return
construct_response(*__res, response);
send(clientfd, response, strlen(response), 0);
send(clientfd, response, strlen(response), MSG_EOR);
}
char *read_file(const char *__path) {
@@ -98,3 +95,37 @@ void free_response(HttpResponse *__res) {
free(__res);
}
HttpServerRunStatus cgi(const char *__path, int clientfd) {
pid_t pid;
pid = fork(); // Create a child process
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
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);
execl("/usr/bin/python3", "python3", path, NULL);
perror("Exec failed");
exit(EXIT_FAILURE);
} else {
wait(NULL);
close(clientfd);
return HTTP_SRS_RUNNING;
}
}