xref: /AOO41X/main/ucb/source/ucp/package/pkguri.cxx (revision 2f86921c33504fdff5a030df6c0b258927045abb)
1*2f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2f86921cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2f86921cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2f86921cSAndrew Rist  * distributed with this work for additional information
6*2f86921cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2f86921cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2f86921cSAndrew Rist  * "License"); you may not use this file except in compliance
9*2f86921cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*2f86921cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*2f86921cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2f86921cSAndrew Rist  * software distributed under the License is distributed on an
15*2f86921cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2f86921cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*2f86921cSAndrew Rist  * specific language governing permissions and limitations
18*2f86921cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*2f86921cSAndrew Rist  *************************************************************/
21*2f86921cSAndrew Rist 
22*2f86921cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_ucb.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir /**************************************************************************
28cdf0e10cSrcweir                                 TODO
29cdf0e10cSrcweir  **************************************************************************
30cdf0e10cSrcweir 
31cdf0e10cSrcweir  *************************************************************************/
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
34cdf0e10cSrcweir #include "osl/diagnose.h"
35cdf0e10cSrcweir #include "comphelper/storagehelper.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "../inc/urihelper.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "pkguri.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace package_ucp;
42cdf0e10cSrcweir using namespace rtl;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir //=========================================================================
45cdf0e10cSrcweir //=========================================================================
46cdf0e10cSrcweir //
47cdf0e10cSrcweir // PackageUri Implementation.
48cdf0e10cSrcweir //
49cdf0e10cSrcweir //=========================================================================
50cdf0e10cSrcweir //=========================================================================
51cdf0e10cSrcweir 
normalize(OUString & rURL)52cdf0e10cSrcweir static void normalize( OUString& rURL )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     sal_Int32 nPos = 0;
55cdf0e10cSrcweir     do
56cdf0e10cSrcweir     {
57cdf0e10cSrcweir         nPos = rURL.indexOf( '%', nPos );
58cdf0e10cSrcweir         if ( nPos != -1 )
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir             if ( nPos < ( rURL.getLength() - 2 ) )
61cdf0e10cSrcweir             {
62cdf0e10cSrcweir                 OUString aTmp = rURL.copy( nPos + 1, 2 );
63cdf0e10cSrcweir                 rURL = rURL.replaceAt( nPos + 1, 2, aTmp.toAsciiUpperCase() );
64cdf0e10cSrcweir                 nPos++;
65cdf0e10cSrcweir             }
66cdf0e10cSrcweir         }
67cdf0e10cSrcweir     }
68cdf0e10cSrcweir     while ( nPos != -1 );
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir //=========================================================================
init() const72cdf0e10cSrcweir void PackageUri::init() const
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     // Already inited?
75cdf0e10cSrcweir     if ( m_aUri.getLength() && !m_aPath.getLength() )
76cdf0e10cSrcweir     {
77cdf0e10cSrcweir         // Note: Maybe it's a re-init, setUri only resets m_aPath!
78cdf0e10cSrcweir         m_aPackage = m_aParentUri = m_aName = m_aParam = m_aScheme
79cdf0e10cSrcweir             = OUString();
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         // URI must match at least: <sheme>://<non_empty_url_to_file>
82cdf0e10cSrcweir         if ( ( m_aUri.getLength() < PACKAGE_URL_SCHEME_LENGTH + 4 ) )
83cdf0e10cSrcweir         {
84cdf0e10cSrcweir             // error, but remember that we did a init().
85cdf0e10cSrcweir             m_aPath = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
86cdf0e10cSrcweir             return;
87cdf0e10cSrcweir         }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         // Scheme must be followed by '://'
90cdf0e10cSrcweir         if ( ( m_aUri.getStr()[ PACKAGE_URL_SCHEME_LENGTH ]
91cdf0e10cSrcweir                 != sal_Unicode( ':' ) )
92cdf0e10cSrcweir              ||
93cdf0e10cSrcweir              ( m_aUri.getStr()[ PACKAGE_URL_SCHEME_LENGTH + 1 ]
94cdf0e10cSrcweir                 != sal_Unicode( '/' ) )
95cdf0e10cSrcweir              ||
96cdf0e10cSrcweir              ( m_aUri.getStr()[ PACKAGE_URL_SCHEME_LENGTH + 2 ]
97cdf0e10cSrcweir                 != sal_Unicode( '/' ) ) )
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             // error, but remember that we did a init().
100cdf0e10cSrcweir             m_aPath = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
101cdf0e10cSrcweir             return;
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 		rtl::OUString aPureUri;
105cdf0e10cSrcweir 		sal_Int32 nParam = m_aUri.indexOf( '?' );
106cdf0e10cSrcweir 		if( nParam >= 0 )
107cdf0e10cSrcweir 		{
108cdf0e10cSrcweir 			m_aParam = m_aUri.copy( nParam );
109cdf0e10cSrcweir 			aPureUri = m_aUri.copy( 0, nParam );
110cdf0e10cSrcweir 		}
111cdf0e10cSrcweir 		else
112cdf0e10cSrcweir 			aPureUri = m_aUri;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         // Scheme is case insensitive.
115cdf0e10cSrcweir         m_aScheme = aPureUri.copy(
116cdf0e10cSrcweir             0, PACKAGE_URL_SCHEME_LENGTH ).toAsciiLowerCase();
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         if ( m_aScheme.equalsAsciiL(
119cdf0e10cSrcweir                  RTL_CONSTASCII_STRINGPARAM( PACKAGE_URL_SCHEME ) )
120cdf0e10cSrcweir           || m_aScheme.equalsAsciiL(
121cdf0e10cSrcweir               RTL_CONSTASCII_STRINGPARAM( PACKAGE_ZIP_URL_SCHEME ) ) )
122cdf0e10cSrcweir         {
123cdf0e10cSrcweir         	if ( m_aScheme.equalsAsciiL(
124cdf0e10cSrcweir                      RTL_CONSTASCII_STRINGPARAM( PACKAGE_ZIP_URL_SCHEME ) ) )
125cdf0e10cSrcweir 			{
126cdf0e10cSrcweir 				m_aParam +=
127cdf0e10cSrcweir                     ( m_aParam.getLength()
128cdf0e10cSrcweir                       ? ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "&purezip" ) )
129cdf0e10cSrcweir                       : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "?purezip" ) ) );
130cdf0e10cSrcweir 			}
131cdf0e10cSrcweir 
132cdf0e10cSrcweir             aPureUri = aPureUri.replaceAt( 0,
133cdf0e10cSrcweir                                            m_aScheme.getLength(),
134cdf0e10cSrcweir                                            m_aScheme );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir             sal_Int32 nStart = PACKAGE_URL_SCHEME_LENGTH + 3;
137cdf0e10cSrcweir             sal_Int32 nEnd   = aPureUri.lastIndexOf( '/' );
138cdf0e10cSrcweir             if ( nEnd == PACKAGE_URL_SCHEME_LENGTH + 3 )
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 // Only <scheme>:/// - Empty authority
141cdf0e10cSrcweir 
142cdf0e10cSrcweir                 // error, but remember that we did a init().
143cdf0e10cSrcweir                 m_aPath = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
144cdf0e10cSrcweir                 return;
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir             else if ( nEnd == ( aPureUri.getLength() - 1 ) )
147cdf0e10cSrcweir             {
148cdf0e10cSrcweir                 if ( aPureUri.getStr()[ aPureUri.getLength() - 2 ]
149cdf0e10cSrcweir                                                 == sal_Unicode( '/' ) )
150cdf0e10cSrcweir                 {
151cdf0e10cSrcweir                     // Only <scheme>://// or <scheme>://<something>//
152cdf0e10cSrcweir 
153cdf0e10cSrcweir                     // error, but remember that we did a init().
154cdf0e10cSrcweir                     m_aPath = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
155cdf0e10cSrcweir                     return;
156cdf0e10cSrcweir                 }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir                 // Remove trailing slash.
159cdf0e10cSrcweir                 aPureUri = aPureUri.copy( 0, nEnd );
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 
163cdf0e10cSrcweir             nEnd = aPureUri.indexOf( '/', nStart );
164cdf0e10cSrcweir             if ( nEnd == -1 )
165cdf0e10cSrcweir             {
166cdf0e10cSrcweir                 // root folder.
167cdf0e10cSrcweir 
168cdf0e10cSrcweir                 OUString aNormPackage = aPureUri.copy( nStart );
169cdf0e10cSrcweir                 normalize( aNormPackage );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir                 aPureUri = aPureUri.replaceAt(
172cdf0e10cSrcweir                     nStart, aPureUri.getLength() - nStart, aNormPackage );
173cdf0e10cSrcweir                 m_aPackage
174cdf0e10cSrcweir                     = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
175cdf0e10cSrcweir                 m_aPath = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
176cdf0e10cSrcweir 				m_aUri = m_aUri.replaceAt( 0,
177cdf0e10cSrcweir                                            ( nParam >= 0 )
178cdf0e10cSrcweir                                            ? nParam
179cdf0e10cSrcweir                                            : m_aUri.getLength(), aPureUri );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir                 sal_Int32 nLastSlash = m_aPackage.lastIndexOf( '/' );
182cdf0e10cSrcweir                 if ( nLastSlash != -1 )
183cdf0e10cSrcweir                     m_aName = ::ucb_impl::urihelper::decodeSegment(
184cdf0e10cSrcweir                         m_aPackage.copy( nLastSlash + 1 ) );
185cdf0e10cSrcweir                 else
186cdf0e10cSrcweir                     m_aName
187cdf0e10cSrcweir                         = ::ucb_impl::urihelper::decodeSegment( m_aPackage );
188cdf0e10cSrcweir             }
189cdf0e10cSrcweir             else
190cdf0e10cSrcweir             {
191cdf0e10cSrcweir                 m_aPath = aPureUri.copy( nEnd + 1 );
192cdf0e10cSrcweir 
193cdf0e10cSrcweir                 // Unexpected sequences of characters:
194cdf0e10cSrcweir                 // - empty path segments
195cdf0e10cSrcweir                 // - encoded slashes
196cdf0e10cSrcweir                 // - parent folder segments ".."
197cdf0e10cSrcweir                 // - current folder segments "."
198cdf0e10cSrcweir                 if ( m_aPath.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "//" ) ) ) != -1
199cdf0e10cSrcweir                   || m_aPath.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "%2F" ) ) ) != -1
200cdf0e10cSrcweir                   || m_aPath.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "%2f" ) ) ) != -1
201cdf0e10cSrcweir                   || ::comphelper::OStorageHelper::PathHasSegment( m_aPath, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ".." ) ) )
202cdf0e10cSrcweir                   || ::comphelper::OStorageHelper::PathHasSegment( m_aPath, ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "." ) ) ) )
203cdf0e10cSrcweir                 {
204cdf0e10cSrcweir                     // error, but remember that we did a init().
205cdf0e10cSrcweir                     m_aPath = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
206cdf0e10cSrcweir                     return;
207cdf0e10cSrcweir                 }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir                 OUString aNormPackage = aPureUri.copy( nStart, nEnd - nStart );
210cdf0e10cSrcweir                 normalize( aNormPackage );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir                 aPureUri = aPureUri.replaceAt(
213cdf0e10cSrcweir                     nStart, nEnd - nStart, aNormPackage );
214cdf0e10cSrcweir                 aPureUri = aPureUri.replaceAt(
215cdf0e10cSrcweir                     nEnd + 1,
216cdf0e10cSrcweir                     aPureUri.getLength() - nEnd - 1,
217cdf0e10cSrcweir                     ::ucb_impl::urihelper::encodeURI( m_aPath ) );
218cdf0e10cSrcweir 
219cdf0e10cSrcweir                 m_aPackage
220cdf0e10cSrcweir                     = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
221cdf0e10cSrcweir 				m_aPath = ::ucb_impl::urihelper::decodeSegment( m_aPath );
222cdf0e10cSrcweir 				m_aUri = m_aUri.replaceAt( 0,
223cdf0e10cSrcweir                                            ( nParam >= 0 )
224cdf0e10cSrcweir                                            ? nParam
225cdf0e10cSrcweir                                            : m_aUri.getLength(), aPureUri );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir                 sal_Int32 nLastSlash = aPureUri.lastIndexOf( '/' );
228cdf0e10cSrcweir                 if ( nLastSlash != -1 )
229cdf0e10cSrcweir                 {
230cdf0e10cSrcweir                     m_aParentUri = aPureUri.copy( 0, nLastSlash );
231cdf0e10cSrcweir                     m_aName = ::ucb_impl::urihelper::decodeSegment(
232cdf0e10cSrcweir                         aPureUri.copy( nLastSlash + 1 ) );
233cdf0e10cSrcweir                 }
234cdf0e10cSrcweir             }
235cdf0e10cSrcweir 
236cdf0e10cSrcweir             // success
237cdf0e10cSrcweir             m_bValid = true;
238cdf0e10cSrcweir         }
239cdf0e10cSrcweir         else
240cdf0e10cSrcweir         {
241cdf0e10cSrcweir             // error, but remember that we did a init().
242cdf0e10cSrcweir             m_aPath = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
243cdf0e10cSrcweir         }
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir }
246