fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / ucp / webdav / SerfUri.cxx
blobdf299338667eb84e0c497a670dd978df39223960
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <string.h>
21 #include <rtl/uri.hxx>
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include "SerfUri.hxx"
25 #include "DAVException.hxx"
26 #include "AprEnv.hxx"
28 #include "../inc/urihelper.hxx"
30 using namespace http_dav_ucp;
33 SerfUri::SerfUri( const apr_uri_t * inUri )
34 throw ( DAVException )
35 : mAprUri( *inUri )
36 , mURI()
37 , mScheme()
38 , mUserInfo()
39 , mHostName()
40 , mPort()
41 , mPath()
43 if ( inUri == 0 )
44 throw DAVException( DAVException::DAV_INVALID_ARG );
46 char * uri = apr_uri_unparse( apr_environment::AprEnv::getAprEnv()->getAprPool(), &mAprUri, 0 );
48 if ( uri == 0 )
49 throw DAVException( DAVException::DAV_INVALID_ARG );
51 init( &mAprUri );
53 calculateURI();
56 SerfUri::SerfUri( const OUString & inUri )
57 throw ( DAVException )
58 : mAprUri()
59 , mURI()
60 , mScheme()
61 , mUserInfo()
62 , mHostName()
63 , mPort()
64 , mPath()
66 if ( inUri.getLength() <= 0 )
67 throw DAVException( DAVException::DAV_INVALID_ARG );
69 // #i77023#
70 OUString aEscapedUri( ucb_impl::urihelper::encodeURI( inUri ) );
72 OString theInputUri(
73 aEscapedUri.getStr(), aEscapedUri.getLength(), RTL_TEXTENCODING_UTF8 );
75 if ( apr_uri_parse( apr_environment::AprEnv::getAprEnv()->getAprPool(),
76 theInputUri.getStr(), &mAprUri ) != APR_SUCCESS )
78 throw DAVException( DAVException::DAV_INVALID_ARG );
80 if ( !mAprUri.port )
82 mAprUri.port = apr_uri_port_of_scheme( mAprUri.scheme );
84 if ( !mAprUri.path )
86 mAprUri.path = (char *)"/";
89 init( &mAprUri );
91 calculateURI();
94 void SerfUri::init( const apr_uri_t * pUri )
96 mScheme = OStringToOUString( pUri->scheme, RTL_TEXTENCODING_UTF8 );
97 mUserInfo = OStringToOUString( pUri->user, RTL_TEXTENCODING_UTF8 );
98 mHostName = OStringToOUString( pUri->hostname, RTL_TEXTENCODING_UTF8 );
99 mPort = pUri->port;
100 mPath = OStringToOUString( pUri->path, RTL_TEXTENCODING_UTF8 );
102 if ( pUri->query )
104 mPath += "?";
105 mPath += OStringToOUString( pUri->query, RTL_TEXTENCODING_UTF8 );
108 if ( pUri->fragment )
110 mPath += "#";
111 mPath += OStringToOUString( pUri->fragment, RTL_TEXTENCODING_UTF8 );
115 SerfUri::~SerfUri( )
119 void SerfUri::calculateURI ()
121 OUStringBuffer aBuf( mScheme );
122 aBuf.append( "://" );
123 if ( mUserInfo.getLength() > 0 )
125 aBuf.append( mUserInfo );
126 aBuf.append( "@" );
128 // Is host a numeric IPv6 address?
129 if ( ( mHostName.indexOf( ':' ) != -1 ) &&
130 ( mHostName[ 0 ] != '[' ) )
132 aBuf.append( "[" );
133 aBuf.append( mHostName );
134 aBuf.append( "]" );
136 else
138 aBuf.append( mHostName );
141 // append port, but only, if not default port.
142 bool bAppendPort = true;
143 switch ( mPort )
145 case DEFAULT_HTTP_PORT:
146 bAppendPort = (mScheme != "http");
147 break;
149 case DEFAULT_HTTPS_PORT:
150 bAppendPort = (mScheme != "https");
151 break;
153 if ( bAppendPort )
155 aBuf.append( ":" );
156 aBuf.append( OUString::number( mPort ) );
158 aBuf.append( mPath );
160 mURI = aBuf.makeStringAndClear();
163 OUString SerfUri::GetPathBaseName () const
165 sal_Int32 nPos = mPath.lastIndexOf ('/');
166 sal_Int32 nTrail = 0;
167 if (nPos == mPath.getLength () - 1)
169 // Trailing slash found. Skip.
170 nTrail = 1;
171 nPos = mPath.lastIndexOf ('/', nPos);
173 if (nPos != -1)
175 OUString aTemp(
176 mPath.copy (nPos + 1, mPath.getLength () - nPos - 1 - nTrail) );
178 // query, fragment present?
179 nPos = aTemp.indexOf( '?' );
180 if ( nPos == -1 )
181 nPos = aTemp.indexOf( '#' );
183 if ( nPos != -1 )
184 aTemp = aTemp.copy( 0, nPos );
186 return aTemp;
188 else
189 return OUString("/");
192 bool SerfUri::operator== ( const SerfUri & rOther ) const
194 return ( mURI == rOther.mURI );
197 OUString SerfUri::GetPathBaseNameUnescaped () const
199 return unescape( GetPathBaseName() );
202 void SerfUri::AppendPath (const OUString& rPath)
204 if (mPath.lastIndexOf ('/') != mPath.getLength () - 1)
205 mPath += OUString("/");
207 mPath += rPath;
208 calculateURI ();
211 // static
212 OUString SerfUri::escapeSegment( const OUString& segment )
214 return rtl::Uri::encode( segment,
215 rtl_UriCharClassPchar,
216 rtl_UriEncodeIgnoreEscapes,
217 RTL_TEXTENCODING_UTF8 );
220 // static
221 OUString SerfUri::unescape( const OUString& segment )
223 return rtl::Uri::decode( segment,
224 rtl_UriDecodeWithCharset,
225 RTL_TEXTENCODING_UTF8 );
228 // static
229 OUString SerfUri::makeConnectionEndPointString(
230 const OUString & rHostName, int nPort )
232 OUStringBuffer aBuf;
234 // Is host a numeric IPv6 address?
235 if ( ( rHostName.indexOf( ':' ) != -1 ) &&
236 ( rHostName[ 0 ] != '[' ) )
238 aBuf.append( "[" );
239 aBuf.append( rHostName );
240 aBuf.append( "]" );
242 else
244 aBuf.append( rHostName );
247 if ( ( nPort != DEFAULT_HTTP_PORT ) && ( nPort != DEFAULT_HTTPS_PORT ) )
249 aBuf.append( ":" );
250 aBuf.append( OUString::number( sal_Int32( nPort ) ) );
252 return aBuf.makeStringAndClear();
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */