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 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include <boost/unordered_map.hpp>
#include <vector>
#include <string.h>
#include "osl/diagnose.h"
#include "osl/time.h"
#include <rtl/string.h>
#include <ne_socket.h>
#include <ne_auth.h>
#include <ne_redirect.h>
#include <ne_ssl.h>
#if NEON_VERSION < 0x0260
// old neon versions forgot to set this
extern "C" {
#endif
#include <ne_compress.h>
#if NEON_VERSION < 0x0260
}
#endif
#include "libxml/parser.h"
#include "rtl/ustrbuf.hxx"
#include "comphelper/sequence.hxx"
#include <comphelper/stl_types.hxx>
#include "ucbhelper/simplecertificatevalidationrequest.hxx"
#include "DAVAuthListener.hxx"
#include "NeonTypes.hxx"
#include "NeonSession.hxx"
#include "NeonInputStream.hxx"
#include "NeonPropFindRequest.hxx"
#include "NeonHeadRequest.hxx"
#include "NeonUri.hxx"
#include "LinkSequence.hxx"
#include "UCBDeadPropertyValue.hxx"
#include <com/sun/star/xml/crypto/XSecurityEnvironment.hpp>
#include <com/sun/star/security/XCertificate.hpp>
#include <com/sun/star/security/CertificateValidity.hpp>
#include <com/sun/star/security/CertificateContainerStatus.hpp>
#include <com/sun/star/security/CertificateContainer.hpp>
#include <com/sun/star/security/XCertificateContainer.hpp>
#include <com/sun/star/ucb/Lock.hpp>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/xml/crypto/XSEInitializer.hpp>
#include <boost/bind.hpp>
using namespace com::sun::star;
using namespace webdav_ucp;
#define SEINITIALIZER_COMPONENT "com.sun.star.xml.crypto.SEInitializer"
#ifndef EOL
# define EOL "\r\n"
#endif
// -------------------------------------------------------------------
// RequestData
// -------------------------------------------------------------------
struct RequestData
{
// POST
rtl::OUString aContentType;
rtl::OUString aReferer;
RequestData() {}
RequestData( const rtl::OUString & rContentType,
const rtl::OUString & rReferer )
: aContentType( rContentType ), aReferer( rReferer ) {}
};
// -------------------------------------------------------------------
// RequestDataMap
// -------------------------------------------------------------------
struct equalPtr
{
bool operator()( const ne_request* p1, const ne_request* p2 ) const
{
return p1 == p2;
}
};
struct hashPtr
{
size_t operator()( const ne_request* p ) const
{
return (size_t)p;
}
};
typedef boost::unordered_map
<
ne_request*,
RequestData,
hashPtr,
equalPtr
>
RequestDataMap;
// -------------------------------------------------------------------
// Helper fuction
// -------------------------------------------------------------------
static sal_uInt16 makeStatusCode( const rtl::OUString & rStatusText )
{
// Extract status code from session error string. Unfortunately
// neon provides no direct access to the status code...
if ( rStatusText.getLength() < 3 )
{
OSL_FAIL(
"makeStatusCode - status text string to short!" );
return 0;
}
sal_Int32 nPos = rStatusText.indexOf( ' ' );
if ( nPos == -1 )
{
OSL_FAIL( "makeStatusCode - wrong status text format!" );
return 0;
}
return sal_uInt16( rStatusText.copy( 0, nPos ).toInt32() );
}
// -------------------------------------------------------------------
static bool noKeepAlive( const uno::Sequence< beans::NamedValue >& rFlags )
{
if ( !rFlags.hasElements() )
return false;
// find "KeepAlive" property
const beans::NamedValue* pAry(rFlags.getConstArray());
const sal_Int32 nLen(rFlags.getLength());
const beans::NamedValue* pValue(
std::find_if(pAry,pAry+nLen,
boost::bind(comphelper::TNamedValueEqualFunctor(),
_1,
rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("KeepAlive")))));
if ( pValue != pAry+nLen && !pValue->Value.get<sal_Bool>() )
return true;
return false;
}
// -------------------------------------------------------------------
struct NeonRequestContext
{
uno::Reference< io::XOutputStream > xOutputStream;
rtl::Reference< NeonInputStream > xInputStream;
const std::vector< ::rtl::OUString > * pHeaderNames;
DAVResource * pResource;
NeonRequestContext( uno::Reference< io::XOutputStream > & xOutStrm )
: xOutputStream( xOutStrm ), xInputStream( 0 ),
pHeaderNames( 0 ), pResource( 0 ) {}
NeonRequestContext( const rtl::Reference< NeonInputStream > & xInStrm )
: xOutputStream( 0 ), xInputStream( xInStrm ),
pHeaderNames( 0 ), pResource( 0 ) {}
NeonRequestContext( uno::Reference< io::XOutputStream > & xOutStrm,
const std::vector< ::rtl::OUString > & inHeaderNames,
DAVResource & ioResource )
: xOutputStream( xOutStrm ), xInputStream( 0 ),
pHeaderNames( &inHeaderNames ), pResource( &ioResource ) {}
NeonRequestContext( const rtl::Reference< NeonInputStream > & xInStrm,
const std::vector< ::rtl::OUString > & inHeaderNames,
DAVResource & ioResource )
: xOutputStream( 0 ), xInputStream( xInStrm ),
pHeaderNames( &inHeaderNames ), pResource( &ioResource ) {}
};
//--------------------------------------------------------------------
//--------------------------------------------------------------------
//
// Callback functions
//
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// -------------------------------------------------------------------
// ResponseBlockReader
// A simple Neon response_block_reader for use with an XInputStream
// -------------------------------------------------------------------
extern "C" int NeonSession_ResponseBlockReader(void * inUserData,
const char * inBuf,
size_t inLen )
{
// neon sometimes calls this function with (inLen == 0)...
if ( inLen > 0 )
{
NeonRequestContext * pCtx
= static_cast< NeonRequestContext * >( inUserData );
rtl::Reference< NeonInputStream > xInputStream(
pCtx->xInputStream );
if ( xInputStream.is() )
xInputStream->AddToStream( inBuf, inLen );
}
return 0;
}
// -------------------------------------------------------------------
// ResponseBlockWriter
// A simple Neon response_block_reader for use with an XOutputStream
// -------------------------------------------------------------------
extern "C" int NeonSession_ResponseBlockWriter( void * inUserData,
const char * inBuf,
size_t inLen )
{
// neon calls this function with (inLen == 0)...
if ( inLen > 0 )
{
NeonRequestContext * pCtx
= static_cast< NeonRequestContext * >( inUserData );
uno::Reference< io::XOutputStream > xOutputStream
= pCtx->xOutputStream;
if ( xOutputStream.is() )
{
const uno::Sequence< sal_Int8 > aSeq( (sal_Int8 *)inBuf, inLen );
xOutputStream->writeBytes( aSeq );
}
}
return 0;
}
// -------------------------------------------------------------------
extern "C" int NeonSession_NeonAuth( void * inUserData,
#ifdef NE_FEATURE_SSPI
const char * inAuthProtocol,
#endif
const char * inRealm,
int attempt,
char * inoutUserName,
char * inoutPassWord )
{
/* The callback used to request the username and password in the given
* realm. The username and password must be copied into the buffers
* which are both of size NE_ABUFSIZ. The 'attempt' parameter is zero
* on the first call to the callback, and increases by one each time
* an attempt to authenticate fails.
*
* The callback must return zero to indicate that authentication
* should be attempted with the username/password, or non-zero to
* cancel the request. (if non-zero, username and password are
* ignored.) */
NeonSession * theSession = static_cast< NeonSession * >( inUserData );
DAVAuthListener * pListener
= theSession->getRequestEnvironment().m_xAuthListener.get();
if ( !pListener )
{
// abort
return -1;
}
rtl::OUString theUserName;
rtl::OUString thePassWord;
if ( attempt == 0 )
{
// neon does not handle username supplied with request URI (for
// instance when doing FTP over proxy - last checked: 0.23.5 )
try
{
NeonUri uri( theSession->getRequestEnvironment().m_aRequestURI );
rtl::OUString aUserInfo( uri.GetUserInfo() );
if ( aUserInfo.getLength() )
{
sal_Int32 nPos = aUserInfo.indexOf( '@' );
if ( nPos == -1 )
{
theUserName = aUserInfo;
}
else
{
theUserName = aUserInfo.copy( 0, nPos );
thePassWord = aUserInfo.copy( nPos + 1 );
}
}
}
catch ( DAVException const & )
{
// abort
return -1;
}
}
else
{
// username buffer is prefilled with user name from last attempt.
theUserName = rtl::OUString::createFromAscii( inoutUserName );
// @@@ Neon does not initialize password buffer (last checked: 0.22.0).
//thePassWord = rtl::OUString::createFromAscii( inoutPassWord );
}
bool bCanUseSystemCreds = false;
#ifdef NE_FEATURE_SSPI
bCanUseSystemCreds
= (attempt == 0) && // avoid endless loops
ne_has_support( NE_FEATURE_SSPI ) && // Windows-only feature.
( ( ne_strcasecmp( inAuthProtocol, "NTLM" ) == 0 ) ||
( ne_strcasecmp( inAuthProtocol, "Negotiate" ) == 0 ) );
#endif
int theRetVal = pListener->authenticate(
rtl::OUString::createFromAscii( inRealm ),
theSession->getHostName(),
theUserName,
thePassWord,
bCanUseSystemCreds);
rtl::OString aUser(
rtl::OUStringToOString( theUserName, RTL_TEXTENCODING_UTF8 ) );
if ( aUser.getLength() > ( NE_ABUFSIZ - 1 ) )
{
OSL_FAIL(
"NeonSession_NeonAuth - username to long!" );
return -1;
}
rtl::OString aPass(
rtl::OUStringToOString( thePassWord, RTL_TEXTENCODING_UTF8 ) );
if ( aPass.getLength() > ( NE_ABUFSIZ - 1 ) )
{
OSL_FAIL(
"NeonSession_NeonAuth - password to long!" );
return -1;
}
strcpy( inoutUserName, // #100211# - checked
rtl::OUStringToOString( theUserName, RTL_TEXTENCODING_UTF8 ).getStr() );
strcpy( inoutPassWord, // #100211# - checked
rtl::OUStringToOString( thePassWord, RTL_TEXTENCODING_UTF8 ).getStr() );
return theRetVal;
}
// -------------------------------------------------------------------
namespace {
// -------------------------------------------------------------------
// Helper function
::rtl::OUString GetHostnamePart( const ::rtl::OUString& _rRawString )
{
::rtl::OUString sPart;
::rtl::OUString sPartId(RTL_CONSTASCII_USTRINGPARAM("CN="));
sal_Int32 nContStart = _rRawString.indexOf( sPartId );
if ( nContStart != -1 )
{
nContStart = nContStart + sPartId.getLength();
sal_Int32 nContEnd
= _rRawString.indexOf( sal_Unicode( ',' ), nContStart );
sPart = _rRawString.copy( nContStart, nContEnd - nContStart );
}
return sPart;
}
} // namespace
// -------------------------------------------------------------------
extern "C" int NeonSession_CertificationNotify( void *userdata,
int failures,
const ne_ssl_certificate *cert )
{
OSL_ASSERT( cert );
NeonSession * pSession = static_cast< NeonSession * >( userdata );
uno::Reference< security::XCertificateContainer > xCertificateContainer;
try
{
xCertificateContainer
= uno::Reference< security::XCertificateContainer >(
pSession->getMSF()->createInstance(
rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
"com.sun.star.security.CertificateContainer" )) ),
uno::UNO_QUERY );
}
catch ( uno::Exception const & )
{
}
if ( !xCertificateContainer.is() )
return 1;
failures = 0;
char * dn = ne_ssl_readable_dname( ne_ssl_cert_subject( cert ) );
rtl::OUString cert_subject( dn, strlen( dn ), RTL_TEXTENCODING_UTF8, 0 );
ne_free( dn );
security::CertificateContainerStatus certificateContainer(
xCertificateContainer->hasCertificate(
pSession->getHostName(), cert_subject ) );
if ( certificateContainer != security::CertificateContainerStatus_NOCERT )
return
certificateContainer == security::CertificateContainerStatus_TRUSTED
? 0
: 1;
uno::Reference< xml::crypto::XSEInitializer > xSEInitializer;
try
{
xSEInitializer = uno::Reference< xml::crypto::XSEInitializer >(
pSession->getMSF()->createInstance(
rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( SEINITIALIZER_COMPONENT )) ),
uno::UNO_QUERY );
}
catch ( uno::Exception const & )
{
}
if ( !xSEInitializer.is() )
return 1;
uno::Reference< xml::crypto::XXMLSecurityContext > xSecurityContext(
xSEInitializer->createSecurityContext( rtl::OUString() ) );
uno::Reference< xml::crypto::XSecurityEnvironment > xSecurityEnv(
xSecurityContext->getSecurityEnvironment() );
//The end entity certificate
char * eeCertB64 = ne_ssl_cert_export( cert );
rtl::OString sEECertB64( eeCertB64 );
uno::Reference< security::XCertificate > xEECert(
xSecurityEnv->createCertificateFromAscii(
rtl::OStringToOUString( sEECertB64, RTL_TEXTENCODING_ASCII_US ) ) );
ne_free( eeCertB64 );
eeCertB64 = 0;
std::vector< uno::Reference< security::XCertificate > > vecCerts;
const ne_ssl_certificate * issuerCert = cert;
do
{
//get the intermediate certificate
//the returned value is const ! Therfore it does not need to be freed
//with ne_ssl_cert_free, which takes a non-const argument
issuerCert = ne_ssl_cert_signedby( issuerCert );
if ( NULL == issuerCert )
break;
char * imCertB64 = ne_ssl_cert_export( issuerCert );
rtl::OString sInterMediateCertB64( imCertB64 );
ne_free( imCertB64 );
uno::Reference< security::XCertificate> xImCert(
xSecurityEnv->createCertificateFromAscii(
rtl::OStringToOUString(
sInterMediateCertB64, RTL_TEXTENCODING_ASCII_US ) ) );
if ( xImCert.is() )
vecCerts.push_back( xImCert );
}
while ( 1 );
sal_Int64 certValidity = xSecurityEnv->verifyCertificate( xEECert,
::comphelper::containerToSequence( vecCerts ) );
if ( pSession->isDomainMatch(
GetHostnamePart( xEECert.get()->getSubjectName() ) ) )
{
// if host name matched with certificate then look if the
// certificate was ok
if( certValidity == security::CertificateValidity::VALID )
return 0;
}
const uno::Reference< ucb::XCommandEnvironment > xEnv(
pSession->getRequestEnvironment().m_xEnv );
if ( xEnv.is() )
{
failures = static_cast< int >( certValidity );
uno::Reference< task::XInteractionHandler > xIH(
xEnv->getInteractionHandler() );
if ( xIH.is() )
{
rtl::Reference< ucbhelper::SimpleCertificateValidationRequest >
xRequest( new ucbhelper::SimpleCertificateValidationRequest(
(sal_Int32)failures, xEECert, pSession->getHostName() ) );
xIH->handle( xRequest.get() );
rtl::Reference< ucbhelper::InteractionContinuation > xSelection
= xRequest->getSelection();
if ( xSelection.is() )
{
uno::Reference< task::XInteractionApprove > xApprove(
xSelection.get(), uno::UNO_QUERY );
if ( xApprove.is() )
{
xCertificateContainer->addCertificate(
pSession->getHostName(), cert_subject, sal_True );
return 0;
}
else
{
// Don't trust cert
xCertificateContainer->addCertificate(
pSession->getHostName(), cert_subject, sal_False );
return 1;
}
}
}
else
{
// Don't trust cert
xCertificateContainer->addCertificate(
pSession->getHostName(), cert_subject, sal_False );
return 1;
}
}
return 1;
}
// -------------------------------------------------------------------
extern "C" void NeonSession_PreSendRequest( ne_request * req,
void * userdata,
ne_buffer * headers )
{
// userdata -> value returned by 'create'
NeonSession * pSession = static_cast< NeonSession * >( userdata );
if ( pSession )
{
// If there is a proxy server in between, it shall never use
// cached data. We always want 'up-to-date' data.
ne_buffer_concat( headers, "Pragma: no-cache", EOL, NULL );
// alternative, but understoud by HTTP 1.1 servers only:
// ne_buffer_concat( headers, "Cache-Control: max-age=0", EOL, NULL );
const RequestDataMap * pRequestData
= static_cast< const RequestDataMap* >(
pSession->getRequestData() );
RequestDataMap::const_iterator it = pRequestData->find( req );
if ( it != pRequestData->end() )
{
if ( (*it).second.aContentType.getLength() )
{
char * pData = headers->data;
if ( strstr( pData, "Content-Type:" ) == NULL )
{
rtl::OString aType
= rtl::OUStringToOString( (*it).second.aContentType,
RTL_TEXTENCODING_UTF8 );
ne_buffer_concat( headers, "Content-Type: ",
aType.getStr(), EOL, NULL );
}
}
if ( (*it).second.aReferer.getLength() )
{
char * pData = headers->data;
if ( strstr( pData, "Referer:" ) == NULL )
{
rtl::OString aReferer
= rtl::OUStringToOString( (*it).second.aReferer,
RTL_TEXTENCODING_UTF8 );
ne_buffer_concat( headers, "Referer: ",
aReferer.getStr(), EOL, NULL );
}
}
}
const DAVRequestHeaders & rHeaders
= pSession->getRequestEnvironment().m_aRequestHeaders;
DAVRequestHeaders::const_iterator it1( rHeaders.begin() );
const DAVRequestHeaders::const_iterator end1( rHeaders.end() );
while ( it1 != end1 )
{
rtl::OString aHeader
= rtl::OUStringToOString( (*it1).first,
RTL_TEXTENCODING_UTF8 );
rtl::OString aValue
= rtl::OUStringToOString( (*it1).second,
RTL_TEXTENCODING_UTF8 );
ne_buffer_concat( headers, aHeader.getStr(), ": ",
aValue.getStr(), EOL, NULL );
++it1;
}
}
}
// -------------------------------------------------------------------
// static members!
bool NeonSession::m_bGlobalsInited = false;
//See https://bugzilla.redhat.com/show_bug.cgi?id=544619#c4
//neon is threadsafe, but uses gnutls which is only thread-safe
//if initialized to be thread-safe. cups, unfortunately, generally
//initializes it first, and as non-thread-safe, leaving the entire
//stack unsafe
osl::Mutex aGlobalNeonMutex;
NeonLockStore NeonSession::m_aNeonLockStore;
// -------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------
NeonSession::NeonSession(
const rtl::Reference< DAVSessionFactory > & rSessionFactory,
const rtl::OUString& inUri,
const uno::Sequence< beans::NamedValue >& rFlags,
const ucbhelper::InternetProxyDecider & rProxyDecider )
throw ( DAVException )
: DAVSession( rSessionFactory ),
m_aFlags( rFlags ),
m_pHttpSession( 0 ),
m_pRequestData( new RequestDataMap ),
m_rProxyDecider( rProxyDecider )
{
NeonUri theUri( inUri );
m_aScheme = theUri.GetScheme();
m_aHostName = theUri.GetHost();
m_nPort = theUri.GetPort();
}
// -------------------------------------------------------------------
// Destructor
// -------------------------------------------------------------------
NeonSession::~NeonSession( )
{
if ( m_pHttpSession )
{
{
osl::Guard< osl::Mutex > theGlobalGuard( aGlobalNeonMutex );
ne_session_destroy( m_pHttpSession );
}
m_pHttpSession = 0;
}
delete static_cast< RequestDataMap * >( m_pRequestData );
}
// -------------------------------------------------------------------
void NeonSession::Init( const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
m_aEnv = rEnv;
Init();
}
// -------------------------------------------------------------------
void NeonSession::Init()
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
bool bCreateNewSession = false;
if ( m_pHttpSession == 0 )
{
// Ensure that Neon sockets are initialized
osl::Guard< osl::Mutex > theGlobalGuard( aGlobalNeonMutex );
if ( !m_bGlobalsInited )
{
if ( ne_sock_init() != 0 )
throw DAVException( DAVException::DAV_SESSION_CREATE,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
// #122205# - libxml2 needs to be initialized once if used by
// multithreaded programs like OOo.
xmlInitParser();
#if 0
// for more debug flags see ne_utils.h; NE_DEBUGGING must be defined
// while compiling neon in order to actually activate neon debug
// output.
ne_debug_init( stderr, NE_DBG_FLUSH
| NE_DBG_HTTP
// | NE_DBG_HTTPBODY
// | NE_DBG_HTTPAUTH
// | NE_DBG_XML
// | NE_DBG_XMLPARSE
// | NE_DBG_LOCKS
);
#endif
m_bGlobalsInited = true;
}
const ucbhelper::InternetProxyServer & rProxyCfg = getProxySettings();
m_aProxyName = rProxyCfg.aName;
m_nProxyPort = rProxyCfg.nPort;
// Not yet initialized. Create new session.
bCreateNewSession = true;
}
else
{
// #112271# Check whether proxy settings are still valid (They may
// change at any time). If not, create new Neon session.
const ucbhelper::InternetProxyServer & rProxyCfg = getProxySettings();
if ( ( rProxyCfg.aName != m_aProxyName )
|| ( rProxyCfg.nPort != m_nProxyPort ) )
{
m_aProxyName = rProxyCfg.aName;
m_nProxyPort = rProxyCfg.nPort;
// new session needed, destroy old first
{
osl::Guard< osl::Mutex > theGlobalGuard( aGlobalNeonMutex );
ne_session_destroy( m_pHttpSession );
}
m_pHttpSession = 0;
bCreateNewSession = true;
}
}
if ( bCreateNewSession )
{
// @@@ For FTP over HTTP proxy inUserInfo is needed to be able to
// build the complete request URI (including user:pass), but
// currently (0.22.0) neon does not allow to pass the user info
// to the session
{
osl::Guard< osl::Mutex > theGlobalGuard( aGlobalNeonMutex );
m_pHttpSession = ne_session_create(
rtl::OUStringToOString( m_aScheme, RTL_TEXTENCODING_UTF8 ).getStr(),
/* theUri.GetUserInfo(),
@@@ for FTP via HTTP proxy, but not supported by Neon */
rtl::OUStringToOString( m_aHostName, RTL_TEXTENCODING_UTF8 ).getStr(),
m_nPort );
}
if ( m_pHttpSession == 0 )
throw DAVException( DAVException::DAV_SESSION_CREATE,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
// Register the session with the lock store
m_aNeonLockStore.registerSession( m_pHttpSession );
if ( m_aScheme.equalsIgnoreAsciiCase(
rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "https" ) ) ) )
{
// Set a failure callback for certificate check
ne_ssl_set_verify(
m_pHttpSession, NeonSession_CertificationNotify, this);
}
// Add hooks (i.e. for adding additional headers to the request)
#if 0
/* Hook called when a request is created. */
//typedef void (*ne_create_request_fn)(ne_request *req, void *userdata,
// const char *method, const char *path);
ne_hook_create_request( m_pHttpSession, create_req_hook_fn, this );
#endif
/* Hook called before the request is sent. 'header' is the raw HTTP
* header before the trailing CRLF is added: add in more here. */
//typedef void (*ne_pre_send_fn)(ne_request *req, void *userdata,
// ne_buffer *header);
ne_hook_pre_send( m_pHttpSession, NeonSession_PreSendRequest, this );
#if 0
/* Hook called after the request is sent. May return:
* NE_OK everything is okay
* NE_RETRY try sending the request again.
* anything else signifies an error, and the request is failed. The
* return code is passed back the _dispatch caller, so the session error
* must also be set appropriately (ne_set_error).
*/
//typedef int (*ne_post_send_fn)(ne_request *req, void *userdata,
// const ne_status *status);
ne_hook_post_send( m_pHttpSession, post_send_req_hook_fn, this );
/* Hook called when the request is destroyed. */
//typedef void (*ne_destroy_req_fn)(ne_request *req, void *userdata);
ne_hook_destroy_request( m_pHttpSession, destroy_req_hook_fn, this );
/* Hook called when the session is destroyed. */
//typedef void (*ne_destroy_sess_fn)(void *userdata);
ne_hook_destroy_session( m_pHttpSession, destroy_sess_hook_fn, this );
#endif
if ( m_aProxyName.getLength() )
{
ne_session_proxy( m_pHttpSession,
rtl::OUStringToOString(
m_aProxyName,
RTL_TEXTENCODING_UTF8 ).getStr(),
m_nProxyPort );
}
// avoid KeepAlive?
if ( noKeepAlive(m_aFlags) )
ne_set_session_flag( m_pHttpSession, NE_SESSFLAG_PERSIST, 0 );
// Register for redirects.
ne_redirect_register( m_pHttpSession );
// authentication callbacks.
#if NEON_VERSION >= 0x0260
ne_add_server_auth( m_pHttpSession, NE_AUTH_ALL, NeonSession_NeonAuth, this );
ne_add_proxy_auth ( m_pHttpSession, NE_AUTH_ALL, NeonSession_NeonAuth, this );
#else
ne_set_server_auth( m_pHttpSession, NeonSession_NeonAuth, this );
ne_set_proxy_auth ( m_pHttpSession, NeonSession_NeonAuth, this );
#endif
}
}
// -------------------------------------------------------------------
// virtual
sal_Bool NeonSession::CanUse( const rtl::OUString & inUri,
const uno::Sequence< beans::NamedValue >& rFlags )
{
try
{
NeonUri theUri( inUri );
if ( ( theUri.GetPort() == m_nPort ) &&
( theUri.GetHost() == m_aHostName ) &&
( theUri.GetScheme() == m_aScheme ) &&
( rFlags == m_aFlags ) )
return sal_True;
}
catch ( DAVException const & )
{
return sal_False;
}
return sal_False;
}
// -------------------------------------------------------------------
// virtual
sal_Bool NeonSession::UsesProxy()
{
Init();
return ( m_aProxyName.getLength() > 0 );
}
// -------------------------------------------------------------------
// OPTIONS
// -------------------------------------------------------------------
void NeonSession::OPTIONS( const rtl::OUString & inPath,
DAVCapabilities & outCapabilities,
const DAVRequestEnvironment & rEnv )
throw( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
HttpServerCapabilities servercaps;
memset( &servercaps, 0, sizeof( servercaps ) );
int theRetVal = ne_options( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
&servercaps );
HandleError( theRetVal, inPath, rEnv );
outCapabilities.class1 = !!servercaps.dav_class1;
outCapabilities.class2 = !!servercaps.dav_class2;
outCapabilities.executable = !!servercaps.dav_executable;
}
// -------------------------------------------------------------------
// PROPFIND - allprop & named
// -------------------------------------------------------------------
void NeonSession::PROPFIND( const rtl::OUString & inPath,
const Depth inDepth,
const std::vector< rtl::OUString > & inPropNames,
std::vector< DAVResource > & ioResources,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
int theRetVal = NE_OK;
NeonPropFindRequest theRequest( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
inDepth,
inPropNames,
ioResources,
theRetVal );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// PROPFIND - propnames
// -------------------------------------------------------------------
void NeonSession::PROPFIND( const rtl::OUString & inPath,
const Depth inDepth,
std::vector< DAVResourceInfo > & ioResInfo,
const DAVRequestEnvironment & rEnv )
throw( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
int theRetVal = NE_OK;
NeonPropFindRequest theRequest( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
inDepth,
ioResInfo,
theRetVal );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// PROPPATCH
// -------------------------------------------------------------------
void NeonSession::PROPPATCH( const rtl::OUString & inPath,
const std::vector< ProppatchValue > & inValues,
const DAVRequestEnvironment & rEnv )
throw( DAVException )
{
/* @@@ Which standard live properties can be set by the client?
This is a known WebDAV RFC issue ( verified: 04/10/2001 )
--> http://www.ics.uci.edu/pub/ietf/webdav/protocol/issues.html
mod_dav implementation:
creationdate r ( File System prop )
displayname w
getcontentlanguage r ( #ifdef DAV_DISABLE_WRITEABLE_PROPS )
getcontentlength r ( File System prop )
getcontenttype r ( #ifdef DAV_DISABLE_WRITEABLE_PROPS )
getetag r ( File System prop )
getlastmodified r ( File System prop )
lockdiscovery r
resourcetype r
source w
supportedlock r
executable w ( #ifndef WIN32 )
All dead properties are of course writable.
*/
int theRetVal = NE_OK;
int n; // for the "for" loop
// Generate the list of properties we want to set.
int nPropCount = inValues.size();
ne_proppatch_operation* pItems
= new ne_proppatch_operation[ nPropCount + 1 ];
for ( n = 0; n < nPropCount; ++n )
{
const ProppatchValue & rValue = inValues[ n ];
// Split fullname into namespace and name!
ne_propname * pName = new ne_propname;
DAVProperties::createNeonPropName( rValue.name, *pName );
pItems[ n ].name = pName;
if ( rValue.operation == PROPSET )
{
pItems[ n ].type = ne_propset;
rtl::OUString aStringValue;
if ( DAVProperties::isUCBDeadProperty( *pName ) )
{
// DAV dead property added by WebDAV UCP?
if ( !UCBDeadPropertyValue::toXML( rValue.value,
aStringValue ) )
{
// Error!
pItems[ n ].value = 0;
theRetVal = NE_ERROR;
nPropCount = n + 1;
break;
}
}
else if ( !( rValue.value >>= aStringValue ) )
{
// complex properties...
if ( rValue.name == DAVProperties::SOURCE )
{
uno::Sequence< ucb::Link > aLinks;
if ( rValue.value >>= aLinks )
{
LinkSequence::toXML( aLinks, aStringValue );
}
else
{
// Error!
pItems[ n ].value = 0;
theRetVal = NE_ERROR;
nPropCount = n + 1;
break;
}
}
else
{
OSL_FAIL( "NeonSession::PROPPATCH - unsupported type!" );
// Error!
pItems[ n ].value = 0;
theRetVal = NE_ERROR;
nPropCount = n + 1;
break;
}
}
pItems[ n ].value
= strdup( rtl::OUStringToOString( aStringValue,
RTL_TEXTENCODING_UTF8 ).getStr() );
}
else
{
pItems[ n ].type = ne_propremove;
pItems[ n ].value = 0;
}
}
if ( theRetVal == NE_OK )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
pItems[ n ].name = 0;
theRetVal = ne_proppatch( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
pItems );
}
for ( n = 0; n < nPropCount; ++n )
{
free( (void *)pItems[ n ].name->name );
delete pItems[ n ].name;
free( (void *)pItems[ n ].value );
}
delete [] pItems;
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// HEAD
// -------------------------------------------------------------------
void NeonSession::HEAD( const ::rtl::OUString & inPath,
const std::vector< ::rtl::OUString > & inHeaderNames,
DAVResource & ioResource,
const DAVRequestEnvironment & rEnv )
throw( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
int theRetVal = NE_OK;
NeonHeadRequest theRequest( m_pHttpSession,
inPath,
inHeaderNames,
ioResource,
theRetVal );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// GET
// -------------------------------------------------------------------
uno::Reference< io::XInputStream >
NeonSession::GET( const rtl::OUString & inPath,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
rtl::Reference< NeonInputStream > xInputStream( new NeonInputStream );
NeonRequestContext aCtx( xInputStream );
int theRetVal = GET( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
NeonSession_ResponseBlockReader,
false,
&aCtx );
HandleError( theRetVal, inPath, rEnv );
return uno::Reference< io::XInputStream >( xInputStream.get() );
}
// -------------------------------------------------------------------
// GET
// -------------------------------------------------------------------
void NeonSession::GET( const rtl::OUString & inPath,
uno::Reference< io::XOutputStream > & ioOutputStream,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
NeonRequestContext aCtx( ioOutputStream );
int theRetVal = GET( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
NeonSession_ResponseBlockWriter,
false,
&aCtx );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// GET
// -------------------------------------------------------------------
uno::Reference< io::XInputStream >
NeonSession::GET( const rtl::OUString & inPath,
const std::vector< ::rtl::OUString > & inHeaderNames,
DAVResource & ioResource,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
ioResource.uri = inPath;
ioResource.properties.clear();
rtl::Reference< NeonInputStream > xInputStream( new NeonInputStream );
NeonRequestContext aCtx( xInputStream, inHeaderNames, ioResource );
int theRetVal = GET( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
NeonSession_ResponseBlockReader,
true,
&aCtx );
HandleError( theRetVal, inPath, rEnv );
return uno::Reference< io::XInputStream >( xInputStream.get() );
}
// -------------------------------------------------------------------
// GET
// -------------------------------------------------------------------
void NeonSession::GET( const rtl::OUString & inPath,
uno::Reference< io::XOutputStream > & ioOutputStream,
const std::vector< ::rtl::OUString > & inHeaderNames,
DAVResource & ioResource,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
ioResource.uri = inPath;
ioResource.properties.clear();
NeonRequestContext aCtx( ioOutputStream, inHeaderNames, ioResource );
int theRetVal = GET( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
NeonSession_ResponseBlockWriter,
true,
&aCtx );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// PUT
// -------------------------------------------------------------------
void NeonSession::PUT( const rtl::OUString & inPath,
const uno::Reference< io::XInputStream > & inInputStream,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
uno::Sequence< sal_Int8 > aDataToSend;
if ( !getDataFromInputStream( inInputStream, aDataToSend, false ) )
throw DAVException( DAVException::DAV_INVALID_ARG );
Init( rEnv );
int theRetVal = PUT( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
reinterpret_cast< const char * >(
aDataToSend.getConstArray() ),
aDataToSend.getLength() );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// POST
// -------------------------------------------------------------------
uno::Reference< io::XInputStream >
NeonSession::POST( const rtl::OUString & inPath,
const rtl::OUString & rContentType,
const rtl::OUString & rReferer,
const uno::Reference< io::XInputStream > & inInputStream,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
uno::Sequence< sal_Int8 > aDataToSend;
if ( !getDataFromInputStream( inInputStream, aDataToSend, true ) )
throw DAVException( DAVException::DAV_INVALID_ARG );
Init( rEnv );
rtl::Reference< NeonInputStream > xInputStream( new NeonInputStream );
NeonRequestContext aCtx( xInputStream );
int theRetVal = POST( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
reinterpret_cast< const char * >(
aDataToSend.getConstArray() ),
NeonSession_ResponseBlockReader,
&aCtx,
rContentType,
rReferer );
HandleError( theRetVal, inPath, rEnv );
return uno::Reference< io::XInputStream >( xInputStream.get() );
}
// -------------------------------------------------------------------
// POST
// -------------------------------------------------------------------
void NeonSession::POST( const rtl::OUString & inPath,
const rtl::OUString & rContentType,
const rtl::OUString & rReferer,
const uno::Reference< io::XInputStream > & inInputStream,
uno::Reference< io::XOutputStream > & oOutputStream,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
uno::Sequence< sal_Int8 > aDataToSend;
if ( !getDataFromInputStream( inInputStream, aDataToSend, true ) )
throw DAVException( DAVException::DAV_INVALID_ARG );
Init( rEnv );
NeonRequestContext aCtx( oOutputStream );
int theRetVal = POST( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr(),
reinterpret_cast< const char * >(
aDataToSend.getConstArray() ),
NeonSession_ResponseBlockWriter,
&aCtx,
rContentType,
rReferer );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// MKCOL
// -------------------------------------------------------------------
void NeonSession::MKCOL( const rtl::OUString & inPath,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
int theRetVal = ne_mkcol( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr() );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// COPY
// -------------------------------------------------------------------
void NeonSession::COPY( const rtl::OUString & inSourceURL,
const rtl::OUString & inDestinationURL,
const DAVRequestEnvironment & rEnv,
sal_Bool inOverWrite )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
NeonUri theSourceUri( inSourceURL );
NeonUri theDestinationUri( inDestinationURL );
int theRetVal = ne_copy( m_pHttpSession,
inOverWrite ? 1 : 0,
NE_DEPTH_INFINITE,
rtl::OUStringToOString(
theSourceUri.GetPath(),
RTL_TEXTENCODING_UTF8 ).getStr(),
rtl::OUStringToOString(
theDestinationUri.GetPath(),
RTL_TEXTENCODING_UTF8 ).getStr() );
HandleError( theRetVal, inSourceURL, rEnv );
}
// -------------------------------------------------------------------
// MOVE
// -------------------------------------------------------------------
void NeonSession::MOVE( const rtl::OUString & inSourceURL,
const rtl::OUString & inDestinationURL,
const DAVRequestEnvironment & rEnv,
sal_Bool inOverWrite )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
NeonUri theSourceUri( inSourceURL );
NeonUri theDestinationUri( inDestinationURL );
int theRetVal = ne_move( m_pHttpSession,
inOverWrite ? 1 : 0,
rtl::OUStringToOString(
theSourceUri.GetPath(),
RTL_TEXTENCODING_UTF8 ).getStr(),
rtl::OUStringToOString(
theDestinationUri.GetPath(),
RTL_TEXTENCODING_UTF8 ).getStr() );
HandleError( theRetVal, inSourceURL, rEnv );
}
// -------------------------------------------------------------------
// DESTROY
// -------------------------------------------------------------------
void NeonSession::DESTROY( const rtl::OUString & inPath,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
int theRetVal = ne_delete( m_pHttpSession,
rtl::OUStringToOString(
inPath, RTL_TEXTENCODING_UTF8 ).getStr() );
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
namespace
{
sal_Int32 lastChanceToSendRefreshRequest( TimeValue const & rStart,
int timeout )
{
TimeValue aEnd;
osl_getSystemTime( &aEnd );
// Try to estimate a safe absolute time for sending the
// lock refresh request.
sal_Int32 lastChanceToSendRefreshRequest = -1;
if ( timeout != NE_TIMEOUT_INFINITE )
{
sal_Int32 calltime = aEnd.Seconds - rStart.Seconds;
if ( calltime <= timeout )
{
lastChanceToSendRefreshRequest
= aEnd.Seconds + timeout - calltime;
}
else
{
OSL_TRACE( "No chance to refresh lock before timeout!" );
}
}
return lastChanceToSendRefreshRequest;
}
} // namespace
// -------------------------------------------------------------------
// LOCK (set new lock)
// -------------------------------------------------------------------
void NeonSession::LOCK( const ::rtl::OUString & inPath,
ucb::Lock & rLock,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
Init( rEnv );
/* Create a depth zero, exclusive write lock, with default timeout
* (allowing a server to pick a default). token, owner and uri are
* unset. */
NeonLock * theLock = ne_lock_create();
// Set the lock uri
ne_uri aUri;
ne_uri_parse( rtl::OUStringToOString( makeAbsoluteURL( inPath ),
RTL_TEXTENCODING_UTF8 ).getStr(),
&aUri );
theLock->uri = aUri;
// Set the lock depth
switch( rLock.Depth )
{
case ucb::LockDepth_ZERO:
theLock->depth = NE_DEPTH_ZERO;
break;
case ucb::LockDepth_ONE:
theLock->depth = NE_DEPTH_ONE;
break;
case ucb::LockDepth_INFINITY:
theLock->depth = NE_DEPTH_INFINITE;
break;
default:
throw DAVException( DAVException::DAV_INVALID_ARG );
}
// Set the lock scope
switch ( rLock.Scope )
{
case ucb::LockScope_EXCLUSIVE:
theLock->scope = ne_lockscope_exclusive;
break;
case ucb::LockScope_SHARED:
theLock->scope = ne_lockscope_shared;
break;
default:
throw DAVException( DAVException::DAV_INVALID_ARG );
}
// Set the lock timeout
theLock->timeout = (long)rLock.Timeout;
// Set the lock owner
rtl::OUString aValue;
rLock.Owner >>= aValue;
theLock->owner =
ne_strdup( rtl::OUStringToOString( aValue,
RTL_TEXTENCODING_UTF8 ).getStr() );
TimeValue startCall;
osl_getSystemTime( &startCall );
int theRetVal = ne_lock( m_pHttpSession, theLock );
if ( theRetVal == NE_OK )
{
m_aNeonLockStore.addLock( theLock,
this,
lastChanceToSendRefreshRequest(
startCall, theLock->timeout ) );
uno::Sequence< rtl::OUString > aTokens( 1 );
aTokens[ 0 ] = rtl::OUString::createFromAscii( theLock->token );
rLock.LockTokens = aTokens;
OSL_TRACE( "NeonSession::LOCK: created lock for %s. token: %s",
rtl::OUStringToOString( makeAbsoluteURL( inPath ),
RTL_TEXTENCODING_UTF8 ).getStr(),
theLock->token );
}
else
{
ne_lock_destroy( theLock );
OSL_TRACE( "NeonSession::LOCK: obtaining lock for %s failed!",
rtl::OUStringToOString( makeAbsoluteURL( inPath ),
RTL_TEXTENCODING_UTF8 ).getStr() );
}
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// LOCK (refresh existing lock)
// -------------------------------------------------------------------
sal_Int64 NeonSession::LOCK( const ::rtl::OUString & inPath,
sal_Int64 nTimeout,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
// Try to get the neon lock from lock store
NeonLock * theLock
= m_aNeonLockStore.findByUri( makeAbsoluteURL( inPath ) );
if ( !theLock )
throw DAVException( DAVException::DAV_NOT_LOCKED );
Init( rEnv );
// refresh existing lock.
theLock->timeout = static_cast< long >( nTimeout );
TimeValue startCall;
osl_getSystemTime( &startCall );
int theRetVal = ne_lock_refresh( m_pHttpSession, theLock );
if ( theRetVal == NE_OK )
{
m_aNeonLockStore.updateLock( theLock,
lastChanceToSendRefreshRequest(
startCall, theLock->timeout ) );
}
HandleError( theRetVal, inPath, rEnv );
return theLock->timeout;
}
// -------------------------------------------------------------------
// LOCK (refresh existing lock)
// -------------------------------------------------------------------
bool NeonSession::LOCK( NeonLock * pLock,
sal_Int32 & rlastChanceToSendRefreshRequest )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
#if OSL_DEBUG_LEVEL > 0
char * p = ne_uri_unparse( &(pLock->uri) );
OSL_TRACE( "NeonSession::LOCK: Refreshing lock for %s.", p );
ne_free( p );
#endif
// refresh existing lock.
TimeValue startCall;
osl_getSystemTime( &startCall );
if ( ne_lock_refresh( m_pHttpSession, pLock ) == NE_OK )
{
rlastChanceToSendRefreshRequest
= lastChanceToSendRefreshRequest( startCall, pLock->timeout );
OSL_TRACE( "Lock successfully refreshed." );
return true;
}
else
{
OSL_TRACE( "Lock not refreshed!" );
return false;
}
}
// -------------------------------------------------------------------
// UNLOCK
// -------------------------------------------------------------------
void NeonSession::UNLOCK( const ::rtl::OUString & inPath,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
// get the neon lock from lock store
NeonLock * theLock
= m_aNeonLockStore.findByUri( makeAbsoluteURL( inPath ) );
if ( !theLock )
throw DAVException( DAVException::DAV_NOT_LOCKED );
Init( rEnv );
int theRetVal = ne_unlock( m_pHttpSession, theLock );
if ( theRetVal == NE_OK )
{
m_aNeonLockStore.removeLock( theLock );
ne_lock_destroy( theLock );
}
else
{
OSL_TRACE( "NeonSession::UNLOCK: unlocking of %s failed.",
rtl::OUStringToOString( makeAbsoluteURL( inPath ),
RTL_TEXTENCODING_UTF8 ).getStr() );
}
HandleError( theRetVal, inPath, rEnv );
}
// -------------------------------------------------------------------
// UNLOCK
// -------------------------------------------------------------------
bool NeonSession::UNLOCK( NeonLock * pLock )
{
osl::Guard< osl::Mutex > theGuard( m_aMutex );
#if OSL_DEBUG_LEVEL > 0
char * p = ne_uri_unparse( &(pLock->uri) );
OSL_TRACE( "NeonSession::UNLOCK: Unlocking %s.", p );
ne_free( p );
#endif
if ( ne_unlock( m_pHttpSession, pLock ) == NE_OK )
{
OSL_TRACE( "UNLOCK succeeded." );
return true;
}
else
{
OSL_TRACE( "UNLOCK failed!" );
return false;
}
}
// -------------------------------------------------------------------
void NeonSession::abort()
throw ( DAVException )
{
if ( m_pHttpSession )
{
osl::Guard< osl::Mutex > theGlobalGuard( aGlobalNeonMutex );
ne_close_connection( m_pHttpSession );
}
}
// -------------------------------------------------------------------
const ucbhelper::InternetProxyServer & NeonSession::getProxySettings() const
{
if ( m_aScheme.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "http" ) ) ||
m_aScheme.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "https" ) ) )
{
return m_rProxyDecider.getProxy( m_aScheme,
m_aHostName,
m_nPort );
}
else
{
return m_rProxyDecider.getProxy( m_aScheme,
rtl::OUString() /* not used */,
-1 /* not used */ );
}
}
// -------------------------------------------------------------------
namespace {
bool containsLocktoken( const uno::Sequence< ucb::Lock > & rLocks,
const char * token )
{
for ( sal_Int32 n = 0; n < rLocks.getLength(); ++n )
{
const uno::Sequence< rtl::OUString > & rTokens
= rLocks[ n ].LockTokens;
for ( sal_Int32 m = 0; m < rTokens.getLength(); ++m )
{
if ( rTokens[ m ].equalsAscii( token ) )
return true;
}
}
return false;
}
} // namespace
// -------------------------------------------------------------------
bool NeonSession::removeExpiredLocktoken( const rtl::OUString & inURL,
const DAVRequestEnvironment & rEnv )
{
NeonLock * theLock = m_aNeonLockStore.findByUri( inURL );
if ( !theLock )
return false;
// do a lockdiscovery to check whether this lock is still valid.
try
{
// @@@ Alternative: use ne_lock_discover() => less overhead
std::vector< DAVResource > aResources;
std::vector< rtl::OUString > aPropNames;
aPropNames.push_back( DAVProperties::LOCKDISCOVERY );
PROPFIND( rEnv.m_aRequestURI, DAVZERO, aPropNames, aResources, rEnv );
if ( aResources.empty() )
return false;
std::vector< DAVPropertyValue >::const_iterator it
= aResources[ 0 ].properties.begin();
std::vector< DAVPropertyValue >::const_iterator end
= aResources[ 0 ].properties.end();
while ( it != end )
{
if ( (*it).Name.equals( DAVProperties::LOCKDISCOVERY ) )
{
uno::Sequence< ucb::Lock > aLocks;
if ( !( (*it).Value >>= aLocks ) )
return false;
if ( !containsLocktoken( aLocks, theLock->token ) )
{
// expired!
break;
}
// still valid.
return false;
}
++it;
}
// No lockdiscovery prop in propfind result / locktoken not found
// in propfind result -> not locked
OSL_TRACE( "NeonSession::removeExpiredLocktoken: Removing "
" expired lock token for %s. token: %s",
rtl::OUStringToOString( inURL,
RTL_TEXTENCODING_UTF8 ).getStr(),
theLock->token );
m_aNeonLockStore.removeLock( theLock );
ne_lock_destroy( theLock );
return true;
}
catch ( DAVException const & )
{
}
return false;
}
// -------------------------------------------------------------------
// HandleError
// Common Error Handler
// -------------------------------------------------------------------
void NeonSession::HandleError( int nError,
const rtl::OUString & inPath,
const DAVRequestEnvironment & rEnv )
throw ( DAVException )
{
m_aEnv = DAVRequestEnvironment();
// Map error code to DAVException.
switch ( nError )
{
case NE_OK:
return;
case NE_ERROR: // Generic error
{
rtl::OUString aText = rtl::OUString::createFromAscii(
ne_get_error( m_pHttpSession ) );
sal_uInt16 code = makeStatusCode( aText );
if ( code == SC_LOCKED )
{
if ( m_aNeonLockStore.findByUri(
makeAbsoluteURL( inPath ) ) == 0 )
{
// locked by 3rd party
throw DAVException( DAVException::DAV_LOCKED );
}
else
{
// locked by ourself
throw DAVException( DAVException::DAV_LOCKED_SELF );
}
}
// Special handling for 400 and 412 status codes, which may indicate
// that a lock previously obtained by us has been released meanwhile
// by the server. Unfortunately, RFC is not clear at this point,
// thus server implementations behave different...
else if ( code == SC_BAD_REQUEST || code == SC_PRECONDITION_FAILED )
{
if ( removeExpiredLocktoken( makeAbsoluteURL( inPath ), rEnv ) )
throw DAVException( DAVException::DAV_LOCK_EXPIRED );
}
throw DAVException( DAVException::DAV_HTTP_ERROR, aText, code );
}
case NE_LOOKUP: // Name lookup failed.
throw DAVException( DAVException::DAV_HTTP_LOOKUP,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
case NE_AUTH: // User authentication failed on server
throw DAVException( DAVException::DAV_HTTP_AUTH,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
case NE_PROXYAUTH: // User authentication failed on proxy
throw DAVException( DAVException::DAV_HTTP_AUTHPROXY,
NeonUri::makeConnectionEndPointString(
m_aProxyName, m_nProxyPort ) );
case NE_CONNECT: // Could not connect to server
throw DAVException( DAVException::DAV_HTTP_CONNECT,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
case NE_TIMEOUT: // Connection timed out
throw DAVException( DAVException::DAV_HTTP_TIMEOUT,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
case NE_FAILED: // The precondition failed
throw DAVException( DAVException::DAV_HTTP_FAILED,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
case NE_RETRY: // Retry request (ne_end_request ONLY)
throw DAVException( DAVException::DAV_HTTP_RETRY,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
case NE_REDIRECT:
{
NeonUri aUri( ne_redirect_location( m_pHttpSession ) );
throw DAVException(
DAVException::DAV_HTTP_REDIRECT, aUri.GetURI() );
}
default:
{
OSL_TRACE( "NeonSession::HandleError : Unknown Neon error code!" );
throw DAVException( DAVException::DAV_HTTP_ERROR,
rtl::OUString::createFromAscii(
ne_get_error( m_pHttpSession ) ) );
}
}
}
// -------------------------------------------------------------------
namespace {
void runResponseHeaderHandler( void * userdata,
const char * value )
{
rtl::OUString aHeader( rtl::OUString::createFromAscii( value ) );
sal_Int32 nPos = aHeader.indexOf( ':' );
if ( nPos != -1 )
{
rtl::OUString aHeaderName( aHeader.copy( 0, nPos ) );
NeonRequestContext * pCtx
= static_cast< NeonRequestContext * >( userdata );
// Note: Empty vector means that all headers are requested.
bool bIncludeIt = ( pCtx->pHeaderNames->size() == 0 );
if ( !bIncludeIt )
{
// Check whether this header was requested.
std::vector< ::rtl::OUString >::const_iterator it(
pCtx->pHeaderNames->begin() );
const std::vector< ::rtl::OUString >::const_iterator end(
pCtx->pHeaderNames->end() );
while ( it != end )
{
// header names are case insensitive
if ( (*it).equalsIgnoreAsciiCase( aHeaderName ) )
{
aHeaderName = (*it);
break;
}
++it;
}
if ( it != end )
bIncludeIt = true;
}
if ( bIncludeIt )
{
// Create & set the PropertyValue
DAVPropertyValue thePropertyValue;
thePropertyValue.IsCaseSensitive = false;
thePropertyValue.Name = aHeaderName;
if ( nPos < aHeader.getLength() )
thePropertyValue.Value <<= aHeader.copy( nPos + 1 ).trim();
// Add the newly created PropertyValue
pCtx->pResource->properties.push_back( thePropertyValue );
}
}
}
} // namespace
// -------------------------------------------------------------------
// static
int NeonSession::GET( ne_session * sess,
const char * uri,
ne_block_reader reader,
bool getheaders,
void * userdata )
{
//struct get_context ctx;
ne_request * req = ne_request_create( sess, "GET", uri );
int ret;
ne_decompress * dc
= ne_decompress_reader( req, ne_accept_2xx, reader, userdata );
{
osl::Guard< osl::Mutex > theGlobalGuard( aGlobalNeonMutex );
ret = ne_request_dispatch( req );
}
if ( getheaders )
{
void *cursor = NULL;
const char *name, *value;
while ( ( cursor = ne_response_header_iterate(
req, cursor, &name, &value ) ) != NULL )
{
char buffer[8192];
ne_snprintf(buffer, sizeof buffer, "%s: %s", name, value);
runResponseHeaderHandler(userdata, buffer);
}
}
if ( ret == NE_OK && ne_get_status( req )->klass != 2 )
ret = NE_ERROR;
if ( dc != 0 )
ne_decompress_destroy(dc);
ne_request_destroy( req );
return ret;
}
// -------------------------------------------------------------------
// static
int NeonSession::PUT( ne_session * sess,
const char * uri,
const char * buffer,
size_t size)
{
ne_request * req = ne_request_create( sess, "PUT", uri );
int ret;
ne_lock_using_resource( req, uri, 0 );
ne_lock_using_parent( req, uri );
ne_set_request_body_buffer( req, buffer, size );
{
osl::Guard< osl::Mutex > theGlobalGuard( aGlobalNeonMutex );
ret = ne_request_dispatch( req );
}
if ( ret == NE_OK && ne_get_status( req )->klass != 2 )
ret = NE_ERROR;
ne_request_destroy( req );
return ret;
}
// -------------------------------------------------------------------
int NeonSession::POST( ne_session * sess,
const char * uri,
const char * buffer,
ne_block_reader reader,
void * userdata,
const rtl::OUString & rContentType,
const rtl::OUString & rReferer )
{
ne_request * req = ne_request_create( sess, "POST", uri );
//struct get_context ctx;
int ret;
RequestDataMap * pData = 0;
if ( rContentType.getLength() || rReferer.getLength() )
{
// Remember contenttype and referer. Data will be added to HTTP request
// header in in 'PreSendRequest' callback.
pData = static_cast< RequestDataMap* >( m_pRequestData );
(*pData)[ req ] = RequestData( rContentType, rReferer );
}
//ctx.total = -1;
//ctx.fd = fd;
//ctx.error = 0;
//ctx.session = sess;
///* Read the value of the Content-Length header into ctx.total */
//ne_add_response_header_handler( req, "Content-Length",
// ne_handle_numeric_header, &ctx.total );
ne_add_response_body_reader( req, ne_accept_2xx, reader, userdata );
ne_set_request_body_buffer( req, buffer, strlen( buffer ) );
{
osl::Guard< osl::Mutex > theGlobalGuard( aGlobalNeonMutex );
ret = ne_request_dispatch( req );
}
//if ( ctx.error )
// ret = NE_ERROR;
//else
if ( ret == NE_OK && ne_get_status( req )->klass != 2 )
ret = NE_ERROR;
ne_request_destroy( req );
if ( pData )
{
// Remove request data from session's list.
RequestDataMap::iterator it = pData->find( req );
if ( it != pData->end() )
pData->erase( it );
}
return ret;
}
// -------------------------------------------------------------------
// static
bool
NeonSession::getDataFromInputStream(
const uno::Reference< io::XInputStream > & xStream,
uno::Sequence< sal_Int8 > & rData,
bool bAppendTrailingZeroByte )
{
if ( xStream.is() )
{
uno::Reference< io::XSeekable > xSeekable( xStream, uno::UNO_QUERY );
if ( xSeekable.is() )
{
try
{
sal_Int32 nSize
= sal::static_int_cast<sal_Int32>(xSeekable->getLength());
sal_Int32 nRead
= xStream->readBytes( rData, nSize );
if ( nRead == nSize )
{
if ( bAppendTrailingZeroByte )
{
rData.realloc( nSize + 1 );
rData[ nSize ] = sal_Int8( 0 );
}
return true;
}
}
catch ( io::NotConnectedException const & )
{
// readBytes
}
catch ( io::BufferSizeExceededException const & )
{
// readBytes
}
catch ( io::IOException const & )
{
// getLength, readBytes
}
}
else
{
try
{
uno::Sequence< sal_Int8 > aBuffer;
sal_Int32 nPos = 0;
sal_Int32 nRead = xStream->readSomeBytes( aBuffer, 65536 );
while ( nRead > 0 )
{
if ( rData.getLength() < ( nPos + nRead ) )
rData.realloc( nPos + nRead );
aBuffer.realloc( nRead );
rtl_copyMemory( (void*)( rData.getArray() + nPos ),
(const void*)aBuffer.getConstArray(),
nRead );
nPos += nRead;
aBuffer.realloc( 0 );
nRead = xStream->readSomeBytes( aBuffer, 65536 );
}
if ( bAppendTrailingZeroByte )
{
rData.realloc( nPos + 1 );
rData[ nPos ] = sal_Int8( 0 );
}
return true;
}
catch ( io::NotConnectedException const & )
{
// readBytes
}
catch ( io::BufferSizeExceededException const & )
{
// readBytes
}
catch ( io::IOException const & )
{
// readBytes
}
}
}
return false;
}
// ---------------------------------------------------------------------
sal_Bool
NeonSession::isDomainMatch( rtl::OUString certHostName )
{
rtl::OUString hostName = getHostName();
if (hostName.equalsIgnoreAsciiCase( certHostName ) )
return sal_True;
if ( 0 == certHostName.indexOf( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("*")) ) &&
hostName.getLength() >= certHostName.getLength() )
{
rtl::OUString cmpStr = certHostName.copy( 1 );
if ( hostName.matchIgnoreAsciiCase(
cmpStr, hostName.getLength() - cmpStr.getLength() ) )
return sal_True;
}
return sal_False;
}
// ---------------------------------------------------------------------
rtl::OUString NeonSession::makeAbsoluteURL( rtl::OUString const & rURL ) const
{
try
{
// Is URL relative or already absolute?
if ( rURL[ 0 ] != sal_Unicode( '/' ) )
{
// absolute.
return rtl::OUString( rURL );
}
else
{
ne_uri aUri;
memset( &aUri, 0, sizeof( aUri ) );
ne_fill_server_uri( m_pHttpSession, &aUri );
aUri.path
= ne_strdup( rtl::OUStringToOString(
rURL, RTL_TEXTENCODING_UTF8 ).getStr() );
NeonUri aNeonUri( &aUri );
ne_uri_free( &aUri );
return aNeonUri.GetURI();
}
}
catch ( DAVException const & )
{
}
// error.
return rtl::OUString();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|