From 474a362034b1feee88f3dd1aa194bdecd1cf68bd Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Mon, 22 Jun 2020 19:07:00 -0500 Subject: [PATCH] Use authToken for Curl --- Core/Net/Curl.cs | 43 +++++++++++++++++++++++++++---------------- Core/Net/Net.cs | 4 ++-- Tests/Core/Net/Net.cs | 2 +- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/Core/Net/Curl.cs b/Core/Net/Curl.cs index e4338e016b..0daf3039c3 100644 --- a/Core/Net/Curl.cs +++ b/Core/Net/Curl.cs @@ -41,7 +41,15 @@ public static void CleanUp() _initComplete = false; } - + private static CurlSlist GetHeaders(string token) + { + var l = new CurlSlist(); + if (!string.IsNullOrEmpty(token)) + { + l.Append($"Authorization: token {token}"); + } + return l; + } /// /// Creates a CurlEasy object that calls the given writeback function @@ -51,7 +59,7 @@ public static void CleanUp() /// The CurlEasy object /// /// Adapted from MultiDemo.cs in the curlsharp repo - public static CurlEasy CreateEasy(string url, CurlWriteCallback wf, CurlHeaderCallback hwf = null) + public static CurlEasy CreateEasy(string url, string authToken, CurlWriteCallback wf, CurlHeaderCallback hwf = null) { if (!_initComplete) { @@ -59,25 +67,29 @@ public static CurlEasy CreateEasy(string url, CurlWriteCallback wf, CurlHeaderCa Init(); } - var easy = new CurlEasy(); - easy.Url = url; - easy.WriteData = null; - easy.WriteFunction = wf; + var easy = new CurlEasy() + { + Url = url, + WriteData = null, + WriteFunction = wf, + Encoding = "deflate, gzip", + // Follow redirects + FollowLocation = true, + UserAgent = Net.UserAgentString, + SslVerifyPeer = true, + HeaderData = null, + HttpHeader = GetHeaders(authToken), + }; if (hwf != null) { easy.HeaderFunction = hwf; } - easy.Encoding = "deflate, gzip"; - easy.FollowLocation = true; // Follow redirects - easy.UserAgent = Net.UserAgentString; - easy.SslVerifyPeer = true; var caBundle = ResolveCurlCaBundle(); if (caBundle != null) { easy.CaInfo = caBundle; } - return easy; } @@ -85,22 +97,22 @@ public static CurlEasy CreateEasy(string url, CurlWriteCallback wf, CurlHeaderCa /// Creates a CurlEasy object that writes to the given stream. /// Can call a writeback function for the header. /// - public static CurlEasy CreateEasy(string url, FileStream stream, CurlHeaderCallback hwf = null) + public static CurlEasy CreateEasy(string url, string authToken, FileStream stream, CurlHeaderCallback hwf = null) { // Let's make a happy closure around this stream! - return CreateEasy(url, delegate(byte[] buf, int size, int nmemb, object extraData) + return CreateEasy(url, authToken, delegate(byte[] buf, int size, int nmemb, object extraData) { stream.Write(buf, 0, size * nmemb); return size * nmemb; }, hwf); } - public static CurlEasy CreateEasy(Uri url, FileStream stream) + public static CurlEasy CreateEasy(Uri url, string authToken, FileStream stream) { // Curl interacts poorly with KS for some (but not all) modules unless // the original string is used, hence .OriginalString rather than .ToString // here. - return CreateEasy(url.OriginalString, stream); + return CreateEasy(url.OriginalString, authToken, stream); } /// @@ -138,4 +150,3 @@ private static string ResolveCurlCaBundle() } } } - diff --git a/Core/Net/Net.cs b/Core/Net/Net.cs index 9e0751dd87..c218c95bce 100644 --- a/Core/Net/Net.cs +++ b/Core/Net/Net.cs @@ -124,7 +124,7 @@ public static string Download(string url, out string etag, string filename = nul { string header = string.Empty; - var client = Curl.CreateEasy(url, stream, delegate(byte[] buf, int size, int nmemb, object extraData) + var client = Curl.CreateEasy(url, null, stream, delegate(byte[] buf, int size, int nmemb, object extraData) { header += Encoding.UTF8.GetString(buf); return size * nmemb; @@ -288,7 +288,7 @@ public static string DownloadText(Uri url, string authToken = "", string mimeTyp string content = string.Empty; string header = string.Empty; - var client = Curl.CreateEasy(url.OriginalString, + var client = Curl.CreateEasy(url.OriginalString, authToken, delegate (byte[] buf, int size, int nmemb, object extraData) { content += Encoding.UTF8.GetString(buf); diff --git a/Tests/Core/Net/Net.cs b/Tests/Core/Net/Net.cs index b667dece91..3c27cea077 100644 --- a/Tests/Core/Net/Net.cs +++ b/Tests/Core/Net/Net.cs @@ -67,7 +67,7 @@ public void SSLenforced() // this test on that platform. if (Platform.IsWindows) return; - var curl = Curl.CreateEasy("https://example.com", (FileStream) null); + var curl = Curl.CreateEasy("https://example.com", null, (FileStream) null); Assert.IsTrue(curl.SslVerifyPeer, "We should enforce SSL"); }