Feature/netcore preparation (#2035)

* Move to use package reference for restoring nuget packages.

* Return a task result for this async method.

* Update to a supported version of the .NET Framework. This also has the side effect of allowing us to automatically generate our binding redirects on build.

* Set the solution to target VS2017

* Update test solution csproj file to support being built through MSBuild 15

* Move to use package reference for restoring nuget packages.

* Return a task result for this async method.

* Update to a supported version of the .NET Framework. This also has the side effect of allowing us to automatically generate our binding redirects on build.

* Set the solution to target VS2017

* Update test solution csproj file to support being built through MSBuild 15

* DateTimeRoutines does not have Nuget packages that support .NET Standard (and therefore .NET Core). We will have to include them for now until we can get rid of this dependency.

* Move the interfaces into their own files. This will be useful when we share them between the .NET Core and .NET Framework WebAPI

* Stage services that need to point to the new interface namespace.

* Update CurlSharp to fix memory leak issue and support better runtime compatibility with OSX and Linux

* Start spliting some interfaces into their own files - this will help by allowing us to split them out in the future into a seperate project so the actual implementations can stay within their respective architectures when required
This commit is contained in:
Nathan Holland
2017-10-29 23:19:09 +13:00
committed by flightlevel
parent 7829643104
commit 8a6b9d4de7
166 changed files with 2093 additions and 975 deletions

View File

@@ -2,7 +2,7 @@
*
* CurlS#arp
*
* Copyright (c) 2014 Dr. Masroor Ehsan (masroore@gmail.com)
* Copyright (c) 2013-2017 Dr. Masroor Ehsan (masroore@gmail.com)
* Portions copyright (c) 2004, 2005 Jeff Phillips (jeff@jeffp.net)
*
* This software is licensed as described in the file LICENSE, which you
@@ -56,7 +56,7 @@ namespace CurlSharp
private bool _cookieSession;
private CurlShare _curlShare;
private string _customRequest;
private Object _debugData;
private object _debugData;
private int _dnsCacheTimeout;
private bool _dnsUseGlobalCache;
private string _egdSocket;
@@ -79,7 +79,7 @@ namespace CurlSharp
private bool _ftpUseEprt;
private bool _ftpUseEpsv;
private GCHandle _hThis;
private Object _headerData;
private object _headerData;
private CurlHttpAuth _httpAuth;
private bool _httpGet;
private CurlHttpMultiPartForm _httpMultiPartForm;
@@ -88,10 +88,8 @@ namespace CurlSharp
private bool _ignoreContentLength;
private long _infileSize;
private string _interface;
private Object _ioctlData;
private object _ioctlData;
private string _krb4Level;
private CurlCode _lastErrorCode;
private string _lastErrorDescription;
private int _lowSpeedLimit;
private int _lowSpeedTime;
private int _maxConnects;
@@ -118,6 +116,7 @@ namespace CurlSharp
private NativeMethods._CurlDebugCallback _pcbDebug;
private NativeMethods._CurlIoctlCallback _pcbIoctl;
private NativeMethods._CurlProgressCallback _pcbProgress;
private NativeMethods._CurlSslCtxCallback _pcbSslCtx;
#endif
private CurlDebugCallback _pfCurlDebug;
private CurlHeaderCallback _pfCurlHeader;
@@ -130,27 +129,27 @@ namespace CurlSharp
private bool _post;
private int _postFieldSize;
private string _postFields;
private Object _privateData;
private Object _progressData;
private object _progressData;
private string _proxy;
private int _proxyPort;
private string _proxyUserPwd;
private bool _put;
private string _randomFile;
private string _range;
private Object _readData;
private object _readData;
private string _referer;
private int _resumeFrom;
private string _sourceUrl;
private string _sslCert;
private string _sslCertPasswd;
private string _sslCipherList;
private Object _sslContextData;
private object _sslContextData;
private string _sslEngine;
private bool _sslEngineDefault;
private string _sslKey;
private string _sslKeyPasswd;
private bool _sslVerifyPeer;
private bool _sslVerifyHost;
private bool _tcpNoDelay;
private int _timeValue;
private int _timeout;
@@ -161,7 +160,7 @@ namespace CurlSharp
private string _userAgent;
private string _userPwd;
private bool _verbose;
private Object _writeData;
private object _writeData;
private string _writeInfo;
/// <summary>
@@ -180,6 +179,7 @@ namespace CurlSharp
Curl.EnsureCurl();
_pCurl = NativeMethods.curl_easy_init();
ensureHandle();
NativeMethods.curl_easy_setopt(_pCurl, CurlOption.NoSignal, 1L);
NativeMethods.curl_easy_setopt(_pCurl, CurlOption.NoProgress, IntPtr.Zero);
#if USE_LIBCURLSHIM
_pMyStrings = NativeMethods.curl_shim_alloc_strings();
@@ -199,11 +199,7 @@ namespace CurlSharp
installDelegates();
}
public object Private
{
get { return _privateData; }
set { _privateData = value; }
}
public object Private { get; set; }
public object WriteData
{
@@ -227,6 +223,8 @@ namespace CurlSharp
/// <returns></returns>
private CurlCode setWriteData(object data)
{
freeHandle(ref _curlWriteData);
_curlWriteData = getHandle(data);
return setCurlOpt(_curlWriteData, CurlOption.WriteData);
}
@@ -292,6 +290,18 @@ namespace CurlSharp
return setCurlOpt(_curlDebugData, CurlOption.DebugData);
}
private IntPtr _curlSslCtxData = IntPtr.Zero;
/// <summary>
/// Object to pass to OnSslCtxCallback.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
private CurlCode setSslCtxData(object data)
{
_curlSslCtxData = getHandle(data);
return setCurlOpt(_curlSslCtxData, CurlOption.SslCtxData);
}
private IntPtr _curlIoctlData = IntPtr.Zero;
@@ -355,6 +365,17 @@ namespace CurlSharp
}
}
public object SslCtxData
{
get { return _sslContextData; }
set
{
_sslContextData = value;
#if !USE_LIBCURLSHIM
setSslCtxData(value);
#endif
}
}
public object IoctlData
{
@@ -426,7 +447,7 @@ namespace CurlSharp
_ftpAuth = value;
var l = Convert.ToInt32(value);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.FtpSslAuth, (IntPtr) l),
CurlOption.FtpSslAuth);
CurlOption.FtpSslAuth);
}
}
@@ -438,7 +459,7 @@ namespace CurlSharp
_httpVersion = value;
var l = Convert.ToInt32(value);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.HttpVersion, (IntPtr) l),
CurlOption.HttpVersion);
CurlOption.HttpVersion);
}
}
@@ -450,7 +471,7 @@ namespace CurlSharp
_httpAuth = value;
var l = Convert.ToInt32(value);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.HttpAuth, (IntPtr) l),
CurlOption.HttpAuth);
CurlOption.HttpAuth);
}
}
@@ -462,7 +483,7 @@ namespace CurlSharp
_ftpSsl = value;
var l = Convert.ToInt32(value);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.FtpSsl, (IntPtr) l),
CurlOption.FtpSsl);
CurlOption.FtpSsl);
}
}
@@ -474,7 +495,7 @@ namespace CurlSharp
_closePolicy = value;
var l = Convert.ToInt32(value);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.ClosePolicy, (IntPtr) l),
CurlOption.ClosePolicy);
CurlOption.ClosePolicy);
}
}
@@ -514,12 +535,14 @@ namespace CurlSharp
set { setFunctionOptions(CurlOption.IoctlFunction, value); }
}
public string LastErrorDescription
public CurlSslContextCallback SslContextFunction
{
get { return _lastErrorDescription; }
get { return _pfCurlSslContext; }
set { setFunctionOptions(CurlOption.SslCtxFunction, value); }
}
public string LastErrorDescription { get; private set; }
public bool NoProgress
{
get { return _noProgress; }
@@ -592,6 +615,12 @@ namespace CurlSharp
set { setBoolOption(CurlOption.SslVerifyPeer, ref _sslVerifyPeer, value); }
}
public bool SslVerifyhost
{
get { return _sslVerifyHost; }
set { setBoolOption(CurlOption.SslVerifyhost, ref _sslVerifyHost, value); }
}
public bool FreshConnect
{
get { return _freshConnect; }
@@ -1106,10 +1135,7 @@ namespace CurlSharp
get { return getSlistInfo(CurlInfo.SslEngines); }
}
public CurlCode LastErrorCode
{
get { return _lastErrorCode; }
}
public CurlCode LastErrorCode { get; private set; }
/// <summary>
/// Cleanup unmanaged resources.
@@ -1122,7 +1148,7 @@ namespace CurlSharp
private void resetPrivateVariables()
{
_privateData = null;
Private = null;
_pfCurlWrite = null;
_writeData = null;
@@ -1179,10 +1205,10 @@ namespace CurlSharp
/// </summary>
private void setLastError(CurlCode code, CurlOption opt)
{
if (LastErrorCode == CurlCode.Ok && code != CurlCode.Ok)
if ((LastErrorCode == CurlCode.Ok) && (code != CurlCode.Ok))
{
_lastErrorCode = code;
_lastErrorDescription = string.Format("Error: {0} setting option {1}", StrError(code), opt);
LastErrorCode = code;
LastErrorDescription = string.Format("Error: {0} setting option {1}", StrError(code), opt);
}
}
@@ -1191,10 +1217,10 @@ namespace CurlSharp
/// </summary>
private void setLastError(CurlCode code, CurlInfo info)
{
if (LastErrorCode == CurlCode.Ok && code != CurlCode.Ok)
if ((LastErrorCode == CurlCode.Ok) && (code != CurlCode.Ok))
{
_lastErrorCode = code;
_lastErrorDescription = string.Format("Error: {0} getting info {1}", StrError(code), info);
LastErrorCode = code;
LastErrorDescription = string.Format("Error: {0} getting info {1}", StrError(code), info);
}
}
@@ -1233,6 +1259,7 @@ namespace CurlSharp
freeHandle(ref _curlProgressData);
freeHandle(ref _curlHeaderData);
freeHandle(ref _curlIoctlData);
freeHandle(ref _curlSslCtxData);
#endif
NativeMethods.curl_easy_cleanup(_pCurl);
@@ -1293,6 +1320,12 @@ namespace CurlSharp
private void setStringOption(CurlOption option, out string field, string value)
{
// all string options are copied by the library, the only exception to this rule is PostFields option
if (option == CurlOption.PostFields)
option = CurlOption.CopyPostFields;
if ((option == CurlOption.CopyPostFields) && (PostFieldSize == 0))
PostFieldSize = System.Text.Encoding.UTF8.GetByteCount(value);
setStringOption(option, value);
field = value;
}
@@ -1313,7 +1346,13 @@ namespace CurlSharp
#else
// convert the string to a null-terminated one
var buffer = System.Text.Encoding.UTF8.GetBytes(value + "\0");
setLastError(NativeMethods.curl_easy_setopt(_pCurl, option, buffer), option);
unsafe
{
fixed (byte* bufPtr = &buffer[0])
{
setLastError(NativeMethods.curl_easy_setopt(_pCurl, option, buffer), option);
}
}
#endif
}
}
@@ -1337,7 +1376,7 @@ namespace CurlSharp
/// <see cref="CurlCode.BadFunctionArgument" />
/// will be returned if the type of value of <c>parameter</c> is invalid.
/// </returns>
public CurlCode SetOpt(CurlOption option, Object parameter)
public CurlCode SetOpt(CurlOption option, object parameter)
{
ensureHandle();
var retCode = CurlCode.Ok;
@@ -1346,10 +1385,8 @@ namespace CurlSharp
if ((int) option < CURLOPTTYPE_OBJECTPOINT)
{
var i = 0;
if (option == CurlOption.DnsUseGlobalCache || option == CurlOption.SourcePort)
{
if ((option == CurlOption.DnsUseGlobalCache) || (option == CurlOption.SourcePort))
return CurlCode.BadFunctionArgument;
}
if (option == CurlOption.TimeValue)
{
@@ -1365,17 +1402,17 @@ namespace CurlSharp
retCode = NativeMethods.curl_easy_setopt(_pCurl, option, (IntPtr) i);
}
// object cases: the majority
// object cases: the majority
else if ((int) option < CURLOPTTYPE_FUNCTIONPOINT)
{
return setObjectOptions(option, parameter);
}
// FUNCTIONPOINT args, for delegates
// FUNCTIONPOINT args, for delegates
else if ((int) option < CURLOPTTYPE_OFF_T)
{
return setFunctionOptions(option, parameter);
}
// otherwise, it's one of those 64-bit off_t guys!
// otherwise, it's one of those 64-bit off_t guys!
else
{
var i = Convert.ToInt64(parameter);
@@ -1390,9 +1427,9 @@ namespace CurlSharp
var retCode = CurlCode.Ok;
switch (option)
{
// various data items
// various data items
case CurlOption.Private:
_privateData = parameter;
Private = parameter;
break;
case CurlOption.WriteData:
_writeData = parameter;
@@ -1409,12 +1446,15 @@ namespace CurlSharp
case CurlOption.HeaderData:
_headerData = parameter;
break;
case CurlOption.SslCtxData:
_sslContextData = parameter;
break;
case CurlOption.IoctlData:
_ioctlData = parameter;
break;
// items that can't be set externally or
// obsolete items
// items that can't be set externally or
// obsolete items
case CurlOption.ErrorBuffer:
case CurlOption.Stderr:
case CurlOption.SourceHost:
@@ -1422,7 +1462,7 @@ namespace CurlSharp
case CurlOption.PasvHost:
return CurlCode.BadFunctionArgument;
// singular case for share
// singular case for share
case CurlOption.Share:
{
_curlShare = parameter as CurlShare;
@@ -1430,7 +1470,7 @@ namespace CurlSharp
break;
}
// multipart HTTP post
// multipart HTTP post
case CurlOption.HttpPost:
{
_httpMultiPartForm = parameter as CurlHttpMultiPartForm;
@@ -1438,7 +1478,7 @@ namespace CurlSharp
break;
}
// items curl wants as a curl_slist
// items curl wants as a curl_slist
case CurlOption.HttpHeader:
case CurlOption.Prequote:
case CurlOption.Quote:
@@ -1452,12 +1492,12 @@ namespace CurlSharp
break;
}
// string items
// string items
default:
{
var s = parameter as string;
setStringOption(option, s);
retCode = _lastErrorCode;
retCode = LastErrorCode;
break;
}
}
@@ -1560,6 +1600,14 @@ namespace CurlSharp
break;
}
case CurlOption.SslCtxFunction:
{
var sf = pfn as CurlSslContextCallback;
if (sf == null)
return CurlCode.BadFunctionArgument;
_pfCurlSslContext = sf;
break;
}
case CurlOption.IoctlFunction:
{
@@ -1590,7 +1638,13 @@ namespace CurlSharp
public CurlCode Perform()
{
ensureHandle();
return NativeMethods.curl_easy_perform(_pCurl);
var nativeRet = NativeMethods.curl_easy_perform(_pCurl);
#if !USE_LIBCURLSHIM
freeHandle(ref _curlWriteData);
#endif
return nativeRet;
}
/// <summary>
@@ -1606,12 +1660,60 @@ namespace CurlSharp
return new CurlEasy(this);
}
/// <summary>
/// URL encode a String.
/// </summary>
/// <param name="url">The string to URL encode.</param>
/// <param name="length">
/// Input string length;
/// use 0 for cURL to determine.
/// </param>
/// <returns>A new URL encoded string.</returns>
/// <exception cref="NullReferenceException">
/// This is thrown if
/// the native <c>CURL*</c> handle wasn't created successfully.
/// </exception>
public string Escape(string url)
{
ensureHandle();
var length = System.Text.Encoding.ASCII.GetBytes(url).Length;
var p = NativeMethods.curl_easy_escape(_pCurl, url, length);
var s = Marshal.PtrToStringAnsi(p);
NativeMethods.curl_free(p);
return s;
}
/// <summary>
/// URL decode a String.
/// </summary>
/// <param name="url">The string to URL decode.</param>
/// <param name="length">
/// Input string length;
/// use 0 for cURL to determine.
/// </param>
/// <returns>A new URL decoded string.</returns>
/// <exception cref="NullReferenceException">
/// This is thrown if
/// the native <c>CURL*</c> handle wasn't created successfully.
/// </exception>
public string Unescape(string url)
{
ensureHandle();
var length = System.Text.Encoding.ASCII.GetBytes(url).Length;
var p = NativeMethods.curl_easy_unescape(_pCurl, url, length, out int outLength);
var s = Marshal.PtrToStringAnsi(p, outLength);
NativeMethods.curl_free(p);
return s;
}
/// <summary>
/// Get a string description of an error code.
/// </summary>
/// <param name="code">Error code.</param>
/// <returns>String description of the error code.</returns>
public String StrError(CurlCode code)
public string StrError(CurlCode code)
{
return Marshal.PtrToStringAnsi(NativeMethods.curl_easy_strerror(code));
}
@@ -1635,7 +1737,7 @@ namespace CurlSharp
/// This is thrown if
/// the native <c>CURL*</c> handle wasn't created successfully.
/// </exception>
public CurlCode GetInfo(CurlInfo info, ref Object objInfo)
public CurlCode GetInfo(CurlInfo info, ref object objInfo)
{
ensureHandle();
var retCode = CurlCode.Ok;
@@ -1651,7 +1753,7 @@ namespace CurlSharp
// private data
if (info == CurlInfo.Private)
{
objInfo = _privateData;
objInfo = Private;
return retCode;
}
@@ -1681,7 +1783,7 @@ namespace CurlSharp
{
ensureHandle();
// ensure it's an integral type
if ((int) info < CURLINFO_LONG || (int) info >= CURLINFO_DOUBLE)
if (((int) info < CURLINFO_LONG) || ((int) info >= CURLINFO_DOUBLE))
{
setLastError(CurlCode.BadFunctionArgument, info);
return -1;
@@ -1779,7 +1881,7 @@ namespace CurlSharp
var retCode = CurlCode.Ok;
var ptr = IntPtr.Zero;
if ((int) info < CURLINFO_STRING || (int) info >= CURLINFO_LONG)
if (((int) info < CURLINFO_STRING) || ((int) info >= CURLINFO_LONG))
return CurlCode.BadFunctionArgument;
retCode = NativeMethods.curl_easy_getinfo(_pCurl, info, ref ptr);
if (retCode == CurlCode.Ok)
@@ -1839,7 +1941,7 @@ namespace CurlSharp
var ptr = IntPtr.Zero;
// ensure it's an integral type
if ((int) info < CURLINFO_LONG || (int) info >= CURLINFO_DOUBLE)
if (((int) info < CURLINFO_LONG) || ((int) info >= CURLINFO_DOUBLE))
return CurlCode.BadFunctionArgument;
retCode = NativeMethods.curl_easy_getinfo(_pCurl, info, ref ptr);
@@ -1876,10 +1978,8 @@ namespace CurlSharp
retCode = NativeMethods.curl_easy_getinfo(_pCurl, info, ref ptr);
if (retCode == CurlCode.Ok)
{
if ((int) ptr < 0)
dt = new DateTime(0);
}
return retCode;
}
@@ -1908,34 +2008,38 @@ namespace CurlSharp
_pcbProgress = _curlProgressCallback;
_pcbDebug = _curlDebugCallback;
_pcbHeader = _curlHeaderCallback;
_pcbSslCtx = _curlSslCtxCallback;
_pcbIoctl = _curlIoctlCallback;
setLastError(NativeMethods.curl_easy_setopt_cb(_pCurl, CurlOption.WriteFunction, _pcbWrite),
CurlOption.WriteFunction);
setLastError(NativeMethods.curl_easy_setopt_cb(_pCurl, CurlOption.ReadFunction, _pcbRead),
CurlOption.ReadFunction);
setLastError(NativeMethods.curl_easy_setopt_cb(_pCurl, CurlOption.ProgressFunction, _pcbProgress),
CurlOption.ProgressFunction);
setLastError(NativeMethods.curl_easy_setopt_cb(_pCurl, CurlOption.HeaderFunction, _pcbHeader),
CurlOption.HeaderFunction);
setLastError(NativeMethods.curl_easy_setopt_cb(_pCurl, CurlOption.DebugFunction, _pcbDebug),
CurlOption.DebugFunction);
setLastError(NativeMethods.curl_easy_setopt_cb(_pCurl, CurlOption.IoctlFunction, _pcbIoctl),
CurlOption.IoctlFunction);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.WriteFunction, _pcbWrite),
CurlOption.WriteFunction);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.ReadFunction, _pcbRead),
CurlOption.ReadFunction);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.ProgressFunction, _pcbProgress),
CurlOption.ProgressFunction);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.HeaderFunction, _pcbHeader),
CurlOption.HeaderFunction);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.DebugFunction, _pcbDebug),
CurlOption.DebugFunction);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.SslCtxFunction, _pcbSslCtx),
CurlOption.SslCtxFunction);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.IoctlFunction, _pcbIoctl),
CurlOption.IoctlFunction);
setLastError(NativeMethods.curl_easy_setopt(_pCurl, CurlOption.NoProgress, (IntPtr) 0),
CurlOption.NoProgress);
CurlOption.NoProgress);
setWriteData(null);
setReadData(null);
setHeaderData(null);
setProgressData(null);
setDebugData(null);
setSslCtxData(null);
setIoctlData(null);
#endif
}
#if USE_LIBCURLSHIM
// called by libcurlshim
// called by libcurlshim
private static int _shimWriteCallback(IntPtr buf, int sz, int nmemb, IntPtr parm)
{
var bytes = sz*nmemb;
@@ -1958,9 +2062,7 @@ namespace CurlSharp
var b = new byte[bytes];
var gch = (GCHandle) parm;
var curlEasy = (CurlEasy) gch.Target;
if (curlEasy == null)
return 0;
if (curlEasy._pfCurlRead == null)
if (curlEasy?._pfCurlRead == null)
return 0;
var nRead = curlEasy._pfCurlRead(b, sz, nmemb, curlEasy._readData);
if (nRead > 0)
@@ -1976,9 +2078,7 @@ namespace CurlSharp
{
var gch = (GCHandle) parm;
var curlEasy = (CurlEasy) gch.Target;
if (curlEasy == null)
return 0;
if (curlEasy._pfCurlProgress == null)
if (curlEasy?._pfCurlProgress == null)
return 0;
var nprog = curlEasy._pfCurlProgress(curlEasy._progressData, dlTotal, dlNow, ulTotal, ulNow);
return nprog;
@@ -1989,12 +2089,10 @@ namespace CurlSharp
{
var gch = (GCHandle) parm;
var curlEasy = (CurlEasy) gch.Target;
if (curlEasy == null)
return 0;
if (curlEasy._pfCurlDebug == null)
if (curlEasy?._pfCurlDebug == null)
return 0;
var message = Marshal.PtrToStringAnsi(msgBuf, msgBufSize);
curlEasy._pfCurlDebug(infoType, message, curlEasy._debugData);
curlEasy._pfCurlDebug(infoType, message, msgBufSize, curlEasy._debugData);
return 0;
}
@@ -2079,7 +2177,7 @@ namespace CurlSharp
if (_pfCurlDebug != null)
{
var userdata = getObject(pUserData);
_pfCurlDebug(infoType, message, userdata);
_pfCurlDebug(infoType, message, size, userdata);
}
return 0;
}
@@ -2107,7 +2205,7 @@ namespace CurlSharp
private CurlIoError _curlIoctlCallback(CurlIoCommand cmd, IntPtr parm)
{
if (_pfCurlIoctl == null || _ioctlData == null)
if ((_pfCurlIoctl == null) || (_ioctlData == null))
return CurlIoError.UnknownCommand;
return _pfCurlIoctl(cmd, _ioctlData);
}