1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ucb.hxx"
26 
27 #include <string.h>
28 #include "DAVProperties.hxx"
29 
30 using namespace http_dav_ucp;
31 
32 const ::rtl::OUString DAVProperties::CREATIONDATE =
33                 ::rtl::OUString::createFromAscii( "DAV:creationdate" );
34 const ::rtl::OUString DAVProperties::DISPLAYNAME =
35                 ::rtl::OUString::createFromAscii( "DAV:displayname" );
36 const ::rtl::OUString DAVProperties::GETCONTENTLANGUAGE =
37                 ::rtl::OUString::createFromAscii( "DAV:getcontentlanguage" );
38 const ::rtl::OUString DAVProperties::GETCONTENTLENGTH =
39                 ::rtl::OUString::createFromAscii( "DAV:getcontentlength" );
40 const ::rtl::OUString DAVProperties::GETCONTENTTYPE =
41                 ::rtl::OUString::createFromAscii( "DAV:getcontenttype" );
42 const ::rtl::OUString DAVProperties::GETETAG =
43                 ::rtl::OUString::createFromAscii( "DAV:getetag" );
44 const ::rtl::OUString DAVProperties::GETLASTMODIFIED =
45                 ::rtl::OUString::createFromAscii( "DAV:getlastmodified" );
46 const ::rtl::OUString DAVProperties::LOCKDISCOVERY =
47                 ::rtl::OUString::createFromAscii( "DAV:lockdiscovery" );
48 const ::rtl::OUString DAVProperties::RESOURCETYPE =
49                 ::rtl::OUString::createFromAscii( "DAV:resourcetype" );
50 const ::rtl::OUString DAVProperties::SUPPORTEDLOCK =
51                 ::rtl::OUString::createFromAscii( "DAV:supportedlock" );
52 
53 const ::rtl::OUString DAVProperties::EXECUTABLE =
54                 ::rtl::OUString::createFromAscii(
55                                 "http://apache.org/dav/props/executable" );
56 
57 // -------------------------------------------------------------------
58 // static
59 void DAVProperties::createSerfPropName( const rtl::OUString & rFullName,
60                                         SerfPropName & rName )
61 {
62     if ( rFullName.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "DAV:" ) ) == 0 )
63     {
64         rName.nspace = "DAV:";
65         rName.name
66             = strdup( rtl::OUStringToOString(
67                         rFullName.copy( RTL_CONSTASCII_LENGTH( "DAV:" ) ),
68                                         RTL_TEXTENCODING_UTF8 ) );
69     }
70     else if ( rFullName.compareToAscii( RTL_CONSTASCII_STRINGPARAM(
71                 "http://apache.org/dav/props/" ) ) == 0 )
72     {
73         rName.nspace = "http://apache.org/dav/props/";
74         rName.name
75             = strdup( rtl::OUStringToOString(
76                         rFullName.copy(
77                             RTL_CONSTASCII_LENGTH(
78                                 "http://apache.org/dav/props/" ) ),
79                             RTL_TEXTENCODING_UTF8 ) );
80     }
81     else if ( rFullName.compareToAscii( RTL_CONSTASCII_STRINGPARAM(
82                 "http://ucb.openoffice.org/dav/props/" ) ) == 0 )
83     {
84         rName.nspace = "http://ucb.openoffice.org/dav/props/";
85         rName.name
86             = strdup( rtl::OUStringToOString(
87                         rFullName.copy(
88                             RTL_CONSTASCII_LENGTH(
89                                 "http://ucb.openoffice.org/dav/props/" ) ),
90                             RTL_TEXTENCODING_UTF8 ) );
91     }
92     else if ( rFullName.compareToAscii( RTL_CONSTASCII_STRINGPARAM(
93                 "<prop:" ) ) == 0 )
94     {
95         // Support for 3rd party namespaces/props
96 
97         rtl::OString aFullName
98             = rtl::OUStringToOString( rFullName, RTL_TEXTENCODING_UTF8 );
99 
100         // Format: <prop:the_propname xmlns:prop="the_namespace">
101 
102         sal_Int32 nStart = RTL_CONSTASCII_LENGTH( "<prop:" );
103         sal_Int32 nLen = aFullName.indexOf( ' ' ) - nStart;
104         rName.name = strdup( aFullName.copy( nStart, nLen ) );
105 
106         nStart = aFullName.indexOf( '=', nStart + nLen ) + 2; // after ="
107         nLen = aFullName.getLength() - RTL_CONSTASCII_LENGTH( "\">" ) - nStart;
108         rName.nspace = strdup( aFullName.copy( nStart, nLen ) );
109     }
110     else
111     {
112         // Add our namespace to our own properties.
113         rName.nspace = "http://ucb.openoffice.org/dav/props/";
114         rName.name
115             = strdup( rtl::OUStringToOString( rFullName,
116                                               RTL_TEXTENCODING_UTF8 ) );
117     }
118 }
119 
120 // -------------------------------------------------------------------
121 // static
122 void DAVProperties::createUCBPropName( const char * nspace,
123                                        const char * name,
124                                        rtl::OUString & rFullName )
125 {
126     rtl::OUString aNameSpace
127         = rtl::OStringToOUString( nspace, RTL_TEXTENCODING_UTF8 );
128     rtl::OUString aName
129         = rtl::OStringToOUString( name,   RTL_TEXTENCODING_UTF8 );
130 
131     if ( !aNameSpace.getLength() )
132     {
133         // Some servers send XML without proper namespaces. Assume "DAV:"
134         // in this case, if name is a well-known dav property name.
135         // Although this is not 100% correct, it solves many problems.
136 
137         if ( DAVProperties::RESOURCETYPE.matchIgnoreAsciiCase( aName, 4 )  ||
138              DAVProperties::SUPPORTEDLOCK.matchIgnoreAsciiCase( aName, 4 ) ||
139              DAVProperties::LOCKDISCOVERY.matchIgnoreAsciiCase( aName, 4 ) ||
140              DAVProperties::CREATIONDATE.matchIgnoreAsciiCase( aName, 4 ) ||
141              DAVProperties::DISPLAYNAME.matchIgnoreAsciiCase( aName, 4 ) ||
142              DAVProperties::GETCONTENTLANGUAGE.matchIgnoreAsciiCase( aName, 4 ) ||
143              DAVProperties::GETCONTENTLENGTH.matchIgnoreAsciiCase( aName, 4 ) ||
144              DAVProperties::GETCONTENTTYPE.matchIgnoreAsciiCase( aName, 4 ) ||
145              DAVProperties::GETETAG.matchIgnoreAsciiCase( aName, 4 ) ||
146              DAVProperties::GETLASTMODIFIED.matchIgnoreAsciiCase( aName, 4 ) )
147         {
148             aNameSpace = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DAV:" ) );
149         }
150     }
151 
152     // Note: Concatenating strings BEFORE comparing against known namespaces
153     //       is important. See RFC 2815 ( 23.4.2 Meaning of Qualified Names ).
154     rFullName  = aNameSpace;
155     rFullName += aName;
156 
157     if ( rFullName.compareToAscii( RTL_CONSTASCII_STRINGPARAM(
158                 "DAV:" ) ) == 0 )
159     {
160         // Okay, Just concat strings.
161     }
162     else if ( rFullName.compareToAscii( RTL_CONSTASCII_STRINGPARAM(
163                 "http://apache.org/dav/props/" ) ) == 0 )
164     {
165         // Okay, Just concat strings.
166     }
167     else if ( rFullName.compareToAscii( RTL_CONSTASCII_STRINGPARAM(
168                 "http://ucb.openoffice.org/dav/props/" ) ) == 0 )
169     {
170         // Remove namespace from our own properties.
171         rFullName = rFullName.copy(
172                         RTL_CONSTASCII_LENGTH(
173                             "http://ucb.openoffice.org/dav/props/" ) );
174     }
175     else
176     {
177         // Create property name that encodes, namespace and name ( XML ).
178         rFullName  = rtl::OUString::createFromAscii( "<prop:" );
179         rFullName += aName;
180         rFullName += rtl::OUString::createFromAscii( " xmlns:prop=\"" );
181         rFullName += aNameSpace;
182         rFullName += rtl::OUString::createFromAscii( "\">" );
183     }
184 }
185 
186 // -------------------------------------------------------------------
187 // static
188 bool DAVProperties::isUCBDeadProperty( const SerfPropName & rName )
189 {
190     return ( rName.nspace &&
191              ( rtl_str_compareIgnoreAsciiCase(
192                  rName.nspace, "http://ucb.openoffice.org/dav/props/" )
193                == 0 ) );
194 }
195