Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : /**************************************************************************
22 : TODO
23 : **************************************************************************
24 :
25 : *************************************************************************/
26 :
27 : #include "rtl/ustrbuf.hxx"
28 : #include "osl/diagnose.h"
29 : #include "comphelper/storagehelper.hxx"
30 :
31 : #include "../inc/urihelper.hxx"
32 :
33 : #include "pkguri.hxx"
34 :
35 : using namespace package_ucp;
36 :
37 :
38 :
39 :
40 :
41 : // PackageUri Implementation.
42 :
43 :
44 :
45 :
46 66 : static void normalize( OUString& rURL )
47 : {
48 66 : sal_Int32 nPos = 0;
49 644 : do
50 : {
51 644 : nPos = rURL.indexOf( '%', nPos );
52 644 : if ( nPos != -1 )
53 : {
54 578 : if ( nPos < ( rURL.getLength() - 2 ) )
55 : {
56 578 : OUString aTmp = rURL.copy( nPos + 1, 2 );
57 578 : rURL = rURL.replaceAt( nPos + 1, 2, aTmp.toAsciiUpperCase() );
58 578 : nPos++;
59 : }
60 : }
61 : }
62 : while ( nPos != -1 );
63 66 : }
64 :
65 :
66 420 : void PackageUri::init() const
67 : {
68 : // Already inited?
69 420 : if ( !m_aUri.isEmpty() && m_aPath.isEmpty() )
70 : {
71 : // Note: Maybe it's a re-init, setUri only resets m_aPath!
72 66 : m_aPackage.clear();
73 66 : m_aParentUri.clear();
74 66 : m_aName.clear();
75 66 : m_aParam.clear();
76 66 : m_aScheme.clear();
77 :
78 : // URI must match at least: <sheme>://<non_empty_url_to_file>
79 66 : if ( ( m_aUri.getLength() < PACKAGE_URL_SCHEME_LENGTH + 4 ) )
80 : {
81 : // error, but remember that we did a init().
82 0 : m_aPath = "/";
83 0 : return;
84 : }
85 :
86 : // Scheme must be followed by '://'
87 132 : if ( ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH ] != ':' )
88 66 : ||
89 66 : ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH + 1 ] != '/' )
90 132 : ||
91 66 : ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH + 2 ] != '/' ) )
92 : {
93 : // error, but remember that we did a init().
94 0 : m_aPath = "/";
95 0 : return;
96 : }
97 :
98 66 : OUString aPureUri;
99 66 : sal_Int32 nParam = m_aUri.indexOf( '?' );
100 66 : if( nParam >= 0 )
101 : {
102 0 : m_aParam = m_aUri.copy( nParam );
103 0 : aPureUri = m_aUri.copy( 0, nParam );
104 : }
105 : else
106 66 : aPureUri = m_aUri;
107 :
108 : // Scheme is case insensitive.
109 132 : m_aScheme = aPureUri.copy(
110 66 : 0, PACKAGE_URL_SCHEME_LENGTH ).toAsciiLowerCase();
111 :
112 66 : if ( m_aScheme == PACKAGE_URL_SCHEME || m_aScheme == PACKAGE_ZIP_URL_SCHEME )
113 : {
114 66 : if ( m_aScheme == PACKAGE_ZIP_URL_SCHEME )
115 : {
116 76 : m_aParam +=
117 38 : ( !m_aParam.isEmpty()
118 : ? OUString( "&purezip" )
119 76 : : OUString( "?purezip" ) );
120 : }
121 :
122 132 : aPureUri = aPureUri.replaceAt( 0,
123 : m_aScheme.getLength(),
124 66 : m_aScheme );
125 :
126 66 : sal_Int32 nStart = PACKAGE_URL_SCHEME_LENGTH + 3;
127 66 : sal_Int32 nEnd = aPureUri.lastIndexOf( '/' );
128 66 : if ( nEnd == PACKAGE_URL_SCHEME_LENGTH + 3 )
129 : {
130 : // Only <scheme>:/// - Empty authority
131 :
132 : // error, but remember that we did a init().
133 0 : m_aPath = "/";
134 0 : return;
135 : }
136 66 : else if ( nEnd == ( aPureUri.getLength() - 1 ) )
137 : {
138 6 : if ( aPureUri[ aPureUri.getLength() - 2 ] == '/' )
139 : {
140 : // Only <scheme>://// or <scheme>://<something>
141 :
142 : // error, but remember that we did a init().
143 0 : m_aPath = "/";
144 0 : return;
145 : }
146 :
147 : // Remove trailing slash.
148 6 : aPureUri = aPureUri.copy( 0, nEnd );
149 : }
150 :
151 :
152 66 : nEnd = aPureUri.indexOf( '/', nStart );
153 66 : if ( nEnd == -1 )
154 : {
155 : // root folder.
156 :
157 38 : OUString aNormPackage = aPureUri.copy( nStart );
158 38 : normalize( aNormPackage );
159 :
160 76 : aPureUri = aPureUri.replaceAt(
161 76 : nStart, aPureUri.getLength() - nStart, aNormPackage );
162 : m_aPackage
163 38 : = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
164 38 : m_aPath = "/";
165 114 : m_aUri = m_aUri.replaceAt( 0,
166 : ( nParam >= 0 )
167 : ? nParam
168 76 : : m_aUri.getLength(), aPureUri );
169 :
170 38 : sal_Int32 nLastSlash = m_aPackage.lastIndexOf( '/' );
171 38 : if ( nLastSlash != -1 )
172 76 : m_aName = ::ucb_impl::urihelper::decodeSegment(
173 38 : m_aPackage.copy( nLastSlash + 1 ) );
174 : else
175 : m_aName
176 0 : = ::ucb_impl::urihelper::decodeSegment( m_aPackage );
177 : }
178 : else
179 : {
180 28 : m_aPath = aPureUri.copy( nEnd + 1 );
181 :
182 : // Unexpected sequences of characters:
183 : // - empty path segments
184 : // - encoded slashes
185 : // - parent folder segments ".."
186 : // - current folder segments "."
187 84 : if ( m_aPath.indexOf( "//" ) != -1
188 28 : || m_aPath.indexOf( "%2F" ) != -1
189 28 : || m_aPath.indexOf( "%2f" ) != -1
190 84 : || ::comphelper::OStorageHelper::PathHasSegment( m_aPath, OUString( ".." ) )
191 112 : || ::comphelper::OStorageHelper::PathHasSegment( m_aPath, OUString( "." ) ) )
192 : {
193 : // error, but remember that we did a init().
194 0 : m_aPath = "/";
195 0 : return;
196 : }
197 :
198 28 : OUString aNormPackage = aPureUri.copy( nStart, nEnd - nStart );
199 28 : normalize( aNormPackage );
200 :
201 56 : aPureUri = aPureUri.replaceAt(
202 28 : nStart, nEnd - nStart, aNormPackage );
203 84 : aPureUri = aPureUri.replaceAt(
204 : nEnd + 1,
205 28 : aPureUri.getLength() - nEnd - 1,
206 28 : ::ucb_impl::urihelper::encodeURI( m_aPath ) );
207 :
208 : m_aPackage
209 28 : = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
210 28 : m_aPath = ::ucb_impl::urihelper::decodeSegment( m_aPath );
211 84 : m_aUri = m_aUri.replaceAt( 0,
212 : ( nParam >= 0 )
213 : ? nParam
214 56 : : m_aUri.getLength(), aPureUri );
215 :
216 28 : sal_Int32 nLastSlash = aPureUri.lastIndexOf( '/' );
217 28 : if ( nLastSlash != -1 )
218 : {
219 28 : m_aParentUri = aPureUri.copy( 0, nLastSlash );
220 56 : m_aName = ::ucb_impl::urihelper::decodeSegment(
221 28 : aPureUri.copy( nLastSlash + 1 ) );
222 28 : }
223 : }
224 :
225 : // success
226 66 : m_bValid = true;
227 : }
228 : else
229 : {
230 : // error, but remember that we did a init().
231 0 : m_aPath = "/";
232 66 : }
233 : }
234 : }
235 :
236 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|