aboutsummaryrefslogtreecommitdiff
path: root/sys/imio/imt/t_urlget.x
blob: e0e7bf266931122838fd1696d9250018bb789ea2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<error.h>
include	<syserr.h>
include	<imhdr.h>
include	<imset.h>
include <mach.h>

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