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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/* 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];
static int
get_url_socket(machine,port)
char* machine;
int port;
{
int s;
struct sockaddr_in sin;
#ifdef __hpux
int socksize = 0;
int socksizelen = sizeof socksize;
#endif
struct servent* sp;
struct hostent* hp;
#ifdef h_addr
int x = 0;
register char** cp;
static char* alist[1];
#endif /* h_addr */
static struct hostent def;
static struct in_addr defaddr;
static char namebuf[256];
if (port) {
if ((sp = getservbyport(htons(port),"tcp")) == NULL) {
fprintf(stderr, "port %d/tcp: Unknown service.\n", port);
return -1;
}
}
else {
if ((sp = getservbyname("www", "tcp")) == NULL) {
fprintf(stderr, "www/tcp: Unknown service.\n");
return -1;
}
}
/* If not a raw ip address, try nameserver */
if (!isdigit(*machine)
#ifdef INADDR_NONE
|| (defaddr.s_addr = inet_addr(machine)) == INADDR_NONE)
#else
|| (long)(defaddr.s_addr = inet_addr(machine)) == -1)
#endif
hp = gethostbyname(machine);
else {
/* Raw ip address, fake */
(void) strcpy(namebuf, machine);
def.h_name = namebuf;
#ifdef h_addr
def.h_addr_list = alist;
#endif
def.h_addr = (char*)&defaddr;
def.h_length = sizeof(struct in_addr);
def.h_addrtype = AF_INET;
def.h_aliases = 0;
hp = &def;
}
if (hp == NULL) {
fprintf(stderr, "%s: Unknown host.\n", machine);
return -1;
}
bzero((char*)&sin, sizeof sin);
sin.sin_family = hp->h_addrtype;
sin.sin_port = sp->s_port;
/* The following is kinda gross. The name server under 4.3
** returns a list of addresses, each of which should be tried
** in turn if the previous one fails. However, 4.2 hostent
** structure doesn't have this list of addresses.
** Under 4.3, h_addr is a #define to h_addr_list[0].
** We use this to figure out whether to include the NS specific
** code... */
#ifdef h_addr
/* get a socket and initiate connection -- use multiple addresses */
for (cp = hp->h_addr_list; cp && *cp; cp++) {
extern char* inet_ntoa _((const struct in_addr));
s = socket(hp->h_addrtype, SOCK_STREAM, 0);
if (s < 0) {
perror("socket");
return -1;
}
bcopy(*cp, (char*)&sin.sin_addr, hp->h_length);
if (x < 0)
fprintf(stderr, "trying %s\n", inet_ntoa(sin.sin_addr));
x = connect(s, (struct sockaddr*)&sin, sizeof (sin));
if (x == 0)
break;
fprintf(stderr, "connection to %s: ", inet_ntoa(sin.sin_addr));
perror("");
(void) close(s);
}
if (x < 0) {
fprintf(stderr, "giving up...\n");
return -1;
}
#else /* no name server */
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
return -1;
}
/* And then connect */
bcopy(hp->h_addr, (char*)&sin.sin_addr, hp->h_length);
if (connect(s, (struct sockaddr*)&sin, sizeof sin) < 0) {
perror("connect");
(void) close(s);
return -1;
}
#endif /* !h_addr */
#ifdef __hpux /* recommended by raj@cup.hp.com */
#define HPSOCKSIZE 0x8000
getsockopt(s, SOL_SOCKET, SO_SNDBUF, (caddr_t)&socksize, (caddr_t)&socksizelen);
if (socksize < HPSOCKSIZE) {
socksize = HPSOCKSIZE;
setsockopt(s, SOL_SOCKET, SO_SNDBUF, (caddr_t)&socksize, sizeof(socksize));
}
socksize = 0;
socksizelen = sizeof(socksize);
getsockopt(s, SOL_SOCKET, SO_RCVBUF, (caddr_t)&socksize, (caddr_t)&socksizelen);
if (socksize < HPSOCKSIZE) {
socksize = HPSOCKSIZE;
setsockopt(s, SOL_SOCKET, SO_RCVBUF, (caddr_t)&socksize, sizeof(socksize));
}
#endif
return s;
}
/* 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_url_socket(host,port);
/* 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;
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 */
|