httpd/src/http/http.h
2026-06-24 16:56:11 +02:00

118 lines
2.4 KiB
C

#ifndef HTTP_H
#define HTTP_H
// === Definitions
// #define _POSIX_C_SOURCE 200809L
// Error codes
#define ERR_HTTP_INVALID_INPUT -1
#define ERR_HTTP_NOT_IMPLEMENTED -2
#define ERR_HTTP_OUT_OF_MEMORY -4
#define ERR_HTTP_INTERNAL_ERROR -5
// === Includes
#include "../config/config.h"
#include "../utils/string/string.h"
// === Enums
enum http_method
{
INVALID_METHOD,
GET,
// POST,
// PUT,
// DELETE,
// PATCH,
HEAD,
// OPTIONS,
// CONNECT,
// TRACE
};
// === Structures
struct http_header
{
struct string *field;
struct string *value;
struct http_header *next;
};
struct http_request
{
enum http_method method;
struct string *target;
struct string *queries;
struct string *protocol;
struct http_header *headers; // Headers linked list
int status_code; // Set by program
};
struct http_response
{
struct string *protocol;
int status_code;
struct string *status_msg;
struct http_header *headers; // Headers linked list
};
// === Functions
/* @brief Initializes the HTTP module with the given configuration
*
* @warn Do no use any other function of this module before calling that one
*
* @param cfg
*/
void http_init(struct config *cfg);
/* @brief Reads, parses the request and responds adequately all-in-one
*
* @param client_fd
*/
void handle_request(int client_fd, char *client_ip);
/* @brief Parses the HTTP request and splits it into a request structure
*
* @param req
*
* @return A pointer to the structure containing the request infos on success,
* NULL otherwise
*/
struct http_request *parse_request(struct string *req);
/* @brief Generates a response to the given request
*
* @param req
*
* @return A pointer to the generated response struct on success,
* NULL otherwise
*/
struct http_response *generate_response(struct http_request *req);
/* @brief Formats the given response structure into a valid HTTP response
* string
*
* @param resp
*
* @return A pointer to the string containing the response on success,
* NULL otherwise
*/
struct string *format_response(struct http_response *resp);
/* @brief Free all allocated memory inside req and req itself
*
* @param req
*/
void destroy_request(struct http_request *req);
/* @brief Free all allocated memory inside resp and resp itself
*
* @param resp
*/
void destroy_response(struct http_response *resp);
#endif // ! HTTP_H