File: url.c

package info (click to toggle)
trn4 4.0-test77-18
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 4,016 kB
  • sloc: ansic: 48,332; sh: 6,795; tcl: 1,696; yacc: 662; perl: 108; makefile: 26
file content (254 lines) | stat: -rw-r--r-- 5,958 bytes parent folder | download | duplicates (6)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* This file Copyright 1993 by Clifford A. Adams */
/* url.c
 *
 * Routines for handling WWW URL references.
 */

#include "EXTERN.h"
#include "common.h"
#ifdef USEURL
#include "term.h"
#include "util.h"
#include "util2.h"
#include "INTERN.h"
#include "url.h"
#include "url.ih"

/* Lower-level net routines grabbed from nntpinit.c.
 * The special cases (DECNET, EXCELAN, and NONETD) are not supported.
 */

/* NOTE: If running Winsock, NNTP must be enabled so that the Winsock
 *       initialization will be done.  (common.h will check for this)
 */
#ifdef WINSOCK
#include <winsock.h>
WSADATA wsaData;
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#endif

#ifndef WINSOCK
unsigned long inet_addr _((char*));
struct servent* getservbyname();
struct hostent* gethostbyname();
#endif

static char url_buf[1030];
/* XXX just a little bit larger than necessary... */
static char url_type[256];
static char url_host[256];
static int  url_port;
static char url_path[1024];

/* returns TRUE if successful */
bool
fetch_http(host,port,path,outname)
char* host;
int port;
char* path;
char* outname;
{
    int sock;
    FILE* fp_out;
    int len;

    sock = get_tcp_socket(host,port,"http");

    /* XXX length check */
    /* XXX later consider using HTTP/1.0 format (and user-agent) */
    sprintf(url_buf, "GET %s\n",path);
    /* Should I be writing the 0 char at the end? */
    if (write(sock, url_buf, strlen(url_buf)+1) < 0) {
	printf("\nError: writing on URL socket\n");
	close(sock);
	return FALSE;
    }

    fp_out = fopen(outname,"w");
    if (!fp_out) {
	printf("\nURL output file could not be opened.\n");
	return FALSE;
    }
    /* XXX some kind of URL timeout would be really nice */
    /* (the old nicebg code caused portability problems) */
    /* later consider larger buffers, spinner */
    while (1) {
	if ((len = read(sock, url_buf, 1024)) < 0) {
	    printf("\nError: reading URL reply\n");
	    return FALSE;
	}
	if (len == 0) {
	    break;	/* no data, end connection */
	}
	fwrite(url_buf,1,len,fp_out);
    }
    fclose(fp_out);
    close(sock);
    return TRUE;
}

/* add port support later? */
bool
fetch_ftp(host,origpath,outname)
char* host;
char* origpath;
char* outname;
{
#ifdef USEFTP
    static char cmdline[1024];
    static char path[512];	/* use to make writable copy */
    /* buffers used because because filexp overwrites previous call results */
    static char username[128];
    static char userhost[128];
    char* p;
    int status;
    char* cdpath;
    int x,y,l;

    safecpy(path,origpath,510);
    p = rindex(path, '/');	/* p points to last slash or NULL*/
    if (p == NULL) {
	printf("Error: URL:ftp path has no '/' character.\n") FLUSH;
	return FALSE;
    }
    if (p[1] == '\0') {
	printf("Error: URL:ftp path has no final filename.\n") FLUSH;
	return FALSE;
    }
    safecpy(username,filexp("%L"),120);
    safecpy(userhost,filexp("%H"),120);
    if (p != path) {	/* not of form /foo */
	*p = '\0';
	cdpath = path;
    } else
	cdpath = "/";

    sprintf(cmdline,"%s/ftpgrab %s ftp %s@%s %s %s %s",
	    filexp("%X"),host,username,userhost,cdpath,p+1,outname);

    /* modified escape_shell_cmd code from NCSA HTTPD util.c */
    /* serious security holes could result without this code */
    l = strlen(cmdline);
    for (x = 0; cmdline[x]; x++) {
	if (index("&;`'\"|*?~<>^()[]{}$\\",cmdline[x])) {
	    for (y = l+1; y > x; y--)
		cmdline[y] = cmdline[y-1];
	    l++; /* length has been increased */
	    cmdline[x] = '\\';
	    x++; /* skip the character */
	}
    }

#if 0
    printf("ftpgrab command:\n|%s|\n",cmdline);
#endif

    *p = '/';
    status = doshell(NULL,cmdline);
#if 0
    printf("\nFTP command status is %d\n",status) FLUSH;
    while (!input_pending()) ;
    eat_typeahead();
#endif
    return TRUE;
#else
    printf("\nThis copy of trn does not have URL:ftp support.\n");
    return FALSE;
#endif
}

/* right now only full, absolute URLs are allowed. */
/* use relative URLs later? */
/* later: pay more attention to long URLs */
bool
parse_url(url)
char* url;
{
    char* s;
    char* p;

    /* consider using 0 as default to look up the service? */
    url_port = 80;	/* the default */
    if (!url || !*url) {
	printf("Empty URL -- ignoring.\n") FLUSH;
	return FALSE;
    }
    p = url_type;
    for (s = url; *s && *s != ':'; *p++ = *s++) ;
    *p = '\0';
    if (!*s) {
	printf("Incomplete URL: %s\n",url) FLUSH;
	return FALSE;
    }
    s++;
    if (strnEQ(s,"//",2)) {
	/* normal URL type, will have host (optional portnum) */
	s += 2;
	p = url_host;
	/* check for address literal: news://[ip:v6:address]:port/ */
	if (*s == '[') {
	    while (*s && *s != ']')
		*p++ = *s++;
	    if (!*s) {
		printf("Bad address literal: %s\n",url) FLUSH;
		return FALSE;
	    }
	    s++;	/* skip ] */
	} else
	    while (*s && *s != '/' && *s != ':') *p++ = *s++;
	*p = '\0';
	if (!*s) {
	    printf("Incomplete URL: %s\n",url) FLUSH;
	    return FALSE;
	}
	if (*s == ':') {
	    s++;
	    p = url_buf;	/* temp space */
	    if (!isdigit(*s)) {
		printf("Bad URL (non-numeric portnum): %s\n",url) FLUSH;
		return FALSE;
	    }
	    while (isdigit(*s)) *p++ = *s++;
	    *p = '\0';
	    url_port = atoi(url_buf);
	}
    } else {
	if (!strEQ(url_type,"news")) {
	    printf("URL needs a hostname: %s\n",url);
	    return FALSE;
	}
    }
    /* finally, just do the path */
    if (*s != '/') {
	printf("Bad URL (path does not start with /): %s\n",url) FLUSH;
	return FALSE;
    }
    strcpy(url_path,s);
    return TRUE;
}

bool
url_get(url,outfile)
char* url;
char* outfile;
{
    bool flag;
    
    if (!parse_url(url))
	return FALSE;

    if (strEQ(url_type,"http"))
	flag = fetch_http(url_host,url_port,url_path,outfile);
    else if (strEQ(url_type,"ftp"))
	flag = fetch_ftp(url_host,url_path,outfile);
    else {
	if (url_type)
	    printf("\nURL type %s not supported (yet?)\n",url_type) FLUSH;
	flag = FALSE;
    }
    return flag;
}
#endif /* USEURL */