Repository navigation
Expand file tree
/
Copy pathajax.cgi
More file actions
168 lines (150 loc) · 3.5 KB
/
Copy pathajax.cgi
File metadata and controls
168 lines (150 loc) · 3.5 KB
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
use strict;
use warnings;
use CGI;
use WebComponent::Ajax;
$CGI::LIST_CONTEXT_WARN = 0;
$CGI::Application::LIST_CONTEXT_WARN = 0;
use IO::Handle;
#open(TICKLOG, ">/dev/pts/9") or open(TICKLOG, ">&STDERR");
#TICKLOG->autoflush(1);
use Time::HiRes 'gettimeofday';
my $start = gettimeofday;
my $time_last;
sub tick {
my($w) = @_;
my $now = gettimeofday;
my $t = $now - $start;
my $tms = int(($now - $time_last) * 1000);
$time_last = $now;
my ($package, $filename, $line) = caller;
# printf TICKLOG "$$ %-40s %5d %5d %.3f\n", $filename, $line, $tms, $t;
# TICKLOG->flush();
}
my $have_fcgi;
eval {
require CGI::Fast;
$have_fcgi = 1;
};
#
# If no CGI vars, assume we are invoked as a fastcgi service.
#
my $n_requests = 0;
if ($have_fcgi && $ENV{REQUEST_METHOD} eq '')
{
#
# Precompile modules. Find where we found one, and use that path
# to walk for the rest.
#
my $mod_path = $INC{"WebComponent/Ajax.pm"};
if ($mod_path && $mod_path =~ s,WebApplication/WebComponent/Ajax\.pm$,,)
{
local $SIG{__WARN__} = sub {};
for my $what (qw(SeedViewer RAST WebApplication))
{
for my $which (qw(WebPage WebComponent))
{
opendir(D, "$mod_path/$what/$which") or next;
my @x = grep { /^[^.]/ } readdir(D);
for my $mod (@x)
{
$mod =~ s/\.pm$//;
my $fullmod = join("::", $what, $which, $mod);
eval " require $fullmod; ";
}
closedir(D);
}
}
}
my $max_requests = 100;
while (($max_requests == 0 || $n_requests++ < $max_requests) &&
(my $cgi = new CGI::Fast()))
{
eval {
&main($cgi);
};
if ($@)
{
if ($@ =~ /^cgi_exit/)
{
# this is ok.
}
elsif (ref($@) ne 'ARRAY')
{
warn "code died, cgi=$cgi returning error '$@'\n";
print $cgi->header(-status => '500 error in body of cgi processing', -charset => 'UTF-8');
print $@;
}
}
endloop:
}
}
else
{
my $cgi = new CGI();
eval { &main($cgi); };
if ($@)
{
my $error = $@;
print CGI::header();
print CGI::start_html();
# print out the error
print '<pre>'.$error.'</pre>';
print CGI::end_html();
}
}
sub main
{
my($cgi) = @_;
my ($ajaxError, @cookieList);
my $app = $cgi->param('app');
my $page = $cgi->param('page');
my $sub_to_call = $cgi->param('sub');
my $cookies = $cgi->param('cookies');
# require the web page package
my $package = $app."::WebPage::".$page;
my $package_ = 'WebPage::'.$page;
my $realPage = $package;
{
no strict;
eval "require $package";
if ($@) {
eval "require $package_";
$realPage = $package_;
if ($@) {
$ajaxError = "Sorry, but the page '$page' was not found.";
}
}
}
$cgi->delete('app');
$cgi->delete('sub');
$cgi->delete('cookies');
if ($cookies && ! defined $ajaxError) {
my $method = $realPage . "::" . $cookies;
@cookieList = eval("$method(\$cgi)");
if ($@) {
$ajaxError = $@;
}
}
print $cgi->header(-cookie => \@cookieList, -charset => 'UTF-8');
my $result;
if (! defined $ajaxError) {
eval {
$result = &WebComponent::Ajax::render($app, $realPage, $sub_to_call, $cgi);
};
if ($@) {
$ajaxError = $@;
}
}
if (defined $ajaxError) {
$result = CGI::div({style => join("\n", "margin: 20px 10px 20px 10px;",
"padding-left: 10px;",
"padding-right: 10px;",
"width: 80%;",
"color: #fff;",
"background: #ff5555;",
"border: 2px solid #ee2222;") },
"Failure in component: $ajaxError");
}
print $result;
}
1;