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
;
38 //=========================================================================
39 //=========================================================================
41 // PackageUri Implementation.
43 //=========================================================================
44 //=========================================================================
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() );
65 //=========================================================================
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!
72 m_aPackage
= m_aParentUri
= m_aName
= m_aParam
= m_aScheme
75 // URI must match at least: <sheme>://<non_empty_url_to_file>
76 if ( ( m_aUri
.getLength() < PACKAGE_URL_SCHEME_LENGTH
+ 4 ) )
78 // error, but remember that we did a init().
79 m_aPath
= OUString( "/" );
83 // Scheme must be followed by '://'
84 if ( ( m_aUri
.getStr()[ PACKAGE_URL_SCHEME_LENGTH
]
85 != sal_Unicode( ':' ) )
87 ( m_aUri
.getStr()[ PACKAGE_URL_SCHEME_LENGTH
+ 1 ]
88 != sal_Unicode( '/' ) )
90 ( m_aUri
.getStr()[ PACKAGE_URL_SCHEME_LENGTH
+ 2 ]
91 != sal_Unicode( '/' ) ) )
93 // error, but remember that we did a init().
94 m_aPath
= OUString( "/" );
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().
133 m_aPath
= OUString( "/" );
136 else if ( nEnd
== ( aPureUri
.getLength() - 1 ) )
138 if ( aPureUri
.getStr()[ aPureUri
.getLength() - 2 ]
139 == sal_Unicode( '/' ) )
141 // Only <scheme>://// or <scheme>://<something>//
143 // error, but remember that we did a init().
144 m_aPath
= OUString( "/" );
148 // Remove trailing slash.
149 aPureUri
= aPureUri
.copy( 0, nEnd
);
153 nEnd
= aPureUri
.indexOf( '/', nStart
);
158 OUString aNormPackage
= aPureUri
.copy( nStart
);
159 normalize( aNormPackage
);
161 aPureUri
= aPureUri
.replaceAt(
162 nStart
, aPureUri
.getLength() - nStart
, aNormPackage
);
164 = ::ucb_impl::urihelper::decodeSegment( aNormPackage
);
165 m_aPath
= OUString( "/" );
166 m_aUri
= m_aUri
.replaceAt( 0,
169 : m_aUri
.getLength(), aPureUri
);
171 sal_Int32 nLastSlash
= m_aPackage
.lastIndexOf( '/' );
172 if ( nLastSlash
!= -1 )
173 m_aName
= ::ucb_impl::urihelper::decodeSegment(
174 m_aPackage
.copy( nLastSlash
+ 1 ) );
177 = ::ucb_impl::urihelper::decodeSegment( m_aPackage
);
181 m_aPath
= aPureUri
.copy( nEnd
+ 1 );
183 // Unexpected sequences of characters:
184 // - empty path segments
186 // - parent folder segments ".."
187 // - current folder segments "."
188 if ( m_aPath
.indexOf( "//" ) != -1
189 || m_aPath
.indexOf( "%2F" ) != -1
190 || m_aPath
.indexOf( "%2f" ) != -1
191 || ::comphelper::OStorageHelper::PathHasSegment( m_aPath
, OUString( ".." ) )
192 || ::comphelper::OStorageHelper::PathHasSegment( m_aPath
, OUString( "." ) ) )
194 // error, but remember that we did a init().
195 m_aPath
= OUString( "/" );
199 OUString aNormPackage
= aPureUri
.copy( nStart
, nEnd
- nStart
);
200 normalize( aNormPackage
);
202 aPureUri
= aPureUri
.replaceAt(
203 nStart
, nEnd
- nStart
, aNormPackage
);
204 aPureUri
= aPureUri
.replaceAt(
206 aPureUri
.getLength() - nEnd
- 1,
207 ::ucb_impl::urihelper::encodeURI( m_aPath
) );
210 = ::ucb_impl::urihelper::decodeSegment( aNormPackage
);
211 m_aPath
= ::ucb_impl::urihelper::decodeSegment( m_aPath
);
212 m_aUri
= m_aUri
.replaceAt( 0,
215 : m_aUri
.getLength(), aPureUri
);
217 sal_Int32 nLastSlash
= aPureUri
.lastIndexOf( '/' );
218 if ( nLastSlash
!= -1 )
220 m_aParentUri
= aPureUri
.copy( 0, nLastSlash
);
221 m_aName
= ::ucb_impl::urihelper::decodeSegment(
222 aPureUri
.copy( nLastSlash
+ 1 ) );
231 // error, but remember that we did a init().
232 m_aPath
= OUString( "/" );
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */