Example #1
0
        private static async void doHttp(SdSource source, String url, Dictionary<String, String> args, SdNode config, __CacheBlock cache, HttpCallback callback) {


            var encoding = Encoding.GetEncoding(config.encode());

            AsyncHttpClient client = new AsyncHttpClient();

            if (config.isInCookie() && string.IsNullOrEmpty(source.cookies()) == false) {
                client.Cookies(source.cookies());
            }

            client.Header("User-Agent", source.ua());
            client.Encoding(config.encode());

            string newUrl = null;
            if (url.IndexOf(" ") >= 0)
                newUrl = Uri.EscapeUriString(url);
            else
                newUrl = url;

            if (config.isInReferer()) {
                client.Header("Referer", source.buildReferer(config, url));
            }

            if (string.IsNullOrEmpty(config.accept) == false) {
                client.Header("Accept", config.accept);
                client.Header("X-Requested-With", "XMLHttpRequest");
            }

            client.Url(newUrl);

            string temp = null;

            try {
                AsyncHttpResponse rsp = null;
                if ("post".Equals(config.method))
                    rsp = await client.Post(args);
                else
                    rsp = await client.Get();

                if (rsp.StatusCode == HttpStatusCode.Ok) {
                    source.setCookies(rsp.Cookies);
                    temp = rsp.GetString();
                }
            }
            catch(Exception ex) {
                Util.log(source, "HTTP", ex.Message);
            }

            if (temp == null) {
                if (cache == null)
                    callback(-2, null);
                else
                    callback(1, cache.value);
            }
            else
                callback(1, temp);
        }
Example #2
0
        public static void log(SdSource source, String tag, String msg, Exception tr)
        {
            Debug.WriteLine(msg, tag);

            if (SdApi._listener != null)
            {
                SdApi._listener.run(source, tag, msg, tr);
            }
        }
Example #3
0
 //调用函数
 public string callJs(SdSource source, string fun, params object[] args)
 {
     try
     {
         return engine.CallFunction(fun, args.ToArray()).ToString();
     }
     catch (Exception ex) {
         Util.log(source, "JsEngine.callJs:" + fun, ex.Message, ex);
         return null;
     }
 }
Example #4
0
        internal SdJscript(SdSource source, XElement node) {
            s = source;

            if (node == null) {
                code = "";
                require = new SdNode(source, null);
            }
            else {
                code = node.Element("code").Value.Trim();
                require = new SdNode(source, node.Element("require"));
            }
        }
Example #5
0
        internal SdJscript(SdSource source, XElement node)
        {
            s = source;

            if (node == null) {
                code = "";
                require = Util.createNode(source).buildForNode(null);
            }
            else {
                code = node.Element("code").Value.Trim();
                require = Util.createNode(source).buildForNode(node.Element("require"));
            }
        }
Example #6
0
        public JsEngine loadJs(SdSource source, string funs)
        {
            try
            {
                engine.RunScript(funs);//预加载了批函数
            }
            catch (Exception ex)
            {
                Util.log(source, "JsEngine.loadJs", ex.Message, ex);
                throw ex;
            }

            return this;
        }
Example #7
0
        public JsEngine(SdSource s)
        {
            source = s;
            engine = new ChakraHost();

            //if (s != null) {
            //    engine.RegisterFunction("print", (args) =>
            //    {
            //        //if (args.Length > 0) {
            //        //    Util.log(source, "JsEngine.print", args[0].ConvertToString().ToString());
            //        //}
            //    });
            //}
        }
Example #8
0
        public JsEngine(SdSource s)
        {
            source = s;
            engine = new ChakraHost();

            //if (s != null) {
            //    engine.RegisterFunction("print", (args) =>
            //    {
            //        //if (args.Length > 0) {
            //        //    Util.log(source, "JsEngine.print", args[0].ConvertToString().ToString());
            //        //}
            //    });
            //}
        }
Example #9
0
        internal SdJscript(SdSource source, XElement node)
        {
            s = source;

            if (node == null)
            {
                code    = "";
                require = Util.createNode(source).buildForNode(null);
            }
            else
            {
                code    = node.Element("code").Value.Trim();
                require = Util.createNode(source).buildForNode(node.Element("require"));
            }
        }
