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