Parsing of simple requests. HashMap.
This commit is contained in:
@@ -7,30 +7,79 @@
|
||||
#define SIMPLEHTTPSERVER_H
|
||||
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include "utils/hashmap.h"
|
||||
|
||||
struct HttpRequest
|
||||
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
|
||||
};
|
||||
|
||||
struct HttpResponse
|
||||
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
|
||||
};
|
||||
|
||||
struct HttpServer
|
||||
typedef struct http_request
|
||||
{
|
||||
struct HttpResponse* (*handleRequest)(struct HttpRequest* 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;
|
||||
|
||||
void start_http_server(struct HttpServer* http_server, const char *addr, short port);
|
||||
void process_conn(struct HttpServer* http_server, int server_fd, struct sockaddr* address, socklen_t* addr_len);
|
||||
typedef struct http_response
|
||||
{
|
||||
http_version_t http_version;
|
||||
u_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(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);
|
||||
|
||||
#endif //SIMPLEHTTPSERVER_H
|
||||
Reference in New Issue
Block a user