From fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 8 Jul 2015 20:46:52 -0400 Subject: Initial commit --- sys/imio/imt/t_urlget.x | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 sys/imio/imt/t_urlget.x (limited to 'sys/imio/imt/t_urlget.x') diff --git a/sys/imio/imt/t_urlget.x b/sys/imio/imt/t_urlget.x new file mode 100644 index 00000000..e0e7bf26 --- /dev/null +++ b/sys/imio/imt/t_urlget.x @@ -0,0 +1,94 @@ +# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc. + +include +include +include +include +include + +task urlget = t_urlget + + + +# URLGET -- Do an HTTP GET of a URL to the named file. + +procedure t_urlget () + +pointer reply +char url[SZ_PATHNAME], fname[SZ_PATHNAME], extn[SZ_PATHNAME] +char cache[SZ_PATHNAME], lfname[SZ_PATHNAME] +int nread +bool use_cache, verbose + +int url_get() +bool fcaccess() + +begin + # Get the parameters + call clgstr ("url", url, SZ_PATHNAME) + + call url_to_name (url, fname, SZ_PATHNAME) + call strcpy ("", extn, SZ_PATHNAME) + call strcpy ("/tmp/cache/", cache, SZ_PATHNAME) + verbose = true + use_cache = false + + + # Tell them what we're doing. + if (verbose) { + call printf ("%s -> %s\n") + call pargstr (url) + call pargstr (fname) + call flush (STDOUT) + } + + # Retrieve the URL. + if (use_cache) { + call aclrc (lfname, SZ_FNAME); + + if (fcaccess (cache, url, "fits")) { + call fcname (cache, url, "f", lfname, SZ_PATHNAME) + if (extn[1] != EOS) { + # Add an extension to the cached file. + call strcat (".", lfname, SZ_PATHNAME) + call strcat (extn, lfname, SZ_PATHNAME) + } + } else { + # Add it to the cache, also handles the download. + call fcadd (cache, url, extn, lfname, SZ_PATHNAME) + } + call fcopy (lfname, fname) + + } else { + # Not in cache, or not using the cache, so force the download. + call calloc (reply, SZ_LINE, TY_CHAR) + nread = url_get (url, fname, reply) + call mfree (reply, TY_CHAR) + } +end + + +# URL_TO_NAME -- Generate a filename from a URL. + +procedure url_to_name (url, name, maxch) + +char url[ARB] #i URL being accessed +char name[ARB] #o output name +int maxch #i max size of output name + +int ip, strlen() +char ch + +begin + ip = strlen (url) + while (ip > 1) { + ch = url[ip] + if (ch == '/' || ch == '?' || ch == '&' || ch == ';' || ch == '=') { + call strcpy (url[ip+1], name, maxch) + return + } + ip = ip - 1 + } + + call strcpy (url[ip], name, maxch) +end -- cgit