Example #10
0
        public async static void http(SdSource source, bool isUpdate, HttpMessage msg)
        {
            log(source, "Util.http", msg.url);

            String cacheKey2 = null;
            String args      = "";

            if (msg.form == null)
            {
                cacheKey2 = msg.url;
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(msg.url);
                foreach (String key in msg.form.Keys)
                {
                    sb.Append(key).Append("=").Append(msg.form[key]).Append(";");
                }
                cacheKey2 = sb.ToString();
                args      = cacheKey2;
            }
            String cacheKey = cacheKey2;

            __CacheBlock block = await cache.get(cacheKey);

            if (isUpdate == false && msg.config.cache > 0)
            {
                if (block != null && block.isOuttime(msg.config) == false)
                {
                    log(source, "Util.incache.url", msg.url);
                    msg.callback(1, msg, block.value, null);
                    return;
                }
            }

            doHttp(source, msg, block, (code, msg2, data, url302) => {
                if (code == 1)
                {
                    cache.save(cacheKey, data);
                }

                msg.callback(code, msg2, data, url302);
            });

            source.DoTraceUrl(msg.url, args, msg.config);
        }
Example #11
0
        //
        //--------------------------------
        //

        public static void log(SdSource source, SdNode node, String url, String json, int tag)
        {
            log(source, node.name, "tag=" + tag);

            if (url == null)
            {
                log(source, node.name, "url=null");
            }
            else
            {
                log(source, node.name, url);
            }

            if (json == null)
            {
                log(source, node.name, "json=null");
            }
            else
            {
                log(source, node.name, json);
            }
        }
Example #12
0
        public async static void http(SdSource source, bool isUpdate, String url, Dictionary<String, String> args, SdNode config, HttpCallback callback) {
            __CacheBlock block = null;
            if (isUpdate == false && config.cache > 0) {
                block = cache.get(url);
            }

            if (block != null) {
                if (config.cache == 1 || block.seconds() <= config.cache) {
                    await Task.Delay(100);
                    callback(1, block.value);
                    return;
                }
            }

            doHttp(source, url, args, config, block,(code, data) => {
                if (code == 1 && config.cache > 0) {
                    cache.save(url, data);
                }

                callback(code, data);
            });
        }
Example #13
0
File: Util.cs Project: noear/SiteD
        public async static void http(SdSource source, bool isUpdate, HttpMessage msg) {
            log(source, "Util.http", msg.url);

            String cacheKey2 = null;
            String args = "";
            if (msg.form == null)
                cacheKey2 = msg.url;
            else {
                StringBuilder sb = new StringBuilder();
                sb.Append(msg.url);
                foreach (String key in msg.form.Keys) {
                    sb.Append(key).Append("=").Append(msg.form[key]).Append(";");
                }
                cacheKey2 = sb.ToString();
                args = cacheKey2;
            }
             String cacheKey = cacheKey2;

            __CacheBlock block = await cache.get(cacheKey);

            if (isUpdate == false && msg.config.cache > 0) {
                if (block != null && block.isOuttime(msg.config) == false) {
                    log(source, "Util.incache.url", msg.url);
                    msg.callback(1, msg, block.value, null);
                    return;
                }
            }

            doHttp(source, msg, block, (code, msg2, data, url302) => {
                if (code == 1) {
                    cache.save(cacheKey, data);
                }

                msg.callback(code, msg2, data, url302);
            });

            source.DoTraceUrl(msg.url, args, msg.config);
        }
Example #14
0
 public virtual SdNodeSet createNodeSet(SdSource source)
 {
     return(new SdNodeSet(source));
 }
Example #15
0
        public static void log(SdSource source, String tag, String msg, Exception tr)
        {
            Debug.WriteLine(msg, tag);

            if (SdSource.logListener != null)
            {
                SdSource.logListener(source, tag, msg, tr);
            }
        }
Example #16
0
        private static void log(SdSource source, String tag, String msg)
        {
            Debug.WriteLine(msg, tag);

            if (SdSource.logListener != null)
            {
                SdSource.logListener(source, tag, msg, null);
            }
        }
