various changes
- added silent_mode for benchmarking - fixed compiler errors - moved variables to globals.h
This commit is contained in:
		
							parent
							
								
									36f9faebfe
								
							
						
					
					
						commit
						94c94ca6de
					
				@ -2,7 +2,7 @@ DEP_BASE_DIR=../deps
 | 
				
			|||||||
DEP_INCLUDE_DIR=$(DEP_BASE_DIR)/include
 | 
					DEP_INCLUDE_DIR=$(DEP_BASE_DIR)/include
 | 
				
			||||||
DEP_LIB_DIR=$(abspath $(DEP_BASE_DIR)/lib)
 | 
					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
 | 
					# include lib directory into runtime path to facilitate dynamic linking
 | 
				
			||||||
LDFLAGS=-pthread -Wl,-rpath -Wl,$(DEP_LIB_DIR)
 | 
					LDFLAGS=-pthread -Wl,-rpath -Wl,$(DEP_LIB_DIR)
 | 
				
			||||||
 | 
				
			|||||||
@ -30,7 +30,8 @@ struct bufio {
 | 
				
			|||||||
    buffer_t buf;       // holds data that was received
 | 
					    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; }
 | 
					static int min(int a, int b) { return a < b ? a : b; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* Create a new bufio object from a socket. */
 | 
					/* Create a new bufio object from a socket. */
 | 
				
			||||||
@ -95,8 +96,8 @@ void bufio_truncate(struct bufio * self)
 | 
				
			|||||||
static ssize_t
 | 
					static ssize_t
 | 
				
			||||||
read_more(struct bufio *self)
 | 
					read_more(struct bufio *self)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    char * buf = buffer_ensure_capacity(&self->buf, BUFSIZE);
 | 
					    char * buf = buffer_ensure_capacity(&self->buf, READSIZE);
 | 
				
			||||||
    int bread = recv(self->socket, buf, BUFSIZE, MSG_NOSIGNAL);
 | 
					    int bread = recv(self->socket, buf, READSIZE, MSG_NOSIGNAL);
 | 
				
			||||||
    if (bread < 1)
 | 
					    if (bread < 1)
 | 
				
			||||||
        return bread;
 | 
					        return bread;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										7
									
								
								src/globals.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/globals.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Declarations of various global variables.
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extern char *server_root;
 | 
				
			||||||
 | 
					extern bool silent_mode;
 | 
				
			||||||
@ -348,7 +348,7 @@ http_handle_transaction(struct http_client *self)
 | 
				
			|||||||
    http_add_header(&ta.resp_headers, "Server", "CS3214-Personal-Server");
 | 
					    http_add_header(&ta.resp_headers, "Server", "CS3214-Personal-Server");
 | 
				
			||||||
    buffer_init(&ta.resp_body, 0);
 | 
					    buffer_init(&ta.resp_body, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    bool rc;
 | 
					    bool rc = false;
 | 
				
			||||||
    char *req_path = bufio_offset2ptr(ta.client->bufio, ta.req_path);
 | 
					    char *req_path = bufio_offset2ptr(ta.client->bufio, ta.req_path);
 | 
				
			||||||
    if (STARTS_WITH(req_path, "/api")) {
 | 
					    if (STARTS_WITH(req_path, "/api")) {
 | 
				
			||||||
        rc = handle_api(&ta);
 | 
					        rc = handle_api(&ta);
 | 
				
			||||||
 | 
				
			|||||||
@ -56,6 +56,4 @@ void http_setup_client(struct http_client *, struct bufio *bufio);
 | 
				
			|||||||
bool http_handle_transaction(struct http_client *);
 | 
					bool http_handle_transaction(struct http_client *);
 | 
				
			||||||
void http_add_header(buffer_t * resp, char* key, char* fmt, ...);
 | 
					void http_add_header(buffer_t * resp, char* key, char* fmt, ...);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern char *server_root;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#endif /* _HTTP_H */
 | 
					#endif /* _HTTP_H */
 | 
				
			||||||
 | 
				
			|||||||
@ -13,6 +13,9 @@
 | 
				
			|||||||
#include "http.h"
 | 
					#include "http.h"
 | 
				
			||||||
#include "socket.h"
 | 
					#include "socket.h"
 | 
				
			||||||
#include "bufio.h"
 | 
					#include "bufio.h"
 | 
				
			||||||
 | 
					#include "globals.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool silent_mode = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * A non-concurrent, iterative server that serves one client at a time.
 | 
					 * A non-concurrent, iterative server that serves one client at a time.
 | 
				
			||||||
@ -47,12 +50,16 @@ main(int ac, char *av[])
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    int opt;
 | 
					    int opt;
 | 
				
			||||||
    char *port_string = NULL;
 | 
					    char *port_string = NULL;
 | 
				
			||||||
    while ((opt = getopt(ac, av, "hp:R:")) != -1) {
 | 
					    while ((opt = getopt(ac, av, "hp:R:s")) != -1) {
 | 
				
			||||||
        switch (opt) {
 | 
					        switch (opt) {
 | 
				
			||||||
            case 'p':
 | 
					            case 'p':
 | 
				
			||||||
                port_string = optarg;
 | 
					                port_string = optarg;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            case 's':
 | 
				
			||||||
 | 
					                silent_mode = true;
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            case 'R':
 | 
					            case 'R':
 | 
				
			||||||
                server_root = optarg;
 | 
					                server_root = optarg;
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										19
									
								
								src/socket.c
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/socket.c
									
									
									
									
									
								
							@ -21,6 +21,7 @@
 | 
				
			|||||||
#include <assert.h>
 | 
					#include <assert.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "socket.h"
 | 
					#include "socket.h"
 | 
				
			||||||
 | 
					#include "globals.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Find a suitable IPv4 address to bind to, create a socket, bind it,
 | 
					 * 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.
 | 
					    /* The following will help with debugging your server.
 | 
				
			||||||
     * Adjust and/or remove as you see fit.
 | 
					     * Adjust and/or remove as you see fit.
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    char peer_addr[1024], peer_port[10];
 | 
					    if (!silent_mode) {
 | 
				
			||||||
    int rc = getnameinfo(&peer, peersize,
 | 
					        char peer_addr[1024], peer_port[10];
 | 
				
			||||||
                         peer_addr, sizeof peer_addr, peer_port, sizeof peer_port,
 | 
					        int rc = getnameinfo(&peer, peersize,
 | 
				
			||||||
                         NI_NUMERICHOST | NI_NUMERICSERV);
 | 
					                             peer_addr, sizeof peer_addr, peer_port, sizeof peer_port,
 | 
				
			||||||
    if (rc != 0) {
 | 
					                             NI_NUMERICHOST | NI_NUMERICSERV);
 | 
				
			||||||
        fprintf(stderr, "getnameinfo error: %s\n", gai_strerror(rc));
 | 
					        if (rc != 0) {
 | 
				
			||||||
        return -1;
 | 
					            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;
 | 
					    return client;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user