Exemple #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);
        }
Exemple #2
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);
        }
Exemple #3
0
        public async Task <__CacheBlock> get(string key)
        {
            if (folder != null)
            {
                String key_md5 = Util.md5(key);
                String path    = key_md5.Substring(0, 2);

                try {
                    StorageFolder dir2 = null;

                    await semaphore.WaitAsync();

                    try {
                        dir2 = await folder.CreateFolderAsync(path, CreationCollisionOption.OpenIfExists);
                    }
                    finally {
                        semaphore.Release();
                    }

                    if (dir2 == null)
                    {
                        return(null);
                    }

                    var item = await dir2.TryGetItemAsync(key_md5);

                    if (item != null)
                    {
                        var file = item as StorageFile;
                        if (file != null)
                        {
                            __CacheBlock block = new __CacheBlock();
                            block.time  = file.DateCreated.DateTime;
                            block.value = await FileIO.ReadTextAsync(file);

                            return(block);
                        }
                    }
                }
                catch (Exception ex) {
                    Debug.WriteLine(ex.Message, "ERROR " + "__FileCache.get");
                }
            }

            return(null);
        }
Exemple #4
0
        public async Task<__CacheBlock> get(string key) {
            if (folder != null) {
                String key_md5 = Util.md5(key);
                String path = key_md5.Substring(0, 2);

                try {
                    StorageFolder dir2 = null;

                    await semaphore.WaitAsync();
                    try {
                        dir2 = await folder.CreateFolderAsync(path, CreationCollisionOption.OpenIfExists);
                    }
                    finally {
                        semaphore.Release();
                    }

                    if (dir2 == null)
                        return null;

                    var item = await dir2.TryGetItemAsync(key_md5);
                    if (item != null) {
                        var file = item as StorageFile;
                        if (file != null) {
                            __CacheBlock block = new __CacheBlock();
                            block.time = file.DateCreated.DateTime;
                            block.value = await FileIO.ReadTextAsync(file);

                            return block;
                        }
                    }
                }
                catch (Exception ex) {
                    Debug.WriteLine(ex.Message, "ERROR " + "__FileCache.get");
                }
            }

            return null;
        }
Exemple #5
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);
            }
        }
Exemple #6
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);
        }