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