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"
32 #include <rtl/uri.hxx>
33 #include <rtl/ustring.hxx>
34 #include <rtl/ustrbuf.hxx>
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*
49 #if HAVE_GCC_PRAGMA_DIAGNOSTIC_MODIFY
50 #pragma GCC diagnostic ignored "-Wwrite-strings"
55 const ne_uri g_sUriDefaultsHTTP
= { (char *) "http",
62 const ne_uri g_sUriDefaultsHTTPS
= { (char *) "https",
69 const ne_uri g_sUriDefaultsFTP
= { (char *) "ftp",
78 # if defined __SUNPRO_CC
82 NeonUri::NeonUri( const ne_uri
* inUri
)
83 throw ( DAVException
)
86 throw DAVException( DAVException::DAV_INVALID_ARG
);
88 char * uri
= ne_uri_unparse( inUri
);
91 throw DAVException( DAVException::DAV_INVALID_ARG
);
93 init( OString( uri
), inUri
);
99 NeonUri::NeonUri( const OUString
& inUri
)
100 throw ( DAVException
)
102 if ( inUri
.isEmpty() )
103 throw DAVException( DAVException::DAV_INVALID_ARG
);
106 OUString
aEscapedUri( ucb_impl::urihelper::encodeURI( inUri
) );
109 aEscapedUri
.getStr(), aEscapedUri
.getLength(), RTL_TEXTENCODING_UTF8
);
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
);
124 void NeonUri::init( const OString
& rUri
, const ne_uri
* pUri
)
127 const ne_uri
* pUriDefs
128 = rUri
.matchIgnoreAsciiCase( "ftp:" ) ?
130 rUri
.matchIgnoreAsciiCase( "https:" ) ?
131 &g_sUriDefaultsHTTPS
:
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
);
150 mPath
+= "?" + OStringToOUString( pUri
->query
, RTL_TEXTENCODING_UTF8
);
153 if ( pUri
->fragment
)
155 mPath
+= "#" + OStringToOUString( pUri
->fragment
, RTL_TEXTENCODING_UTF8
);
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( "]" );
183 aBuf
.append( mHostName
);
186 // append port, but only, if not default port.
187 bool bAppendPort
= true;
190 case DEFAULT_HTTP_PORT
:
191 bAppendPort
= mScheme
!= "http";
194 case DEFAULT_HTTPS_PORT
:
195 bAppendPort
= mScheme
!= "https";
198 case DEFAULT_FTP_PORT
:
199 bAppendPort
= mScheme
!= "ftp";
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.
220 nPos
= mPath
.lastIndexOf ('/', nPos
);
225 mPath
.copy (nPos
+ 1, mPath
.getLength () - nPos
- 1 - nTrail
) );
227 // query, fragment present?
228 nPos
= aTemp
.indexOf( '?' );
230 nPos
= aTemp
.indexOf( '#' );
233 aTemp
= aTemp
.copy( 0, nPos
);
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)
261 OUString
NeonUri::escapeSegment( const OUString
& segment
)
263 return rtl::Uri::encode( segment
,
264 rtl_UriCharClassPchar
,
265 rtl_UriEncodeIgnoreEscapes
,
266 RTL_TEXTENCODING_UTF8
);
270 OUString
NeonUri::unescape( const OUString
& segment
)
272 return rtl::Uri::decode( segment
,
273 rtl_UriDecodeWithCharset
,
274 RTL_TEXTENCODING_UTF8
);
278 OUString
NeonUri::makeConnectionEndPointString(
279 const OUString
& rHostName
, int nPort
)
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( "]" );
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: */