1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 /**************************************************************************
23 **************************************************************************
25 *************************************************************************/
27 #include <sal/config.h>
29 #include <string_view>
31 #include <comphelper/storagehelper.hxx>
33 #include "../inc/urihelper.hxx"
37 using namespace package_ucp
;
40 // PackageUri Implementation.
43 static void normalize( OUString
& rURL
)
48 nPos
= rURL
.indexOf( '%', nPos
);
51 if ( nPos
< ( rURL
.getLength() - 2 ) )
53 OUString aTmp
= rURL
.copy( nPos
+ 1, 2 );
54 rURL
= rURL
.replaceAt( nPos
+ 1, 2, aTmp
.toAsciiUpperCase() );
63 void PackageUri::init() const
66 if ( m_aUri
.isEmpty() || !m_aPath
.isEmpty() )
69 // Note: Maybe it's a re-init, setUri only resets m_aPath!
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().
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().
97 sal_Int32 nParam
= m_aUri
.indexOf( '?' );
100 m_aParam
= m_aUri
.copy( nParam
);
101 aPureUri
= m_aUri
.copy( 0, nParam
);
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
)
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(),
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().
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().
145 // Remove trailing slash.
146 aPureUri
= aPureUri
.copy( 0, nEnd
);
150 nEnd
= aPureUri
.indexOf( '/', nStart
);
155 OUString aNormPackage
= aPureUri
.copy( nStart
);
156 normalize( aNormPackage
);
158 aPureUri
= aPureUri
.replaceAt(
159 nStart
, aPureUri
.getLength() - nStart
, aNormPackage
);
161 = ::ucb_impl::urihelper::decodeSegment( aNormPackage
);
163 m_aUri
= m_aUri
.replaceAt( 0,
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 ) );
174 = ::ucb_impl::urihelper::decodeSegment( m_aPackage
);
178 m_aPath
= aPureUri
.copy( nEnd
+ 1 );
180 // Unexpected sequences of characters:
181 // - empty path segments
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().
196 OUString aNormPackage
= aPureUri
.copy( nStart
, nEnd
- nStart
);
197 normalize( aNormPackage
);
199 aPureUri
= aPureUri
.replaceAt(
200 nStart
, nEnd
- nStart
, aNormPackage
);
201 aPureUri
= aPureUri
.replaceAt(
203 aPureUri
.getLength() - nEnd
- 1,
204 ::ucb_impl::urihelper::encodeURI( m_aPath
) );
207 = ::ucb_impl::urihelper::decodeSegment( aNormPackage
);
208 m_aPath
= ::ucb_impl::urihelper::decodeSegment( m_aPath
);
209 m_aUri
= m_aUri
.replaceAt( 0,
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 ) );
228 // error, but remember that we did an init().
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */