LibreOffice Module ucb (master) 1
pkguri.cxx
Go to the documentation of this file.
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 <sal/config.h>
28
29#include <string_view>
30
32
33#include "../inc/urihelper.hxx"
34
35#include "pkguri.hxx"
36
37using namespace package_ucp;
38
39
40// PackageUri Implementation.
41
42
43static void normalize( OUString& rURL )
44{
45 sal_Int32 nPos = 0;
46 do
47 {
48 nPos = rURL.indexOf( '%', nPos );
49 if ( nPos != -1 )
50 {
51 if ( nPos < ( rURL.getLength() - 2 ) )
52 {
53 OUString aTmp = rURL.copy( nPos + 1, 2 );
54 rURL = rURL.replaceAt( nPos + 1, 2, aTmp.toAsciiUpperCase() );
55 nPos++;
56 }
57 }
58 }
59 while ( nPos != -1 );
60}
61
62
63void PackageUri::init() const
64{
65 // Already inited?
66 if ( m_aUri.isEmpty() || !m_aPath.isEmpty() )
67 return;
68
69 // Note: Maybe it's a re-init, setUri only resets m_aPath!
70 m_aPackage.clear();
71 m_aParentUri.clear();
72 m_aName.clear();
73 m_aParam.clear();
74 m_aScheme.clear();
75
76 // URI must match at least: <scheme>://<non_empty_url_to_file>
77 if ( m_aUri.getLength() < PACKAGE_URL_SCHEME_LENGTH + 4 )
78 {
79 // error, but remember that we did an init().
80 m_aPath = "/";
81 return;
82 }
83
84 // Scheme must be followed by '://'
85 if ( ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH ] != ':' )
86 ||
87 ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH + 1 ] != '/' )
88 ||
89 ( m_aUri[ PACKAGE_URL_SCHEME_LENGTH + 2 ] != '/' ) )
90 {
91 // error, but remember that we did an init().
92 m_aPath = "/";
93 return;
94 }
95
96 OUString aPureUri;
97 sal_Int32 nParam = m_aUri.indexOf( '?' );
98 if( nParam >= 0 )
99 {
100 m_aParam = m_aUri.copy( nParam );
101 aPureUri = m_aUri.copy( 0, nParam );
102 }
103 else
104 aPureUri = m_aUri;
105
106 // Scheme is case insensitive.
107 m_aScheme = aPureUri.copy(
108 0, PACKAGE_URL_SCHEME_LENGTH ).toAsciiLowerCase();
109
111 {
113 {
114 m_aParam +=
115 ( !m_aParam.isEmpty()
116 ? std::u16string_view( u"&purezip" )
117 : std::u16string_view( u"?purezip" ) );
118 }
119
120 aPureUri = aPureUri.replaceAt( 0,
121 m_aScheme.getLength(),
122 m_aScheme );
123
124 sal_Int32 nStart = PACKAGE_URL_SCHEME_LENGTH + 3;
125 sal_Int32 nEnd = aPureUri.lastIndexOf( '/' );
126 if ( nEnd == PACKAGE_URL_SCHEME_LENGTH + 3 )
127 {
128 // Only <scheme>:/// - Empty authority
129
130 // error, but remember that we did an init().
131 m_aPath = "/";
132 return;
133 }
134 else if ( nEnd == ( aPureUri.getLength() - 1 ) )
135 {
136 if ( aPureUri[ aPureUri.getLength() - 2 ] == '/' )
137 {
138 // Only <scheme>://// or <scheme>://<something>
139
140 // error, but remember that we did an init().
141 m_aPath = "/";
142 return;
143 }
144
145 // Remove trailing slash.
146 aPureUri = aPureUri.copy( 0, nEnd );
147 }
148
149
150 nEnd = aPureUri.indexOf( '/', nStart );
151 if ( nEnd == -1 )
152 {
153 // root folder.
154
155 OUString aNormPackage = aPureUri.copy( nStart );
156 normalize( aNormPackage );
157
158 aPureUri = aPureUri.replaceAt(
159 nStart, aPureUri.getLength() - nStart, aNormPackage );
161 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
162 m_aPath = "/";
163 m_aUri = m_aUri.replaceAt( 0,
164 ( nParam >= 0 )
165 ? nParam
166 : m_aUri.getLength(), aPureUri );
167
168 sal_Int32 nLastSlash = m_aPackage.lastIndexOf( '/' );
169 if ( nLastSlash != -1 )
171 m_aPackage.copy( nLastSlash + 1 ) );
172 else
173 m_aName
175 }
176 else
177 {
178 m_aPath = aPureUri.copy( nEnd + 1 );
179
180 // Unexpected sequences of characters:
181 // - empty path segments
182 // - encoded slashes
183 // - parent folder segments ".."
184 // - current folder segments "."
185 if ( m_aPath.indexOf( "//" ) != -1
186 || m_aPath.indexOf( "%2F" ) != -1
187 || m_aPath.indexOf( "%2f" ) != -1
190 {
191 // error, but remember that we did an init().
192 m_aPath = "/";
193 return;
194 }
195
196 OUString aNormPackage = aPureUri.copy( nStart, nEnd - nStart );
197 normalize( aNormPackage );
198
199 aPureUri = aPureUri.replaceAt(
200 nStart, nEnd - nStart, aNormPackage );
201 aPureUri = aPureUri.replaceAt(
202 nEnd + 1,
203 aPureUri.getLength() - nEnd - 1,
205
207 = ::ucb_impl::urihelper::decodeSegment( aNormPackage );
209 m_aUri = m_aUri.replaceAt( 0,
210 ( nParam >= 0 )
211 ? nParam
212 : m_aUri.getLength(), aPureUri );
213
214 sal_Int32 nLastSlash = aPureUri.lastIndexOf( '/' );
215 if ( nLastSlash != -1 )
216 {
217 m_aParentUri = aPureUri.copy( 0, nLastSlash );
219 aPureUri.copy( nLastSlash + 1 ) );
220 }
221 }
222
223 // success
224 m_bValid = true;
225 }
226 else
227 {
228 // error, but remember that we did an init().
229 m_aPath = "/";
230 }
231
232}
233
234/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool PathHasSegment(std::u16string_view aPath, std::u16string_view aSegment)
float u
sal_uInt16 nPos
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:44
OUString decodeSegment(const OUString &rSegment)
Definition: urihelper.hxx:37
static void normalize(OUString &rURL)
Definition: pkguri.cxx:43
#define PACKAGE_URL_SCHEME
Definition: pkguri.hxx:28
#define PACKAGE_ZIP_URL_SCHEME
Definition: pkguri.hxx:29
#define PACKAGE_URL_SCHEME_LENGTH
Definition: pkguri.hxx:30