[a8e391]: whip / Whip.pm Maximize Restore History

Download this file

Whip.pm    438 lines (346 with data), 10.4 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
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
# Whip is where the basic stuff goes.
package Whip;
use warnings;
use strict;
use XML::Parser;
use Whip::Tag;
use CGI qw(:standard);
use Whip::State;
my %whip_tag_syntax;
sub WH_DOCROOT () { 0 }
sub WH_PAGE_ID () { 1 }
sub WH_COOKIE () { 2 }
sub WH_QUERY () { 3 }
sub WH_USER () { 4 }
# Create the Whip singleton. It's not truly a singleton, though,
# because we may have several of them handling requests in a given
# program.
sub new {
my $class = shift;
my %param = @_;
my $query = delete $param{query};
my $cookie = $query->cookie("whip_uid");
# If we have a cookie from the browser, then try to load the state
# for it. Failure to load a state isn't bad; we'll just create a
# new one.
my $user;
if (defined $cookie and length $cookie) {
eval {
$user = Whip::State->thaw($cookie);
}
}
# The user state was loaded from the cookie, so we don't need to
# create a new cookie. Otherwise the user state was stale/removed,
# and we build a new one and a cookie to go with it.
if (defined $user) {
undef $cookie;
}
else {
$user = Whip::State->new({});
$cookie = $query->cookie( -name => "whip_uid", -value => $user->freeze() );
}
my $self = bless
[ delete $param{docroot}, # WH_DOCROOT
undef, # WH_PAGE_ID
$cookie, # WH_COOKIE
$query, # WH_QUERY
$user, # WH_USER
], $class;
return $self;
}
### Emit a document.
sub emit_document {
my $self = shift;
my %param = @_;
# If we have a cookie, be sure to send it to the browser so the user
# may be identified later.
my @cookie;
if (defined $self->[WH_COOKIE]) {
push @cookie, -cookie => $self->[WH_COOKIE];
}
# This should never happen, but it just might.
unless (defined $self->[WH_USER]) {
die "No user";
}
# Flush any changes to the user state.
$self->[WH_USER]->freeze();
print( header( -status => delete $param{status},
@cookie,
),
start_html(delete $param{title}),
delete $param{body},
end_html(),
);
exit 0;
}
### Throw an error.
sub error {
my ($self, $status, $title, $body) = @_;
die [ $status, $title, $body ];
}
### Set syntax for a package.
sub set_syntax {
my ($self, $pkg, $syntax) = @_;
$whip_tag_syntax{$pkg} = $syntax;
}
### Manage a page cache.
sub CACHE_DATA () { 0 }
sub CACHE_AGE () { 1 }
my %page_cache;
# Translate a page ID into an absolute path, via the docroot.
sub absolute_path {
my ($self, $page_id) = @_;
my $path = $self->[WH_DOCROOT] . "/$page_id";
$path =~ s{/\.+/}{/}g;
$path =~ s{^\.+/}{/};
return $path;
}
# Translate a page ID into a perl package name.
sub page_to_package {
my ($self, $page_id) = @_;
unless ($page_id =~ /^(.+?)\.([^\.]+)$/) {
$self->error( 404, "Page Not Found",
"<tt>" . escapeHTML($page_id) .
"</tt> is not a valid whip page ID."
);
}
my $package = $1;
$package =~ tr[a-zA-Z_0-9:][_]cs;
if ($2 eq "tag") {
return "Whip::Tag::$package";
}
if ($2 eq "do") {
return "Whip::Action::$package";
}
if ($2 eq "form") {
return "Whip::Form::$package";
}
$self->error( 500, "Error Executing Page",
"<tt>" . escapeHTML($page_id) .
"</tt> is not executable."
);
}
# Determine if the data for a page ID is stale.
sub cache_outdated {
my ($self, $path) = @_;
my $page_age = -M $path;
if ( exists($page_cache{$path}) and
$page_cache{$path}->[CACHE_AGE] <= $page_age
) {
return 0;
}
return 1;
}
# Load a page, or return a cached page if it's still fresh. We use
# $held_self for import() routines, which cannot accept $self as a
# parameter.
my $held_self;
sub load_page {
my ($self, $page_id) = @_;
# Whip::Tag's import can't refer to this object as itself, so it
# calls load_page by package instead. This if/else saves $self when
# called by object, and restores $self when called by package.
if ($self eq __PACKAGE__) {
$self = $held_self;
}
else {
$held_self = $self;
}
my $path = $self->absolute_path($page_id);
unless (-e $path) {
$self->error( 404, "Page Not Found",
"The file for <tt>" . escapeHTML($page_id) .
"</tt> does not exist."
);
}
unless ($self->cache_outdated($path)) {
return $page_cache{$path}->[CACHE_DATA];
}
unless (open PAGE, $path) {
$self->error( 500, "Error Loading Page",
"The file for <tt>" . escapeHTML($page_id) .
"</tt> could not be opened: $!"
);
}
# If page is executable, then load it.
my $page_data;
if ($page_id =~ /\.(do|tag|form)$/) {
require $path;
$page_data = $self->page_to_package($page_id);
# Tags have special on-load needs.
if ($page_id =~ /\.tag$/) {
$page_data->set_syntax();
}
}
else {
local $/;
$page_data = <PAGE>;
close PAGE;
}
$page_cache{$path} =
[ $page_data, # CACHE_DATA
-M $path, # CACHE_AGE
];
return $page_data;
}
### XML parser and tag stack.
my @tag_stack;
# Parser callback for a beginning tag.
sub start_element {
my ($self, $param, $parser, $tag, $args) = @_;
my $page_package = $self->load_page("$tag.tag");
# Page tag is special.
if ($tag eq "page") {
if (@tag_stack) {
$self->error( 500, "Invalid Whip Content",
"<tt>" . escapeHTML("$tag.tag") .
"</tt> <tt>&lt;page&gt;</tt> must be the outermost tag."
);
}
}
# else {
# unless (@tag_stack) {
# $self->error( 500, "Invalid Whip Content",
# "<tt>" . escapeHTML("$tag.tag") .
# "</tt> <tt>" . escapeHTML("<$tag>") .
# "</tt> must be within a &lt;page&gt; tag."
# );
# }
# }
push @tag_stack, $page_package->new($self, $tag, $args, $param);
}
# Parser callback for an ending tag.
sub end_element {
my ($self, $param, $parser, $tag) = @_;
unless (@tag_stack) {
$self->error( 500, "Invalid Whip Content",
"<tt>" . escapeHTML("</$tag>") .
"</tt> has no corresponding <tt>" .
escapeHTML("<$tag>") . "</tt>."
);
}
my $open_tag = $tag_stack[-1]->name();
unless ($tag eq $open_tag) {
$self->error( 500, "Invalid Whip Content",
"<tt>" . escapeHTML("</$tag>") .
"</tt> attempts to close <tt>" .
escapeHTML("<$open_tag>") . "</tt>."
);
}
# Pop the tag off the stack, validate its data, build a hash of
# useful parameters, and close the tag with that.
my $done_tag = pop @tag_stack;
# Validate parameters, and build a data hash.
my @data;
if (exists $whip_tag_syntax{ref($done_tag)}) {
my @syntax = @{$whip_tag_syntax{ref($done_tag)}};
while (my ($sub_tag, $flags) = splice(@syntax, 0, 2)) {
my @default;
if (ref($flags) eq "ARRAY") {
($flags, @default) = @$flags;
}
my $legal = 1;
if ($flags & LIST) {
my @value = $done_tag->fetch($sub_tag, @default);
push @data, [@value];
if ($flags & REQ and !@value) {
$legal = 0;
last;
}
}
elsif ($flags & SCALAR) {
my $value = $done_tag->fetch($sub_tag, @default);
unless (defined $value) {
if ($flags & REQ) {
$legal = 0;
last;
}
$value = "";
}
push @data, $value;
}
else {
$self->error( 500, "Invalid Whip Syntax",
"<tt>" . escapeHTML("<$tag>") .
"</tt> has unknown flags for subtag <tt>" .
escapeHTML("<$sub_tag>") . "</tt>."
);
}
unless ($legal) {
$self->error( 500, "Invalid Whip Content",
"<tt>" . escapeHTML("<$tag>") .
"</tt> requires <tt>" .
escapeHTML("<$sub_tag>") . "</tt>."
);
}
}
}
# Pass the data to the close tag.
$done_tag->close(@data);
}
### Give this tag's contents to some container. Propagates up the
### container thingy.
sub emit {
my ($self, $type, $value) = @_;
my $take_method = "take_$type";
# Give its content to its container.
my $i = @tag_stack;
while ($i--) {
if ($tag_stack[$i]->can($take_method)) {
$tag_stack[$i]->$take_method($value);
return;
}
}
}
### Parser callback for some random text.
sub text {
my ($self, $param, $parser, $text) = @_;
# Ignore all-whitespace text.
return unless $text =~ /\S/;
$self->start_element($param, $parser, "text", { text => $text });
$self->end_element($param, $parser, "text");
}
# Render a page.
sub render_page {
my ($self, $page_id, $param) = @_;
unless ($page_id =~ /\.page$/) {
$self->error( 500, "Error Rendering Page",
"<tt>" . escapeHTML($page_id) .
"</tt> is not renderable."
);
}
my $parser = XML::Parser->new
( Handlers =>
{ Start => sub { $self->start_element($param, @_) },
End => sub { $self->end_element($param, @_) },
Char => sub { $self->text($param, @_) },
}
);
my $page_data = $self->load_page($page_id);
$self->[WH_PAGE_ID] = $page_id;
$parser->parse($page_data);
undef $self->[WH_PAGE_ID];
}
### Accessor: Get the current page ID.
sub get_page_id {
my $self = shift;
return $self->[WH_PAGE_ID];
}
### Execute a page to parse some $data. Expects $page to be a
### document name, $args is a hashref of attributes for the tag that
### triggered the execution, and $data is a reference to the remainder
### of the page being rendered. Returns ($html, $state), which are
### HTML and state variables that will be assimilated by the parent
### thing.
sub execute_page {
my ($self, $page_id) = @_;
my $package_name = $self->load_page($page_id);
$package_name->run($self);
}
### Submit something.
sub submit_page {
my ($self, $page_id, $param) = @_;
my $package_name = $self->load_page($page_id);
$package_name->submit_form($self, $page_id, $param);
}
1;