Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / ucb / source / ucp / webdav / SerfUri.cxx
blob704d8eb3627b8ae1034844384956040fa3e8e491
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;
32 # if defined __SUNPRO_CC
33 # pragma enable_warn
34 #endif
36 // -------------------------------------------------------------------
37 // Constructor
38 // -------------------------------------------------------------------
40 namespace {
42 inline bool matchIgnoreAsciiCase(rtl::OString const & rStr1,
43 sal_Char const * pStr2,
44 sal_Int32 nStr2Len) SAL_THROW(())
46 return
47 rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
48 rStr1.getStr(), rStr1.getLength(), pStr2, nStr2Len, nStr2Len)
49 == 0;
54 SerfUri::SerfUri( const apr_uri_t * inUri )
55 throw ( DAVException )
56 : mAprUri( *inUri )
57 , mURI()
58 , mScheme()
59 , mUserInfo()
60 , mHostName()
61 , mPort()
62 , mPath()
64 if ( inUri == 0 )
65 throw DAVException( DAVException::DAV_INVALID_ARG );
67 char * uri = apr_uri_unparse( apr_environment::AprEnv::getAprEnv()->getAprPool(), &mAprUri, 0 );
69 if ( uri == 0 )
70 throw DAVException( DAVException::DAV_INVALID_ARG );
72 init( &mAprUri );
74 calculateURI();
77 SerfUri::SerfUri( const rtl::OUString & inUri )
78 throw ( DAVException )
79 : mAprUri()
80 , mURI()
81 , mScheme()
82 , mUserInfo()
83 , mHostName()
84 , mPort()
85 , mPath()
87 if ( inUri.getLength() <= 0 )
88 throw DAVException( DAVException::DAV_INVALID_ARG );
90 // #i77023#
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 );
101 if ( !mAprUri.port )
103 mAprUri.port = apr_uri_port_of_scheme( mAprUri.scheme );
105 if ( !mAprUri.path )
107 mAprUri.path = "/";
110 init( &mAprUri );
112 calculateURI();
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 );
120 mPort = pUri->port;
121 mPath = rtl::OStringToOUString( pUri->path, RTL_TEXTENCODING_UTF8 );
123 if ( pUri->query )
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 );
136 SerfUri::~SerfUri( )
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( "]" );
157 else
159 aBuf.append( mHostName );
162 // append port, but only, if not default port.
163 bool bAppendPort = true;
164 switch ( mPort )
166 case DEFAULT_HTTP_PORT:
167 bAppendPort = !mScheme.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "http" ) );
168 break;
170 case DEFAULT_HTTPS_PORT:
171 bAppendPort = !mScheme.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "https" ) );
172 break;
174 if ( bAppendPort )
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.
191 nTrail = 1;
192 nPos = mPath.lastIndexOf ('/', nPos);
194 if (nPos != -1)
196 rtl::OUString aTemp(
197 mPath.copy (nPos + 1, mPath.getLength () - nPos - 1 - nTrail) );
199 // query, fragment present?
200 nPos = aTemp.indexOf( '?' );
201 if ( nPos == -1 )
202 nPos = aTemp.indexOf( '#' );
204 if ( nPos != -1 )
205 aTemp = aTemp.copy( 0, nPos );
207 return aTemp;
209 else
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 ("/");
228 mPath += rPath;
229 calculateURI ();
232 // static
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 );
241 // static
242 rtl::OUString SerfUri::unescape( const rtl::OUString& segment )
244 return rtl::Uri::decode( segment,
245 rtl_UriDecodeWithCharset,
246 RTL_TEXTENCODING_UTF8 );
249 // static
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( "]" );
263 else
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: */