Example #17
0
        //
        //--------------------------------
        //

        public static void log(SdSource source, SdNode node, String url, String json)
        {
            if (url == null)
                log(source, node.name, "url=null");
            else
                log(source, node.name, url);

            if (json == null)
                log(source, node.name, "json=null");
            else
                log(source, node.name, json);
        }
Example #18
0
 public SdNode()
 {
     this.source = null;
 }
Example #19
0
 private SdNode(SdSource source) {
     this.source = source;
 }
Example #20
0
 public static SdNodeSet createNodeSet(SdSource source)
 {
     return(SdApi._factory.createNodeSet(source));
 }
Example #21
0
File: Util.cs Project: noear/SiteD
        private static async void doHttp(SdSource source, HttpMessage msg, __CacheBlock cache, HttpCallback callback) {
            var encoding = Encoding.GetEncoding(msg.config.encode());

            AsyncHttpClient client = new AsyncHttpClient();
            client.UserAgent(msg.config.ua());
            client.Encoding(msg.config.encode());

            foreach (String key in msg.header.Keys) {
                client.Header(key, msg.header[key]);
            }

            string newUrl = null;
            if (msg.url.IndexOf(" ") >= 0)
                newUrl = Uri.EscapeUriString(msg.url);
            else
                newUrl = msg.url;

            client.Url(newUrl);

            string temp = null;

            AsyncHttpResponse rsp = null;
            try {
                if ("post".Equals(msg.config.method))
                    rsp = await client.Post(msg.form);
                else
                    rsp = await client.Get();

                if (rsp.StatusCode == HttpStatusCode.OK) {
                    source.setCookies(rsp.Cookies);
                    temp = rsp.GetString();

                    if (string.IsNullOrEmpty(rsp.location) == false) {
                        Uri uri = new  Uri(msg.url);
                        rsp.location = uri.Scheme + "://" + uri.Host + rsp.location;
                    }
                }
            }
            catch (Exception ex) {
                Util.log(source, "HTTP", ex.Message);
            }

            if (temp == null) {
                if (cache == null || cache.value == null)
                    callback(-2, msg, null, rsp.location);
                else
                    callback(1, msg, cache.value, rsp.location);
            }
            else
                callback(1, msg, temp, rsp.location);
        }
Example #22
0
File: Util.cs Project: noear/SiteD
 public static SdNodeSet createNodeSet(SdSource source) {
     return SdApi._factory.createNodeSet(source);
 }
Example #23
0
File: Util.cs Project: noear/SiteD
        public static void log(SdSource source, String tag, String msg)
        {
            Debug.WriteLine(msg, tag);

            if (SdApi._listener != null)
            {
                SdApi._listener.run(source, tag, msg, null);
            }
        }
Example #24
0
        //---------------

        public SdNodeSet(SdSource source)
        {
            this.source = source;
        }
Example #25
0
 internal SdNodeSet(SdSource source, XElement element) :this(source){
    
     loadByElement(element);
 }
Example #26
0
 public DdNode(SdSource source)
     : base(source)
 {
 }
Example #27
0
        private static async void doHttp(SdSource source, HttpMessage msg, __CacheBlock cache, HttpCallback callback)
        {
            var encoding = Encoding.GetEncoding(msg.config.encode());

            AsyncHttpClient client = new AsyncHttpClient();

            client.UserAgent(msg.config.ua());
            client.Encoding(msg.config.encode());

            foreach (String key in msg.header.Keys)
            {
                client.Header(key, msg.header[key]);
            }

            string newUrl = null;

            if (msg.url.IndexOf(" ") >= 0)
            {
                newUrl = Uri.EscapeUriString(msg.url);
            }
            else
            {
                newUrl = msg.url;
            }

            client.Url(newUrl);

            string temp = null;

            AsyncHttpResponse rsp = null;

            try {
                if ("post".Equals(msg.config.method))
                {
                    rsp = await client.Post(msg.form);
                }
                else
                {
                    rsp = await client.Get();
                }

                if (rsp.StatusCode == HttpStatusCode.OK)
                {
                    source.setCookies(rsp.Cookies);
                    temp = rsp.GetString();

                    if (string.IsNullOrEmpty(rsp.location) == false)
                    {
                        Uri uri = new  Uri(msg.url);
                        rsp.location = uri.Scheme + "://" + uri.Host + rsp.location;
                    }
                }
            }
            catch (Exception ex) {
                Util.log(source, "HTTP", ex.Message);
            }

            if (temp == null)
            {
                if (cache == null || cache.value == null)
                {
                    callback(-2, msg, null, rsp.location);
                }
                else
                {
                    callback(1, msg, cache.value, rsp.location);
                }
            }
            else
            {
                callback(1, msg, temp, rsp.location);
            }
        }
