fix: send response to client
This commit is contained in:
@@ -22,6 +22,7 @@ static cws_http_s *http_new() {
|
||||
|
||||
request->http_version = string_new("", 16);
|
||||
request->location = string_new("", 128);
|
||||
request->location_path = string_new("", 128);
|
||||
|
||||
return request;
|
||||
}
|
||||
@@ -89,6 +90,7 @@ static size_t file_data(const char *path, char **data) {
|
||||
/* Retrieve file size */
|
||||
fseek(file, 0, SEEK_END);
|
||||
const size_t content_length = ftell(file);
|
||||
errno = 0;
|
||||
rewind(file);
|
||||
if (errno != 0) {
|
||||
fclose(file);
|
||||
@@ -198,16 +200,16 @@ cws_http_s *cws_http_parse(string_s *request_str) {
|
||||
|
||||
return NULL;
|
||||
}
|
||||
CWS_LOG_DEBUG("location: %s", pch);
|
||||
string_append(request->location, pch);
|
||||
// TODO: fix www
|
||||
request->location_path = string_format("%s/%s", "www", request->location->data);
|
||||
|
||||
/* Adjust location path */
|
||||
if (strcmp(string_cstr(request->location), "/") == 0) {
|
||||
string_append(request->location_path, "/index.html");
|
||||
string_append(request->location_path, "www/");
|
||||
CWS_LOG_DEBUG("location path: %s", request->location_path->data);
|
||||
|
||||
if (strcmp(request->location->data, "/") == 0) {
|
||||
string_append(request->location_path, "index.html");
|
||||
} else {
|
||||
string_append(request->location_path, string_cstr(request->location));
|
||||
string_append(request->location_path, request->location->data);
|
||||
}
|
||||
CWS_LOG_DEBUG("location path: %s", request->location_path->data);
|
||||
|
||||
@@ -272,7 +274,7 @@ size_t http_response_builder(char **response, cws_http_status_e status, char *co
|
||||
char *status_code = http_status_string(status);
|
||||
|
||||
size_t header_len = http_header_len(status_code, content_type, body_len_bytes);
|
||||
size_t total_len = header_len + body_len_bytes;
|
||||
size_t total_len = header_len + body_len_bytes + 1;
|
||||
|
||||
*response = malloc(total_len);
|
||||
if (*response == NULL) {
|
||||
|
||||
@@ -19,6 +19,11 @@ int main(void) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (sigaction(SIGTERM, &act, NULL)) {
|
||||
CWS_LOG_ERROR("sigaction()");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
cws_config_s *config = cws_config_init();
|
||||
if (!config) {
|
||||
CWS_LOG_ERROR("Unable to read config file");
|
||||
|
||||
@@ -48,7 +48,7 @@ cws_worker_s **cws_worker_new(size_t workers_num, cws_config_s *config) {
|
||||
if (workers == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memset(workers, 0, sizeof **workers * workers_num);
|
||||
memset(workers, 0, workers_num * sizeof *workers);
|
||||
|
||||
for (size_t i = 0; i < workers_num; ++i) {
|
||||
workers[i] = malloc(sizeof(cws_worker_s));
|
||||
@@ -176,17 +176,15 @@ cws_server_ret cws_server_handle_client_data(int epfd, int client_fd) {
|
||||
}
|
||||
|
||||
cws_http_s *request = cws_http_parse(data);
|
||||
request->sockfd = client_fd;
|
||||
|
||||
string_free(data);
|
||||
|
||||
// TODO: fix response
|
||||
|
||||
if (request == NULL) {
|
||||
cws_server_close_client(epfd, client_fd);
|
||||
|
||||
return CWS_SERVER_HTTP_PARSE_ERROR;
|
||||
}
|
||||
request->sockfd = client_fd;
|
||||
|
||||
cws_http_send_response(request, HTTP_OK);
|
||||
|
||||
cws_http_free(request);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user