| 1 | |||
| 2 | |||
| 3 | package HTTP::DAVServer::PROPFIND; | ||
| 4 | |||
| 5 | our $VERSION=0.1; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | HTTP::DAVServer::PROPFIND - Implements the PROPFIND method | ||
| 13 | |||
| 14 | =cut | ||
| 15 | |||
| 16 | use File::stat qw(stat); | ||
| 17 | |||
| 18 | our %fileProps = ( | ||
| 19 | creationdate => 1, | ||
| 20 | displayname => 1, | ||
| 21 | getcontentlanguage => 1, | ||
| 22 | getcontentlength => 1, | ||
| 23 | getetag => 1, | ||
| 24 | getlastmodified => 1, | ||
| 25 | getcontenttype => 1, | ||
| 26 | lockdiscovery => 1, | ||
| 27 | resourcetype => 1, | ||
| 28 | source => 1, | ||
| 29 | supportedlock => 1, | ||
| 30 | ); | ||
| 31 | |||
| 32 | our %collProps = ( | ||
| 33 | creationdate => 1, | ||
| 34 | displayname => 1, | ||
| 35 | resourcetype => 1, | ||
| 36 | supportedlock => 1, | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub handle { | ||
| 40 | |||
| 41 | my ($self, $r, $url, $responder, $req) = @_; | ||
| 42 | |||
| 43 | $responder->badRequest( $r, "PROPFINDVAGUE" ) unless exists $req->{'{DAV:}prop'} | ||
| 44 | || exists $req->{'{DAV:}allprop'} | ||
| 45 | || exists $req->{'{DAV:}propname'}; | ||
| 46 | |||
| 47 | $url =~ s#/*$##; | ||
| 48 | |||
| 49 | my $depth=$r->http('Depth'); | ||
| 50 | $depth="infinite" unless $depth =~ /^(0|1|infinite)$/; | ||
| 51 | |||
| 52 | my $root = $HTTP::DAVServer::ROOT; | ||
| 53 | my $host = $HTTP::DAVServer::HOST; | ||
| 54 | |||
| 55 | |||
| 56 | # Property names response | ||
| 57 | if ( exists $req->{'{DAV:}propname'} ) { | ||
| 58 | |||
| 59 | my $xml="<response>"; | ||
| 60 | |||
| 61 | if ( $depth eq "0") { | ||
| 62 | |||
| 63 | $xml .= qq(<propstat>\n<href>http://$host$url</href>\n<prop>\n); | ||
| 64 | |||
| 65 | map { $xml .= qq(\t<$_/>\n) } keys %fileProps if -f "$root/$url"; | ||
| 66 | map { $xml .= qq(\t<$_/>\n) } keys %collProps if -d "$root/$url"; | ||
| 67 | |||
| 68 | if (-r "$root/$url") { | ||
| 69 | $xml .= qq(</prop>\n<status>HTTP/1.1 200 OK</status>\n</propstat>\n); | ||
| 70 | } else { | ||
| 71 | $xml .= qq(</prop>\n<status>HTTP/1.1 404 Not Found</status>\n</propstat>\n); | ||
| 72 | } | ||
| 73 | |||
| 74 | } else { | ||
| 75 | |||
| 76 | if ( -f "$root/$url" ) { | ||
| 77 | $xml .= qq(<propstat>\n<href>http://$host$url</href>\n<prop>\n); | ||
| 78 | map { $xml .= qq(\t<$_/>\n) } keys %fileProps; | ||
| 79 | $xml .= qq(</prop>\n<status>HTTP/1.1 200 OK</status>\n</propstat>\n); | ||
| 80 | } | ||
| 81 | |||
| 82 | if ( -d "$root/$url" ) { | ||
| 83 | |||
| 84 | $xml .= qq(<propstat>\n<href>http://$host$url</href>\n<prop>\n); | ||
| 85 | map { $xml .= qq(\t<$_/>\n) } keys %collProps; | ||
| 86 | $xml .= qq(</prop>\n<status>HTTP/1.1 200 OK</status>\n</propstat>\n); | ||
| 87 | |||
| 88 | opendir DIR, "$root/$url"; | ||
| 89 | while ( my $file = readdir DIR ) { | ||
| 90 | if (-f "$root/$url/$file") { | ||
| 91 | $xml .= qq(</response>\n<response>\n); | ||
| 92 | $xml .= qq(<propstat>\n<href>http://$host$url/$file</href>\n<prop>\n); | ||
| 93 | map { $xml .= qq(\t<$_/>\n) } keys %fileProps; | ||
| 94 | $xml .= qq(</prop>\n<status>HTTP/1.1 200 OK</status>\n</propstat>\n); | ||
| 95 | last; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | $xml .= "</response>\n"; | ||
| 102 | |||
| 103 | $responder->multiStatus( $r, $xml ); | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | my $reqProps=$req->{'{DAV:}prop'} || undef; | ||
| 109 | |||
| 110 | # Property values response | ||
| 111 | my $response=[]; | ||
| 112 | fetchProps( $root, $url, "http://$host", $reqProps, $depth, $response ); | ||
| 113 | my $xml = qq(<response>\n) . join ( qq(</response>\n<response>\n), @$response) . qq(</response>\n); | ||
| 114 | $responder->multiStatus( $r, $xml ); | ||
| 115 | |||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | sub fetchProps { | ||
| 120 | |||
| 121 | my ($root, $url, $urlPrefix, $reqProps, $depth, $response) = @_; | ||
| 122 | |||
| 123 | $DB::single=1; | ||
| 124 | my $path="$root$url"; | ||
| 125 | |||
| 126 | if ($depth eq "0") { | ||
| 127 | |||
| 128 | if (-d $path) { | ||
| 129 | |||
| 130 | return push @$response, collection( $path, $url, $urlPrefix, $reqProps ); | ||
| 131 | |||
| 132 | } else { | ||
| 133 | |||
| 134 | return push @$response, file( $path, $url, $urlPrefix, $reqProps ); | ||
| 135 | |||
| 136 | } | ||
| 137 | |||
| 138 | } | ||
| 139 | |||
| 140 | if ($depth eq "1") { | ||
| 141 | |||
| 142 | if (-d $path) { | ||
| 143 | |||
| 144 | fetchProps($root, $url, $urlPrefix, $reqProps, 0, $response); | ||
| 145 | |||
| 146 | opendir PATH, $path; | ||
| 147 | while (my $item = readdir PATH) { | ||
| 148 | next if $item =~ /^\.\.?$/; | ||
| 149 | fetchProps($root, "$url/$item", $urlPrefix, $reqProps, 0, $response); | ||
| 150 | } | ||
| 151 | closedir PATH; | ||
| 152 | |||
| 153 | } else { | ||
| 154 | fetchProps($root, $url, $urlPrefix, $reqProps, 0, $response); | ||
| 155 | } | ||
| 156 | |||
| 157 | } | ||
| 158 | |||
| 159 | if ($depth eq "infinite") { | ||
| 160 | |||
| 161 | if (-d $path) { | ||
| 162 | |||
| 163 | opendir PATH, $path; | ||
| 164 | while (my $item = readdir PATH) { | ||
| 165 | fetchProps($root, "$url/$item", $urlPrefix, $reqProps, $depth, $response); | ||
| 166 | } | ||
| 167 | closedir PATH; | ||
| 168 | |||
| 169 | } else { | ||
| 170 | fetchProps($root, $url, $urlPrefix, $reqProps, 0, $response); | ||
| 171 | } | ||
| 172 | |||
| 173 | } | ||
| 174 | |||
| 175 | } | ||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | sub collection { | ||
| 182 | |||
| 183 | my ($path, $url, $urlPrefix, $reqProps) = @_; | ||
| 184 | |||
| 185 | $reqProps ||= \%collProps; | ||
| 186 | |||
| 187 | my $stat=stat($path); | ||
| 188 | |||
| 189 | my $ret=qq(\t<href>$urlPrefix$url/</href>\n) | ||
| 190 | . qq(\t<propstat>) | ||
| 191 | . qq(<prop>\n); | ||
| 192 | |||
| 193 | foreach my $reqProp ( keys %{$reqProps} ) { | ||
| 194 | |||
| 195 | $reqProp =~ s/^{DAV:}//; | ||
| 196 | |||
| 197 | $ret .= qq(\t<$reqProp><collection/></$reqProp>\n) and next if $reqProp eq "resourcetype"; | ||
| 198 | |||
| 199 | if ($reqProp eq "creationdate") { | ||
| 200 | $ret .= qq(\t<$reqProp>) . HTTP::DAVServer::dateEpoch( $stat->mtime ) . qq(</$reqProp>\n); | ||
| 201 | next; | ||
| 202 | } | ||
| 203 | |||
| 204 | if ($reqProp eq "displayname") { | ||
| 205 | $path =~ /([^\/]+\/)$/; | ||
| 206 | $ret .= qq(\t<$reqProp>$1</$reqProp>\n); | ||
| 207 | next; | ||
| 208 | } | ||
| 209 | |||
| 210 | warn "Collection property $reqProp not known\n" if $HTTP::DAVServer::WARN; | ||
| 211 | |||
| 212 | } | ||
| 213 | |||
| 214 | $ret .= qq(</prop>) | ||
| 215 | . qq(<status>HTTP/1.1 200 OK</status>) | ||
| 216 | . qq(</propstat>); | ||
| 217 | |||
| 218 | return $ret; | ||
| 219 | |||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | sub file { | ||
| 224 | |||
| 225 | my ($path, $url, $urlPrefix, $reqProps) = @_; | ||
| 226 | $reqProps ||= \%fileProps; | ||
| 227 | |||
| 228 | my $stat=stat($path); | ||
| 229 | |||
| 230 | my $ret=qq(\t<href>$urlPrefix$url</href>\n) | ||
| 231 | . qq(\t<propstat>) | ||
| 232 | . qq(<prop>\n); | ||
| 233 | |||
| 234 | foreach my $reqProp ( keys %{$reqProps} ) { | ||
| 235 | |||
| 236 | $reqProp =~ s/^{DAV:}//; | ||
| 237 | |||
| 238 | $ret .= qq(\t<$reqProp/>\n) and next if $reqProp eq "resourcetype"; | ||
| 239 | |||
| 240 | if ($reqProp eq "creationdate") { | ||
| 241 | $ret .= qq(\t<$reqProp>) . HTTP::DAVServer::dateEpoch( $stat->mtime ) . qq(</$reqProp>\n); | ||
| 242 | next; | ||
| 243 | } | ||
| 244 | |||
| 245 | if ($reqProp eq "displayname") { | ||
| 246 | $path =~ /([^\/]+\/)$/; | ||
| 247 | $ret .= qq(\t<$reqProp>$1</$reqProp>\n); | ||
| 248 | next; | ||
| 249 | } | ||
| 250 | |||
| 251 | if ($reqProp eq "getcontentlength") { | ||
| 252 | $ret .= qq(\t<$reqProp>) . $stat->size . qq(</$reqProp>\n); | ||
| 253 | next; | ||
| 254 | } | ||
| 255 | |||
| 256 | warn "Collection property $reqProp not known\n" if $HTTP::DAVServer::WARN; | ||
| 257 | |||
| 258 | } | ||
| 259 | |||
| 260 | $ret .= qq(</prop>) | ||
| 261 | . qq(<status>HTTP/1.1 200 OK</status>) | ||
| 262 | . qq(</propstat>); | ||
| 263 | |||
| 264 | return $ret; | ||
| 265 | |||
| 266 | } | ||
| 267 | |||
| 268 | |||
| 269 | =head1 SUPPORT | ||
| 270 | |||
| 271 | For technical support please email to jlawrenc@cpan.org ... | ||
| 272 | for faster service please include "HTTP::DAVServer" and "help" in your subject line. | ||
| 273 | |||
| 274 | =head1 AUTHOR | ||
| 275 | |||
| 276 | Jay J. Lawrence - jlawrenc@cpan.org | ||
| 277 | Infonium Inc., Canada | ||
| 278 | http://www.infonium.ca/ | ||
| 279 | |||
| 280 | =head1 COPYRIGHT | ||
| 281 | |||
| 282 | Copyright (c) 2003 Jay J. Lawrence, Infonium Inc. All rights reserved. | ||
| 283 | This program is free software; you can redistribute | ||
| 284 | it and/or modify it under the same terms as Perl itself. | ||
| 285 | |||
| 286 | The full text of the license can be found in the | ||
| 287 | LICENSE file included with this module. | ||
| 288 | |||
| 289 | =head1 ACKNOWLEDGEMENTS | ||
| 290 | |||
| 291 | Thank you to the authors of my prequisite modules. With out your help this code | ||
| 292 | would be much more difficult to write! | ||
| 293 | |||
| 294 | XML::Simple - Grant McLean | ||
| 295 | XML::SAX - Matt Sergeant | ||
| 296 | DateTime - Dave Rolsky | ||
| 297 | |||
| 298 | Also the authors of litmus, a very helpful tool indeed! | ||
| 299 | |||
| 300 | =head1 SEE ALSO | ||
| 301 | |||
| 302 | HTTP::DAV, HTTP::Webdav, http://www.webdav.org/, RFC 2518 | ||
| 303 | |||
| 304 | =cut | ||
| 305 | |||
| 306 | 1; | ||
| 307 |