HTTPCLIENT-2106: Added charset parameter for DigestScheme

This commit is contained in:
Oleg Kalnichevski 2020-08-10 15:28:44 +02:00
parent 1a70490aac
commit 1c55aa1548
2 changed files with 39 additions and 6 deletions

View File

@ -27,6 +27,8 @@
package org.apache.hc.client5.http.impl.auth; package org.apache.hc.client5.http.impl.auth;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -46,13 +48,13 @@ import java.util.StringTokenizer;
import org.apache.hc.client5.http.auth.AuthChallenge; import org.apache.hc.client5.http.auth.AuthChallenge;
import org.apache.hc.client5.http.auth.AuthScheme; import org.apache.hc.client5.http.auth.AuthScheme;
import org.apache.hc.client5.http.auth.StandardAuthScheme;
import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.AuthenticationException; import org.apache.hc.client5.http.auth.AuthenticationException;
import org.apache.hc.client5.http.utils.ByteArrayBuilder;
import org.apache.hc.client5.http.auth.Credentials; import org.apache.hc.client5.http.auth.Credentials;
import org.apache.hc.client5.http.auth.CredentialsProvider; import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.auth.MalformedChallengeException; import org.apache.hc.client5.http.auth.MalformedChallengeException;
import org.apache.hc.client5.http.auth.StandardAuthScheme;
import org.apache.hc.client5.http.utils.ByteArrayBuilder;
import org.apache.hc.core5.annotation.Internal; import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.http.ClassicHttpRequest; import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpEntity;
@ -103,6 +105,7 @@ public class DigestScheme implements AuthScheme, Serializable {
private static final int QOP_AUTH_INT = 1; private static final int QOP_AUTH_INT = 1;
private static final int QOP_AUTH = 2; private static final int QOP_AUTH = 2;
private transient Charset defaultCharset;
private final Map<String, String> paramMap; private final Map<String, String> paramMap;
private boolean complete; private boolean complete;
private transient ByteArrayBuilder buffer; private transient ByteArrayBuilder buffer;
@ -117,6 +120,11 @@ public class DigestScheme implements AuthScheme, Serializable {
private char[] password; private char[] password;
public DigestScheme() { public DigestScheme() {
this(StandardCharsets.ISO_8859_1);
}
public DigestScheme(final Charset charset) {
this.defaultCharset = charset != null ? charset : StandardCharsets.ISO_8859_1;
this.paramMap = new HashMap<>(); this.paramMap = new HashMap<>();
this.complete = false; this.complete = false;
} }
@ -263,11 +271,11 @@ public class DigestScheme implements AuthScheme, Serializable {
} }
final String charsetName = this.paramMap.get("charset"); final String charsetName = this.paramMap.get("charset");
Charset charset; final Charset charset;
try { try {
charset = charsetName != null ? Charset.forName(charsetName) : StandardCharsets.ISO_8859_1; charset = charsetName != null ? Charset.forName(charsetName) : defaultCharset;
} catch (final UnsupportedCharsetException ex) { } catch (final UnsupportedCharsetException ex) {
charset = StandardCharsets.ISO_8859_1; throw new AuthenticationException("Unsupported charset: " + charsetName);
} }
String digAlg = algorithm; String digAlg = algorithm;
@ -470,6 +478,16 @@ public class DigestScheme implements AuthScheme, Serializable {
return tmp; return tmp;
} }
private void writeObject(final ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeUTF(defaultCharset.name());
}
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.defaultCharset = Charset.forName(in.readUTF());
}
@Override @Override
public String toString() { public String toString() {
return getName() + this.paramMap; return getName() + this.paramMap;

View File

@ -27,6 +27,8 @@
package org.apache.hc.client5.http.impl.auth; package org.apache.hc.client5.http.impl.auth;
import java.nio.charset.Charset;
import org.apache.hc.client5.http.auth.AuthScheme; import org.apache.hc.client5.http.auth.AuthScheme;
import org.apache.hc.client5.http.auth.AuthSchemeFactory; import org.apache.hc.client5.http.auth.AuthSchemeFactory;
import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.Contract;
@ -47,9 +49,22 @@ public class DigestSchemeFactory implements AuthSchemeFactory {
*/ */
public static final DigestSchemeFactory INSTANCE = new DigestSchemeFactory(); public static final DigestSchemeFactory INSTANCE = new DigestSchemeFactory();
private final Charset charset;
/**
* @since 5.1
*/
public DigestSchemeFactory(final Charset charset) {
this.charset = charset;
}
public DigestSchemeFactory() {
this(null);
}
@Override @Override
public AuthScheme create(final HttpContext context) { public AuthScheme create(final HttpContext context) {
return new DigestScheme(); return new DigestScheme(charset);
} }
} }