Line data Source code
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 :
21 : #include "stocservices.hxx"
22 :
23 : #include "UriReference.hxx"
24 :
25 : #include "boost/noncopyable.hpp"
26 : #include "com/sun/star/lang/IllegalArgumentException.hpp"
27 : #include "com/sun/star/lang/XServiceInfo.hpp"
28 : #include "com/sun/star/uno/Reference.hxx"
29 : #include "com/sun/star/uno/RuntimeException.hpp"
30 : #include "com/sun/star/uno/Sequence.hxx"
31 : #include "com/sun/star/uno/XInterface.hpp"
32 : #include "com/sun/star/uri/XUriReference.hpp"
33 : #include "com/sun/star/uri/XUriSchemeParser.hpp"
34 : #include "com/sun/star/uri/XVndSunStarScriptUrlReference.hpp"
35 : #include "cppuhelper/implbase1.hxx"
36 : #include "cppuhelper/implbase2.hxx"
37 : #include "cppuhelper/supportsservice.hxx"
38 : #include "cppuhelper/weak.hxx"
39 : #include "osl/mutex.hxx"
40 : #include "rtl/uri.hxx"
41 : #include "rtl/ustrbuf.hxx"
42 : #include "rtl/ustring.hxx"
43 : #include "sal/types.h"
44 :
45 : #include <exception>
46 :
47 : namespace {
48 :
49 0 : int getHexWeight(sal_Unicode c) {
50 0 : return c >= '0' && c <= '9' ? static_cast< int >(c - '0')
51 0 : : c >= 'A' && c <= 'F' ? static_cast< int >(c - 'A' + 10)
52 0 : : c >= 'a' && c <= 'f' ? static_cast< int >(c - 'a' + 10)
53 0 : : -1;
54 : }
55 :
56 0 : int parseEscaped(OUString const & part, sal_Int32 * index) {
57 0 : if (part.getLength() - *index < 3 || part[*index] != '%') {
58 0 : return -1;
59 : }
60 0 : int n1 = getHexWeight(part[*index + 1]);
61 0 : int n2 = getHexWeight(part[*index + 2]);
62 0 : if (n1 < 0 || n2 < 0) {
63 0 : return -1;
64 : }
65 0 : *index += 3;
66 0 : return (n1 << 4) | n2;
67 : }
68 :
69 0 : OUString parsePart(
70 : OUString const & part, bool namePart, sal_Int32 * index)
71 : {
72 0 : OUStringBuffer buf;
73 0 : while (*index < part.getLength()) {
74 0 : sal_Unicode c = part[*index];
75 0 : if (namePart ? c == '?' : c == '&' || c == '=') {
76 0 : break;
77 0 : } else if (c == '%') {
78 0 : sal_Int32 i = *index;
79 0 : int n = parseEscaped(part, &i);
80 0 : if (n >= 0 && n <= 0x7F) {
81 0 : buf.append(static_cast< sal_Unicode >(n));
82 0 : } else if (n >= 0xC0 && n <= 0xFC) {
83 : sal_Int32 encoded;
84 : int shift;
85 : sal_Int32 min;
86 0 : if (n <= 0xDF) {
87 0 : encoded = (n & 0x1F) << 6;
88 0 : shift = 0;
89 0 : min = 0x80;
90 0 : } else if (n <= 0xEF) {
91 0 : encoded = (n & 0x0F) << 12;
92 0 : shift = 6;
93 0 : min = 0x800;
94 0 : } else if (n <= 0xF7) {
95 0 : encoded = (n & 0x07) << 18;
96 0 : shift = 12;
97 0 : min = 0x10000;
98 0 : } else if (n <= 0xFB) {
99 0 : encoded = (n & 0x03) << 24;
100 0 : shift = 18;
101 0 : min = 0x200000;
102 : } else {
103 0 : encoded = 0;
104 0 : shift = 24;
105 0 : min = 0x4000000;
106 : }
107 0 : bool utf8 = true;
108 0 : for (; shift >= 0; shift -= 6) {
109 0 : n = parseEscaped(part, &i);
110 0 : if (n < 0x80 || n > 0xBF) {
111 0 : utf8 = false;
112 0 : break;
113 : }
114 0 : encoded |= (n & 0x3F) << shift;
115 : }
116 0 : if (!utf8 || encoded < min
117 0 : || (encoded >= 0xD800 && encoded <= 0xDFFF)
118 0 : || encoded > 0x10FFFF)
119 : {
120 : break;
121 : }
122 0 : if (encoded <= 0xFFFF) {
123 0 : buf.append(static_cast< sal_Unicode >(encoded));
124 : } else {
125 : buf.append(static_cast< sal_Unicode >(
126 0 : (encoded >> 10) | 0xD800));
127 : buf.append(static_cast< sal_Unicode >(
128 0 : (encoded & 0x3FF) | 0xDC00));
129 : }
130 : } else {
131 : break;
132 : }
133 0 : *index = i;
134 : } else {
135 0 : buf.append(c);
136 0 : ++*index;
137 : }
138 : }
139 0 : return buf.makeStringAndClear();
140 : }
141 :
142 : namespace
143 : {
144 0 : static OUString encodeNameOrParamFragment( OUString const & fragment )
145 : {
146 : static sal_Bool const aCharClass[] =
147 : { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* NameOrParamFragment */
148 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
149 : 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* !"#$%&'()*+,-./*/
150 : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, /*0123456789:;<=>?*/
151 : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*@ABCDEFGHIJKLMNO*/
152 : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, /*PQRSTUVWXYZ[\]^_*/
153 : 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*`abcdefghijklmno*/
154 : 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 /*pqrstuvwxyz{|}~ */
155 : };
156 :
157 : return rtl::Uri::encode(
158 : fragment,
159 : aCharClass,
160 : rtl_UriEncodeIgnoreEscapes,
161 : RTL_TEXTENCODING_UTF8
162 0 : );
163 : }
164 : }
165 :
166 0 : bool parseSchemeSpecificPart(OUString const & part) {
167 0 : sal_Int32 len = part.getLength();
168 0 : sal_Int32 i = 0;
169 0 : if (parsePart(part, true, &i).isEmpty() || part[0] == '/') {
170 0 : return false;
171 : }
172 0 : if (i == len) {
173 0 : return true;
174 : }
175 : for (;;) {
176 0 : ++i; // skip '?' or '&'
177 0 : if (parsePart(part, false, &i).isEmpty() || i == len
178 0 : || part[i] != '=')
179 : {
180 0 : return false;
181 : }
182 0 : ++i;
183 0 : parsePart(part, false, &i);
184 0 : if (i == len) {
185 0 : return true;
186 : }
187 0 : if (part[i] != '&') {
188 0 : return false;
189 : }
190 0 : }
191 : }
192 :
193 : class UrlReference:
194 : public cppu::WeakImplHelper1<css::uri::XVndSunStarScriptUrlReference>,
195 : private boost::noncopyable
196 : {
197 : public:
198 0 : UrlReference(OUString const & scheme, OUString const & path):
199 : m_base(
200 0 : scheme, false, false, OUString(), path, false, OUString())
201 0 : {}
202 :
203 0 : virtual OUString SAL_CALL getUriReference()
204 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
205 0 : { return m_base.getUriReference(); }
206 :
207 0 : virtual sal_Bool SAL_CALL isAbsolute()
208 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
209 0 : { return m_base.isAbsolute(); }
210 :
211 0 : virtual OUString SAL_CALL getScheme()
212 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
213 0 : { return m_base.getScheme(); }
214 :
215 0 : virtual OUString SAL_CALL getSchemeSpecificPart()
216 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
217 0 : { return m_base.getSchemeSpecificPart(); }
218 :
219 0 : virtual sal_Bool SAL_CALL isHierarchical()
220 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
221 0 : { return m_base.isHierarchical(); }
222 :
223 0 : virtual sal_Bool SAL_CALL hasAuthority()
224 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
225 0 : { return m_base.hasAuthority(); }
226 :
227 0 : virtual OUString SAL_CALL getAuthority()
228 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
229 0 : { return m_base.getAuthority(); }
230 :
231 0 : virtual OUString SAL_CALL getPath()
232 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
233 0 : { return m_base.getPath(); }
234 :
235 0 : virtual sal_Bool SAL_CALL hasRelativePath()
236 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
237 0 : { return m_base.hasRelativePath(); }
238 :
239 0 : virtual sal_Int32 SAL_CALL getPathSegmentCount()
240 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
241 0 : { return m_base.getPathSegmentCount(); }
242 :
243 0 : virtual OUString SAL_CALL getPathSegment(sal_Int32 index)
244 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
245 0 : { return m_base.getPathSegment(index); }
246 :
247 0 : virtual sal_Bool SAL_CALL hasQuery()
248 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
249 0 : { return m_base.hasQuery(); }
250 :
251 0 : virtual OUString SAL_CALL getQuery()
252 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
253 0 : { return m_base.getQuery(); }
254 :
255 0 : virtual sal_Bool SAL_CALL hasFragment()
256 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
257 0 : { return m_base.hasFragment(); }
258 :
259 0 : virtual OUString SAL_CALL getFragment()
260 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
261 0 : { return m_base.getFragment(); }
262 :
263 0 : virtual void SAL_CALL setFragment(OUString const & fragment)
264 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
265 0 : { m_base.setFragment(fragment); }
266 :
267 0 : virtual void SAL_CALL clearFragment()
268 : throw (com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
269 0 : { m_base.clearFragment(); }
270 :
271 : virtual OUString SAL_CALL getName() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
272 :
273 : virtual void SAL_CALL setName(OUString const & name)
274 : throw (css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception) SAL_OVERRIDE;
275 :
276 : virtual sal_Bool SAL_CALL hasParameter(OUString const & key)
277 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
278 :
279 : virtual OUString SAL_CALL getParameter(OUString const & key)
280 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
281 :
282 : virtual void SAL_CALL setParameter(OUString const & key, OUString const & value)
283 : throw (css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception) SAL_OVERRIDE;
284 :
285 : private:
286 0 : virtual ~UrlReference() {}
287 :
288 : sal_Int32 findParameter(OUString const & key);
289 :
290 : stoc::uriproc::UriReference m_base;
291 : };
292 :
293 0 : OUString UrlReference::getName() throw (css::uno::RuntimeException, std::exception) {
294 0 : osl::MutexGuard g(m_base.m_mutex);
295 0 : sal_Int32 i = 0;
296 0 : return parsePart(m_base.m_path, true, &i);
297 : }
298 :
299 0 : void SAL_CALL UrlReference::setName(OUString const & name) throw (css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception)
300 : {
301 0 : if (name.isEmpty())
302 : throw css::lang::IllegalArgumentException(
303 0 : OUString(), *this, 1);
304 :
305 0 : osl::MutexGuard g(m_base.m_mutex);
306 0 : sal_Int32 i = 0;
307 0 : parsePart(m_base.m_path, true, &i);
308 :
309 0 : OUStringBuffer newPath;
310 0 : newPath.append(encodeNameOrParamFragment(name));
311 0 : newPath.append(m_base.m_path.copy(i));
312 0 : m_base.m_path = newPath.makeStringAndClear();
313 0 : }
314 :
315 0 : sal_Bool UrlReference::hasParameter(OUString const & key)
316 : throw (css::uno::RuntimeException, std::exception)
317 : {
318 0 : osl::MutexGuard g(m_base.m_mutex);
319 0 : return findParameter(key) >= 0;
320 : }
321 :
322 0 : OUString UrlReference::getParameter(OUString const & key)
323 : throw (css::uno::RuntimeException, std::exception)
324 : {
325 0 : osl::MutexGuard g(m_base.m_mutex);
326 0 : sal_Int32 i = findParameter(key);
327 0 : return i >= 0 ? parsePart(m_base.m_path, false, &i) : OUString();
328 : }
329 :
330 0 : void UrlReference::setParameter(OUString const & key, OUString const & value)
331 : throw (css::uno::RuntimeException, css::lang::IllegalArgumentException, std::exception)
332 : {
333 0 : if (key.isEmpty())
334 : throw css::lang::IllegalArgumentException(
335 0 : OUString(), *this, 1);
336 :
337 0 : osl::MutexGuard g(m_base.m_mutex);
338 0 : sal_Int32 i = findParameter(key);
339 0 : bool bExistent = ( i>=0 );
340 0 : if (!bExistent) {
341 0 : i = m_base.m_path.getLength();
342 : }
343 :
344 0 : OUStringBuffer newPath;
345 0 : newPath.append(m_base.m_path.copy(0, i));
346 0 : if (!bExistent) {
347 0 : newPath.append( m_base.m_path.indexOf('?') < 0 ? '?' : '&' );
348 0 : newPath.append(encodeNameOrParamFragment(key));
349 0 : newPath.append('=');
350 : }
351 0 : newPath.append(encodeNameOrParamFragment(value));
352 0 : if (bExistent) {
353 : /*oldValue = */
354 0 : parsePart(m_base.m_path, false, &i); // skip key
355 0 : newPath.append(m_base.m_path.copy(i));
356 : }
357 :
358 0 : m_base.m_path = newPath.makeStringAndClear();
359 0 : }
360 :
361 0 : sal_Int32 UrlReference::findParameter(OUString const & key) {
362 0 : sal_Int32 i = 0;
363 0 : parsePart(m_base.m_path, true, &i); // skip name
364 : for (;;) {
365 0 : if (i == m_base.m_path.getLength()) {
366 0 : return -1;
367 : }
368 0 : ++i; // skip '?' or '&'
369 0 : OUString k = parsePart(m_base.m_path, false, &i);
370 0 : ++i; // skip '='
371 0 : if (k == key) {
372 0 : return i;
373 : }
374 0 : parsePart(m_base.m_path, false, &i); // skip value
375 0 : }
376 : }
377 :
378 : class Parser:
379 : public cppu::WeakImplHelper2<
380 : css::lang::XServiceInfo, css::uri::XUriSchemeParser>,
381 : private boost::noncopyable
382 : {
383 : public:
384 0 : Parser() {}
385 :
386 : virtual OUString SAL_CALL getImplementationName()
387 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
388 :
389 : virtual sal_Bool SAL_CALL supportsService(OUString const & serviceName)
390 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
391 :
392 : virtual css::uno::Sequence< OUString > SAL_CALL
393 : getSupportedServiceNames() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
394 :
395 : virtual css::uno::Reference< css::uri::XUriReference > SAL_CALL
396 : parse(
397 : OUString const & scheme, OUString const & schemeSpecificPart)
398 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
399 :
400 : private:
401 0 : virtual ~Parser() {}
402 : };
403 :
404 0 : OUString Parser::getImplementationName()
405 : throw (css::uno::RuntimeException, std::exception)
406 : {
407 : return stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTscript::
408 0 : getImplementationName();
409 : }
410 :
411 0 : sal_Bool Parser::supportsService(OUString const & serviceName)
412 : throw (css::uno::RuntimeException, std::exception)
413 : {
414 0 : return cppu::supportsService(this, serviceName);
415 : }
416 :
417 0 : css::uno::Sequence< OUString > Parser::getSupportedServiceNames()
418 : throw (css::uno::RuntimeException, std::exception)
419 : {
420 : return stoc_services::UriSchemeParser_vndDOTsunDOTstarDOTscript::
421 0 : getSupportedServiceNames();
422 : }
423 :
424 : css::uno::Reference< css::uri::XUriReference >
425 0 : Parser::parse(
426 : OUString const & scheme, OUString const & schemeSpecificPart)
427 : throw (css::uno::RuntimeException, std::exception)
428 : {
429 0 : if (!parseSchemeSpecificPart(schemeSpecificPart)) {
430 0 : return 0;
431 : }
432 0 : return new UrlReference(scheme, schemeSpecificPart);
433 : }
434 :
435 : }
436 :
437 : namespace stoc_services {
438 : namespace UriSchemeParser_vndDOTsunDOTstarDOTscript {
439 :
440 0 : css::uno::Reference< css::uno::XInterface > create(
441 : SAL_UNUSED_PARAMETER css::uno::Reference< css::uno::XComponentContext >
442 : const &)
443 : SAL_THROW((css::uno::Exception))
444 : {
445 : //TODO: single instance
446 0 : return static_cast< cppu::OWeakObject * >(new Parser);
447 : }
448 :
449 0 : OUString getImplementationName() {
450 0 : return OUString("com.sun.star.comp.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript");
451 : }
452 :
453 0 : css::uno::Sequence< OUString > getSupportedServiceNames() {
454 0 : css::uno::Sequence< OUString > s(1);
455 0 : s[0] = "com.sun.star.uri.UriSchemeParser_vndDOTsunDOTstarDOTscript";
456 0 : return s;
457 : }
458 :
459 : } }
460 :
461 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|