示例#1
0
        private void UpdateFromDependencies(HttpResponse response)
        {
            CacheDependency dep = null;

            if ((this._etag == null) && this._generateEtagFromFiles)
            {
                dep = response.CreateCacheDependencyForResponse();
                if (dep == null)
                {
                    return;
                }
                string uniqueID = dep.GetUniqueID();
                if (uniqueID == null)
                {
                    throw new HttpException(System.Web.SR.GetString("No_UniqueId_Cache_Dependency"));
                }
                DateTime      time    = this.UpdateLastModifiedTimeFromDependency(dep);
                StringBuilder builder = new StringBuilder(0x100);
                builder.Append(HttpRuntime.AppDomainIdInternal);
                builder.Append(uniqueID);
                builder.Append("+LM");
                builder.Append(time.Ticks.ToString(CultureInfo.InvariantCulture));
                this._etag = MachineKeySection.HashAndBase64EncodeString(builder.ToString());
                this._etag = "\"" + this._etag + "\"";
            }
            if (this._generateLastModifiedFromFiles)
            {
                if (dep == null)
                {
                    dep = response.CreateCacheDependencyForResponse();
                    if (dep == null)
                    {
                        return;
                    }
                }
                DateTime utcDate = this.UpdateLastModifiedTimeFromDependency(dep);
                this.UtcSetLastModified(utcDate);
            }
        }
        /*
         * Calculate LastModified and ETag
         * 
         * The LastModified date is the latest last-modified date of 
         * every file that is added as a dependency.
         * 
         * The ETag is generated by concatentating the appdomain id, 
         * filenames and last modified dates of all files into a single string, 
         * then hashing it and Base 64 encoding the hash.
         */
        void UpdateFromDependencies(HttpResponse response) {
            CacheDependency dep = null;
            // if _etag != null && _generateEtagFromFiles == true, then this HttpCachePolicy
            // was created from HttpCachePolicySettings and we don't need to update _etag.
            if (_etag == null && _generateEtagFromFiles) {
                dep = response.CreateCacheDependencyForResponse();
                if (dep == null) {
                    return;
                }
                string id = dep.GetUniqueID();
                if (id == null) {
                    throw new HttpException(SR.GetString(SR.No_UniqueId_Cache_Dependency));
                }
                DateTime utcFileLastModifiedMax = UpdateLastModifiedTimeFromDependency(dep);
                StringBuilder sb = new StringBuilder(256);
                sb.Append(HttpRuntime.AppDomainIdInternal);
                sb.Append(id);
                sb.Append("+LM");
                sb.Append(utcFileLastModifiedMax.Ticks.ToString(CultureInfo.InvariantCulture));
                _etag = Convert.ToBase64String(CryptoUtil.ComputeSHA256Hash(Encoding.UTF8.GetBytes(sb.ToString())));

                //WOS 1540412: if we generate the etag based on file dependencies, encapsulate it within quotes.
                _etag = "\"" + _etag + "\"";
            }

            if (_generateLastModifiedFromFiles) {
                if (dep == null) {
                    dep = response.CreateCacheDependencyForResponse();
                    if (dep == null) {
                        return;
                    }
                }
                DateTime utcFileLastModifiedMax = UpdateLastModifiedTimeFromDependency(dep);
                UtcSetLastModified(utcFileLastModifiedMax);                
            }
        }