It would be handy if we had a single URL for binary package installation
that was both simple to remember and would hit a random mirror.  In the
same way that "ftp#.freebsd.org" always gets you to data, I'd like to have
something convenient.

>From the testing I've done so far, it looks like if pkg_add is sent to a
HTTP URL that returns a random Location: line that points at a mirror, it
will redirect correctly to a directory to find the file.  The correct
package name needs to be pulled from the request and re-tacked onto the
end of the next request for the file.

So, if we had a program on dragonflybsd.org that returned a Location
header with the requested file's name tacked on it, we'd have an
always-balanced, easy-to-remember source for pkgsrc binaries.

The program to do the redirect would be something like the below:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>

static int VerboseOpt;
static const char *FilePath;
static const char *FileName;
static char *Av0;

static const char *filecomp(const char *path);

char *Mirrors[] = {
    
"ftp://packages.stura.uni-rostock.de/pkgsrc-current/DragonFly/RELEASE/i386/All/";,
    "http://chlamydia.fs.ei.tum.de/pub/DragonFly/packages/RELEASE/i386/All/";,
    
"ftp://ftp.pvv.ntnu.no/pub/DragonFly/packages/pkgsrc-current/DragonFly/RELEASE/i386/All/";,
    "http://theshell.com/pub/DragonFly/packages/RELEASE/i386/All/";,
    "http://www.gobsd.com/pkgsrc/DragonFly/RELEASE/i386/All/";,
    "ftp://ftp.jaist.ac.jp/pub/DragonFly/packages/DEVELOPMENT/i386/";
};

int
main(int ac, char **av)
{
    FILE *fi;
    char *base;
    int i;

    /*
     * Process options
     */
    Av0 = av[0];
    for (i = 1; i < ac; ++i) {
   char *ptr = av[i];
   if (*ptr != '-') {
       FilePath = ptr;
       continue;
   }
   ptr += 2;
   switch(ptr[-1]) {
   case 'v':
       VerboseOpt = 1;
       break;
   }
    }

    /*
     * Output headers and HTML start.
     */

    if (FilePath == NULL) {
   fprintf(stderr, "%s: no file specified\n", av[0]);
   exit(1);
    }
    FileName = filecomp(FilePath);

    srandom(time(NULL));
    printf("Location: %s",
Mirrors[random()%(sizeof(Mirrors)/sizeof(Mirrors[0]))]);
    printf("%s", FileName);
    printf("\r\n\r\n");
}

static
const char *
filecomp(const char *path)
{
    const char *ptr;

    if ((ptr = strrchr(path, '/')) != NULL)
   return(ptr + 1);
    else
   return(path);
}


Reply via email to