Esempio n. 1
0
        /// <summary>
        /// Extract <c>DateTime</c> information from an <c>Easy</c> object.
        /// </summary>
        /// <param name="info">One of the values in the
        /// <see cref="CURLINFO"/> enumeration. In this case, it must
        /// specifically be <see cref="CURLINFO.CURLINFO_FILETIME"/>.
        /// </param>
        /// <param name="dt">Reference to a <c>DateTime</c> value.</param>
        /// <returns>The <see cref="CURLcode"/> obtained from the internal
        /// call to <c>curl_easy_getinfo()</c>.
        /// </returns>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
        public CURLcode GetInfo(CURLINFO info, ref DateTime dt)
        {
            EnsureHandle();
            CURLcode retCode = CURLcode.CURLE_OK;
            IntPtr   ptr     = IntPtr.Zero;

            if (info != CURLINFO.CURLINFO_FILETIME)
            {
                return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
            }

            retCode = External.curl_easy_getinfo(m_pCURL, info, ref ptr);
            if (retCode == CURLcode.CURLE_OK)
            {
                if ((int)ptr < 0)
                {
                    dt = new DateTime(0);
                }
                else
                {
                    int yy = 0, mm = 0, dd = 0, hh = 0, mn = 0, ss = 0;
                    External.curl_shim_get_file_time((int)ptr, ref yy,
                                                     ref mm, ref dd, ref hh, ref mn, ref ss);
                    dt = new DateTime(yy, mm, dd, hh, mn, ss);
                }
            }
            return(retCode);
        }
Esempio n. 2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <exception cref="System.InvalidOperationException">This is thrown
 /// if <see cref="Curl"/> hasn't bee properly initialized.</exception>
 /// <exception cref="System.NullReferenceException">This is thrown if
 /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
 public Easy()
 {
     Curl.EnsureCurl();
     m_pCURL = External.curl_easy_init();
     EnsureHandle();
     External.curl_easy_setopt(m_pCURL, CURLoption.CURLOPT_NOPROGRESS,
                               IntPtr.Zero);
     m_pMyStrings     = External.curl_shim_alloc_strings();
     m_pfWrite        = null;
     m_privateData    = null;
     m_writeData      = null;
     m_pfRead         = null;
     m_readData       = null;
     m_pfProgress     = null;
     m_progressData   = null;
     m_pfDebug        = null;
     m_debugData      = null;
     m_pfHeader       = null;
     m_headerData     = null;
     m_pfSSLContext   = null;
     m_sslContextData = null;
     m_pfIoctl        = null;
     m_ioctlData      = null;
     InstallDelegates();
 }
Esempio n. 3
0
        /// <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="System.InvalidOperationException">
        /// Thrown if cURL isn't properly initialized.</exception>
        public static string Unescape(string url, int length)
        {
            EnsureCurl();
            IntPtr p = External.curl_unescape(url, length);
            String s = Marshal.PtrToStringAnsi(p);

            External.curl_free(p);
            return(s);
        }
Esempio n. 4
0
 /// <summary>
 /// Process-wide initialization -- call only once per process.
 /// </summary>
 /// <param name="flags">An or'd combination of
 /// <see cref="CURLinitFlag"/> members.</param>
 /// <returns>A <see cref="CURLcode"/>, hopefully
 /// <c>CURLcode.CURLE_OK</c>.</returns>
 public static CURLcode GlobalInit(int flags)
 {
     sm_curlCode = External.curl_global_init(flags);
     if (sm_curlCode == CURLcode.CURLE_OK)
     {
         External.curl_shim_initialize();
     }
     return(sm_curlCode);
 }
Esempio n. 5
0
 /// <summary>
 /// Process-wide cleanup -- call just before exiting process.
 /// </summary>
 /// <remarks>
 /// While it's not necessary that your program call this method
 /// before exiting, doing so will prevent leaks of native cURL resources.
 /// </remarks>
 public static void GlobalCleanup()
 {
     if (sm_curlCode == CURLcode.CURLE_OK)
     {
         External.curl_shim_cleanup();
         External.curl_global_cleanup();
         sm_curlCode = CURLcode.CURLE_FAILED_INIT;
     }
 }
Esempio n. 6
0
 private void InstallDelegates()
 {
     m_pDelLock   = new External.CURLSH_LOCK_DELEGATE(LockDelegate);
     m_pDelUnlock = new External.CURLSH_UNLOCK_DELEGATE(UnlockDelegate);
     m_hThis      = GCHandle.Alloc(this);
     m_ptrThis    = (IntPtr)m_hThis;
     External.curl_shim_install_share_delegates(m_pShare,
                                                m_ptrThis, m_pDelLock, m_pDelUnlock);
 }
Esempio n. 7
0
        private External.CURLSH_UNLOCK_DELEGATE m_pDelUnlock; // unlock delegate

        /// <summary>
        /// Constructor
        /// </summary>
        /// <exception cref="System.InvalidOperationException">This is thrown
        /// if <see cref="Curl"/> hasn't bee properly initialized.</exception>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>share</c> handle wasn't created successfully.</exception>
        public Share()
        {
            Curl.EnsureCurl();
            m_pShare = External.curl_share_init();
            EnsureHandle();
            m_pfLock   = null;
            m_pfUnlock = null;
            m_userData = null;
            InstallDelegates();
        }
