746a53
From 78b62ef1206621e8f4f1628ad4eb0a7be877c96f Mon Sep 17 00:00:00 2001
746a53
From: Johannes Schindelin <johannes.schindelin@gmx.de>
746a53
Date: Fri, 7 Dec 2018 17:04:39 +0100
746a53
Subject: [PATCH] Upon HTTP_1_1_REQUIRED, retry the request with HTTP/1.1
746a53
746a53
This is a companion patch to cbea2fd2c (NTLM: force the connection to
746a53
HTTP/1.1, 2018-12-06): with NTLM, we can switch to HTTP/1.1
746a53
preemptively. However, with other (Negotiate) authentication it is not
746a53
clear to this developer whether there is a way to make it work with
746a53
HTTP/2, so let's try HTTP/2 first and fall back in case we encounter the
746a53
error HTTP_1_1_REQUIRED.
746a53
746a53
Note: we will still keep the NTLM workaround, as it avoids an extra
746a53
round trip.
746a53
746a53
Daniel Stenberg helped a lot with this patch, in particular by
746a53
suggesting to introduce the Curl_h2_http_1_1_error() function.
746a53
746a53
Closes #3349
746a53
746a53
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
746a53
746a53
Upstream-commit: d997aa0e963c5be5de100dccdc5208d39bd3d62b
746a53
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
746a53
---
746a53
 lib/http2.c |  8 ++++++++
746a53
 lib/http2.h |  4 ++++
746a53
 lib/multi.c | 20 ++++++++++++++++++++
746a53
 3 files changed, 32 insertions(+)
746a53
746a53
diff --git a/lib/http2.c b/lib/http2.c
746a53
index d769193..3071097 100644
746a53
--- a/lib/http2.c
746a53
+++ b/lib/http2.c
746a53
@@ -2300,6 +2300,14 @@ void Curl_http2_cleanup_dependencies(struct Curl_easy *data)
746a53
     Curl_http2_remove_child(data->set.stream_depends_on, data);
746a53
 }
746a53
 
746a53
+/* Only call this function for a transfer that already got a HTTP/2
746a53
+   CURLE_HTTP2_STREAM error! */
746a53
+bool Curl_h2_http_1_1_error(struct connectdata *conn)
746a53
+{
746a53
+  struct http_conn *httpc = &conn->proto.httpc;
746a53
+  return (httpc->error_code == NGHTTP2_HTTP_1_1_REQUIRED);
746a53
+}
746a53
+
746a53
 #else /* !USE_NGHTTP2 */
746a53
 
746a53
 /* Satisfy external references even if http2 is not compiled in. */
746a53
diff --git a/lib/http2.h b/lib/http2.h
746a53
index 21cd9b8..91e504c 100644
746a53
--- a/lib/http2.h
746a53
+++ b/lib/http2.h
746a53
@@ -59,6 +59,9 @@ CURLcode Curl_http2_add_child(struct Curl_easy *parent,
746a53
 void Curl_http2_remove_child(struct Curl_easy *parent,
746a53
                              struct Curl_easy *child);
746a53
 void Curl_http2_cleanup_dependencies(struct Curl_easy *data);
746a53
+
746a53
+/* returns true if the HTTP/2 stream error was HTTP_1_1_REQUIRED */
746a53
+bool Curl_h2_http_1_1_error(struct connectdata *conn);
746a53
 #else /* USE_NGHTTP2 */
746a53
 #define Curl_http2_init(x) CURLE_UNSUPPORTED_PROTOCOL
746a53
 #define Curl_http2_send_request(x) CURLE_UNSUPPORTED_PROTOCOL
746a53
@@ -74,6 +77,7 @@ void Curl_http2_cleanup_dependencies(struct Curl_easy *data);
746a53
 #define Curl_http2_add_child(x, y, z)
746a53
 #define Curl_http2_remove_child(x, y)
746a53
 #define Curl_http2_cleanup_dependencies(x)
746a53
+#define Curl_h2_http_1_1_error(x) 0
746a53
 #endif
746a53
 
746a53
 #endif /* HEADER_CURL_HTTP2_H */
746a53
diff --git a/lib/multi.c b/lib/multi.c
746a53
index 0f57fd5..d64ba94 100644
746a53
--- a/lib/multi.c
746a53
+++ b/lib/multi.c
746a53
@@ -46,6 +46,7 @@
746a53
 #include "vtls/vtls.h"
746a53
 #include "connect.h"
746a53
 #include "http_proxy.h"
746a53
+#include "http2.h"
746a53
 /* The last 3 #include files should be in this order */
746a53
 #include "curl_printf.h"
746a53
 #include "curl_memory.h"
746a53
@@ -1943,6 +1944,25 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
746a53
           done = TRUE;
746a53
         }
746a53
       }
746a53
+      else if((CURLE_HTTP2_STREAM == result) &&
746a53
+                Curl_h2_http_1_1_error(data->easy_conn)) {
746a53
+        CURLcode ret = Curl_retry_request(data->easy_conn, &newurl);
746a53
+
746a53
+        infof(data, "Forcing HTTP/1.1 for NTLM");
746a53
+        data->set.httpversion = CURL_HTTP_VERSION_1_1;
746a53
+
746a53
+        if(!ret)
746a53
+          retry = (newurl)?TRUE:FALSE;
746a53
+        else
746a53
+          result = ret;
746a53
+
746a53
+        if(retry) {
746a53
+          /* if we are to retry, set the result to OK and consider the
746a53
+             request as done */
746a53
+          result = CURLE_OK;
746a53
+          done = TRUE;
746a53
+        }
746a53
+      }
746a53
 
746a53
       if(result) {
746a53
         /*
746a53
-- 
746a53
2.37.3
746a53