| 1 | |||
| 2 | |||
| 3 | package HTTP::DAVServer::Respond; | ||
| 4 | |||
| 5 | our $VERSION=0.1; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | HTTP::DAVServer::Respond - Produces all response codes, headers and sends output to client | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | # 0 flags does not have content | ||
| 17 | # 1 flags must have content | ||
| 18 | # -1 flags may have content | ||
| 19 | |||
| 20 | my $methods = { | ||
| 21 | 'OPTIONS' => 0, | ||
| 22 | 'GET' => 0, | ||
| 23 | 'HEAD' => 0, | ||
| 24 | 'POST' => 1, | ||
| 25 | 'DELETE' => 0, | ||
| 26 | 'PROPFIND' => 1, | ||
| 27 | 'PROPPATCH' => 1, | ||
| 28 | 'COPY' => 0, | ||
| 29 | 'MOVE' => 0, | ||
| 30 | 'PUT' => 1, | ||
| 31 | 'MKCOL' => 0, | ||
| 32 | }; | ||
| 33 | |||
| 34 | sub handles { | ||
| 35 | |||
| 36 | if ($_[1]) { | ||
| 37 | return exists $methods->{$_[1]}; | ||
| 38 | } | ||
| 39 | return $methods; | ||
| 40 | |||
| 41 | } | ||
| 42 | |||
| 43 | sub hasContent { | ||
| 44 | |||
| 45 | exists $methods->{$_[1]} && return $methods->{$_[1]}; | ||
| 46 | warn "hasContent called with no valid method name ($_[1])\n" if $HTTP::DAVServer::WARN; | ||
| 47 | return 0; | ||
| 48 | |||
| 49 | } | ||
| 50 | |||
| 51 | sub ok { | ||
| 52 | |||
| 53 | my ($self, $r) = @_; | ||
| 54 | warn "OK @_\n" if $HTTP::DAVServer::TRACE; | ||
| 55 | |||
| 56 | print $r->header( | ||
| 57 | -status => "200 OK", | ||
| 58 | $self->headers, | ||
| 59 | -Content_Length => 0, | ||
| 60 | -Content_Type => "text/html; charset=UTF-8", | ||
| 61 | ); | ||
| 62 | |||
| 63 | exit 0; | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | sub created { | ||
| 68 | |||
| 69 | my ($self, $r) = @_; | ||
| 70 | warn "CREATED @_\n" if $HTTP::DAVServer::TRACE; | ||
| 71 | |||
| 72 | print $r->header( | ||
| 73 | -status => "201 Created", | ||
| 74 | $self->headers, | ||
| 75 | -Content_Length => 0, | ||
| 76 | -Content_Type => "text/html; charset=UTF-8", | ||
| 77 | ); | ||
| 78 | |||
| 79 | exit 0; | ||
| 80 | |||
| 81 | } | ||
| 82 | |||
| 83 | sub multiStatus { | ||
| 84 | |||
| 85 | warn "MULTISTATUS @_\n" if $HTTP::DAVServer::TRACE; | ||
| 86 | my ($self, $r, $xml) = @_; | ||
| 87 | |||
| 88 | my $message = qq(<?xml version="1.0" encoding="utf-8" ?>\n<multistatus xmlns="DAV:">\n$xml\n</multistatus>); | ||
| 89 | warn "RESPOND XML:\n$message\n" if $HTTP::DAVServer::TRACE; | ||
| 90 | |||
| 91 | print $r->header( | ||
| 92 | -status => "207 Multi-Status", | ||
| 93 | $self->headers, | ||
| 94 | -Content_Length => length $message, | ||
| 95 | -Content_Type => "text/xml; charset=UTF-8", | ||
| 96 | ); | ||
| 97 | |||
| 98 | print $message; | ||
| 99 | |||
| 100 | exit 0; | ||
| 101 | |||
| 102 | } | ||
| 103 | |||
| 104 | sub badRequest { | ||
| 105 | |||
| 106 | warn "BADREQUEST @_\n" if $HTTP::DAVServer::TRACE; | ||
| 107 | |||
| 108 | my ($self, $r, $flag, $detail) = @_; | ||
| 109 | |||
| 110 | my $message = "<h1>400 Bad Request</h1>\n$flag $detail\n"; | ||
| 111 | |||
| 112 | print $r->header( | ||
| 113 | -status => "400 Bad Request", | ||
| 114 | $self->headers, | ||
| 115 | -Content_Length => length $message, | ||
| 116 | -Content_Type => "text/html; charset=UTF-8", | ||
| 117 | ); | ||
| 118 | |||
| 119 | print $message; | ||
| 120 | |||
| 121 | exit 0; | ||
| 122 | |||
| 123 | } | ||
| 124 | |||
| 125 | sub challenge { | ||
| 126 | |||
| 127 | warn "CHALLENGE @_\n" if $HTTP::DAVServer::TRACE; | ||
| 128 | my ($self, $r) = @_; | ||
| 129 | |||
| 130 | print $r->header( | ||
| 131 | -status => "401 Unauthorized", | ||
| 132 | $self->headers, | ||
| 133 | -Content_Length => 0, | ||
| 134 | -Content_Type => "text/html; charset=UTF-8", | ||
| 135 | -WWW_Authenticate => qq(Digest realm="mymac", stale=false, nonce="c847ab2bf1b3661a9bf2a6bef87a9ef1", qop="auth", algorithm="MD5"), | ||
| 136 | ); | ||
| 137 | |||
| 138 | exit 0; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub forbidden { | ||
| 142 | |||
| 143 | warn "FORBIDDEN @_\n" if $HTTP::DAVServer::TRACE; | ||
| 144 | |||
| 145 | my ($self, $r) = @_; | ||
| 146 | |||
| 147 | print $r->header( | ||
| 148 | -status => "403 Forbidden", | ||
| 149 | $self->headers, | ||
| 150 | -Content_Length => 0, | ||
| 151 | -Content_Type => "text/html; charset=UTF-8", | ||
| 152 | ); | ||
| 153 | |||
| 154 | exit 0; | ||
| 155 | |||
| 156 | } | ||
| 157 | |||
| 158 | sub notFound { | ||
| 159 | |||
| 160 | warn "NOTFOUND @_\n" if $HTTP::DAVServer::TRACE; | ||
| 161 | my ($self, $r) = @_; | ||
| 162 | |||
| 163 | print $r->header( | ||
| 164 | -status => "404 Not Found", | ||
| 165 | $self->headers, | ||
| 166 | -Content_Length => 0, | ||
| 167 | -Content_Type => "text/html; charset=UTF-8", | ||
| 168 | ); | ||
| 169 | |||
| 170 | exit 0; | ||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | sub notAllowed { | ||
| 175 | |||
| 176 | warn "NOTALLOWED @_\n" if $HTTP::DAVServer::TRACE; | ||
| 177 | |||
| 178 | my ($self, $r) = @_; | ||
| 179 | |||
| 180 | print $r->header( | ||
| 181 | -status => "405 Method Not Allowed", | ||
| 182 | $self->headers, | ||
| 183 | -Content_Length => 0, | ||
| 184 | -Content_Type => "text/html; charset=UTF-8", | ||
| 185 | ); | ||
| 186 | |||
| 187 | exit 0; | ||
| 188 | |||
| 189 | } | ||
| 190 | |||
| 191 | sub conflict { | ||
| 192 | |||
| 193 | warn "CONFLICT @_\n" if $HTTP::DAVServer::TRACE; | ||
| 194 | my ($self, $r) = @_; | ||
| 195 | |||
| 196 | print $r->header( | ||
| 197 | -status => "409 Conflict", | ||
| 198 | $self->headers, | ||
| 199 | -Content_Length => 0, | ||
| 200 | -Content_Type => "text/html; charset=UTF-8", | ||
| 201 | ); | ||
| 202 | |||
| 203 | exit 0; | ||
| 204 | |||
| 205 | } | ||
| 206 | |||
| 207 | sub unsupported { | ||
| 208 | |||
| 209 | warn "UNSUPPORTED @_\n" if $HTTP::DAVServer::TRACE; | ||
| 210 | my ($self, $r) = @_; | ||
| 211 | |||
| 212 | print $r->header( | ||
| 213 | -status => "415 Unsupported Media Type", | ||
| 214 | $self->headers, | ||
| 215 | -Content_Length => 0, | ||
| 216 | -Content_Type => "text/html; charset=UTF-8", | ||
| 217 | ); | ||
| 218 | |||
| 219 | exit 0; | ||
| 220 | |||
| 221 | } | ||
| 222 | |||
| 223 | sub serverError { | ||
| 224 | |||
| 225 | warn "SERVERERROR @_\n" if $HTTP::DAVServer::TRACE; | ||
| 226 | my ($self, $r) = @_; | ||
| 227 | |||
| 228 | print $r->header( | ||
| 229 | -status => "500 Server Error", | ||
| 230 | $self->headers, | ||
| 231 | -Content_Length => 0, | ||
| 232 | -Content_Type => "text/html; charset=UTF-8", | ||
| 233 | ); | ||
| 234 | |||
| 235 | exit 0; | ||
| 236 | |||
| 237 | } | ||
| 238 | |||
| 239 | sub notImplemented { | ||
| 240 | |||
| 241 | warn "SERVERERROR @_\n" if $HTTP::DAVServer::TRACE; | ||
| 242 | my ($self, $r) = @_; | ||
| 243 | |||
| 244 | print $r->header( | ||
| 245 | -status => "501 Not Implemented", | ||
| 246 | $self->headers, | ||
| 247 | -Content_Length => 0, | ||
| 248 | -Content_Type => "text/html; charset=UTF-8", | ||
| 249 | ); | ||
| 250 | |||
| 251 | exit 0; | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | sub diskFull { | ||
| 256 | |||
| 257 | warn "DISKFULL @_\n" if $HTTP::DAVServer::TRACE; | ||
| 258 | my ($self, $r) = @_; | ||
| 259 | |||
| 260 | print $r->header( | ||
| 261 | -status => "507 Insufficient Storage", | ||
| 262 | $self->headers, | ||
| 263 | -Content_Length => 0, | ||
| 264 | -Content_Type => "text/html; charset=UTF-8", | ||
| 265 | ); | ||
| 266 | |||
| 267 | exit 0; | ||
| 268 | |||
| 269 | } | ||
| 270 | |||
| 271 | sub Server { | ||
| 272 | return "Jay's DAV server"; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub DAV { | ||
| 276 | return "1"; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub headers { | ||
| 280 | my $self=shift; | ||
| 281 | return ( | ||
| 282 | -nph => 1, | ||
| 283 | -Server => $self->Server, | ||
| 284 | -DAV => $self->DAV, | ||
| 285 | ) | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | =head1 SUPPORT | ||
| 290 | |||
| 291 | For technical support please email to jlawrenc@cpan.org ... | ||
| 292 | for faster service please include "HTTP::DAVServer" and "help" in your subject line. | ||
| 293 | |||
| 294 | =head1 AUTHOR | ||
| 295 | |||
| 296 | Jay J. Lawrence - jlawrenc@cpan.org | ||
| 297 | Infonium Inc., Canada | ||
| 298 | http://www.infonium.ca/ | ||
| 299 | |||
| 300 | =head1 COPYRIGHT | ||
| 301 | |||
| 302 | Copyright (c) 2003 Jay J. Lawrence, Infonium Inc. All rights reserved. | ||
| 303 | This program is free software; you can redistribute | ||
| 304 | it and/or modify it under the same terms as Perl itself. | ||
| 305 | |||
| 306 | The full text of the license can be found in the | ||
| 307 | LICENSE file included with this module. | ||
| 308 | |||
| 309 | =head1 ACKNOWLEDGEMENTS | ||
| 310 | |||
| 311 | Thank you to the authors of my prequisite modules. With out your help this code | ||
| 312 | would be much more difficult to write! | ||
| 313 | |||
| 314 | XML::Simple - Grant McLean | ||
| 315 | XML::SAX - Matt Sergeant | ||
| 316 | DateTime - Dave Rolsky | ||
| 317 | |||
| 318 | Also the authors of litmus, a very helpful tool indeed! | ||
| 319 | |||
| 320 | =head1 SEE ALSO | ||
| 321 | |||
| 322 | HTTP::DAV, HTTP::Webdav, http://www.webdav.org/, RFC 2518 | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | 1; | ||
| 327 |