/src/libreoffice/framework/source/services/urltransformer.cxx
Line | Count | Source |
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 <cppuhelper/implbase.hxx> |
21 | | #include <cppuhelper/supportsservice.hxx> |
22 | | #include <tools/urlobj.hxx> |
23 | | #include <rtl/ustrbuf.hxx> |
24 | | |
25 | | #include <com/sun/star/util/XURLTransformer.hpp> |
26 | | #include <com/sun/star/util/URL.hpp> |
27 | | #include <com/sun/star/lang/XServiceInfo.hpp> |
28 | | #include <com/sun/star/uno/XComponentContext.hpp> |
29 | | |
30 | | namespace { |
31 | | |
32 | | class URLTransformer : public ::cppu::WeakImplHelper< css::util::XURLTransformer, css::lang::XServiceInfo> |
33 | | { |
34 | | public: |
35 | 8.57k | URLTransformer() {} |
36 | | |
37 | | virtual OUString SAL_CALL getImplementationName() override |
38 | 0 | { |
39 | 0 | return u"com.sun.star.comp.framework.URLTransformer"_ustr; |
40 | 0 | } |
41 | | |
42 | | virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override |
43 | 0 | { |
44 | 0 | return cppu::supportsService(this, ServiceName); |
45 | 0 | } |
46 | | |
47 | | virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override |
48 | 0 | { |
49 | 0 | return {u"com.sun.star.util.URLTransformer"_ustr}; |
50 | 0 | } |
51 | | |
52 | | virtual sal_Bool SAL_CALL parseStrict( css::util::URL& aURL ) override; |
53 | | |
54 | | virtual sal_Bool SAL_CALL parseSmart( css::util::URL& aURL, const OUString& sSmartProtocol ) override; |
55 | | |
56 | | virtual sal_Bool SAL_CALL assemble( css::util::URL& aURL ) override; |
57 | | |
58 | | virtual OUString SAL_CALL getPresentation( const css::util::URL& aURL, sal_Bool bWithPassword ) override; |
59 | | }; |
60 | | |
61 | | void lcl_ParserHelper(INetURLObject& _rParser, css::util::URL& _rURL) |
62 | 58 | { |
63 | | // Get all information about this URL. |
64 | 58 | _rURL.Protocol = INetURLObject::GetScheme( _rParser.GetProtocol() ); |
65 | 58 | _rURL.User = _rParser.GetUser ( INetURLObject::DecodeMechanism::WithCharset ); |
66 | 58 | _rURL.Password = _rParser.GetPass ( INetURLObject::DecodeMechanism::WithCharset ); |
67 | 58 | _rURL.Server = _rParser.GetHost ( INetURLObject::DecodeMechanism::WithCharset ); |
68 | 58 | _rURL.Port = static_cast<sal_Int16>(_rParser.GetPort()); |
69 | | |
70 | 58 | sal_Int32 nCount = _rParser.getSegmentCount( false ); |
71 | 58 | if ( nCount > 0 ) |
72 | 48 | { |
73 | | // Don't add last segment as it is the name! |
74 | 48 | --nCount; |
75 | | |
76 | 48 | OUStringBuffer aPath(128); |
77 | 88 | for ( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++ ) |
78 | 40 | { |
79 | 40 | aPath.append( "/" |
80 | 40 | + _rParser.getName( nIndex, false, INetURLObject::DecodeMechanism::NONE )); |
81 | 40 | } |
82 | | |
83 | 48 | if ( nCount > 0 ) |
84 | 19 | aPath.append( '/' ); // final slash! |
85 | | |
86 | 48 | _rURL.Path = aPath.makeStringAndClear(); |
87 | 48 | _rURL.Name = _rParser.getName( INetURLObject::LAST_SEGMENT, false, INetURLObject::DecodeMechanism::NONE ); |
88 | 48 | } |
89 | 10 | else |
90 | 10 | { |
91 | 10 | _rURL.Path = _rParser.GetURLPath( INetURLObject::DecodeMechanism::NONE ); |
92 | 10 | _rURL.Name = _rParser.GetLastName(); |
93 | 10 | } |
94 | | |
95 | 58 | _rURL.Arguments = _rParser.GetParam(); |
96 | 58 | _rURL.Mark = _rParser.GetMark( INetURLObject::DecodeMechanism::WithCharset ); |
97 | | |
98 | | // INetURLObject supports only an intelligent method of parsing URL's. So write |
99 | | // back Complete to have a valid encoded URL in all cases! |
100 | 58 | _rURL.Complete = _rParser.GetMainURL( INetURLObject::DecodeMechanism::NONE ); |
101 | | |
102 | 58 | _rParser.SetMark( u"" ); |
103 | 58 | _rParser.SetParam( u"" ); |
104 | | |
105 | 58 | _rURL.Main = _rParser.GetMainURL( INetURLObject::DecodeMechanism::NONE ); |
106 | 58 | } |
107 | | |
108 | | // XURLTransformer |
109 | | sal_Bool SAL_CALL URLTransformer::parseStrict( css::util::URL& aURL ) |
110 | 64 | { |
111 | | // Safe impossible cases. |
112 | 64 | if ( aURL.Complete.isEmpty() ) |
113 | 0 | { |
114 | 0 | return false; |
115 | 0 | } |
116 | | // Try to extract the protocol |
117 | 64 | sal_Int32 nURLIndex = aURL.Complete.indexOf( ':' ); |
118 | 64 | if ( nURLIndex <= 1 ) |
119 | 4 | return false; |
120 | | |
121 | 60 | std::u16string_view aProtocol = aURL.Complete.subView( 0, nURLIndex+1 ); |
122 | | |
123 | | // If INetURLObject knows this protocol let it parse |
124 | 60 | if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INetProtocol::NotValid ) |
125 | 60 | { |
126 | | // Initialize parser with given URL. |
127 | 60 | INetURLObject aParser( aURL.Complete ); |
128 | | |
129 | | // Get all information about this URL. |
130 | 60 | INetProtocol eINetProt = aParser.GetProtocol(); |
131 | 60 | if ( eINetProt == INetProtocol::NotValid ) |
132 | 2 | { |
133 | 2 | return false; |
134 | 2 | } |
135 | 58 | else if ( !aParser.HasError() ) |
136 | 58 | { |
137 | 58 | lcl_ParserHelper(aParser,aURL); |
138 | | // Return "URL is parsed". |
139 | 58 | return true; |
140 | 58 | } |
141 | 60 | } |
142 | 0 | else |
143 | 0 | { |
144 | | // Minimal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented |
145 | | // in framework! |
146 | 0 | aURL.Protocol = aProtocol; |
147 | 0 | aURL.Main = aURL.Complete; |
148 | 0 | aURL.Path = aURL.Complete.copy( nURLIndex+1 ); |
149 | | |
150 | | // Return "URL is parsed". |
151 | 0 | return true; |
152 | 0 | } |
153 | | |
154 | 0 | return false; |
155 | 60 | } |
156 | | |
157 | | // XURLTransformer |
158 | | |
159 | | sal_Bool SAL_CALL URLTransformer::parseSmart( css::util::URL& aURL, |
160 | | const OUString& sSmartProtocol ) |
161 | 0 | { |
162 | | // Safe impossible cases. |
163 | 0 | if ( aURL.Complete.isEmpty() ) |
164 | 0 | { |
165 | 0 | return false; |
166 | 0 | } |
167 | | |
168 | | // Initialize parser with given URL. |
169 | 0 | INetURLObject aParser; |
170 | |
|
171 | 0 | aParser.SetSmartProtocol( INetURLObject::CompareProtocolScheme( sSmartProtocol )); |
172 | 0 | bool bOk = aParser.SetSmartURL( aURL.Complete ); |
173 | 0 | if ( bOk ) |
174 | 0 | { |
175 | 0 | lcl_ParserHelper(aParser,aURL); |
176 | | // Return "URL is parsed". |
177 | 0 | return true; |
178 | 0 | } |
179 | 0 | else |
180 | 0 | { |
181 | | // Minimal support for unknown protocols. This is mandatory to support the "Protocol Handlers" implemented |
182 | | // in framework! |
183 | 0 | if ( INetURLObject::CompareProtocolScheme( sSmartProtocol ) == INetProtocol::NotValid ) |
184 | 0 | { |
185 | | // Try to extract the protocol |
186 | 0 | sal_Int32 nIndex = aURL.Complete.indexOf( ':' ); |
187 | 0 | if ( nIndex > 1 ) |
188 | 0 | { |
189 | 0 | OUString aProtocol = aURL.Complete.copy( 0, nIndex+1 ); |
190 | | |
191 | | // If INetURLObject knows this protocol something is wrong as detected before => |
192 | | // give up and return false! |
193 | 0 | if ( INetURLObject::CompareProtocolScheme( aProtocol ) != INetProtocol::NotValid ) |
194 | 0 | return false; |
195 | 0 | else |
196 | 0 | aURL.Protocol = aProtocol; |
197 | 0 | } |
198 | 0 | else |
199 | 0 | return false; |
200 | | |
201 | 0 | aURL.Main = aURL.Complete; |
202 | 0 | aURL.Path = aURL.Complete.copy( nIndex+1 ); |
203 | 0 | return true; |
204 | 0 | } |
205 | 0 | else |
206 | 0 | return false; |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | | // XURLTransformer |
211 | | sal_Bool SAL_CALL URLTransformer::assemble( css::util::URL& aURL ) |
212 | 0 | { |
213 | | // Initialize parser. |
214 | 0 | INetURLObject aParser; |
215 | |
|
216 | 0 | if ( INetURLObject::CompareProtocolScheme( aURL.Protocol ) != INetProtocol::NotValid ) |
217 | 0 | { |
218 | 0 | OUStringBuffer aCompletePath( aURL.Path ); |
219 | | |
220 | | // Concat the name if it is provided, just support a final slash |
221 | 0 | if ( !aURL.Name.isEmpty() ) |
222 | 0 | { |
223 | 0 | sal_Int32 nIndex = aURL.Path.lastIndexOf( '/' ); |
224 | 0 | if ( nIndex == ( aURL.Path.getLength() -1 )) |
225 | 0 | aCompletePath.append( aURL.Name ); |
226 | 0 | else |
227 | 0 | { |
228 | 0 | aCompletePath.append( "/" + aURL.Name ); |
229 | 0 | } |
230 | 0 | } |
231 | |
|
232 | 0 | bool bResult = aParser.ConcatData( |
233 | 0 | INetURLObject::CompareProtocolScheme( aURL.Protocol ) , |
234 | 0 | aURL.User , |
235 | 0 | aURL.Password , |
236 | 0 | aURL.Server , |
237 | 0 | aURL.Port , |
238 | 0 | aCompletePath); |
239 | |
|
240 | 0 | if ( !bResult ) |
241 | 0 | return false; |
242 | | |
243 | | // First parse URL WITHOUT ... |
244 | 0 | aURL.Main = aParser.GetMainURL( INetURLObject::DecodeMechanism::NONE ); |
245 | | // ...and then WITH parameter and mark. |
246 | 0 | aParser.SetParam( aURL.Arguments); |
247 | 0 | aParser.SetMark ( aURL.Mark, INetURLObject::EncodeMechanism::All ); |
248 | 0 | aURL.Complete = aParser.GetMainURL( INetURLObject::DecodeMechanism::NONE ); |
249 | | |
250 | | // Return "URL is assembled". |
251 | 0 | return true; |
252 | 0 | } |
253 | 0 | else if ( !aURL.Protocol.isEmpty() ) |
254 | 0 | { |
255 | | // Minimal support for unknown protocols |
256 | 0 | aURL.Complete = aURL.Protocol + aURL.Path; |
257 | 0 | aURL.Main = aURL.Complete; |
258 | 0 | return true; |
259 | 0 | } |
260 | | |
261 | 0 | return false; |
262 | 0 | } |
263 | | |
264 | | // XURLTransformer |
265 | | |
266 | | OUString SAL_CALL URLTransformer::getPresentation( const css::util::URL& aURL, |
267 | | sal_Bool bWithPassword ) |
268 | 0 | { |
269 | | // Safe impossible cases. |
270 | 0 | if ( aURL.Complete.isEmpty() ) |
271 | 0 | { |
272 | 0 | return OUString(); |
273 | 0 | } |
274 | | |
275 | | // Check given URL |
276 | 0 | css::util::URL aTestURL = aURL; |
277 | 0 | bool bParseResult = parseSmart( aTestURL, aTestURL.Protocol ); |
278 | 0 | if ( bParseResult ) |
279 | 0 | { |
280 | 0 | if ( !bWithPassword && !aTestURL.Password.isEmpty() ) |
281 | 0 | { |
282 | | // Exchange password text with other placeholder string |
283 | 0 | aTestURL.Password = "<******>"; |
284 | 0 | assemble( aTestURL ); |
285 | 0 | } |
286 | | |
287 | | // Convert internal URLs to "praesentation"-URLs! |
288 | 0 | OUString sPraesentationURL; |
289 | 0 | INetURLObject::translateToExternal( aTestURL.Complete, sPraesentationURL, INetURLObject::DecodeMechanism::Unambiguous ); |
290 | |
|
291 | 0 | return sPraesentationURL; |
292 | 0 | } |
293 | 0 | else |
294 | 0 | return OUString(); |
295 | 0 | } |
296 | | |
297 | | } |
298 | | |
299 | | extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * |
300 | | com_sun_star_comp_framework_URLTransformer_get_implementation( |
301 | | css::uno::XComponentContext *, |
302 | | css::uno::Sequence<css::uno::Any> const &) |
303 | 8.57k | { |
304 | 8.57k | return cppu::acquire(new URLTransformer()); |
305 | 8.57k | } |
306 | | |
307 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |