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