1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <rtl/uri.hxx>
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
24 #include "SerfUri.hxx"
25 #include "DAVException.hxx"
28 #include "../inc/urihelper.hxx"
30 using namespace http_dav_ucp
;
33 SerfUri::SerfUri( const apr_uri_t
* inUri
)
34 throw ( DAVException
)
44 throw DAVException( DAVException::DAV_INVALID_ARG
);
46 char * uri
= apr_uri_unparse( apr_environment::AprEnv::getAprEnv()->getAprPool(), &mAprUri
, 0 );
49 throw DAVException( DAVException::DAV_INVALID_ARG
);
56 SerfUri::SerfUri( const OUString
& inUri
)
57 throw ( DAVException
)
66 if ( inUri
.getLength() <= 0 )
67 throw DAVException( DAVException::DAV_INVALID_ARG
);
70 OUString
aEscapedUri( ucb_impl::urihelper::encodeURI( inUri
) );
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
);
82 mAprUri
.port
= apr_uri_port_of_scheme( mAprUri
.scheme
);
86 mAprUri
.path
= (char *)"/";
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
);
100 mPath
= OStringToOUString( pUri
->path
, RTL_TEXTENCODING_UTF8
);
105 mPath
+= OStringToOUString( pUri
->query
, RTL_TEXTENCODING_UTF8
);
108 if ( pUri
->fragment
)
111 mPath
+= OStringToOUString( pUri
->fragment
, RTL_TEXTENCODING_UTF8
);
119 void SerfUri::calculateURI ()
121 OUStringBuffer
aBuf( mScheme
);
122 aBuf
.append( "://" );
123 if ( mUserInfo
.getLength() > 0 )
125 aBuf
.append( mUserInfo
);
128 // Is host a numeric IPv6 address?
129 if ( ( mHostName
.indexOf( ':' ) != -1 ) &&
130 ( mHostName
[ 0 ] != '[' ) )
133 aBuf
.append( mHostName
);
138 aBuf
.append( mHostName
);
141 // append port, but only, if not default port.
142 bool bAppendPort
= true;
145 case DEFAULT_HTTP_PORT
:
146 bAppendPort
= (mScheme
!= "http");
149 case DEFAULT_HTTPS_PORT
:
150 bAppendPort
= (mScheme
!= "https");
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.
171 nPos
= mPath
.lastIndexOf ('/', nPos
);
176 mPath
.copy (nPos
+ 1, mPath
.getLength () - nPos
- 1 - nTrail
) );
178 // query, fragment present?
179 nPos
= aTemp
.indexOf( '?' );
181 nPos
= aTemp
.indexOf( '#' );
184 aTemp
= aTemp
.copy( 0, nPos
);
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("/");
212 OUString
SerfUri::escapeSegment( const OUString
& segment
)
214 return rtl::Uri::encode( segment
,
215 rtl_UriCharClassPchar
,
216 rtl_UriEncodeIgnoreEscapes
,
217 RTL_TEXTENCODING_UTF8
);
221 OUString
SerfUri::unescape( const OUString
& segment
)
223 return rtl::Uri::decode( segment
,
224 rtl_UriDecodeWithCharset
,
225 RTL_TEXTENCODING_UTF8
);
229 OUString
SerfUri::makeConnectionEndPointString(
230 const OUString
& rHostName
, int nPort
)
234 // Is host a numeric IPv6 address?
235 if ( ( rHostName
.indexOf( ':' ) != -1 ) &&
236 ( rHostName
[ 0 ] != '[' ) )
239 aBuf
.append( rHostName
);
244 aBuf
.append( rHostName
);
247 if ( ( nPort
!= DEFAULT_HTTP_PORT
) && ( nPort
!= DEFAULT_HTTPS_PORT
) )
250 aBuf
.append( OUString::number( sal_Int32( nPort
) ) );
252 return aBuf
.makeStringAndClear();
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */