Update git submodules
[LibreOffice.git] / ucb / source / ucp / package / pkguri.cxx
blobb1de54aa4756f2626ffe766a3822c9d6587fbce7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 /**************************************************************************
22 TODO
23 **************************************************************************
25 *************************************************************************/
27 #include <sal/config.h>
29 #include <string_view>
31 #include <comphelper/storagehelper.hxx>
33 #include "../inc/urihelper.hxx"
35 #include "pkguri.hxx"
37 using namespace package_ucp;
40 // PackageUri Implementation.
43 static void normalize( OUString& rURL )
45 sal_Int32 nPos = 0;
48 nPos = rURL.indexOf( '%', nPos );
49 if ( nPos != -1 )
51 if ( nPos < ( rURL.getLength() - 2 ) )
53 OUString aTmp = rURL.copy( nPos + 1, 2 );
54 rURL = rURL.replaceAt( nPos + 1, 2, aTmp.toAsciiUpperCase() );
55 nPos++;
59 while ( nPos != -1 );
63 void PackageUri::init() const
65 // Already inited?
66 if ( m_aUri.isEmpty() || !m_aPath.isEmpty() )
67 return;
69 // Note: Maybe it's a re-init, setUri only resets m_aPath!
70 m_aPackage.clear();
71 m_aParentUri.clear();
72 m_aName.clear();
73 m_aParam.clear();
74 m_aScheme.clear();
76 // URI must match at least: <scheme>://<non_empty_url_to_file>
77 if ( m_aUri.getLength() < PACKAGE_URL_SCHEME_LENGTH + 4 )
79 // error, but remember that we did an init().
80 m_aPath = "/";
81 return;
84 // Scheme must be followed by '://'
85 if ( ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH ] != ':' )
87 ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH + 1 ] != '/' )
89 ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH + 2 ] != '/' ) )
91 // error, but remember that we did an init().
92 m_aPath = "/";
93 return;
96 OUString aPureUri;
97 sal_Int32 nParam = m_aUri.indexOf( '?' );
98 if( nParam >= 0 )
100 m_aParam = m_aUri.copy( nParam );
101 aPureUri = m_aUri.copy( 0, nParam );
103 else
104 aPureUri = m_aUri;
106 // Scheme is case insensitive.
107 m_aScheme = aPureUri.copy(
108 0, PACKAGE_URL_SCHEME_LENGTH ).toAsciiLowerCase();
110 if ( m_aScheme == PACKAGE_URL_SCHEME || m_aScheme == PACKAGE_ZIP_URL_SCHEME )
112 if ( m_aScheme == PACKAGE_ZIP_URL_SCHEME )
114 m_aParam +=
115 ( !m_aParam.isEmpty()
116 ? std::u16string_view( u"&purezip" )
117 : std::u16string_view( u"?purezip" ) );
120 aPureUri = aPureUri.replaceAt( 0,
121 m_aScheme.getLength(),
122 m_aScheme );
124 sal_Int32 nStart = PACKAGE_URL_SCHEME_LENGTH + 3;
125 sal_Int32 nEnd = aPureUri.lastIndexOf( '/' );
126 if ( nEnd == PACKAGE_URL_SCHEME_LENGTH + 3 )
128 // Only <scheme>:/// - Empty authority
130 // error, but remember that we did an init().
131 m_aPath = "/";
132 return;
134 else if ( nEnd == ( aPureUri.getLength() - 1 ) )
136 if ( aPureUri[ aPureUri.getLength() - 2 ] == '/' )
138 // Only <scheme>://// or <scheme>://<something>
140 // error, but remember that we did an init().
141 m_aPath = "/";
142 return;
145 // Remove trailing slash.
146 aPureUri = aPureUri.copy( 0, nEnd );
150 nEnd = aPureUri.indexOf( '/', nStart );
151 if ( nEnd == -1 )
153 // root folder.
155 OUString aNormPackage = aPureUri.copy( nStart );
156 normalize( aNormPackage );
158 aPureUri = aPureUri.replaceAt(
159 nStart, aPureUri.getLength() - nStart, aNormPackage );
160 m_aPackage
161 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
162 m_aPath = "/";
163 m_aUri = m_aUri.replaceAt( 0,
164 ( nParam >= 0 )
165 ? nParam
166 : m_aUri.getLength(), aPureUri );
168 sal_Int32 nLastSlash = m_aPackage.lastIndexOf( '/' );
169 if ( nLastSlash != -1 )
170 m_aName = ::ucb_impl::urihelper::decodeSegment(
171 m_aPackage.copy( nLastSlash + 1 ) );
172 else
173 m_aName
174 = ::ucb_impl::urihelper::decodeSegment( m_aPackage );
176 else
178 m_aPath = aPureUri.copy( nEnd + 1 );
180 // Unexpected sequences of characters:
181 // - empty path segments
182 // - encoded slashes
183 // - parent folder segments ".."
184 // - current folder segments "."
185 if ( m_aPath.indexOf( "//" ) != -1
186 || m_aPath.indexOf( "%2F" ) != -1
187 || m_aPath.indexOf( "%2f" ) != -1
188 || ::comphelper::OStorageHelper::PathHasSegment( m_aPath, u".." )
189 || ::comphelper::OStorageHelper::PathHasSegment( m_aPath, u"." ) )
191 // error, but remember that we did an init().
192 m_aPath = "/";
193 return;
196 OUString aNormPackage = aPureUri.copy( nStart, nEnd - nStart );
197 normalize( aNormPackage );
199 aPureUri = aPureUri.replaceAt(
200 nStart, nEnd - nStart, aNormPackage );
201 aPureUri = aPureUri.replaceAt(
202 nEnd + 1,
203 aPureUri.getLength() - nEnd - 1,
204 ::ucb_impl::urihelper::encodeURI( m_aPath ) );
206 m_aPackage
207 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
208 m_aPath = ::ucb_impl::urihelper::decodeSegment( m_aPath );
209 m_aUri = m_aUri.replaceAt( 0,
210 ( nParam >= 0 )
211 ? nParam
212 : m_aUri.getLength(), aPureUri );
214 sal_Int32 nLastSlash = aPureUri.lastIndexOf( '/' );
215 if ( nLastSlash != -1 )
217 m_aParentUri = aPureUri.copy( 0, nLastSlash );
218 m_aName = ::ucb_impl::urihelper::decodeSegment(
219 aPureUri.copy( nLastSlash + 1 ) );
223 // success
224 m_bValid = true;
226 else
228 // error, but remember that we did an init().
229 m_aPath = "/";
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */