LibreOffice Module ucb (master)  1
DAVProperties.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <string.h>
21 #include "DAVProperties.hxx"
22 #include <rtl/ustrbuf.hxx>
23 
24 using namespace http_dav_ucp;
25 
26 const OUString DAVProperties::CREATIONDATE =
27  OUString( "DAV:creationdate" );
28 const OUString DAVProperties::DISPLAYNAME =
29  OUString( "DAV:displayname" );
30 const OUString DAVProperties::GETCONTENTLANGUAGE =
31  OUString( "DAV:getcontentlanguage" );
32 const OUString DAVProperties::GETCONTENTLENGTH =
33  OUString( "DAV:getcontentlength" );
34 const OUString DAVProperties::GETCONTENTTYPE =
35  OUString( "DAV:getcontenttype" );
36 const OUString DAVProperties::GETETAG =
37  OUString( "DAV:getetag" );
38 const OUString DAVProperties::GETLASTMODIFIED =
39  OUString( "DAV:getlastmodified" );
40 const OUString DAVProperties::LOCKDISCOVERY =
41  OUString( "DAV:lockdiscovery" );
42 const OUString DAVProperties::RESOURCETYPE =
43  OUString( "DAV:resourcetype" );
44 const OUString DAVProperties::SUPPORTEDLOCK =
45  OUString( "DAV:supportedlock" );
46 
47 const OUString DAVProperties::EXECUTABLE =
48  OUString( "http://apache.org/dav/props/executable" );
49 
50 
51 // static
52 void DAVProperties::createSerfPropName( const OUString & rFullName,
53  SerfPropName & rName )
54 {
55  if ( rFullName.startsWith( "DAV:" ) )
56  {
57  rName.nspace = "DAV:";
58  rName.name
59  = strdup( OUStringToOString(
60  rFullName.copy( RTL_CONSTASCII_LENGTH( "DAV:" ) ),
61  RTL_TEXTENCODING_UTF8 ).getStr() );
62  }
63  else if ( rFullName.startsWith( "http://apache.org/dav/props/" ) )
64  {
65  rName.nspace = "http://apache.org/dav/props/";
66  rName.name
67  = strdup( OUStringToOString(
68  rFullName.copy(
69  RTL_CONSTASCII_LENGTH(
70  "http://apache.org/dav/props/" ) ),
71  RTL_TEXTENCODING_UTF8 ).getStr() );
72  }
73  else if ( rFullName.startsWith( "http://ucb.openoffice.org/dav/props/" ) )
74  {
75  rName.nspace = "http://ucb.openoffice.org/dav/props/";
76  rName.name
77  = strdup( OUStringToOString(
78  rFullName.copy(
79  RTL_CONSTASCII_LENGTH(
80  "http://ucb.openoffice.org/dav/props/" ) ),
81  RTL_TEXTENCODING_UTF8 ).getStr() );
82  }
83  else if ( rFullName.startsWith( "<prop:" ) )
84  {
85  // Support for 3rd party namespaces/props
86 
87  OString aFullName
88  = OUStringToOString( rFullName, RTL_TEXTENCODING_UTF8 );
89 
90  // Format: <prop:the_propname xmlns:prop="the_namespace">
91 
92  sal_Int32 nStart = RTL_CONSTASCII_LENGTH( "<prop:" );
93  sal_Int32 nLen = aFullName.indexOf( ' ' ) - nStart;
94  rName.name = strdup( aFullName.copy( nStart, nLen ).getStr() );
95 
96  nStart = aFullName.indexOf( '=', nStart + nLen ) + 2; // after ="
97  nLen = aFullName.getLength() - RTL_CONSTASCII_LENGTH( "\">" ) - nStart;
98  rName.nspace = strdup( aFullName.copy( nStart, nLen ).getStr() );
99  }
100  else
101  {
102  // Add our namespace to our own properties.
103  rName.nspace = "http://ucb.openoffice.org/dav/props/";
104  rName.name
105  = strdup( OUStringToOString( rFullName,
106  RTL_TEXTENCODING_UTF8 ).getStr() );
107  }
108 }
109 
110 
111 // static
112 void DAVProperties::createUCBPropName( const char * nspace,
113  const char * name,
114  OUString & rFullName )
115 {
116  OUString aNameSpace
117  = OStringToOUString( nspace, RTL_TEXTENCODING_UTF8 );
118  OUString aName
119  = OStringToOUString( name, RTL_TEXTENCODING_UTF8 );
120 
121  if ( !aNameSpace.getLength() )
122  {
123  // Some servers send XML without proper namespaces. Assume "DAV:"
124  // in this case, if name is a well-known dav property name.
125  // Although this is not 100% correct, it solves many problems.
126 
127  if ( DAVProperties::RESOURCETYPE.matchIgnoreAsciiCase( aName, 4 ) ||
128  DAVProperties::SUPPORTEDLOCK.matchIgnoreAsciiCase( aName, 4 ) ||
129  DAVProperties::LOCKDISCOVERY.matchIgnoreAsciiCase( aName, 4 ) ||
130  DAVProperties::CREATIONDATE.matchIgnoreAsciiCase( aName, 4 ) ||
131  DAVProperties::DISPLAYNAME.matchIgnoreAsciiCase( aName, 4 ) ||
132  DAVProperties::GETCONTENTLANGUAGE.matchIgnoreAsciiCase( aName, 4 ) ||
133  DAVProperties::GETCONTENTLENGTH.matchIgnoreAsciiCase( aName, 4 ) ||
134  DAVProperties::GETCONTENTTYPE.matchIgnoreAsciiCase( aName, 4 ) ||
135  DAVProperties::GETETAG.matchIgnoreAsciiCase( aName, 4 ) ||
136  DAVProperties::GETLASTMODIFIED.matchIgnoreAsciiCase( aName, 4 ) )
137  {
138  aNameSpace = "DAV:";
139  }
140  }
141 
142  // Note: Concatenating strings BEFORE comparing against known namespaces
143  // is important. See RFC 2815 ( 23.4.2 Meaning of Qualified Names ).
144  rFullName = aNameSpace;
145  rFullName += aName;
146 
147  if ( rFullName.startsWith( "DAV:" ) )
148  {
149  // Okay, Just concat strings.
150  }
151  else if ( rFullName.startsWith( "http://apache.org/dav/props/" ) )
152  {
153  // Okay, Just concat strings.
154  }
155  else if ( rFullName.startsWith( "http://ucb.openoffice.org/dav/props/" ) )
156  {
157  // Remove namespace from our own properties.
158  rFullName = rFullName.copy(
159  RTL_CONSTASCII_LENGTH(
160  "http://ucb.openoffice.org/dav/props/" ) );
161  }
162  else
163  {
164  // Create property name that encodes, namespace and name ( XML ).
165  rFullName = "<prop:";
166  rFullName += aName;
167  rFullName += " xmlns:prop=\"";
168  rFullName += aNameSpace;
169  rFullName += "\">";
170  }
171 }
172 
173 
174 // static
176 {
177  return ( rName.nspace &&
178  ( rtl_str_compareIgnoreAsciiCase(
179  rName.nspace, "http://ucb.openoffice.org/dav/props/" )
180  == 0 ) );
181 }
182 
183 bool DAVProperties::isUCBSpecialProperty(const OUString& rFullName, OUString& rParsedName)
184 {
185  sal_Int32 nLen = rFullName.getLength();
186  if ( nLen <= 0 ||
187  !rFullName.startsWith( "<prop:" ) ||
188  !rFullName.endsWith( "\">" ) )
189  return false;
190 
191  sal_Int32 nStart = RTL_CONSTASCII_LENGTH( "<prop:" );
192  sal_Int32 nEnd = rFullName.indexOf( ' ', nStart );
193  if ( nEnd == -1 )
194  return false;
195 
196  OUString sPropName = rFullName.copy( nStart, nEnd - nStart );
197  if ( !sPropName.getLength() )
198  return false;
199 
200  // TODO skip whitespaces?
201  if ( !rFullName.match( "xmlns:prop=\"", ++nEnd ) )
202  return false;
203 
204  nStart = nEnd + RTL_CONSTASCII_LENGTH( "xmlns:prop=\"" );
205  nEnd = rFullName.indexOf( '"', nStart );
206  if ( nEnd != nLen - RTL_CONSTASCII_LENGTH( "\">" ) )
207  return false;
208 
209  OUString sNamesp = rFullName.copy( nStart, nEnd - nStart );
210  if ( !( nLen = sNamesp.getLength() ) )
211  return false;
212 
213  OUStringBuffer aBuff( sNamesp );
214  if ( sNamesp[nLen - 1] != '/' )
215  aBuff.append( '/' );
216  aBuff.append( sPropName );
217  rParsedName = aBuff.makeStringAndClear();
218 
219  return rParsedName.getLength();
220 }
221 
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const OUString GETCONTENTTYPE
static const OUString RESOURCETYPE
static const OUString GETLASTMODIFIED
static const OUString CREATIONDATE
static const OUString EXECUTABLE
static void createUCBPropName(const char *nspace, const char *name, OUString &rFullName)
static const OUString DISPLAYNAME
static const OUString GETETAG
static const OUString LOCKDISCOVERY
static bool isUCBDeadProperty(const SerfPropName &rName)
static void createSerfPropName(const OUString &rFullName, SerfPropName &rName)
static const OUString GETCONTENTLENGTH
static const OUString SUPPORTEDLOCK
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
static bool isUCBSpecialProperty(const OUString &rFullName, OUString &rParsedName)
static const OUString GETCONTENTLANGUAGE
OString const aName