You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2020/09/27 10:42:20 UTC

[httpcomponents-client] 02/08: HTTPCLIENT-2106: Added charset parameter for DigestScheme

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit 1c55aa1548f04af9d84bfd4df023e6ecd63f3378
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Mon Aug 10 15:28:44 2020 +0200

    HTTPCLIENT-2106: Added charset parameter for DigestScheme
---
 .../hc/client5/http/impl/auth/DigestScheme.java    | 28 ++++++++++++++++++----
 .../http/impl/auth/DigestSchemeFactory.java        | 17 ++++++++++++-
 2 files changed, 39 insertions(+), 6 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java
index 7353e6f..9aec188 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestScheme.java
@@ -27,6 +27,8 @@
 package org.apache.hc.client5.http.impl.auth;
 
 import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.Serializable;
 import java.nio.charset.Charset;
 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.AuthScheme;
-import org.apache.hc.client5.http.auth.StandardAuthScheme;
 import org.apache.hc.client5.http.auth.AuthScope;
 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.CredentialsProvider;
 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.http.ClassicHttpRequest;
 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 = 2;
 
+    private transient Charset defaultCharset;
     private final Map<String, String> paramMap;
     private boolean complete;
     private transient ByteArrayBuilder buffer;
@@ -117,6 +120,11 @@ public class DigestScheme implements AuthScheme, Serializable {
     private char[] password;
 
     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.complete = false;
     }
@@ -263,11 +271,11 @@ public class DigestScheme implements AuthScheme, Serializable {
         }
 
         final String charsetName = this.paramMap.get("charset");
-        Charset charset;
+        final Charset charset;
         try {
-            charset = charsetName != null ? Charset.forName(charsetName) : StandardCharsets.ISO_8859_1;
+            charset = charsetName != null ? Charset.forName(charsetName) : defaultCharset;
         } catch (final UnsupportedCharsetException ex) {
-            charset = StandardCharsets.ISO_8859_1;
+            throw new AuthenticationException("Unsupported charset: " + charsetName);
         }
 
         String digAlg = algorithm;
@@ -470,6 +478,16 @@ public class DigestScheme implements AuthScheme, Serializable {
         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
     public String toString() {
         return getName() + this.paramMap;
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestSchemeFactory.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestSchemeFactory.java
index 26e7898..2e3c5ad 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestSchemeFactory.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/auth/DigestSchemeFactory.java
@@ -27,6 +27,8 @@
 
 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.AuthSchemeFactory;
 import org.apache.hc.core5.annotation.Contract;
@@ -47,9 +49,22 @@ public class DigestSchemeFactory implements AuthSchemeFactory {
      */
     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
     public AuthScheme create(final HttpContext context) {
-        return new DigestScheme();
+        return new DigestScheme(charset);
     }
 
 }