refactor(req/res): new request/response structure
This commit is contained in:
20
include/http/handler.h
Normal file
20
include/http/handler.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef CWS_HANDLER_H
|
||||
#define CWS_HANDLER_H
|
||||
|
||||
#include "http/request.h"
|
||||
#include "http/response.h"
|
||||
|
||||
/* Configuration for static file serving */
|
||||
typedef struct cws_handler_config {
|
||||
const char *root_dir; /*< "www" */
|
||||
const char *index_file; /*< "index.html" */
|
||||
} cws_handler_config_s;
|
||||
|
||||
/* Static file handler */
|
||||
cws_response_s *cws_handler_static_file(cws_request_s *request, cws_handler_config_s *config);
|
||||
|
||||
/* Error handlers */
|
||||
cws_response_s *cws_handler_not_found(cws_request_s *request);
|
||||
cws_response_s *cws_handler_not_implemented(cws_request_s *request);
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef CWS_REQUEST_H
|
||||
#define CWS_REQUEST_H
|
||||
|
||||
#include "http/types.h"
|
||||
#include <myclib/myhashmap.h>
|
||||
#include <myclib/mystring.h>
|
||||
#include <stddef.h>
|
||||
@@ -9,28 +10,13 @@
|
||||
#define CWS_HTTP_HEADER_MAX 512
|
||||
#define CWS_HTTP_HEADER_CONTENT_MAX 1024
|
||||
|
||||
typedef enum cws_http_method {
|
||||
HTTP_GET,
|
||||
HTTP_POST,
|
||||
HTTP_PUT,
|
||||
HTTP_DELETE,
|
||||
HTTP_HEAD,
|
||||
HTTP_UNKNOWN,
|
||||
} cws_http_method_e;
|
||||
|
||||
typedef enum cws_http_status {
|
||||
HTTP_OK,
|
||||
HTTP_NOT_FOUND,
|
||||
HTTP_NOT_IMPLEMENTED,
|
||||
} cws_http_status_e;
|
||||
|
||||
typedef struct cws_request {
|
||||
int sockfd;
|
||||
cws_http_method_e method;
|
||||
string_s *location;
|
||||
string_s *location_path;
|
||||
string_s *path;
|
||||
string_s *query_string;
|
||||
string_s *http_version;
|
||||
hashmap_s *headers;
|
||||
string_s *body;
|
||||
} cws_request_s;
|
||||
|
||||
cws_request_s *cws_http_parse(string_s *request_str);
|
||||
|
||||
@@ -1,8 +1,39 @@
|
||||
#ifndef CWS_RESPONSE_H
|
||||
#define CWS_RESPONSE_H
|
||||
|
||||
#include "http/request.h"
|
||||
#include "http/types.h"
|
||||
#include <myclib/myhashmap.h>
|
||||
#include <myclib/mystring.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void cws_http_send_response(cws_request_s *request, cws_http_status_e status);
|
||||
typedef enum cws_response_body_type {
|
||||
RESPONSE_BODY_NONE,
|
||||
RESPONSE_BODY_STRING,
|
||||
RESPONSE_BODY_FILE,
|
||||
} cws_response_body_type_e;
|
||||
|
||||
typedef struct cws_response {
|
||||
cws_http_status_e status;
|
||||
hashmap_s *headers;
|
||||
|
||||
/* Body handling */
|
||||
cws_response_body_type_e body_type;
|
||||
string_s *body_string;
|
||||
FILE *body_file;
|
||||
size_t content_length;
|
||||
} cws_response_s;
|
||||
|
||||
cws_response_s *cws_response_new(cws_http_status_e status);
|
||||
void cws_response_free(cws_response_s *response);
|
||||
|
||||
void cws_response_set_header(cws_response_s *response, const char *key, const char *value);
|
||||
void cws_response_set_body_string(cws_response_s *response, const char *body);
|
||||
void cws_response_set_body_file(cws_response_s *response, const char *filepath);
|
||||
|
||||
cws_response_s *cws_response_html(cws_http_status_e status, const char *title, const char *body);
|
||||
cws_response_s *cws_response_error(cws_http_status_e status, const char *message);
|
||||
|
||||
int cws_response_send(int sockfd, cws_response_s *response);
|
||||
|
||||
#endif
|
||||
|
||||
27
include/http/types.h
Normal file
27
include/http/types.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef CWS_HTTP_TYPES_H
|
||||
#define CWS_HTTP_TYPES_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* HTTP Methods */
|
||||
typedef enum cws_http_method {
|
||||
HTTP_GET,
|
||||
HTTP_POST,
|
||||
HTTP_PUT,
|
||||
HTTP_DELETE,
|
||||
HTTP_HEAD,
|
||||
HTTP_UNKNOWN,
|
||||
} cws_http_method_e;
|
||||
|
||||
/* HTTP Status Codes */
|
||||
typedef enum cws_http_status {
|
||||
HTTP_OK = 200,
|
||||
HTTP_BAD_REQUEST = 400,
|
||||
HTTP_NOT_FOUND = 404,
|
||||
HTTP_INTERNAL_ERROR = 500,
|
||||
HTTP_NOT_IMPLEMENTED = 501,
|
||||
} cws_http_status_e;
|
||||
|
||||
const char *cws_http_status_string(cws_http_status_e status);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user