File: Utility.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (213 lines) | stat: -rw-r--r-- 8,045 bytes parent folder | download | duplicates (7)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.ServiceModel.Web
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Net.Mime;
    using System.Runtime;
    using System.Text;
    using System.Globalization;
    using System.ServiceModel.Channels;
    
    static class Utility
    {
        public const string applicationXml = "application/xml";
        public const string textXml = "text/xml";
        public const string applicationJson = "application/json";
        public const string textJson = "text/json";
        public const string GET = "GET";
        

        public static bool IsXmlContent(this string contentType)
        {
            if (contentType == null)
            {
                return true;
            }

            string contentTypeProcessed = contentType.Trim();

            return contentTypeProcessed.StartsWith(applicationXml, StringComparison.OrdinalIgnoreCase)
                || contentTypeProcessed.StartsWith(textXml, StringComparison.OrdinalIgnoreCase);
        }

        public static bool IsJsonContent(this string contentType)
        {
            if (contentType == null)
            {
                return true;
            }

            string contentTypeProcessed = contentType.Trim();

            return contentTypeProcessed.StartsWith(applicationJson, StringComparison.OrdinalIgnoreCase)
                || contentTypeProcessed.StartsWith(textJson, StringComparison.OrdinalIgnoreCase);
        }

        public static string CombineUri(string former, string latter)
        {
            // Appending the latter string to the form string,
            // while making sure there is a single slash char seperating the latter and the former.
            // This method behaves differently than new Uri(baseUri, relativeUri)
            // as CombineUri simply appends, whereas new Uri() actually replaces the last segment
            // of the its base path with the relative uri.

            StringBuilder builder = new StringBuilder();
            if (former.Length > 0 && latter.Length > 0)
            {
                if (former[former.Length - 1] == '/' && latter[0] == '/')
                {
                    builder.Append(former, 0, former.Length - 1);
                    builder.Append(latter);
                    return builder.ToString();
                }

                if (former[former.Length - 1] != '/' && latter[0] != '/')
                {
                    builder.Append(former);
                    builder.Append('/');
                    builder.Append(latter);
                    return builder.ToString();
                }
            }

            return former + latter;
        }
        public static List<string> QuoteAwareStringSplit(string str)
        {
            List<string> subStrings = new List<string>();
            int offset = 0;
            while (true)
            {
                string subString = QuoteAwareSubString(str, ref offset);
                if (subString == null)
                {
                    break;
                }
                subStrings.Add(subString);
            }

            return subStrings;
        }

        // This method extracts substrings from a string starting at the offset
        // and up until the next comma in the string.  The sub string extraction is 
        // quote aware such that commas inside quoted-strings are ignored.  On return, 
        // offset points to the next char beyond the comma of the substring returned 
        // and may point beyond the length of the header.
        public static string QuoteAwareSubString(string str, ref int offset)
        {
            // this method will filter out empty-string and white-space-only items in 
            // the header.  For example "x,,y" and "x, ,y" would result in just "x" and "y"
            // substrings being returned.

            if (string.IsNullOrEmpty(str) || offset >= str.Length)
            {
                return null;
            }

            int startIndex = (offset > 0) ? offset : 0;

            // trim whitespace and commas from the begining of the item
            while (char.IsWhiteSpace(str[startIndex]) || str[startIndex] == ',')
            {
                startIndex++;
                if (startIndex >= str.Length)
                {
                    return null;
                }
            }

            int endIndex = startIndex;
            bool insideQuotes = false;

            while (endIndex < str.Length)
            {
                if (str[endIndex] == '\"' &&
                   (!insideQuotes || endIndex == 0 || str[endIndex - 1] != '\\'))
                {
                    insideQuotes = !insideQuotes;
                }
                else if (str[endIndex] == ',' && !insideQuotes)
                {
                    break;
                }
                endIndex++;
            }
            offset = endIndex + 1;

            // trim whitespace from the end of the item; the substring is guaranteed to
            // have at least one non-whitespace character
            while (char.IsWhiteSpace(str[endIndex - 1]))
            {
                endIndex--;
            }

            return str.Substring(startIndex, endIndex - startIndex);
        }

        public static ContentType GetContentType(string contentType)
        {
            string contentTypeTrimmed = contentType.Trim();
            if (!string.IsNullOrEmpty(contentTypeTrimmed))
            {
                return GetContentTypeOrNull(contentTypeTrimmed);
            }
            return null;
        }

        public static ContentType GetContentTypeOrNull(string contentType)
        {
            try
            {
                Fx.Assert(contentType == contentType.Trim(), "The ContentType input argument should already be trimmed.");
                Fx.Assert(!string.IsNullOrEmpty(contentType), "The ContentType input argument should not be null or empty.");
                
                ContentType contentTypeToReturn = new ContentType(contentType);

                // Need to check for "*/<Something-other-than-*>" because the ContentType constructor doesn't catch this
                string[] typeAndSubType = contentTypeToReturn.MediaType.Split('/');
                Fx.Assert(typeAndSubType.Length == 2, "The creation of the ContentType would have failed if there wasn't a type and subtype.");
                if (typeAndSubType[0][0] == '*' && typeAndSubType[0].Length == 1 &&
                    !(typeAndSubType[1][0] == '*' && typeAndSubType[1].Length == 1))
                {
                    // 



                    // throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new FormatException(
                    // SR2.GetString(SR2.InvalidContentType, contentType)));
                    return null;
                }
                return contentTypeToReturn;
            }
            catch (FormatException e)
            {
                // Return null to indicate that the content type creation failed
                System.ServiceModel.DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning);
            }
            return null;
        }

        public static string IEnumerableToCommaSeparatedString(IEnumerable<string> items)
        {
            Fx.Assert(items != null, "The 'items' argument should never be null.");
            return string.Join(", ", items);
        }

        public static void AddRange<T>(ICollection<T> list, IEnumerable<T> itemsToAdd)
        {
            Fx.Assert(list != null, "The 'list' argument should never be null.");
            Fx.Assert(itemsToAdd != null, "The 'itemsToAdd' argument should never be null.");

            foreach (T item in itemsToAdd)
            {
                list.Add(item);
            }
        }
    }
}