forked from alandipert/ncsa-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTAccess.c
650 lines (548 loc) · 15.8 KB
/
HTAccess.c
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/* Access Manager HTAccess.c
** ==============
**
** Authors
** TBL Tim Berners-Lee timbl@info.cern.ch
** JFG Jean-Francois Groff jfg@dxcern.cern.ch
** DD Denis DeLaRoca (310) 825-4580 <CSP1DWD@mvs.oac.ucla.edu>
** History
** 8 Jun 92 Telnet hopping prohibited as telnet is not secure TBL
** 26 Jun 92 When over DECnet, suppressed FTP, Gopher and News. JFG
** 6 Oct 92 Moved HTClientHost and logfile into here. TBL
** 17 Dec 92 Tn3270 added, bug fix. DD
** 4 Feb 93 Access registration, Search escapes bad chars TBL
** PARAMETERS TO HTSEARCH AND HTLOADRELATIVE CHANGED
** 28 May 93 WAIS gateway explicit if no WAIS library linked in.
**
** Bugs
** This module assumes that that the graphic object is hypertext, as it
** needs to select it when it has been loaded. A superclass needs to be
** defined which accepts select and select_anchor.
*/
#include "../config.h"
#ifndef DEFAULT_WAIS_GATEWAY
#define DEFAULT_WAIS_GATEWAY "http://www.ncsa.uiuc.edu:8001/"
#endif
/* Implements:
*/
#include "HTAccess.h"
/* Uses:
*/
#include "HTParse.h"
#include "HTUtils.h"
#include "HTML.h" /* SCW */
#include <stdio.h>
#include "HTList.h"
#include "HText.h" /* See bugs above */
#include "HTAlert.h"
#include "../src/mosaic.h"
#include "../src/gui.h"
#include "../src/mo-www.h"
#include "../src/proxy.h"
#include "../src/proxy-dialogs.h"
#ifndef DISABLE_TRACE
extern int www2Trace;
#endif
extern char *mo_check_for_proxy (char *);
int has_fallbacks(char *protocol);
char *currentURL=NULL;
/* These flags may be set to modify the operation of this module
*/
PUBLIC char * HTClientHost = 0; /* Name of remote login host if any */
/* To generate other things, play with these:
*/
PUBLIC HTFormat HTOutputFormat = NULL;
PUBLIC HTStream* HTOutputStream = NULL; /* For non-interactive, set this */
PUBLIC BOOL using_gateway = NO; /* are we using a gateway? */
PUBLIC BOOL using_proxy = NO; /* are we using a proxy gateway? */
PUBLIC char *proxy_host_fix=NULL; /* Host: header fix */
PRIVATE HTList * protocols = NULL; /* List of registered protocol descriptors */
/* Register a Protocol HTRegisterProtocol
** -------------------
*/
PUBLIC BOOL HTRegisterProtocol(protocol)
HTProtocol * protocol;
{
if (!protocols) protocols = HTList_new();
HTList_addObject(protocols, protocol);
return YES;
}
/* Register all known protocols
** ----------------------------
**
** Add to or subtract from this list if you add or remove protocol modules.
** This routine is called the first time the protocol list is needed,
** unless any protocols are already registered, in which case it is not called.
** Therefore the application can override this list.
*/
PRIVATE void HTAccessInit NOARGS /* Call me once */
{
extern HTProtocol HTTP, HTFile, HTTelnet, HTTn3270, HTRlogin;
extern HTProtocol HTFTP, HTNews, HTGopher, HTMailto, HTNNTP;
#ifdef DIRECT_WAIS
extern HTProtocol HTWAIS;
#endif
HTRegisterProtocol(&HTFTP);
HTRegisterProtocol(&HTNews);
HTRegisterProtocol(&HTGopher);
#ifdef DIRECT_WAIS
HTRegisterProtocol(&HTWAIS);
#endif
HTRegisterProtocol(&HTTP);
HTRegisterProtocol(&HTFile);
HTRegisterProtocol(&HTTelnet);
HTRegisterProtocol(&HTTn3270);
HTRegisterProtocol(&HTRlogin);
HTRegisterProtocol(&HTMailto);
HTRegisterProtocol(&HTNNTP);
}
/* Find physical name and access protocol
** --------------------------------------
**
**
** On entry,
** addr must point to the fully qualified hypertext reference.
** anchor a pareent anchor with whose address is addr
**
** On exit,
** returns HT_NO_ACCESS Error has occured.
** HT_OK Success
**
*/
PRIVATE int get_physical ARGS3(
char *, addr,
HTParentAnchor *, anchor,
int, bong)
{
char * access=NULL; /* Name of access method */
char * host = NULL;
struct Proxy *GetNoProxy();
extern int useKeepAlive;
HTAnchor_setPhysical(anchor, addr);
access = HTParse(HTAnchor_physical(anchor),
"file:", PARSE_ACCESS);
host = HTParse(HTAnchor_physical(anchor), "", PARSE_HOST);
/*
** set useKeepAlive to the default here because the last pass might
** have turned this off, and the default might have been on.
*/
/*
Init_useKeepAlive();
*/
useKeepAlive=1;
/* Check whether gateway access has been set up for this
*/
#define USE_GATEWAYS
#ifdef USE_GATEWAYS
/* make sure the using_proxy variable is false */
using_proxy = NO;
if (proxy_host_fix) {
free(proxy_host_fix);
proxy_host_fix=NULL;
}
{
char *tmp_access,*tmp_host,*ptr;
for (ptr=tmp_access=strdup(access); ptr && *ptr; ptr++) *ptr=tolower(*ptr);
for (ptr=tmp_host=strdup(host); ptr && *ptr; ptr++) *ptr=tolower(*ptr);
if (!GetNoProxy(tmp_access, tmp_host)) {
char *gateway_parameter, *gateway, *proxy;
struct Proxy *proxent = NULL, *GetProxy();
char *proxyentry = NULL;
proxy_host_fix=strdup(tmp_host);
/* search for gateways */
gateway_parameter = (char *)malloc(strlen(tmp_access)+20);
if (gateway_parameter == NULL) outofmem(__FILE__, "HTLoad");
strcpy(gateway_parameter, "WWW_");
strcat(gateway_parameter, tmp_access);
strcat(gateway_parameter, "_GATEWAY");
gateway = (char *)getenv(gateway_parameter); /* coerce for decstation */
/* search for proxy servers */
strcpy(gateway_parameter, tmp_access);
strcat(gateway_parameter, "_proxy");
proxy = (char *)getenv(gateway_parameter);
free(gateway_parameter);
/*
* Check the proxies list
*/
if ((proxy == NULL)||(proxy[0] == '\0')) {
int fMatchEnd;
char *scheme_info;
scheme_info =HTParse(HTAnchor_physical(anchor), "", PARSE_HOST);
fMatchEnd = 1; /* match hosts from the end */
if ((scheme_info != NULL) && (*scheme_info == '\0')) {
scheme_info = HTParse(HTAnchor_physical(anchor), "", PARSE_PATH);
fMatchEnd = 0; /* match other scheme_info at beginning*/
}
if (bong) { /* this one is bad - disable! */
proxent =
GetProxy(tmp_access, scheme_info, fMatchEnd);
if (proxent != NULL) proxent->alive = bong;
}
proxent = GetProxy(tmp_access, scheme_info, fMatchEnd);
if (proxent != NULL) {
useKeepAlive = 0; /* proxies don't keepalive */
StrAllocCopy(proxyentry, proxent->transport);
StrAllocCat(proxyentry, "://");
StrAllocCat(proxyentry, proxent->address);
StrAllocCat(proxyentry, ":");
StrAllocCat(proxyentry, proxent->port);
StrAllocCat(proxyentry, "/");
proxy = proxyentry;
}
}
#ifndef DIRECT_WAIS
if (!gateway && 0==strcmp(tmp_access, "wais")) {
gateway = DEFAULT_WAIS_GATEWAY;
}
#endif
/* proxy servers have precedence over gateway servers */
if (proxy) {
char * gatewayed;
gatewayed = NULL;
StrAllocCopy(gatewayed,proxy);
StrAllocCat(gatewayed,addr);
using_proxy = YES;
HTAnchor_setPhysical(anchor, gatewayed);
free(gatewayed);
free(access);
if (proxyentry)
free(proxyentry);
access = HTParse(HTAnchor_physical(anchor),
"http:", PARSE_ACCESS);
} else if (gateway) {
char * gatewayed;
gatewayed = NULL;
StrAllocCopy(gatewayed,gateway);
StrAllocCat(gatewayed,addr);
using_gateway = YES;
HTAnchor_setPhysical(anchor, gatewayed);
free(gatewayed);
free(access);
access = HTParse(HTAnchor_physical(anchor),
"http:", PARSE_ACCESS);
} else {
if (proxy_host_fix) {
free(proxy_host_fix);
}
proxy_host_fix=NULL;
using_proxy = NO;
using_gateway = NO;
ClearTempBongedProxies();
}
}
free(tmp_access);
free(tmp_host);
}
#endif
/* Search registered protocols to find suitable one
*/
{
int i, n;
if (!protocols) HTAccessInit();
n = HTList_count(protocols);
for (i=0; i<n; i++) {
HTProtocol *p = HTList_objectAt(protocols, i);
if (strcmp(p->name, access)==0) {
HTAnchor_setProtocol(anchor, p);
free(access);
return (HT_OK);
}
}
}
free(access);
return HT_NO_ACCESS;
}
/* Load a document
** ---------------
**
** This is an internal routine, which has an address AND a matching
** anchor. (The public routines are called with one OR the other.)
**
** On entry,
** addr must point to the fully qualified hypertext reference.
** anchor a pareent anchor with whose address is addr
**
** On exit,
** returns <0 Error has occured.
** HT_LOADED Success
** HT_NO_DATA Success, but no document loaded.
** (telnet sesssion started etc)
**
*/
PRIVATE int HTLoad ARGS4(
WWW_CONST char *, addr,
HTParentAnchor *, anchor,
HTFormat, format_out,
HTStream *, sink)
{
HTProtocol* p;
int ret, status = get_physical(addr, anchor, 0);
int retry=5;
static char *buf1="Do you want to disable the proxy server:\n\n";
static char *buf2="\n\nAlready attempted 5 contacts.";
char *finbuf,*host;
int fallbacks;
/*
* Yes...I know I'm only calling this to get the "name", but it is better
* than looping through all of the proxy list everytime a proxy fails!
* --SWP
*/
p = HTAnchor_protocol(anchor);
if (!p) {
return(HT_NOT_LOADED);
}
fallbacks=has_fallbacks(p->name);
while (1) {
if (status < 0) return status; /* Can't resolve or forbidden */
retry=5;
retry_proxy:
p = HTAnchor_protocol(anchor);
ret = (*(p->load))(HTAnchor_physical(anchor),
anchor, format_out, sink);
if (ret==HT_INTERRUPTED || HTCheckActiveIcon(0)==HT_INTERRUPTED) {
if (using_proxy) {
ClearTempBongedProxies();
}
return(HT_INTERRUPTED);
}
/*
* HT_REDIRECTING supplied by Dan Riley -- dsr@lns598.lns.cornell.edu
*/
if (!using_proxy || !fallbacks
|| ret == HT_LOADED || ret == HT_REDIRECTING
|| (ret == HT_NO_DATA && strncmp((char *)anchor, "telnet", 6) == 0)) {
if (using_proxy) {
ClearTempBongedProxies();
}
return(ret);
}
if (retry>0) {
retry--;
HTProgress("Retrying proxy server...");
goto retry_proxy;
}
/* must be using proxy and have a problem to get here! */
host=HTParse(HTAnchor_physical(anchor), "", PARSE_HOST);
finbuf=(char *)calloc((strlen(host)+
strlen(buf1)+
strlen(buf2)+
5),
sizeof(char));
sprintf(finbuf,"%s%s?%s",buf1,host,buf2);
if (HTConfirm(finbuf)) {
free(finbuf);
finbuf=(char *)calloc((strlen(host)+
strlen("Disabling proxy server ")+
strlen(" and trying again.")+
5),
sizeof(char));
sprintf(finbuf,"Disabling proxy server %s and trying again.",
host);
HTProgress(finbuf);
application_user_feedback(finbuf);
free(finbuf);
finbuf=NULL;
status = get_physical(addr, anchor, 1); /* Perm disable */
}
else if (HTConfirm("Try next fallback proxy server?")) {
status = get_physical(addr, anchor, 2); /* Temp disable */
}
/* else -- Try the same one again */
if (finbuf) {
free(finbuf);
}
}
}
/* Get a save stream for a document
** --------------------------------
*/
PUBLIC HTStream *HTSaveStream ARGS1(HTParentAnchor *, anchor)
{
HTProtocol * p = HTAnchor_protocol(anchor);
if (!p) return NULL;
return (*p->saveStream)(anchor);
}
/* Load a document - with logging etc
** ----------------------------------
**
** - Checks or documents already loaded
** - Logs the access
** - Allows stdin filter option
** - Trace ouput and error messages
**
** On Entry,
** anchor is the node_anchor for the document
** full_address The address of the document to be accessed.
** filter if YES, treat stdin as HTML
**
** On Exit,
** returns 1 Success in opening document
** 0 Failure
** -1 Interrupted
**
*/
/* This is exported all the way to gui-documents.c at the moment,
to tell mo_load_window_text when to use a redirected URL instead. */
char *use_this_url_instead;
PRIVATE int HTLoadDocument ARGS4(
WWW_CONST char *, full_address,
HTParentAnchor *, anchor,
HTFormat, format_out,
HTStream*, sink)
{
int status;
use_this_url_instead = NULL;
/* We LOVE goto's!
*
* Let's rephrase this..._You_ love goto's...we _abhore_ goto's. People who
* LOVE goto's should be shot.
*/
try_again:
#ifndef DISABLE_TRACE
if (www2Trace) fprintf (stderr,
"HTAccess: loading document %s\n", full_address);
#endif
status = HTLoad(full_address, anchor, format_out, sink);
if (status == HT_LOADED) {
#ifndef DISABLE_TRACE
if (www2Trace) {
fprintf(stderr, "HTAccess: `%s' has been accessed.\n",
full_address);
}
#endif
return 1;
}
if (status == HT_REDIRECTING)
{
/* Exported from HTMIME.c, of all places. */
extern char *redirecting_url;
#ifndef DISABLE_TRACE
if (www2Trace)
{
fprintf (stderr, "HTAccess: '%s' is a redirection URL.\n",
full_address);
fprintf (stderr, "HTAccess: Redirecting to '%s'\n",
redirecting_url);
}
#endif
full_address = redirecting_url;
use_this_url_instead = full_address;
goto try_again;
}
if (status == HT_INTERRUPTED)
{
#ifndef DISABLE_TRACE
if (www2Trace)
fprintf (stderr,
"HTAccess: We were interrupted.\n");
#endif
return -1;
}
if (status == HT_NO_DATA) {
#ifndef DISABLE_TRACE
if (www2Trace) {
fprintf(stderr,
"HTAccess: `%s' has been accessed, No data left.\n",
full_address);
}
#endif
return 0;
}
if (status<0) { /* Failure in accessing a document */
#ifndef DISABLE_TRACE
if (www2Trace) fprintf(stderr,
"HTAccess: Can't access `%s'\n", full_address);
#endif
return 0;
}
/* If you get this, then please find which routine is returning
a positive unrecognised error code! */
#ifndef DISABLE_TRACE
if (www2Trace)
fprintf(stderr,
"**** HTAccess: socket or file number %d returned by obsolete load routine!\n", status);
#endif
return 0;
} /* HTLoadDocument */
/* Load a document from absolute name
** ---------------
**
** On Entry,
** addr The absolute address of the document to be accessed.
** filter if YES, treat document as HTML
**
** On Exit,
** returns 1 Success in opening document
** 0 Failure
** -1 Interrupted
**
**
*/
PUBLIC int HTLoadAbsolute ARGS1(WWW_CONST char *,addr)
{
if (currentURL) {
free(currentURL);
}
currentURL=strdup(addr);
return HTLoadDocument( addr,
HTAnchor_parent(HTAnchor_findAddress(addr)),
HTOutputFormat ? HTOutputFormat : WWW_PRESENT,
HTOutputStream);
}
/* Load a document from absolute name to stream
** --------------------------------------------
**
** On Entry,
** addr The absolute address of the document to be accessed.
** sink if non-NULL, send data down this stream
**
** On Exit,
** returns YES Success in opening document
** NO Failure
**
**
*/
PUBLIC BOOL HTLoadToStream ARGS3(
WWW_CONST char *, addr,
BOOL, filter,
HTStream *, sink)
{
return HTLoadDocument(addr,
HTAnchor_parent(HTAnchor_findAddress(addr)),
HTOutputFormat ? HTOutputFormat : WWW_PRESENT,
sink);
}
/* Load a document from relative name
** ---------------
**
** On Entry,
** relative_name The relative address of the document
** to be accessed.
**
** On Exit,
** returns YES Success in opening document
** NO Failure
**
**
*/
PUBLIC BOOL HTLoadRelative ARGS2(
WWW_CONST char *, relative_name,
HTParentAnchor *, here)
{
char * full_address = 0;
BOOL result;
char * mycopy = 0;
char * stripped = 0;
char * current_address =
HTAnchor_address((HTAnchor*)here);
StrAllocCopy(mycopy, relative_name);
stripped = HTStrip(mycopy);
full_address = HTParse(stripped,
current_address,
PARSE_ACCESS|PARSE_HOST|PARSE_PATH|PARSE_PUNCTUATION);
result = HTLoadAbsolute(full_address);
free(full_address);
free(current_address);
free(mycopy);
return result;
}