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 "rtl/ustrbuf.hxx"
28 #include "osl/diagnose.h"
29 #include "comphelper/storagehelper.hxx"
31 #include "../inc/urihelper.hxx"
35 using namespace package_ucp
;
41 // PackageUri Implementation.
46 static void normalize( OUString
& rURL
)
51 nPos
= rURL
.indexOf( '%', nPos
);
54 if ( nPos
< ( rURL
.getLength() - 2 ) )
56 OUString aTmp
= rURL
.copy( nPos
+ 1, 2 );
57 rURL
= rURL
.replaceAt( nPos
+ 1, 2, aTmp
.toAsciiUpperCase() );
66 void PackageUri::init() const
69 if ( !m_aUri
.isEmpty() && m_aPath
.isEmpty() )
71 // Note: Maybe it's a re-init, setUri only resets m_aPath!
78 // URI must match at least: <sheme>://<non_empty_url_to_file>
79 if ( ( m_aUri
.getLength() < PACKAGE_URL_SCHEME_LENGTH
+ 4 ) )
81 // error, but remember that we did a init().
86 // Scheme must be followed by '://'
87 if ( ( m_aUri
[ PACKAGE_URL_SCHEME_LENGTH
] != ':' )
89 ( m_aUri
[ PACKAGE_URL_SCHEME_LENGTH
+ 1 ] != '/' )
91 ( m_aUri
[ PACKAGE_URL_SCHEME_LENGTH
+ 2 ] != '/' ) )
93 // error, but remember that we did a init().
99 sal_Int32 nParam
= m_aUri
.indexOf( '?' );
102 m_aParam
= m_aUri
.copy( nParam
);
103 aPureUri
= m_aUri
.copy( 0, nParam
);
108 // Scheme is case insensitive.
109 m_aScheme
= aPureUri
.copy(
110 0, PACKAGE_URL_SCHEME_LENGTH
).toAsciiLowerCase();
112 if ( m_aScheme
== PACKAGE_URL_SCHEME
|| m_aScheme
== PACKAGE_ZIP_URL_SCHEME
)
114 if ( m_aScheme
== PACKAGE_ZIP_URL_SCHEME
)
117 ( !m_aParam
.isEmpty()
118 ? OUString( "&purezip" )
119 : OUString( "?purezip" ) );
122 aPureUri
= aPureUri
.replaceAt( 0,
123 m_aScheme
.getLength(),
126 sal_Int32 nStart
= PACKAGE_URL_SCHEME_LENGTH
+ 3;
127 sal_Int32 nEnd
= aPureUri
.lastIndexOf( '/' );
128 if ( nEnd
== PACKAGE_URL_SCHEME_LENGTH
+ 3 )
130 // Only <scheme>:/// - Empty authority
132 // error, but remember that we did a init().
136 else if ( nEnd
== ( aPureUri
.getLength() - 1 ) )
138 if ( aPureUri
[ aPureUri
.getLength() - 2 ] == '/' )
140 // Only <scheme>://// or <scheme>://<something>
142 // error, but remember that we did a init().
147 // Remove trailing slash.
148 aPureUri
= aPureUri
.copy( 0, nEnd
);
152 nEnd
= aPureUri
.indexOf( '/', nStart
);
157 OUString aNormPackage
= aPureUri
.copy( nStart
);
158 normalize( aNormPackage
);
160 aPureUri
= aPureUri
.replaceAt(
161 nStart
, aPureUri
.getLength() - nStart
, aNormPackage
);
163 = ::ucb_impl::urihelper::decodeSegment( aNormPackage
);
165 m_aUri
= m_aUri
.replaceAt( 0,
168 : m_aUri
.getLength(), aPureUri
);
170 sal_Int32 nLastSlash
= m_aPackage
.lastIndexOf( '/' );
171 if ( nLastSlash
!= -1 )
172 m_aName
= ::ucb_impl::urihelper::decodeSegment(
173 m_aPackage
.copy( nLastSlash
+ 1 ) );
176 = ::ucb_impl::urihelper::decodeSegment( m_aPackage
);
180 m_aPath
= aPureUri
.copy( nEnd
+ 1 );
182 // Unexpected sequences of characters:
183 // - empty path segments
185 // - parent folder segments ".."
186 // - current folder segments "."
187 if ( m_aPath
.indexOf( "//" ) != -1
188 || m_aPath
.indexOf( "%2F" ) != -1
189 || m_aPath
.indexOf( "%2f" ) != -1
190 || ::comphelper::OStorageHelper::PathHasSegment( m_aPath
, OUString( ".." ) )
191 || ::comphelper::OStorageHelper::PathHasSegment( m_aPath
, OUString( "." ) ) )
193 // error, but remember that we did a init().
198 OUString aNormPackage
= aPureUri
.copy( nStart
, nEnd
- nStart
);
199 normalize( aNormPackage
);
201 aPureUri
= aPureUri
.replaceAt(
202 nStart
, nEnd
- nStart
, aNormPackage
);
203 aPureUri
= aPureUri
.replaceAt(
205 aPureUri
.getLength() - nEnd
- 1,
206 ::ucb_impl::urihelper::encodeURI( m_aPath
) );
209 = ::ucb_impl::urihelper::decodeSegment( aNormPackage
);
210 m_aPath
= ::ucb_impl::urihelper::decodeSegment( m_aPath
);
211 m_aUri
= m_aUri
.replaceAt( 0,
214 : m_aUri
.getLength(), aPureUri
);
216 sal_Int32 nLastSlash
= aPureUri
.lastIndexOf( '/' );
217 if ( nLastSlash
!= -1 )
219 m_aParentUri
= aPureUri
.copy( 0, nLastSlash
);
220 m_aName
= ::ucb_impl::urihelper::decodeSegment(
221 aPureUri
.copy( nLastSlash
+ 1 ) );
230 // error, but remember that we did a init().
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */