fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / ucp / webdav-neon / NeonUri.cxx
blob596293199a20a2dfe1e6dd20f55c26ba7802f302
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 // 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*
46 #ifdef __GNUC__
47 #pragma GCC diagnostic ignored "-Wwrite-strings"
48 #endif
50 namespace {
52 const ne_uri g_sUriDefaultsHTTP = { const_cast<char *>("http"),
53 NULL,
54 NULL,
55 DEFAULT_HTTP_PORT,
56 NULL,
57 NULL,
58 NULL };
59 const ne_uri g_sUriDefaultsHTTPS = { const_cast<char *>("https"),
60 NULL,
61 NULL,
62 DEFAULT_HTTPS_PORT,
63 NULL,
64 NULL,
65 NULL };
66 const ne_uri g_sUriDefaultsFTP = { const_cast<char *>("ftp"),
67 NULL,
68 NULL,
69 DEFAULT_FTP_PORT,
70 NULL,
71 NULL,
72 NULL };
73 } // namespace
75 NeonUri::NeonUri( const ne_uri * inUri )
76 throw ( DAVException )
78 if ( inUri == 0 )
79 throw DAVException( DAVException::DAV_INVALID_ARG );
81 char * uri = ne_uri_unparse( inUri );
83 if ( uri == 0 )
84 throw DAVException( DAVException::DAV_INVALID_ARG );
86 init( OString( uri ), inUri );
87 ne_free( uri );
89 calculateURI();
92 NeonUri::NeonUri( const OUString & inUri )
93 throw ( DAVException )
95 if ( inUri.isEmpty() )
96 throw DAVException( DAVException::DAV_INVALID_ARG );
98 // #i77023#
99 OUString aEscapedUri( ucb_impl::urihelper::encodeURI( inUri ) );
101 OString theInputUri(
102 aEscapedUri.getStr(), aEscapedUri.getLength(), RTL_TEXTENCODING_UTF8 );
104 ne_uri theUri;
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 );
114 calculateURI();
117 void NeonUri::init( const OString & rUri, const ne_uri * pUri )
119 // Complete URI.
120 const ne_uri * pUriDefs
121 = rUri.matchIgnoreAsciiCase( "ftp:" ) ?
122 &g_sUriDefaultsFTP :
123 rUri.matchIgnoreAsciiCase( "https:" ) ?
124 &g_sUriDefaultsHTTPS :
125 &g_sUriDefaultsHTTP;
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 );
141 if ( pUri->query )
143 mPath += "?" + OStringToOUString( pUri->query, RTL_TEXTENCODING_UTF8 );
146 if ( pUri->fragment )
148 mPath += "#" + OStringToOUString( pUri->fragment, RTL_TEXTENCODING_UTF8 );
152 NeonUri::~NeonUri( )
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( "]" );
174 else
176 aBuf.append( mHostName );
179 // append port, but only, if not default port.
180 bool bAppendPort = true;
181 switch ( mPort )
183 case DEFAULT_HTTP_PORT:
184 bAppendPort = mScheme != "http";
185 break;
187 case DEFAULT_HTTPS_PORT:
188 bAppendPort = mScheme != "https";
189 break;
191 case DEFAULT_FTP_PORT:
192 bAppendPort = mScheme != "ftp";
193 break;
195 if ( bAppendPort )
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.
212 nTrail = 1;
213 nPos = mPath.lastIndexOf ('/', nPos);
215 if (nPos != -1)
217 OUString aTemp(
218 mPath.copy (nPos + 1, mPath.getLength () - nPos - 1 - nTrail) );
220 // query, fragment present?
221 nPos = aTemp.indexOf( '?' );
222 if ( nPos == -1 )
223 nPos = aTemp.indexOf( '#' );
225 if ( nPos != -1 )
226 aTemp = aTemp.copy( 0, nPos );
228 return aTemp;
230 else
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)
247 mPath += "/";
249 mPath += rPath;
250 calculateURI ();
253 // static
254 OUString NeonUri::escapeSegment( const OUString& segment )
256 return rtl::Uri::encode( segment,
257 rtl_UriCharClassPchar,
258 rtl_UriEncodeIgnoreEscapes,
259 RTL_TEXTENCODING_UTF8 );
262 // static
263 OUString NeonUri::unescape( const OUString& segment )
265 return rtl::Uri::decode( segment,
266 rtl_UriDecodeWithCharset,
267 RTL_TEXTENCODING_UTF8 );
270 // static
271 OUString NeonUri::makeConnectionEndPointString(
272 const OUString & rHostName, int nPort )
274 OUStringBuffer aBuf;
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( "]" );
284 else
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: */