| 1 | package OpenInteract; | ||
| 2 | |||
| 3 | # $Id: OpenInteract.pm,v 1.55 2004/09/28 16:10:37 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Apache::Constants qw( :common :remotehost :response ); | ||
| 7 | use Apache::Request; | ||
| 8 | use Data::Dumper qw( Dumper ); | ||
| 9 | |||
| 10 | $OpenInteract::VERSION = '1.62'; | ||
| 11 | |||
| 12 | # Generic separator used in display | ||
| 13 | |||
| 14 | my $SEP = '=' x 30; | ||
| 15 | |||
| 16 | # Keep track of what's been require'd | ||
| 17 | |||
| 18 | my %REQ = (); | ||
| 19 | |||
| 20 | |||
| 21 | sub handler ($$) { | ||
| 22 | my ( $class, $apache ) = @_; | ||
| 23 | |||
| 24 | # Create the big cheese object (aka, "Big R") and populate with some | ||
| 25 | # basic info | ||
| 26 | |||
| 27 | my $R = eval { $class->setup_request( $apache ) }; | ||
| 28 | if ( $@ ) { | ||
| 29 | $class->send_html( $apache, $@ ); | ||
| 30 | return OK; | ||
| 31 | } | ||
| 32 | $R->DEBUG && $R->scrib( 1, "\n\n$SEP\nRequest started:", scalar localtime( $R->{time} ), "\n", | ||
| 33 | "path: (", $apache->parsed_uri->path, ") PID: ($$)" ); | ||
| 34 | |||
| 35 | # Go through all of our important steps -- we setup the basic | ||
| 36 | # environment, generate the content and run additional routines after | ||
| 37 | # the content has completed for tracking (session, cookies, etc.) | ||
| 38 | # | ||
| 39 | # If there is a problem with a routine it will die with the | ||
| 40 | # appropriate Apache constant (usually OK) and if we find it we force | ||
| 41 | # it to numeric context and return it. | ||
| 42 | |||
| 43 | my ( $page ); | ||
| 44 | eval { | ||
| 45 | $class->setup_server_interface( $R, $apache ); | ||
| 46 | $class->setup_cache( $R ); | ||
| 47 | $class->parse_uri( $R ); | ||
| 48 | $class->find_action_handler( $R ); | ||
| 49 | $class->check_database( $R ); | ||
| 50 | $class->setup_cookies_and_session( $R ); | ||
| 51 | $class->setup_authentication( $R ); | ||
| 52 | $class->setup_theme( $R ); | ||
| 53 | $page = $class->run_content_handler( $R ); | ||
| 54 | $class->finish_cookies_and_session( $R ); | ||
| 55 | }; | ||
| 56 | if ( $@ ) { | ||
| 57 | warn " --EXITED WITH ERROR from main handler eval block\nError: $@\n"; | ||
| 58 | return $class->bail( $@ ); | ||
| 59 | } | ||
| 60 | |||
| 61 | if ( $R->{page}{send_file} ) { | ||
| 62 | $class->send_static_file( $R ); | ||
| 63 | } | ||
| 64 | elsif ( my $redirect_url = $R->{page}{http_redirect} ) { | ||
| 65 | my $apr = $R->apache; | ||
| 66 | $apr->no_cache(1); | ||
| 67 | $apr->headers_out->set( Location => $redirect_url ); | ||
| 68 | $apr->status( REDIRECT ); | ||
| 69 | $apr->send_http_header; | ||
| 70 | } | ||
| 71 | else { | ||
| 72 | $class->send_html( $apache, $page, $R ); | ||
| 73 | } | ||
| 74 | |||
| 75 | $class->cleanup( $R ); | ||
| 76 | return OK; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub bail { | ||
| 80 | my ( $class, $msg ) = @_; | ||
| 81 | $msg = $msg + 0; # force scalar to numeric | ||
| 82 | return $msg; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | # Setup the OpenInteract::Request object | ||
| 87 | |||
| 88 | sub setup_request { | ||
| 89 | my ( $class, $apache ) = @_; | ||
| 90 | |||
| 91 | # Read the stash class from our httpd.conf and grab the config | ||
| 92 | # object | ||
| 93 | |||
| 94 | my $STASH_CLASS = $apache->dir_config( 'OIStashClass' ); | ||
| 95 | unless ( $REQ{ $STASH_CLASS } ) { | ||
| 96 | eval "require $STASH_CLASS"; | ||
| 97 | if ( $@ ) { | ||
| 98 | $apache->child_terminate; | ||
| 99 | die "Cannot require stash class ($STASH_CLASS) -- ", | ||
| 100 | "fatal event, will terminate Apache child\n"; | ||
| 101 | } | ||
| 102 | $REQ{ $STASH_CLASS }++; | ||
| 103 | } | ||
| 104 | my $C = $STASH_CLASS->get_stash( 'config' ); | ||
| 105 | unless ( ref $C and scalar keys %{ $C } ) { | ||
| 106 | $apache->child_terminate; | ||
| 107 | die "Cannot find configuration object from stash class ($STASH_CLASS) -- ", | ||
| 108 | "fatal event, will terminate Apache child\n"; | ||
| 109 | } | ||
| 110 | |||
| 111 | # Create the base request object that contains other objects and | ||
| 112 | # info | ||
| 113 | |||
| 114 | my $REQUEST_CLASS = $C->{server_info}{request_class}; | ||
| 115 | unless ( $REQ{ $REQUEST_CLASS } ) { | ||
| 116 | eval "require $REQUEST_CLASS"; | ||
| 117 | if ( $@ ) { | ||
| 118 | $apache->child_terminate; | ||
| 119 | die "Cannot require request class ($REQUEST_CLASS) -- ", | ||
| 120 | "fatal event, will terminate Apache child\n"; | ||
| 121 | } | ||
| 122 | $REQ{ $REQUEST_CLASS }++; | ||
| 123 | } | ||
| 124 | my $R = $REQUEST_CLASS->instance; | ||
| 125 | $R->{stash_class} = $STASH_CLASS; | ||
| 126 | $R->{pid} = $$; | ||
| 127 | $R->{time} = time; | ||
| 128 | return $R; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | # The Apache::Request subclasses the main Apache object and has | ||
| 133 | # additional methods to parse GET/POST data, including file | ||
| 134 | # uploads. Stash this object then get the server name and remote IP | ||
| 135 | # address -- if you're using a proxy, be sure that this has been | ||
| 136 | # passed from the front end server using mod_proxy_add_forward | ||
| 137 | |||
| 138 | sub setup_apache { my $c = shift; return $c->setup_server_interface( @_ ) } | ||
| 139 | |||
| 140 | sub setup_server_interface { | ||
| 141 | my ( $class, $R, $apache ) = @_; | ||
| 142 | my $apr = Apache::Request->new( $apache ); | ||
| 143 | $R->stash( 'apache', $apr ); | ||
| 144 | |||
| 145 | my $srv = $apr->server; | ||
| 146 | $R->{server_name} = $srv->server_hostname; | ||
| 147 | $R->DEBUG && $R->scrib( 1, "Server hostname set to $R->{server_name}" ); | ||
| 148 | |||
| 149 | $R->{remote_host} = $apr->connection->remote_ip(); | ||
| 150 | $R->DEBUG && $R->scrib( 1, "Request coming from $R->{remote_host}" ); | ||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | # Create the cache object if we're supposed to | ||
| 156 | |||
| 157 | sub setup_cache { | ||
| 158 | my ( $class, $R ) = @_; | ||
| 159 | my $CONFIG = $R->CONFIG; | ||
| 160 | my $cache_info = $CONFIG->{cache_info}{data}; | ||
| 161 | if ( ! $R->cache and $cache_info->{use} ) { | ||
| 162 | my $cache_class = $cache_info->{class}; | ||
| 163 | eval "require $cache_class"; | ||
| 164 | if ( $@ ) { | ||
| 165 | $R->scrib( 0, "Cannot include cache class [$cache_class]: $@\n", | ||
| 166 | "Continuing with operation..." ); | ||
| 167 | return; | ||
| 168 | } | ||
| 169 | $R->DEBUG && $R->scrib( 1, "Using cache and setting up with [$cache_class]" ); | ||
| 170 | my $cache = $cache_class->new( $CONFIG ); | ||
| 171 | $R->stash( 'cache', $cache ); | ||
| 172 | } | ||
| 173 | return; | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | # Parse the URL into pieces, stroing everything relevant in | ||
| 178 | # $R->{path}. Also find the 'action' specified in the URL -- we use | ||
| 179 | # this to find a handler in the action table. Note that if the first | ||
| 180 | # item is actually a directive (such as 'Popup'), then we shift it off | ||
| 181 | # and it in $R->{ui}{directive} so the UI handler knows it's | ||
| 182 | # around. After we do this the $R->{path}{current} should be | ||
| 183 | # consistent, with the action as the first member. | ||
| 184 | |||
| 185 | sub parse_uri { | ||
| 186 | my ( $class, $R, $uri ) = @_; | ||
| 187 | |||
| 188 | my $apache = $R->apache; | ||
| 189 | |||
| 190 | # Get the Apache::URI object and put it in $R | ||
| 191 | unless ( $uri ) { | ||
| 192 | $uri = $apache->parsed_uri; | ||
| 193 | } | ||
| 194 | |||
| 195 | # TODO: Do we EVER retrieve the URI object from the stash? Why put | ||
| 196 | # it there? | ||
| 197 | |||
| 198 | $R->stash( 'uri', $uri ); | ||
| 199 | |||
| 200 | # Get the path info from the URL and put it in $R; we save it twice | ||
| 201 | # so we can shift items from one and still keep the original; we | ||
| 202 | # also get the action name from the first item in the path | ||
| 203 | |||
| 204 | my $location = $apache->location; | ||
| 205 | my $path = $uri->path; | ||
| 206 | $R->DEBUG && $R->scrib( 1, "Original path: ($path)" ); | ||
| 207 | if ( $location ne '/' ) { | ||
| 208 | $path =~ s/^$location//; | ||
| 209 | $R->{path}{location} = $location; | ||
| 210 | $R->DEBUG && $R->scrib( 1, "Modified path by removing ($location): ($path)" ); | ||
| 211 | } | ||
| 212 | my @choices = split /\//, $path; | ||
| 213 | shift @choices; | ||
| 214 | $R->DEBUG && $R->scrib( 1, "Items in the path: ", join( " // ", @choices ) ); | ||
| 215 | my @full_choices = @choices; | ||
| 216 | $R->{path}{current} = \@choices; | ||
| 217 | $R->{path}{full} = \@full_choices; | ||
| 218 | |||
| 219 | # If the first item is a directive, remove it and save it for the ui | ||
| 220 | # handler; otherwise it's as if it never existed | ||
| 221 | |||
| 222 | if ( $R->CONFIG->{page_directives}{ $R->{path}{current}->[0] } ) { | ||
| 223 | $R->{ui}{directive} = shift @{ $R->{path}{current} }; | ||
| 224 | $path = '/' . join( '/', @{ $R->{path}{current} } ); | ||
| 225 | } | ||
| 226 | $R->{ui}{action} = $R->{path}{current}->[0]; | ||
| 227 | $R->DEBUG && $R->scrib( 1, "Action found from URL: $R->{ui}{action}" ); | ||
| 228 | |||
| 229 | # Note that $path might have been modified if the first item was a | ||
| 230 | # directive | ||
| 231 | |||
| 232 | $R->{path}{original} = $path; | ||
| 233 | $R->{path}{original} .= '?' . $uri->query if ( $uri->query ); | ||
| 234 | $R->DEBUG && $R->scrib( 1, "Original path/query string set to: $R->{path}{original}" ); | ||
| 235 | return; | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | # Match up the URL path to the UI action (Conductor) and store the | ||
| 240 | # relevant information in $R | ||
| 241 | |||
| 242 | sub find_action_handler { | ||
| 243 | my ( $class, $R ) = @_; | ||
| 244 | ( $R->{ui}{class}, $R->{ui}{method} ) = $R->lookup_conductor( $R->{ui}{action} ); | ||
| 245 | unless ( $R->{ui}{class} ) { | ||
| 246 | $R->scrib( 0, " Conductor not found; displaying oops page." ); | ||
| 247 | eval { $R->throw({ code => 301, | ||
| 248 | type => 'file', | ||
| 249 | user_msg => "Bad URL", | ||
| 250 | system_msg => "Cannot find conductor for $R->{ui}{action}", | ||
| 251 | extra => { url => $R->{path}{original} } }) }; | ||
| 252 | if ( $@ ) { | ||
| 253 | $class->send_html( $R->apache, $@, $R ); | ||
| 254 | die OK . "\n"; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | $R->DEBUG && $R->scrib( 1, "Found $R->{ui}{class} // $R->{ui}{method} for conductor" ); | ||
| 258 | return; | ||
| 259 | } | ||
| 260 | |||
| 261 | # Ensure our main database is up, otherwise bail. | ||
| 262 | |||
| 263 | sub check_database { | ||
| 264 | my ( $class, $R ) = @_; | ||
| 265 | my $db = $R->db( 'main' ); | ||
| 266 | eval { | ||
| 267 | unless ( $db ) { | ||
| 268 | $R->apache->child_terminate; | ||
| 269 | die "Database not found -- fatal event, will terminate Apache child\n"; | ||
| 270 | } | ||
| 271 | $R->DEBUG && warn "Found item: ", ref( $db ), "\n"; | ||
| 272 | $db->ping; | ||
| 273 | }; | ||
| 274 | if ( $@ ) { | ||
| 275 | $R->apache->child_terminate; | ||
| 276 | $R->scrib( 0, "Cannot ping database -- fatal event, will terminate Apache child" ); | ||
| 277 | my $error_msg = $R->throw({ code => 11 }); | ||
| 278 | $class->send_html( $R->apache, $error_msg, $R ); | ||
| 279 | die OK . "\n"; | ||
| 280 | } | ||
| 281 | return; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub setup_cookies_and_session { | ||
| 285 | my ( $class, $R ) = @_; | ||
| 286 | eval { | ||
| 287 | $R->DEBUG && $R->scrib( 2, "Trying to use cookie class: ", $R->cookies ); | ||
| 288 | $R->cookies->parse; | ||
| 289 | $R->DEBUG && $R->scrib( 2, "Cookies in:", Dumper( $R->{cookie}{in} ) ); | ||
| 290 | $R->DEBUG && $R->scrib( 2, "Trying to use session class: ", $R->session ); | ||
| 291 | $R->session->parse; | ||
| 292 | }; | ||
| 293 | if ( $@ ) { | ||
| 294 | $class->send_html( $R->apache, $@, $R ); | ||
| 295 | die OK . "\n"; | ||
| 296 | } | ||
| 297 | return; | ||
| 298 | } | ||
| 299 | |||
| 300 | |||
| 301 | sub finish_cookies_and_session { | ||
| 302 | my ( $class, $R ) = @_; | ||
| 303 | eval { | ||
| 304 | $R->session->save; | ||
| 305 | $R->cookies->bake; | ||
| 306 | $R->DEBUG && $R->scrib( 2, "Cookies out:", | ||
| 307 | join(" // ", map { $_->name . ' = ' . $_->value } | ||
| 308 | values %{ $R->{cookie}{out} } ) ); | ||
| 309 | }; | ||
| 310 | if ( $@ ) { | ||
| 311 | $class->send_html( $R->apache, $@, $R ); | ||
| 312 | die OK . "\n"; | ||
| 313 | } | ||
| 314 | return; | ||
| 315 | } | ||
| 316 | |||
| 317 | |||
| 318 | # Call the various user/group authentication routines | ||
| 319 | |||
| 320 | sub setup_authentication { | ||
| 321 | my ( $class, $R ) = @_; | ||
| 322 | my ( $error_msg ); | ||
| 323 | if ( my $auth_class = $R->auth ) { | ||
| 324 | eval { | ||
| 325 | $auth_class->user; | ||
| 326 | $auth_class->group; | ||
| 327 | $auth_class->is_admin; | ||
| 328 | $auth_class->custom_handler; | ||
| 329 | }; | ||
| 330 | $error_msg = $@; | ||
| 331 | } | ||
| 332 | else { | ||
| 333 | $error_msg = "Authentication cannot be setup! Please ensure 'auth' " . | ||
| 334 | "is setup in your server configuration under 'system_alias'"; | ||
| 335 | } | ||
| 336 | if ( $error_msg ) { | ||
| 337 | $class->send_html( $R->apache, $error_msg, $R ); | ||
| 338 | die OK . "\n"; | ||
| 339 | } | ||
| 340 | |||
| 341 | my $login_info = $R->CONFIG->{login}; | ||
| 342 | return undef unless ( $login_info->{required} ); | ||
| 343 | return undef if ( $R->{auth}{logged_in} ); | ||
| 344 | my $url_requires_login = | ||
| 345 | $class->url_requires_login( $R->{path}{original}, | ||
| 346 | $login_info->{required_skip} ); | ||
| 347 | return undef unless ( $url_requires_login ); | ||
| 348 | return $class->required_login_not_found( $R ); | ||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | # True if the URL requires login (that is, it DOESN'T match a member | ||
| 353 | # of \@url_to_skip) | ||
| 354 | |||
| 355 | sub url_requires_login { | ||
| 356 | my ( $class, $url, $url_to_skip ) = @_; | ||
| 357 | return 1 unless ( $url_to_skip ); | ||
| 358 | my @urls_to_check = ( ref $url_to_skip eq 'ARRAY' ) | ||
| 359 | ? @{ $url_to_skip } : ( $url_to_skip ); | ||
| 360 | my $url_skip = 0; | ||
| 361 | foreach my $url_check ( @urls_to_check ) { | ||
| 362 | next unless ( $url_check ); | ||
| 363 | $url_skip++ if ( $url =~ /$url_check/ ); | ||
| 364 | } | ||
| 365 | return ( ! $url_skip ); | ||
| 366 | } | ||
| 367 | |||
| 368 | # Reset the URL to 'login.required_url' (could this have side | ||
| 369 | # effects?) | ||
| 370 | |||
| 371 | sub required_login_not_found { | ||
| 372 | my ( $class, $R ) = @_; | ||
| 373 | my $required_url = $R->CONFIG->{login}{required_url}; | ||
| 374 | unless ( $required_url ) { | ||
| 375 | $R->scrib( 0, "You have 'login.required' enabled so I'm ensuring ", | ||
| 376 | "that all users have a login, but you don't have ", | ||
| 377 | "'login_required_url' set to a URL where I should ", | ||
| 378 | "send them. Ignoring login requirement setting..." ); | ||
| 379 | return undef; | ||
| 380 | } | ||
| 381 | my $host = $R->{server_name}; | ||
| 382 | my $full_url = join( '', 'http://', $host, $required_url ); | ||
| 383 | my $uri = Apache::URI->parse( $R->apache, $full_url ); | ||
| 384 | $R->DEBUG && $R->scrib( 1, "Resetting request URL to '$full_url' (composed ", | ||
| 385 | "of host '$host' and path '$required_url') since ", | ||
| 386 | "login required and none found" ); | ||
| 387 | |||
| 388 | # This is available for the login page to set in a hidden variable | ||
| 389 | # of its choosing; it's also used in the template plugin method | ||
| 390 | # 'return_url()' called by the login box... | ||
| 391 | |||
| 392 | $R->{path}{login_fail} = $R->{path}{original}; | ||
| 393 | return $class->parse_uri( $R, $uri ); | ||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | # Create the theme used; note that logged-in users can choose | ||
| 398 | # their own, but anonymous users have to stick with 'main'. Each | ||
| 399 | # UI handler (conductor) can decide what to do with the object, but | ||
| 400 | # for now we won't try to fetch all the properties or anything | ||
| 401 | |||
| 402 | sub setup_theme { | ||
| 403 | my ( $class, $R ) = @_; | ||
| 404 | my $C = $R->CONFIG; | ||
| 405 | my $theme_refresh = $R->CONFIG->{session_info}{cache_theme}; | ||
| 406 | if ( $theme_refresh > 0 ) { | ||
| 407 | if ( my $theme = $R->{session}{_oi_cache}{theme} ) { | ||
| 408 | if ( time < $R->{session}{_oi_cache}{theme_refresh_on} ) { | ||
| 409 | $R->DEBUG && $R->scrib( 1, "Got theme from session ok" ); | ||
| 410 | $R->{theme} = $theme; | ||
| 411 | return; | ||
| 412 | } | ||
| 413 | $R->DEBUG && $R->scrib( 1, "Theme session cache expired; refreshing from db" ); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | $R->{theme} = ( $R->{auth}{user} and $R->{auth}{user}{theme_id} ) | ||
| 418 | ? eval { $R->{auth}{user}->theme } | ||
| 419 | : eval { $R->theme->fetch( $C->{default_objects}{theme} ) }; | ||
| 420 | if ( $@ ) { | ||
| 421 | my $ei = SPOPS::Error->get; | ||
| 422 | OpenInteract::Error->set( $ei ); | ||
| 423 | $R->throw({ code => 404 }); | ||
| 424 | $R->scrib( 0, "Error! Cannot retrieve theme! ( Class: ", $R->theme, ")", | ||
| 425 | "with error ($@ / $ei->{system_msg}) Help!" ); | ||
| 426 | my $admin_email = $C->{mail}{admin_email} || $C->{admin_email}; | ||
| 427 | my $error_msg = <<THEMERR; | ||
| 428 | Fundamental part of OpenInteract (themes) not functioning; please contact the | ||
| 429 | system administrator (<a href="mailto:$admin_email">$admin_email</a>). | ||
| 430 | THEMERR | ||
| 431 | $class->send_html( $R->apache, $error_msg, $R ); | ||
| 432 | die OK . "\n"; | ||
| 433 | } | ||
| 434 | |||
| 435 | # Find all the properties before we potentially cache | ||
| 436 | |||
| 437 | $R->{theme}->discover_properties; | ||
| 438 | if ( $theme_refresh > 0 ) { | ||
| 439 | $R->{session}{_oi_cache}{theme} = $R->{theme}; | ||
| 440 | $R->{session}{_oi_cache}{theme_refresh_on} = time + ( $theme_refresh + 60 ); | ||
| 441 | $R->DEBUG && $R->scrib( 1, "Set theme to session cache, expires ", | ||
| 442 | "in [$theme_refresh] minutes" ); | ||
| 443 | } | ||
| 444 | return; | ||
| 445 | } | ||
| 446 | |||
| 447 | |||
| 448 | # Runs the content handler -- this should either return the full page | ||
| 449 | # ready for display or put the information into $R necessary to send a | ||
| 450 | # static (non-HTML) file | ||
| 451 | |||
| 452 | sub run_content_handler { | ||
| 453 | my ( $class, $R ) = @_; | ||
| 454 | my ( $ui_class, $ui_method ) = ( $R->{ui}{class}, $R->{ui}{method} ); | ||
| 455 | $R->DEBUG && $R->scrib( 1, "Trying the conductor: <<$ui_class/$ui_method>>" ); | ||
| 456 | return $ui_class->$ui_method(); | ||
| 457 | } | ||
| 458 | |||
| 459 | |||
| 460 | # Send a static (non-html/text) file to the user; note that the | ||
| 461 | # content type should already have been set -- normally this is done | ||
| 462 | # automatically by Apache, particularly if the URL ends with a known | ||
| 463 | # filetype | ||
| 464 | |||
| 465 | sub send_static_file { | ||
| 466 | my ( $class, $R ) = @_; | ||
| 467 | my $file_spec = $R->{page}{send_file}; | ||
| 468 | my ( $file_size ); | ||
| 469 | my ( $fh ); | ||
| 470 | |||
| 471 | # File is a handle... | ||
| 472 | |||
| 473 | if ( ref $file_spec ) { | ||
| 474 | $fh = $file_spec; | ||
| 475 | my $default_type = 'application/octet-stream'; | ||
| 476 | unless ( $R->{page}{content_type} ) { | ||
| 477 | $R->scrib( 0, "No content type set for filehandle to send, ", | ||
| 478 | "using default '$default_type'\n" ); | ||
| 479 | $R->{page}{content_type} = $default_type; | ||
| 480 | } | ||
| 481 | $file_size = (stat $fh)[7]; | ||
| 482 | $R->DEBUG && $R->scrib( 1, "Sending filehandle of size", | ||
| 483 | "($file_size) and type", | ||
| 484 | "($R->{page}{content_type})" ); | ||
| 485 | } | ||
| 486 | |||
| 487 | # File is a filename... | ||
| 488 | else { | ||
| 489 | $fh = Apache->gensym; | ||
| 490 | eval { open( $fh, $file_spec ) || die $! }; | ||
| 491 | if ( $@ ) { | ||
| 492 | $R->scrib( 0, "Cannot open static file from filesystem ($file_spec): $@" ); | ||
| 493 | return NOT_FOUND; | ||
| 494 | } | ||
| 495 | $file_size = $R->{page}{send_file_size} | ||
| 496 | || (stat $file_spec)[7]; | ||
| 497 | $R->DEBUG && $R->scrib( 1, "Sending file ($file_spec) of size", | ||
| 498 | "($file_size) and type", | ||
| 499 | "($R->{page}{content_type})" ); | ||
| 500 | } | ||
| 501 | $R->apache->headers_out->{'Content-Length'} = $file_size; | ||
| 502 | $R->apache->send_http_header( $R->{page}{content_type} ); | ||
| 503 | $R->apache->send_fd( $fh ); | ||
| 504 | close( $fh ); | ||
| 505 | } | ||
| 506 | |||
| 507 | |||
| 508 | # Send plain html (or text) to the browser | ||
| 509 | |||
| 510 | sub send_html { | ||
| 511 | my ( $class, $apache, $content, $R ) = @_; | ||
| 512 | if ( ref $R ) { | ||
| 513 | unless ( $R->CONFIG->{no_promotion} ) { | ||
| 514 | $apache->headers_out->{'X-Powered-By'} = | ||
| 515 | "OpenInteract $OpenInteract::VERSION"; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | my $content_type = $R->{page}{content_type} || | ||
| 519 | $apache->content_type || | ||
| 520 | 'text/html'; | ||
| 521 | $content_type = ( $content_type eq 'httpd/unix-directory' ) | ||
| 522 | ? 'text/html' : $content_type; | ||
| 523 | $apache->send_http_header( $content_type ); | ||
| 524 | $apache->print( $content ); | ||
| 525 | } | ||
| 526 | |||
| 527 | |||
| 528 | # Do any necessary cleanup -- logging, remove stash entries, etc. | ||
| 529 | |||
| 530 | sub cleanup { | ||
| 531 | my ( $class, $R ) = @_; | ||
| 532 | |||
| 533 | # Wrap this in an eval so it won't bomb if 0.56 isn't installed | ||
| 534 | |||
| 535 | eval { SPOPS::Exception->clear_stack }; | ||
| 536 | |||
| 537 | $R->DEBUG && $R->scrib( 2, "\n\nErrors: ", Dumper( $R->error_object->report ), "\n" ); | ||
| 538 | $R->error->clear; | ||
| 539 | $R->error_object->clear_listing; | ||
| 540 | $R->DEBUG && $R->scrib( 1, "\nRequest done:", scalar localtime, "\n", | ||
| 541 | "path: ($R->{path}{original}) PID: ($$)\n", | ||
| 542 | "from: ($R->{remote_host})\n$SEP\n" ); | ||
| 543 | $R->finish_request; | ||
| 544 | return; | ||
| 545 | } | ||
| 546 | |||
| 547 | 1; | ||
| 548 | |||
| 549 | __END__ |