|
1 | 1 | /* |
2 | | - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
18 | 18 |
|
19 | 19 | import java.net.URI; |
20 | 20 | import java.net.URISyntaxException; |
| 21 | +import java.net.URLEncoder; |
| 22 | +import java.nio.charset.StandardCharsets; |
21 | 23 |
|
22 | 24 | import jcifs.DialectVersion; |
23 | 25 |
|
|
34 | 36 | * @author Prafull Kumar Soni |
35 | 37 | * @author Artem Bilan |
36 | 38 | * @author Gregory Bragg |
| 39 | + * @author Jelle Smits |
37 | 40 | * |
38 | 41 | * @since 6.0 |
39 | 42 | */ |
@@ -163,16 +166,19 @@ public void setSmbMaxVersion(DialectVersion _smbMaxVersion) { |
163 | 166 | this.smbMaxVersion = _smbMaxVersion; |
164 | 167 | } |
165 | 168 |
|
166 | | - String getDomainUserPass(boolean _includePassword) { |
| 169 | + String getDomainUserPass(boolean _includePassword, boolean _urlEncode) { |
167 | 170 | String domainUserPass; |
| 171 | + String username = _urlEncode ? URLEncoder.encode(this.username, StandardCharsets.UTF_8) : this.username; |
| 172 | + String password = _urlEncode ? URLEncoder.encode(this.password, StandardCharsets.UTF_8) : this.password; |
168 | 173 | if (StringUtils.hasText(this.domain)) { |
169 | | - domainUserPass = String.format("%s;%s", this.domain, this.username); |
| 174 | + String domain = _urlEncode ? URLEncoder.encode(this.domain, StandardCharsets.UTF_8) : this.domain; |
| 175 | + domainUserPass = String.format("%s;%s", domain, username); |
170 | 176 | } |
171 | 177 | else { |
172 | | - domainUserPass = this.username; |
| 178 | + domainUserPass = username; |
173 | 179 | } |
174 | | - if (StringUtils.hasText(this.password)) { |
175 | | - domainUserPass += ":" + (_includePassword ? this.password : "********"); |
| 180 | + if (StringUtils.hasText(password)) { |
| 181 | + domainUserPass += ":" + (_includePassword ? password : "********"); |
176 | 182 | } |
177 | 183 | return domainUserPass; |
178 | 184 | } |
@@ -211,20 +217,22 @@ public final String rawUrl() { |
211 | 217 | } |
212 | 218 |
|
213 | 219 | /** |
214 | | - * Return the url string for the share connection without encoding. |
| 220 | + * Return the url string for the share connection without encoding |
| 221 | + * the host and path. The {@code domainUserPass} is encoded, as |
| 222 | + * {@link java.net.URL} requires them to be encoded otherwise its parsing fails. |
215 | 223 | * Used in the {@link SmbShare} constructor delegation. |
216 | 224 | * @param _includePassword whether password has to be masked in credentials of URL. |
217 | 225 | * @return the url string for the share connection without encoding. |
218 | 226 | * @since 6.3.8 |
219 | 227 | */ |
220 | 228 | public final String rawUrl(boolean _includePassword) { |
221 | | - String domainUserPass = getDomainUserPass(_includePassword); |
| 229 | + String domainUserPass = getDomainUserPass(_includePassword, true); |
222 | 230 | String path = cleanPath(); |
223 | 231 | return "smb://%s@%s%s".formatted(domainUserPass, getHostPort(), path); |
224 | 232 | } |
225 | 233 |
|
226 | 234 | private URI createUri(boolean _includePassword) { |
227 | | - String domainUserPass = getDomainUserPass(_includePassword); |
| 235 | + String domainUserPass = getDomainUserPass(_includePassword, false); |
228 | 236 | String path = cleanPath(); |
229 | 237 | try { |
230 | 238 | return new URI("smb", domainUserPass, this.host, this.port, path, null, null); |
|
0 commit comments