|
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 |
|
@@ -163,16 +165,19 @@ public void setSmbMaxVersion(DialectVersion _smbMaxVersion) {
|
163 | 165 | this.smbMaxVersion = _smbMaxVersion;
|
164 | 166 | }
|
165 | 167 |
|
166 |
| - String getDomainUserPass(boolean _includePassword) { |
| 168 | + String getDomainUserPass(boolean _includePassword, boolean _urlEncode) { |
167 | 169 | String domainUserPass;
|
| 170 | + String username = _urlEncode ? URLEncoder.encode(this.username, StandardCharsets.UTF_8) : this.username; |
| 171 | + String password = _urlEncode ? URLEncoder.encode(this.password, StandardCharsets.UTF_8) : this.password; |
168 | 172 | if (StringUtils.hasText(this.domain)) {
|
169 |
| - domainUserPass = String.format("%s;%s", this.domain, this.username); |
| 173 | + String domain = _urlEncode ? URLEncoder.encode(this.domain, StandardCharsets.UTF_8) : this.domain; |
| 174 | + domainUserPass = String.format("%s;%s", domain, username); |
170 | 175 | }
|
171 | 176 | else {
|
172 |
| - domainUserPass = this.username; |
| 177 | + domainUserPass = username; |
173 | 178 | }
|
174 |
| - if (StringUtils.hasText(this.password)) { |
175 |
| - domainUserPass += ":" + (_includePassword ? this.password : "********"); |
| 179 | + if (StringUtils.hasText(password)) { |
| 180 | + domainUserPass += ":" + (_includePassword ? password : "********"); |
176 | 181 | }
|
177 | 182 | return domainUserPass;
|
178 | 183 | }
|
@@ -211,20 +216,24 @@ public final String rawUrl() {
|
211 | 216 | }
|
212 | 217 |
|
213 | 218 | /**
|
214 |
| - * Return the url string for the share connection without encoding. |
| 219 | + * Return the url string for the share connection without encoding |
| 220 | + * the host and path. The {@code domainUserPass} is encoded, as |
| 221 | + * {@link java.net.URL} identifies the host part by looking |
| 222 | + * for the first {@code @} character, which fails if the |
| 223 | + * domain, username or password contains that character. |
215 | 224 | * Used in the {@link SmbShare} constructor delegation.
|
216 | 225 | * @param _includePassword whether password has to be masked in credentials of URL.
|
217 | 226 | * @return the url string for the share connection without encoding.
|
218 | 227 | * @since 6.3.8
|
219 | 228 | */
|
220 | 229 | public final String rawUrl(boolean _includePassword) {
|
221 |
| - String domainUserPass = getDomainUserPass(_includePassword); |
| 230 | + String domainUserPass = getDomainUserPass(_includePassword, true); |
222 | 231 | String path = cleanPath();
|
223 | 232 | return "smb://%s@%s%s".formatted(domainUserPass, getHostPort(), path);
|
224 | 233 | }
|
225 | 234 |
|
226 | 235 | private URI createUri(boolean _includePassword) {
|
227 |
| - String domainUserPass = getDomainUserPass(_includePassword); |
| 236 | + String domainUserPass = getDomainUserPass(_includePassword, false); |
228 | 237 | String path = cleanPath();
|
229 | 238 | try {
|
230 | 239 | return new URI("smb", domainUserPass, this.host, this.port, path, null, null);
|
|
0 commit comments