Esempio n. 8
0
        /// <summary>
        /// Extract <c>int</c> information from an <c>Easy</c> object.
        /// </summary>
        /// <param name="info">One of the values in the
        /// <see cref="CURLINFO"/> enumeration. In this case, it must
        /// specifically be one of the members that obtains a <c>double</c>.
        /// </param>
        /// <param name="dblVal">Reference to an <c>double</c> value.</param>
        /// <returns>The <see cref="CURLcode"/> obtained from the internal
        /// call to <c>curl_easy_getinfo()</c>.
        /// </returns>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
        public CURLcode GetInfo(CURLINFO info, ref double dblVal)
        {
            EnsureHandle();

            // ensure it's an integral type
            if ((int)info < CURLINFO_DOUBLE)
            {
                return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
            }

            return(External.curl_easy_getinfo_64(m_pCURL, info, ref dblVal));
        }
Esempio n. 9
0
 private void Dispose(bool disposing)
 {
     lock (this)
     {
         // no if (disposing) pattern to clean up managed objects
         if (m_pStringList != IntPtr.Zero)
         {
             External.curl_shim_free_slist(m_pStringList);
             m_pStringList = IntPtr.Zero;
         }
     }
 }
Esempio n. 10
0
 private void Dispose(bool disposing)
 {
     lock (this)
     {
         // no if (disposing) pattern to clean up managed objects
         if (m_pItems[0] != IntPtr.Zero)
         {
             External.curl_formfree(m_pItems[0]);
         }
         m_pItems[0] = IntPtr.Zero;
         m_pItems[1] = IntPtr.Zero;
     }
 }
Esempio n. 11
0
        /// <summary>
        /// Set options for this object.
        /// </summary>
        /// <param name="option">
        /// One of the values in the <see cref="CURLSHoption"/>
        /// enumeration.
        /// </param>
        /// <param name="parameter">
        /// An appropriate object based on the value passed in the
        /// <c>option</c> argument. See <see cref="CURLSHoption"/>
        /// for more information about the appropriate parameter type.
        /// </param>
        /// <returns>
        /// A <see cref="CURLSHcode"/>, hopefully
        /// <c>CURLSHcode.CURLSHE_OK</c>.
        /// </returns>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>share</c> handle wasn't created successfully.</exception>
        public CURLSHcode SetOpt(CURLSHoption option, Object parameter)
        {
            EnsureHandle();
            CURLSHcode retCode = CURLSHcode.CURLSHE_OK;

            switch (option)
            {
            case CURLSHoption.CURLSHOPT_LOCKFUNC:
                LockFunction lf = parameter as LockFunction;
                if (lf == null)
                {
                    return(CURLSHcode.CURLSHE_BAD_OPTION);
                }
                m_pfLock = lf;
                break;

            case CURLSHoption.CURLSHOPT_UNLOCKFUNC:
                UnlockFunction ulf = parameter as UnlockFunction;
                if (ulf == null)
                {
                    return(CURLSHcode.CURLSHE_BAD_OPTION);
                }
                m_pfUnlock = ulf;
                break;

            case CURLSHoption.CURLSHOPT_SHARE:
            case CURLSHoption.CURLSHOPT_UNSHARE:
            {
                CURLlockData opt = (CURLlockData)
                                   Convert.ToInt32(parameter);
                if ((opt != CURLlockData.CURL_LOCK_DATA_COOKIE) &&
                    (opt != CURLlockData.CURL_LOCK_DATA_DNS))
                {
                    return(CURLSHcode.CURLSHE_BAD_OPTION);
                }
                retCode = External.curl_share_setopt(m_pShare,
                                                     option, (IntPtr)opt);
                break;
            }

            case CURLSHoption.CURLSHOPT_USERDATA:
                m_userData = parameter;
                break;

            default:
                retCode = CURLSHcode.CURLSHE_BAD_OPTION;
                break;
            }
            return(retCode);
        }
Esempio n. 12
0
 private void Dispose(bool disposing)
 {
     lock (this)
     {
         // if (disposing) cleanup managed objects
         if (m_pShare != IntPtr.Zero)
         {
             External.curl_shim_cleanup_share_delegates(m_pShare);
             External.curl_share_cleanup(m_pShare);
             m_hThis.Free();
             m_ptrThis = IntPtr.Zero;
             m_pShare  = IntPtr.Zero;
         }
     }
 }
Esempio n. 13
0
 // install the fuctions that will be called from libcurlshim
 private void InstallDelegates()
 {
     m_pDelWrite  = new External.CURL_WRITE_DELEGATE(WriteDelegate);
     m_pDelRead   = new External.CURL_READ_DELEGATE(ReadDelegate);
     m_pDelProg   = new External.CURL_PROGRESS_DELEGATE(ProgressDelegate);
     m_pDelDebug  = new External.CURL_DEBUG_DELEGATE(DebugDelegate);
     m_pDelHeader = new External.CURL_HEADER_DELEGATE(HeaderDelegate);
     m_pDelSSLCtx = new External.CURL_SSL_CTX_DELEGATE(SSLCtxDelegate);
     m_pDelIoctl  = new External.CURL_IOCTL_DELEGATE(IoctlDelegate);
     m_hThis      = GCHandle.Alloc(this);
     m_ptrThis    = (IntPtr)m_hThis;
     External.curl_shim_install_delegates(m_pCURL, m_ptrThis,
                                          m_pDelWrite, m_pDelRead, m_pDelProg,
                                          m_pDelDebug, m_pDelHeader, m_pDelSSLCtx, m_pDelIoctl);
 }
Esempio n. 14
0
 private void Dispose(bool disposing)
 {
     lock (this)
     {
         // if (disposing) cleanup managed resources
         // cleanup unmanaged resources
         if (m_pCURL != IntPtr.Zero)
         {
             External.curl_shim_cleanup_delegates(m_ptrThis);
             External.curl_easy_cleanup(m_pCURL);
             External.curl_shim_free_strings(m_pMyStrings);
             m_hThis.Free();
             m_pCURL = IntPtr.Zero;
         }
     }
 }
Esempio n. 15
0
        /// <summary>
        /// Extract <c>string</c> information from an <c>Easy</c> object.
        /// </summary>
        /// <param name="info">One of the values in the
        /// <see cref="CURLINFO"/> enumeration. In this case, it must
        /// specifically be one of the members that obtains a <c>string</c>.
        /// </param>
        /// <param name="strVal">Reference to an <c>string</c> value.</param>
        /// <returns>The <see cref="CURLcode"/> obtained from the internal
        /// call to <c>curl_easy_getinfo()</c>.
        /// </returns>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
        public CURLcode GetInfo(CURLINFO info, ref string strVal)
        {
            EnsureHandle();
            CURLcode retCode = CURLcode.CURLE_OK;
            IntPtr   ptr     = IntPtr.Zero;

            if ((int)info < CURLINFO_STRING || (int)info >= CURLINFO_LONG)
            {
                return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
            }
            retCode = External.curl_easy_getinfo(m_pCURL, info, ref ptr);
            if (retCode == CURLcode.CURLE_OK)
            {
                strVal = Marshal.PtrToStringAnsi(ptr);
            }
            return(retCode);
        }
Esempio n. 16
0
        /// <summary>
        /// Extract <c>int</c> information from an <c>Easy</c> object.
        /// </summary>
        /// <param name="info">One of the values in the
        /// <see cref="CURLINFO"/> enumeration. In this case, it must
        /// specifically be one of the members that obtains an <c>int</c>.
        /// </param>
        /// <param name="intVal">Reference to an <c>int</c> value.</param>
        /// <returns>The <see cref="CURLcode"/> obtained from the internal
        /// call to <c>curl_easy_getinfo()</c>.
        /// </returns>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
        public CURLcode GetInfo(CURLINFO info, ref int intVal)
        {
            EnsureHandle();
            CURLcode retCode = CURLcode.CURLE_OK;
            IntPtr   ptr     = IntPtr.Zero;

            // ensure it's an integral type
            if ((int)info < CURLINFO_LONG || (int)info >= CURLINFO_DOUBLE)
            {
                return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
            }

            retCode = External.curl_easy_getinfo(m_pCURL, info, ref ptr);
            if (retCode == CURLcode.CURLE_OK)
            {
                intVal = (int)ptr;
            }
            return(retCode);
        }
Esempio n. 17
0
 private Easy(Easy from)
 {
     m_pCURL = External.curl_easy_duphandle(from.m_pCURL);
     EnsureHandle();
     m_pMyStrings     = External.curl_shim_alloc_strings();
     m_pfWrite        = null;
     m_privateData    = null;
     m_writeData      = null;
     m_pfRead         = null;
     m_readData       = null;
     m_pfProgress     = null;
     m_progressData   = null;
     m_pfDebug        = null;
     m_debugData      = null;
     m_pfHeader       = null;
     m_headerData     = null;
     m_pfSSLContext   = null;
     m_sslContextData = null;
     m_pfIoctl        = null;
     m_ioctlData      = null;
     InstallDelegates();
 }
