refactor(http): change cws_http_s to cws_request_s
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
#define CWS_SERVER_H
|
#define CWS_SERVER_H
|
||||||
|
|
||||||
#include <myclib/myhashmap.h>
|
#include <myclib/myhashmap.h>
|
||||||
#include <myclib/mysocket.h>
|
#include <netdb.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
#include "config/config.h"
|
#include "config/config.h"
|
||||||
|
|||||||
@@ -24,17 +24,17 @@ typedef enum cws_http_status {
|
|||||||
HTTP_NOT_IMPLEMENTED,
|
HTTP_NOT_IMPLEMENTED,
|
||||||
} cws_http_status_e;
|
} cws_http_status_e;
|
||||||
|
|
||||||
typedef struct cws_http {
|
typedef struct cws_request {
|
||||||
int sockfd;
|
int sockfd;
|
||||||
cws_http_method_e method;
|
cws_http_method_e method;
|
||||||
string_s *location;
|
string_s *location;
|
||||||
string_s *location_path;
|
string_s *location_path;
|
||||||
string_s *http_version;
|
string_s *http_version;
|
||||||
hashmap_s *headers;
|
hashmap_s *headers;
|
||||||
} cws_http_s;
|
} cws_request_s;
|
||||||
|
|
||||||
cws_http_s *cws_http_parse(string_s *request_str);
|
cws_request_s *cws_http_parse(string_s *request_str);
|
||||||
|
|
||||||
void cws_http_free(cws_http_s *request);
|
void cws_http_free(cws_request_s *request);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,6 +9,6 @@ size_t http_simple_html(char **response, cws_http_status_e status, char *title,
|
|||||||
size_t http_response_builder(char **response, cws_http_status_e status, char *content_type,
|
size_t http_response_builder(char **response, cws_http_status_e status, char *content_type,
|
||||||
char *body, size_t body_len_bytes);
|
char *body, size_t body_len_bytes);
|
||||||
|
|
||||||
void cws_http_send_response(cws_http_s *request, cws_http_status_e status);
|
void cws_http_send_response(cws_request_s *request, cws_http_status_e status);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ static int cws_server_handle_client_data(int epfd, int client_fd) {
|
|||||||
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
|
return CWS_SERVER_CLIENT_DISCONNECTED_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_http_s *request = cws_http_parse(data);
|
cws_request_s *request = cws_http_parse(data);
|
||||||
string_free(data);
|
string_free(data);
|
||||||
if (request == NULL) {
|
if (request == NULL) {
|
||||||
cws_server_close_client(epfd, client_fd);
|
cws_server_close_client(epfd, client_fd);
|
||||||
@@ -54,7 +54,7 @@ static int cws_server_handle_client_data(int epfd, int client_fd) {
|
|||||||
cws_http_send_response(request, HTTP_OK);
|
cws_http_send_response(request, HTTP_OK);
|
||||||
|
|
||||||
cws_http_free(request);
|
cws_http_free(request);
|
||||||
// cws_server_close_client(epfd, client_fd);
|
cws_server_close_client(epfd, client_fd);
|
||||||
|
|
||||||
return CWS_SERVER_OK;
|
return CWS_SERVER_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
#include "utils/debug.h"
|
#include "utils/debug.h"
|
||||||
#include "utils/hash.h"
|
#include "utils/hash.h"
|
||||||
|
|
||||||
static cws_http_s *http_new() {
|
static cws_request_s *http_new() {
|
||||||
cws_http_s *request = malloc(sizeof(cws_http_s));
|
cws_request_s *request = malloc(sizeof *request);
|
||||||
if (!request) {
|
if (!request) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -34,7 +34,7 @@ static cws_http_method_e http_parse_method(const char *method) {
|
|||||||
return HTTP_UNKNOWN;
|
return HTTP_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool parse_method(cws_http_s *req, char **cursor) {
|
static bool parse_method(cws_request_s *req, char **cursor) {
|
||||||
char *s = *cursor + strspn(*cursor, " ");
|
char *s = *cursor + strspn(*cursor, " ");
|
||||||
size_t len = strcspn(s, " ");
|
size_t len = strcspn(s, " ");
|
||||||
if (len == 0 || s[len] == '\0') {
|
if (len == 0 || s[len] == '\0') {
|
||||||
@@ -49,7 +49,7 @@ static bool parse_method(cws_http_s *req, char **cursor) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool parse_location(cws_http_s *req, char **cursor) {
|
static bool parse_location(cws_request_s *req, char **cursor) {
|
||||||
char *s = *cursor + strspn(*cursor, " ");
|
char *s = *cursor + strspn(*cursor, " ");
|
||||||
size_t len = strcspn(s, " ");
|
size_t len = strcspn(s, " ");
|
||||||
if (len == 0 || s[len] == '\0') {
|
if (len == 0 || s[len] == '\0') {
|
||||||
@@ -64,7 +64,7 @@ static bool parse_location(cws_http_s *req, char **cursor) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool parse_version(cws_http_s *req, char **cursor) {
|
static bool parse_version(cws_request_s *req, char **cursor) {
|
||||||
char *s = *cursor + strspn(*cursor, " \t");
|
char *s = *cursor + strspn(*cursor, " \t");
|
||||||
size_t len = strcspn(s, "\r\n");
|
size_t len = strcspn(s, "\r\n");
|
||||||
if (len == 0 || s[len] == '\0') {
|
if (len == 0 || s[len] == '\0') {
|
||||||
@@ -79,7 +79,7 @@ static bool parse_version(cws_http_s *req, char **cursor) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool parse_headers(cws_http_s *req, char **cursor) {
|
static bool parse_headers(cws_request_s *req, char **cursor) {
|
||||||
req->headers =
|
req->headers =
|
||||||
hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn,
|
hm_new(my_str_hash_fn, my_str_equal_fn, my_str_free_fn, my_str_free_fn,
|
||||||
sizeof(char) * CWS_HTTP_HEADER_MAX, sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX);
|
sizeof(char) * CWS_HTTP_HEADER_MAX, sizeof(char) * CWS_HTTP_HEADER_CONTENT_MAX);
|
||||||
@@ -125,12 +125,12 @@ static bool parse_headers(cws_http_s *req, char **cursor) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_http_s *cws_http_parse(string_s *request_str) {
|
cws_request_s *cws_http_parse(string_s *request_str) {
|
||||||
if (!request_str || !request_str->data) {
|
if (!request_str || !request_str->data) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
cws_http_s *request = http_new();
|
cws_request_s *request = http_new();
|
||||||
if (!request) {
|
if (!request) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -173,7 +173,7 @@ cws_http_s *cws_http_parse(string_s *request_str) {
|
|||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cws_http_free(cws_http_s *request) {
|
void cws_http_free(cws_request_s *request) {
|
||||||
if (!request) {
|
if (!request) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "http/response.h"
|
#include "http/response.h"
|
||||||
|
|
||||||
#include "http/mime.h"
|
#include "http/mime.h"
|
||||||
|
#include "http/request.h"
|
||||||
#include "utils/debug.h"
|
#include "utils/debug.h"
|
||||||
#include <core/socket.h>
|
#include <core/socket.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@@ -99,8 +100,7 @@ static size_t file_data(const char *path, char **data) {
|
|||||||
return content_length;
|
return content_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void http_send_resource(cws_http_s *request) {
|
static void http_send_resource(cws_request_s *request) {
|
||||||
/* Retrieve correct Content-Type */
|
|
||||||
char content_type[CWS_HTTP_CONTENT_TYPE];
|
char content_type[CWS_HTTP_CONTENT_TYPE];
|
||||||
http_get_content_type(request->location_path->data, content_type);
|
http_get_content_type(request->location_path->data, content_type);
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
|
|||||||
return total_len;
|
return total_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cws_http_send_response(cws_http_s *request, cws_http_status_e status) {
|
void cws_http_send_response(cws_request_s *request, cws_http_status_e status) {
|
||||||
char *response = NULL;
|
char *response = NULL;
|
||||||
|
|
||||||
switch (status) {
|
switch (status) {
|
||||||
|
|||||||
Reference in New Issue
Block a user