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 // FIXME: not sure whether initializing a ne_uri statically is supposed to work
44 // the string fields of ne_uri are char*, not const char*
47 #pragma GCC diagnostic ignored "-Wwrite-strings"
52 const ne_uri g_sUriDefaultsHTTP
= { const_cast<char *>("http"),
59 const ne_uri g_sUriDefaultsHTTPS
= { const_cast<char *>("https"),
66 const ne_uri g_sUriDefaultsFTP
= { const_cast<char *>("ftp"),
75 NeonUri::NeonUri( const ne_uri
* inUri
)
76 throw ( DAVException
)
79 throw DAVException( DAVException::DAV_INVALID_ARG
);
81 char * uri
= ne_uri_unparse( inUri
);
84 throw DAVException( DAVException::DAV_INVALID_ARG
);
86 init( OString( uri
), inUri
);
92 NeonUri::NeonUri( const OUString
& inUri
)
93 throw ( DAVException
)
95 if ( inUri
.isEmpty() )
96 throw DAVException( DAVException::DAV_INVALID_ARG
);
99 OUString
aEscapedUri( ucb_impl::urihelper::encodeURI( inUri
) );
102 aEscapedUri
.getStr(), aEscapedUri
.getLength(), RTL_TEXTENCODING_UTF8
);
105 if ( ne_uri_parse( theInputUri
.getStr(), &theUri
) != 0 )
107 ne_uri_free( &theUri
);
108 throw DAVException( DAVException::DAV_INVALID_ARG
);
111 init( theInputUri
, &theUri
);
112 ne_uri_free( &theUri
);
117 void NeonUri::init( const OString
& rUri
, const ne_uri
* pUri
)
120 const ne_uri
* pUriDefs
121 = rUri
.matchIgnoreAsciiCase( "ftp:" ) ?
123 rUri
.matchIgnoreAsciiCase( "https:" ) ?
124 &g_sUriDefaultsHTTPS
:
127 mScheme
= OStringToOUString(
128 pUri
->scheme
? pUri
->scheme
: pUriDefs
->scheme
,
129 RTL_TEXTENCODING_UTF8
);
130 mUserInfo
= OStringToOUString(
131 pUri
->userinfo
? pUri
->userinfo
: pUriDefs
->userinfo
,
132 RTL_TEXTENCODING_UTF8
);
133 mHostName
= OStringToOUString(
134 pUri
->host
? pUri
->host
: pUriDefs
->host
,
135 RTL_TEXTENCODING_UTF8
);
136 mPort
= pUri
->port
> 0 ? pUri
->port
: pUriDefs
->port
;
137 mPath
= OStringToOUString(
138 pUri
->path
? pUri
->path
: pUriDefs
->path
,
139 RTL_TEXTENCODING_UTF8
);
143 mPath
+= "?" + OStringToOUString( pUri
->query
, RTL_TEXTENCODING_UTF8
);
146 if ( pUri
->fragment
)
148 mPath
+= "#" + OStringToOUString( pUri
->fragment
, RTL_TEXTENCODING_UTF8
);
156 void NeonUri::calculateURI ()
158 OUStringBuffer
aBuf( mScheme
);
159 aBuf
.appendAscii( "://" );
160 if ( !mUserInfo
.isEmpty() )
162 //TODO! differentiate between empty and missing userinfo
163 aBuf
.append( mUserInfo
);
164 aBuf
.appendAscii( "@" );
166 // Is host a numeric IPv6 address?
167 if ( ( mHostName
.indexOf( ':' ) != -1 ) &&
168 ( mHostName
[ 0 ] != '[' ) )
170 aBuf
.appendAscii( "[" );
171 aBuf
.append( mHostName
);
172 aBuf
.appendAscii( "]" );
176 aBuf
.append( mHostName
);
179 // append port, but only, if not default port.
180 bool bAppendPort
= true;
183 case DEFAULT_HTTP_PORT
:
184 bAppendPort
= mScheme
!= "http";
187 case DEFAULT_HTTPS_PORT
:
188 bAppendPort
= mScheme
!= "https";
191 case DEFAULT_FTP_PORT
:
192 bAppendPort
= mScheme
!= "ftp";
197 aBuf
.appendAscii( ":" );
198 aBuf
.append( OUString::number( mPort
) );
200 aBuf
.append( mPath
);
202 mURI
= aBuf
.makeStringAndClear();
205 OUString
NeonUri::GetPathBaseName () const
207 sal_Int32 nPos
= mPath
.lastIndexOf ('/');
208 sal_Int32 nTrail
= 0;
209 if (nPos
== mPath
.getLength () - 1)
211 // Trailing slash found. Skip.
213 nPos
= mPath
.lastIndexOf ('/', nPos
);
218 mPath
.copy (nPos
+ 1, mPath
.getLength () - nPos
- 1 - nTrail
) );
220 // query, fragment present?
221 nPos
= aTemp
.indexOf( '?' );
223 nPos
= aTemp
.indexOf( '#' );
226 aTemp
= aTemp
.copy( 0, nPos
);
231 return OUString("/");
234 bool NeonUri::operator== ( const NeonUri
& rOther
) const
236 return ( mURI
== rOther
.mURI
);
239 OUString
NeonUri::GetPathBaseNameUnescaped () const
241 return unescape( GetPathBaseName() );
244 void NeonUri::AppendPath (const OUString
& rPath
)
246 if (mPath
.lastIndexOf ('/') != mPath
.getLength () - 1)
254 OUString
NeonUri::escapeSegment( const OUString
& segment
)
256 return rtl::Uri::encode( segment
,
257 rtl_UriCharClassPchar
,
258 rtl_UriEncodeIgnoreEscapes
,
259 RTL_TEXTENCODING_UTF8
);
263 OUString
NeonUri::unescape( const OUString
& segment
)
265 return rtl::Uri::decode( segment
,
266 rtl_UriDecodeWithCharset
,
267 RTL_TEXTENCODING_UTF8
);
271 OUString
NeonUri::makeConnectionEndPointString(
272 const OUString
& rHostName
, int nPort
)
276 // Is host a numeric IPv6 address?
277 if ( ( rHostName
.indexOf( ':' ) != -1 ) &&
278 ( rHostName
[ 0 ] != '[' ) )
280 aBuf
.appendAscii( "[" );
281 aBuf
.append( rHostName
);
282 aBuf
.appendAscii( "]" );
286 aBuf
.append( rHostName
);
289 if ( ( nPort
!= DEFAULT_HTTP_PORT
) && ( nPort
!= DEFAULT_HTTPS_PORT
) )
291 aBuf
.appendAscii( ":" );
292 aBuf
.append( OUString::number( nPort
) );
294 return aBuf
.makeStringAndClear();
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */