From 97a72783f501ac99c275b859d343237f563e5290 Mon Sep 17 00:00:00 2001 From: Robert James Kaes Date: Sun, 7 Apr 2002 21:32:01 +0000 Subject: Cleaned up some of the code to handle errors better. Also, a little bit easier to read in the variable department. --- src/conns.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++------- src/conns.h | 9 ++++++--- 2 files changed, 56 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/conns.c b/src/conns.c index e0881ed..7bf86b2 100644 --- a/src/conns.c +++ b/src/conns.c @@ -1,4 +1,4 @@ -/* $Id: conns.c,v 1.6 2001-11-25 22:06:20 rjkaes Exp $ +/* $Id: conns.c,v 1.7 2002-04-07 21:32:01 rjkaes Exp $ * * Create and free the connection structure. One day there could be * other connnection related tasks put here, but for now the header @@ -25,24 +25,64 @@ #include "stats.h" #include "utils.h" -void -initialize_conn(struct conn_s *connptr) +struct conn_s * +initialize_conn(int client_fd) { - connptr->client_fd = connptr->server_fd = -1; - connptr->cbuffer = new_buffer(); - connptr->sbuffer = new_buffer(); + struct conn_s *connptr; + struct buffer_s *cbuffer, *sbuffer; + + assert(client_fd >= 0); + + /* + * Allocate the memory for all the internal componets + */ + cbuffer = new_buffer(); + sbuffer = new_buffer(); + + if (!cbuffer || !sbuffer) + goto error_exit; + + /* + * Allocate the space for the conn_s structure itself. + */ + connptr = safemalloc(sizeof(struct conn_s)); + if (!connptr) + goto error_exit; + + connptr->client_fd = client_fd; + connptr->server_fd = -1; + + connptr->cbuffer = cbuffer; + connptr->sbuffer = sbuffer; - connptr->send_response_message = FALSE; + connptr->request_line = NULL; + + connptr->response_message_sent = FALSE; connptr->connect_method = FALSE; connptr->protocol.major = connptr->protocol.minor = 0; update_stats(STAT_OPEN); + + return connptr; + +error_exit: + /* + * If we got here, there was a problem allocating memory + */ + if (cbuffer) + delete_buffer(cbuffer); + if (sbuffer) + delete_buffer(sbuffer); + + return NULL; } void destroy_conn(struct conn_s *connptr) { + assert(connptr != NULL); + if (connptr->client_fd != -1) close(connptr->client_fd); if (connptr->server_fd != -1) @@ -53,6 +93,9 @@ destroy_conn(struct conn_s *connptr) if (connptr->sbuffer) delete_buffer(connptr->sbuffer); + if (connptr->request_line) + safefree(connptr->request_line); + safefree(connptr); update_stats(STAT_CLOSE); diff --git a/src/conns.h b/src/conns.h index e29b76b..a0a31a9 100644 --- a/src/conns.h +++ b/src/conns.h @@ -1,4 +1,4 @@ -/* $Id: conns.h,v 1.5 2001-11-25 22:06:20 rjkaes Exp $ +/* $Id: conns.h,v 1.6 2002-04-07 21:32:01 rjkaes Exp $ * * See 'conns.c' for a detailed description. * @@ -30,8 +30,11 @@ struct conn_s { struct buffer_s *cbuffer; struct buffer_s *sbuffer; + /* The request line (first line) from the client */ + char *request_line; + bool_t connect_method; - bool_t send_response_message; + bool_t response_message_sent; /* * Store the incoming request's HTTP protocol. @@ -45,7 +48,7 @@ struct conn_s { /* * Functions for the creation and destruction of a connection structure. */ -extern void initialize_conn(struct conn_s *connptr); +extern struct conn_s* initialize_conn(int client_fd); extern void destroy_conn(struct conn_s *connptr); #endif -- cgit v1.2.3