Esempio n. 18
0
        /// <summary>
        /// Extract information from a cURL handle.
        /// </summary>
        /// <param name="info">One of the values in the
        /// <see cref="CURLINFO"/> enumeration.</param>
        /// <param name="objInfo">Reference to an object into which the
        /// value specified by <c>info</c> is written.</param>
        /// <returns>The <see cref="CURLcode"/> obtained from the internal
        /// call to <c>curl_easy_getinfo()</c>.
        /// </returns>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
        public CURLcode GetInfo(CURLINFO info, ref Object objInfo)
        {
            EnsureHandle();
            CURLcode retCode = CURLcode.CURLE_OK;
            IntPtr   ptr     = IntPtr.Zero;

            if ((int)info < CURLINFO_STRING)
            {
                return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
            }

            // trickery for filetime
            if (info == CURLINFO.CURLINFO_FILETIME)
            {
                return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
            }

            // private data
            if (info == CURLINFO.CURLINFO_PRIVATE)
            {
                objInfo = m_privateData;
                return(retCode);
            }

            // string case
            if ((int)info < CURLINFO_LONG)
            {
                retCode = External.curl_easy_getinfo(m_pCURL, info, ref ptr);
                if (retCode == CURLcode.CURLE_OK)
                {
                    objInfo = (Object)Marshal.PtrToStringAnsi(ptr);
                }
                return(retCode);
            }

            // int or double: return problem
            return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
        }
Esempio n. 19
0
        /// <summary>
        /// Extract <c>Slist</c> information from an <c>Easy</c> object.
        /// </summary>
        /// <param name="info">One of the values in the
        /// <see cref="CURLINFO"/> enumeration. In this case, it must
        /// specifically be one of the members that obtains an <c>Slist</c>.
        /// </param>
        /// <param name="slist">Reference to an <c>Slist</c> value.</param>
        /// <returns>The <see cref="CURLcode"/> obtained from the internal
        /// call to <c>curl_easy_getinfo()</c>.
        /// </returns>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
        public CURLcode GetInfo(CURLINFO info, ref Slist slist)
        {
            EnsureHandle();
            CURLcode retCode = CURLcode.CURLE_OK;
            IntPtr   ptr = IntPtr.Zero, ptrs = IntPtr.Zero;

            if ((int)info < CURLINFO_SLIST)
            {
                return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
            }
            retCode = External.curl_easy_getinfo(m_pCURL, info, ref ptr);
            if (retCode != CURLcode.CURLE_OK)
            {
                return(retCode);
            }
            slist = new Slist();
            while (ptr != IntPtr.Zero)
            {
                ptr = External.curl_shim_get_string_from_slist(
                    ptr, ref ptrs);
                slist.Append(Marshal.PtrToStringAnsi(ptrs));
            }
            return(retCode);
        }
