add log and remove notes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "http/http.h"
|
||||
|
||||
#include <stdio.h> /* Debug */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
@@ -21,7 +21,7 @@ cws_http *cws_http_parse(char *request_str, int sockfd) {
|
||||
if (pch == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
printf("[client::http] method: %s\n", pch);
|
||||
CWS_LOG_DEBUG("[client::http] method: %s", pch);
|
||||
cws_http_parse_method(request, pch);
|
||||
|
||||
/* Parse location */
|
||||
@@ -29,7 +29,7 @@ cws_http *cws_http_parse(char *request_str, int sockfd) {
|
||||
if (pch == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
printf("[client::http] location: %s\n", pch);
|
||||
CWS_LOG_DEBUG("[client::http] location: %s", pch);
|
||||
strncpy(request->location, pch, CWS_HTTP_LOCATION_LEN);
|
||||
|
||||
/* Parse location path */
|
||||
@@ -38,14 +38,14 @@ cws_http *cws_http_parse(char *request_str, int sockfd) {
|
||||
} else {
|
||||
snprintf(request->location_path, CWS_HTTP_LOCATION_PATH_LEN, "%s%s", CWS_WWW, request->location);
|
||||
}
|
||||
fprintf(stdout, "[client::http] location path: %s\n", request->location_path);
|
||||
CWS_LOG_DEBUG("[client::http] location path: %s", request->location_path);
|
||||
|
||||
/* Parse HTTP version */
|
||||
pch = strtok(NULL, " \r\n");
|
||||
if (pch == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
printf("[client::http] version: %s\n", pch);
|
||||
CWS_LOG_DEBUG("[client::http] version: %s", pch);
|
||||
strncpy(request->http_version, pch, CWS_HTTP_VERSION_LEN);
|
||||
|
||||
/* Parse other stuff... */
|
||||
@@ -90,7 +90,7 @@ void cws_http_send_response(cws_http *request) {
|
||||
char *file_data = malloc(content_length);
|
||||
if (file_data == NULL) {
|
||||
fclose(file);
|
||||
fprintf(stderr, RED "Unable to allocate file data\n");
|
||||
CWS_LOG_ERROR("Unable to allocate file data");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
17
src/main.c
17
src/main.c
@@ -1,36 +1,31 @@
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server/server.h"
|
||||
#include "utils/colors.h"
|
||||
#include "utils/config.h"
|
||||
|
||||
void cws_signal_handler(int signo) {
|
||||
fprintf(stdout, BLUE "[server] Cleaning up resources...\n" RESET);
|
||||
cws_server_run = false;
|
||||
}
|
||||
void cws_signal_handler(int signo) { cws_server_run = false; }
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int ret;
|
||||
|
||||
cws_config *config = cws_config_init();
|
||||
if (config == NULL) {
|
||||
fprintf(stderr, RED BOLD "[server] Unable to read config file\n" RESET);
|
||||
CWS_LOG_ERROR("[server] Unable to read config file");
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct sigaction act = {
|
||||
.sa_handler = cws_signal_handler
|
||||
};
|
||||
struct sigaction act = {.sa_handler = cws_signal_handler};
|
||||
ret = sigaction(SIGINT, &act, NULL);
|
||||
|
||||
fprintf(stdout, BOLD GREEN "[server] Running cws on http://%s:%s...\n" RESET, config->host, config->port);
|
||||
CWS_LOG_INFO("[server] Running cws on http://%s:%s...", config->host, config->port);
|
||||
|
||||
ret = cws_server_start(config->host, config->port);
|
||||
if (ret < 0) {
|
||||
fprintf(stderr, BOLD RED "[server] Unable to start web server\n" RESET);
|
||||
CWS_LOG_ERROR("[server] Unable to start web server");
|
||||
}
|
||||
|
||||
cws_config_free(config);
|
||||
|
||||
@@ -26,7 +26,7 @@ int cws_server_start(const char *hostname, const char *service) {
|
||||
|
||||
int status = getaddrinfo(hostname, service, &hints, &res);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED BOLD "[server] getaddrinfo() error: %s\n" RESET, gai_strerror(status));
|
||||
CWS_LOG_ERROR("[server] getaddrinfo() error: %s", gai_strerror(status));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -35,19 +35,19 @@ int cws_server_start(const char *hostname, const char *service) {
|
||||
const int opt = 1;
|
||||
status = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED BOLD "[server] setsockopt(): %s\n" RESET, strerror(errno));
|
||||
CWS_LOG_ERROR("[server] setsockopt(): %s", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
status = bind(sockfd, res->ai_addr, res->ai_addrlen);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED BOLD "[server] bind(): %s\n" RESET, strerror(errno));
|
||||
CWS_LOG_ERROR("[server] bind(): %s", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
status = listen(sockfd, CWS_SERVER_BACKLOG);
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED BOLD "[server] listen(): %s\n" RESET, gai_strerror(status));
|
||||
CWS_LOG_ERROR("[server] listen(): %s", gai_strerror(status));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -61,10 +61,13 @@ int cws_server_start(const char *hostname, const char *service) {
|
||||
|
||||
void cws_server_setup_hints(struct addrinfo *hints, size_t len, const char *hostname) {
|
||||
memset(hints, 0, len);
|
||||
|
||||
/* IPv4 or IPv6 */
|
||||
hints->ai_family = AF_UNSPEC;
|
||||
|
||||
/* TCP */
|
||||
hints->ai_socktype = SOCK_STREAM;
|
||||
|
||||
if (hostname == NULL) {
|
||||
/* Fill in IP for me */
|
||||
hints->ai_flags = AI_PASSIVE;
|
||||
@@ -94,13 +97,14 @@ void cws_server_loop(int sockfd) {
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
client_fd = cws_server_accept_client(sockfd, &their_sa, &theirsa_size);
|
||||
cws_utils_get_client_ip(&their_sa, ip);
|
||||
fprintf(stdout, BLUE "[server] Client (%s) connected\n" RESET, ip);
|
||||
CWS_LOG_INFO("[server] Client (%s) connected", ip);
|
||||
|
||||
cws_fd_set_nonblocking(client_fd);
|
||||
cws_epoll_add(epfd, client_fd, EPOLLIN);
|
||||
cws_hm_push(clients, client_fd, &their_sa);
|
||||
} else {
|
||||
char data[4096] = {0};
|
||||
|
||||
/* Incoming data */
|
||||
client_fd = revents[i].data.fd;
|
||||
const ssize_t bytes_read = recv(client_fd, data, sizeof data, 0);
|
||||
@@ -110,7 +114,7 @@ void cws_server_loop(int sockfd) {
|
||||
|
||||
if (bytes_read == 0) {
|
||||
/* Client disconnected */
|
||||
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
|
||||
CWS_LOG_INFO("[server] Client (%s) disconnected", ip);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
continue;
|
||||
}
|
||||
@@ -135,7 +139,7 @@ void cws_server_loop(int sockfd) {
|
||||
}
|
||||
|
||||
cws_http_send_response(request);
|
||||
fprintf(stdout, BLUE "[server] Client (%s) disconnected\n" RESET, ip);
|
||||
CWS_LOG_INFO("[server] Client (%s) disconnected", ip);
|
||||
cws_server_close_client(epfd, client_fd, clients);
|
||||
cws_http_free(request);
|
||||
|
||||
@@ -150,7 +154,7 @@ void cws_server_loop(int sockfd) {
|
||||
close(epfd);
|
||||
cws_server_close_all_fds(clients);
|
||||
cws_hm_free(clients);
|
||||
fprintf(stdout, BLUE "[server] Closing...\n" RESET);
|
||||
CWS_LOG_INFO("[server] Closing...");
|
||||
}
|
||||
|
||||
void cws_epoll_add(int epfd, int sockfd, uint32_t events) {
|
||||
@@ -160,7 +164,7 @@ void cws_epoll_add(int epfd, int sockfd, uint32_t events) {
|
||||
const int status = epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &event);
|
||||
|
||||
if (status != 0) {
|
||||
fprintf(stderr, RED BOLD "[server] epoll_ctl_add(): %s\n" RESET, strerror(errno));
|
||||
CWS_LOG_ERROR("[server] epoll_ctl_add(): %s", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@@ -169,7 +173,7 @@ void cws_epoll_del(int epfd, int sockfd) {
|
||||
const int status = epoll_ctl(epfd, EPOLL_CTL_DEL, sockfd, NULL);
|
||||
|
||||
if (status != 0) {
|
||||
fprintf(stdout, RED BOLD "[server] epoll_ctl_del(): %s\n" RESET, strerror(errno));
|
||||
CWS_LOG_ERROR("[server] epoll_ctl_del(): %s", strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@@ -178,7 +182,7 @@ void cws_fd_set_nonblocking(int sockfd) {
|
||||
const int status = fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||
|
||||
if (status == -1) {
|
||||
fprintf(stderr, RED BOLD "[server] fcntl(): %s\n" RESET, gai_strerror(status));
|
||||
CWS_LOG_ERROR("[server] fcntl(): %s", gai_strerror(status));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
@@ -188,7 +192,7 @@ int cws_server_accept_client(int sockfd, struct sockaddr_storage *their_sa, sock
|
||||
|
||||
if (client_fd == -1) {
|
||||
if (errno != EWOULDBLOCK) {
|
||||
fprintf(stderr, RED BOLD "[server] accept(): %s\n" RESET, strerror(errno));
|
||||
CWS_LOG_ERROR("[server] accept(): %s", strerror(errno));
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ void cws_utils_print_ips(const char *hostname, const char *port) {
|
||||
|
||||
int status = getaddrinfo(hostname, port, &ai, &res);
|
||||
if (status < 0) {
|
||||
fprintf(stderr, RED "getaddrinfo(): %s\n" RESET, gai_strerror(status));
|
||||
CWS_LOG_ERROR("getaddrinfo(): %s", gai_strerror(status));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@@ -28,11 +28,11 @@ void cws_utils_print_ips(const char *hostname, const char *port) {
|
||||
if (p->ai_family == AF_INET) {
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)p->ai_addr;
|
||||
inet_ntop(AF_INET, &sin->sin_addr, ipv4, INET_ADDRSTRLEN);
|
||||
fprintf(stdout, BLUE "%s\n" RESET, ipv4);
|
||||
CWS_LOG_INFO("%s", ipv4);
|
||||
} else if (p->ai_family == AF_INET6) {
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)p->ai_addr;
|
||||
inet_ntop(AF_INET6, &sin6->sin6_addr, ipv6, INET6_ADDRSTRLEN);
|
||||
fprintf(stdout, BLUE "%s\n" RESET, ipv6);
|
||||
CWS_LOG_INFO("%s", ipv6);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user