initial http parsing

This commit is contained in:
2024-11-11 00:50:39 +01:00
parent 7db494eaaf
commit e5e40c795c
17 changed files with 176 additions and 32 deletions

48
include/http/http.h Normal file
View File

@@ -0,0 +1,48 @@
#ifndef __HTTP_H__
#define __HTTP_H__
#include <stdio.h> /* Debug */
#include <stdlib.h>
#include <string.h>
#define WWW "../www/" /**< Directory used to get html files */
/** In the future I'll move conf stuff under a server struct, I can skip just because I want something that works */
#define LOCATION_LEN 1024
#define HTTP_VERSION_LEN 8
#define USER_AGENT_LEN 1024
#define HOST_LEN 1024
enum http_method {
GET, /**< GET method */
POST, /**< POST method */
};
/* In the future I'll add HEAD, PUT, DELETE */
/**
* @brief HTTP request struct
*
*/
typedef struct http {
enum http_method method; /**< HTTP request method */
char location[LOCATION_LEN]; /**< Resource requested */
char http_version[HTTP_VERSION_LEN]; /**< HTTP version */
char user_agent[USER_AGENT_LEN]; /**< User-Agent */
char host[HOST_LEN]; /**< Host */
} http_t;
/* Connection */
/* Accept-Encoding */
/* Accept-Language */
/**
* @brief Parses a HTTP request
*
* @param request[in] The http request sent to the server
* @return Returns a http_t pointer to the request
*/
http_t *http_parse(char *request_str);
void http_parse_method(http_t *request, const char *method);
void http_free(http_t *request);
void http_send_response(http_t *request);
#endif

View File

@@ -14,7 +14,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "hashmap.h"
#include "utils/hashmap.h"
/* On which port the server will run */
#define PORT 3030
@@ -33,12 +33,12 @@
*
* @param hostname[in] The hostname of the server (default localhost, it could be NULL)
* @param service[in] The service (found in /etc/services) or the port where to run
* @return int 0 on success, -1 on error
* @return 0 on success, -1 on error
*/
int start_server(const char *hostname, const char *service);
/**
* @brief Sets the up hints object
* @brief Setups hints object
*
* @param hints[out] The hints addrinfo
* @param len[in] The length of hints
@@ -62,7 +62,6 @@ void handle_clients(int sockfd);
*/
void epoll_ctl_add(int epfd, int sockfd, uint32_t events);
/* Remove a file descriptor from the interest list */
/**
* @brief Removes a file descriptor from the interest list
*
@@ -84,7 +83,7 @@ void setnonblocking(int sockfd);
* @param sockfd[in] Server's file descriptor
* @param their_sa[out] Populates the struct with client's information
* @param theirsa_size[in] Size of the struct
* @return int Returns -1 on error or the file descriptor on success
* @return Returns -1 on error or the file descriptor on success
*/
int handle_new_client(int sockfd, struct sockaddr_storage *their_sa, socklen_t *theirsa_size);

View File

@@ -9,18 +9,22 @@
/* Each process on Linux can have a maximum of 1024 open file descriptors */
#define HASHMAP_MAX_CLIENTS 1024
/**
* @brief Hash map struct
*
*/
typedef struct bucket {
int sockfd; /**< Client socket descriptor */
int sockfd; /**< Client socket descriptor */
struct sockaddr_storage sas; /**< Associated socket address */
struct bucket *next; /**< Next node in case of collision */
struct bucket *prev; /**< Previous node in case of collision */
struct bucket *next; /**< Next node in case of collision */
struct bucket *prev; /**< Previous node in case of collision */
} bucket_t;
/**
* @brief Calculates the hash code of a given file descriptor
*
* @param sockfd[in] The file descriptor
* @return int Returns the hash code
* @return Returns the hash code
*/
int hash(int sockfd);
@@ -53,7 +57,7 @@ void hm_remove(bucket_t *bucket, int sockfd);
*
* @param bucket[in] The hash map
* @param sockfd[in] The file descriptor (key)
* @return struct hashmap* Returns NULL or the key pointer
* @return Returns NULL or the key pointer
*/
bucket_t *hm_lookup(bucket_t *bucket, int sockfd);