Debian Codegraph @

ucb/source/ucp/cmis/cmis_url.cxx

raw
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
10 #include <sal/config.h>
11
12 #include <rtl/uri.hxx>
13 #include <officecfg/Office/Security.hxx>
14 #include <tools/urlobj.hxx>
15
16 #include "cmis_url.hxx"
17
18 namespace cmis
19 {
20
21 static OUString CheckInsecureProtocol(OUString const& rURL)
22 {
23 std::u16string_view rest;
24 if (rURL.startsWithIgnoreAsciiCase("http://", &rest))
25 {
26 if (!officecfg::Office::Security::Net::AllowInsecureProtocols::get())
27 {
28 // "http" not allowed -> immediately redirect to "https",
29 // better than showing confusing error to user
30 return OUString::Concat("https://") + rest;
31 }
32 }
33 return rURL;
34 }
35
36
37 URL::URL( std::u16string_view urlStr )
38 {
39 INetURLObject aUrl( urlStr );
40
41 // Decode the authority to get the binding URL and repository id
42 OUString sDecodedHost = aUrl.GetHost( INetURLObject::DecodeMechanism::WithCharset );
43 INetURLObject aHostUrl( sDecodedHost );
44 m_sBindingUrl = CheckInsecureProtocol(aHostUrl.GetURLNoMark());
45 m_sRepositoryId = aHostUrl.GetMark( );
46
47 m_sUser = aUrl.GetUser( INetURLObject::DecodeMechanism::WithCharset );
48 m_sPass = aUrl.GetPass( INetURLObject::DecodeMechanism::WithCharset );
49
50 // Store the path to the object
51 m_sPath = aUrl.GetURLPath( INetURLObject::DecodeMechanism::WithCharset );
52 m_sId = aUrl.GetMark( INetURLObject::DecodeMechanism::WithCharset );
53
54 if ( m_sPath == "/" && m_sBindingUrl.indexOf( "google" ) != -1 )
55 m_sId = "root";
56 }
57
58
59 void URL::setObjectPath( const OUString& sPath )
60 {
61 m_sPath = sPath;
62 }
63
64 void URL::setObjectId( const OUString& sId )
65 {
66 m_sId = sId;
67 }
68
69 void URL::setUsername( const OUString& sUser )
70 {
71 m_sUser = sUser;
72 }
73
74 OUString URL::asString( ) const
75 {
76 OUString sUrl;
77 // Related tdf#96174, can no longer save on Google Drive
78 // the user field may contain characters that need to be escaped according to
79 // RFC3896 userinfo URI field
80 // see <https://tools.ietf.org/html/rfc3986#section-3.2.1>
81 OUString sEncodedUser = ( m_sUser.isEmpty() ?
82 OUString() :
83 rtl::Uri::encode( m_sUser, rtl_UriCharClassUserinfo,
84 rtl_UriEncodeIgnoreEscapes, RTL_TEXTENCODING_UTF8) );
85 OUString sEncodedBinding = rtl::Uri::encode(
86 m_sBindingUrl + "#" + m_sRepositoryId,
87 rtl_UriCharClassRelSegment,
88 rtl_UriEncodeKeepEscapes,
89 RTL_TEXTENCODING_UTF8 );
90 sUrl = "vnd.libreoffice.cmis://" +
91 ( sEncodedUser.isEmpty() ? OUString( ) : (sEncodedUser + "@") ) +
92 sEncodedBinding;
93
94 if ( !m_sPath.isEmpty( ) )
95 {
96 sal_Int32 nPos = -1;
97 OUStringBuffer sEncodedPath;
98 do
99 {
100 sal_Int32 nStartPos = nPos + 1;
101 nPos = m_sPath.indexOf( '/', nStartPos );
102 sal_Int32 nLen = nPos - nStartPos;
103 if ( nPos == -1 )
104 nLen = m_sPath.getLength( ) - nStartPos;
105 OUString sSegment = m_sPath.copy( nStartPos, nLen );
106
107 if ( !sSegment.isEmpty( ) )
108 {
109 sEncodedPath.append("/" + rtl::Uri::encode( sSegment,
110 rtl_UriCharClassRelSegment,
111 rtl_UriEncodeKeepEscapes,
112 RTL_TEXTENCODING_UTF8 ));
113 }
114 }
115 while ( nPos != -1 );
116 sUrl += sEncodedPath;
117 } else if ( !m_sId.isEmpty( ) )
118 {
119 sUrl += "#" + rtl::Uri::encode( m_sId,
120 rtl_UriCharClassRelSegment,
121 rtl_UriEncodeKeepEscapes,
122 RTL_TEXTENCODING_UTF8 );
123 }
124
125 return sUrl;
126 }
127 }
128
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */