private CurlSlist _SetHeader(string authb, Dictionary <string, string> urlargs)
        {
            if (
                (string.IsNullOrWhiteSpace(authb)) &&
                (urlargs == null)
                )
            {
                return(null);
            }

            CurlSlist sl = new CurlSlist();

            if ((urlargs != null) && (urlargs.Count > 0))
            {
                foreach (var row in urlargs)
                {
                    sl.Append(row.Key + ": " + row.Value);
                }
            }
            if (!string.IsNullOrWhiteSpace(authb))
            {
                sl.Append(
                    string.Format(
                        stCurlClientSet.urlBearerShare,
                        authb
                        )
                    );
            }
            return(sl);
        }
示例#2
0
        private static void Main(string[] args)
        {
            try
            {
                Curl.GlobalInit(CurlInitFlag.All);

                using (var curl = new CurlEasy())
                {
                    /* This is the URL for your mailserver */
                    curl.Url = "smtp://*****:*****@example.org>");

                    /* Add two recipients, in this particular case they correspond to
                     * the To: and Cc: addressees in the header, but they could be any
                     * kind of recipient. */
                    using (var recipients = new CurlSlist())
                    {
                        recipients.Append("<*****@*****.**>");
                        recipients.Append("<*****@*****.**>");
                        curl.SetOpt(CurlOption.MailRcpt, recipients);

                        /* We're using a callback function to specify the payload (the
                         * headers and body of the message). You could just use the
                         * ReadData option to  specify a FILE pointer to read from. */
                        curl.ReadFunction = PayloadSource;
                        curl.ReadData     = new UploadContext();
                        curl.Upload       = true;

                        var res = curl.Perform();
                        if (res != CurlCode.Ok)
                        {
                            Console.WriteLine("CurlEasy.Perform() failed: " + res);
                        }
                    }

                    /* curl won't send the QUIT command until you call cleanup, so you should be
                     * able to re-use this connection for additional messages (setting
                     * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
                     * Perform() again. It may not be a good idea to keep the
                     * connection open for a very long time though (more than a few minutes may
                     * result in the server timing out the connection), and you do want to clean
                     * up in the end.
                     */
                }

                Curl.GlobalCleanup();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.ReadLine();
            }
        }
示例#3
0
        private static void Main(string[] args)
        {
            try
            {
                Curl.GlobalInit(CurlInitFlag.All);

                using (var curl = new CurlEasy())
                {
                    curl.Url    = "smtp://*****:*****@example.com>");
                    using (var recipients = new CurlSlist())
                    {
                        recipients.Append("<*****@*****.**>");
                        recipients.Append("<*****@*****.**>");
                        var s = recipients.Strings;
                        curl.SetOpt(CurlOption.MailRcpt, recipients.Handle);

                        curl.Perform();
                    }
                }

                Curl.GlobalCleanup();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
示例#4
0
        private static CurlSlist GetHeaders(string token)
        {
            var l = new CurlSlist();

            if (!string.IsNullOrEmpty(token))
            {
                l.Append($"Authorization: token {token}");
            }
            return(l);
        }
示例#5
0
        private CurlSlist GetHeaders(HttpHeaderHelper httpHeaders)
        {
            var headers = new CurlSlist();

            foreach (var httpHeader in httpHeaders.GetRequestHeaders())
            {
                headers.Append($"{httpHeader.Key}: {httpHeader.Value}");
            }

            return(headers);
        }
示例#6
0
        private CurlSlist SerializeHeaders(HttpWebRequest webRequest)
        {
            if (webRequest.SendChunked)
            {
                throw new NotSupportedException("Chunked transfer is not supported");
            }

            if (webRequest.ContentLength > 0)
            {
                webRequest.Headers.Add("Content-Length", webRequest.ContentLength.ToString());
            }

            if (webRequest.AutomaticDecompression.HasFlag(DecompressionMethods.GZip))
            {
                if (webRequest.AutomaticDecompression.HasFlag(DecompressionMethods.Deflate))
                {
                    webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
                }
                else
                {
                    webRequest.Headers.Add("Accept-Encoding", "gzip");
                }
            }
            else
            {
                if (webRequest.AutomaticDecompression.HasFlag(DecompressionMethods.Deflate))
                {
                    webRequest.Headers.Add("Accept-Encoding", "deflate");
                }
            }


            var curlHeaders = new CurlSlist();

            for (int i = 0; i < webRequest.Headers.Count; i++)
            {
                curlHeaders.Append(webRequest.Headers.GetKey(i) + ": " + webRequest.Headers.Get(i));
            }

            curlHeaders.Append("Content-Type: " + webRequest.ContentType ?? string.Empty);

            return(curlHeaders);
        }
示例#7
0
        private CurlSlist SerializeHeaders(HttpRequest request)
        {
            if (!request.Headers.ContainsKey("Accept-Encoding"))
            {
                request.Headers.Add("Accept-Encoding", "gzip");
            }

            if (request.Headers.ContentType == null)
            {
                request.Headers.ContentType = string.Empty;
            }

            var curlHeaders = new CurlSlist();

            foreach (var header in request.Headers)
            {
                curlHeaders.Append(header.Key + ": " + header.Value.ToString());
            }

            return(curlHeaders);
        }
示例#8
0
        private CurlEasy GetCurlEasy(string url, string cipher, CurlSlist headers)
        {
            var curlEasy = new CurlEasy
            {
                //TODO: Add certificates to cURL tests SslVerifyPeer = true, CaInfo = full path to certs
                WriteFunction = HandleResponse,
                SslVerifyPeer = false,
                SslVerifyhost = false,
                Url           = url,
                SslCipherList = cipher,
                HttpHeader    = headers
            };

            //TODO: Add certificates to cURL tests SslVerifyPeer
            if (_securityContext.SendClientCert)
            {
                curlEasy.SslCert      = AppSettingsHelper.CurlClientCertificate;
                curlEasy.SslKey       = AppSettingsHelper.CurlClientKey;
                curlEasy.SslKeyPasswd = AppSettingsHelper.CurlClientPassword;
            }

            return(curlEasy);
        }
示例#9
0
        public static CurlResponse PerformCurl(CurlRequest curlRequest, ServerConfig config)
        {
            lock (instance)
            {
                var headerBuffers  = new List <byte[]>();
                var contentBuffers = new List <byte[]>();

                using (var easy = new CurlEasy())
                {
                    easy.Url            = curlRequest.Url;
                    easy.BufferSize     = 64 * 1024;
                    easy.UserAgent      = BrowserUtil.ChromeUserAgent;
                    easy.FollowLocation = false;
                    easy.ConnectTimeout = 20;
                    if (curlRequest.Headers != null)
                    {
                        CurlSlist curlHeaders = new CurlSlist();
                        foreach (var header in curlRequest.Headers)
                        {
                            curlHeaders.Append(header.Key + ": " + header.Value);
                        }
                        easy.SetOpt(CurlOption.HttpHeader, curlHeaders);
                    }

                    easy.WriteFunction = (byte[] buf, int size, int nmemb, object data) =>
                    {
                        contentBuffers.Add(buf);
                        return(size * nmemb);
                    };

                    easy.HeaderFunction = (byte[] buf, int size, int nmemb, object extraData) =>
                    {
                        headerBuffers.Add(buf);
                        return(size * nmemb);
                    };

                    if (!string.IsNullOrEmpty(curlRequest.Cookies))
                    {
                        easy.Cookie = curlRequest.Cookies;
                    }

                    if (!string.IsNullOrEmpty(curlRequest.Referer))
                    {
                        easy.Referer = curlRequest.Referer;
                    }

                    if (curlRequest.Method == HttpMethod.Post)
                    {
                        if (!string.IsNullOrEmpty(curlRequest.RawPOSTDdata))
                        {
                            easy.Post          = true;
                            easy.PostFields    = curlRequest.RawPOSTDdata;
                            easy.PostFieldSize = Encoding.UTF8.GetByteCount(curlRequest.RawPOSTDdata);
                        }
                        else
                        {
                            easy.Post = true;
                            var postString = StringUtil.PostDataFromDict(curlRequest.PostData);
                            easy.PostFields    = postString;
                            easy.PostFieldSize = Encoding.UTF8.GetByteCount(postString);
                        }
                    }

                    if (JackettStartup.DoSSLFix == true)
                    {
                        // http://stackoverflow.com/questions/31107851/how-to-fix-curl-35-cannot-communicate-securely-with-peer-no-common-encryptio
                        // https://git.fedorahosted.org/cgit/mod_nss.git/plain/docs/mod_nss.html
                        easy.SslCipherList = SSLFix.CipherList;
                        easy.FreshConnect  = true;
                        easy.ForbidReuse   = true;
                    }

                    if (JackettStartup.IgnoreSslErrors == true)
                    {
                        easy.SetOpt(CurlOption.SslVerifyhost, false);
                        easy.SetOpt(CurlOption.SslVerifyPeer, false);
                    }

                    var proxy = config.GetProxyUrl();
                    if (proxy != null)
                    {
                        easy.SetOpt(CurlOption.HttpProxyTunnel, 1);
                        easy.SetOpt(CurlOption.Proxy, proxy);

                        var authString = config.GetProxyAuthString();
                        if (authString != null)
                        {
                            easy.SetOpt(CurlOption.ProxyUserPwd, authString);
                        }
                    }

                    easy.Perform();

                    if (easy.LastErrorCode != CurlCode.Ok)
                    {
                        var message = "Error " + easy.LastErrorCode.ToString() + " " + easy.LastErrorDescription + " " + easy.ErrorBuffer;
                        if (null != OnErrorMessage)
                        {
                            OnErrorMessage(message);
                        }
                        else
                        {
                            Console.WriteLine(message);
                        }
                    }
                }

                var headerBytes  = Combine(headerBuffers.ToArray());
                var headerString = Encoding.UTF8.GetString(headerBytes);
                if (config.GetProxyUrl() != null)
                {
                    var firstcrlf  = headerString.IndexOf("\r\n\r\n");
                    var secondcrlf = headerString.IndexOf("\r\n\r\n", firstcrlf + 1);
                    if (secondcrlf > 0)
                    {
                        headerString = headerString.Substring(firstcrlf + 4, secondcrlf - (firstcrlf));
                    }
                }
                var            headerParts   = headerString.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                var            headers       = new List <string[]>();
                var            headerCount   = 0;
                HttpStatusCode status        = HttpStatusCode.NotImplemented;
                var            cookieBuilder = new StringBuilder();
                var            cookies       = new List <Tuple <string, string> >();
                foreach (var headerPart in headerParts)
                {
                    if (headerCount == 0)
                    {
                        var split = headerPart.Split(' ');
                        if (split.Length < 2)
                        {
                            throw new Exception("HTTP Header missing");
                        }
                        var responseCode = int.Parse(headerPart.Split(' ')[1]);
                        status = (HttpStatusCode)responseCode;
                    }
                    else
                    {
                        var keyVal = headerPart.Split(new char[] { ':' }, 2);
                        if (keyVal.Length > 1)
                        {
                            var key   = keyVal[0].ToLower().Trim();
                            var value = keyVal[1].Trim();

                            if (key == "set-cookie")
                            {
                                var nameSplit = value.IndexOf('=');
                                if (nameSplit > -1)
                                {
                                    var cKey = value.Substring(0, nameSplit);
                                    var cVal = value.Split(';')[0] + ";";
                                    cookies.Add(new Tuple <string, string>(cKey, cVal));
                                }
                            }
                            else
                            {
                                headers.Add(new[] { key, value });
                            }
                        }
                    }

                    headerCount++;
                }

                foreach (var cookieGroup in cookies.GroupBy(c => c.Item1))
                {
                    cookieBuilder.AppendFormat("{0} ", cookieGroup.Last().Item2);
                }

                // add some debug output to track down the problem causing people getting InternalServerError results
                if (status == HttpStatusCode.NotImplemented || status == HttpStatusCode.InternalServerError)
                {
                    try
                    {
                        OnErrorMessage("got NotImplemented/InternalServerError");
                        OnErrorMessage("request.Method: " + curlRequest.Method);
                        OnErrorMessage("request.Url: " + curlRequest.Url);
                        OnErrorMessage("request.Cookies: " + curlRequest.Cookies);
                        OnErrorMessage("request.Referer: " + curlRequest.Referer);
                        OnErrorMessage("request.RawPOSTDdata: " + curlRequest.RawPOSTDdata);
                        OnErrorMessage("cookies: " + cookieBuilder.ToString().Trim());
                        OnErrorMessage("headerString:\n" + headerString);

                        foreach (var headerPart in headerParts)
                        {
                            OnErrorMessage("headerParts: " + headerPart);
                        }
                    }
                    catch (Exception ex)
                    {
                        OnErrorMessage(string.Format("CurlHelper: error while handling NotImplemented/InternalServerError:\n{0}", ex));
                    }
                }

                var contentBytes = Combine(contentBuffers.ToArray());
                var curlResponse = new CurlResponse(headers, contentBytes, status, cookieBuilder.ToString().Trim());
                return(curlResponse);
            }
        }
示例#10
0
        public static string Download(string url, string filename = null, IUser user = null, Dictionary <string, string> additionalHeaders = null)
        {
            user = user ?? new NullUser();
            user.RaiseMessage("Downloading {0}", url);

            // Generate a temporary file if none is provided.
            if (filename == null)
            {
                filename = FileTransaction.GetTempFileName();
            }

            Log.DebugFormat("Downloading {0} to {1}", url, filename);

            var agent = MakeDefaultHttpClient();

            additionalHeaders?.ToList().ForEach(p => agent.Headers.Add(p.Key, p.Value));

            try
            {
                agent.DownloadFile(url, filename);
            }
            catch (Exception ex)
            {
                Log.Info("Download failed, trying with curlsharp...", ex);

                try
                {
                    Curl.Init();

                    using (FileStream stream = File.OpenWrite(filename))
                        using (var curl = Curl.CreateEasy(url, stream))
                        {
                            var headers = new CurlSlist();
                            additionalHeaders?.ToList().ForEach(p => headers.Append($"{p.Key}: {p.Value}"));
                            curl.HttpHeader = headers;
                            curl.SetOpt(CurlOption.Timeout, TIMEOUT_SECONDS);

                            CurlCode result = curl.Perform();
                            if (result != CurlCode.Ok)
                            {
                                throw new Kraken("curl download of " + url + " failed with CurlCode " + result);
                            }
                            else
                            {
                                Log.Debug("curlsharp download successful");
                            }
                        }

                    Curl.CleanUp();
                    return(filename);
                }
                catch
                {
                    // D'oh, failed again. Fall through to clean-up handling.
                }

                // Clean up our file, it's unlikely to be complete.
                // We do this even though we're using transactional files, as we may not be in a transaction.
                // It's okay if this fails.
                try
                {
                    Log.DebugFormat("Removing {0} after web error failure", filename);
                    FileTransaction.Delete(filename);
                }
                catch
                {
                    // Apparently we need a catch, even if we do nothing.
                }

                // Look for an exception regarding the authentication.
                if (Regex.IsMatch(ex.ToString(), "The authentication or decryption has failed."))
                {
                    throw new MissingCertificateKraken("Failed downloading " + url, ex);
                }

                // Not the exception we were looking for! Throw it further upwards!
                throw;
            }

            return(filename);
        }