Exemplo n.º 1
0
 private static void setRequestParam(HttpRequest request, string param)
 {
     if (!string.IsNullOrEmpty(param))
     {
         string[] paramNameValue = param.Split(':');
         string   value          = string.Empty;
         if (paramNameValue[0] == HttpHeader.Cookie)
         {
             request.Cookies = ParseCookie(paramNameValue[1]);
         }
         else if (paramNameValue[0] == HttpHeader.ContentLength)
         {
             request.Parameters.Add(paramNameValue[0], paramNameValue[1]);
             request.ContentLength = TryParse.Int32(paramNameValue[1]);
         }
         else if (paramNameValue[0] == HttpHeader.Connection)
         {
             request.Parameters.Add(paramNameValue[0], paramNameValue[1]);
             request.ShouldKeepAlive = paramNameValue[1] == HttpHeader.ConnectionKeepAlive;
         }
         else
         {
             for (int i = 1, icount = paramNameValue.Length; i < icount; i++)
             {
                 value = string.Concat(value, i > 1 ? ":" : string.Empty, paramNameValue[i].Trim());
             }
             request.Parameters.Add(paramNameValue[0], value);
         }
     }
 }
Exemplo n.º 2
0
        static void GoogleSearch()
        {
            var q =
                from sp in Http.Get(new Uri("https://google.com/"))
                .Submit(0, new NameValueCollection {
                ["q"] = "foobar"
            })
                .Html().Content()
                from sr in
                Query.Generate(sp, curr =>
            {
                var next = curr.TryBaseHref(curr.QuerySelectorAll("#foot a.fl")
                                            .Last()             // Next
                                            .GetAttributeValue("href"));
                return(Http.Get(new Uri(next)).Html().Content());
            })
                .TakeWhile(h => (TryParse.Int32(HttpUtility.ParseQueryString(h.BaseUrl.Query)["start"]) ?? 1) < 30)
                from r in sr.QuerySelectorAll(".g")
                select new
            {
                Title   = r.QuerySelector(".r")?.InnerText,
                Summary = r.QuerySelector(".st")?.InnerText,
                Href    = sr.TryBaseHref(r.QuerySelector(".r a")?.GetAttributeValue("href")),
            }
            into e
            where !string.IsNullOrWhiteSpace(e.Title) &&
            e.Href != null &&
            !string.IsNullOrWhiteSpace(e.Summary)
            select e;

            q.Dump();
        }
Exemplo n.º 3
0
        static IEnumerable <T> TableRowsCore <T>(HtmlObject table, Func <HtmlObject, IEnumerable <HtmlObject>, T> rowSelector)
        {
            // TODO https://www.w3.org/TR/html5/tabular-data.html#processing-model-1

            var spans = new ArrayList <CellSpan>();

            foreach (var e in
                     from g in table.ChildElements
                     where g.IsNamed("tbody") || g.IsNamed("thead") || g.IsNamed("tfoot") || g.IsNamed("tr")
                     from tr in g.IsNamed("tr")
                         ? new[] { g } :
                     g.ChildElements.Where(e => e.IsNamed("tr"))
                     select new
            {
                Row = tr,
                Cells = tr.ChildElements.Where(e => e.IsNamed("td") || e.IsNamed("th")).ToArray(),
            })
            {
                var tds = e.Cells;
                spans.EnsureCapacity(tds.Length);

                var cells = new ArrayList <HtmlObject>();
                cells.EnsureCapacity(tds.Length);

                var i = 0;
                foreach (var td in tds)
                {
                    while (true)
                    {
                        var rspan = spans[i, CellSpan.Zero];
                        if (rspan.Rows == 0)
                        {
                            break;
                        }
                        i += rspan.Cols;
                    }

                    var colspan = TryParse.Int32(td.GetAttributeValue("colspan"), NumberStyles.Integer & ~NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture) ?? 1;
                    var rowspan = TryParse.Int32(td.GetAttributeValue("rowspan"), NumberStyles.Integer & ~NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture) ?? 1;
                    var span    = colspan > 1 || rowspan > 1 ? new CellSpan(colspan, rowspan) : CellSpan.One;

                    spans[i] = span;
                    cells[i] = td;

                    i += span.Cols;
                }

                for (i = 0; i < spans.Count; i++)
                {
                    var span    = spans[i];
                    var rowspan = span.Rows - 1;
                    spans[i] = rowspan > 0 ? new CellSpan(span.Cols, rowspan) : CellSpan.Zero;
                }

                yield return(rowSelector(e.Row, cells.ToArray()));
            }
        }
Exemplo n.º 4
0
        public static Config FromDictionary(IDictionary dictionary)
        {
            var url  = TryParse.Uri(dictionary.Get(ConfigKeys.Url), UriKind.RelativeOrAbsolute);
            var host = dictionary.Get(ConfigKeys.Host);

            if (url == null && string.IsNullOrEmpty(host))
            {
                throw new ApplicationException("The \"url\" or \"host\" setting is not found in configuration");
            }

            var project = dictionary.Get(ConfigKeys.Project);

            if (string.IsNullOrEmpty(project))
            {
                throw new ApplicationException("The \"project\" setting is not found in configuration");
            }

            if (url != null)
            {
                return(new Config(url.Host, project)
                {
                    Port = url.Port,
                    UseSsl = url.Scheme.Contains("https"),
                    Path = url.AbsolutePath,
                    Username = dictionary.Get(ConfigKeys.Username),
                    Password = dictionary.Get(ConfigKeys.Passwrod),
                    ReportAsynchronously = TryParse.Boolean(dictionary.Get(ConfigKeys.ReportAsynchronously)).GetValueOrDefault(true)
                });
            }
            else
            {
                return(new Config(host, project)
                {
                    Port = TryParse.Int32(dictionary.Get(ConfigKeys.Port)).GetValueOrDefault(80),
                    UseSsl = TryParse.Boolean(dictionary.Get(ConfigKeys.UseSsl)).GetValueOrDefault(),
                    Path = dictionary.Get(ConfigKeys.Path),
                    Username = dictionary.Get(ConfigKeys.Username),
                    Password = dictionary.Get(ConfigKeys.Passwrod),
                    ReportAsynchronously = TryParse.Boolean(dictionary.Get(ConfigKeys.ReportAsynchronously)).GetValueOrDefault(true)
                });
            }
        }
Exemplo n.º 5
0
 public static int GetInt32(this IDictionary <string, string> aValues, string aParam, int aDefault = 0)
 {
     return(TryParse.Int32(aValues.GetString(aParam), aDefault));
 }
Exemplo n.º 6
0
 static void Null()
 {
     Assert.Equal(TryParse.Int32(null), None());
 }
Exemplo n.º 7
0
 static void Fail()
 {
     Assert.Equal(TryParse.Int32("a"), None());
 }
Exemplo n.º 8
0
 static void Success()
 {
     Assert.Equal(TryParse.Int32("1"), Some(1));
 }
Exemplo n.º 9
0
 public static int GetInt32(this System.Xml.XmlReader aReader, string aAttribute, int aDefault = 0)
 {
     return(TryParse.Int32(aReader.GetAttribute(aAttribute), aDefault));
 }