summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
authorerdgeist <>2006-12-16 13:23:12 +0000
committererdgeist <>2006-12-16 13:23:12 +0000
commitb7526f25e86f79eadfac5e98ae395e337fa74757 (patch)
treeb194470a7ffbfa89a0c5d2cefa1d4a9bfaa74a19 /opentracker.c
parentde03db7dcdee85606dc9fd3bcba0080aadbdf950 (diff)
Fixed two mem leaks from httpd :/
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c49
1 files changed, 14 insertions, 35 deletions
diff --git a/opentracker.c b/opentracker.c
index 83c340c..a0316cf 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -38,8 +38,6 @@ static void panic(const char* routine) {
38struct http_data { 38struct http_data {
39 array r; 39 array r;
40 io_batch iob; 40 io_batch iob;
41 char* hdrbuf;
42 int hlen;
43 unsigned long ip; 41 unsigned long ip;
44}; 42};
45 43
@@ -61,28 +59,13 @@ int header_complete(struct http_data* r)
61 return 0; 59 return 0;
62} 60}
63 61
64void httperror(struct http_data* r,const char* title,const char* message) 62void httperror(struct http_data* h,const char* title,const char* message)
65{ 63{
66 char* c; 64 char* c = (char*)malloc(strlen(message)+strlen(title)+200);
67 c=r->hdrbuf=(char*)malloc(strlen(message)+strlen(title)+200); 65 if( !c) iob_addbuf(&h->iob, "HTTP/1.0 500 internal error\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nout of memory\n", 90);
68 66 else iob_addbuf_free( &h->iob, c,
69 if (!c) 67 sprintf( c, "HTTP/1.0 %s\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: %ld\r\n\r\n<title>%s</title>\n",
70 { 68 title, strlen(message)+strlen(title)+16-4,title+4) );
71 r->hdrbuf="HTTP/1.0 500 internal error\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nout of memory\n";
72 r->hlen=strlen(r->hdrbuf);
73 }
74 else
75 {
76 c+=fmt_str(c,"HTTP/1.0 ");
77 c+=fmt_str(c,title);
78 c+=fmt_str(c,"\r\nContent-Type: text/html\r\nConnection: close\r\nContent-Length: ");
79 c+=fmt_ulong(c,strlen(message)+strlen(title)+16-4);
80 c+=fmt_str(c,"\r\n\r\n<title>");
81 c+=fmt_str(c,title+4);
82 c+=fmt_str(c,"</title>\n");
83 r->hlen=c - r->hdrbuf;
84 }
85 iob_addbuf(&r->iob,r->hdrbuf,r->hlen);
86} 69}
87 70
88// bestimmten http parameter auslesen und adresse zurueckgeben 71// bestimmten http parameter auslesen und adresse zurueckgeben
@@ -279,7 +262,7 @@ e500:
279 reply = malloc( numwant*6+128 ); // peerlist + seeder, peers and lametta n*6+81 a.t.m. 262 reply = malloc( numwant*6+128 ); // peerlist + seeder, peers and lametta n*6+81 a.t.m.
280 if( reply ) 263 if( reply )
281 reply_size = return_peers_for_torrent( torrent, numwant, reply ); 264 reply_size = return_peers_for_torrent( torrent, numwant, reply );
282 if( !reply || ( reply_size < 0 ) ) { 265 if( !reply || ( reply_size <= 0 ) ) {
283 if( reply ) free( reply ); 266 if( reply ) free( reply );
284 goto e500; 267 goto e500;
285 } 268 }
@@ -301,12 +284,9 @@ e404:
301 goto bailout; 284 goto bailout;
302 } 285 }
303 286
304 c=h->hdrbuf=(char*)malloc(80); 287 c=(char*)malloc(80);
305 c+=fmt_str(c,"HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: "); 288 iob_addbuf_free( &h->iob, c, sprintf( c, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %ld\r\n\r\n", (long)reply_size ));
306 c+=fmt_ulonglong(c, reply_size ); 289 if( reply && reply_size ) iob_addbuf_free(&h->iob, reply, reply_size );
307 c+=fmt_str(c,"\r\n\r\n");
308 iob_addbuf(&h->iob,h->hdrbuf,c - h->hdrbuf);
309 if( reply && reply_size ) iob_addbuf_free(&h->iob,reply, reply_size );
310 290
311bailout: 291bailout:
312 io_dontwantread(s); 292 io_dontwantread(s);
@@ -388,7 +368,7 @@ int main()
388 { 368 {
389 array_reset(&h->r); 369 array_reset(&h->r);
390 iob_reset(&h->iob); 370 iob_reset(&h->iob);
391 free(h->hdrbuf); h->hdrbuf=0; 371 free(h);
392 } 372 }
393 io_close(i); 373 io_close(i);
394 } 374 }
@@ -421,15 +401,14 @@ emerge:
421 struct http_data* h=io_getcookie(i); 401 struct http_data* h=io_getcookie(i);
422 402
423 int64 r=iob_send(i,&h->iob); 403 int64 r=iob_send(i,&h->iob);
424
425 if (r==-1) 404 if (r==-1)
426 io_eagain(i); 405 io_eagain(i);
427 else 406 else
428 if (r<=0) 407 if ((r<=0)||(h->iob.bytesleft==0))
429 { 408 {
430 array_trunc(&h->r); 409 array_reset(&h->r);
431 iob_reset(&h->iob); 410 iob_reset(&h->iob);
432 free(h->hdrbuf); h->hdrbuf=0; free(h); 411 free(h);
433 io_close(i); 412 io_close(i);
434 } 413 }
435 } 414 }