| 1 | =pod | ||
| 2 | |||
| 3 | =begin classdoc | ||
| 4 | |||
| 5 | Base class for Content classes. | ||
| 6 | Provides an interface definition for | ||
| 7 | generating or storing content, or generating a header, | ||
| 8 | given an HTTP::Request, a URI, URL parameters or POSTed content, | ||
| 9 | HTTP::Daemon::Threaded::SessionCache object, and | ||
| 10 | a HTTP::Daemon::Threaded::ContentParams context | ||
| 11 | <p> | ||
| 12 | Copyright© 2006-2008, Dean Arnold, Presicient Corp., USA<br> | ||
| 13 | All rights reserved. | ||
| 14 | <p> | ||
| 15 | Licensed under the Academic Free License version 3.0, as specified at | ||
| 16 | <a href='http://www.opensource.org/licenses/afl-3.0.php'>OpenSource.org</a>. | ||
| 17 | |||
| 18 | @author D. Arnold | ||
| 19 | @since 2006-08-21 | ||
| 20 | @self $self | ||
| 21 | |||
| 22 | =end classdoc | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | package HTTP::Daemon::Threaded::Content; | ||
| 27 | |||
| 28 | use HTTP::Daemon::Threaded::Logable; | ||
| 29 | use base qw(HTTP::Daemon::Threaded::Logable); | ||
| 30 | |||
| 31 | use strict; | ||
| 32 | use warnings; | ||
| 33 | |||
| 34 | our $VERSION = '0.91'; | ||
| 35 | |||
| 36 | =pod | ||
| 37 | |||
| 38 | =begin classdoc | ||
| 39 | |||
| 40 | |||
| 41 | Constructor. Stores ContentParams, SessionCache, and Logger objects, | ||
| 42 | and performs any content-specific initialization. | ||
| 43 | |||
| 44 | @param LogLevel <i>(optional)</i> logging level; 1 => errors only; 2 => errors and warnings only; 3 => errors, warnings, | ||
| 45 | and info messages; default 1 | ||
| 46 | @param EventLogger <i>(optional)</i> Instance of a HTTP::Daemon::Threaded::Logger to receive | ||
| 47 | event notifications (except for web requests) | ||
| 48 | @param SessionCache <i>(optional)</i> threads::shared object implementing HTTP::Daemon::Threaded::SessionCache | ||
| 49 | @param ContentParams <i>(optional)</i> name of a ContentsParam concrete implementation | ||
| 50 | |||
| 51 | @returns a HTTP::Daemon::Threaded::Content object | ||
| 52 | |||
| 53 | =end classdoc | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub new { | ||
| 58 | my ($class, %args) = @_; | ||
| 59 | return bless \%args, $class; | ||
| 60 | } | ||
| 61 | |||
| 62 | =pod | ||
| 63 | |||
| 64 | =begin classdoc | ||
| 65 | |||
| 66 | |||
| 67 | Generate and send content. Uses input URI, URL parameters, Cookie and Session | ||
| 68 | objects to load and/or generate content, including | ||
| 69 | providing any needed HTTP headers. Note that the <code>$params</code> | ||
| 70 | parameter <i>may</i> be an array of multipart HTTP::Message objects if | ||
| 71 | a multipart POST request was received (e.g., for file upload). | ||
| 72 | <p> | ||
| 73 | Content handlers should restrict the HTTP::Daemon::ClientConn response | ||
| 74 | methods to <code>send_response(), send_file_response(), send_error(), or | ||
| 75 | send_redirect()</code>, as HTTP::Daemon::Threaded::Socket provides | ||
| 76 | automatic web logging for those methods. | ||
| 77 | |||
| 78 | @param $fd HTTP::Daemon::Threaded::Socket object for the client | ||
| 79 | @param $request HTTP::Request object | ||
| 80 | @param $uri request URI | ||
| 81 | @param $params hashref of URL parameters (key => value) OR arrayref of HTTP::Message | ||
| 82 | objects (for multipart POSTs) | ||
| 83 | @param $session <i>(optional)</i> a HTTP::Daemon::Threaded::Session object, | ||
| 84 | if the application configured a SessionCache class, and if WebClient | ||
| 85 | was able to recover an existing session from such a SessionCache. | ||
| 86 | =end classdoc | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub getContent { | ||
| 91 | my ($self, $fd, $request, $uri, $params, $session) = @_; | ||
| 92 | $fd->send_error(404); | ||
| 93 | } | ||
| 94 | |||
| 95 | =pod | ||
| 96 | |||
| 97 | =begin classdoc | ||
| 98 | |||
| 99 | |||
| 100 | Generate and send content header only. Uses input URI, URL parameters, Cookie | ||
| 101 | and Session objects to load and/or generate content, including | ||
| 102 | providing any needed HTTP headers. | ||
| 103 | <p> | ||
| 104 | Content handlers should restrict the HTTP::Daemon::ClientConn response | ||
| 105 | methods to <code>send_response(), send_file_header(), send_error(), or | ||
| 106 | send_redirect()</code>, as HTTP::Daemon::Threaded::Socket provides | ||
| 107 | automatic web logging for those methods. | ||
| 108 | |||
| 109 | @param $fd HTTP::Daemon::Threaded::Socket object for the client | ||
| 110 | @param $request HTTP::Request object | ||
| 111 | @param $uri request URI | ||
| 112 | @param $params hashref of URL parameters (key => value) | ||
| 113 | @param $session <i>(optional)</i> a HTTP::Daemon::Threaded::Session object, | ||
| 114 | if the application configured a SessionCache class, and if WebClient | ||
| 115 | was able to recover an existing session from such a SessionCache. | ||
| 116 | =end classdoc | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub getHeader { | ||
| 121 | my ($self, $fd, $request, $uri, $params, $session) = @_; | ||
| 122 | $fd->send_error(404); | ||
| 123 | } | ||
| 124 | |||
| 125 | =pod | ||
| 126 | |||
| 127 | =begin classdoc | ||
| 128 | |||
| 129 | |||
| 130 | Put content. Uses input URI, URL parameters, Cookie and Session | ||
| 131 | objects to process received content (via PUT request), including | ||
| 132 | providing any needed HTTP headers. | ||
| 133 | <p> | ||
| 134 | Content handlers should restrict the HTTP::Daemon::ClientConn response | ||
| 135 | methods to <code>send_response(), send_file_response(), send_file_header(), send_error(), or | ||
| 136 | send_redirect()</code>, as HTTP::Daemon::Threaded::Socket provides | ||
| 137 | automatic web logging for those methods. | ||
| 138 | |||
| 139 | @param $fd HTTP::Daemon::Threaded::Socket object for the client | ||
| 140 | @param $request HTTP::Request object | ||
| 141 | @param $uri request URI | ||
| 142 | @param $content decoded PUT content | ||
| 143 | @param $session <i>(optional)</i> a HTTP::Daemon::Threaded::Session object, | ||
| 144 | if the application configured a SessionCache class, and if WebClient | ||
| 145 | was able to recover an existing session from such a SessionCache. | ||
| 146 | =end classdoc | ||
| 147 | |||
| 148 | =cut | ||
| 149 | |||
| 150 | sub putContent { | ||
| 151 | my ($self, $fd, $request, $uri, $content, $session) = @_; | ||
| 152 | |||
| 153 | $fd->send_error(401); | ||
| 154 | } | ||
| 155 | |||
| 156 | =pod | ||
| 157 | |||
| 158 | =begin classdoc | ||
| 159 | |||
| 160 | |||
| 161 | Set a response cookie. Convenience method to format and set a Set-Cookie header. | ||
| 162 | |||
| 163 | @param $res HTTP::Response object | ||
| 164 | @param $session HTTP::Daemon::Threaded::Session object from which to extract the cookie. | ||
| 165 | |||
| 166 | @return this Content object | ||
| 167 | =end classdoc | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub setCookie { | ||
| 172 | my ($self, $res, $session) = @_; | ||
| 173 | |||
| 174 | $res->header('Set-Cookie' => $session->getCookie()); | ||
| 175 | return $self; | ||
| 176 | } | ||
| 177 | |||
| 178 | 1; |