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/08/10 15:23:22 UTC

[httpcomponents-client] branch 5.1.x updated: 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 5.1.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/5.1.x by this push:
     new edde79a  HTTPCLIENT-2106: Added charset parameter for DigestScheme
edde79a is described below

commit edde79a73638e88b209fcd8ed9bc9af2c5972af3
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 d9b5177..fc6f477 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;
@@ -99,6 +101,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;
@@ -113,6 +116,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;
     }
@@ -256,11 +264,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;
@@ -463,6 +471,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);
     }
 
 }