fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / ucp / package / pkguri.cxx
blob306c0f67bd9721aeb6208704003c90a52cc06c01
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 "rtl/ustrbuf.hxx"
28 #include "osl/diagnose.h"
29 #include "comphelper/storagehelper.hxx"
31 #include "../inc/urihelper.hxx"
33 #include "pkguri.hxx"
35 using namespace package_ucp;
41 // PackageUri Implementation.
46 static void normalize( OUString& rURL )
48 sal_Int32 nPos = 0;
51 nPos = rURL.indexOf( '%', nPos );
52 if ( nPos != -1 )
54 if ( nPos < ( rURL.getLength() - 2 ) )
56 OUString aTmp = rURL.copy( nPos + 1, 2 );
57 rURL = rURL.replaceAt( nPos + 1, 2, aTmp.toAsciiUpperCase() );
58 nPos++;
62 while ( nPos != -1 );
66 void PackageUri::init() const
68 // Already inited?
69 if ( !m_aUri.isEmpty() && m_aPath.isEmpty() )
71 // Note: Maybe it's a re-init, setUri only resets m_aPath!
72 m_aPackage.clear();
73 m_aParentUri.clear();
74 m_aName.clear();
75 m_aParam.clear();
76 m_aScheme.clear();
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().
82 m_aPath = "/";
83 return;
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().
94 m_aPath = "/";
95 return;
98 OUString aPureUri;
99 sal_Int32 nParam = m_aUri.indexOf( '?' );
100 if( nParam >= 0 )
102 m_aParam = m_aUri.copy( nParam );
103 aPureUri = m_aUri.copy( 0, nParam );
105 else
106 aPureUri = m_aUri;
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 )
116 m_aParam +=
117 ( !m_aParam.isEmpty()
118 ? OUString( "&purezip" )
119 : OUString( "?purezip" ) );
122 aPureUri = aPureUri.replaceAt( 0,
123 m_aScheme.getLength(),
124 m_aScheme );
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 = "/";
134 return;
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().
143 m_aPath = "/";
144 return;
147 // Remove trailing slash.
148 aPureUri = aPureUri.copy( 0, nEnd );
152 nEnd = aPureUri.indexOf( '/', nStart );
153 if ( nEnd == -1 )
155 // root folder.
157 OUString aNormPackage = aPureUri.copy( nStart );
158 normalize( aNormPackage );
160 aPureUri = aPureUri.replaceAt(
161 nStart, aPureUri.getLength() - nStart, aNormPackage );
162 m_aPackage
163 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
164 m_aPath = "/";
165 m_aUri = m_aUri.replaceAt( 0,
166 ( nParam >= 0 )
167 ? nParam
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 ) );
174 else
175 m_aName
176 = ::ucb_impl::urihelper::decodeSegment( m_aPackage );
178 else
180 m_aPath = aPureUri.copy( nEnd + 1 );
182 // Unexpected sequences of characters:
183 // - empty path segments
184 // - encoded slashes
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().
194 m_aPath = "/";
195 return;
198 OUString aNormPackage = aPureUri.copy( nStart, nEnd - nStart );
199 normalize( aNormPackage );
201 aPureUri = aPureUri.replaceAt(
202 nStart, nEnd - nStart, aNormPackage );
203 aPureUri = aPureUri.replaceAt(
204 nEnd + 1,
205 aPureUri.getLength() - nEnd - 1,
206 ::ucb_impl::urihelper::encodeURI( m_aPath ) );
208 m_aPackage
209 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
210 m_aPath = ::ucb_impl::urihelper::decodeSegment( m_aPath );
211 m_aUri = m_aUri.replaceAt( 0,
212 ( nParam >= 0 )
213 ? nParam
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 ) );
225 // success
226 m_bValid = true;
228 else
230 // error, but remember that we did a init().
231 m_aPath = "/";
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */