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
;
32 # if defined __SUNPRO_CC
36 // -------------------------------------------------------------------
38 // -------------------------------------------------------------------
42 inline bool matchIgnoreAsciiCase(rtl::OString
const & rStr1
,
43 sal_Char
const * pStr2
,
44 sal_Int32 nStr2Len
) SAL_THROW(())
47 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
48 rStr1
.getStr(), rStr1
.getLength(), pStr2
, nStr2Len
, nStr2Len
)
54 SerfUri::SerfUri( const apr_uri_t
* inUri
)
55 throw ( DAVException
)
65 throw DAVException( DAVException::DAV_INVALID_ARG
);
67 char * uri
= apr_uri_unparse( apr_environment::AprEnv::getAprEnv()->getAprPool(), &mAprUri
, 0 );
70 throw DAVException( DAVException::DAV_INVALID_ARG
);
77 SerfUri::SerfUri( const rtl::OUString
& inUri
)
78 throw ( DAVException
)
87 if ( inUri
.getLength() <= 0 )
88 throw DAVException( DAVException::DAV_INVALID_ARG
);
91 rtl::OUString
aEscapedUri( ucb_impl::urihelper::encodeURI( inUri
) );
93 rtl::OString
theInputUri(
94 aEscapedUri
.getStr(), aEscapedUri
.getLength(), RTL_TEXTENCODING_UTF8
);
96 if ( apr_uri_parse( apr_environment::AprEnv::getAprEnv()->getAprPool(),
97 theInputUri
.getStr(), &mAprUri
) != APR_SUCCESS
)
99 throw DAVException( DAVException::DAV_INVALID_ARG
);
103 mAprUri
.port
= apr_uri_port_of_scheme( mAprUri
.scheme
);
115 void SerfUri::init( const apr_uri_t
* pUri
)
117 mScheme
= rtl::OStringToOUString( pUri
->scheme
, RTL_TEXTENCODING_UTF8
);
118 mUserInfo
= rtl::OStringToOUString( pUri
->user
, RTL_TEXTENCODING_UTF8
);
119 mHostName
= rtl::OStringToOUString( pUri
->hostname
, RTL_TEXTENCODING_UTF8
);
121 mPath
= rtl::OStringToOUString( pUri
->path
, RTL_TEXTENCODING_UTF8
);
125 mPath
+= rtl::OUString::createFromAscii( "?" );
126 mPath
+= rtl::OStringToOUString( pUri
->query
, RTL_TEXTENCODING_UTF8
);
129 if ( pUri
->fragment
)
131 mPath
+= rtl::OUString::createFromAscii( "#" );
132 mPath
+= rtl::OStringToOUString( pUri
->fragment
, RTL_TEXTENCODING_UTF8
);
140 void SerfUri::calculateURI ()
142 rtl::OUStringBuffer
aBuf( mScheme
);
143 aBuf
.appendAscii( "://" );
144 if ( mUserInfo
.getLength() > 0 )
146 aBuf
.append( mUserInfo
);
147 aBuf
.appendAscii( "@" );
149 // Is host a numeric IPv6 address?
150 if ( ( mHostName
.indexOf( ':' ) != -1 ) &&
151 ( mHostName
[ 0 ] != sal_Unicode( '[' ) ) )
153 aBuf
.appendAscii( "[" );
154 aBuf
.append( mHostName
);
155 aBuf
.appendAscii( "]" );
159 aBuf
.append( mHostName
);
162 // append port, but only, if not default port.
163 bool bAppendPort
= true;
166 case DEFAULT_HTTP_PORT
:
167 bAppendPort
= !mScheme
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "http" ) );
170 case DEFAULT_HTTPS_PORT
:
171 bAppendPort
= !mScheme
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "https" ) );
176 aBuf
.appendAscii( ":" );
177 aBuf
.append( rtl::OUString::valueOf( mPort
) );
179 aBuf
.append( mPath
);
181 mURI
= aBuf
.makeStringAndClear();
184 ::rtl::OUString
SerfUri::GetPathBaseName () const
186 sal_Int32 nPos
= mPath
.lastIndexOf ('/');
187 sal_Int32 nTrail
= 0;
188 if (nPos
== mPath
.getLength () - 1)
190 // Trailing slash found. Skip.
192 nPos
= mPath
.lastIndexOf ('/', nPos
);
197 mPath
.copy (nPos
+ 1, mPath
.getLength () - nPos
- 1 - nTrail
) );
199 // query, fragment present?
200 nPos
= aTemp
.indexOf( '?' );
202 nPos
= aTemp
.indexOf( '#' );
205 aTemp
= aTemp
.copy( 0, nPos
);
210 return rtl::OUString::createFromAscii ("/");
213 bool SerfUri::operator== ( const SerfUri
& rOther
) const
215 return ( mURI
== rOther
.mURI
);
218 ::rtl::OUString
SerfUri::GetPathBaseNameUnescaped () const
220 return unescape( GetPathBaseName() );
223 void SerfUri::AppendPath (const rtl::OUString
& rPath
)
225 if (mPath
.lastIndexOf ('/') != mPath
.getLength () - 1)
226 mPath
+= rtl::OUString::createFromAscii ("/");
233 rtl::OUString
SerfUri::escapeSegment( const rtl::OUString
& segment
)
235 return rtl::Uri::encode( segment
,
236 rtl_UriCharClassPchar
,
237 rtl_UriEncodeIgnoreEscapes
,
238 RTL_TEXTENCODING_UTF8
);
242 rtl::OUString
SerfUri::unescape( const rtl::OUString
& segment
)
244 return rtl::Uri::decode( segment
,
245 rtl_UriDecodeWithCharset
,
246 RTL_TEXTENCODING_UTF8
);
250 rtl::OUString
SerfUri::makeConnectionEndPointString(
251 const rtl::OUString
& rHostName
, int nPort
)
253 rtl::OUStringBuffer aBuf
;
255 // Is host a numeric IPv6 address?
256 if ( ( rHostName
.indexOf( ':' ) != -1 ) &&
257 ( rHostName
[ 0 ] != sal_Unicode( '[' ) ) )
259 aBuf
.appendAscii( "[" );
260 aBuf
.append( rHostName
);
261 aBuf
.appendAscii( "]" );
265 aBuf
.append( rHostName
);
268 if ( ( nPort
!= DEFAULT_HTTP_PORT
) && ( nPort
!= DEFAULT_HTTPS_PORT
) )
270 aBuf
.appendAscii( ":" );
271 aBuf
.append( rtl::OUString::valueOf( sal_Int32( nPort
) ) );
273 return aBuf
.makeStringAndClear();
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */