Example #1
0
        public CURLE GetInfo(CURLINFO info, out CurlSlist value)
        {
            value = null;
            IntPtr ptr    = IntPtr.Zero;
            var    result = Lib.curl_easy_getinfo_ptr(easyPtr, info, ref ptr);

            value = new CurlSlist(ptr);
            return(result);
        }
Example #2
0
        private void Prepare()
        {
            try
            {
                status  = 0;
                message = null;

                thisHandle = GCHandle.Alloc(this);

                SetOpt(CURLOPT.URL, uri.AbsoluteUri);

                var upperMethod = method.ToUpper();
                switch (upperMethod)
                {
                case "GET":
                    SetOpt(CURLOPT.HTTPGET, true);
                    break;

                case "HEAD":
                    SetOpt(CURLOPT.NOBODY, true);
                    break;

                case "POST":
                    SetOpt(CURLOPT.POST, true);
                    break;

                default:
                    SetOpt(CURLOPT.CUSTOMREQUEST, method);
                    break;
                }

                if (useHttp2)
                {
                    SetOpt(CURLOPT.HTTP_VERSION, (long)HTTPVersion.VERSION_2TLS);
                }
                else
                {
                    SetOpt(CURLOPT.HTTP_VERSION, (long)HTTPVersion.VERSION_1_1);
                }
                SetOpt(CURLOPT.PIPEWAIT, true);

                SetOpt(CURLOPT.SSL_VERIFYHOST, !insecure);
                SetOpt(CURLOPT.SSL_VERIFYPEER, !insecure);

                // Ca cert path
                SetOpt(CURLOPT.CAINFO, s_capath);

                // Fill request header
                var requestHeader = new CurlSlist(IntPtr.Zero);
                if (disableExpect)
                {
                    requestHeader.Append("Expect:");
                }
                requestHeader.Append($"Content-Type:{contentType}");
                if (userHeader != null)
                {
                    foreach (var entry in userHeader)
                    {
                        requestHeader.Append(entry.Key + ":" + entry.Value);
                    }
                }

                SetOpt(CURLOPT.HTTPHEADER, (IntPtr)requestHeader);
                // Fill request body
                if (outData != null && outData.Length > 0)
                {
                    SetOpt(CURLOPT.POSTFIELDS, outData);
                    SetOpt(CURLOPT.POSTFIELDSIZE, outData.Length);
                }

                // Handle response header
                responseHeaderStream = new MemoryStream();
                SetOpt(CURLOPT.HEADERFUNCTION, (Delegates.HeaderFunction)HeaderFunction);
                SetOpt(CURLOPT.HEADERDATA, (IntPtr)thisHandle);

                bool rangeRequest = rangeStart > 0 || rangeEnd > 0;

                if (rangeRequest)
                {
                    if (rangeEnd == 0)
                    {
                        SetOpt(CURLOPT.RANGE, $"{rangeStart}-");
                    }
                    else
                    {
                        SetOpt(CURLOPT.RANGE, $"{rangeStart}-{rangeEnd}");
                    }
                }
                else
                {
                    SetOpt(CURLOPT.RANGE, IntPtr.Zero);
                }

                // Handle response body
                recievedDataLength = 0;
                if (string.IsNullOrEmpty(outputPath))
                {
                    responseBodyStream = new MemoryStream();
                }
                else
                {
                    var dir = Path.GetDirectoryName(outputPath);
                    if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    responseBodyStream = new FileStream(outputPath, rangeRequest ? FileMode.Append : FileMode.OpenOrCreate);
                }
                SetOpt(CURLOPT.WRITEFUNCTION, (Delegates.WriteFunction)WriteFunction);
                SetOpt(CURLOPT.WRITEDATA, (IntPtr)thisHandle);

                // Debug
                if (debug)
                {
                    outHeader = null;
                    SetOpt(CURLOPT.VERBOSE, true);
                    SetOpt(CURLOPT.DEBUGFUNCTION, DebugFunction);
                    SetOpt(CURLOPT.DEBUGDATA, (IntPtr)thisHandle);
                }
                else
                {
                    SetOpt(CURLOPT.VERBOSE, false);
                    SetOpt(CURLOPT.DEBUGFUNCTION, IntPtr.Zero);
                    SetOpt(CURLOPT.DEBUGDATA, IntPtr.Zero);
                }

                if (progressCallback != null)
                {
                    SetOpt(CURLOPT.NOPROGRESS, false);
                    SetOpt(CURLOPT.XFERINFOFUNCTION, ProgressFunction);
                    SetOpt(CURLOPT.XFERINFODATA, (IntPtr)thisHandle);
                }
                else
                {
                    SetOpt(CURLOPT.NOPROGRESS, true);
                    SetOpt(CURLOPT.XFERINFOFUNCTION, IntPtr.Zero);
                    SetOpt(CURLOPT.XFERINFODATA, IntPtr.Zero);
                }

                // Timeout
                SetOpt(CURLOPT.CONNECTTIMEOUT_MS, connectionTimeout);
                SetOpt(CURLOPT.TIMEOUT_MS, timeout);
                SetOpt(CURLOPT.LOW_SPEED_LIMIT, lowSpeedLimit);
                SetOpt(CURLOPT.LOW_SPEED_TIME, lowSpeedTimeout);

                // Speed limitation
                SetOpt(CURLOPT.MAX_SEND_SPEED_LARGE, outSpeedLimit);
                SetOpt(CURLOPT.MAX_RECV_SPEED_LARGE, inSpeedLimit);
            }
            catch (Exception e)
            {
                CurlLog.LogError("Unexpected exception: " + e);
            }
        }
