fix: even more night fixes (seems like a friday)

This commit is contained in:
Gu://em_ 2025-11-27 01:01:03 +01:00
parent 7633ac9de5
commit c675e3253b
18 changed files with 111 additions and 44 deletions

View file

@ -3,8 +3,10 @@
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "../server/server.h" #include "../server/server.h"
#include "../utils/files/files.h"
static struct config *config; static struct config *config;
@ -42,8 +44,24 @@ void stop_daemon(void)
} }
int start_daemon(void) int start_daemon(void)
{
pid_t pid = fork();
if (!pid) // Daemon
{ {
start_server("localhost", config->servers->port); start_server("localhost", config->servers->port);
}
else
{
// Write pid
int err = write_pid(config->pid_file, pid);
if (err != 0)
{
kill(pid, SIGINT);
return 1;
}
exit(1);
}
return 0; return 0;
} }

View file

@ -326,10 +326,6 @@ struct string *format_response(struct http_response *resp)
string_concat_str(res, "\r\n", 2); string_concat_str(res, "\r\n", 2);
// Time
char *time = get_time();
string_concat_str(res, time, strlen(time));
// Headers // Headers
struct http_header *cur_header = resp->headers; struct http_header *cur_header = resp->headers;
while (cur_header != NULL) while (cur_header != NULL)
@ -337,6 +333,11 @@ struct string *format_response(struct http_response *resp)
string_concat_str(res, cur_header->field->data, string_concat_str(res, cur_header->field->data,
cur_header->field->size); cur_header->field->size);
string_concat_str(res, ": ", 2);
string_concat_str(res, cur_header->value->data,
cur_header->value->size);
string_concat_str(res, "\r\n", 2); string_concat_str(res, "\r\n", 2);
cur_header = cur_header->next; cur_header = cur_header->next;

View file

@ -13,8 +13,8 @@
// === Includes // === Includes
#include "../utils/string/string.h"
#include "../config/config.h" #include "../config/config.h"
#include "../utils/string/string.h"
// === Enums // === Enums

View file

@ -44,8 +44,7 @@ void log_request(char* request_type, char* target, char* client_ip);
* @param target * @param target
* @param client_ip * @param client_ip
*/ */
void log_response(int status_code, char* request_type, char* target, char* client_ip); void log_response(int status_code, char *request_type, char *target,
char *client_ip);
#endif // ! LOGS_H #endif // ! LOGS_H

View file

@ -18,6 +18,7 @@ int main(int argc, char **argv)
// Initialize modules // Initialize modules
log_init(config); // Ignore ret val log_init(config); // Ignore ret val
http_init(config); http_init(config);
daemon_init(config);
// Start server // Start server
switch (config->daemon) switch (config->daemon)

View file

@ -1,7 +1,6 @@
#include "files.h" #include "files.h"
#include <stdio.h> #include <stdio.h>
// #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
// #include "../string/string.h" // #include "../string/string.h"
@ -59,3 +58,33 @@ ssize_t get_file_content_size(const char *path)
return res; return res;
} }
int write_to_file(const char *path, struct string *buf)
{
FILE *stream = fopen(path, "w");
if (stream == NULL)
return 1;
fwrite(buf->data, sizeof(char), buf->size, stream);
fclose(stream);
return 0;
}
int write_pid(const char *filepath, int pid)
{
FILE *stream = fopen(filepath, "w");
if (stream == NULL)
return 1;
if (fprintf(stream, "%d", pid) <= 0)
{
fclose(stream);
return 1;
}
fclose(stream);
return 0;
}

View file

@ -42,7 +42,6 @@ int is_directory(const char* path);
*/ */
char *get_file(const char *path); char *get_file(const char *path);
/* /*
* @brief * @brief
* *
@ -68,7 +67,7 @@ bool check_filename(struct string* path);
* *
* @return * @return
*/ */
bool sanitize_filename(struct string* filename); // bool sanitize_filename(struct string *filename);
/* /*
* @brief * @brief
@ -79,4 +78,22 @@ bool sanitize_filename(struct string* filename);
*/ */
ssize_t get_file_content_size(const char *path); ssize_t get_file_content_size(const char *path);
/*
* @brief
*
* @param path
*
* @return 0 on success, the corresponding error code otherwise
*/
int write_to_file(const char *path, struct string* buf);
/*
* @brief
*
* @param path
*
* @return 0 on success, the corresponding error code otherwise
*/
int write_pid(const char *filepath, int pid);
#endif // ! FILES_H #endif // ! FILES_H

View file

@ -8,8 +8,9 @@
// === Includes // === Includes
#include <stdbool.h> #include <stdbool.h>
#include <sys/types.h>
#include <stddef.h> #include <stddef.h>
#include <sys/types.h>
#include "../string/string.h" #include "../string/string.h"
// === Functions // === Functions

View file

@ -1,12 +1,12 @@
#ifndef WORDS_H #ifndef WORDS_H
#define WORDS_H #define WORDS_H
// === Includes // === Includes
#include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
#include "../string/string.h" #include "../string/string.h"
// === Functions // === Functions
@ -30,8 +30,8 @@ ssize_t read_word_delim(struct string *str, size_t offset, struct string **res,
/* /*
* Doc: TODO * Doc: TODO
*/ */
ssize_t read_word_restrict(struct string *str, size_t offset, struct string **res, ssize_t read_word_restrict(struct string *str, size_t offset,
const char *restr); struct string **res, const char *restr);
/* /*
* Doc: TODO * Doc: TODO

View file

@ -1,6 +1,7 @@
[global] [global]
log = true log = true
pid_file = /tmp/HTTPd.pid pid_file = /tmp/HTTPd.pid
daemon = start
[[vhosts]] [[vhosts]]
server_name = my_server server_name = my_server