Browse code

Experimental curl removal: fix proxy handling and honour 301/302

git-svn: trunk@2307

Nigel Horne authored on 2006/09/27 01:43:17
Showing 2 changed files
... ...
@@ -1,3 +1,8 @@
1
+Tue Sep 26 17:42:17 BST 2006 (njh)
2
+----------------------------------
3
+  * libclamav/mbox.c:	Experimental curl removal: fix proxy handling and
4
+				honour 301/302
5
+
1 6
 Tue Sep 26 09:40:03 BST 2006 (trog)
2 7
 -----------------------------------
3 8
   * libclamav/unrar.c: Don't be so keen to report an error.
... ...
@@ -16,7 +16,7 @@
16 16
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 17
  *  MA 02110-1301, USA.
18 18
  */
19
-static	char	const	rcsid[] = "$Id: mbox.c,v 1.343 2006/09/22 18:37:22 njh Exp $";
19
+static	char	const	rcsid[] = "$Id: mbox.c,v 1.344 2006/09/26 16:41:47 njh Exp $";
20 20
 
21 21
 #if HAVE_CONFIG_H
22 22
 #include "clamav-config.h"
... ...
@@ -251,7 +251,11 @@ static	void	hrefs_done(blob *b,tag_arguments_t* hrefs);
251 251
 #ifdef	WITH_CURL
252 252
 struct arg {
253 253
 	CURL *curl;
254
+#ifdef CL_EXPERIMENTAL
255
+	char *url;
256
+#else
254 257
 	const char *url;
258
+#endif
255 259
 	const char *dir;
256 260
 	char *filename;
257 261
 };
... ...
@@ -3943,7 +3947,7 @@ do_checkURLs(message *m, const char *dir, tag_arguments_t *hrefs)
3943 3943
 				continue;
3944 3944
 			}
3945 3945
 			args[n].dir = dir;
3946
-			args[n].url = url;
3946
+			args[n].url = strdup(url);
3947 3947
 			args[n].filename = strdup(name);
3948 3948
 			pthread_create(&tid[n], NULL, getURL, &args[n]);
3949 3949
 #else
... ...
@@ -3953,11 +3957,12 @@ do_checkURLs(message *m, const char *dir, tag_arguments_t *hrefs)
3953 3953
 				cli_errmsg("curl_easy_init failed\n");
3954 3954
 				continue;
3955 3955
 			}
3956
-			arg.url = url;
3956
+			arg.url = strdup(url);
3957 3957
 			arg.dir = dir;
3958 3958
 			arg.filename = name;
3959 3959
 			getURL(&arg);
3960 3960
 			curl_easy_cleanup(arg.curl);
3961
+			free(arg.url);
3961 3962
 #endif
3962 3963
 
3963 3964
 #else	/*!WITH_CURL*/
... ...
@@ -4002,6 +4007,7 @@ do_checkURLs(message *m, const char *dir, tag_arguments_t *hrefs)
4002 4002
 	while(--n >= 0) {
4003 4003
 		pthread_join(tid[n], NULL);
4004 4004
 		free(args[n].filename);
4005
+		free(args[n].url);
4005 4006
 		curl_easy_cleanup(args[n].curl);
4006 4007
 	}
4007 4008
 #endif
... ...
@@ -4256,8 +4262,8 @@ static	int	nonblock_connect(int sock, const struct sockaddr *addr, socklen_t add
4256 4256
 static	int	connect_error(int sock);
4257 4257
 static	int	my_r_gethostbyname(const char *hostname, struct hostent *hp, char *buf, size_t len);
4258 4258
 
4259
-#define NONBLOCK_SELECT_MAX_FAILURES 3
4260
-#define NONBLOCK_MAX_BOGUS_LOOPS     10
4259
+#define NONBLOCK_SELECT_MAX_FAILURES	3
4260
+#define NONBLOCK_MAX_BOGUS_LOOPS	10
4261 4261
 
4262 4262
 static void *
4263 4263
 #ifdef	CL_THREAD_SAFE
... ...
@@ -4284,7 +4290,7 @@ getURL(struct arg *arg)
4284 4284
 	static int tcp;
4285 4285
 	int doingsite, firstpacket;
4286 4286
 	char *ptr;
4287
-	int flags;
4287
+	int flags, via_proxy;
4288 4288
 	const char *proxy;
4289 4289
 
4290 4290
 	if(strlen(url) > (sizeof(site) - 1)) {
... ...
@@ -4328,7 +4334,10 @@ getURL(struct arg *arg)
4328 4328
 	ptr = site;
4329 4329
 
4330 4330
 	proxy = getenv("http_proxy");	/* FIXME: handle no_proxy */
4331
-	if(proxy && *proxy) {
4331
+
4332
+	via_proxy = (proxy && *proxy);
4333
+
4334
+	if(via_proxy) {
4332 4335
 		if(strncasecmp(proxy, "http://", 7) != 0) {
4333 4336
 			cli_warnmsg("Unsupported proxy protocol\n");
4334 4337
 			fclose(fp);
... ...
@@ -4419,13 +4428,15 @@ getURL(struct arg *arg)
4419 4419
 	/*
4420 4420
 	 * TODO: consider HTTP/1.1
4421 4421
 	 */
4422
-	if(proxy && *proxy)
4422
+	if(via_proxy)
4423 4423
 		snprintf(buf, sizeof(buf) - 1,
4424 4424
 			"GET %s HTTP/1.0\nUser-Agent: www.clamav.net\n\n", url);
4425 4425
 	else
4426 4426
 		snprintf(buf, sizeof(buf) - 1,
4427 4427
 			"GET /%s HTTP/1.0\nUser-Agent: www.clamav.net\n\n", url);
4428 4428
 
4429
+	cli_dbgmsg("%s\n", buf);
4430
+
4429 4431
 	if(send(sd, buf, strlen(buf), 0) < 0) {
4430 4432
 		close(sd);
4431 4433
 		fclose(fp);
... ...
@@ -4480,11 +4491,34 @@ getURL(struct arg *arg)
4480 4480
 				cli_dbgmsg("HTTP status %d\n", status);
4481 4481
 
4482 4482
 				free(statusptr);
4483
+
4484
+				if((status == 301) || (status == 302)) {
4485
+					char *location;
4486
+
4487
+					location = strstr(buf, "\nLocation: ");
4488
+
4489
+					/*
4490
+					 * FIXME: it may not be in this packet
4491
+					 */
4492
+					if(location) {
4493
+						char *end;
4494
+
4495
+						fclose(fp);
4496
+						close(sd);
4497
+						unlink(fout);
4498
+
4499
+						location += 11;
4500
+						free(arg->url);
4501
+						end = location;
4502
+						while(*end && (*end != '\n'))
4503
+							end++;
4504
+						*end = '\0';
4505
+						arg->url = strdup(location);
4506
+						cli_dbgmsg("Redirecting to %s\n", arg->url);
4507
+						return getURL(arg);
4508
+					}
4509
+				}
4483 4510
 			}
4484
-			/*
4485
-			 * TODO: follow 301 and 302
4486
-			 * (look for Location in the header)
4487
-			 */
4488 4511
 			firstpacket = 0;
4489 4512
 		}
4490 4513