Example #28
0
        public SdNode(SdSource source, XElement cfg) {
            this.source = source;
            _isEmpty = (cfg == null);

            if (cfg != null) {
                this.name = cfg.Name.LocalName;

                this.title = cfg.Attribute("title")?.Value;
                this.method = cfg.Attribute("method")?.Value;
                this.parse = cfg.Attribute("parse")?.Value;
                this.parseUrl = cfg.Attribute("parseUrl")?.Value;
                this.url = cfg.Attribute("url")?.Value;
                this.expr = cfg.Attribute("expr")?.Value;
                this._run = cfg.Attribute("run")?.Value;
                this._encode = cfg.Attribute("encode")?.Value;
                this._ua = cfg.Attribute("ua")?.Value;

                if (string.IsNullOrEmpty(this.method)) {
                    this.method = "get";
                }

                {
                    string temp = cfg.Attribute("cache")?.Value;
                    if (string.IsNullOrEmpty(temp) == false) {
                        int len = temp.Length;
                        if (len == 1) {
                            cache = int.Parse(temp);
                        }
                        else if (len > 1) {
                            cache = int.Parse(temp.Substring(0, len - 1));

                            string p = temp.Substring(len - 1);
                            switch (p) {
                                case "d": cache = cache * 24 * 60 * 60; break;
                                case "h": cache = cache * 60 * 60; break;
                                case "m": cache = cache * 60; break;
                            }
                        }
                    }
                }

                this.accept = cfg.Attribute("accept")?.Value;
                this.header = cfg.Attribute("header")?.Value;
                if (this.header == null)
                    this.header = "";

                string w = cfg.Attribute("w")?.Value;
                if (string.IsNullOrEmpty(w) == false) {
                    string h = cfg.Attribute("h")?.Value;
                    WHp = float.Parse(w) / float.Parse(h);
                }

                this.buildRef = cfg.Attribute("buildRef")?.Value;
                this.buildUrl = cfg.Attribute("buildUrl")?.Value;
                this.buildKey = cfg.Attribute("buildKey")?.Value;
                this.buildWeb = cfg.Attribute("buildWeb")?.Value;

                this.addKey = cfg.Attribute("addKey")?.Value;
                string _addPage = cfg.Attribute("addPage")?.Value;
                if (string.IsNullOrEmpty(_addPage) == false) {
                    this.addPage = int.Parse(_addPage);
                }

                //搜索物有
                this.args = cfg.Attribute("args")?.Value;

                if (cfg.HasElements) {
                    _items = new List<SdNode>();
                    _adds = new List<SdNode>();

                    foreach (XElement e1 in cfg.Elements()) {
                        if (e1.Name.LocalName.Equals("item")) {
                            SdNode temp = new SdNode(source).buildForItem(e1, this);
                            _items.Add(temp);
                        }
                        else {
                            SdNode temp = new SdNode(source).buildForAdd(e1, this);
                            _adds.Add(temp);
                        }
                    }
                }
            }
        }
Example #29
0
 internal SdNodeSet(SdSource source) {
     _items = new Dictionary<string, ISdNode>();
     _source = source;
 }
Example #30
0
 public virtual SdNode createNode(SdSource source)
 {
     return new SdNode(source);
 }
Example #31
0
 public virtual SdNodeSet createNodeSet(SdSource source)
 {
     return new SdNodeSet(source);
 }
Example #32
0
        private string _ua; //http ua

        #endregion Fields

        #region Constructors

        public SdNode(SdSource source)
        {
            this.source = source;
        }
Example #33
0
 public override SdNodeSet createNodeSet(SdSource source)
 {
     return new DdNodeSet(source);
 }
Example #34
0
 public virtual SdNode createNode(SdSource source)
 {
     return(new SdNode(source));
 }