Commit 1fb8628861c0415833f99c9d4de8c890f4a8707f

Authored by Georg Hopp
1 parent 142319ae

fix memory leak introduced when https was added as well as a bug in request line parsing

@@ -19,8 +19,8 @@ httpParserRequestVars(HttpParser this) @@ -19,8 +19,8 @@ httpParserRequestVars(HttpParser this)
19 } 19 }
20 20
21 request->path = malloc(delim - request->uri + 1); 21 request->path = malloc(delim - request->uri + 1);
22 - request->path[delim - request->uri + 1] = 0;  
23 - memcpy(request->path, request->uri, delim - request->uri + 1); 22 + request->path[delim - request->uri] = 0;
  23 + memcpy(request->path, request->uri, delim - request->uri);
24 24
25 while(NULL != delim && 0 != *delim) { 25 while(NULL != delim && 0 != *delim) {
26 char * key = delim + 1; 26 char * key = delim + 1;
@@ -75,6 +75,7 @@ httpRequestDtor(void * _this) @@ -75,6 +75,7 @@ httpRequestDtor(void * _this)
75 75
76 FREE(this->uri); 76 FREE(this->uri);
77 FREE(this->method); 77 FREE(this->method);
  78 + FREE(this->path);
78 79
79 PARENTCALL(_this, Class, dtor); 80 PARENTCALL(_this, Class, dtor);
80 } 81 }
@@ -35,6 +35,10 @@ @@ -35,6 +35,10 @@
35 35
36 #include "utils/memory.h" 36 #include "utils/memory.h"
37 37
  38 +
  39 +void serverCloseConn(Server, unsigned int);
  40 +
  41 +
38 static 42 static
39 int 43 int
40 serverCtor(void * _this, va_list * params) 44 serverCtor(void * _this, va_list * params)
@@ -102,18 +106,9 @@ serverDtor(void * _this) @@ -102,18 +106,9 @@ serverDtor(void * _this)
102 int i; 106 int i;
103 107
104 for (i=0; i<this->nfds; i++) { 108 for (i=0; i<this->nfds; i++) {
105 - if (this->sock->handle != (this->fds)[i].fd) {  
106 - Stream st = (this->conns[(this->fds)[i].fd]).stream;  
107 -  
108 - delete((this->conns[(this->fds)[i].fd]).sock);  
109 - delete((this->conns[(this->fds)[i].fd]).worker);  
110 -  
111 - if (NULL != st && STREAM_SSL == st->type) {  
112 - SSL_shutdown((st->handle).ssl);  
113 - SSL_free((st->handle).ssl);  
114 - }  
115 -  
116 - delete((this->conns[(this->fds)[i].fd]).stream); 109 + if (this->sock->handle != (this->fds)[i].fd &&
  110 + this->sockSSL->handle != (this->fds)[i].fd) {
  111 + serverCloseConn(this, i);
117 } 112 }
118 } 113 }
119 114
@@ -122,6 +117,7 @@ serverDtor(void * _this) @@ -122,6 +117,7 @@ serverDtor(void * _this)
122 117
123 delete(this->sock); 118 delete(this->sock);
124 delete(this->sockSSL); 119 delete(this->sockSSL);
  120 +
125 SSL_CTX_free(this->ctx); 121 SSL_CTX_free(this->ctx);
126 ERR_free_strings(); 122 ERR_free_strings();
127 } 123 }
@@ -31,16 +31,17 @@ void @@ -31,16 +31,17 @@ void
31 serverCloseConn(Server this, unsigned int i) 31 serverCloseConn(Server this, unsigned int i)
32 { 32 {
33 int fd = (this->fds)[i].fd; 33 int fd = (this->fds)[i].fd;
34 - Stream st = (this->conns[(this->fds)[i].fd]).stream; 34 + Stream st = (this->conns[fd]).stream;
35 35
36 delete((this->conns)[fd].sock); 36 delete((this->conns)[fd].sock);
37 delete((this->conns)[fd].worker); 37 delete((this->conns)[fd].worker);
38 38
39 if (NULL != st && STREAM_SSL == st->type) { 39 if (NULL != st && STREAM_SSL == st->type) {
40 SSL_shutdown((st->handle).ssl); 40 SSL_shutdown((st->handle).ssl);
  41 + SSL_free((st->handle).ssl);
41 } 42 }
42 43
43 - delete((this->conns)[fd].stream); 44 + delete(st);
44 45
45 memset(&(this->fds[i]), 0, sizeof(struct pollfd)); 46 memset(&(this->fds[i]), 0, sizeof(struct pollfd));
46 } 47 }
@@ -36,36 +36,36 @@ int @@ -36,36 +36,36 @@ int
36 serverHandleAccept(Server this, unsigned int i) 36 serverHandleAccept(Server this, unsigned int i)
37 { 37 {
38 char remoteAddr[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; 38 char remoteAddr[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
39 - Sock acc = NULL; 39 + Sock acc = NULL;
40 Stream st; 40 Stream st;
41 41
42 if (this->nfds >= this->max_fds) { 42 if (this->nfds >= this->max_fds) {
43 return -1; 43 return -1;
44 } 44 }
45 45
46 - switch(i) {  
47 - case 0:  
48 - // no SSL  
49 - acc = socketAccept(this->sock, &remoteAddr);  
50 - st = new(Stream, STREAM_FD, acc->handle);  
51 - break;  
52 -  
53 - case 1:  
54 - // SSL  
55 - {  
56 - SSL * ssl = SSL_new(this->ctx);  
57 - acc = socketAccept(this->sockSSL, &remoteAddr);  
58 - SSL_set_fd(ssl, acc->handle);  
59 - SSL_accept(ssl);  
60 - st = new(Stream, STREAM_SSL, ssl);  
61 - }  
62 - break;  
63 -  
64 - default:  
65 - break;  
66 - } 46 + acc = socketAccept((0 == i)? this->sock : this->sockSSL, &remoteAddr);
67 47
68 if (-1 != acc->handle) { 48 if (-1 != acc->handle) {
  49 + switch(i) {
  50 + case 0:
  51 + // no SSL
  52 + st = new(Stream, STREAM_FD, acc->handle);
  53 + break;
  54 +
  55 + case 1:
  56 + // SSL
  57 + {
  58 + SSL * ssl = SSL_new(this->ctx);
  59 + SSL_set_fd(ssl, acc->handle);
  60 + SSL_accept(ssl);
  61 + st = new(Stream, STREAM_SSL, ssl);
  62 + }
  63 + break;
  64 +
  65 + default:
  66 + break;
  67 + }
  68 +
69 // save the socket handle 69 // save the socket handle
70 (this->conns)[acc->handle].sock = acc; 70 (this->conns)[acc->handle].sock = acc;
71 71
Please register or login to post a comment