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 .
20 #include <rtl/uri.hxx>
21 #include <rtl/ustring.hxx>
22 #include <rtl/ustrbuf.hxx>
23 #include "SerfUri.hxx"
24 #include "DAVException.hxx"
27 #include <urihelper.hxx>
29 using namespace http_dav_ucp
;
32 SerfUri::SerfUri( const apr_uri_t
* inUri
)
41 if ( inUri
== nullptr )
42 throw DAVException( DAVException::DAV_INVALID_ARG
);
44 char * uri
= apr_uri_unparse( apr_environment::AprEnv::getAprEnv()->getAprPool(), &mAprUri
, 0 );
47 throw DAVException( DAVException::DAV_INVALID_ARG
);
54 SerfUri::SerfUri( const OUString
& inUri
)
63 if ( inUri
.getLength() <= 0 )
64 throw DAVException( DAVException::DAV_INVALID_ARG
);
67 OUString
aEscapedUri( ucb_impl::urihelper::encodeURI( inUri
) );
70 aEscapedUri
.getStr(), aEscapedUri
.getLength(), RTL_TEXTENCODING_UTF8
);
72 if ( apr_uri_parse( apr_environment::AprEnv::getAprEnv()->getAprPool(),
73 theInputUri
.getStr(), &mAprUri
) != APR_SUCCESS
)
75 throw DAVException( DAVException::DAV_INVALID_ARG
);
79 mAprUri
.port
= apr_uri_port_of_scheme( mAprUri
.scheme
);
83 mAprUri
.path
= const_cast<char *>("/");
91 void SerfUri::init( const apr_uri_t
* pUri
)
93 mScheme
= pUri
->scheme
? OStringToOUString( pUri
->scheme
, RTL_TEXTENCODING_UTF8
) : "";
94 mUserInfo
= pUri
->user
? OStringToOUString( pUri
->user
, RTL_TEXTENCODING_UTF8
) : "";
95 mHostName
= pUri
->hostname
? OStringToOUString( pUri
->hostname
, RTL_TEXTENCODING_UTF8
) : "";
97 mPath
= OStringToOUString( pUri
->path
, RTL_TEXTENCODING_UTF8
);
102 mPath
+= OStringToOUString( pUri
->query
, RTL_TEXTENCODING_UTF8
);
105 if ( pUri
->fragment
)
108 mPath
+= OStringToOUString( pUri
->fragment
, RTL_TEXTENCODING_UTF8
);
112 void SerfUri::calculateURI ()
114 OUStringBuffer
aBuf( mScheme
);
115 aBuf
.append( "://" );
116 if ( mUserInfo
.getLength() > 0 )
118 aBuf
.append( mUserInfo
);
121 // Is host a numeric IPv6 address?
122 if ( ( mHostName
.indexOf( ':' ) != -1 ) &&
123 ( mHostName
[ 0 ] != '[' ) )
126 aBuf
.append( mHostName
);
131 aBuf
.append( mHostName
);
134 // append port, but only, if not default port.
135 bool bAppendPort
= true;
138 case DEFAULT_HTTP_PORT
:
139 bAppendPort
= (mScheme
!= "http");
142 case DEFAULT_HTTPS_PORT
:
143 bAppendPort
= (mScheme
!= "https");
149 aBuf
.append( mPort
);
151 aBuf
.append( mPath
);
153 mURI
= aBuf
.makeStringAndClear();
156 OUString
SerfUri::GetPathBaseName () const
158 sal_Int32 nPos
= mPath
.lastIndexOf ('/');
159 sal_Int32 nTrail
= 0;
160 if (nPos
== mPath
.getLength () - 1)
162 // Trailing slash found. Skip.
164 nPos
= mPath
.lastIndexOf ('/', nPos
);
169 mPath
.copy (nPos
+ 1, mPath
.getLength () - nPos
- 1 - nTrail
) );
171 // query, fragment present?
172 nPos
= aTemp
.indexOf( '?' );
174 nPos
= aTemp
.indexOf( '#' );
177 aTemp
= aTemp
.copy( 0, nPos
);
185 bool SerfUri::operator== ( const SerfUri
& rOther
) const
187 return ( mURI
== rOther
.mURI
);
190 OUString
SerfUri::GetPathBaseNameUnescaped () const
192 return unescape( GetPathBaseName() );
195 void SerfUri::AppendPath (const OUString
& rPath
)
197 if (mPath
.lastIndexOf ('/') != mPath
.getLength () - 1)
205 OUString
SerfUri::escapeSegment( const OUString
& segment
)
207 return rtl::Uri::encode( segment
,
208 rtl_UriCharClassPchar
,
209 rtl_UriEncodeIgnoreEscapes
,
210 RTL_TEXTENCODING_UTF8
);
214 OUString
SerfUri::unescape( const OUString
& segment
)
216 return rtl::Uri::decode( segment
,
217 rtl_UriDecodeWithCharset
,
218 RTL_TEXTENCODING_UTF8
);
222 OUString
SerfUri::makeConnectionEndPointString(
223 const OUString
& rHostName
, int nPort
)
227 // Is host a numeric IPv6 address?
228 if ( ( rHostName
.indexOf( ':' ) != -1 ) &&
229 ( rHostName
[ 0 ] != '[' ) )
232 aBuf
.append( rHostName
);
237 aBuf
.append( rHostName
);
240 if ( ( nPort
!= DEFAULT_HTTP_PORT
) && ( nPort
!= DEFAULT_HTTPS_PORT
) )
243 aBuf
.append( sal_Int32( nPort
) );
245 return aBuf
.makeStringAndClear();
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */