update credits
[LibreOffice.git] / ucb / source / ucp / package / pkguri.cxx
blob29e9cd61c4049983d15755249f63dd02d348d7a9
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;
38 //=========================================================================
39 //=========================================================================
41 // PackageUri Implementation.
43 //=========================================================================
44 //=========================================================================
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 );
65 //=========================================================================
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 = m_aParentUri = m_aName = m_aParam = m_aScheme
73 = OUString();
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( "/" );
80 return;
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( "/" );
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 = OUString( "/" );
134 return;
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( "/" );
145 return;
148 // Remove trailing slash.
149 aPureUri = aPureUri.copy( 0, nEnd );
153 nEnd = aPureUri.indexOf( '/', nStart );
154 if ( nEnd == -1 )
156 // root folder.
158 OUString aNormPackage = aPureUri.copy( nStart );
159 normalize( aNormPackage );
161 aPureUri = aPureUri.replaceAt(
162 nStart, aPureUri.getLength() - nStart, aNormPackage );
163 m_aPackage
164 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
165 m_aPath = OUString( "/" );
166 m_aUri = m_aUri.replaceAt( 0,
167 ( nParam >= 0 )
168 ? nParam
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 ) );
175 else
176 m_aName
177 = ::ucb_impl::urihelper::decodeSegment( m_aPackage );
179 else
181 m_aPath = aPureUri.copy( nEnd + 1 );
183 // Unexpected sequences of characters:
184 // - empty path segments
185 // - encoded slashes
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( "/" );
196 return;
199 OUString aNormPackage = aPureUri.copy( nStart, nEnd - nStart );
200 normalize( aNormPackage );
202 aPureUri = aPureUri.replaceAt(
203 nStart, nEnd - nStart, aNormPackage );
204 aPureUri = aPureUri.replaceAt(
205 nEnd + 1,
206 aPureUri.getLength() - nEnd - 1,
207 ::ucb_impl::urihelper::encodeURI( m_aPath ) );
209 m_aPackage
210 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
211 m_aPath = ::ucb_impl::urihelper::decodeSegment( m_aPath );
212 m_aUri = m_aUri.replaceAt( 0,
213 ( nParam >= 0 )
214 ? nParam
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 ) );
226 // success
227 m_bValid = true;
229 else
231 // error, but remember that we did a init().
232 m_aPath = OUString( "/" );
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */