public Curl(int bufferSize) { if (_initialized != CurlCode.Ok) { if (_initialized == CurlCode.NotInitialized) { throw new CurlNotInitializedException(); } throw new CurlException(_initialized, this); } _buffer = new byte[bufferSize]; _errorBuffer = Marshal.AllocHGlobal(ErrorBufferSize); _curl = CurlNative.EasyInit(); if (_curl == IntPtr.Zero) { throw new CurlEasyInitializeException("Curl Easy failed to initialize!"); } CurlCode result = CurlNative.EasySetOpt(_curl, CurlOption.Errorbuffer, _errorBuffer); if (result != CurlCode.Ok) { throw new CurlException(result, this); } }
public static void Deinitialize() { if (_initialized != CurlCode.NotInitialized) { CurlNative.GlobalCleanup(); _initialized = CurlCode.NotInitialized; } }
public static bool Initialize() { if (_initialized != CurlCode.Ok) { _initialized = CurlNative.GlobalInit(CurlGlobal.Default); } return(_initialized == CurlCode.Ok); }
internal string GetError(CurlCode code) { if (code == CurlCode.NotInitialized) { return("Curl Global not initialized"); } string error = MarshalString.Utf8ToString(_errorBuffer); return(string.IsNullOrEmpty(error) ? MarshalString.Utf8ToString(CurlNative.EasyStrError(code)) : error); }
public ArraySegment <byte> GetBytes(string url) { IntPtr urlpointer = IntPtr.Zero; IntPtr useragentpointer = IntPtr.Zero; try { urlpointer = MarshalString.StringToUtf8(url); CurlCode result = CurlNative.EasySetOpt(_curl, CurlOption.Url, urlpointer); if (result != CurlCode.Ok) { throw new CurlException(result, this); } useragentpointer = MarshalString.StringToUtf8(UserAgent); CurlCode result1 = CurlNative.EasySetOpt(_curl, CurlOption.Useragent, useragentpointer); if (result1 != CurlCode.Ok) { throw new CurlException(result1, this); } CurlCode result2 = CurlNative.EasySetOpt(_curl, CurlOption.WriteData, this); if (result2 != CurlCode.Ok) { throw new CurlException(result2, this); } CurlCode result3 = CurlNative.EasySetOpt(_curl, CurlOption.WriteFunction, WriteCallback); if (result3 != CurlCode.Ok) { throw new CurlException(result3, this); } CurlCode result4 = CurlNative.EasyPerform(_curl); if (result4 != CurlCode.Ok) { throw new CurlException(result4, this); } return(new ArraySegment <byte>(_buffer, 0, _offset)); } finally { if (urlpointer != IntPtr.Zero) { Marshal.FreeHGlobal(urlpointer); } if (useragentpointer != IntPtr.Zero) { Marshal.FreeHGlobal(useragentpointer); } } }
public void Reset() { _offset = 0; CurlNative.EasyReset(_curl); CurlCode result = CurlNative.EasySetOpt(_curl, CurlOption.Errorbuffer, _errorBuffer); if (result != CurlCode.Ok) { throw new CurlException(result, this); } }
private void Cleanup() { if (_disposed) { return; } if (_errorBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(_errorBuffer); } if (_curl != IntPtr.Zero) { CurlNative.EasyCleanup(_curl); } _disposed = true; }
public static void Initialize(SharedData data) { Flags = data; if (data == SharedData.None) { Handle = CurlNative.ShareHandle.Null; return; } Handle = CurlNative.ShareInit(); if (Handle == CurlNative.ShareHandle.Null) { return; } ThrowIfNotOk(Handle.ShareSetOpt(CurlShareOption.LockFunc, LockFunction)); ThrowIfNotOk(Handle.ShareSetOpt(CurlShareOption.UnlockFunc, UnlockFunction)); if (data.HasFlagFast(SharedData.Cookie)) { ThrowIfNotOk(Handle.ShareSetOpt(CurlShareOption.Share, CurlLockData.Cookie)); } if (data.HasFlagFast(SharedData.Dns)) { ThrowIfNotOk(Handle.ShareSetOpt(CurlShareOption.Share, CurlLockData.Dns)); } if (data.HasFlagFast(SharedData.SslSession)) { ThrowIfNotOk(Handle.ShareSetOpt(CurlShareOption.Share, CurlLockData.SslSession)); } if (data.HasFlagFast(SharedData.Connection)) { ThrowIfNotOk(Handle.ShareSetOpt(CurlShareOption.Share, CurlLockData.Connect)); } if (data.HasFlagFast(SharedData.Psl)) { ThrowIfNotOk(Handle.ShareSetOpt(CurlShareOption.Share, CurlLockData.Psl)); } }
public static string GetVersion() { return(MarshalString.Utf8ToString(CurlNative.GetVersion())); }
public string Post(string url, string data, Encoding encoding) { IntPtr urlpointer = IntPtr.Zero; IntPtr useragentpointer = IntPtr.Zero; IntPtr datapointer = IntPtr.Zero; try { urlpointer = MarshalString.StringToUtf8(url); CurlCode result = CurlNative.EasySetOpt(_curl, CurlOption.Url, urlpointer); if (result != CurlCode.Ok) { throw new CurlException(result, this); } useragentpointer = MarshalString.StringToUtf8(UserAgent); CurlCode result1 = CurlNative.EasySetOpt(_curl, CurlOption.Useragent, useragentpointer); if (result1 != CurlCode.Ok) { throw new CurlException(result1, this); } datapointer = MarshalString.StringToUtf8(data); CurlCode result2 = CurlNative.EasySetOpt(_curl, CurlOption.Postfields, datapointer); if (result2 != CurlCode.Ok) { throw new CurlException(result2, this); } CurlCode result3 = CurlNative.EasySetOpt(_curl, CurlOption.WriteData, this); if (result3 != CurlCode.Ok) { throw new CurlException(result3, this); } CurlCode result4 = CurlNative.EasySetOpt(_curl, CurlOption.WriteFunction, WriteCallback); if (result4 != CurlCode.Ok) { throw new CurlException(result4, this); } CurlCode result5 = CurlNative.EasyPerform(_curl); if (result5 != CurlCode.Ok) { throw new CurlException(result5, this); } return(encoding.GetString(_buffer, 0, _offset)); } finally { if (urlpointer != IntPtr.Zero) { Marshal.FreeHGlobal(urlpointer); } if (useragentpointer != IntPtr.Zero) { Marshal.FreeHGlobal(useragentpointer); } if (datapointer != IntPtr.Zero) { Marshal.FreeHGlobal(datapointer); } } }