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  OUStringBuffer aBuf( mScheme );
115  aBuf.append( "://" );
116  if ( mUserInfo.getLength() > 0 )
117  {
118  aBuf.append( mUserInfo );
119  aBuf.append( "@" );
120  }
121  // Is host a numeric IPv6 address?
122  if ( ( mHostName.indexOf( ':' ) != -1 ) &&
123  ( mHostName[ 0 ] != '[' ) )
124  {
125  aBuf.append( "[" );
126  aBuf.append( mHostName );
127  aBuf.append( "]" );
128  }
129  else
130  {
131  aBuf.append( mHostName );
132  }
133 
134  // append port, but only, if not default port.
135  bool bAppendPort = true;
136  switch ( mPort )
137  {
138  case DEFAULT_HTTP_PORT:
139  bAppendPort = (mScheme != "http");
140  break;
141 
142  case DEFAULT_HTTPS_PORT:
143  bAppendPort = (mScheme != "https");
144  break;
145  }
146  if ( bAppendPort )
147  {
148  aBuf.append( ":" );
149  aBuf.append( OUString::number( mPort ) );
150  }
151  aBuf.append( mPath );
152 
153  mURI = aBuf.makeStringAndClear();
154 }
155 
156 OUString SerfUri::GetPathBaseName () const
157 {
158  sal_Int32 nPos = mPath.lastIndexOf ('/');
159  sal_Int32 nTrail = 0;
160  if (nPos == mPath.getLength () - 1)
161  {
162  // Trailing slash found. Skip.
163  nTrail = 1;
164  nPos = mPath.lastIndexOf ('/', nPos);
165  }
166  if (nPos != -1)
167  {
168  OUString aTemp(
169  mPath.copy (nPos + 1, mPath.getLength () - nPos - 1 - nTrail) );
170 
171  // query, fragment present?
172  nPos = aTemp.indexOf( '?' );
173  if ( nPos == -1 )
174  nPos = aTemp.indexOf( '#' );
175 
176  if ( nPos != -1 )
177  aTemp = aTemp.copy( 0, nPos );
178 
179  return aTemp;
180  }
181  else
182  return "/";
183 }
184 
185 bool SerfUri::operator== ( const SerfUri & rOther ) const
186 {
187  return ( mURI == rOther.mURI );
188 }
189 
191 {
192  return unescape( GetPathBaseName() );
193 }
194 
195 void SerfUri::AppendPath (const OUString& rPath)
196 {
197  if (mPath.lastIndexOf ('/') != mPath.getLength () - 1)
198  mPath += "/";
199 
200  mPath += rPath;
201  calculateURI ();
202 };
203 
204 // static
205 OUString SerfUri::escapeSegment( const OUString& segment )
206 {
207  return rtl::Uri::encode( segment,
208  rtl_UriCharClassPchar,
209  rtl_UriEncodeIgnoreEscapes,
210  RTL_TEXTENCODING_UTF8 );
211 }
212 
213 // static
214 OUString SerfUri::unescape( const OUString& segment )
215 {
216  return rtl::Uri::decode( segment,
217  rtl_UriDecodeWithCharset,
218  RTL_TEXTENCODING_UTF8 );
219 }
220 
221 // static
223  const OUString & rHostName, int nPort )
224 {
225  OUStringBuffer aBuf;
226 
227  // Is host a numeric IPv6 address?
228  if ( ( rHostName.indexOf( ':' ) != -1 ) &&
229  ( rHostName[ 0 ] != '[' ) )
230  {
231  aBuf.append( "[" );
232  aBuf.append( rHostName );
233  aBuf.append( "]" );
234  }
235  else
236  {
237  aBuf.append( rHostName );
238  }
239 
240  if ( ( nPort != DEFAULT_HTTP_PORT ) && ( nPort != DEFAULT_HTTPS_PORT ) )
241  {
242  aBuf.append( ":" );
243  aBuf.append( OUString::number( sal_Int32( nPort ) ) );
244  }
245  return aBuf.makeStringAndClear();
246 }
247 
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator==(const SerfUri &rOther) const
Definition: SerfUri.cxx:185
OUString GetPathBaseName() const
Definition: SerfUri.cxx:156
void AppendPath(const OUString &rPath)
Definition: SerfUri.cxx:195
#define DEFAULT_HTTPS_PORT
Definition: SerfUri.hxx:32
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:45
aBuf
OUString makeConnectionEndPointString() const
Definition: SerfUri.hxx:95
static AprEnv * getAprEnv()
Definition: AprEnv.cxx:45
#define DEFAULT_HTTP_PORT
Definition: SerfUri.hxx:31
static OUString escapeSegment(const OUString &segment)
Definition: SerfUri.cxx:205
SerfUri(const OUString &inUri)
Definition: SerfUri.cxx:54
OUString GetPathBaseNameUnescaped() const
Definition: SerfUri.cxx:190
sal_uInt16 nPos
static OUString unescape(const OUString &string)
Definition: SerfUri.cxx:214
void init(const apr_uri_t *pUri)
Definition: SerfUri.cxx:91