Stop leaking all ScPostIt instances.
[LibreOffice.git] / ucb / source / ucp / webdav-neon / NeonUri.cxx
blob6d8be547790004b99730bdfcba142024b01c6b94
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
31 #include <string.h>
32 #include <rtl/uri.hxx>
33 #include <rtl/ustring.hxx>
34 #include <rtl/ustrbuf.hxx>
35 #include "ne_alloc.h"
36 #include "NeonUri.hxx"
37 #include "DAVException.hxx"
39 #include "../inc/urihelper.hxx"
41 using namespace webdav_ucp;
43 # if defined __SUNPRO_CC
44 // FIXME: not sure whether initializing a ne_uri statically is supposed to work
45 // the string fields of ne_uri are char*, not const char*
46 # pragma disable_warn
47 # endif
49 #if HAVE_GCC_PRAGMA_DIAGNOSTIC_MODIFY
50 #pragma GCC diagnostic ignored "-Wwrite-strings"
51 #endif
53 namespace {
55 const ne_uri g_sUriDefaultsHTTP = { (char *) "http",
56 NULL,
57 NULL,
58 DEFAULT_HTTP_PORT,
59 NULL,
60 NULL,
61 NULL };
62 const ne_uri g_sUriDefaultsHTTPS = { (char *) "https",
63 NULL,
64 NULL,
65 DEFAULT_HTTPS_PORT,
66 NULL,
67 NULL,
68 NULL };
69 const ne_uri g_sUriDefaultsFTP = { (char *) "ftp",
70 NULL,
71 NULL,
72 DEFAULT_FTP_PORT,
73 NULL,
74 NULL,
75 NULL };
76 } // namespace
78 # if defined __SUNPRO_CC
79 # pragma enable_warn
80 #endif
82 NeonUri::NeonUri( const ne_uri * inUri )
83 throw ( DAVException )
85 if ( inUri == 0 )
86 throw DAVException( DAVException::DAV_INVALID_ARG );
88 char * uri = ne_uri_unparse( inUri );
90 if ( uri == 0 )
91 throw DAVException( DAVException::DAV_INVALID_ARG );
93 init( OString( uri ), inUri );
94 ne_free( uri );
96 calculateURI();
99 NeonUri::NeonUri( const OUString & inUri )
100 throw ( DAVException )
102 if ( inUri.isEmpty() )
103 throw DAVException( DAVException::DAV_INVALID_ARG );
105 // #i77023#
106 OUString aEscapedUri( ucb_impl::urihelper::encodeURI( inUri ) );
108 OString theInputUri(
109 aEscapedUri.getStr(), aEscapedUri.getLength(), RTL_TEXTENCODING_UTF8 );
111 ne_uri theUri;
112 if ( ne_uri_parse( theInputUri.getStr(), &theUri ) != 0 )
114 ne_uri_free( &theUri );
115 throw DAVException( DAVException::DAV_INVALID_ARG );
118 init( theInputUri, &theUri );
119 ne_uri_free( &theUri );
121 calculateURI();
124 void NeonUri::init( const OString & rUri, const ne_uri * pUri )
126 // Complete URI.
127 const ne_uri * pUriDefs
128 = rUri.matchIgnoreAsciiCase( "ftp:" ) ?
129 &g_sUriDefaultsFTP :
130 rUri.matchIgnoreAsciiCase( "https:" ) ?
131 &g_sUriDefaultsHTTPS :
132 &g_sUriDefaultsHTTP;
134 mScheme = OStringToOUString(
135 pUri->scheme ? pUri->scheme : pUriDefs->scheme,
136 RTL_TEXTENCODING_UTF8 );
137 mUserInfo = OStringToOUString(
138 pUri->userinfo ? pUri->userinfo : pUriDefs->userinfo,
139 RTL_TEXTENCODING_UTF8 );
140 mHostName = OStringToOUString(
141 pUri->host ? pUri->host : pUriDefs->host,
142 RTL_TEXTENCODING_UTF8 );
143 mPort = pUri->port > 0 ? pUri->port : pUriDefs->port;
144 mPath = OStringToOUString(
145 pUri->path ? pUri->path : pUriDefs->path,
146 RTL_TEXTENCODING_UTF8 );
148 if ( pUri->query )
150 mPath += "?" + OStringToOUString( pUri->query, RTL_TEXTENCODING_UTF8 );
153 if ( pUri->fragment )
155 mPath += "#" + OStringToOUString( pUri->fragment, RTL_TEXTENCODING_UTF8 );
159 NeonUri::~NeonUri( )
163 void NeonUri::calculateURI ()
165 OUStringBuffer aBuf( mScheme );
166 aBuf.appendAscii( "://" );
167 if ( !mUserInfo.isEmpty() )
169 //TODO! differentiate between empty and missing userinfo
170 aBuf.append( mUserInfo );
171 aBuf.appendAscii( "@" );
173 // Is host a numeric IPv6 address?
174 if ( ( mHostName.indexOf( ':' ) != -1 ) &&
175 ( mHostName[ 0 ] != '[' ) )
177 aBuf.appendAscii( "[" );
178 aBuf.append( mHostName );
179 aBuf.appendAscii( "]" );
181 else
183 aBuf.append( mHostName );
186 // append port, but only, if not default port.
187 bool bAppendPort = true;
188 switch ( mPort )
190 case DEFAULT_HTTP_PORT:
191 bAppendPort = mScheme != "http";
192 break;
194 case DEFAULT_HTTPS_PORT:
195 bAppendPort = mScheme != "https";
196 break;
198 case DEFAULT_FTP_PORT:
199 bAppendPort = mScheme != "ftp";
200 break;
202 if ( bAppendPort )
204 aBuf.appendAscii( ":" );
205 aBuf.append( OUString::number( mPort ) );
207 aBuf.append( mPath );
209 mURI = aBuf.makeStringAndClear();
212 OUString NeonUri::GetPathBaseName () const
214 sal_Int32 nPos = mPath.lastIndexOf ('/');
215 sal_Int32 nTrail = 0;
216 if (nPos == mPath.getLength () - 1)
218 // Trailing slash found. Skip.
219 nTrail = 1;
220 nPos = mPath.lastIndexOf ('/', nPos);
222 if (nPos != -1)
224 OUString aTemp(
225 mPath.copy (nPos + 1, mPath.getLength () - nPos - 1 - nTrail) );
227 // query, fragment present?
228 nPos = aTemp.indexOf( '?' );
229 if ( nPos == -1 )
230 nPos = aTemp.indexOf( '#' );
232 if ( nPos != -1 )
233 aTemp = aTemp.copy( 0, nPos );
235 return aTemp;
237 else
238 return OUString("/");
241 bool NeonUri::operator== ( const NeonUri & rOther ) const
243 return ( mURI == rOther.mURI );
246 OUString NeonUri::GetPathBaseNameUnescaped () const
248 return unescape( GetPathBaseName() );
251 void NeonUri::AppendPath (const OUString& rPath)
253 if (mPath.lastIndexOf ('/') != mPath.getLength () - 1)
254 mPath += "/";
256 mPath += rPath;
257 calculateURI ();
260 // static
261 OUString NeonUri::escapeSegment( const OUString& segment )
263 return rtl::Uri::encode( segment,
264 rtl_UriCharClassPchar,
265 rtl_UriEncodeIgnoreEscapes,
266 RTL_TEXTENCODING_UTF8 );
269 // static
270 OUString NeonUri::unescape( const OUString& segment )
272 return rtl::Uri::decode( segment,
273 rtl_UriDecodeWithCharset,
274 RTL_TEXTENCODING_UTF8 );
277 // static
278 OUString NeonUri::makeConnectionEndPointString(
279 const OUString & rHostName, int nPort )
281 OUStringBuffer aBuf;
283 // Is host a numeric IPv6 address?
284 if ( ( rHostName.indexOf( ':' ) != -1 ) &&
285 ( rHostName[ 0 ] != '[' ) )
287 aBuf.appendAscii( "[" );
288 aBuf.append( rHostName );
289 aBuf.appendAscii( "]" );
291 else
293 aBuf.append( rHostName );
296 if ( ( nPort != DEFAULT_HTTP_PORT ) && ( nPort != DEFAULT_HTTPS_PORT ) )
298 aBuf.appendAscii( ":" );
299 aBuf.append( OUString::number( nPort ) );
301 return aBuf.makeStringAndClear();
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */