예제 #1
0
        internal static string CreateOutputCachedItemKey(
            string path,
            CacheRequestMethod method,
            HttpContext context,
            CachedVary cachedVary)
        {
            StringBuilder sb;
            int           i, j, n;
            string        name, value;

            string[]            a;
            byte[]              buf, hash;
            HttpRequest         request;
            NameValueCollection col;
            int contentLength;

            if (method == CacheRequestMethod.Post)
            {
                sb = new StringBuilder("System.Web.Http.HttpRawResponse\nM=3\n");
            }
            else
            {
                sb = new StringBuilder("System.Web.Http.HttpRawResponse\nM=2\n");
            }

            sb.Append(CultureInfo.InvariantCulture.TextInfo.ToLower(path));

            /* key for cached vary item has additional information */
            if (cachedVary != null)
            {
                request = context.Request;

                /* params part */
                for (j = 0; j <= 2; j++)
                {
                    switch (j)
                    {
                    case 0:
                        sb.Append("\nVH");
                        a   = cachedVary._headers;
                        col = request.ServerVariables;
                        break;

                    case 1:
                        sb.Append("\nVPQ");
                        a   = cachedVary._params;
                        col = request.QueryString;
                        break;

                    case 2:
                    default:
                        sb.Append("\nVPF");
                        a   = cachedVary._params;
                        col = request.Form;
                        if (method != CacheRequestMethod.Post)
                        {
                            col = null;
                        }

                        break;
                    }

                    if (col == null)
                    {
                        continue;
                    }

                    /* handle all params case (VaryByParams[*] = true) */
                    if (a == null && cachedVary._varyByAllParams && j != 0)
                    {
                        a = col.AllKeys;
                        for (i = a.Length - 1; i >= 0; i--)
                        {
                            if (a[i] != null)
                            {
                                a[i] = CultureInfo.InvariantCulture.TextInfo.ToLower(a[i]);
                            }
                        }

                        Array.Sort(a, InvariantComparer.Default);
                    }

                    if (a != null)
                    {
                        for (i = 0, n = a.Length; i < n; i++)
                        {
                            name  = a[i];
                            value = col[name];
                            if (value == null)
                            {
                                value = NULL_VARYBY_VALUE;
                            }

                            sb.Append("\tPN:");
                            sb.Append(name);
                            sb.Append("\tPV:");
                            sb.Append(value);
                        }
                    }
                }

                /* custom string part */
                sb.Append("\nVC");
                if (cachedVary._varyByCustom != null)
                {
                    sb.Append("\tCN:");
                    sb.Append(cachedVary._varyByCustom);
                    sb.Append("\tCV:");

                    try {
                        value = context.ApplicationInstance.GetVaryByCustomString(
                            context, cachedVary._varyByCustom);
                        if (value == null)
                        {
                            value = NULL_VARYBY_VALUE;
                        }
                    }
                    catch (Exception e) {
                        value = ERROR_VARYBY_VALUE;
                        HttpApplicationFactory.RaiseError(e);
                    }

                    sb.Append(value);
                }

                /*
                 * if VaryByParms=*, and method is not a form, then
                 * use a cryptographically strong hash of the data as
                 * part of the key.
                 */
                sb.Append("\nVPD");
                if (method == CacheRequestMethod.Post &&
                    cachedVary._varyByAllParams &&
                    request.Form.Count == 0)
                {
                    contentLength = request.ContentLength;
                    if (contentLength > MAX_POST_KEY_LENGTH || contentLength < 0)
                    {
                        return(null);
                    }

                    if (contentLength > 0)
                    {
                        buf = ((HttpInputStream)request.InputStream).Data;
                        if (buf == null)
                        {
                            return(null);
                        }

                        hash  = MachineKey.HashData(buf, s_hashModifier, 0, buf.Length);
                        value = Convert.ToBase64String(hash);
                        sb.Append(value);
                    }
                }

                sb.Append("\nEOV");
            }

            return(sb.ToString());
        }