diff --git a/src/Makefile b/src/Makefile index c81f2a7..1a61164 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ DEP_BASE_DIR=../deps DEP_INCLUDE_DIR=$(DEP_BASE_DIR)/include DEP_LIB_DIR=$(abspath $(DEP_BASE_DIR)/lib) -CFLAGS=-g -pthread -std=gnu11 -Wall -Werror -Wmissing-prototypes -I$(DEP_INCLUDE_DIR) +CFLAGS=-g -O2 -pthread -std=gnu11 -Wall -Werror -Wmissing-prototypes -I$(DEP_INCLUDE_DIR) # include lib directory into runtime path to facilitate dynamic linking LDFLAGS=-pthread -Wl,-rpath -Wl,$(DEP_LIB_DIR) diff --git a/src/bufio.c b/src/bufio.c index 4e6fcee..9fc2970 100644 --- a/src/bufio.c +++ b/src/bufio.c @@ -30,7 +30,8 @@ struct bufio { buffer_t buf; // holds data that was received }; -static const int BUFSIZE = 1536; +static const int BUFSIZE = 8192; +static const int READSIZE = 2048; static int min(int a, int b) { return a < b ? a : b; } /* Create a new bufio object from a socket. */ @@ -95,8 +96,8 @@ void bufio_truncate(struct bufio * self) static ssize_t read_more(struct bufio *self) { - char * buf = buffer_ensure_capacity(&self->buf, BUFSIZE); - int bread = recv(self->socket, buf, BUFSIZE, MSG_NOSIGNAL); + char * buf = buffer_ensure_capacity(&self->buf, READSIZE); + int bread = recv(self->socket, buf, READSIZE, MSG_NOSIGNAL); if (bread < 1) return bread; diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 0000000..04a2d14 --- /dev/null +++ b/src/globals.h @@ -0,0 +1,7 @@ +/* + * Declarations of various global variables. + */ +#include + +extern char *server_root; +extern bool silent_mode; diff --git a/src/http.c b/src/http.c index 2fedb3e..c680be7 100644 --- a/src/http.c +++ b/src/http.c @@ -348,7 +348,7 @@ http_handle_transaction(struct http_client *self) http_add_header(&ta.resp_headers, "Server", "CS3214-Personal-Server"); buffer_init(&ta.resp_body, 0); - bool rc; + bool rc = false; char *req_path = bufio_offset2ptr(ta.client->bufio, ta.req_path); if (STARTS_WITH(req_path, "/api")) { rc = handle_api(&ta); diff --git a/src/http.h b/src/http.h index 94a43ea..f0092b0 100644 --- a/src/http.h +++ b/src/http.h @@ -56,6 +56,4 @@ void http_setup_client(struct http_client *, struct bufio *bufio); bool http_handle_transaction(struct http_client *); void http_add_header(buffer_t * resp, char* key, char* fmt, ...); -extern char *server_root; - #endif /* _HTTP_H */ diff --git a/src/main.c b/src/main.c index 11276fe..566fed9 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,9 @@ #include "http.h" #include "socket.h" #include "bufio.h" +#include "globals.h" + +bool silent_mode = false; /* * A non-concurrent, iterative server that serves one client at a time. @@ -47,12 +50,16 @@ main(int ac, char *av[]) { int opt; char *port_string = NULL; - while ((opt = getopt(ac, av, "hp:R:")) != -1) { + while ((opt = getopt(ac, av, "hp:R:s")) != -1) { switch (opt) { case 'p': port_string = optarg; break; + case 's': + silent_mode = true; + break; + case 'R': server_root = optarg; break; diff --git a/src/socket.c b/src/socket.c index 762892d..3fc02d2 100644 --- a/src/socket.c +++ b/src/socket.c @@ -21,6 +21,7 @@ #include #include "socket.h" +#include "globals.h" /* * Find a suitable IPv4 address to bind to, create a socket, bind it, @@ -124,15 +125,17 @@ socket_accept_client(int accepting_socket) /* The following will help with debugging your server. * Adjust and/or remove as you see fit. */ - char peer_addr[1024], peer_port[10]; - int rc = getnameinfo(&peer, peersize, - peer_addr, sizeof peer_addr, peer_port, sizeof peer_port, - NI_NUMERICHOST | NI_NUMERICSERV); - if (rc != 0) { - fprintf(stderr, "getnameinfo error: %s\n", gai_strerror(rc)); - return -1; + if (!silent_mode) { + char peer_addr[1024], peer_port[10]; + int rc = getnameinfo(&peer, peersize, + peer_addr, sizeof peer_addr, peer_port, sizeof peer_port, + NI_NUMERICHOST | NI_NUMERICSERV); + if (rc != 0) { + fprintf(stderr, "getnameinfo error: %s\n", gai_strerror(rc)); + return -1; + } + fprintf(stderr, "Accepted connection from %s:%s\n", peer_addr, peer_port); } - fprintf(stderr, "Accepted connection from %s:%s\n", peer_addr, peer_port); return client; }