bump product version to 7.2.5.1
[LibreOffice.git] / ucb / source / ucp / webdav / SerfUri.cxx
blobc48b4f254b72441db21d4b379e8944ea40bb0b5a
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 <rtl/uri.hxx>
21 #include <rtl/ustring.hxx>
22 #include <rtl/ustrbuf.hxx>
23 #include "SerfUri.hxx"
24 #include "DAVException.hxx"
25 #include "AprEnv.hxx"
27 #include <urihelper.hxx>
29 using namespace http_dav_ucp;
32 SerfUri::SerfUri( const apr_uri_t * inUri )
33 : mAprUri( *inUri )
34 , mURI()
35 , mScheme()
36 , mUserInfo()
37 , mHostName()
38 , mPort()
39 , mPath()
41 if ( inUri == nullptr )
42 throw DAVException( DAVException::DAV_INVALID_ARG );
44 char * uri = apr_uri_unparse( apr_environment::AprEnv::getAprEnv()->getAprPool(), &mAprUri, 0 );
46 if ( uri == nullptr )
47 throw DAVException( DAVException::DAV_INVALID_ARG );
49 init( &mAprUri );
51 calculateURI();
54 SerfUri::SerfUri( const OUString & inUri )
55 : mAprUri()
56 , mURI()
57 , mScheme()
58 , mUserInfo()
59 , mHostName()
60 , mPort()
61 , mPath()
63 if ( inUri.getLength() <= 0 )
64 throw DAVException( DAVException::DAV_INVALID_ARG );
66 // #i77023#
67 OUString aEscapedUri( ucb_impl::urihelper::encodeURI( inUri ) );
69 OString theInputUri(
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 );
77 if ( !mAprUri.port )
79 mAprUri.port = apr_uri_port_of_scheme( mAprUri.scheme );
81 if ( !mAprUri.path )
83 mAprUri.path = const_cast<char *>("/");
86 init( &mAprUri );
88 calculateURI();
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 ) : "";
96 mPort = pUri->port;
97 mPath = OStringToOUString( pUri->path, RTL_TEXTENCODING_UTF8 );
99 if ( pUri->query )
101 mPath += "?";
102 mPath += OStringToOUString( pUri->query, RTL_TEXTENCODING_UTF8 );
105 if ( pUri->fragment )
107 mPath += "#";
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 );
119 aBuf.append( "@" );
121 // Is host a numeric IPv6 address?
122 if ( ( mHostName.indexOf( ':' ) != -1 ) &&
123 ( mHostName[ 0 ] != '[' ) )
125 aBuf.append( "[" );
126 aBuf.append( mHostName );
127 aBuf.append( "]" );
129 else
131 aBuf.append( mHostName );
134 // append port, but only, if not default port.
135 bool bAppendPort = true;
136 switch ( mPort )
138 case DEFAULT_HTTP_PORT:
139 bAppendPort = (mScheme != "http");
140 break;
142 case DEFAULT_HTTPS_PORT:
143 bAppendPort = (mScheme != "https");
144 break;
146 if ( bAppendPort )
148 aBuf.append( ":" );
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.
163 nTrail = 1;
164 nPos = mPath.lastIndexOf ('/', nPos);
166 if (nPos != -1)
168 OUString aTemp(
169 mPath.copy (nPos + 1, mPath.getLength () - nPos - 1 - nTrail) );
171 // query, fragment present?
172 nPos = aTemp.indexOf( '?' );
173 if ( nPos == -1 )
174 nPos = aTemp.indexOf( '#' );
176 if ( nPos != -1 )
177 aTemp = aTemp.copy( 0, nPos );
179 return aTemp;
181 else
182 return "/";
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)
198 mPath += "/";
200 mPath += rPath;
201 calculateURI ();
204 // static
205 OUString SerfUri::escapeSegment( const OUString& segment )
207 return rtl::Uri::encode( segment,
208 rtl_UriCharClassPchar,
209 rtl_UriEncodeIgnoreEscapes,
210 RTL_TEXTENCODING_UTF8 );
213 // static
214 OUString SerfUri::unescape( const OUString& segment )
216 return rtl::Uri::decode( segment,
217 rtl_UriDecodeWithCharset,
218 RTL_TEXTENCODING_UTF8 );
221 // static
222 OUString SerfUri::makeConnectionEndPointString(
223 const OUString & rHostName, int nPort )
225 OUStringBuffer aBuf;
227 // Is host a numeric IPv6 address?
228 if ( ( rHostName.indexOf( ':' ) != -1 ) &&
229 ( rHostName[ 0 ] != '[' ) )
231 aBuf.append( "[" );
232 aBuf.append( rHostName );
233 aBuf.append( "]" );
235 else
237 aBuf.append( rHostName );
240 if ( ( nPort != DEFAULT_HTTP_PORT ) && ( nPort != DEFAULT_HTTPS_PORT ) )
242 aBuf.append( ":" );
243 aBuf.append( sal_Int32( nPort ) );
245 return aBuf.makeStringAndClear();
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */