CGI
This commit is contained in:
@@ -4,12 +4,12 @@
|
||||
#include "http_response.h"
|
||||
#include "http_status.h"
|
||||
#include <asm-generic/socket.h>
|
||||
#include <malloc.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
HttpServerRunStatus http_server_setup(HttpServer *s) {
|
||||
@@ -49,19 +49,9 @@ HttpServerRunStatus http_server_run(HttpServer *s) {
|
||||
accept(s->server_fd, (struct sockaddr *)s->address, &addrlen)) < 0)
|
||||
return HTTP_SRS_ACCEPT_FAILED;
|
||||
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
close(client_fd);
|
||||
return HTTP_SRS_FORK_FAILED;
|
||||
}
|
||||
if (pid > 0) {
|
||||
close(client_fd);
|
||||
return HTTP_SRS_WONT_HANDLE;
|
||||
}
|
||||
|
||||
char request[BUFSIZ] = {0};
|
||||
|
||||
if ((valread = read(client_fd, request, 1024 - 1)) < 0)
|
||||
if ((valread = recv(client_fd, request, 1024 - 1, MSG_PEEK)) < 0)
|
||||
return HTTP_SRS_READ_FAILED;
|
||||
|
||||
HttpRequest *req = handle_request(request);
|
||||
@@ -72,6 +62,8 @@ HttpServerRunStatus http_server_run(HttpServer *s) {
|
||||
|
||||
if (!strcmp(req->path, "/")) {
|
||||
res = from_file("./" DEFAULT_HTML);
|
||||
} else if (!strncmp(req->path, "/cgi/", strlen("/cgi/"))) {
|
||||
return cgi(req->path, client_fd);
|
||||
} else {
|
||||
char path[] = ".";
|
||||
strcat(path, req->path);
|
||||
@@ -80,12 +72,18 @@ HttpServerRunStatus http_server_run(HttpServer *s) {
|
||||
// print_request(req);
|
||||
// free_request(req);
|
||||
if (res == NULL) {
|
||||
char *body = malloc(sizeof(char) * 128);
|
||||
body = "<!DOCTYPE html><html><body><h1>404 Not "
|
||||
"Found</h1></body></html>";
|
||||
HttpResponse __res = {
|
||||
.status_code = HTTP_NOT_FOUND,
|
||||
.body = "",
|
||||
.content_type = HTTP_CT_PLAIN_TEXT,
|
||||
.content_length = 1,
|
||||
.body_in_heap = false,
|
||||
.content_length = strlen(body),
|
||||
.content_type = HTTP_CT_HTML,
|
||||
.body = body,
|
||||
.body_in_heap = false, // Lorsqu'on essaie de libérer le body
|
||||
// sur cette instance, le server crash.
|
||||
// Alors, on ne le libère pas et ça cause
|
||||
// des memory leaks.
|
||||
};
|
||||
res = malloc(sizeof(__res));
|
||||
*res = __res;
|
||||
@@ -94,7 +92,6 @@ HttpServerRunStatus http_server_run(HttpServer *s) {
|
||||
http_respond(res, client_fd);
|
||||
free_response(res);
|
||||
close(client_fd);
|
||||
|
||||
return HTTP_SRS_RUNNING;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user