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