@@ -130,25 +130,54 @@ public function ensureSystemAddressBookExists(string $principal, string $uri, ar
130130 }, $ this ->dbConnection );
131131 }
132132
133+ private function prepareUri (string $ host , string $ path ): string {
134+ /*
135+ * The trailing slash is important for merging the uris together.
136+ *
137+ * $host is stored in oc_trusted_servers.url and usually without a trailing slash.
138+ *
139+ * Example for a report request
140+ *
141+ * $host = 'https://server.internal/cloud'
142+ * $path = 'remote.php/dav/addressbooks/system/system/system'
143+ *
144+ * Without the trailing slash, the webroot is missing:
145+ * https://server.internal/remote.php/dav/addressbooks/system/system/system
146+ *
147+ * Example for a download request
148+ *
149+ * $host = 'https://server.internal/cloud'
150+ * $path = '/cloud/remote.php/dav/addressbooks/system/system/system/Database:alice.vcf'
151+ *
152+ * The response from the remote usually contains the webroot already and must be normalized to:
153+ * https://server.internal/cloud/remote.php/dav/addressbooks/system/system/system/Database:alice.vcf
154+ */
155+ $ host = rtrim ($ host , '/ ' ) . '/ ' ;
156+
157+ $ uri = \GuzzleHttp \Psr7 \UriResolver::resolve (
158+ \GuzzleHttp \Psr7 \Utils::uriFor ($ host ),
159+ \GuzzleHttp \Psr7 \Utils::uriFor ($ path )
160+ );
161+
162+ return (string )$ uri ;
163+ }
164+
133165 /**
134166 * @throws ClientExceptionInterface
135167 */
136168 protected function requestSyncReport (string $ url , string $ userName , string $ addressBookUrl , string $ sharedSecret , ?string $ syncToken ): array {
137169 $ client = $ this ->clientService ->newClient ();
138-
139- // the trailing slash is important for merging base_uri and uri
140- $ url = rtrim ($ url , '/ ' ) . '/ ' ;
170+ $ uri = $ this ->prepareUri ($ url , $ addressBookUrl );
141171
142172 $ options = [
143173 'auth ' => [$ userName , $ sharedSecret ],
144- 'base_uri ' => $ url ,
145174 'body ' => $ this ->buildSyncCollectionRequestBody ($ syncToken ),
146175 'headers ' => ['Content-Type ' => 'application/xml ' ]
147176 ];
148177
149178 $ response = $ client ->request (
150179 'REPORT ' ,
151- $ addressBookUrl ,
180+ $ uri ,
152181 $ options
153182 );
154183
@@ -160,17 +189,14 @@ protected function requestSyncReport(string $url, string $userName, string $addr
160189
161190 protected function download (string $ url , string $ userName , string $ sharedSecret , string $ resourcePath ): string {
162191 $ client = $ this ->clientService ->newClient ();
163-
164- // the trailing slash is important for merging base_uri and uri
165- $ url = rtrim ($ url , '/ ' ) . '/ ' ;
192+ $ uri = $ this ->prepareUri ($ url , $ resourcePath );
166193
167194 $ options = [
168195 'auth ' => [$ userName , $ sharedSecret ],
169- 'base_uri ' => $ url ,
170196 ];
171197
172198 $ response = $ client ->get (
173- $ resourcePath ,
199+ $ uri ,
174200 $ options
175201 );
176202
0 commit comments