Esempio n. 20
0
        /// <summary>
        /// Add a multi-part form section.
        /// </summary>
        /// <param name="args">
        /// Argument list, as described in the remarks.
        /// </param>
        /// <returns>
        /// A <see cref="CURLFORMcode"/>, hopefully
        /// <c>CURLFORMcode.CURL_FORMADD_OK</c>.
        /// </returns>
        /// <remarks>
        /// This is definitely the workhorse method for this class. It
        /// should be called in roughly the same manner as
        /// <c>curl_formadd()</c>, except you would omit the first two
        /// <c>struct curl_httppost**</c> arguments (<c>firstitem</c> and
        /// <c>lastitem</c>), which are wrapped in this class. So you should
        /// pass arguments in the following sequence:
        /// <para>
        /// <c>MultiPartForm.AddSection(option1, value1, ..., optionX, valueX,
        /// CURLformoption.CURLFORM_END)</c>;
        /// </para>
        /// <para>
        /// For a complete list of possible options, see the documentation for
        /// the <see cref="CURLformoption"/> enumeration.
        /// </para>
        /// <note>
        /// The pointer options (<c>CURLFORM_PTRNAME</c>, etc.) make an
        /// internal copy of the passed <c>byte</c> array. Therefore, any
        /// changes you make to the client copy of this array AFTER calling
        /// this method, won't be reflected internally with <c>cURL</c>. The
        /// purpose of providing the pointer options is to support the
        /// posting of non-string binary data.
        /// </note>
        /// </remarks>
        public CURLFORMcode AddSection(params object[] args)
        {
            int          nCount     = args.Length;
            int          nRealCount = nCount;
            CURLFORMcode retCode    = CURLFORMcode.CURL_FORMADD_OK;

            CurlForms[] aForms = null;

            // one arg or even number of args is an error
            if ((nCount == 1) || (nCount % 2 == 0))
            {
                return(CURLFORMcode.CURL_FORMADD_INCOMPLETE);
            }

            // ensure the last argument is CURLFORM_END
            CURLformoption iCode = (CURLformoption)
                                   Convert.ToInt32(args.GetValue(nCount - 1));

            if (iCode != CURLformoption.CURLFORM_END)
            {
                return(CURLFORMcode.CURL_FORMADD_INCOMPLETE);
            }

            // walk through any passed arrays to get the true number of
            // items and ensure the child arrays are properly (and not
            // prematurely) terminated with CURLFORM_END
            for (int i = 0; i < nCount; i += 2)
            {
                iCode = (CURLformoption)Convert.ToInt32(args.GetValue(i));
                switch (iCode)
                {
                case CURLformoption.CURLFORM_ARRAY:
                {
                    aForms = args.GetValue(i + 1) as CurlForms[];
                    if (aForms == null)
                    {
                        return(CURLFORMcode.CURL_FORMADD_INCOMPLETE);
                    }
                    int nFormsCount = aForms.Length;
                    for (int j = 0; j < nFormsCount; j++)
                    {
                        CurlForms pcf = aForms.GetValue(j) as CurlForms;
                        if (pcf == null)
                        {
                            return(CURLFORMcode.CURL_FORMADD_INCOMPLETE);
                        }
                        if (j == nFormsCount - 1)
                        {
                            if (pcf.opt != CURLformoption.CURLFORM_END)
                            {
                                return(CURLFORMcode.CURL_FORMADD_INCOMPLETE);
                            }
                        }
                        else
                        {
                            if (pcf.opt == CURLformoption.CURLFORM_END)
                            {
                                return(CURLFORMcode.CURL_FORMADD_INCOMPLETE);
                            }
                        }
                    }
                    // -2 accounts for the fact that we're a) not
                    // including the item with CURLFORM_END and b) not
                    // including CURLFORM_ARRAY in what we pass to cURL
                    nRealCount += 2 * (nFormsCount - 2);
                    break;
                }

                default:
                    break;
                }
            }

            // allocate the IntPtr array for the data
            IntPtr[] aPointers = new IntPtr[nRealCount];
            for (int i = 0; i < nRealCount - 1; i++)
            {
                aPointers[i] = IntPtr.Zero;
            }
            aPointers[nRealCount - 1] = (IntPtr)CURLformoption.CURLFORM_END;

            // now we go through the args
            aForms = null;
            int    formArrayPos = 0;
            int    argArrayPos  = 0;
            int    ptrArrayPos  = 0;
            Object obj          = null;

            while ((retCode == CURLFORMcode.CURL_FORMADD_OK) &&
                   (ptrArrayPos < nRealCount))
            {
                if (aForms != null)
                {
                    CurlForms pcf = aForms.GetValue(formArrayPos++)
                                    as CurlForms;
                    if (pcf == null)
                    {
                        retCode = CURLFORMcode.CURL_FORMADD_UNKNOWN_OPTION;
                        break;
                    }
                    iCode = pcf.opt;
                    obj   = pcf.val;
                }
                else
                {
                    iCode = (CURLformoption)Convert.ToInt32(
                        args.GetValue(argArrayPos++));
                    obj = (iCode == CURLformoption.CURLFORM_END) ? null :
                          args.GetValue(argArrayPos++);
                }

                switch (iCode)
                {
                // handle byte-array pointer-related items
                case CURLformoption.CURLFORM_PTRNAME:
                case CURLformoption.CURLFORM_PTRCONTENTS:
                case CURLformoption.CURLFORM_BUFFERPTR:
                {
                    byte[] bytes = obj as byte[];
                    if (bytes == null)
                    {
                        retCode = CURLFORMcode.CURL_FORMADD_UNKNOWN_OPTION;
                    }
                    else
                    {
                        int    nLen = bytes.Length;
                        IntPtr ptr  = Marshal.AllocHGlobal(nLen);
                        if (ptr != IntPtr.Zero)
                        {
                            aPointers[ptrArrayPos++] = (IntPtr)iCode;
                            // copy bytes to unmanaged buffer
                            for (int j = 0; j < nLen; j++)
                            {
                                Marshal.WriteByte(ptr, bytes[j]);
                            }
                            aPointers[ptrArrayPos++] = ptr;
                        }
                        else
                        {
                            retCode = CURLFORMcode.CURL_FORMADD_MEMORY;
                        }
                    }
                    break;
                }

                // length values
                case CURLformoption.CURLFORM_NAMELENGTH:
                case CURLformoption.CURLFORM_CONTENTSLENGTH:
                case CURLformoption.CURLFORM_BUFFERLENGTH:
                    aPointers[ptrArrayPos++] = (IntPtr)iCode;
                    aPointers[ptrArrayPos++] = (IntPtr)
                                               Convert.ToInt32(obj);
                    break;

                // strings
                case CURLformoption.CURLFORM_COPYNAME:
                case CURLformoption.CURLFORM_COPYCONTENTS:
                case CURLformoption.CURLFORM_FILECONTENT:
                case CURLformoption.CURLFORM_FILE:
                case CURLformoption.CURLFORM_CONTENTTYPE:
                case CURLformoption.CURLFORM_FILENAME:
                case CURLformoption.CURLFORM_BUFFER:
                {
                    aPointers[ptrArrayPos++] = (IntPtr)iCode;
                    string s = obj as String;
                    if (s == null)
                    {
                        retCode = CURLFORMcode.CURL_FORMADD_UNKNOWN_OPTION;
                    }
                    else
                    {
                        IntPtr p = Marshal.StringToHGlobalAnsi(s);
                        if (p != IntPtr.Zero)
                        {
                            aPointers[ptrArrayPos++] = p;
                        }
                        else
                        {
                            retCode = CURLFORMcode.CURL_FORMADD_MEMORY;
                        }
                    }
                    break;
                }

                // array case: already handled
                case CURLformoption.CURLFORM_ARRAY:
                    if (aForms != null)
                    {
                        retCode = CURLFORMcode.CURL_FORMADD_ILLEGAL_ARRAY;
                    }
                    else
                    {
                        aForms = obj as CurlForms[];
                        if (aForms == null)
                        {
                            retCode = CURLFORMcode.CURL_FORMADD_UNKNOWN_OPTION;
                        }
                    }
                    break;

                // slist
                case CURLformoption.CURLFORM_CONTENTHEADER:
                {
                    aPointers[ptrArrayPos++] = (IntPtr)iCode;
                    Slist s = obj as Slist;
                    if (s == null)
                    {
                        retCode = CURLFORMcode.CURL_FORMADD_UNKNOWN_OPTION;
                    }
                    else
                    {
                        aPointers[ptrArrayPos++] = s.GetHandle();
                    }
                    break;
                }

                // erroneous stuff
                case CURLformoption.CURLFORM_NOTHING:
                    retCode = CURLFORMcode.CURL_FORMADD_INCOMPLETE;
                    break;

                // end
                case CURLformoption.CURLFORM_END:
                    if (aForms != null) // end of form
                    {
                        aForms       = null;
                        formArrayPos = 0;
                    }
                    else
                    {
                        aPointers[ptrArrayPos++] = (IntPtr)iCode;
                    }
                    break;

                // default is unknown
                default:
                    retCode = CURLFORMcode.CURL_FORMADD_UNKNOWN_OPTION;
                    break;
                }
            }

            // ensure we didn't come up short on parameters
            if (ptrArrayPos != nRealCount)
            {
                retCode = CURLFORMcode.CURL_FORMADD_INCOMPLETE;
            }

            // if we're OK here, call into curl
            if (retCode == CURLFORMcode.CURL_FORMADD_OK)
            {
                retCode = (CURLFORMcode)External.curl_shim_formadd(
                    m_pItems, aPointers, nRealCount);
            }

            // unmarshal native allocations
            for (int i = 0; i < nRealCount - 1; i += 2)
            {
                iCode = (CURLformoption)(int)aPointers[i];
                switch (iCode)
                {
                case CURLformoption.CURLFORM_COPYNAME:
                case CURLformoption.CURLFORM_COPYCONTENTS:
                case CURLformoption.CURLFORM_FILECONTENT:
                case CURLformoption.CURLFORM_FILE:
                case CURLformoption.CURLFORM_CONTENTTYPE:
                case CURLformoption.CURLFORM_FILENAME:
                case CURLformoption.CURLFORM_BUFFER:
                // byte buffer cases
                case CURLformoption.CURLFORM_PTRNAME:
                case CURLformoption.CURLFORM_PTRCONTENTS:
                case CURLformoption.CURLFORM_BUFFERPTR:
                {
                    if (aPointers[i + 1] != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(aPointers[i + 1]);
                    }
                    break;
                }

                default:
                    break;
                }
            }

            return(retCode);
        }
