-
Notifications
You must be signed in to change notification settings - Fork 0
/
weblink.pl
executable file
·597 lines (535 loc) · 13.3 KB
/
weblink.pl
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
#!/usr/bin/perl -s
#
# *******************************************************************
# * *
# * WEBLINK Copyright (C) 1996 Jozsef Hollosi *
# * *
# * WEBLINK Version 1.04 *
# * *
# * All Rights Reserved. Unpublished rights reserved under *
# * the copyright laws of the United States. *
# * *
# * The software contained on this media is proprietary to and *
# * embodies the confidential technology developed by J. Hollosi. *
# * Possession, use, duplication or dissemination of the software *
# * and media is authorized only pursuant to a valid written *
# * license agreement from Jozsef Hollosi. *
# * *
# * Specifically, WEBLINK can be freely modified and distributed *
# * in original or modified form with the VAC simulation package, *
# * as long as this copyright notice is unchanged. *
# * *
# *******************************************************************
#
# FOR WEBLINK USERS:
#
# To run weblink as an interface type:
#
# weblink.pl [-browser=browser] [-stdout=xterminalname]
# [-srcfile=sourcefile] [-default=defaultfile] [-basedir=dirname]
#
# FOR WEBLINK PROGRAMMERS:
#
# Weblink is a standalone webserver/runtime environment. It works
# as a standard webserver, with an extension server-side language,
# called HyPerlText. It enables you to insert Perl statements directly
# into your HTML text.
#
# Syntax:
#
# eval: <#@ expr#>
# eval+print: <#= expr#>
# if: <#? expr#> ... <#/?#>
# if not: <#! expr#> ... <#/!#>
# loop: <#loop#> ... <#break expr#> ... <#/loop#>
#
# limitations: ifs and ifnots can be embedded, but loops can't
#
# variables:
# - the system keeps infinite state of all variables during the session
# EXCEPT:
# %FORM and @FORM has the latest submitted form variables
#
# - all variables and function names starting with _ are reserved
# $_agent is either netscape or mosaic
#
# - all other variables are controlled by the user
#
# - subroutine for running external programs in the background:
# &_background($command,$nohup);
# where $nohup=0 means that the job is killed when you exit weblink,
# while $nohup=1 lets the job run further.
#
# --------------------------------------------------------------------------
# Put switches which come from command line into _VARIABLES
$_BROWSER = $browser;
$_STDOUT_XTERM = $stdout;
$_BASEDIR = ($basedir)? $basedir : ".";
$_SRCFILE = ($srcfile)? $srcfile : "Weblink/interface.html";
# Execute the $default file which sets some variables, among others
# the $stdout variable which contains the name of the xterminal that
# should show the STDOUT. Empty string implies stdout into the original window.
$default = "Weblink/default" unless $default;
if(-e $default){
require $default;
}
else {
print STDERR "Warning: Customization file $default was not found!\n";
}
unless(-e $_SRCFILE){
die "Source file $srcfile was not found!\n";
}
# Put switches which come from $default into _VARIABLES unless already set
$_BROWSER = $browser unless $_BROWSER;
$_STDOUT_XTERM = $stdout unless $_STDOUT_XTERM;
$_STDOUT_XTERM = "original" unless $_STDOUT_XTERM; # To be safe.
# Start work
&_system_setup;
&_start_browser;
while (1) {
accept(_SOCK,"websocket");
$_SOCK = 1;
select(_SOCK); $| = 1;
select(STDOUT); $| = 1;
$_file = &_read_input;
#&_debug("file = $_file\n");
&_process($_file);
shutdown(_SOCK, 2);
close(_SOCK);
}
exit(0);
# -------------------------------------------------------------------
sub _system_setup
{
$|=1;
$SIG{'PIPE'} = '_sigpipe';
$SIG{'CHLD'} = 'IGNORE';
&_redirect_stdout($_STDOUT_XTERM);
}
sub _start_server
{
$_websocket = &_open_socket("websocket", 3131);
if ( fork() ) {
print "Location: http://". $ENV{"SERVER_NAME"}. ":$_websocket/\n\n";
exit;
}
### foreach (3..16) { syscall(6, $_); }
close(STDIN); close(STDOUT); close(STDERR);
alarm(300);
}
sub _start_browser
{
if($ENV{"OSTYPE"} eq "darwin"){
# On the MAC use the generic open statement
$_browser = 'open';
}else{
# On other platforms find the browser
@_bro=("netscape","mozilla"); unshift(@_bro,$_BROWSER) if $_BROWSER;
TRY:
foreach $_bro (@_bro){
foreach $_dir (split(/:/, $ENV{"PATH"}.
":/usr/bin/X11".
":/usr/local/bin".
":/usr/local/gnu/bin".
":/opt/bin".
":/opt/gnu/bin"
)) {
if ( -x $_dir."/$_bro" ) {
$_browser = $_dir."/$_bro";
last TRY;
}
}
}
}
if ( defined($_browser) ) {
print STDERR "Starting browser $_browser...\n";
$_websocket = &_open_socket("websocket", 3131);
if ( fork() ) {
return;
}
else {
if ( fork() ) {
sleep(10);
exit;
}
else {
exec("$_browser http://127.0.0.1:$_websocket");
}
}
}
else {
print STDERR "ERROR: browser $_BROWSER not found.\n";
exit(1);
}
}
sub _redirect_stdout
{
local($_XTERM)=@_;
# find the new $_XTERM program
if ( $_XTERM ne "original") {
foreach $_dir (split(/:/, $ENV{"PATH"}.
":/usr/bin/X11:/usr/X11R6/bin".
":/usr/X11R5/bin:/usr/X386/bin")) {
if ( -x $_dir."/$_XTERM" ) {
$_xterm = $_dir."/$_XTERM";
last;
}
}
if ( defined($_xterm) ) {
if ( ! $_xtermsocket ) {
$_xtermsocket = &_open_socket("xterm", 3114);
}
if ( fork() ) {
# nothing
}
else {
if ( fork() ) {
sleep(10);
exit;
}
else {
exec($_xterm, "-e","telnet", "localhost", $_xtermsocket);
}
}
accept(STDOUT, "xterm");
}
else {
print "WARNING: $_XTERM is not found in your PATH\n";
}
}
open(STDERR, ">&STDOUT");
select(STDERR); $|=1; select(STDOUT); $|=1;
$_STDOUT = 1;
&_stdout("You will see STDOUT and STDERR from WEBLINK here.\n");
}
sub _getline
{
local($_str, $_ch);
while (1) {
if ( ! sysread(_SOCK, $_ch, 1) ) { return($_str); }
$_str .= $_ch;
if ( $_ch eq "\n" ) { return($_str); }
}
}
sub _sigpipe
{
#&_debug("SIGPIPE called\n");
$_SOCK = 0;
}
sub _sigchld
{
#&_debug("SIGCHLD called\n");
}
sub _read_input
{
#&_debug("read_input\n");
($_type, $_file) = split(" ", &_getline);
#&_debug("type = $_type\n");
#&_debug("file = $_file\n");
$_CONTENT_LENGTH = 0;
while ( $_line = &_getline ) {
#&_debug("line read = $_line\n");
$_line =~ s/[\n\r]+$//;
if ( $_line =~ /^$/ ) { last; }
if ( $_line =~ /^User-Agent:\s+(.+)/i ) {
$_agent = $1;
if ( $_agent =~ /^Mozilla.(\d)/ ) {
$_agentversion = $1;
if ( $_agentversion >= 2 ) {
$_agent = "netscape2";
}
else {
$_agent = "netscape";
}
}
else {
$_agent = "mosaic";
}
}
if ( $_line =~ /^Content-Length:\s+(\d+)/i ) {
$_CONTENT_LENGTH = $1;
}
}
#&_debug("finished header\n");
$_getform = '';
$_postform = '';
if ( $_file =~ /^(.*)\?(.*)$/ ) {
$_file = $1;
$_getform = $2;
}
if ( $_CONTENT_LENGTH > 0 ) {
#&_debug("before post content read $_CONTENT_LENGTH\n");
read(_SOCK, $_postform, $_CONTENT_LENGTH);
#&_debug("read post content len=".length($_postform)."\n");
}
undef(@FORM);
undef(%FORM);
&_formvalues( $_getform . "&" . $_postform );
return($_file);
}
sub _formvalues
{
if ( $_[0] eq "&" ) { return; }
local($_str) = $_[0];
local($_var, $_val);
$_str =~ s/&/&&/g;
$_str = "&".$_str."&";
while ( $_str =~ /&([\w\d]+)=([^&]*)&/g ) {
$_var = $1;
$_val = $2;
push(@FORM, $_var);
$FORM{$_var} = $_val;
}
foreach $_var ( @FORM ) {
$FORM{$_var} = &_unhtml($FORM{$_var});
}
}
sub _unhtml
{
local($_str);
$_str = '';
$_[0] =~ s/\+/ /g;
foreach $_piece ( split(/(%..)/, $_[0]) ) {
if ( $_piece =~ /%(..)/ ) {
$_num = hex($1);
$_str .= sprintf("%c", $_num);
}
else {
$_str .= $_piece;
}
}
#TOTH: replace web newline "\x0D\x0A" by Perl newline "\n"
$_str=~s/\x0D\x0A/\n/g;
return($_str);
}
sub _print_result_ok
{
&_printout("HTTP/1.0 200 Document follows\n");
&_printout("MIME-Version: 1.0\n");
&_printout("Server: WEBLINK/1.0\n");
&_printout("Content-Type: $_[0]\n");
if ( $_[1] > 0 ) {
&_printout("Content-Length: $_[1]\n");
}
&_printout("\n");
}
sub _print_result_error
{
&_printout("HTTP/1.0 404 Not found\n");
&_printout("MIME-Version: 1.0\n");
&_printout("Server: WEBLINK/1.0\n");
&_printout("Content-Type: text/html\n\n");
&_printout("
<HTML>
<HEAD>
<TITLE>Error</TITLE>
</HEAD>
<BODY>
<H1>Error 404</H1>
Not found - file does not exist or is read protected
</BODY>
</HTML>
");
}
sub _open_socket
{
$_LSOCK = $_[0];
local($_tryport) = $_[1];
$_AF_INET = 2;
$_PF_INET = 2;
$_SOL_SOCKET = 0xffff;
$_SO_REUSEADDR = 4;
$_sockaddr = 'S n a4 x8';
$_pat = 'S n C4 x8';
($_name, $aliases, $proto) = getprotobyname('tcp');
while (1) {
$_thisport = pack($_pat, $_AF_INET, $_tryport, 127,0,0,1);
# Try to open socket streams 1, 2 or die
socket($_LSOCK, $_PF_INET, 1, $proto) ||
socket($_LSOCK, $_PF_INET, 2, $proto) ||
die "Error: could not open socket.\n";
setsockopt($_LSOCK, $_SOL_SOCKET, $_SO_REUSEADDR, "\001\0\0\0");
if ( bind($_LSOCK,$_thisport) ) { last; }
$_tryport++;
}
listen($_LSOCK,5);
return($_tryport);
}
sub _process
{
#&_debug("process = $_[0]\n");
$_SOURCE = "$_BASEDIR/$_[0]";
if ( -d $_SOURCE ) {
$_SOURCE .= "$_SRCFILE";
if ( ! -f $_SOURCE ) {
$_SOURCE =~ s/\/+[^\/]+$//;
opendir(DIR, $_SOURCE);
foreach (sort grep(!/^\./, readdir(DIR))) {
&_printout("<a href=$_");
if ( -d "$_SOURCE/$_" ) {
&_printout("/");
}
&_printout(">$_</a><br>\n");
}
closedir(DIR);
return;
}
}
if ( ! open(_SOURCE, $_SOURCE) ) {
&_print_result_error;
return;
}
if ( $_SOURCE =~ /(gif|jpeg)$/ ) {
&_print_result_ok("image/$1", 0);
while ( read(_SOURCE, $_readbuffer, 10000) ) {
&_printout($_readbuffer);
}
return;
}
elsif ( $_SOURCE =~ /html?$/ ) {
&_print_result_ok("text/html", 0);
}
else {
&_print_result_ok("text/plain", 0);
}
$_part = '';
while ( $_line = <_SOURCE> ) {
&_processline($_line);
}
close(_SOURCE);
}
sub _processline
{
local($_line) = $_[0];
foreach $_token ( split(/(<#|#>)/, $_line) ) {
if ( $_token eq "<#" ) {
$_part = $_token;
$_inpart = 1;
next;
}
elsif ( $_token eq "#>" ) {
$_part .= $_token;
$_inpart = 0;
}
else {
if ( $_inpart ) {
$_part .= $_token;
next;
}
$_part = $_token;
}
if ( $_part =~ /^<#break(\s+\S.*)?#>$/i ) {
$_args = $1;
if ( $_readloop ) { $_loopcontent .= $_part; next; }
if ( $_falsemode ) { next; }
if ( ! $_inloop ) { next; }
if ( ( length($_args) > 0 ) && ( ! &_val($_args) ) ) { next; }
$_inloop = 0;
#&_debug("breaking out\n");
return;
}
if ( $_part =~ /^<#\/loop#>$/i ) {
$_readloop = 0;
$_inloop = 1;
if ( $_falsemode ) { next; }
while ($_inloop) {
&_processline($_loopcontent);
}
next;
}
if ( $_readloop ) {
$_loopcontent .= $_part;
next;
}
if ( $_part =~ /^<#loop#>$/i ) {
$_readloop = 1;
$_loopcontent = '';
next;
}
if ( $_part =~ /^<#\/.?#>$/ ) {
if ( $_level > 0 ) { $_level--; }
$_falsemode = $_falsemode[$_level];
next;
}
#&_debug("PART ==$_part==\n");
if ( $_part =~ /^<#(.)\s+((.|\n)+)#>$/ ) {
$_action = $1;
$_args = $2;
#&_debug("action=$_action args=$_args\n");
if ( ( $_action eq "?" ) || ( $_action eq "!" ) ) {
$_level++;
if ( $_falsemode ) {
;
}
elsif ( ( $_action eq "?" ) && ( ! &_val($_args) ) ) {
$_falsemode = 1;
}
elsif ( ( $_action eq "!" ) && ( &_val($_args) ) ) {
$_falsemode = 1;
}
$_falsemode[$_level] = $_falsemode;
next;
}
if ( $_action eq "=" ) {
if ( ! $_falsemode ) { &_printout(&_val($_args)); }
next;
}
if ( $_action eq "@" ) {
if ( ! $_falsemode ) { &_val($_args); }
next;
}
}
if ( $_part =~ /^<#/ ) { next; }
if ( ! $_falsemode ) { &_printout($_part); }
}
}
sub _val
{
local($_str) = $_[0];
$_str =~ s/^\s+//;
$_str =~ s/\s+$//;
#&_debug("val = $_str\n");
return(eval $_str);
}
sub _printout
{
if ( $_SOCK ) {
syswrite(_SOCK, $_[0], length($_[0]));
#&_debug("printout: >>> $_[0] <<<\n");
}
}
sub _stdout
{
local($_numwritten);
if ( $_STDOUT ) {
$_numwritten = syswrite(STDOUT, $_[0], length($_[0]));
if ( $_numwritten != length($_[0]) ) {
close(STDOUT);
$_STDOUT = 0;
#&_debug("closed xterm\n");
}
}
}
sub _debug
{
print STDERR "debug: ", $_[0] if $debug;
}
# ---------- special user subroutine ----------
sub _background
{
local($command,$nohup)=@_;
#&_debug("background: $command\n");
if ( fork() ) {
return;
}
else {
if ( fork() ) {
sleep(10);
exit;
}
}
setpgrp(0,$$) if $nohup;
exec($command);
exit;
}
############## END ############