Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ParsedMediaTypeHeaderValue.cs « TestCommon « Microsoft « Microsoft.TestCommon « test - github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95fd2a697a3b208d12bb1540553f61f8c312820c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Net.Http.Headers;

namespace Microsoft.TestCommon
{
    internal class ParsedMediaTypeHeaderValue
    {
        private const string MediaRangeAsterisk = "*";
        private const char MediaTypeSubTypeDelimiter = '/';
        private const string QualityFactorParameterName = "q";
        private const double DefaultQualityFactor = 1.0;

        private MediaTypeHeaderValue mediaType;
        private string type;
        private string subType;
        private bool? hasNonQualityFactorParameter;
        private double? qualityFactor;

        public ParsedMediaTypeHeaderValue(MediaTypeHeaderValue mediaType)
        {
            this.mediaType = mediaType;
            string[] splitMediaType = mediaType.MediaType.Split(MediaTypeSubTypeDelimiter);
            this.type = splitMediaType[0];
            this.subType = splitMediaType[1];
        }

        public string Type
        {
            get
            {
                return this.type;
            }
        }

        public string SubType
        {
            get
            {
                return this.subType;
            }
        }

        public bool IsAllMediaRange
        {
            get
            {
                return this.IsSubTypeMediaRange && String.Equals(MediaRangeAsterisk, this.Type, StringComparison.Ordinal);
            }
        }

        public bool IsSubTypeMediaRange
        {
            get
            {
                return String.Equals(MediaRangeAsterisk, this.SubType, StringComparison.Ordinal);
            }
        }

        public bool HasNonQualityFactorParameter
        {
            get
            {
                if (!this.hasNonQualityFactorParameter.HasValue)
                {
                    this.hasNonQualityFactorParameter = false;
                    foreach (NameValueHeaderValue param in this.mediaType.Parameters)
                    {
                        if (!String.Equals(QualityFactorParameterName, param.Name, StringComparison.Ordinal))
                        {
                            this.hasNonQualityFactorParameter = true;
                        }
                    }
                }

                return this.hasNonQualityFactorParameter.Value;
            }
        }

        public string CharSet
        {
            get
            {
                return this.mediaType.CharSet;
            }
        }

        public double QualityFactor
        {
            get
            {
                if (!this.qualityFactor.HasValue)
                {
                    MediaTypeWithQualityHeaderValue mediaTypeWithQuality = this.mediaType as MediaTypeWithQualityHeaderValue;
                    if (mediaTypeWithQuality != null)
                    {
                        this.qualityFactor = mediaTypeWithQuality.Quality;
                    }

                    if (!this.qualityFactor.HasValue)
                    {
                        this.qualityFactor = DefaultQualityFactor;
                    }
                }

                return this.qualityFactor.Value;
            }
        }
    }
}