summaryrefslogtreecommitdiff
path: root/lib/YAWF.pm
blob: 062cae6e74f1f091a50520665f65dd5ce767be42 (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
# YAWF.pm - the core module for YAWF
#   Yet Another Website Framework
#   Yorhels Awesome Website Framework

package YAWF;

use strict;
use warnings;

# Store the object in a global variable for some functions that don't get it
# passed as an argument. This will break when:
#  - using a threaded environment (threading sucks anyway)
#  - handling multiple requests asynchronously (which this framework can't do)
#  - handling multiple sites in the same perl process. This may be useful in
#    a FastCGI or mod_perl environment, so need to find a fix for that >.>
our $OBJ;
my @handlers;


# 'redirect' this import to YAWF::XML
sub import {
  require YAWF::XML;
  YAWF::XML->import(@_);
}


# The holy init() function
sub init {
  my %o = (
    mail_from => '<noreply-yawf@blicky.net>',
    mail_sendmail => '/usr/sbin/sendmail',
    error_500_handler => \&YAWF::DefaultHandlers::error_500,
    error_404_handler => \&YAWF::DefaultHandlers::error_404,
    @_
  );
  die "No namespace argument specified!" if !$o{namespace};

  # create object
  $OBJ = bless {
    _YAWF => \%o,
    $o{object_data} && ref $o{object_data} eq 'HASH' ? %{ delete $o{object_data} } : (),
  }, 'YAWF::Object';

  # install a warning handler to write to the log file
  $SIG{__WARN__} = sub { $YAWF::OBJ->log($_) for @_; };

  # load optional modules
  require Time::HiRes if $OBJ->debug;

  # load the modules
  $OBJ->load_modules;

  # initialize DB connection
  $OBJ->dbInit if $o{db_login};

  # plain old CGI
  if($ENV{GATEWAY_INTERFACE} && $ENV{GATEWAY_INTERFACE} =~ /CGI/i) {
    $OBJ->handle_request;
  }
  # otherwise, assume a FastCGI environment
  else {
    require FCGI;
    import FCGI;
    my $r = FCGI::Request();
    while($r->Accept() >= 0) {
      $OBJ->handle_request;
      $r->Finish();
    }
  }

  # close the DB connection
  $OBJ->dbDisconnect if $o{db_login};
}


# Maps URLs to handlers
sub register {
  push @handlers, @_;
}





# The namespace which inherits all functions to be available in the global
# object. These functions are not inherited by the main YAWF namespace.
package YAWF::Object;

use YAWF::Response;
use YAWF::Request;
use YAWF::Misc;


# This function will load all site modules and import the exported functions
sub load_modules {
  my $s = shift;
  if($s->{_YAWF}{db_login}) {
    require YAWF::DB;
    import YAWF::DB;
  }
  (my $f = $s->{_YAWF}{namespace}) =~ s/::/\//g;
  for my $p (@INC) {
    for (glob $p.'/'.$f.'/{DB,Util,Handler}/*.pm') {
      (my $m = $_) =~ s{^\Q$p/}{};
      $m =~ s/\.pm$//;
      $m =~ s{/}{::}g;
      # the following is pretty much equivalent to eval "use $m";
      require $_;
      no strict 'refs';
      "$m"->import if *{"${m}::import"}{CODE};
    }
  }
}


# Handles a request (sounds pretty obvious to me...)
sub handle_request {
  my $self = shift;

  my $start = [Time::HiRes::gettimeofday()] if $self->debug;

  # put everything in an eval to catch any error, even
  # those caused by a YAWF core module
  eval { 

    # initialize request and response objects
    $self->reqInit();
    $self->resInit();
    
    # make sure our DB connection is still there and start a new transaction
    $self->dbCheck() if $self->{_YAWF}{db_login};

    # call pre request handler, if any
    $self->{_YAWF}{pre_request_handler}->($self) if $self->{_YAWF}{pre_request_handler};

    # find the handler
    my $loc = $self->reqPath;
    study $loc;
    my $han = $self->{_YAWF}{error_404_handler};
    my @args;
    for (@handlers ? 0..$#handlers/2 : ()) {
      if($loc =~ /^$handlers[$_*2]$/) {
        @args = map defined $-[$_] ? substr $loc, $-[$_], $+[$_]-$-[$_] : undef, 1..$#- if $#-;
        $han = $handlers[$_*2+1];
        last;
      }
    }
    
    # execute handler
    my $ret = $han->($self, @args);

    # give 404 page if the handler returned 404...
    if($ret && $ret eq '404') {
      $ret = $self->{_YAWF}{error_404_handler}->($self) if $han ne $self->{_YAWF}{error_404_handler};
      YAWF::DefaultHandlers::error_404($self) if $ret && $ret eq '404';
    }

    # execute post request handler, if any
    $self->{_YAWF}{post_request_handler}->($self) if $self->{_YAWF}{post_request_handler};

    # commit changes
    $self->dbCommit if $self->{_YAWF}{db_login};
  };

  # error handling
  if($@) {
    chomp( my $err = $@ );

    # act as if the changes to the DB never happened
    if($self->{_YAWF}{db_login}) {
      eval { $self->dbRollBack; };
      warn $@ if $@;
    }

    # Call the error_500_handler
    # The handler should manually call dbCommit if it makes any changes to the DB
    eval {
      $self->resInit;
      $self->{_YAWF}{error_500_handler}->($self, $err);
    };
    if($@) {
      chomp( my $m = $@ );
      warn "Error handler died as well, something is seriously wrong with your code. ($m)\n";
      YAWF::DefaultHandlers::error_500($self, $err);
    }

    # write detailed information about this error to the log
    $self->log(
      "FATAL ERROR!\n".
      "HTTP Request Headers:\n".
      join('', map sprintf("  %s: %s\n", $_, $self->reqHeader($_)), $self->reqHeader).
      "Param dump:\n".
      join('', map sprintf("  %s: %s\n", $_, $self->reqParam($_)), $self->reqParam).
      "Error:\n  $err\n"
    );
  }

  # finalize response (flush output, etc)
  eval { $self->resFinish; };
  warn $@ if $@;

  # log debug information in the form of:
  # >  12ms (SQL:  8ms,  2 qs) for http://beta.vndb.org/v10
  if($self->debug) {
    
    # SQL stats (don't count the ping and commit as queries, but do count their time)
    my($sqlt, $sqlc) = (0);
    if($self->{_YAWF}{db_login}) {
      $sqlc = grep $_->[0] ne 'ping/rollback' && $_->[0] ne 'commit', @{$self->{_YAWF}{DB}{queries}};
      $sqlt += $_->[1]*1000
        for (@{$self->{_YAWF}{DB}{queries}});
    }

    my $time = Time::HiRes::tv_interval($start)*1000;
    $self->log(sprintf('>%4dms (SQL:%4dms,%3d qs) for %s',
      $time, $sqlt, $sqlc, $self->reqURI), 1);
  }
}


# convenience function
sub debug {
  return shift->{_YAWF}{debug};
}


# writes a message to the log file. date, time and URL are automatically added
# An optional 3rd argument can be passed to exclude the date, time and url information
sub log {
  my($self, $msg, $excl) = @_;
  chomp $msg;
  $msg =~ s/\n/\n  | /g;
  if($self->{_YAWF}{logfile} && open my $F, '>>:utf8', $self->{_YAWF}{logfile}) {
    flock $F, 2;
    seek $F, 0, 2;
    printf $F "[%s] %s: %s\n", scalar localtime(), $self->reqURI||'[init]', $msg if !$excl;
    print $F "$msg\n" if $excl;
    flock $F, 4;
    close $F;
  }
}



# put the default handlers in a separate namespace
# (in case we do decide to use the HTML generator here)
package YAWF::DefaultHandlers;


# these are defaults, you really want to replace these boring pages
sub error_404 {
  my $s = shift;
  $s->resInit;
  $s->resStatus(404);
  very_simple_page($s, '404 - Page Not Found', 'The page you were looking for does not exist...');
}


# a *very* helpful error message :-)
sub error_500 {
  my $s = shift;
  $s->resInit;
  $s->resStatus(500);
  very_simple_page($s, '500 - Internal Server Error', 'Ooooopsie~, something went wrong!');
}


# and an equally beautiful page
sub very_simple_page {
  my($s, $title, $msg) = @_;
  my $fd = $s->resFd;
  print $fd <<__;
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <title>$title</title>
</head>
<body>
 <h1>$title</h1>
 <p>$msg</p>
</body>
</html>
__
}



1;