Esempio n. 21
0
 /// <summary>
 /// Append a string to the list.
 /// </summary>
 /// <param name="str">The <c>string</c> to append.</param>
 public void Append(string str)
 {
     m_pStringList = External.curl_shim_add_string_to_slist(
         m_pStringList, str);
 }
Esempio n. 22
0
 /// <summary>
 /// Return a String description of an error code.
 /// </summary>
 /// <param name="errorNum">
 /// The <see cref="CURLSHcode"/> for which to obtain the error
 /// string description.
 /// </param>
 /// <returns>The string description.</returns>
 public String StrError(CURLSHcode errorNum)
 {
     return(Marshal.PtrToStringAnsi(External.curl_share_strerror(
                                        errorNum)));
 }
Esempio n. 23
0
 /// <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)
 {
     return(Marshal.PtrToStringAnsi(
                External.curl_easy_strerror(code)));
 }
Esempio n. 24
0
 /// <summary>
 /// Perform a transfer.
 /// </summary>
 /// <exception cref="System.NullReferenceException">This is thrown if
 /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
 /// <returns>The <see cref="CURLcode"/> obtained from the internal
 /// call to <c>curl_easy_perform()</c>.
 /// </returns>
 public CURLcode Perform()
 {
     EnsureHandle();
     return((CURLcode)External.curl_easy_perform(m_pCURL));
 }
