这个HTTP缓存方案可能出现什么问题?

时间:2017-08-24 14:46:34

标签: c# .net http caching

我知道滚动你自己的HTTP缓存可能是不明智的,但这是在项目中使用的。它似乎缓存了一个奇怪的响应,见下文,因为此请求是针对图像的。它似乎是一个非错误响应(否则它不会被缓存),但它不是图像,它是一个HTML页面。除了200之外,哪些响应代码可以被解释为单位成功?

IEnumerator ReallyDownloadFile(string url, string filename, System.Action<string> on_complete,System.Action<string> on_error, bool allow_caching)
{
    WWW www = new WWW (url);

    yield return www;

    if ((www.error != "")&&(www.error != null)) {
        on_error.Invoke (www.error);
    } else {
        System.IO.File.WriteAllBytes (Application.persistentDataPath + "/filecache/" + filename, www.bytes);
        on_complete.Invoke (Application.persistentDataPath + "/filecache/" + filename);
    }
}

这是回应。我不知道这是从哪里来的。路由器? CDN可能。你能解释一下发生了什么吗?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
<meta name="csrf_token" content="2qe6UtbjdrA6e4ol/eNeo5988K4uoH7r"/>
<meta name="csrf_token" content="UHQ2/wbKRgNdaE14EMIHbKUsa2bLJurm"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv='Pragma' content='no-cache'/>

        <script type="text/javascript">
            /*
             JQuery is not compatible with PSP & NDSi
             script execution will stop when the jquery import.
             we should put the following script before the jquery is imported
             */
            var hardwarePlatform = navigator.platform.toLowerCase();
            var agent = navigator.userAgent.toLowerCase();
            var isPsp = (agent.indexOf("playstation") != -1);
            var isNdsi = (agent.indexOf("nintendo dsi") != -1);
            if (isPsp || isNdsi) {
                window.location.href = "notsupported.html";
            }
        </script>

        <script type="text/javascript" src="../lib/jquery-1.7.2.min.js"></script>
        <script type="text/javascript" src="../lib/log4javascript_lite.js"></script>
        <script type="text/javascript" src="../js/redirect.js"></script>

        <title></title>

        <script type="text/javascript">
            var DEFAULT_GATEWAY_IP = "192.168.1.1";
            var DEFAULT_GATEWAY_DOMAIN = new Array();
            var GATEWAY_DOMAIN = new Array();
            var AJAX_HEADER = '../';
            var AJAX_TAIL = '';
            var AJAX_TIMEOUT = 30000;

            var MACRO_NO_SIM_CARD = '255';
            var MACRO_CPIN_FAIL = '256';
            var MACRO_PIN_READY = '257';
            var MACRO_PIN_DISABLE = '258';
            var MACRO_PIN_VALIDATE = '259';
            var MACRO_PIN_REQUIRED = '260';
            var MACRO_PUK_REQUIRED = '261';

            var log = log4javascript.getNullLogger();            
            var hardwarePlatform = navigator.platform.toLowerCase();
            var agent = navigator.userAgent.toLowerCase();

            var isIpod = hardwarePlatform.indexOf("ipod") != -1;
            var isIphone = hardwarePlatform.indexOf("iphone") != -1;
            var isIpad =  hardwarePlatform.indexOf("ipad") != -1;
            var isAndroid = agent.indexOf("android") !=-1;

            log.debug("INDEX : hardwarePlatform = " + hardwarePlatform);
            log.debug("INDEX : agent = " + agent);
            function gotoPageWithoutHistory(url) {
                log.debug('MAIN : gotoPageWithoutHistory(' + url + ')');
                window.location.replace(url);
            }

            // internal use only
            function _recursiveXml2Object($xml) {
                if ($xml.children().size() > 0) {
                    var _obj = {};
                    $xml.children().each(function() {
                        var _childObj = ($(this).children().size() > 0) ? _recursiveXml2Object($(this)) : $(this).text();
                        if ($(this).siblings().size() > 0 && $(this).siblings().get(0).tagName == this.tagName) {
                            if (_obj[this.tagName] == null) {
                                _obj[this.tagName] = [];
                            }
                            _obj[this.tagName].push(_childObj);
                        }
                        else {
                            _obj[this.tagName] = _childObj;
                        }
                    });
                    return _obj;
                }
                else {
                    return $xml.text();
                }
            }

            // convert XML string to an Object.
            // $xml, which is an jQuery xml object.
            function xml2object($xml) {
                var obj = new Object();
                if ($xml.find('response').size() > 0) {
                    var _response = _recursiveXml2Object($xml.find('response'));
                    obj.type = 'response';
                    obj.response = _response;
                }
                else if ($xml.find('error').size() > 0) {
                    var _code = $xml.find('code').text();
                    var _message = $xml.find('message').text();
                    log.warn('MAIN : error code = ' + _code);
                    log.warn('MAIN : error msg = ' + _message);
                    obj.type = 'error';
                    obj.error = {
                        code: _code,
                        message: _message
                    };
                }
                else if ($xml.find('config').size() > 0) {
                    var _config = _recursiveXml2Object($xml.find('config'));
                    obj.type = 'config';
                    obj.config = _config;
                }
                else {
                    obj.type = 'unknown';
                }
                return obj;
            }

            function getAjaxData(urlstr, callback_func, options) {
                var myurl = AJAX_HEADER + urlstr + AJAX_TAIL;
                var isAsync = true;
                var nTimeout = AJAX_TIMEOUT;
                var errorCallback = null;

                if (options) {
                    if (options.sync) {
                        isAsync = (options.sync == true) ? false : true;
                    }
                    if (options.timeout) {
                        nTimeout = parseInt(options.timeout, 10);
                        if (isNaN(nTimeout)) {
                            nTimeout = AJAX_TIMEOUT;
                        }

                    }
                    errorCallback = options.errorCB;
                }
                var headers = {};
                headers['__RequestVerificationToken'] = g_requestVerificationToken;

                $.ajax({
                    async: isAsync,
                    headers: headers,
                    //cache: false,
                    type: 'GET',
                    timeout: nTimeout,
                    url: myurl,
                    //dataType: ($.browser.msie) ? "text" : "xml",
                    error: function(XMLHttpRequest, textStatus) {
                        try {
                            if (jQuery.isFunction(errorCallback)) {
                                errorCallback(XMLHttpRequest, textStatus);
                            }
                            log.error('MAIN : getAjaxData(' + myurl + ') error.');
                            log.error('MAIN : XMLHttpRequest.readyState = ' + XMLHttpRequest.readyState);
                            log.error('MAIN : XMLHttpRequest.status = ' + XMLHttpRequest.status);
                            log.error('MAIN : textStatus ' + textStatus);
                        }
                        catch (exception) {
                            log.error(exception);
                        }
                    },
                    success: function(data) {
                        log.debug('MAIN : getAjaxData(' + myurl + ') sucess.');
                        log.trace(data);
                        var xml;
                        if (typeof data == 'string' || typeof data == 'number') {
                            if (-1 != this.url.indexOf('/api/sdcard/sdcard')) {
                                data = sdResolveCannotParseChar(data);
                            }
                            if (!window.ActiveXObject) {
                                var parser = new DOMParser();
                                xml = parser.parseFromString(data, 'text/xml');
                            }
                            else {
                                //IE
                                xml = new ActiveXObject('Microsoft.XMLDOM');
                                xml.async = false;
                                xml.loadXML(data);
                            }
                        }
                        else {
                            xml = data;
                        }
                        if (typeof callback_func == 'function') {
                            callback_func($(xml));
                        }
                        else {
                            log.error('callback_func is undefined or not a function');
                        }
                    }
                });
            }

            function getConfigData(urlstr, callback_func, options) {
                var myurl = '../' + urlstr + '';
                //var myurl = urlstr + "";
                var isAsync = true;
                var nTimeout = AJAX_TIMEOUT;
                var errorCallback = null;

                if (options) {
                    if (options.sync) {
                        isAsync = (options.sync == true) ? false : true;
                    }
                    if (options.timeout) {
                        nTimeout = parseInt(options.timeout, 10);
                        if (isNaN(nTimeout)) {
                            nTimeout = AJAX_TIMEOUT;
                        }
                    }
                    errorCallback = options.errorCB;
                }

                $.ajax({
                    async: isAsync,
                    //cache: false,
                    type: 'GET',
                    timeout: nTimeout,
                    url: myurl,
                    //dataType: ($.browser.msie) ? "text" : "xml",
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        try {
                            log.debug('MAIN : getConfigData(' + myurl + ') error.');
                            log.error('MAIN : XMLHttpRequest.readyState = ' + XMLHttpRequest.readyState);
                            log.error('MAIN : XMLHttpRequest.status = ' + XMLHttpRequest.status);
                            log.error('MAIN : textStatus ' + textStatus);
                            if (jQuery.isFunction(errorCallback)) {
                                errorCallback(XMLHttpRequest, textStatus);
                            }
                        }
                        catch (exception) {
                            log.error(exception);
                        }
                    },
                    success: function(data) {
                        log.debug('MAIN : getConfigData(' + myurl + ') success.');
                        log.trace(data);
                        var xml;
                        if (typeof data == 'string' || typeof data == 'number') {
                            if (!window.ActiveXObject) {
                                var parser = new DOMParser();
                                xml = parser.parseFromString(data, 'text/xml');
                            }
                            else {
                                //IE
                                xml = new ActiveXObject('Microsoft.XMLDOM');
                                xml.async = false;
                                xml.loadXML(data);
                            }
                        }
                        else {
                            xml = data;
                        }
                        if (typeof callback_func == 'function') {
                            callback_func($(xml));
                        }
                        else {
                            log.error('callback_func is undefined or not a function');
                        }
                    }
                });
            }

            function getDomain(){
                getConfigData("config/lan/config.xml", function($xml){
                    var ret = xml2object($xml);
                    if(ret.type == "config")
                    {
                        DEFAULT_GATEWAY_DOMAIN.push(ret.config.landns.hgwurl.toLowerCase());
                        if( typeof(ret.config.landns.mcdomain) != 'undefined' )
                        {
                            GATEWAY_DOMAIN.push(ret.config.landns.mcdomain.toLowerCase());
                        }
                    }
                }, {
                   sync: true
                });
            }

            function getQueryStringByName(item) {
                var svalue = location.search.match(new RegExp('[\?\&]' + item + '=([^\&]*)(\&?)', 'i'));
                return svalue ? svalue[1] : svalue;
            }

            function isHandheldBrowser() {
                var bRet = false;
                if(0 == login_status){
                    return bRet;
                }               
                if (isIphone || isIpod) {
                    log.debug("INDEX : current browser is iphone or ipod.");
                    bRet = true;
                }
                else if (isPsp) {
                    log.debug("INDEX : current browser is psp.");
                    bRet = true;
                }
                else if (isIpad) {
                    log.debug("INDEX : current browser is ipad.");
                    bRet = true;
                }
                else if (isAndroid) {
                    log.debug("INDEX : current browser is android.");
                    bRet = true;
                }
                else {
                    log.debug("INDEX : screen.height = " + screen.height);
                    log.debug("INDEX : screen.width = " + screen.width);
                    if (screen.height <= 320 || screen.width <= 320) {
                        bRet = true;
                        log.debug("INDEX : current browser screen size is small.");
                    }
                }
                log.debug("INDEX : isHandheldBrowser = " + bRet);
                return bRet;
            }

            function update_openNewWindow () {
                if (window.location.href.indexOf('?updataredirect=') > -1) {
                    var tmpUrl = window.location.href.substring(window.location.href.indexOf("?updataredirect="));
                    var newUrl = "http://" + tmpUrl.substring(tmpUrl.indexOf("?updataredirect=") + 16) + '/';

                    document.getElementById("update_newPage").setAttribute("href", newUrl);
                    document.getElementById("update_newPage").setAttribute("target", "_blank");
                    if($.browser.msie) {
                        $("#update_newPage").get(0).click();
                    } else {
                        var evt = document.createEvent("MouseEvents");  
                        evt.initEvent("click", true, true);  
                        document.getElementById('update_newPage').dispatchEvent(evt);
                    }
                 }
            }

            var g_requestVerificationToken = '';
            function getAjaxToken() {
                 getAjaxData('api/webserver/token', function($xml) {
                    var ret = xml2object($xml);
                     if ('response' == ret.type) {
                         g_requestVerificationToken = ret.response.token;

                     }
                }, {
                       sync: true
                });
            }

            getAjaxToken();

        var WangatewayAddr="";
            var gatewayAddr = "";
            var conntection_status = null;
            var service_status = null;
            var login_status = null;
            // get current settings gateway address
            getAjaxData("api/dhcp/settings", function($xml) {
                var ret = xml2object($xml);
                if ("response" == ret.type) {
                    gatewayAddr = ret.response.DhcpIPAddress;
                }
            }, {
                sync : true
            }
            );

            // get connection status
            getAjaxData("api/monitoring/status", function($xml) {
                var ret = xml2object($xml);
                if ("response" == ret.type) {
                    conntection_status = parseInt(ret.response.ConnectionStatus,10);
                    service_status = parseInt(ret.response.ServiceStatus,10);
            WangatewayAddr = ret.response.WanIPAddress;
                }
            }, {
                sync : true
            }
            );
                        // get connection status
            getAjaxData('config/global/config.xml', function($xml) {
            var config_ret = xml2object($xml);  
            login_status = config_ret.config.login;

            }, {
                sync : true
            }
            );
            if ("" == gatewayAddr) {
                gatewayAddr = DEFAULT_GATEWAY_IP;
            }

            var href = "http://" + DEFAULT_GATEWAY_IP;
            try {
                href = window.location.href;
            }
            catch(exception) {
                href = "http://" + DEFAULT_GATEWAY_IP;
            }
            // get incoming url from querystring
            var incoming_url = href.substring(href.indexOf("?url=") + 5);
            // truncate http://
            if (incoming_url.indexOf("//") > -1) {
                incoming_url = incoming_url.substring(incoming_url.indexOf("//") + 2);
            }
            //get *.html
            var incoming_html = "";
            if (incoming_url.indexOf(".html") > -1) {
                incoming_html = incoming_url.substring(incoming_url.lastIndexOf("/") + 1, incoming_url.length);
            }
            // truncate tail
            if (incoming_url.indexOf("/") !=  -1) {
                incoming_url = incoming_url.substring(0, incoming_url.indexOf("/"));
            }

            incoming_url = incoming_url.toLowerCase();
            var bIsSmallPage = isHandheldBrowser();
            // var prefix = "http://" + gatewayAddr;
            var g_indexIncomingUrlIsGateway = false;
            // if incoming url == 192.168.1.1 or MobileWifi.home then goto login
            // page
            window.name = getQueryStringByName("version");
            //check login status
            var LOGIN_STATES_SUCCEED = "0";
            var userLoginState = LOGIN_STATES_SUCCEED;
            getAjaxData('api/user/state-login', function($xml) {
                var ret = xml2object($xml);
                if (ret.type == 'response') {
                    userLoginState=ret.response.State;
                }
            }, {
                sync: true
            });

            $(document).ready(function() {

                update_openNewWindow();

                if(true == bIsSmallPage) {
                    if (userLoginState != LOGIN_STATES_SUCCEED) {
                        getAjaxData('config/global/config.xml', function($xml) {
                            var config_ret = xml2object($xml);
                            if(config_ret.type == 'config') {
                                if(config_ret.config.commend_enable == '1') {
                                    gotoPageWithoutHistory("../html/commend.html");
                                    g_indexIncomingUrlIsGateway = true;
                                }else {                                 
                                    g_indexIncomingUrlIsGateway = redirectOnCondition("",'index');
                                }
                            }
                        },{
                            sync: true
                        });

                    } else {                    
                        g_indexIncomingUrlIsGateway = redirectOnCondition("",'index');                   
                    }
                } else {
                    g_indexIncomingUrlIsGateway = redirectOnCondition("",'index');
                }

                $( function() {
                    getDomain();
                    if (g_indexIncomingUrlIsGateway) {
                        return;
                    }
                    else if (conntection_status == 901 && service_status == 2) {
                        if ((incoming_url.indexOf(gatewayAddr)==0)
                || (incoming_url.indexOf(DEFAULT_GATEWAY_DOMAIN)==0)
                || (incoming_url.indexOf(WangatewayAddr)==0)
                || (incoming_url.indexOf(GATEWAY_DOMAIN)==0)){
                               gotoPageWithoutHistory("home.html");
                         }
                         else {
                              gotoPageWithoutHistory("update.html");
                         }
                    }
                    else {
                        gotoPageWithoutHistory("home.html");
                    }
                });

             });
        </script>
    </head>

    <body style="background-color: #FFFFFF;">
        <div>
            <a id="update_newPage" href="#" target="_blank"></a>
        </div>
        <noscript>
            Sorry, your browser does not support javascript.
        </noscript>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

这看起来像路由器的DNS拦截/尝试重定向。看起来像是返回200的内容不是所请求的内容的情况。现在的问题是“如何发现这种情况?”

一种方法是使用http://clients1.google.com/generate_204来判断您是否在强制门户网站后面:因为在这种情况下路由器不太可能使用204。然而,对于这个用例来说,这似乎是一种有缺陷的方法。

也许这个问题就是一个很好的例子,说明为什么不应该推出自己的缓存?

无论如何这是必要的吗?我开始认为Unity中的HTTP缓存可能没有这个......