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

Scalars.cs « Protocols « Services « Web « System « System.Web.Services « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0d388dd5e64b446b3329ffd7499113fed45f78e4 (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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//------------------------------------------------------------------------------
// <copyright file="Scalars.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>                                                                
//------------------------------------------------------------------------------

namespace System.Web.Services.Protocols {

    using System.Web.Services;
    using System.Collections;
    using System.Globalization;
    using System.Reflection;
    using System;
    using System.Text;
    using System.Threading;
    
    internal class ScalarFormatter {
        private ScalarFormatter() { }

        internal static string ToString(object value) {
            if (value == null) 
                return string.Empty;
            else if (value is string)
                return (string)value;
            else if (value.GetType().IsEnum)
                return EnumToString(value);
            else
                return (string)Convert.ToString(value, CultureInfo.InvariantCulture);
        }

        internal static object FromString(string value, Type type) {
            try {
                if (type == typeof(string))
                    return value;
                else if (type.IsEnum)
                    return (object)EnumFromString(value, type);
                else
                    return Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }
                throw new ArgumentException(Res.GetString(Res.WebChangeTypeFailed, value, type.FullName), "type", e);
            }
        }

        static object EnumFromString(string value, Type type) {
            return Enum.Parse(type, value);
        }

        static string EnumToString(object value) {
            return Enum.Format(value.GetType(), value, "G");
        }

        // We support: whatever Convert supports, and Enums
        internal static bool IsTypeSupported(Type type) {
            if (type.IsEnum) return true;
            return (
                type == typeof(int) ||
                type == typeof(string) ||
                type == typeof(long) ||
                type == typeof(byte) ||
                type == typeof(sbyte) ||
                type == typeof(short) ||
                type == typeof(bool) ||
                type == typeof(char) ||
                type == typeof(float) ||
                type == typeof(decimal) ||
                type == typeof(DateTime) ||
                type == typeof(UInt16) ||
                type == typeof(UInt32) ||
                type == typeof(UInt64) ||
                type == typeof(double));
        }
    }

    internal class UrlEncoder {
        private UrlEncoder() { }

        private const int Max16BitUtf8SequenceLength = 4;

        internal static string EscapeString(string s, Encoding e) {
            return EscapeStringInternal(s, e == null ? new ASCIIEncoding() : e, false);
        }

        internal static string UrlEscapeString(string s, Encoding e) {
            return EscapeStringInternal(s, e == null ? new ASCIIEncoding() : e, true);
        }

        private static string EscapeStringInternal(string s, Encoding e, bool escapeUriStuff) {
            if (s == null) return null;
            
            byte[] bytes = e.GetBytes(s);
            StringBuilder sb = new StringBuilder(bytes.Length);
            for (int i = 0; i < bytes.Length; i++) {
                byte b = bytes[i];
                char c = (char)b;
                if (b > 0x7f || b < 0x20 || c == '%' || (escapeUriStuff && !IsSafe(c))) {
                    HexEscape8(sb, c);
                }
                else {
                    sb.Append(c);
                }
            }
            return sb.ToString();
        }

        /*
        // Microsoft: adapted from UrlEscapeStringUnicode below
        internal static string EscapeStringUnicode(string s) {
            int l = s.Length;
            StringBuilder sb = new StringBuilder(l);

            for (int i = 0; i < l; i++) {
                char ch = s[i];
                if ((ch & 0xff80) == 0) {
                    sb.Append(ch);
                }
                else {
                    HexEscape16(sb, ch);
                }
            }

            return sb.ToString();
        }
        */

        // Microsoft: copied from System.Web.HttpUtility
        internal static string UrlEscapeStringUnicode(string s) {
            int l = s.Length;
            StringBuilder sb = new StringBuilder(l);

            for (int i = 0; i < l; i++) {
                char ch = s[i];

                if (IsSafe(ch)) {
                    sb.Append(ch);
                }
                else if (ch == ' ') {
                    sb.Append('+');
                }
                else if ((ch & 0xff80) == 0) {  // 7 bit?
                    HexEscape8(sb, ch);
                }
                else { // arbitrary Unicode?
                    HexEscape16(sb, ch);
                }
            }
            return sb.ToString();
        }

        private static void HexEscape8(StringBuilder sb, char c) {
            sb.Append('%');
            sb.Append(HexUpperChars[(c >> 4) & 0xf]);
            sb.Append(HexUpperChars[(c) & 0xf]);
        }

        private static void HexEscape16(StringBuilder sb, char c) {
            sb.Append("%u");
            sb.Append(HexUpperChars[(c >> 12) & 0xf]);
            sb.Append(HexUpperChars[(c >> 8) & 0xf]);
            sb.Append(HexUpperChars[(c >> 4) & 0xf]);
            sb.Append(HexUpperChars[(c) & 0xf]);
        }

        private static bool IsSafe(char ch) {
            if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
                return true;

            switch (ch) {
                case '-':
                case '_':
                case '.':
                case '!':
                case '*':
                case '\'':
                case '(':
                case ')':
                    return true;
            }

            return false;
        }

        internal static readonly char[] HexUpperChars = {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
        };
    }

    internal class ContentType {
        private ContentType() { }
        internal const string TextBase = "text";
        internal const string TextXml     = "text/xml";
        internal const string TextPlain   = "text/plain";
        internal const string TextHtml    = "text/html";

        internal const string ApplicationBase = "application";
        internal const string ApplicationXml = "application/xml";
        internal const string ApplicationSoap = "application/soap+xml";
        internal const string ApplicationOctetStream = "application/octet-stream";

        internal const string ContentEncoding = "Content-Encoding";

        // this returns the "base" part of the contentType/mimeType, e.g. the "text/xml" part w/o
        // the ; CharSet=isoxxx part that sometimes follows.
        internal static string GetBase(string contentType) {
            int semi = contentType.IndexOf(';');
            if (semi >= 0) return contentType.Substring(0, semi);
            return contentType;
        }

        // this returns the "type" part of the contentType/mimeType without subtyape
        internal static string GetMediaType(string contentType) {
            string baseCT = GetBase(contentType);
            int tmp = baseCT.IndexOf('/');
            if (tmp >= 0) return baseCT.Substring(0, tmp);
            return baseCT;
        }

        // grabs as follows (and case-insensitive): .*;\s*charset\s*=\s*[\s'"]*(.*)[\s'"]*
        internal static string GetCharset(string contentType) {
            return GetParameter(contentType, "charset");
        }

        internal static string GetAction(string contentType) {
            return GetParameter(contentType, "action");
        }

        private static string GetParameter(string contentType, string paramName) {
            string[] paramDecls = contentType.Split(new char[] { ';' });
            for (int i = 1; i < paramDecls.Length; i++) {
                string paramDecl = paramDecls[i].TrimStart(null);
                if (String.Compare(paramDecl, 0, paramName, 0, paramName.Length, StringComparison.OrdinalIgnoreCase) == 0) {
                    int equals = paramDecl.IndexOf('=', paramName.Length);
                    if (equals >= 0) 
                        return paramDecl.Substring(equals + 1).Trim(new char[] { ' ', '\'', '\"', '\t' });
                }
            }
            return null;
        }

        internal static bool MatchesBase(string contentType, string baseContentType) {
            return string.Compare(GetBase(contentType), baseContentType, StringComparison.OrdinalIgnoreCase) == 0;
        }

        internal static bool IsApplication(string contentType) {
            return string.Compare(GetMediaType(contentType), ApplicationBase, StringComparison.OrdinalIgnoreCase) == 0;
        }

        internal static bool IsSoap(string contentType) {
            string type = GetBase(contentType);
            return (String.Compare(type, ContentType.TextXml, StringComparison.OrdinalIgnoreCase) == 0) ||
                   (String.Compare(type, ContentType.ApplicationSoap, StringComparison.OrdinalIgnoreCase) == 0);
        }
        
        internal static bool IsXml(string contentType) {
            string type = GetBase(contentType);
            return (String.Compare(type, ContentType.TextXml, StringComparison.OrdinalIgnoreCase) == 0) ||
                   (String.Compare(type, ContentType.ApplicationXml, StringComparison.OrdinalIgnoreCase) == 0);
        }

        internal static bool IsHtml(string contentType) {
            string type = GetBase(contentType);
            return String.Compare(type, ContentType.TextHtml, StringComparison.OrdinalIgnoreCase) == 0;
        }

        internal static string Compose(string contentType, Encoding encoding) {
            return Compose(contentType, encoding, null);
        }

        internal static string Compose(string contentType, Encoding encoding, string action) {
            if (encoding == null && action == null) return contentType;

            StringBuilder sb = new StringBuilder(contentType);
            if (encoding != null) {
                sb.Append("; charset=");
                sb.Append(encoding.WebName);
            }
            if (action != null) {
                sb.Append("; action=\"");
                sb.Append(action);
                sb.Append("\"");
            }
            return sb.ToString();
        }
    }

    internal class MemberHelper {
        private MemberHelper() { }
        static object[] emptyObjectArray = new object[0];

        internal static void SetValue(MemberInfo memberInfo, object target, object value) {
            if (memberInfo is FieldInfo ) {
                ((FieldInfo)memberInfo).SetValue(target, value);
            }
            else {
                ((PropertyInfo)memberInfo).SetValue(target, value, emptyObjectArray);
            }
        }

        internal static object GetValue(MemberInfo memberInfo, object target) {
            if (memberInfo is FieldInfo) {
                return ((FieldInfo)memberInfo).GetValue(target);
            }
            else {
                return ((PropertyInfo)memberInfo).GetValue(target, emptyObjectArray);
            }
        }

        internal static bool IsStatic(MemberInfo memberInfo) {
            if (memberInfo is FieldInfo)
                return ((FieldInfo)memberInfo).IsStatic;
            else
                return false;
        }

        internal static bool CanRead(MemberInfo memberInfo) {
            if (memberInfo is FieldInfo)
                return true;
            else
                return ((PropertyInfo)memberInfo).CanRead;
        }

        internal static bool CanWrite(MemberInfo memberInfo) {
            if (memberInfo is FieldInfo)
                return true;
            else
                return ((PropertyInfo)memberInfo).CanWrite;
        }
    }
}