Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 14 Aug 1996 14:49:22 +0200 (MET DST)
From:      Mikael Hybsch <micke@dynas.se>
To:        freebsd-current@freebsd.org
Subject:   Patch to allow fetch to use a proxy. + BUGFIX
Message-ID:  <Pine.GSO.3.94.960814121658.9490A-100000@spirit.dynas.se>

next in thread | raw e-mail | index | archive | help
The following patch allows fetch to use a http proxy like Harvest cached.
This is necessary if you are behind an application firewall (like I am).
By setting the env. variable HTTP_PROXY to host:port it will
always use that proxy to retrieve the ftp and http files.

There are also 4 (bug)fixes.

1) The regexp used to find the blank line terminating the headers in
the http response found the last blank line instead of the first.
This happens if there were a blank line at the beginning of the data.
Removing the leading ".*" fixed that problem.

2) Removed the requirement that a filename must be specified when you
specify a http URL. It is now possible to get the index page without
knowing it's name by saying "fetch http://www.freebsd.org".

3) If no filename is given in the URL and no -o option the hostname
will be used as the output filename.
(Before if you tried to "fetch http://www.freebsd.org/" fetch would try
 to create the file "").

4) Use \r\n instead of \n in the http request. This is standard
and Harvest Cached didn't work without it. (At least the version=20
we're running here).


diff -ur /usr/src/usr.bin/fetch/fetch.1 ./fetch.1
--- /usr/src/usr.bin/fetch/fetch.1=09Mon Jul 22 09:55:46 1996
+++ ./fetch.1=09Wed Aug 14 14:41:08 1996
@@ -99,6 +99,14 @@
=20
 .Ev FTP_PASSIVE_MODE
 will force the use of passive mode FTP for firewalls.
+
+If
+.Ev HTTP_PROXY
+is set to a value of the form
+.Em host:port
+it specifies the address of a http proxy. The proxy will be used
+for all ftp and http requests. This is usefull if you are behind
+an application firewall.
 .Sh SEE ALSO
 .Xr tftp 1
 .Xr ftp 1
diff -ur /usr/src/usr.bin/fetch/main.c ./main.c
--- /usr/src/usr.bin/fetch/main.c=09Tue Aug 13 15:26:32 1996
+++ ./main.c=09Wed Aug 14 14:37:04 1996
@@ -60,6 +60,7 @@
 char *host =3D 0;
 int passive_mode =3D 0;
 char *file_to_get =3D 0;
+int http_proxy =3D 0;
 int http =3D 0;
 int http_port =3D 80;
 int mirror =3D 0;
@@ -71,7 +72,8 @@
 void usage (), die (), rm (), t_out (), ftpget (), httpget (),
     display (int, int), parse (char *), output_file_name(),
     f_size (char *, int *, time_t *), ftperr (FILE* ftp, char *, ...),
-    filter (unsigned char *, int);
+    filter (unsigned char *, int),
+    setup_http_proxy();
 int match (char *, char *), http_open ();
=20
 void
@@ -193,6 +195,8 @@
 =09signal (SIGQUIT, die);
 =09signal (SIGTERM, die);
=20
+=09setup_http_proxy();
+
 =09if (http)
 =09    httpget ();
 =09else
@@ -391,11 +395,10 @@
 =09=09char *q;
 =09=09s +=3D 7;
 =09=09p =3D strchr (s, '/');
-=09=09if (!p) {
-=09=09=09fprintf (stderr, "%s: no filename??\n", progname);
-=09=09=09usage ();
-=09=09}
-=09=09*p++ =3D 0;
+=09=09if (!p)
+=09=09=09p =3D s + strlen(s);
+=09=09else
+=09=09=09*p++ =3D 0;
 =09=09q =3D strchr (s, ':');
 =09=09if (q && q < p) {
 =09=09=09*q++ =3D 0;
@@ -437,6 +440,8 @@
 =09=09    p =3D file_to_get;
 =09=09else
 =09=09    p++;
+=09=09if (!*p)
+=09=09=09p =3D host;
 =09=09outputfile =3D strdup (p);
 =09}
 }
@@ -484,7 +489,8 @@
 =09restart =3D 0;
=20
 =09s =3D http_open ();
-=09sprintf (str, "GET /%s HTTP/1.0\n\n", file_to_get);
+=09sprintf (str, "GET %s%s HTTP/1.0\r\n\r\n",=20
+=09=09 http_proxy? "" : "/", file_to_get);
 =09i =3D strlen (str);
 =09if (i !=3D write (s, str, i))
 =09    err (1, "could not send GET command to HTTP server.");
@@ -580,7 +586,7 @@
 =09=09=09if (i > 0)
 =09=09=09    size =3D atoi (s+i);
 =09=09=09/* assume that the file to get begins after an empty line */
-=09=09=09i =3D match (".*(\n\n|\r\n\r\n)", s);
+=09=09=09i =3D match ("(\n\n|\r\n\r\n)", s);
 =09=09=09if (i > 0) {
 =09=09=09=09if (s[i] =3D=3D '\r')
 =09=09=09=09    t =3D s+i+4;
@@ -660,3 +666,43 @@
 {
 =09printf ("%s", p);
 }
+
+void
+setup_http_proxy()
+{
+=09char *e;
+=09char *p;
+=09char *url;
+=09unsigned short port;
+
+=09if (!(e =3D getenv("HTTP_PROXY"))
+=09    || !(p =3D strchr(e, ':'))
+=09    || (port =3D atoi(p+1)) =3D=3D 0)
+=09=09return;
+
+=09if (!(url =3D (char *) malloc (strlen(file_to_get)=20
+=09=09=09=09+ strlen(host)=20
+=09=09=09=09+ (change_to_dir ? strlen(change_to_dir) : 0)
+=09=09=09=09+ 50)))
+=09=09return;
+
+=09if (http) {
+=09=09sprintf(url, "http://%s:%d/%s",
+=09=09=09host, http_port, file_to_get);
+=09} else {
+=09=09if (change_to_dir) {
+=09=09=09sprintf(url, "ftp://%s/%s/%s",=20
+=09=09=09=09host, change_to_dir, file_to_get);
+=09=09} else {
+=09=09=09sprintf(url, "ftp://%s/%s", host, file_to_get);
+=09=09}
+=09}
+=09file_to_get =3D url;
+
+=09*p =3D 0;
+=09host =3D strdup(e);
+=09http_port =3D port;
+=09http =3D 1;
+=09http_proxy =3D 1;
+}
+

--=20
 Mikael H=FCbsch                        Email: micke@dynas.se
 DynaSoft, Dynamic Software AB        Phone: +46-8-7250900
 Box 10704=09=09=09      Fax:   +46-8-6494970
 S-121 29 STOCKHOLM, SWEDEN






Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.GSO.3.94.960814121658.9490A-100000>