/usr/share/perl5/HTTP/DAV/Resource.pm is in libhttp-dav-perl 0.48-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 | package HTTP::DAV::Resource;
use strict;
use vars qw($VERSION);
$VERSION = '0.31';
use HTTP::DAV;
use HTTP::DAV::Utils;
use HTTP::DAV::Lock;
use HTTP::Date qw(str2time);
use HTTP::DAV::ResourceList;
use Scalar::Util ();
use URI::Escape;
###########################################################################
# Construct a new object and initialize it
sub new {
my $class = shift;
my $self = bless {}, ref($class) || $class;
$self->_init(@_);
return $self;
}
sub _init {
my ($self, @p) = @_;
####
# This is the order of the arguments unless used as
# named parameters
my ($uri, $lockedresourcelist, $comms, $client)
= HTTP::DAV::Utils::rearrange(
[ 'URI', 'LOCKEDRESOURCELIST', 'COMMS', 'CLIENT' ], @p);
# Optionally add a scheme.
$uri =~ s/^\s*(.*?)\s*$/$1/g; # Remove leading and trailing slashes
$uri = "http://$uri" if ($uri ne "" && $uri !~ /^https?:\/\//);
$self->{"_uri"} = $uri || "";
$self->{"_lockedresourcelist"} = $lockedresourcelist || "";
$self->{"_comms"} = $comms || "";
$self->{"_dav_client"} = $client || "";
# Avoid circular references between
# - HTTP::DAV -> {_workingresource} and
# - HTTP::DAV::Resource -> {_dav_client}
Scalar::Util::weaken($self->{"_dav_client"});
####
# Set the _uri
$self->{_uri} = HTTP::DAV::Utils::make_uri($self->{_uri});
die "HTTP URL required when creating a Resource object\n"
if (!$self->{_uri}->scheme);
####
# Check that the required objects exist
die("Comms object required when creating a Resource object")
unless (defined $self->{_comms}
&& $self->{_comms} =~ /HTTP::DAV::Comms/);
die("Locked ResourceList object required when creating a Resource object")
unless (defined $self->{_lockedresourcelist}
&& $self->{_lockedresourcelist} =~ /HTTP::DAV::ResourceList/);
die("DAV Client required when creating a Resource object")
unless (defined $self->{_dav_client}
&& $self->{_dav_client} =~ /HTTP::DAV/);
}
###########################################################################
# GET/SET
#sub set_lockpolicy { cluck("Can't reset the lockpolicy on a Resource"); 0; }
sub set_parent_resourcelist {
my ($self, $resource_list) = @_;
# Avoid circular references between the
# parent resource list and this child resource
Scalar::Util::weaken($self->{_parent_resourcelist} = $resource_list);
}
sub set_property { $_[0]->{_properties}{ $_[1] } = $_[2]; }
sub set_uri { $_[0]->{_uri} = HTTP::DAV::Utils::make_uri($_[1]); }
# PRIVATE SUBROUTINES
sub _set_content { $_[0]->{_content} = $_[1]; }
sub _set_options { $_[0]->{_options} = $_[1]; }
sub _set_compliance { $_[0]->{_compliance} = $_[1]; }
sub set_locks {
my ($self, @locks) = @_;
# Unset any existing locks because we're about to reset them
# But keep their name temporarily because some of them
# may be ours.
my @old_lock_tokens = keys %{ $self->{_locks} } || ();
#if (@locks && defined $self->{_locks}) {
if (defined $self->{_locks}) {
delete $self->{_locks};
}
foreach my $lock (@locks) {
my $token = $lock->get_locktoken();
#print "Adding $token\n";
# If it exists, we'll set it to owned and reapply
# it (it may have changed since we saw it last.
# Like it might have timed out?
if (grep($token, @old_lock_tokens)) {
$lock->set_owned(1);
}
$self->{_locks}{$token} = $lock;
}
#print "Locks: " . join(' ',keys %{$self->{_locks}} )."\n";
}
sub is_option {
my ($self, $option) = @_;
$self->options if (!defined $self->{_options});
return ($self->{_options} =~ /\b$option\b/i) ? 1 : 0;
}
sub is_dav_compliant {
my $resp = $_[0]->options if (!defined $_[0]->{_options});
$_[0]->{_compliance};
}
sub get_options { $_[0]->{_options}; }
sub get_content { $_[0]->{_content}; }
sub get_content_ref { \$_[0]->{_content}; }
sub get_username {
my ($self) = @_;
my $ra = $self->{_comms}->get_user_agent();
my @userpass = $ra->get_basic_credentials(undef, $self->get_uri());
return $userpass[0];
}
#sub get_lockpolicy { $_[0]->{_lockpolicy}; }
sub get_client { $_[0]->{_dav_client}; }
sub get_resourcelist { $_[0]->{_resource_list}; }
sub get_lockedresourcelist { $_[0]->{_lockedresourcelist}; }
sub get_comms { $_[0]->{_comms}; }
sub get_property { $_[0]->{_properties}{ $_[1] } || ""; }
sub get_uri { $_[0]->{_uri}; }
sub get_uristring { $_[0]->{_uri}->as_string; }
sub get_parent_resourcelist { $_[0]->{_parent_resourcelist}; }
# $self->get_locks( -owned => [0|1] );
# '1' = return any locks owned be me
# '0' = return any locks NOT owned be me
# no value = return all locks
#
sub get_locks {
my ($self, @p) = @_;
my ($owned) = HTTP::DAV::Utils::rearrange(['OWNED'], @p);
$owned = "" unless defined $owned;
#print "owned=$owned,\@p=\"@p\"\n";
my @return_locks = ();
foreach my $token (sort keys %{ $self->{_locks} }) {
my $lock = $self->{_locks}{$token};
if ($owned eq "1" && $lock->is_owned) {
push(@return_locks, $lock);
}
elsif ($owned eq "0" && !$lock->is_owned) {
push(@return_locks, $lock);
}
elsif ($owned eq "") {
push(@return_locks, $lock);
}
}
return @return_locks;
}
sub get_lock {
my ($self, $token) = @_;
return $self->{_locks}{$token} if ($token);
}
# Just pass through to get_locks all of our parameters.
# Then count how many we get back. >1 lock returns 1.
sub is_locked {
my ($self, @p) = @_;
return scalar $self->get_locks(@p);
}
sub is_collection {
my $type = $_[0]->get_property("resourcetype");
return (defined $type && $type =~ /collection/) ? 1 : 0;
}
sub _unset_properties { $_[0]->{_properties} = (); }
sub _unset_lock { delete $_[0]->{_locks}{ $_[1] } if $_[1]; }
sub _unset_locks { $_[0]->{_locks} = (); }
sub _unset_my_locks {
my ($self) = @_;
my @locks = $self->get_locks(-owned => 1);
foreach my $lock (@locks) {
$self->_unset_lock($lock->get_locktoken);
}
$self->get_lockedresourcelist->remove_resource($self);
}
###########################################################################
sub lock {
my ($self, @p) = @_;
my $lock = HTTP::DAV::Lock->new(-owned => 1);
#my $existing_lock = $self->get_lockedresourcelist->get_member($self->uri);
my ($owner, $depth, $timeout, $scope, $type, @other)
= HTTP::DAV::Utils::rearrange(
[ 'OWNER', 'DEPTH', 'TIMEOUT', 'SCOPE', 'TYPE' ], @p);
####
# Set the defaults
# 'owner' default is DAV.pm/v0.1 (ProcessId)
$owner ||= "DAV.pm/v$HTTP::DAV::VERSION ($$)";
# Sanity check. If it ain't 0, then make it infinity.
$depth = (defined $depth && $depth eq "0") ? 0 : "infinity";
# 'scope' default is exclusive
$scope ||= "exclusive";
# 'type' default is write
$type ||= "write";
####
# Setup the headers for the lock request
my $headers = HTTP::DAV::Headers->new;
$headers->header("Content-type", "text/xml; charset=\"utf-8\"");
$headers->header("Depth", $depth);
my $timeoutval = $lock->timeout($timeout);
$headers->header("Timeout", $timeoutval) if ($timeoutval);
# Add any If headers required
#$self->_setup_if_headers($headers);
####
# Setup the XML content for the lock request
my $xml_request = HTTP::DAV::Lock->make_lock_xml(
-owner => $owner,
-timeout => $timeout,
-scope => $scope,
-type => $type,
);
#print "$xml_request\n";
####
# Put the lock request to the remote server
my $resp = $self->{_comms}->do_http_request(
-method => "LOCK",
-url => $self->{_uri},
-headers => $headers,
-content => $xml_request,
);
###
# Handle the lock response
# Normal spec scenario
if ($self->content_type_is_xml($resp)) {
# use XML::DOM to parse the result.
my $parser = new XML::DOM::Parser;
my $doc = $parser->parse($resp->content);
###
# Multistatus response. Generally indicates a failure
if ($resp->code == 207) {
# We're only interested in the error codes that come
# out of the multistatus $resp.
eval { $self->_XML_parse_multistatus($doc, $resp) };
print "XML error: " . $@ if $@;
}
###
# Lock succeeded
# 1. I assume from RFC2518 that if it successsfully locks
# then we will only get back the lockdiscover element
# for MY lock. If not, I will warn the user.
#
# 2. I am fairly sure that my client should only ever be able to
# take out one lock on a resource. As such this program assumes
# that a resource can only have one lock held against it (locks
# owned by other people do not get stored here).
#
elsif ($resp->is_success) {
my $node_prop
= HTTP::DAV::Utils::get_only_element($doc, "D:prop");
my $lock_discovery
= HTTP::DAV::Utils::get_only_element($node_prop,
"D:lockdiscovery");
my @locks
= HTTP::DAV::Lock->XML_lockdiscovery_parse($lock_discovery);
# Degenerate case for bad server mydocsonline.
# Doesn't return a proper lockdiscovery.
# Just use the Lock-Token in the header instead.
if (!@locks && $resp->header('Lock-Token')) {
print
"Using degenerate case of getting Lock-Token from Header.\n"
if $HTTP::DAV::DEBUG > 2;
$locks[0] = HTTP::DAV::Lock->new(-owned => 1);
$locks[0]->set_locktoken($resp->header('Lock-Token'));
}
if ($#locks > 0) {
warn(
"Serious protocol error, expected 1 lock back from request "
. "but got more than one. Don't know which one is mine"
);
}
else {
$self->set_locks(@locks);
foreach my $lock (@locks) { $lock->set_owned(1); }
$self->{_lockedresourcelist}->add_resource($self);
#print $self->{_lockedresourcelist}->as_string;
}
}
# Discard of XML doc safely.
$doc->dispose;
}
return $resp;
}
###########################################################################
sub unlock {
my ($self, @p) = @_;
my ($opaquelocktoken) = HTTP::DAV::Utils::rearrange(['TOKEN'], @p);
my $resp;
my $uri = $self->get_uri();
# If you passed no lock token then I'll try
# and unlock with any tokens I own.
if (!$opaquelocktoken) {
my @locks = $self->get_locks(-owned => 1);
my $num_locks = $#locks + 1;
if ($num_locks == 0) {
# Just use a dummy token. They're unique anyway.
#$opaquelocktoken = "opaquelocktoken:dummytoken-82d32fa22932";
$opaquelocktoken = "";
}
if ($num_locks == 1) {
$opaquelocktoken = $locks[0]->get_locktoken;
}
else {
foreach my $lock (@locks) {
$resp = $self->unlock(-token => $lock->get_locktoken);
return $resp if $resp->is_error();
}
}
}
my $headers = HTTP::DAV::Headers->new;
#$headers->header("Lock-Token", "<${opaquelocktoken}>") if $opaquelocktoken;
$headers->header("Lock-Token", "<${opaquelocktoken}>");
if ($opaquelocktoken) {
warn "UNLOCKING with '$opaquelocktoken'\n" if $HTTP::DAV::DEBUG > 2;
# Put the unlock request to the remote server
$resp = $self->{_comms}->do_http_request(
-method => "UNLOCK",
-url => $self->get_uri,
-headers => $headers,
#-content => no content required
);
}
else {
#print "START\n";
$resp = HTTP::Response->new(500, "Client error. No lock held.");
$resp = HTTP::DAV::Response->clone_http_resp($resp);
#print $resp->as_string();
#print "END\n";
}
if ($resp->is_success) {
$self->_unset_lock($opaquelocktoken);
}
return $resp;
}
###########################################################################
sub forcefully_unlock_all {
my ($self) = @_;
my $resp;
my $discovery_resp = $self->lockdiscovery;
if ($discovery_resp->is_success) {
my @locks = $self->get_locks();
foreach my $lock (@locks) {
my $token = $lock->get_locktoken;
$resp = $self->unlock(-token => $token) if $token;
return $resp if $resp->is_error;
}
}
# In the event that there were no locks to steal,
# then just send a dud request out and let the
# server fail it.
if (!$resp) {
$resp = $self->unlock();
}
return $resp;
}
###########################################################################
sub steal_lock {
my ($self) = @_;
$self->forcefully_unlock_all;
return $self->lock;
}
###########################################################################
sub lockdiscovery {
my ($self, @p) = @_;
my ($depth, @other) = HTTP::DAV::Utils::rearrange(['DEPTH'], @p);
return $self->propfind(
-depth => $depth,
-text => "<D:prop><D:lockdiscovery/></D:prop>"
);
}
###########################################################################
sub propfind {
my ($self, @p) = @_;
my ($depth, $text, @other)
= HTTP::DAV::Utils::rearrange([ 'DEPTH', 'TEXT' ], @p);
# 'depth' default is 1
$depth = 1 unless (defined $depth && $depth ne "");
####
# Setup the headers for the request
my $headers = new HTTP::Headers;
$headers->header("Content-type", "text/xml; charset=\"utf-8\"");
$headers->header("Depth", $depth);
# Create a new XML document
# <D:propfind xmlns:D="DAV:">
# <D:allprop/>
# </D:propfind>
my $xml_request = qq{<?xml version="1.0" encoding="utf-8"?>};
$xml_request .= '<D:propfind xmlns:D="DAV:">';
$xml_request .= $text || "<D:allprop/>";
$xml_request .= "</D:propfind>";
####
# Put the propfind request to the remote server
my $resp = $self->{_comms}->do_http_request(
-method => "PROPFIND",
-url => $self->{_uri},
-headers => $headers,
-content => $xml_request,
);
# Reset the resource list, in case of intermediate errors,
# to keep object state consistent
$self->{_resource_list} = undef;
if (! $self->content_type_is_xml($resp)) {
$resp->add_status_line(
"HTTP/1.1 422 Unprocessable Entity, no XML body.",
"", $self->{_uri}, $self->{_uri}
);
return $resp;
}
# use XML::DOM to parse the result.
my $parser = XML::DOM::Parser->new();
my $xml_resp = $resp->content;
my $doc;
if (! $xml_resp) {
$resp->add_status_line(
"HTTP/1.1 422 Unprocessable Entity, no XML body.",
"", $self->{_uri}, $self->{_uri}
);
return $resp;
}
eval {
$doc = $parser->parse($xml_resp);
} or do {
warn "Unparsable XML received from server (" . length($xml_resp) . " bytes)\n";
warn "ERROR: $@\n";
return $resp;
};
# Setup a ResourceList in which to pump all of the collection
my $resource_list;
eval {
$resource_list = $self->_XML_parse_multistatus($doc, $resp)
} or do {
warn "Error parsing PROPFIND response XML: $@\n";
};
if ($resource_list && $resource_list->count_resources()) {
$self->{_resource_list} = $resource_list;
}
$doc->dispose;
return $resp;
}
###########################################################################
# get/GET the body contents
sub get {
my ($self, @p) = @_;
my ($save_to, $progress_callback, $chunk)
= HTTP::DAV::Utils::rearrange(
[ 'SAVE_TO', 'PROGRESS_CALLBACK', 'CHUNK' ], @p);
#$save_to = URI::Escape::uri_unescape($save_to);
my $resp = $self->{_comms}->do_http_request(
-method => "GET",
-uri => $self->get_uri,
-save_to => $save_to,
-callback => $progress_callback,
-chunk => $chunk
);
# What to do with all of the headers in the response. Put
# them into this object? If so, which ones?
if ($resp->is_success) {
$self->_set_content($resp->content);
}
return $resp;
}
sub GET { shift->get(@_); }
###########################################################################
# put/PUT the body contents
sub put {
my ($self, $content, $custom_headers) = @_;
my $resp;
# Setup the If: header if it is locked
my $headers = HTTP::DAV::Headers->new();
$self->_setup_if_headers($headers);
$self->_setup_custom_headers($custom_headers);
if (!defined $content) {
$content = $self->get_content();
# if ( ! $content ) {
# #$resp = HTTP::DAV::Response->new;
# #$resp->code("400"); ??
# return $resp;
# }
}
$resp = $self->{_comms}->do_http_request(
-method => "PUT",
-uri => $self->get_uri,
-headers => $headers,
-content => $content,
);
#my $unlockresp = $self->unlock;
# What to do with all of the headers in the response. Put
# them into this object? If so, which ones?
# $self->_set_content( $resp->content );
return $resp;
}
sub PUT { my $self = shift; $self->put(@_); }
###########################################################################
# Make a collection
sub mkcol {
my ($self) = @_;
# Setup the If: header if it is locked
my $headers = HTTP::DAV::Headers->new();
$self->_setup_if_headers($headers);
my $resp = $self->{_comms}->do_http_request(
-method => "MKCOL",
-uri => $self->get_uri,
-headers => $headers,
);
# Handle a multistatus response
if ($self->content_type_is_xml($resp) && # XML body
$resp->is_multistatus() # Multistatus
)
{
# use XML::DOM to parse the result.
my $parser = new XML::DOM::Parser;
my $doc = $parser->parse($resp->content);
# We're only interested in the error codes that come out of $resp.
eval { $self->_XML_parse_multistatus($doc, $resp) };
warn "XML error: " . $@ if $@;
$doc->dispose;
}
return $resp;
}
###########################################################################
# Get OPTIONS available on a resource/collection
sub options {
my ($self, $entire_server) = @_;
my $uri = $self->get_uri;
# Doesn't work properly. Sets it as /*
# How do we get LWP to send through just
# OPTIONS * HTTP/1.1
# ??
#$uri->path("*") if $entire_server;
my $resp = $self->{_comms}->do_http_request(
-method => "OPTIONS",
-uri => $uri,
);
if ($resp->header("Allow")) {
#print "Allow: ". $resp->header("Allow") . "\n";
$self->_set_options($resp->header("Allow"));
}
# Get the "DAV" header and look for
# either "DAV:1" or "DAV:1,2"
my $compliance = 0;
if ($resp->header("DAV")) {
$compliance = $resp->header("DAV");
if ($compliance =~ /^\s*1\s*,\s*2/) {
$compliance = 2;
}
elsif ($compliance =~ /^\s*1/) {
$compliance = 1;
}
}
$self->_set_compliance($compliance);
return $resp;
}
sub OPTIONS { my $self = shift; $self->options(@_); }
###########################################################################
# Move or copy a resource/collection
sub move { return shift->_move_copy("MOVE", @_); }
sub copy { return shift->_move_copy("COPY", @_); }
sub _move_copy {
my ($self, $method, @p) = @_;
my ($dest_resource, $overwrite, $depth, $text, @other)
= HTTP::DAV::Utils::rearrange(
[ 'DEST', 'OVERWRITE', 'DEPTH', 'TEXT' ], @p);
# Sanity check. If depth ain't 0, then make it infinity.
# Only infinity allowed for move.
# 0 or infinity allowed for copy.
if ($method eq "MOVE") {
$depth = "infinity";
}
else {
$depth = (defined $depth && $depth eq "0") ? 0 : "infinity";
}
# Sanity check. If overwrite ain't F or 0, then make it T
$overwrite = "F" if (defined $overwrite && $overwrite eq "0");
$overwrite = (defined $overwrite && $overwrite eq "F") ? "F" : "T";
####
# Setup the headers for the lock request
my $headers = new HTTP::Headers;
$headers->header("Depth", $depth);
$headers->header("Overwrite", $overwrite);
# Destination Resource must have a URL
my $dest_url = $dest_resource->get_uri;
my $server_type
= $self->{_comms}->get_server_type($dest_url->host_port());
my $dest_str = $dest_url->as_string;
# Apache, Bad Gateway workaround
if ($server_type =~ /Apache/i && $server_type =~ /DAV\//i) {
#my $dest_str = "http://" . $dest_url->host_port . $dest_url->path;
$dest_str
= $dest_url->scheme . "://"
. $dest_url->host_port
. $dest_url->path;
if ($HTTP::DAV::DEBUG) {
warn
"*** INSTIGATING mod_dav WORKAROUND FOR DESTINATION HEADER BUG IN Resource::_move_copy\n";
warn "*** Server type of "
. $dest_url->host_port()
. ": $server_type\n";
warn "*** Adding port number :"
. $dest_url->port
. " to given url: $dest_url\n";
}
}
# Apache2 mod_dav, Permenantly Moved workaround
# If the src is a collection, then the dest must have a trailing
# slash or mod_dav2 gives a strange "bad url" error in a
# "Moved Permenantly" response.
if ($self->is_collection || $self->get_uri =~ /\/$/) {
$dest_str =~ s#/*$#/#;
}
$headers->header("Destination", $dest_str);
# Join both the If headers together.
$self->_setup_if_headers($headers, 1);
my $if1 = $headers->header('If');
$if1 ||= "";
warn "COPY/MOVE If header for source: $if1\n" if $HTTP::DAV::DEBUG > 2;
$dest_resource->_setup_if_headers($headers, 1);
my $if2 = $headers->header('If');
$if2 ||= "";
warn "COPY/MOVE If header for dest : $if2\n" if $HTTP::DAV::DEBUG > 2;
$if1 = "$if1 $if2" if ($if1 || $if2);
$headers->header('If', $if1) if $if1;
# See from RFC 12.12.
# Valid values for '$text':
#
# <D:keepalive>*</D:keepalive>
# or
# <D:keepalive>
# <D:href>...url1...</D:href>
# <D:href>...url2...</D:href>
# </D:keepalive>
# or
# <D:omit/>
#
my $xml_request;
if ($text) {
$headers->header("Content-type", "text/xml; charset=\"utf-8\"");
$xml_request = qq{<?xml version="1.0" encoding="utf-8"?>};
$xml_request .= '<D:propertybehavior xmlns:D="DAV:">';
$xml_request .= $text;
$xml_request .= "</D:propertybehavior>";
}
####
# Put the copy request to the remote server
my $resp = $self->{_comms}->do_http_request(
-method => $method,
-url => $self->{_uri},
-headers => $headers,
-content => $xml_request,
);
if ($resp->is_multistatus()) {
my $parser = new XML::DOM::Parser;
my $doc = $parser->parse($resp->content);
eval { $self->_XML_parse_multistatus($doc, $resp) };
warn "XML error: " . $@ if $@;
$doc->dispose;
}
# MOVE EATS SOURCE LOCKS
if ($method eq "MOVE") {
$self->_unset_my_locks();
# Well... I'm baffled.
# I previousy had this commented out because my
# undestanding was that the dest lock stayed in tact.
# But mod_dav seems to remove it after a move. So,
# I'm going to fall in line, but if another server
# implements this differently, then I'm going to have
# to pipe up and get them to sort out their differences :)
#$dest_resource->_unset_my_locks();
}
return $resp;
}
###########################################################################
# proppatch a resource/collection
sub proppatch {
my ($self, @p) = @_;
my ($namespace, $propname, $propvalue, $action, $use_nsabbr)
= HTTP::DAV::Utils::rearrange(
[ 'NAMESPACE', 'PROPNAME', 'PROPVALUE', 'ACTION', 'NSABBR' ], @p);
$use_nsabbr ||= 'R';
# Sanity check. If action ain't 'remove' then set it to 'set';
$action = (defined $action && $action eq "remove") ? "remove" : "set";
####
# Setup the headers for the lock request
my $headers = new HTTP::Headers;
$headers->header("Content-type", "text/xml; charset=\"utf-8\"");
$self->_setup_if_headers($headers);
my $xml_request = qq{<?xml version="1.0" encoding="utf-8"?>};
# $xml_request .= "<D:propertyupdate xmlns:D=\"DAV:\">";
# $xml_request .= "<D:$action>";
$xml_request .= "<D:propertyupdate xmlns:D=\"DAV:\"";
$namespace ||= "";
my $nsabbr = 'D';
if ($namespace =~ /dav/i || $namespace eq "") {
# $xml_request .= "<D:prop>";
# if ($action eq "set" ) {
# $xml_request .= "<D:$propname>$propvalue</D:$propname>";
# } else {
# $xml_request .= "<D:$propname/>";
# }
$xml_request .= ">";
}
else {
$nsabbr = $use_nsabbr;
$xml_request .= " xmlns:$nsabbr=\"$namespace\">";
}
# else {
# $xml_request .= "<D:prop xmlns:R=\"".$namespace."\">";
# if ($action eq "set" ) {
# $xml_request .= "<R:$propname>$propvalue</R:$propname>";
# } else {
# $xml_request .= "<R:$propname/>";
# }
$xml_request .= "<D:$action>";
$xml_request .= "<D:prop>";
if ($action eq "set") {
$xml_request .= "<$nsabbr:$propname>$propvalue</$nsabbr:$propname>";
}
else {
$xml_request .= "<$nsabbr:$propname/>";
}
$xml_request .= "</D:prop>";
$xml_request .= "</D:$action>";
$xml_request .= "</D:propertyupdate>";
####
# Put the proppatch request to the remote server
my $resp = $self->{_comms}->do_http_request(
-method => "PROPPATCH",
-url => $self->{_uri},
-headers => $headers,
-content => $xml_request,
);
if ($resp->is_multistatus) {
my $parser = new XML::DOM::Parser;
my $doc = $parser->parse($resp->content);
eval { $self->_XML_parse_multistatus($doc, $resp) };
warn "XML error: " . $@ if $@;
$doc->dispose;
}
return $resp;
}
###########################################################################
# Delete a resource/collection
sub delete {
my ($self) = @_;
# Setup the If: header if it is locked
my $headers = HTTP::DAV::Headers->new();
$self->_setup_if_headers($headers);
# Setup the Depth for the delete request
# The only valid depth is infinity.
#$headers->header("Depth", "infinity");
my $resp = $self->{_comms}->do_http_request(
-method => "DELETE",
-uri => $self->get_uri,
-headers => $headers,
);
# Handle a multistatus response
if ($self->content_type_is_xml($resp) && # XML body
$resp->is_multistatus() # Multistatus
) {
# use XML::DOM to parse the result.
my $parser = new XML::DOM::Parser;
my $doc = $parser->parse($resp->content);
# We're only interested in the error codes that come out of $resp.
eval { $self->_XML_parse_multistatus($doc, $resp) };
warn "XML error: " . $@ if $@;
$doc->dispose;
}
if ($resp->is_success) {
$self->_unset_my_locks();
}
return $resp;
}
sub content_type_is_xml {
my ($self, $resp) = @_;
return unless $resp;
my $type = $resp->content_type;
return unless $type;
if ($type =~ m{(?:application|text)/xml}) {
return 1;
}
return;
}
###########################################################################
###########################################################################
# parses a <D:multistatus> element.
# This is the root level element for a
# PROPFIND body or a failed DELETE body.
# For example. The following is the result of a DELETE operation
# with a locked progeny (child).
#
# >> DELETE /test/dir/newdir/ HTTP/1.1
# << HTTP/1.1 207 Multistatus
# <?xml version="1.0" encoding="utf-8"?>
# <D:multistatus xmlns:D="DAV:">
# <D:response>
# <D:href>/test/dir/newdir/locker/</D:href>
# <D:status>HTTP/1.1 423 Locked</D:status>
# <D:responsedescription>Twas locked baby</D:responsedescription>
# </D:response>
# <D:response>
# <D:href>/test/dir/newdir/</D:href>
# <D:propstat>
# <D:prop><D:lockdiscovery/></D:prop>
# <D:status>HTTP/1.1 424 Failed Dependency</D:status>
# <D:responsedescription>Locks here somewhere</D:status>
# </D:propstat>
# <D:responsedescription>Can't delete him. Lock here</D:responsedescription>
# </D:response>
# <D:responsedescription>Failed delete</D:responsedescription>
# </D:multistatus>
#
sub _XML_parse_multistatus {
my ($self, $doc, $resp) = @_;
my $resource_list = HTTP::DAV::ResourceList->new;
# <!ELEMENT multistatus (response+, responsedescription?) >
# Parse I II III
###
# Parse I
my $node_multistatus
= HTTP::DAV::Utils::get_only_element($doc, "D:multistatus");
###
# Parse III
# Get the overarching responsedescription for the
# multistatus and set it into the DAV:Response object.
my $node_rd = HTTP::DAV::Utils::get_only_element($node_multistatus,
"D:responsedescription");
if ($node_rd) {
my $rd = $node_rd->getFirstChild->getNodeValue();
$resp->set_responsedescription($rd) if $rd;
}
###
# Parse II
# Get all the responses in the multistatus element
# <!ELEMENT multistatus (response+,responsedescription?) >
my @nodes_response
= HTTP::DAV::Utils::get_elements_by_tag_name($node_multistatus,
"D:response");
# Process each response object
#<!ELEMENT response (href, ((href*, status)|(propstat+)), responsedescription?) >
# Parse 1 2 2a 3 4 5
###
# Parse 1.
for my $node_response (@nodes_response) {
###
# Parse 2 and 2a (one or more hrefs)
my @nodes_href
= HTTP::DAV::Utils::get_elements_by_tag_name($node_response,
"D:href");
# Get href <!ELEMENT href (#PCDATA) >
my ($href, $href_a, $resource);
foreach my $node_href (@nodes_href) {
$href = $node_href->getFirstChild->getNodeValue();
# The href may be relative. If so make it absolute.
# With the uri data "/mydir/myfile.txt"
# And the uri of "this" object, "http://site/dir",
# return "http://site/mydir/myfile.txt"
# See the rules of URI.pm
my $href_uri = HTTP::DAV::Utils::make_uri($href);
my $res_url = $href_uri->abs($self->get_uri);
# Just store the first one for later use
$href_a = $res_url unless defined $href_a;
# Create a new Resource to put into the list
# Remove trailing slashes before comparing.
#warn "Am about to compare $res_url and ". $self->get_uri . "\n" ;
if (HTTP::DAV::Utils::compare_uris($res_url, $self->get_uri)) {
$resource = $self;
#warn " Exists. $resource\n";
}
else {
$resource = $self->get_client->new_resource(-uri => $res_url);
$resource_list->add_resource($resource);
#warn " New. $resource\n";
}
}
###
# Parse 3 and 5
# Get the values out of each Response
# <!ELEMENT status (#PCDATA) >
# <!ELEMENT responsedescription (#PCDATA) >
my ($response_status, $response_rd)
= $self->_XML_parse_status($node_response);
if ($response_status) {
$resp->add_status_line(
$response_status,
$response_rd,
"$href_a:response:$node_response",
$href_a
);
}
###
# Parse 4.
# Get the propstat+ list to be processed below
# Process each propstat object within this response
#
# <!ELEMENT propstat (prop, status, responsedescription?) >
# Parse a b c d
###
# Parse a
my @nodes_propstat
= HTTP::DAV::Utils::get_elements_by_tag_name($node_response,
"D:propstat");
# Unset any old properties
$resource->_unset_properties();
foreach my $node_propstat (@nodes_propstat) {
###
# Parse b
my $node_prop = HTTP::DAV::Utils::get_only_element($node_propstat,
"D:prop");
my $prop_hashref
= $resource->_XML_parse_and_store_props($node_prop);
###
# Parse c and d
my ($propstat_status, $propstat_rd)
= $self->_XML_parse_status($node_propstat);
# If there is no rd for this propstat, then use the
# enclosing rd from the actual response.
$propstat_rd = $response_rd unless $propstat_rd;
if ($propstat_status) {
$resp->add_status_line(
$propstat_status,
$propstat_rd,
"$href_a:propstat:$node_propstat",
$href_a
);
}
} # foreach propstat
} # foreach response
#warn "\nEND MULTI:". $self->as_string . $resource_list->as_string;
return $resource_list;
}
###
# This routine takes an XML node and:
# Extracts the D:status and D:responsedescription elements.
# If either of these exists, sets messages into the passed HTTP::DAV::Response object.
# The handle should be unique.
sub _XML_parse_status {
my ($self, $node) = @_;
# <!ELEMENT status (#PCDATA) >
# <!ELEMENT responsedescription (#PCDATA) >
my $node_status = HTTP::DAV::Utils::get_only_element($node, "D:status");
my $node_rd
= HTTP::DAV::Utils::get_only_element($node, "D:responsedescription");
my $status = $node_status->getFirstChild->getNodeValue()
if ($node_status);
my $rd = $node_rd->getFirstChild->getNodeValue() if ($node_rd);
return ($status, $rd);
}
###
# Pass in the XML::DOM prop node Element and it will
# parse and store all of the properties. These ones
# are specifically dealt with:
# creationdate
# getcontenttype
# getcontentlength
# displayname
# getetag
# getlastmodified
# resourcetype
# supportedlock
# lockdiscovery
# source
sub _XML_parse_and_store_props {
my ($self, $node) = @_;
my %return_props = ();
return unless ($node && $node->hasChildNodes());
# These elements will just get copied straight into our properties hash.
my @raw_copy = qw(
creationdate
getlastmodified
getetag
displayname
getcontentlength
getcontenttype
);
my $props = $node->getChildNodes;
my $n = $props->getLength;
for (my $i = 0; $i < $n; $i++) {
my $prop = $props->item($i);
# Ignore anything in the <prop> element which is
# not an Element. i.e. ignore comments, text, etc...
next if ($prop->getNodeTypeName() ne "ELEMENT_NODE");
my $prop_name = $prop->getNodeName();
$prop_name = HTTP::DAV::Utils::XML_remove_namespace($prop_name);
if (grep (/^$prop_name$/i, @raw_copy)) {
my $cdata = HTTP::DAV::Utils::get_only_cdata($prop);
$self->set_property($prop_name, $cdata);
}
elsif ($prop_name eq "lockdiscovery") {
my @locks = HTTP::DAV::Lock->XML_lockdiscovery_parse($prop);
$self->set_locks(@locks);
}
elsif ($prop_name eq "supportedlock") {
my $supportedlock_hashref
= HTTP::DAV::Lock::get_supportedlock_details($prop);
$self->set_property("supportedlocks", $supportedlock_hashref);
}
# Work in progress
# elsif ( $prop_name eq "source" ) {
# my $links = $self->_XML_parse_source_links( $prop );
# $self->set_property( "supportedlocks", $supportedlock_hashref );
# }
#resourcetype and others
else {
my $node_name = HTTP::DAV::Utils::XML_remove_namespace(
$prop->getNodeName());
my $str = "";
my @nodes = $prop->getChildNodes;
foreach my $node (@nodes) { $str .= $node->toString; }
$self->set_property($node_name, $str);
}
}
###
# Cleanup work
# set collection based on resourcetype
#my $getcontenttype = $self->get_property("getcontenttype");
#($getcontenttype && $getcontenttype =~ /directory/i ) ||
my $resourcetype = $self->get_property("resourcetype");
if (($resourcetype && $resourcetype =~ /collection/i)) {
$self->set_property("resourcetype", "collection");
my $uri = HTTP::DAV::Utils::make_trail_slash($self->get_uri);
$self->set_uri($uri);
}
# Clean up the date work.
my $creationdate = $self->get_property("creationdate");
if ($creationdate) {
my ($epochgmt) = HTTP::Date::str2time($creationdate);
$self->set_property("creationepoch", $epochgmt);
$self->set_property("creationdate", HTTP::Date::time2str($epochgmt));
}
my $getlastmodified = $self->get_property("getlastmodified");
if ($getlastmodified) {
my ($epochgmt) = HTTP::Date::str2time($getlastmodified);
$self->set_property("lastmodifiedepoch", $epochgmt);
$self->set_property("lastmodifieddate",
HTTP::Date::time2str($epochgmt));
}
}
sub _setup_custom_headers {
my ($self, $headers, $custom_headers) = @_;
if ($custom_headers && ref $custom_headers eq 'HASH') {
for my $hdr_name (keys %{$custom_headers}) {
my $hdr_value = $custom_headers->{$hdr_name};
warn "Setting custom header $hdr_name to '$hdr_value'\n";
$headers->header($hdr_name => $hdr_value);
}
}
return;
}
###########################################################################
# $self->_setup_if_headers( $headers_obj, [0|1] );
# used by at least PUT,MKCOL,DELETE,COPY/MOVE
sub _setup_if_headers {
my ($self, $headers, $tagged) = @_;
# Setup the If: header if it is locked
my $tokens = $self->{_lockedresourcelist}
->get_locktokens(-uri => $self->get_uri, -owned => 1);
$tagged = 1 unless defined $tagged;
my $if
= $self->{_lockedresourcelist}->tokens_to_if_header($tokens, $tagged);
$headers->header("If", $if) if $if;
warn "Setting if_header to \"If: $if\"\n" if $if && $HTTP::DAV::DEBUG;
}
###########################################################################
# Dump the objects contents as a string
sub as_string {
my ($self, $space, $depth) = @_;
$depth = 1 if (!defined $depth || $depth eq "");
$space = "" unless $space;
my $return;
# Do lock only
if ($depth == 2) {
$return = "${space}'Url': ";
$return .= $self->{_uri}->as_string . "\n";
foreach my $lock ($self->get_locks()) {
$return .= $lock->pretty_print("$space ");
}
return $return;
}
$return .= "${space}Resource\n";
$space .= " ";
$return .= "${space}'Url': ";
$return .= $self->{_uri}->as_string . "\n";
$return .= "${space}'Options': " . $self->{_options} . "\n"
if $self->{_options};
$return .= "${space}Properties\n";
foreach my $prop (sort keys %{ $self->{_properties} }) {
next if $prop =~ /_ls$/;
my $prop_val;
if ($prop eq "supportedlocks" && $depth > 1) {
use Data::Dumper;
$prop_val = $self->get_property($prop);
$prop_val = Data::Dumper->Dump([$prop_val], ['$prop_val']);
}
else {
$prop_val = $self->get_property($prop);
$prop_val =~ s/\n/\\n/g;
}
$return .= "${space} '$prop': $prop_val\n";
}
if (defined $self->{_content}) {
$return .= "${space}'Content':"
. substr($self->{_content}, 0, 50) . "...\n";
}
# DEEP PRINT
if ($depth) {
$return .= "${space}'_locks':\n";
foreach my $lock ($self->get_locks()) {
$return .= $lock->as_string("$space ");
}
$return .= $self->{_resource_list}->as_string($space)
if $self->{_resource_list};
}
# SHALLOW PRINT
else {
$return .= "${space}'_locks': ";
foreach my $lock ($self->get_locks()) {
my $locktoken = $lock->get_locktoken();
my $owned = ($lock->is_owned) ? "owned" : "not owned";
$return .= "${space} $locktoken ($owned)\n";
}
$return
.= "${space}'_resource_list': " . $self->{_resource_list} . "\n";
}
$return;
}
######################################################################
# Dump myself as an 'ls' might.
# Requires you to have already performed a propfind
sub build_ls {
my ($self, $parent_resource) = @_;
# Build some local variables that have been sanitised.
my $exec = $self->get_property("executable") || "?";
my $contenttype = $self->get_property("getcontenttype") || "";
my $supportedlocks = $self->get_property("supportedlocks") || ();
my $epoch = $self->get_property("lastmodifiedepoch") || 0;
my $size = $self->get_property("getcontentlength") || "";
my $is_coll = $self->is_collection() || "?";
my $is_lock = $self->is_locked() || "?";
# Construct a relative URI;
my $abs_uri = $self->get_uri();
my $rel_uri = $abs_uri->rel($parent_resource->get_uri());
$rel_uri = uri_unescape($rel_uri);
####
# Build up a long display name.
# 1.
my $lls = "URL: $abs_uri\n";
foreach my $prop (sort keys %{ $self->{_properties} }) {
next
if ($prop eq "lastmodifiedepoch"
|| $prop eq "creationepoch"
|| $prop eq "supportedlocks");
$lls .= "$prop: ";
if ($prop =~ /Content-Length/) {
$lls .= $self->get_property($prop) . " bytes";
}
else {
$lls .= $self->get_property($prop);
}
$lls .= "\n";
}
# 2. Build a supportedlocks string
if (defined $supportedlocks and ref($supportedlocks) eq "ARRAY") {
my @supported_locks = @{$supportedlocks};
$supportedlocks = "";
foreach my $lock_type_hash (@supported_locks) {
$supportedlocks .= $$lock_type_hash{'type'} . "/"
. $$lock_type_hash{'scope'} . " ";
}
}
else {
$supportedlocks = '"No locking supported"';
}
$lls .= "Locks supported: $supportedlocks\n";
# 3. Print all of the locks.
my @my_locks = $self->get_locks(-owned => 1);
my @not_my_locks = $self->get_locks(-owned => 0);
if ($is_lock) {
$lls .= "Locks: \n";
if (@my_locks) {
$lls .= " My locks:\n";
foreach my $lock (@my_locks) {
$lls .= $lock->pretty_print(" ") . "\n";
}
}
if (@not_my_locks) {
$lls .= " Others' locks:\n";
foreach my $lock (@not_my_locks) {
$lls .= $lock->pretty_print(" ") . "\n";
}
}
}
else {
$lls .= "Locks: Not locked\n";
}
######################################################################
####
# Build up a list of useful information
$self->set_property('rel_uri', $rel_uri);
my @props = ();
push(@props, "<exe>") if ($exec eq "T");
push(@props, "<dir>") if ($is_coll eq "1");
push(@props, "<locked>") if ($is_lock eq "1");
$self->set_property('short_props', join(',', @props));
# Build a (short) display date in either
# "Mmm dd yyyy" or "Mmm dd HH:MM" format.
my $display_date = "?";
if ($epoch > 1) {
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)
= localtime($epoch);
my %mons = (
0 => 'Jan', 1 => 'Feb', 2 => 'Mar', 3 => 'Apr',
4 => 'May', 5 => 'Jun', 6 => 'Jul', 7 => 'Aug',
8 => 'Sep', 9 => 'Oct', 10 => 'Nov', 11 => 'Dec'
);
$year += 1900;
my $month = $mons{$mon};
# If the last modified time is older than six months
# then display in "Mmm dd yyyy" format.
# else display in "Mmm dd HH:MM" format.
if (time - $epoch > (3600 * 24 * 30 * 6)) {
$self->set_property(
'display_date',
sprintf(
"%3s %0.2d %4d",
$month, $mday, $year
)
);
}
else {
$self->set_property(
'display_date',
sprintf(
"%3s %0.2d %0.2d:%0.2d",
$month, $mday, $hour, $min
)
);
}
}
$self->set_property('long_ls', $lls);
# Preset this, but it will be overwritten below
# if it is a collection
$self->set_property('short_ls', $lls);
# Build the short listing if it is a collection
if ($self->is_collection) {
my $short = "";
$short .= "Listing of " . $self->get_uri() . "\n";
my $child_resource_list = $self->get_resourcelist;
if (defined $child_resource_list) {
my @resources = $child_resource_list->get_resources;
foreach my $child_res (@resources) {
$child_res->build_ls($self);
}
# Get the maximum uri length for pretty printing.
my $max_uri_length = 0;
my $max_bytes_length = 0;
foreach my $r ($self, sort by_URI @resources) {
my $l;
$l = length($r->get_property('rel_uri'));
$max_uri_length = $l if $l > $max_uri_length;
$l = length($r->get_property('getcontentlength'));
$max_bytes_length = $l if $l > $max_bytes_length;
}
# Print the listing
foreach my $r ($self, sort by_URI @resources) {
$short .= sprintf(
" %${max_uri_length}s %${max_bytes_length}s %12s %s\n",
$r->get_property('rel_uri'),
$r->get_property('getcontentlength'),
$r->get_property('display_date'),
$r->get_property('short_props')
);
}
} # if defined resource_list
$self->set_property('short_ls', $short);
}
sub by_URI {
my $a_str = $a->get_uri;
my $b_str = $b->get_uri;
return $a_str cmp $b_str;
}
}
1;
__END__
=head1 NAME
HTTP::DAV::Resource - Represents and interfaces with WebDAV Resources
=head1 SYNOPSIS
Sample
=head1 DESCRIPTION
Description here
=head1 CONSTRUCTORS
=over 4
=item B<new>
Returns a new resource represented by the URI.
$r = HTTP::DAV::Resource->new(
-uri => $uri,
-LockedResourceList => $locks,
-Comms => $comms
-Client => $dav_client
);
On creation a Resource object needs 2 other objects passed in:
1. a C<ResourceList> Object. This list will be added to if you lock this Resource.
2. a C<Comms> Object. This object will be used for HTTP communication.
2. a C<HTTP::DAV> Object. This object is where all locks are stored
=back
=head1 METHODS
=over 4
=item B<get/GET>
Performs an HTTP GET and returns a DAV::Response object.
$response = $resource->get;
print $resource->get_content if ($response->is_success);
=item B<put/PUT>
Performs an HTTP PUT and returns a DAV::Response object.
$response = $resource->put( $string );
$string is be passed as the body.
e.g.
$response = $resource->put($string);
print $resource->get_content if ($response->is_success);
Will use a Lock header if this resource was previously locked.
=item B<copy>
Not implemented
=item B<move>
Not implemented
=item B<delete>
Performs an HTTP DELETE and returns a DAV::Response object.
$response = $resource->delete;
print "Delete successful" if ($response->is_success);
Will use a Lock header if this resource was previously locked.
=item B<options>
Performs an HTTP OPTIONS and returns a DAV::Response object.
$response = $resource->options;
print "Yay for PUT!" if $resource->is_option("PUT");
=item B<mkcol>
Performs a WebDAV MKCOL request and returns a DAV::Response object.
$response = $resource->mkcol;
print "MKCOL successful" if ($response->is_success);
Will use a Lock header if this resource was previously locked.
=item B<proppatch>
xxx
=item B<propfind>
Performs a WebDAV PROPFIND request and returns a DAV::Response object.
$response = $resource->propfind;
if ($response->is_success) {
print "PROPFIND successful\n";
print $resource->get_property("displayname") . "\n";
}
A successful PROPFIND fills the object with much data about the Resource.
Including:
displayname
...
TODO
=item B<lock>
Performs a WebDAV LOCK request and returns a DAV::Response object.
$resource->lock(
-owner => "Patrick Collins",
-depth => "infinity"
-scope => "exclusive",
-type => "write"
-timeout => TIMEOUT',
)
lock takes the following arguments.
B<owner> - Indicates who locked this resource
The default value is:
DAV.pm/v$DAV::VERSION ($$)
e.g. DAV.pm/v0.1 (123)
If you use a URL as the owner, the module will
automatically indicate to the server that is is a
URL (<D:href>http://...</D:href>)
B<depth> - Indicates the depth of the lock.
Legal values are 0 or infinity. (1 is not allowed).
The default value is infinity.
A lock value of 0 on a collection will lock just the collection but not it's members, whereas a lock value of infinity will lock the collection and all of it's members.
B<scope> - Indicates the scope of the lock.
Legal DAV values are "exclusive" or "shared".
The default value is exclusive.
See section 6.1 of RFC2518 for a description of shared vs. exclusive locks.
B<type> - Indicates the type of lock (read, write, etc)
The only legal DAV value currently is "write".
The default value is write.
B<timeout> - Indicates when the lock will timeout
The timeout value may be one of, an Absolute Date, a Time Offset from now, or the word "infinity".
The default value is "infinity".
The following are all valid timeout values:
Time Offset:
30s 30 seconds from now
10m ten minutes from now
1h one hour from now
1d tomorrow
3M in three months
10y in ten years time
Absolute Date:
timeout at the indicated time & date (UTC/GMT)
2000-02-31 00:40:33
timeout at the indicated date (UTC/GMT)
2000-02-31
You can use any of the Absolute Date formats specified in HTTP::Date (see perldoc HTTP::Date)
Note: the DAV server may choose to ignore your specified timeout.
=item B<unlock>
Performs a WebDAV UNLOCK request and returns a DAV::Response object.
$response = $resource->unlock()
$response = $resource->unlock( -force => 1 )
$response = $resource->unlock(
-token => "opaquelocktoken:1342-21423-2323" )
This method will automatically use the correct locktoken If: header if this resource was previously locked.
B<force> - Synonymous to calling $resource->forcefully_unlock_all.
=item B<forcefully_unlock_all>
Remove all locks from a resource and return the last DAV::Response object. This method take no arguments.
$response = $resource->forcefully_unlock_all;
This method will perform a lockdiscovery against the resource to determine all of the current locks. Then it will UNLOCK them one by one. unlock( -token => locktoken ).
This unlock process is achievable because DAV does not enforce any security over locks.
Note: this method returns the LAST unlock response (this is sufficient to indicate the success of the sequence of unlocks). If an unlock fails, it will bail and return that response. For instance, In the event that there are 3 shared locks and the second unlock method fails, then you will get returned the unsuccessful second response. The 3rd unlock will not be attempted.
Don't run with this knife, you could hurt someone (or yourself).
=item B<steal_lock>
Removes all locks from a resource, relocks it in your name and returns the DAV::Response object for the lock command. This method takes no arguments.
$response = $resource->steal_lock;
Synonymous to forcefully_unlock_all() and then lock().
=item B<lockdiscovery>
Discover the locks held against this resource and return a DAV::Response object. This method take no arguments.
$response = $resource->lockdiscovery;
@locks = $resource->get_locks if $response->is_success;
This method is in fact a simplified version of propfind().
=item B<as_string>
Returns a string representation of the object. Mainly useful for debugging purposes. It takes no arguments.
print $resource->as_string
=back
=head1 ACCESSOR METHODS (get, set and is)
=over 4
=item B<is_option>
Returns a boolean indicating whether this resource supports the option passed in as a string. The option match is case insensitive so, PUT and Put are should both work.
if ($resource->is_option( "PUT" ) ) {
$resource->put( ... )
}
Note: this routine automatically calls the options() routine which makes the request to the server. Subsequent calls to is_option will use the cached option list. To force a rerequest to the server call options()
=item B<is_locked>
Returns a boolean indicating whether this resource is locked.
@lock = $resource->is_locked( -owned=>[1|0] );
B<owned> - this parameter is used to ask, is this resource locked by me?
Note: You must have already called propfind() or lockdiscovery()
e.g.
Is the resource locked at all?
print "yes" if $resource->is_locked();
Is the resource locked by me?
print "yes" if $resource->is_locked( -owned=>1 );
Is the resource locked by someone other than me?
print "yes" if $resource->is_locked( -owned=>0 );
=item B<is_collection>
Returns a boolean indicating whether this resource is a collection.
print "Directory" if ( $resource->is_collection );
You must first have performed a propfind.
=item B<get_uri>
Returns the URI object for this resource.
print "URL is: " . $resource->get_uri()->as_string . "\n";
See the URI manpage from the LWP libraries (perldoc URI)
=item B<get_property>
Returns a property value. Takes a string as an argument.
print $resource->get_property( "displayname" );
You must first have performed a propfind.
=item B<get_options>
Returns an array of options allowed on this resource.
Note: If $resource->options has not been called then it will return an empty array.
@options = $resource->get_options
=item B<get_content>
Returns the resource's content/body as a string.
The content is typically the result of a GET.
$content = $resource->get_content
=item B<get_content_ref>
Returns the resource's content/body as a reference to a string.
This is useful and more efficient if the content is large.
${$resource->get_content_ref} =~ s/\bfoo\b/bar/g;
Note: You must have already called get()
=item B<get_lock>
Returns the DAV::Lock object if it exists. Requires opaquelocktoken passed as a parameter.
$lock = $resource->get_lock( "opaquelocktoken:234214--342-3444" );
=item B<get_locks>
Returns a list of any DAV::Lock objects held against the resource.
@lock = $resource->get_locks( -owned=>[1|0] );
B<owned> - this parameter indicates which locks you want.
- '1', requests any of my locks. (Locked by this DAV instance).
- '0' ,requests any locks not owned by us.
- any other value or no value, requests ALL locks.
Note: You must have already called propfind() or lockdiscovery()
e.g.
Give me my locks
@lock = $resource->get_locks( -owned=>1 );
Give me all locks
@lock = $resource->get_locks();
=item B<get_lockedresourcelist>
=item B<get_parentresourcelist>
=item B<get_comms>
=item B<set_parent_resourcelist>
$resource->set_parent_resourcelist( $resourcelist )
Sets the parent resource list (ask the question, which collection am I a member of?). See L<HTTP::DAV::ResourceList>.
=back
=cut
|