예제 #1
0
        /// <summary>
        /// 从请求中解析出路由值
        /// </summary>
        /// <param name="request">请求数据</param>
        /// <returns>路由值</returns>
        public IReadOnlyDictionary <string, string> GetRouteValues(RouteRequestData request)
        {
            if (Scheme != null && stringComparer.Equals(Scheme, request.Scheme) == false)
            {
                return(null);
            }


            var routeValues = new Dictionary <string, string>(stringComparer);

            if (HostTemplate != null)
            {
                var values = HostTemplate.GetRouteValues(request.Host);
                if (values == null)
                {
                    return(null);
                }

                foreach (var pair in values)
                {
                    routeValues.Add(pair.key, pair.value);
                }
            }


            {
                var values = PathTemplate.GetRouteValues(request.Path);
                if (values == null)
                {
                    return(null);
                }

                foreach (var pair in values)
                {
                    routeValues.Add(pair.Key, pair.Value);
                }
            }


            if (QueryStringTemplate != null)
            {
                var values = QueryStringTemplate.GetRouteValues(request.Path);
                if (values == null)
                {
                    return(null);
                }

                foreach (var pair in values)
                {
                    routeValues.Add(pair.Key, pair.Value);
                }
            }


            return(routeValues);
        }
예제 #2
0
파일: Plugin.cs 프로젝트: Gerz-inc/mminer
        public bool ReadSettings()
        {
            try
            {
                string  json_str = File.ReadAllText(json_file_name);
                JObject response = JObject.Parse(json_str);

                JArray templates = response.SelectToken("templates").Value <JArray>();
                this.templates.Clear();
                foreach (var item in templates)
                {
                    var templ = new HostTemplate();
                    templ.name = item.SelectToken("name").Value <string>();

                    JObject headers = item.SelectToken("req_headers").Value <JObject>();
                    templ.req_headers = new Dictionary <string, string>();
                    foreach (var item_h in headers)
                    {
                        templ.req_headers[item_h.Key] = item_h.Value.Value <string>();
                    }
                    JObject cookies = item.SelectToken("req_cookies").Value <JObject>();
                    templ.req_cookies = new Dictionary <string, string>();
                    foreach (var item_c in cookies)
                    {
                        templ.req_cookies[item_c.Key] = item_c.Value.Value <string>();
                    }

                    templ.diff_row_regex  = item.SelectToken("diff_row_regex").Value <string>();
                    templ.diff_data_regex = item.SelectToken("diff_data_regex").Value <string>();
                    templ.diff_data_i     = item.SelectToken("diff_data_i").Value <int>();

                    this.templates.Add(templ);
                }

                JArray coins = response.SelectToken("coins").Value <JArray>();
                this.coins.Clear();
                foreach (var item in coins)
                {
                    var coin = new CoinSettings();
                    coin.name     = item.SelectToken("name").Value <string>();
                    coin.url      = item.SelectToken("url").Value <string>();
                    coin.template = item.SelectToken("template").Value <string>();

                    coin.diff_min      = item.SelectToken("diff_min").Value <double>();
                    coin.diff_max      = item.SelectToken("diff_max").Value <double>();
                    coin.diff_work_min = item.SelectToken("diff_work_min").Value <double>();

                    this.coins.Add(coin);
                }
            }
            catch (Exception ex) {}

            return(true);
        }
예제 #3
0
파일: Plugin.cs 프로젝트: Gerz-inc/mminer
        public static bool?Request(CoinSettings coin, HostTemplate templ, out string response, bool escaped = false, int timeout = 10000)
        {
            try
            {
                string query = coin.url;
                if (!escaped)
                {
                    query = Uri.EscapeUriString(coin.url);
                }
                Trace.WriteLine("Request: " + query);

                // Send request
                Uri            url = new Uri(query);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

                foreach (var item_h in templ.req_headers)
                {
                    switch (item_h.Key)
                    {
                    case "User-Agent": req.UserAgent = item_h.Value; break;
                    }
                }

                if (req.CookieContainer == null)
                {
                    req.CookieContainer = new CookieContainer();
                }
                foreach (var item_c in templ.req_cookies)
                {
                    req.CookieContainer.Add(new Cookie(item_c.Key, item_c.Value, "/", url.Host));
                }

                req.Timeout = timeout;
                WebResponse  resp   = req.GetResponse();
                Stream       stream = resp.GetResponseStream();
                StreamReader sr     = new StreamReader(stream);
                response = sr.ReadToEnd();

                return(response != null && response.Length > 0);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Request error: " + coin.url + "\n" + ex.Message);

                response = null;
                return(null);
            }
        }
예제 #4
0
        public LaunchHostRequest(Cluster cluster, ILocation location, HostTemplate template, int launchCount = 1)
        {
            #region Preconditions

            if (launchCount <= 0)
            {
                throw new ArgumentException("Must be > 0", nameof(launchCount));
            }

            #endregion

            Cluster     = cluster ?? throw new ArgumentNullException(nameof(cluster));
            Location    = location ?? throw new ArgumentNullException(nameof(location));
            Template    = template ?? throw new ArgumentNullException(nameof(template));
            LaunchCount = launchCount;
        }
예제 #5
0
        public HttpRequestMessage RewriteRequest(HttpRequestMessage request, IReadOnlyDictionary <string, string> routeValues)
        {
            var builder = new UriBuilder(request.RequestUri);

            builder.Host = HostTemplate.Rewrite(routeValues);
            builder.Path = PathTemplate.Rewrite(routeValues);
            if (Port == null)
            {
                builder.Port = -1;
            }

            else if (Port != "?")
            {
                builder.Port = int.Parse(Port);
            }


            request.RequestUri = builder.Uri;

            return(request);
        }