[mw-devel] MW3 r1358 - trunk/src/client

arthur at sucs.org arthur at sucs.org
Thu Nov 7 15:15:23 GMT 2013


Author: arthur
Date: 2013-11-07 15:15:23 +0000 (Thu, 07 Nov 2013)
New Revision: 1358

Modified:
   trunk/src/client/js.c
   trunk/src/client/log.c
   trunk/src/client/log.h
Log:
Allow for url downloads to be size limited


Modified: trunk/src/client/js.c
===================================================================
--- trunk/src/client/js.c	2013-11-02 03:03:42 UTC (rev 1357)
+++ trunk/src/client/js.c	2013-11-07 15:15:23 UTC (rev 1358)
@@ -728,14 +728,13 @@
 }
 
 /* recieve data from curl into a malloced memory chunk */
- size_t  urldata(  void  *ptr,  size_t  size, size_t nmemb, void *stream)
+size_t urldata(  void  *ptr,  size_t  size, size_t nmemb, void *stream)
 {
 	int addsize = size*nmemb;
 	struct block_t *b = stream;
 
 	if (stream == NULL) return 0;
-	block_append(b, ptr, addsize);
-	return addsize;
+	return block_append(b, ptr, addsize);
 }
 
 /* Function to make a url GET request and return the resulting page

Modified: trunk/src/client/log.c
===================================================================
--- trunk/src/client/log.c	2013-11-02 03:03:42 UTC (rev 1357)
+++ trunk/src/client/log.c	2013-11-07 15:15:23 UTC (rev 1358)
@@ -241,6 +241,10 @@
 
 	c = curl_easy_init();
 
+	/* set max download sizes */
+	block_limit(deli, 8192); 
+	block_limit(body, 4096); 
+
 	/* lets go fishing on del.icio.us */
 	url = cleanup_url(uri->url);
 	md = md5(url);
@@ -354,9 +358,17 @@
 	new->i_size = size;
 	new->i_used = 0;
 	new->p_cursor = new->p_buffer;
+	new->i_limit = 0;
+
 	return new;
 }
 
+void block_limit(struct block_t *p, int limit)
+{
+	if (p == NULL) return;
+	p->i_limit = limit;
+}
+
 void block_free(struct block_t *p)
 {
 	if (p==NULL) return;
@@ -372,6 +384,7 @@
 
 	if (old == NULL) return;
 	offset = old->p_cursor - old->p_buffer;
+	if (old->i_limit > 0 && size > old->i_limit) size = old->i_limit;
 	if (size > old->i_size) {
 		old->p_buffer = realloc(old->p_buffer, size);
 		old->i_size = size;
@@ -381,11 +394,17 @@
 
 int block_append(struct block_t *p, void *data, int size)
 {
-	if (p == NULL) return 1;
-	if (p->i_used + size + 1 > p->i_size)
+	if (p == NULL) return 0;
+	int newsize = p->i_used + size + 1;
+	/* it needs to be bigger grow it */
+	if (newsize > p->i_size) {
 		block_resize(p, p->i_used + size + 1);
+		/* it didnt grow enough, limit the copy */
+		if (p->i_size < newsize)
+			size = p->i_size - p->i_used - 1;
+	}
 	memcpy(&p->p_buffer[p->i_used], data, size);
 	p->i_used += size;
 	p->p_buffer[p->i_used] = 0;
-	return 0;
+	return size;
 }

Modified: trunk/src/client/log.h
===================================================================
--- trunk/src/client/log.h	2013-11-02 03:03:42 UTC (rev 1357)
+++ trunk/src/client/log.h	2013-11-07 15:15:23 UTC (rev 1358)
@@ -7,6 +7,7 @@
 struct block_t {
 	char *p_buffer; 	/* the actual data block */
 	int i_size;		/* actual block size */
+	int i_limit;		/* max size, 0=unlimited */
 
 	int     i_used;		/* current fill level */
 	char *p_cursor;		/* user pointer */
@@ -15,6 +16,7 @@
 struct block_t * block_new(int);
 void block_free(struct block_t *);
 void block_resize(struct block_t *, int);
+void block_limit(struct block_t *, int);
 int block_append(struct block_t *p, void *data, int size);
 
 #endif /* LOG_H */




More information about the mw-devel mailing list