/***************************************************************************
*
* CurlS#arp
*
* 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
* should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of this Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the LICENSE file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
* ANY KIND, either express or implied.
*
**************************************************************************/
using System;
using System.Runtime.InteropServices;
using CurlSharp.Enums;
namespace CurlSharp
{
///
/// Top-level class for initialization and cleanup.
///
///
/// It also implements static methods for capabilities that don't
/// logically belong in a class.
///
public static class Curl
{
// for state management
private static CurlCode _initCode;
///
/// Class constructor - initialize global status.
///
static Curl()
{
_initCode = CurlCode.FailedInit;
}
// hidden instance stuff
///
/// Get the underlying cURL version as a string, example "7.12.2".
///
///
/// Thrown if cURL isn't properly initialized.
///
public static string Version
{
get
{
EnsureCurl();
return Marshal.PtrToStringAnsi(NativeMethods.curl_version());
}
}
///
/// Process-wide initialization -- call only once per process.
///
///
/// An or'd combination of
/// members.
///
///
/// A , hopefully
/// CurlCode.Ok.
///
public static CurlCode GlobalInit(CurlInitFlag flags)
{
_initCode = NativeMethods.curl_global_init((int) flags);
#if USE_LIBCURLSHIM
if (_initCode == CurlCode.Ok)
NativeMethods.curl_shim_initialize();
#endif
return _initCode;
}
///
/// Process-wide cleanup -- call just before exiting process.
///
///
/// While it's not necessary that your program call this method
/// before exiting, doing so will prevent leaks of native cURL resources.
///
public static void GlobalCleanup()
{
if (_initCode == CurlCode.Ok)
{
#if USE_LIBCURLSHIM
NativeMethods.curl_shim_cleanup();
#endif
NativeMethods.curl_global_cleanup();
_initCode = CurlCode.FailedInit;
}
}
///
/// Get a object.
///
///
/// Specify a , such as
/// CurlVersion.Now.
///
/// A object.
///
/// Thrown if cURL isn't properly initialized.
///
public static CurlVersionInfoData GetVersionInfo(CurlVersion ver)
{
EnsureCurl();
return new CurlVersionInfoData(ver);
}
///
/// Called by other classes to ensure valid cURL state.
///
internal static void EnsureCurl()
{
if (_initCode != CurlCode.Ok)
throw new InvalidOperationException("cURL not initialized");
}
}
}