summaryrefslogtreecommitdiff
path: root/apps/JAWS/clients/WebSTONE/bin/webstone-gui.pl
blob: cb0c7224c0963ed8995934b2558128f5759f8afa (plain)
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
#!/pkg/gnu/bin//perl5
#
#$Id$

require 'conf/paths.pl';

#$debug = 1;
$HELPME="http://reality.sgi.com/employees/mblakele_engr/WebStone/";
$| =  1; # set pipes to hot

&html();

sub html {
        local($helper, $wd);

        &start_html_server();
        # These strings are used in, among others, PERL-to-HTML scripts.
        #
        $wd = `pwd`;
        chop $wd;

	print "$html_port\n" if $debug;

        $HTML_STARTPAGE = "http://localhost:$html_port$wd/doc/WebStone.html";

        #
        # Fork off the HTML client, and fork off a server process that
        # handles requests from that client. The parent process waits
        # until the client exits and terminates the server.
        #
        print "Starting $MOSAIC...\n" if $debug;

        if (($client = fork()) == 0) {
                foreach (keys %ENV) {
                        delete $ENV{$_} if (/proxy/i && !/no_proxy/i);
                }
                exec($MOSAIC, "$HTML_STARTPAGE")
                        || die "cannot exec $MOSAIC: $!";
        }

        if (($server = fork()) == 0) {
                if (($helper = fork()) == 0) {
                        alarm 3600;
                        &patience();
                }
                kill 'TERM',$helper;
                $SIG{'PIPE'} = 'IGNORE';
                for (;;) {
                        accept(CLIENT, SOCK) || die "accept: $!";
                        select((select(CLIENT), $| = 1)[0]);
                        &process_html_request();
                        close(CLIENT);
                }
        }

        #
        # Wait until the client terminates, then terminate the server.
        #
        close(SOCK);
        waitpid($client, 0);
        kill('TERM', $server);
        exit;
}

#
# Set up a listener on an arbitrary port. There is no good reason to
# listen on a well-known port number.
#
sub start_html_server {
        local($sockaddr, $proto, $junk);

        $AF_INET = 2;
        $SOCK_STREAM = 2;
        $PORT = 0; #1024;

        $sockaddr = 'S n a4 x8';
        $this = pack($sockaddr, $AF_INET, $PORT, "\0\0\0\0");
        ($junk, $junk, $proto) = getprotobyname('tcp');
        socket(SOCK, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
        setsockopt(SOCK, 0xffff, 0x0004, 1) || die "setsockopt: $!";
        bind(SOCK, $this) || die "bind: $!";
        listen(SOCK, 1) || die "listen: $!";
        ($junk, $html_port) = unpack($sockaddr, getsockname(SOCK));
}


#
# Process one client request.  We expect the client to send stuff that
# begins with:
#
#       command /password/perl_script junk
#
# Where perl_script is the name of a perl file that is executed via
# do "perl_script";
#
# In case of a POST command the values in the client's attribute-value
# list are assigned to the corresponding global PERL variables.
#
sub process_html_request {
        local($request, $command, $script, $magic, $url, $peer);
        local(%args);
 
        #
        # Parse the command and URL. Update the default file prefix.
        #
        $request = <CLIENT>;
        print $request if $debug;
        ($command, $url) = split(/\s+/, $request);
        if ($command eq "" || $command eq "QUIT") {
                return;
        }
 
        #($junk, $script) = split(/\//, $url, 2);
        #($script, $html_script_args) = split(',', $script, 2);
        #($HTML_CWD = "file:$script") =~ s/\/[^\/]*$//;
        $script = $url;
 
        while (<CLIENT>) {
                last if (/^\s+$/);
        }

        if ($command eq "GET") {
		if (-d $script) {
			get_dir($script);
		}
		elsif ($script =~ /\.pl\b/) {
			perl_html_script($script);
		}
		else {
		get_file($script);
		}
        } elsif ($command eq "POST") {
 
        	print $request if $debug;
		flush;
                #
                # Process the attribute-value list.
                #
                if ($_ = <CLIENT>) {
        		print "Hi $_" if $debug;
			flush;
                        s/\s+$//;
                        s/^/\n/;
                        s/&/\n/g;
                        $html_post_attributes = '';
                        $* = 1;
                        for (split(/(%[0-9][0-9A-Z])/, $_)) {
                                $html_post_attributes .= (/%([0-9][0-9A-Z])/) ?
                                        pack('c',hex($1)) : $_;
                        }
                        %args = ('_junk_', split(/\n([^=]+)=/, $html_post_attributes));                  
                        delete $args{'_junk_'};
                        for (keys %args) {
                                print "\$$_ = $args{$_}\n" if $debug;
                                ${$_} = $args{$_};
                        }
                        perl_html_script($script);
                } else { 
                        &bad_html_form($script);
                }
        } else {
                &bad_html_command($request);
        }
}       


#
# Unexpected HTML command.
#
sub bad_html_command {
        local($request) = @_;
 
        print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>Unknown command</TITLE>
<LINK REV="made" HREF=$HELPME>
</HEAD>
<BODY>
<H1>Unknown command</H1>
The command <TT>$request<TT> was not recognized.
</BODY>
</HTML>
EOF
;
}
 
#
# Execute PERL script
#
sub perl_html_script {
        local($script) = @_;
 
        if (! -e $script) {
                print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>File not found</TITLE>
<LINK REV="made" HREF=$HELPME>
</HEAD>
<BODY>
<H1>File not found</H1>
The file <TT>$script</TT> does not exist or is not accessible.
</BODY>
</HTML>
EOF
;               return;
        }
        do $script;
        if ($@ && ($@ ne "\n")) {
                print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>Command failed</TITLE>
<LINK REV="made" HREF=$HELPME>
</HEAD>
<BODY>
<H1>Command failed</H1>
$@
</BODY>
</HTML>
EOF
;
        }
}

#
# Missing attribute list
#
sub bad_html_form {
        local($script) = @_;
 
        print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>No attribute list</TITLE>
<LINK REV="made" HREF=$HELPME>
</HEAD>
<BODY>
<H1>No attribute list</H1>
 
No attribute list was found.
</BODY>
</HTML>
EOF
;
}
 
#
# Give them something to read while the server is initializing.
#
sub patience {
        for (;;) {
                accept(CLIENT, SOCK) || die "accept: $!";
                <CLIENT>;
                print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>Initializing...</TITLE>
<LINK REV="made" HREF=$HELPME>
</HEAD>
<BODY>
<H1>Initializing...</H1>
WebStone is initializing...
</BODY>
</HTML>
EOF
;
                close(CLIENT);
        }
}

sub get_file {
	local($file) = @_;

	print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>$file</TITLE>
</HEAD>
<H1>$file</H1>
<BODY><PRE>
EOF
   	unless ($file =~ /(html|htm|gif|jpeg|jpg)\b/);

	open(FILE, $file);
	while (<FILE>) {
		print CLIENT $_;
	}	
	close(FILE);

	print CLIENT <<EOF
</PRE>
</HTML>
EOF
	unless ($file =~ /(html|htm|gif|jpeg|jpg)\b/);
}

sub get_dir {
	local($dir) = @_;
	opendir(DIRECTORY, $dir);
	@listing = readdir(DIRECTORY);
	closedir(DIRECTORY);
	print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>$dir</TITLE>
</HEAD>
<H1>$dir</H1>
<BODY>
EOF
   ;

	while (<@listing>) {
		print CLIENT "<P><A HREF=$dir/$_>$_</A>";
	}
	print CLIENT "</HTML>";
}