Example #3
0
        private void Prepare()
        {
            try
            {
                status  = 0;
                message = null;

                thisHandle = GCHandle.Alloc(this);

                SetOpt(CURLOPT.URL, uri.AbsoluteUri);
                SetOpt(CURLOPT.CUSTOMREQUEST, method);

                SetOpt(CURLOPT.HTTP_VERSION, (long)HTTPVersion.VERSION_2TLS);
                SetOpt(CURLOPT.PIPEWAIT, true);


                if (insecure)
                {
                    SetOpt(CURLOPT.SSL_VERIFYHOST, false);
                    SetOpt(CURLOPT.SSL_VERIFYPEER, false);
                }

                // Ca cert path
                SetOpt(CURLOPT.CAINFO, s_capath);

                // Fill request header
                var requestHeader = new CurlSlist(IntPtr.Zero);
                requestHeader.Append($"Content-Type:{contentType}");
                if (userHeader != null)
                {
                    foreach (var entry in userHeader)
                    {
                        requestHeader.Append(entry.Key + ":" + entry.Value);
                    }
                }

                SetOpt(CURLOPT.HTTPHEADER, (IntPtr)requestHeader);
                // Fill request body
                if (outData != null && outData.Length > 0)
                {
                    SetOpt(CURLOPT.POSTFIELDS, outData);
                    SetOpt(CURLOPT.POSTFIELDSIZE, outData.Length);
                }

                // Handle response header
                responseHeaderStream = new MemoryStream();
                SetOpt(CURLOPT.HEADERFUNCTION, (Delegates.HeaderFunction)HeaderFunction);
                SetOpt(CURLOPT.HEADERDATA, (IntPtr)thisHandle);

                // Handle response body
                recievedDataLength = 0;
                if (string.IsNullOrEmpty(outputPath))
                {
                    responseBodyStream = new MemoryStream();
                }
                else
                {
                    var dir = Path.GetDirectoryName(outputPath);
                    if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    responseBodyStream = new FileStream(outputPath, FileMode.OpenOrCreate);
                }
                SetOpt(CURLOPT.WRITEFUNCTION, (Delegates.WriteFunction)WriteFunction);
                SetOpt(CURLOPT.WRITEDATA, (IntPtr)thisHandle);

                // Debug
                if (debug)
                {
                    outHeader = null;
                    SetOpt(CURLOPT.VERBOSE, true);
                    SetOpt(CURLOPT.DEBUGFUNCTION, DebugFunction);
                    SetOpt(CURLOPT.DEBUGDATA, (IntPtr)thisHandle);
                }

                // Timeout
                SetOpt(CURLOPT.CONNECTTIMEOUT_MS, connectionTimeout);
                SetOpt(CURLOPT.TIMEOUT_MS, timeout);
                SetOpt(CURLOPT.LOW_SPEED_LIMIT, lowSpeedLimit);
                SetOpt(CURLOPT.LOW_SPEED_TIME, lowSpeedTimeout);

                // Speed limitation
                SetOpt(CURLOPT.MAX_SEND_SPEED_LARGE, outSpeedLimit);
                SetOpt(CURLOPT.MAX_RECV_SPEED_LARGE, inSpeedLimit);
            }
            catch (Exception e)
            {
                CurlLog.LogError("Unexpected exception: " + e);
            }
        }