Files
SimpleHttpServer/src/httpserver.h

87 lines
1.9 KiB
C

//
// Created by nazar on 12.09.2025.
//
// https://datatracker.ietf.org/doc/html/rfc9112#name-message
#ifndef SIMPLEHTTPSERVER_H
#define SIMPLEHTTPSERVER_H
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include "utils/hashmap.h"
#include "utils/string_builder.h"
typedef struct http_version
{
char* http_name; // has to end with "HTTP" btw
size_t http_name_len;
unsigned short major;
unsigned short minor;
} http_version_t;
enum http_method
{
UNKNOWN,
GET,
HEAD,
POST,
PUT,
DELETE,
CONNECT,
OPTIONS,
TRACE,
PATCH
};
enum http_request_parsing_stage
{
START_LINE_METHOD,
START_LINE_PATH,
START_LINE_HTTP_VERSION_NAME,
START_LINE_HTTP_VERSION_MAJOR,
START_LINE_HTTP_VERSION_MINOR,
HEADERS_KEY,
HEADERS_VALUE,
CONTENT
};
typedef struct http_request
{
enum http_method http_method;
char* http_path;
size_t http_path_len;
http_version_t http_version;
hashmap_t* http_headers;
char* http_content;
size_t http_content_len;
} http_request_t;
typedef struct http_response
{
http_version_t http_version;
unsigned short http_status_code;
char* http_status_message;
size_t http_status_message_len;
hashmap_t* http_headers;
char* http_content;
size_t http_content_len;
} http_response_t;
typedef struct http_server
{
http_response_t* (*handle_request)(http_request_t* request);
} http_server_t;
void start_http_server(http_server_t* http_server, const char *addr, short port);
void process_conn(const http_server_t* http_server, int server_fd, struct sockaddr* address, socklen_t* addr_len);
enum http_method parse_method_str(const char* str, size_t len);
const char* http_method_to_string(enum http_method m);
#endif //SIMPLEHTTPSERVER_H