Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use authToken for Curl #3086

Merged
merged 1 commit into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions Core/Net/Curl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// Creates a CurlEasy object that calls the given writeback function
Expand All @@ -51,56 +59,60 @@ public static void CleanUp()
/// <returns>The CurlEasy object</returns>
///
/// 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)
{
Log.Warn("Curl environment not pre-initialised, performing non-threadsafe init.");
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;
}

/// <summary>
/// Creates a CurlEasy object that writes to the given stream.
/// Can call a writeback function for the header.
/// </summary>
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);
}

/// <summary>
Expand Down Expand Up @@ -138,4 +150,3 @@ private static string ResolveCurlCaBundle()
}
}
}

4 changes: 2 additions & 2 deletions Core/Net/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Core/Net/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down