2025-11-21 16:09:48 +01:00
|
|
|
#ifndef HTTP_H
|
|
|
|
|
#define HTTP_H
|
|
|
|
|
|
2025-11-21 19:39:15 +01:00
|
|
|
#include "../utils/string/string.h"
|
|
|
|
|
|
2025-11-21 16:09:48 +01:00
|
|
|
// === Enums
|
|
|
|
|
|
|
|
|
|
enum http_method
|
|
|
|
|
{
|
|
|
|
|
GET,
|
|
|
|
|
POST,
|
|
|
|
|
PUT,
|
|
|
|
|
DELETE,
|
2025-11-21 19:39:15 +01:00
|
|
|
// PATCH,
|
|
|
|
|
// HEAD,
|
|
|
|
|
// OPTIONS,
|
|
|
|
|
// CONNECT,
|
|
|
|
|
// TRACE
|
2025-11-21 16:09:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// === Structures
|
|
|
|
|
|
|
|
|
|
struct http_header
|
|
|
|
|
{
|
2025-11-21 19:39:15 +01:00
|
|
|
struct string *field;
|
|
|
|
|
struct string *value;
|
2025-11-21 16:09:48 +01:00
|
|
|
struct http_header *next;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct http_request
|
|
|
|
|
{
|
|
|
|
|
enum http_method method;
|
2025-11-21 19:39:15 +01:00
|
|
|
struct string *path;
|
|
|
|
|
struct string *protocol;
|
|
|
|
|
struct string *protocol_version;
|
2025-11-21 16:09:48 +01:00
|
|
|
struct http_header *headers; // Headers linked list
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct http_response
|
|
|
|
|
{
|
2025-11-21 19:39:15 +01:00
|
|
|
struct string *protocol;
|
|
|
|
|
struct string *protocol_version;
|
2025-11-21 16:09:48 +01:00
|
|
|
int status_code;
|
2025-11-21 19:39:15 +01:00
|
|
|
struct string *status_msg;
|
2025-11-21 16:09:48 +01:00
|
|
|
struct http_header *headers; // Headers linked list
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-21 20:10:26 +01:00
|
|
|
// === Functions
|
|
|
|
|
|
|
|
|
|
/* @brief Parses the HTTP request and splits it into a request structure
|
|
|
|
|
*
|
|
|
|
|
* @params 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
|
|
|
|
|
*
|
|
|
|
|
* @params 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
|
|
|
|
|
*
|
|
|
|
|
* @params resp
|
|
|
|
|
*
|
|
|
|
|
* @return A pointer to the string containing the response on success,
|
|
|
|
|
* NULL otherwise
|
|
|
|
|
*/
|
|
|
|
|
struct string* format_response(struct http_response* resp);
|
|
|
|
|
|
2025-11-21 16:09:48 +01:00
|
|
|
|
|
|
|
|
#endif // ! HTTP_H
|