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>
index | next in thread | raw e-mail
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 we're running here). diff -ur /usr/src/usr.bin/fetch/fetch.1 ./fetch.1 --- /usr/src/usr.bin/fetch/fetch.1 Mon Jul 22 09:55:46 1996 +++ ./fetch.1 Wed Aug 14 14:41:08 1996 @@ -99,6 +99,14 @@ .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 Tue Aug 13 15:26:32 1996 +++ ./main.c Wed Aug 14 14:37:04 1996 @@ -60,6 +60,7 @@ char *host = 0; int passive_mode = 0; char *file_to_get = 0; +int http_proxy = 0; int http = 0; int http_port = 80; int mirror = 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 (); void @@ -193,6 +195,8 @@ signal (SIGQUIT, die); signal (SIGTERM, die); + setup_http_proxy(); + if (http) httpget (); else @@ -391,11 +395,10 @@ char *q; s += 7; p = strchr (s, '/'); - if (!p) { - fprintf (stderr, "%s: no filename??\n", progname); - usage (); - } - *p++ = 0; + if (!p) + p = s + strlen(s); + else + *p++ = 0; q = strchr (s, ':'); if (q && q < p) { *q++ = 0; @@ -437,6 +440,8 @@ p = file_to_get; else p++; + if (!*p) + p = host; outputfile = strdup (p); } } @@ -484,7 +489,8 @@ restart = 0; s = http_open (); - sprintf (str, "GET /%s HTTP/1.0\n\n", file_to_get); + sprintf (str, "GET %s%s HTTP/1.0\r\n\r\n", + http_proxy? "" : "/", file_to_get); i = strlen (str); if (i != write (s, str, i)) err (1, "could not send GET command to HTTP server."); @@ -580,7 +586,7 @@ if (i > 0) size = atoi (s+i); /* assume that the file to get begins after an empty line */ - i = match (".*(\n\n|\r\n\r\n)", s); + i = match ("(\n\n|\r\n\r\n)", s); if (i > 0) { if (s[i] == '\r') t = s+i+4; @@ -660,3 +666,43 @@ { printf ("%s", p); } + +void +setup_http_proxy() +{ + char *e; + char *p; + char *url; + unsigned short port; + + if (!(e = getenv("HTTP_PROXY")) + || !(p = strchr(e, ':')) + || (port = atoi(p+1)) == 0) + return; + + if (!(url = (char *) malloc (strlen(file_to_get) + + strlen(host) + + (change_to_dir ? strlen(change_to_dir) : 0) + + 50))) + return; + + if (http) { + sprintf(url, "http://%s:%d/%s", + host, http_port, file_to_get); + } else { + if (change_to_dir) { + sprintf(url, "ftp://%s/%s/%s", + host, change_to_dir, file_to_get); + } else { + sprintf(url, "ftp://%s/%s", host, file_to_get); + } + } + file_to_get = url; + + *p = 0; + host = strdup(e); + http_port = port; + http = 1; + http_proxy = 1; +} + -- Mikael Hübsch Email: micke@dynas.se DynaSoft, Dynamic Software AB Phone: +46-8-7250900 Box 10704 Fax: +46-8-6494970 S-121 29 STOCKHOLM, SWEDENhelp
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.GSO.3.94.960814121658.9490A-100000>
