From 2e721674873d28863954f8bf609ab9f75930362e Mon Sep 17 00:00:00 2001 From: Nicolas Favre-Felix Date: Sat, 22 Jan 2011 18:24:44 +0100 Subject: [PATCH] Valgrind cleanup. --- http.c | 71 +++++++++++++++++++++++++++++++++++++--------------------- http.h | 2 +- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/http.c b/http.c index 854c682..363a220 100644 --- a/http.c +++ b/http.c @@ -56,16 +56,49 @@ http_client_read(int fd, short event, void *ctx) { return; } - http_client_serve(c); + if(!c->executing) { + http_client_serve(c); + } +} + +static void +http_client_cleanup(struct http_client *c) { + + free(c->path.s); + memset(&c->path, 0, sizeof(str_t)); + + free(c->body.s); + memset(&c->body, 0, sizeof(str_t)); + + free(c->header_connection.s); + memset(&c->header_connection, 0, sizeof(str_t)); + + free(c->header_if_none_match.s); + memset(&c->header_if_none_match, 0, sizeof(str_t)); + + free(c->out_content_type.s); + memset(&c->out_content_type, 0, sizeof(str_t)); + + free(c->out_etag.s); + memset(&c->out_etag, 0, sizeof(str_t)); + + free(c->buffer); + c->buffer = NULL; + c->sz = 0; + + memset(&c->verb, 0, sizeof(c->verb)); + + c->executing = 0; } + void http_client_free(struct http_client *c) { event_del(&c->ev); close(c->fd); - free(c->buffer); - free((char*)c->out_content_type.s); + + http_client_cleanup(c); free(c); } @@ -96,29 +129,7 @@ http_client_reset(struct http_client *c) { return; } - - free(c->path.s); - memset(&c->path, 0, sizeof(str_t)); - - free(c->body.s); - memset(&c->body, 0, sizeof(str_t)); - - free(c->header_connection.s); - memset(&c->header_connection, 0, sizeof(str_t)); - - free(c->header_if_none_match.s); - memset(&c->header_if_none_match, 0, sizeof(str_t)); - - free(c->out_content_type.s); - memset(&c->out_content_type, 0, sizeof(str_t)); - memset(&c->out_etag, 0, sizeof(str_t)); - - free(c->buffer); - c->buffer = NULL; - c->sz = 0; - - memset(&c->verb, 0, sizeof(c->verb)); - + http_client_cleanup(c); http_parser_init(&c->parser, HTTP_REQUEST); } @@ -211,6 +222,7 @@ http_on_complete(http_parser *p) { struct http_client *c = p->data; int ret = -1; + c->executing = 1; /* check that the command can be executed */ switch(c->verb) { case HTTP_GET: @@ -280,6 +292,7 @@ http_send_reply(struct http_client *c, short code, const char *msg, } else { if(code == 200){ http_client_reset(c); + http_client_serve(c); } else { http_client_free(c); } @@ -409,6 +422,12 @@ http_response_send(struct http_response *r, int fd) { ret = write(fd, s, sz); free(s); + /* cleanup response object */ + for(i = 0; i < r->header_count; ++i) { + free(r->headers[i].s); + } + free(r->headers); + return ret == (int)sz ? 0 : 1; } diff --git a/http.h b/http.h index 64bd07a..22a1f58 100644 --- a/http.h +++ b/http.h @@ -15,7 +15,7 @@ struct http_client { int fd; struct event ev; struct server *s; - int needs_free; + int executing; /* input buffer */ char *buffer;