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