Esempio n. 25
0
        /// <summary>
        /// Set options for this object. See the <c>EasyGet</c> sample for
        /// basic usage.
        /// </summary>
        /// <param name="option">This should be a valid <see cref="CURLoption"/>.</param>
        /// <param name="parameter">This should be a parameter of a varying
        /// type based on the value of the <c>option</c> parameter.</param>
        /// <exception cref="System.NullReferenceException">This is thrown if
        /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
        /// <returns>A <see cref="CURLcode"/>, typically obtained from
        /// <c>cURL</c> internally, but sometimes a
        /// <see cref="CURLcode.CURLE_BAD_FUNCTION_ARGUMENT"/>
        /// will be returned if the type of value of <c>parameter</c> is invalid.
        /// </returns>
        public CURLcode SetOpt(CURLoption option, Object parameter)
        {
            EnsureHandle();
            CURLcode retCode = CURLcode.CURLE_OK;

            // numeric cases
            if ((int)option < CURLOPTTYPE_OBJECTPOINT)
            {
                int i = 0;
                if (option == CURLoption.CURLOPT_DNS_USE_GLOBAL_CACHE ||
                    option == CURLoption.CURLOPT_SOURCE_PORT)
                {
                    return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                }
                else if (option == CURLoption.CURLOPT_TIMEVALUE)
                {
                    // unboxing may throw class cast exception
                    DateTime d         = (DateTime)parameter;
                    DateTime startTime = new DateTime(1970, 1, 1);
                    TimeSpan currTime  = new TimeSpan(DateTime.Now.Ticks -
                                                      startTime.Ticks);
                    i = Convert.ToInt32(currTime.TotalSeconds);
                }
                else
                {
                    i = Convert.ToInt32(parameter);
                }
                retCode = External.curl_easy_setopt(m_pCURL,
                                                    option, (IntPtr)i);
            }

            // object cases: the majority
            else if ((int)option < CURLOPTTYPE_FUNCTIONPOINT)
            {
                switch (option)
                {
                // various data items
                case CURLoption.CURLOPT_PRIVATE:
                    m_privateData = parameter; break;

                case CURLoption.CURLOPT_WRITEDATA:
                    m_writeData = parameter; break;

                case CURLoption.CURLOPT_READDATA:
                    m_readData = parameter; break;

                case CURLoption.CURLOPT_PROGRESSDATA:
                    m_progressData = parameter; break;

                case CURLoption.CURLOPT_DEBUGDATA:
                    m_debugData = parameter; break;

                case CURLoption.CURLOPT_HEADERDATA:
                    m_headerData = parameter; break;

                case CURLoption.CURLOPT_SSL_CTX_DATA:
                    m_sslContextData = parameter; break;

                case CURLoption.CURLOPT_IOCTLDATA:
                    m_ioctlData = parameter; break;

                // items that can't be set externally or
                // obsolete items
                case CURLoption.CURLOPT_ERRORBUFFER:
                case CURLoption.CURLOPT_STDERR:
                case CURLoption.CURLOPT_SOURCE_HOST:
                case CURLoption.CURLOPT_SOURCE_PATH:
                case CURLoption.CURLOPT_PASV_HOST:
                    return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);

                // singular case for share
                case CURLoption.CURLOPT_SHARE:
                {
                    Share share = parameter as Share;
                    if (share == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    retCode = External.curl_easy_setopt(m_pCURL,
                                                        option, share.GetHandle());
                    break;
                }

                // multipart HTTP post
                case CURLoption.CURLOPT_HTTPPOST:
                {
                    MultiPartForm mf = parameter as MultiPartForm;
                    if (mf == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    retCode = External.curl_easy_setopt(m_pCURL,
                                                        option, mf.GetHandle());
                    break;
                }

                // items curl wants as a curl_slist
                case CURLoption.CURLOPT_HTTPHEADER:
                case CURLoption.CURLOPT_PREQUOTE:
                case CURLoption.CURLOPT_QUOTE:
                case CURLoption.CURLOPT_POSTQUOTE:
                case CURLoption.CURLOPT_SOURCE_QUOTE:
                case CURLoption.CURLOPT_TELNETOPTIONS:
                case CURLoption.CURLOPT_HTTP200ALIASES:
                {
                    Slist slist = parameter as Slist;
                    if (slist == null)
                    {
                        retCode = External.curl_easy_setopt(m_pCURL,
                                                            option, IntPtr.Zero);
                    }
                    else
                    {
                        retCode = External.curl_easy_setopt(m_pCURL,
                                                            option, slist.GetHandle());
                    }
                    break;
                }

                // string items
                default:
                {
                    string s = parameter as string;
                    if (s == null)
                    {
                        retCode = External.curl_easy_setopt(m_pCURL,
                                                            option, IntPtr.Zero);
                    }
                    else
                    {
                        IntPtr pCurlStr = External.curl_shim_add_string(
                            m_pMyStrings, s);
                        if (pCurlStr != IntPtr.Zero)
                        {
                            retCode = External.curl_easy_setopt(m_pCURL,
                                                                option, pCurlStr);
                        }
                    }
                    break;
                }
                }
            }

            // FUNCTIONPOINT args, for delegates
            else if ((int)option < CURLOPTTYPE_OFF_T)
            {
                switch (option)
                {
                case CURLoption.CURLOPT_WRITEFUNCTION:
                {
                    WriteFunction wf = parameter as WriteFunction;
                    if (wf == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    m_pfWrite = wf;
                    break;
                }

                case CURLoption.CURLOPT_READFUNCTION:
                {
                    ReadFunction rf = parameter as ReadFunction;
                    if (rf == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    m_pfRead = rf;
                    break;
                }

                case CURLoption.CURLOPT_PROGRESSFUNCTION:
                {
                    ProgressFunction pf = parameter as ProgressFunction;
                    if (pf == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    m_pfProgress = pf;
                    break;
                }

                case CURLoption.CURLOPT_DEBUGFUNCTION:
                {
                    DebugFunction pd = parameter as DebugFunction;
                    if (pd == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    m_pfDebug = pd;
                    break;
                }

                case CURLoption.CURLOPT_HEADERFUNCTION:
                {
                    HeaderFunction hf = parameter as HeaderFunction;
                    if (hf == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    m_pfHeader = hf;
                    break;
                }

                case CURLoption.CURLOPT_SSL_CTX_FUNCTION:
                {
                    SSLContextFunction sf = parameter as SSLContextFunction;
                    if (sf == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    m_pfSSLContext = sf;
                    break;
                }

                case CURLoption.CURLOPT_IOCTLFUNCTION:
                {
                    IoctlFunction iof = parameter as IoctlFunction;
                    if (iof == null)
                    {
                        return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                    }
                    m_pfIoctl = iof;
                    break;
                }

                default:
                    return(CURLcode.CURLE_BAD_FUNCTION_ARGUMENT);
                }
            }

            // otherwise, it's one of those 64-bit off_t guys!
            else
            {
                Int64 i = Convert.ToInt64(parameter);
                retCode = External.curl_easy_setopt_64(m_pCURL,
                                                       option, i);
            }

            return(retCode);
        }
Esempio n. 26
0
 /// <summary>
 /// Reset the internal cURL handle.
 /// </summary>
 /// <exception cref="System.NullReferenceException">This is thrown if
 /// the native <c>CURL*</c> handle wasn't created successfully.</exception>
 public void Reset()
 {
     EnsureHandle();
     External.curl_easy_reset(m_pCURL);
 }
Esempio n. 27
0
 internal VersionInfoData(CURLversion ver)
 {
     m_pVersionInfoData = External.curl_version_info(ver);
 }
Esempio n. 28
0
        private void btn_Post_Click(object sender, EventArgs e)
        {
            IntPtr m_pCURL = External.curl_easy_init();

            CURLcode retCode = CURLcode.CURLE_OK;

            retCode = External.curl_easy_setopt(m_pCURL, CURLoption.CURLOPT_URL, Marshal.StringToCoTaskMemAnsi("https://account.storepos.cn/UnifiedPay/Mch_Reg"));
            retCode = External.curl_easy_setopt(m_pCURL, CURLoption.CURLOPT_POSTFIELDS, Marshal.StringToCoTaskMemAnsi($"a={Uri.EscapeDataString("我是中文")}&b={Uri.EscapeDataString("i am English")}"));
            retCode = External.curl_easy_setopt(m_pCURL, CURLoption.CURLOPT_USERAGENT, Marshal.StringToCoTaskMemAnsi("我是尹自强的工具"));
            retCode = External.curl_easy_setopt(m_pCURL, CURLoption.CURLOPT_SSL_VERIFYHOST, (IntPtr)0);
            retCode = External.curl_easy_setopt(m_pCURL, CURLoption.CURLOPT_SSL_VERIFYPEER, (IntPtr)0);

            CURLcode res = External.curl_easy_perform(m_pCURL);

            if (res != CURLcode.CURLE_OK)
            {
                // Error
            }

            this.txt_Result.AppendText(res.ToString());
            this.txt_Result.AppendText(Environment.NewLine);

            External.curl_easy_cleanup(m_pCURL);

            return;

            //try
            //{
            //    using (var pro = new Process())
            //    {
            //        var str_form = $"a={Uri.EscapeDataString("我是中文")}&b={Uri.EscapeDataString("i am English")}";
            //        var str_cmd = $"-H \"Content-Type:application/x-www-form-urlencoded;charset=UTF-8\" --user-agent \"YZQ.curl\" --data \"{str_form}\" https://account.storepos.cn/UnifiedPay/Mch_Reg";

            //        pro.StartInfo.FileName = Path.Combine(Environment.CurrentDirectory, "Lib\\curl.exe");
            //        pro.StartInfo.UseShellExecute = false;
            //        pro.StartInfo.RedirectStandardInput = true;
            //        pro.StartInfo.RedirectStandardOutput = true;
            //        pro.StartInfo.RedirectStandardError = true;
            //        pro.StartInfo.CreateNoWindow = true;
            //        pro.StartInfo.Arguments = str_cmd;
            //        //pro.StartInfo.StandardInputEncoding = Encoding.UTF8;
            //        pro.StartInfo.StandardOutputEncoding = Encoding.UTF8;
            //        pro.Start();
            //        pro.StandardInput.AutoFlush = true;

            //        //获取cmd窗口的输出信息
            //        var output = pro.StandardOutput.ReadToEnd();

            //        pro.WaitForExit();
            //        pro.Close();

            //        this.AppendResult(string.Format("【FormData】:{0}", str_form));
            //        this.AppendResult(string.Format("【Arguments】:{0}", str_cmd));
            //        this.AppendResult(string.Format("【Response】:{0}", output));
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.Message);
            //}
        }