LibreOffice Module ucb (master)  1
SerfUri.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
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"
26 
27 #include <urihelper.hxx>
28 
29 using namespace http_dav_ucp;
30 
31 
32 SerfUri::SerfUri( const apr_uri_t * inUri )
33  : mAprUri( *inUri )
34  , mURI()
35  , mScheme()
36  , mUserInfo()
37  , mHostName()
38  , mPort()
39  , mPath()
40 {
41  if ( inUri == nullptr )
43 
44  char * uri = apr_uri_unparse( apr_environment::AprEnv::getAprEnv()->getAprPool(), &mAprUri, 0 );
45 
46  if ( uri == nullptr )
48 
49  init( &mAprUri );
50 
51  calculateURI();
52 }
53 
54 SerfUri::SerfUri( const OUString & inUri )
55  : mAprUri()
56  , mURI()
57  , mScheme()
58  , mUserInfo()
59  , mHostName()
60  , mPort()
61  , mPath()
62 {
63  if ( inUri.getLength() <= 0 )
65 
66  // #i77023#
67  OUString aEscapedUri( ucb_impl::urihelper::encodeURI( inUri ) );
68 
69  OString theInputUri(
70  aEscapedUri.getStr(), aEscapedUri.getLength(), RTL_TEXTENCODING_UTF8 );
71 
72  if ( apr_uri_parse( apr_environment::AprEnv::getAprEnv()->getAprPool(),
73  theInputUri.getStr(), &mAprUri ) != APR_SUCCESS )
74  {
76  }
77  if ( !mAprUri.port )
78  {
79  mAprUri.port = apr_uri_port_of_scheme( mAprUri.scheme );
80  }
81  if ( !mAprUri.path )
82  {
83  mAprUri.path = const_cast<char *>("/");
84  }
85 
86  init( &mAprUri );
87 
88  calculateURI();
89 }
90 
91 void SerfUri::init( const apr_uri_t * pUri )
92 {
93  mScheme = OStringToOUString( pUri->scheme, RTL_TEXTENCODING_UTF8 );
94  mUserInfo = OStringToOUString( pUri->user, RTL_TEXTENCODING_UTF8 );
95  mHostName = OStringToOUString( pUri->hostname, RTL_TEXTENCODING_UTF8 );
96  mPort = pUri->port;
97  mPath = OStringToOUString( pUri->path, RTL_TEXTENCODING_UTF8 );
98 
99  if ( pUri->query )
100  {
101  mPath += "?";
102  mPath += OStringToOUString( pUri->query, RTL_TEXTENCODING_UTF8 );
103  }
104 
105  if ( pUri->fragment )
106  {
107  mPath += "#";
108  mPath += OStringToOUString( pUri->fragment, RTL_TEXTENCODING_UTF8 );
109  }
110 }
111 
113 {
114 }
115 
117 {
118  OUStringBuffer aBuf( mScheme );
119  aBuf.append( "://" );
120  if ( mUserInfo.getLength() > 0 )
121  {
122  aBuf.append( mUserInfo );
123  aBuf.append( "@" );
124  }
125  // Is host a numeric IPv6 address?
126  if ( ( mHostName.indexOf( ':' ) != -1 ) &&
127  ( mHostName[ 0 ] != '[' ) )
128  {
129  aBuf.append( "[" );
130  aBuf.append( mHostName );
131  aBuf.append( "]" );
132  }
133  else
134  {
135  aBuf.append( mHostName );
136  }
137 
138  // append port, but only, if not default port.
139  bool bAppendPort = true;
140  switch ( mPort )
141  {
142  case DEFAULT_HTTP_PORT:
143  bAppendPort = (mScheme != "http");
144  break;
145 
146  case DEFAULT_HTTPS_PORT:
147  bAppendPort = (mScheme != "https");
148  break;
149  }
150  if ( bAppendPort )
151  {
152  aBuf.append( ":" );
153  aBuf.append( OUString::number( mPort ) );
154  }
155  aBuf.append( mPath );
156 
157  mURI = aBuf.makeStringAndClear();
158 }
159 
160 OUString SerfUri::GetPathBaseName () const
161 {
162  sal_Int32 nPos = mPath.lastIndexOf ('/');
163  sal_Int32 nTrail = 0;
164  if (nPos == mPath.getLength () - 1)
165  {
166  // Trailing slash found. Skip.
167  nTrail = 1;
168  nPos = mPath.lastIndexOf ('/', nPos);
169  }
170  if (nPos != -1)
171  {
172  OUString aTemp(
173  mPath.copy (nPos + 1, mPath.getLength () - nPos - 1 - nTrail) );
174 
175  // query, fragment present?
176  nPos = aTemp.indexOf( '?' );
177  if ( nPos == -1 )
178  nPos = aTemp.indexOf( '#' );
179 
180  if ( nPos != -1 )
181  aTemp = aTemp.copy( 0, nPos );
182 
183  return aTemp;
184  }
185  else
186  return OUString("/");
187 }
188 
189 bool SerfUri::operator== ( const SerfUri & rOther ) const
190 {
191  return ( mURI == rOther.mURI );
192 }
193 
195 {
196  return unescape( GetPathBaseName() );
197 }
198 
199 void SerfUri::AppendPath (const OUString& rPath)
200 {
201  if (mPath.lastIndexOf ('/') != mPath.getLength () - 1)
202  mPath += "/";
203 
204  mPath += rPath;
205  calculateURI ();
206 };
207 
208 // static
209 OUString SerfUri::escapeSegment( const OUString& segment )
210 {
211  return rtl::Uri::encode( segment,
212  rtl_UriCharClassPchar,
213  rtl_UriEncodeIgnoreEscapes,
214  RTL_TEXTENCODING_UTF8 );
215 }
216 
217 // static
218 OUString SerfUri::unescape( const OUString& segment )
219 {
220  return rtl::Uri::decode( segment,
221  rtl_UriDecodeWithCharset,
222  RTL_TEXTENCODING_UTF8 );
223 }
224 
225 // static
227  const OUString & rHostName, int nPort )
228 {
229  OUStringBuffer aBuf;
230 
231  // Is host a numeric IPv6 address?
232  if ( ( rHostName.indexOf( ':' ) != -1 ) &&
233  ( rHostName[ 0 ] != '[' ) )
234  {
235  aBuf.append( "[" );
236  aBuf.append( rHostName );
237  aBuf.append( "]" );
238  }
239  else
240  {
241  aBuf.append( rHostName );
242  }
243 
244  if ( ( nPort != DEFAULT_HTTP_PORT ) && ( nPort != DEFAULT_HTTPS_PORT ) )
245  {
246  aBuf.append( ":" );
247  aBuf.append( OUString::number( sal_Int32( nPort ) ) );
248  }
249  return aBuf.makeStringAndClear();
250 }
251 
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator==(const SerfUri &rOther) const
Definition: SerfUri.cxx:189
OUString GetPathBaseName() const
Definition: SerfUri.cxx:160
void AppendPath(const OUString &rPath)
Definition: SerfUri.cxx:199
#define DEFAULT_HTTPS_PORT
Definition: SerfUri.hxx:32
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:45
aBuf
OUString makeConnectionEndPointString() const
Definition: SerfUri.hxx:96
static AprEnv * getAprEnv()
Definition: AprEnv.cxx:45
#define DEFAULT_HTTP_PORT
Definition: SerfUri.hxx:31
static OUString escapeSegment(const OUString &segment)
Definition: SerfUri.cxx:209
SerfUri(const OUString &inUri)
Definition: SerfUri.cxx:54
OUString GetPathBaseNameUnescaped() const
Definition: SerfUri.cxx:194
sal_Int32 nPos
static OUString unescape(const OUString &string)
Definition: SerfUri.cxx:218
void init(const apr_uri_t *pUri)
Definition: SerfUri.cxx:91