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_cppuhelper.hxx" 26 27 #include "cppuhelper/unourl.hxx" 28 29 #include "osl/diagnose.h" 30 #include "rtl/malformeduriexception.hxx" 31 #include "rtl/string.h" 32 #include "rtl/textenc.h" 33 #include "rtl/uri.h" 34 #include "rtl/uri.hxx" 35 #include "rtl/ustring.h" 36 #include "rtl/ustring.hxx" 37 #include "sal/types.h" 38 39 #include <map> 40 41 using cppu::UnoUrl; 42 using cppu::UnoUrlDescriptor; 43 44 namespace { 45 46 inline bool isAlphanum(sal_Unicode c) 47 { 48 return (c >= 0x30 && c <= 0x39) // '0'--'9' 49 || (c >= 0x41 && c <= 0x5A) // 'A'--'Z' 50 || (c >= 0x61 && c <= 0x7A); // 'a'--'z' 51 } 52 53 } 54 55 class UnoUrlDescriptor::Impl 56 { 57 public: 58 typedef std::map< rtl::OUString, rtl::OUString > Parameters; 59 60 rtl::OUString m_aDescriptor; 61 rtl::OUString m_aName; 62 Parameters m_aParameters; 63 64 /** @exception rtl::MalformedUriException 65 */ 66 explicit inline Impl(rtl::OUString const & m_aDescriptor); 67 68 inline Impl * clone() const { return new Impl(*this); } 69 }; 70 71 inline UnoUrlDescriptor::Impl::Impl(rtl::OUString const & rDescriptor) 72 { 73 m_aDescriptor = rDescriptor; 74 enum State { STATE_NAME0, STATE_NAME, STATE_KEY0, STATE_KEY, STATE_VALUE }; 75 State eState = STATE_NAME0; 76 sal_Int32 nStart = 0; 77 rtl::OUString aKey; 78 for (sal_Int32 i = 0;; ++i) 79 { 80 bool bEnd = i == rDescriptor.getLength(); 81 sal_Unicode c = bEnd ? 0 : rDescriptor.getStr()[i]; 82 switch (eState) 83 { 84 case STATE_NAME0: 85 if (bEnd || !isAlphanum(c)) 86 throw rtl::MalformedUriException( 87 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 88 "UNO URL contains bad descriptor name"))); 89 nStart = i; 90 eState = STATE_NAME; 91 break; 92 93 case STATE_NAME: 94 if (bEnd || c == 0x2C) // ',' 95 { 96 m_aName 97 = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase(); 98 eState = STATE_KEY0; 99 } 100 else if (!isAlphanum(c)) 101 throw rtl::MalformedUriException( 102 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 103 "UNO URL contains bad descriptor name"))); 104 break; 105 106 case STATE_KEY0: 107 if (bEnd || !isAlphanum(c)) 108 throw rtl::MalformedUriException( 109 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 110 "UNO URL contains bad parameter key"))); 111 nStart = i; 112 eState = STATE_KEY; 113 break; 114 115 case STATE_KEY: 116 if (c == 0x3D) // '=' 117 { 118 aKey = rDescriptor.copy(nStart, i - nStart).toAsciiLowerCase(); 119 nStart = i + 1; 120 eState = STATE_VALUE; 121 } 122 else if (bEnd || !isAlphanum(c)) 123 throw rtl::MalformedUriException( 124 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 125 "UNO URL contains bad parameter key"))); 126 break; 127 128 case STATE_VALUE: 129 if (bEnd || c == 0x2C) // ',' 130 { 131 if (!m_aParameters.insert( 132 Parameters::value_type( 133 aKey, 134 rtl::Uri::decode(rDescriptor.copy(nStart, 135 i - nStart), 136 rtl_UriDecodeWithCharset, 137 RTL_TEXTENCODING_UTF8))).second) 138 throw rtl::MalformedUriException( 139 rtl::OUString( 140 RTL_CONSTASCII_USTRINGPARAM( 141 "UNO URL contains duplicated parameter"))); 142 eState = STATE_KEY0; 143 } 144 break; 145 } 146 if (bEnd) 147 break; 148 } 149 } 150 151 UnoUrlDescriptor::UnoUrlDescriptor(rtl::OUString const & rDescriptor): 152 m_xImpl(new Impl(rDescriptor)) 153 {} 154 155 UnoUrlDescriptor::UnoUrlDescriptor(std::auto_ptr< Impl > & rImpl): 156 m_xImpl(rImpl) 157 {} 158 159 UnoUrlDescriptor::UnoUrlDescriptor(UnoUrlDescriptor const & rOther): 160 m_xImpl(rOther.m_xImpl->clone()) 161 {} 162 163 UnoUrlDescriptor::~UnoUrlDescriptor() 164 {} 165 166 UnoUrlDescriptor & UnoUrlDescriptor::operator =(UnoUrlDescriptor const & rOther) 167 { 168 m_xImpl.reset(rOther.m_xImpl->clone()); 169 return *this; 170 } 171 172 rtl::OUString const & UnoUrlDescriptor::getDescriptor() const 173 { 174 return m_xImpl->m_aDescriptor; 175 } 176 177 rtl::OUString const & UnoUrlDescriptor::getName() const 178 { 179 return m_xImpl->m_aName; 180 } 181 182 bool UnoUrlDescriptor::hasParameter(rtl::OUString const & rKey) const 183 { 184 return m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase()) 185 != m_xImpl->m_aParameters.end(); 186 } 187 188 rtl::OUString UnoUrlDescriptor::getParameter(rtl::OUString const & rKey) const 189 { 190 Impl::Parameters::const_iterator 191 aIt(m_xImpl->m_aParameters.find(rKey.toAsciiLowerCase())); 192 return aIt == m_xImpl->m_aParameters.end() ? rtl::OUString() : aIt->second; 193 } 194 195 class UnoUrl::Impl 196 { 197 public: 198 UnoUrlDescriptor m_aConnection; 199 UnoUrlDescriptor m_aProtocol; 200 rtl::OUString m_aObjectName; 201 202 inline Impl * clone() const { return new Impl(*this); } 203 204 /** @exception rtl::MalformedUriException 205 */ 206 static inline Impl * create(rtl::OUString const & rUrl); 207 208 private: 209 inline Impl(std::auto_ptr< UnoUrlDescriptor::Impl > & rConnection, 210 std::auto_ptr< UnoUrlDescriptor::Impl > & rProtocol, 211 rtl::OUString const & rObjectName); 212 }; 213 214 inline UnoUrl::Impl::Impl(std::auto_ptr< UnoUrlDescriptor::Impl > & rConnection, 215 std::auto_ptr< UnoUrlDescriptor::Impl > & rProtocol, 216 rtl::OUString const & rObjectName): 217 m_aConnection(rConnection), 218 m_aProtocol(rProtocol), 219 m_aObjectName(rObjectName) 220 {} 221 222 inline UnoUrl::Impl * UnoUrl::Impl::create(rtl::OUString const & rUrl) 223 { 224 if (!rUrl.matchIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM("uno:"), 0)) 225 throw rtl::MalformedUriException( 226 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 227 "UNO URL does not start with \"uno:\""))); 228 sal_Int32 i = RTL_CONSTASCII_LENGTH("uno:"); 229 sal_Int32 j = rUrl.indexOf(';', i); 230 if (j < 0) 231 throw rtl::MalformedUriException( 232 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 233 "UNO URL has too few semicolons"))); 234 std::auto_ptr< UnoUrlDescriptor::Impl > 235 xConnection(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i))); 236 i = j + 1; 237 j = rUrl.indexOf(0x3B, i); // ';' 238 if (j < 0) 239 throw rtl::MalformedUriException( 240 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 241 "UNO URL has too few semicolons"))); 242 std::auto_ptr< UnoUrlDescriptor::Impl > 243 xProtocol(new UnoUrlDescriptor::Impl(rUrl.copy(i, j - i))); 244 i = j + 1; 245 if (i == rUrl.getLength()) 246 throw rtl::MalformedUriException( 247 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 248 "UNO URL contains empty ObjectName"))); 249 for (j = i; j < rUrl.getLength(); ++j) 250 { 251 sal_Unicode c = rUrl.getStr()[j]; 252 if (!isAlphanum(c) && c != 0x21 && c != 0x24 // '!', '$' 253 && c != 0x26 && c != 0x27 && c != 0x28 // '&', ''', '(' 254 && c != 0x28 && c != 0x2A && c != 0x2B // ')', '*', '+' 255 && c != 0x2C && c != 0x2D && c != 0x2E // ',', '-', '.' 256 && c != 0x2F && c != 0x3A && c != 0x3D // '/', ':', '=' 257 && c != 0x3F && c != 0x40 && c != 0x5F // '?', '@', '_' 258 && c != 0x7E) // '~' 259 throw rtl::MalformedUriException( 260 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 261 "UNO URL contains invalid ObjectName"))); 262 } 263 return new Impl(xConnection, xProtocol, rUrl.copy(i)); 264 } 265 266 UnoUrl::UnoUrl(rtl::OUString const & rUrl): m_xImpl(Impl::create(rUrl)) 267 {} 268 269 UnoUrl::UnoUrl(UnoUrl const & rOther): m_xImpl(rOther.m_xImpl->clone()) 270 {} 271 272 UnoUrl::~UnoUrl() 273 {} 274 275 UnoUrl & UnoUrl::operator =(UnoUrl const & rOther) 276 { 277 m_xImpl.reset(rOther.m_xImpl->clone()); 278 return *this; 279 } 280 281 UnoUrlDescriptor const & UnoUrl::getConnection() const 282 { 283 return m_xImpl->m_aConnection; 284 } 285 286 UnoUrlDescriptor const & UnoUrl::getProtocol() const 287 { 288 return m_xImpl->m_aProtocol; 289 } 290 291 rtl::OUString const & UnoUrl::getObjectName() const 292 { 293 return m_xImpl->m_aObjectName; 294 } 295