From b938aa51e50852d1b185db3188357073ad374416 Mon Sep 17 00:00:00 2001
From: myfreeer <myfreeer@users.noreply.github.com>
Date: Sun, 13 Dec 2020 15:40:56 +0800
Subject: [PATCH] option: add option to disable Want-Digest header

Sending this HTTP header should be optional.
Should close https://github.com/myfreeer/aria2-build-msys2/issues/10

 --http-want-digest[=true|false] Send Want-Digest HTTP requser header
                              with only limited hash algorithms:
                              SHA-512, SHA-256, and SHA-1.
                              The Want-Digest HTTP header is primarily used
                              in a HTTP request, to ask the responder to
                              provide a digest of the requested resource
                              using the Digest response header

                              Possible Values: true, false
                              Default: false
                              Tags: #advanced, #http
---
 src/AbstractProxyRequestCommand.cc |  1 +
 src/HttpRequestCommand.cc          |  1 +
 src/OptionHandlerFactory.cc        | 12 ++++++++++++
 src/prefs.cc                       |  2 ++
 src/prefs.h                        |  2 ++
 src/usage_text.h                   |  8 ++++++++
 6 files changed, 26 insertions(+)

diff --git a/src/AbstractProxyRequestCommand.cc b/src/AbstractProxyRequestCommand.cc
index bd2bcb3..1feed07 100644
--- a/src/AbstractProxyRequestCommand.cc
+++ b/src/AbstractProxyRequestCommand.cc
@@ -72,6 +72,7 @@ bool AbstractProxyRequestCommand::executeInternal()
   if (httpConnection_->sendBufferIsEmpty()) {
     auto httpRequest = make_unique<HttpRequest>();
     httpRequest->setUserAgent(getOption()->get(PREF_USER_AGENT));
+    httpRequest->setNoWantDigest(!getOption()->getAsBool(PREF_HTTP_WANT_DIGEST));
     httpRequest->setRequest(getRequest());
     httpRequest->setProxyRequest(proxyRequest_);
 
diff --git a/src/HttpRequestCommand.cc b/src/HttpRequestCommand.cc
index a2b8e7e..8c50153 100644
--- a/src/HttpRequestCommand.cc
+++ b/src/HttpRequestCommand.cc
@@ -90,6 +90,7 @@ createHttpRequest(const std::shared_ptr<Request>& req,
 {
   auto httpRequest = make_unique<HttpRequest>();
   httpRequest->setUserAgent(option->get(PREF_USER_AGENT));
+  httpRequest->setNoWantDigest(!option->getAsBool(PREF_HTTP_WANT_DIGEST));
   httpRequest->setRequest(req);
   httpRequest->setFileEntry(fileEntry);
   httpRequest->setSegment(segment);
diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc
index a058eb9..9ff615a 100644
--- a/src/OptionHandlerFactory.cc
+++ b/src/OptionHandlerFactory.cc
@@ -1106,6 +1106,18 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
     op->setChangeOptionForReserved(true);
     handlers.push_back(op);
   }
+  {
+    OptionHandler* op(
+        new BooleanOptionHandler(PREF_HTTP_WANT_DIGEST,
+                                 TEXT_HTTP_WANT_DIGEST,
+                                 A2_V_FALSE, OptionHandler::OPT_ARG));
+    op->addTag(TAG_ADVANCED);
+    op->addTag(TAG_HTTP);
+    op->setInitialOption(true);
+    op->setChangeGlobalOption(true);
+    op->setChangeOptionForReserved(true);
+    handlers.push_back(op);
+  }
   {
     OptionHandler* op(new BooleanOptionHandler(
         PREF_ENABLE_HTTP_KEEP_ALIVE, TEXT_ENABLE_HTTP_KEEP_ALIVE, A2_V_TRUE,
diff --git a/src/prefs.cc b/src/prefs.cc
index 9793706..bdb33e2 100644
--- a/src/prefs.cc
+++ b/src/prefs.cc
@@ -437,6 +437,8 @@ PrefPtr PREF_HTTP_ACCEPT_GZIP = makePref("http-accept-gzip");
 // value: true | false
 PrefPtr PREF_CONTENT_DISPOSITION_DEFAULT_UTF8 =
     makePref("content-disposition-default-utf8");
+// values: true | false
+PrefPtr PREF_HTTP_WANT_DIGEST = makePref("http-want-digest");
 
 /**
  * Proxy related preferences
diff --git a/src/prefs.h b/src/prefs.h
index f014d9c..c88f2d0 100644
--- a/src/prefs.h
+++ b/src/prefs.h
@@ -389,6 +389,8 @@ extern PrefPtr PREF_HTTP_NO_CACHE;
 extern PrefPtr PREF_HTTP_ACCEPT_GZIP;
 // value: true | false
 extern PrefPtr PREF_CONTENT_DISPOSITION_DEFAULT_UTF8;
+// value: true | false
+extern PrefPtr PREF_HTTP_WANT_DIGEST;
 
 /**;
  * Proxy related preferences
diff --git a/src/usage_text.h b/src/usage_text.h
index 7a0a981..aee3ac0 100644
--- a/src/usage_text.h
+++ b/src/usage_text.h
@@ -560,6 +560,14 @@
     "                              Content-Disposition header as UTF-8 instead of\n" \
     "                              ISO-8859-1, for example, the filename parameter,\n" \
     "                              but not the extended version filename*.")
+#define TEXT_HTTP_WANT_DIGEST                          \
+  _(" --http-want-digest[=true|false] Send Want-Digest HTTP requser header\n" \
+    "                              with only limited hash algorithms:\n" \
+    "                              SHA-512, SHA-256, and SHA-1.\n" \
+    "                              The Want-Digest HTTP header is primarily used\n" \
+    "                              in a HTTP request, to ask the responder to\n" \
+    "                              provide a digest of the requested resource\n" \
+    "                              using the Digest response header")
 #define TEXT_EVENT_POLL                                                 \
   _(" --event-poll=POLL            Specify the method for polling events.")
 #define TEXT_BT_EXTERNAL_IP                                             \
-- 
2.29.2

