Revision: 26 Author: matt Date: 2006-08-01 20:52:12 +0000 (Tue, 01 Aug 2006)
Log Message: ----------- Handle errors better Fixup a couple of the test plugin/files Cleanup some warnings Modified Paths: -------------- trunk/demo/xsp/test.xsp trunk/lib/AxKit2/Client.pm trunk/lib/AxKit2/Config/Location.pm trunk/lib/AxKit2/Config/Server.pm trunk/lib/AxKit2/Connection.pm trunk/lib/AxKit2/Constants.pm trunk/lib/AxKit2/HTTPHeaders.pm trunk/plugins/serve_file Modified: trunk/demo/xsp/test.xsp =================================================================== --- trunk/demo/xsp/test.xsp 2006-08-01 04:13:07 UTC (rev 25) +++ trunk/demo/xsp/test.xsp 2006-08-01 20:52:12 UTC (rev 26) @@ -3,5 +3,10 @@ <output> Hello World. The time of request is: <xsp:expr>scalar localtime</xsp:expr> + <structure> + <xsp:expr> + { abc => 42, xyz => { goofy => "yes", ugly => "no" } }; + </xsp:expr> + </structure> </output> </xsp:page> Modified: trunk/lib/AxKit2/Client.pm =================================================================== --- trunk/lib/AxKit2/Client.pm 2006-08-01 04:13:07 UTC (rev 25) +++ trunk/lib/AxKit2/Client.pm 2006-08-01 20:52:12 UTC (rev 26) @@ -67,8 +67,9 @@ for my $plugin ($conf->plugins) { my $plug = plugin_instance($plugin) || next; for my $h ($plug->hooks($hook)) { + $self->log(LOGDEBUG, "$plugin running hook $hook") unless $hook eq 'logging'; eval { @r = $plug->$h($self, $conf, @_) }; - $@ and $self->log(LOGERROR, "FATAL PLUGIN ERROR: $@"); + $@ and $self->log(LOGERROR, "FATAL PLUGIN ERROR: $@"), return SERVER_ERROR; next unless @r; last MAINLOOP unless $r[0] == DECLINED; } @@ -97,36 +98,72 @@ sub hook_uri_to_file { my $self = shift; my ($ret, $out) = $self->run_hooks('uri_translation', @_); - if ($ret == DECLINED) { - return; + if ($ret == DECLINED || $ret == OK) { + return 1; } else { # TODO: output error stuff? + return; } } sub hook_access_control { + 1; } sub hook_authentication { + 1; } sub hook_authorization { + 1; } sub hook_fixup { + 1; } +sub hook_error { + my $self = shift; + $self->headers_out->code(SERVER_ERROR, "Internal Server Error"); + my ($ret) = $self->run_hooks('error'); + if ($ret != OK) { + $self->headers_out->header('Content-Type' => 'text/html; charset=UTF-8'); + $self->send_http_headers; + $self->write(<<EOT); +<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> +<HTML><HEAD> +<TITLE>500 Internal Server Error</TITLE> +</HEAD><BODY> +<H1>Internal Server Error</H1> +The server encountered an internal error or +misconfiguration and was unable to complete +your request.<P> +More information about this error may be available +in the server error log.<P> +<HR> +</BODY></HTML> +EOT + } + else { + # we assume some hook handled the error + } +} + sub hook_xmlresponse { my $self = shift; my ($ret, $out) = $self->run_hooks('xmlresponse', @_); if ($ret == DECLINED) { - return; + return 0; } elsif ($ret == OK) { $out->output($self) if $out; - return 1; + return 1; # stop } + elsif ($ret == SERVER_ERROR) { + $self->hook_error(); + return 1; # stop + } else { # TODO: handle errors } @@ -136,8 +173,15 @@ my $self = shift; my ($ret, $out) = $self->run_hooks('response', @_); if ($ret == DECLINED) { - return; + return 1; } + elsif ($ret == OK) { + return 1; + } + elsif ($ret == SERVER_ERROR) { + $self->hook_error(); + return 1; # stop + } else { # TODO: output error stuff? } Modified: trunk/lib/AxKit2/Config/Location.pm =================================================================== --- trunk/lib/AxKit2/Config/Location.pm 2006-08-01 04:13:07 UTC (rev 25) +++ trunk/lib/AxKit2/Config/Location.pm 2006-08-01 20:52:12 UTC (rev 26) @@ -33,7 +33,6 @@ sub matches { my $self = shift; my $tomatch = shift; - warn("Location: does $self->{__path} match $tomatch ?\n"); return index($tomatch, $self->path) + 1; } Modified: trunk/lib/AxKit2/Config/Server.pm =================================================================== --- trunk/lib/AxKit2/Config/Server.pm 2006-08-01 04:13:07 UTC (rev 25) +++ trunk/lib/AxKit2/Config/Server.pm 2006-08-01 20:52:12 UTC (rev 26) @@ -88,12 +88,10 @@ my $self = shift; my $path = shift; - warn("get_config $path called\n"); # note using first() with reverse() implies last(), # but List::Util has no last() function my $ret = first { $_->matches($path) } reverse @{$self->{Locations}}; - warn("get_config returning $ret or $self\n"); return $ret || $self; } Modified: trunk/lib/AxKit2/Connection.pm =================================================================== --- trunk/lib/AxKit2/Connection.pm 2006-08-01 04:13:07 UTC (rev 25) +++ trunk/lib/AxKit2/Connection.pm 2006-08-01 20:52:12 UTC (rev 26) @@ -155,19 +155,22 @@ $self->{headers_out}->header(Date => HTTP::Date::time2str()); $self->{headers_out}->header(Server => "AxKit-2/v$AxKit2::VERSION"); - $self->hook_uri_to_file($hd, $hd->request_uri); + $self->hook_uri_to_file($hd, $hd->request_uri) + && + $self->hook_access_control($hd) + && + $self->hook_authentication($hd) + && + $self->hook_authorization($hd) + && + $self->hook_fixup($hd) + && + ( + $self->hook_xmlresponse(AxKit2::Processor->new($hd->filename)) + || + $self->hook_response($hd) + ); - $self->hook_access_control($hd); - - $self->hook_authentication($hd); - - $self->hook_authorization($hd); - - $self->hook_fixup($hd); - - $self->hook_xmlresponse(AxKit2::Processor->new($hd->filename)) - || $self->hook_response($hd); - $self->http_response_sent(); } Modified: trunk/lib/AxKit2/Constants.pm =================================================================== --- trunk/lib/AxKit2/Constants.pm 2006-08-01 04:13:07 UTC (rev 25) +++ trunk/lib/AxKit2/Constants.pm 2006-08-01 20:52:12 UTC (rev 26) @@ -20,12 +20,8 @@ # return codes my %return_codes = ( - OK => 900, - DENY => 901, # 550 - DENYSOFT => 902, # 450 - DENYHARD => 903, # 550 + disconnect (deprecated in 0.29) - DENY_DISCONNECT => 903, # 550 + disconnect - DENYSOFT_DISCONNECT => 904, # 450 + disconnect + OK => 200, + SERVER_ERROR => 500, DECLINED => 909, DONE => 910, ); Modified: trunk/lib/AxKit2/HTTPHeaders.pm =================================================================== --- trunk/lib/AxKit2/HTTPHeaders.pm 2006-08-01 04:13:07 UTC (rev 25) +++ trunk/lib/AxKit2/HTTPHeaders.pm 2006-08-01 20:52:12 UTC (rev 26) @@ -160,6 +160,7 @@ $self->{responseLine} = "HTTP/1.0 $code " . $self->http_code_english($code); $self->{code} = $code; $self->{type} = "httpres"; + $self->{vernum} = 1000; return $self; } @@ -222,9 +223,9 @@ } sub version_number { - my AxKit2::HTTPHeaders $self = $_[0]; - return $self->{vernum} unless $_[1]; - return $self->{vernum} = $_[1]; + my AxKit2::HTTPHeaders $self = shift; + @_ and $self->{vernum} = shift; + $self->{vernum}; } sub header { Modified: trunk/plugins/serve_file =================================================================== --- trunk/plugins/serve_file 2006-08-01 04:13:07 UTC (rev 25) +++ trunk/plugins/serve_file 2006-08-01 20:52:12 UTC (rev 26) @@ -18,7 +18,6 @@ "); local $/; $self->client->write(<$fh>); - $self->client->close; } else { $self->client->write("HTTP/1.1 200 OK @@ -32,7 +31,6 @@ <H1>Testing: $file</H1> </BODY> </HTML>"); - $self->client->close(); } }