Skip to content

Commit

Permalink
Use authToken for Curl
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jun 23, 2020
1 parent 85be3ba commit 474a362
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
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

0 comments on commit 474a362

Please sign in to comment.