Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Web / Util / RegexUtil.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Text.RegularExpressions;
7
8 namespace System.Web.Util {
9     internal class RegexUtil {
10
11         // this method is for the regex match which accepts the pattern from developer
12         // since asp.net doesn't have control of the regex pattern string and it is possible 
13         // to take more than 2 sec to match a string, give developer option to set timeout value
14         public static bool IsMatch(string stringToMatch, string pattern, RegexOptions regOption, int? timeoutInMillsec) {            
15             int timeout = GetRegexTimeout(timeoutInMillsec);
16
17             if (timeout > 0 || timeoutInMillsec.HasValue) {
18                 return Regex.IsMatch(stringToMatch, pattern, regOption, TimeSpan.FromMilliseconds((double)timeout));
19             } else {
20                 return Regex.IsMatch(stringToMatch, pattern, regOption);
21             }
22         }
23
24         public static Match Match(string stringToMatch, string pattern, RegexOptions regOption, int? timeoutInMillsec) {
25             int timeout = GetRegexTimeout(timeoutInMillsec);
26
27             if (timeout > 0 || timeoutInMillsec.HasValue) {
28                 return Regex.Match(stringToMatch, pattern, regOption, TimeSpan.FromMilliseconds((double)timeout));
29             } else {
30                 return Regex.Match(stringToMatch, pattern, regOption);
31             }
32         }
33
34         public static Regex CreateRegex(string pattern, RegexOptions option, int? timeoutInMillsec) {
35             int timeout = GetRegexTimeout(timeoutInMillsec);
36
37             if (timeout > 0 || timeoutInMillsec.HasValue) {
38                 return new Regex(pattern, option, TimeSpan.FromMilliseconds((double)timeout));
39             } else {
40                 return new Regex(pattern, option);
41             }
42         }
43
44         // This method is for the regex asp.net controls the regex pattern and it should NOT take longer than 2 secs to match the string
45         // so no need for developer to specify a timeout value
46         internal static Regex CreateRegex(string pattern, RegexOptions option) {
47             return CreateRegex(pattern, option, null);
48         }
49
50         private static bool? _isRegexTimeoutSetInAppDomain;
51         private static bool IsRegexTimeoutSetInAppDomain {
52             get {
53                 if (!_isRegexTimeoutSetInAppDomain.HasValue) {
54                     bool timeoutSetInAppDomain = false;
55                     try {
56                         timeoutSetInAppDomain = AppDomain.CurrentDomain.GetData("REGEX_DEFAULT_MATCH_TIMEOUT") != null;
57                     } catch {
58                     }
59                     _isRegexTimeoutSetInAppDomain = timeoutSetInAppDomain;
60                 }
61                 return _isRegexTimeoutSetInAppDomain.Value;
62             }
63         }
64
65         private static int GetRegexTimeout(int? timeoutInMillsec) {
66             int timeout = -1;
67
68             // here is the logic for using timeout in regex
69             // 1. if the caller sets a timeout value, then we use it(this may cause Regex throw ArgumentOutOfRangeException, 
70             // but developer will know what they need to do when seeing the exception)
71             // 2. if there is global setting in AppDomain, we do nothing(leave it to Regex to handle the timeout)
72             // 3. if the web app targets to 4.6.1+, then we set 2 secs timeout
73             if (timeoutInMillsec.HasValue) {
74                 timeout = timeoutInMillsec.Value;
75             } else {
76                 if (!IsRegexTimeoutSetInAppDomain && BinaryCompatibility.Current.TargetsAtLeastFramework461) {
77                     timeout = 2000;
78                 }
79             }
80             return timeout;
81         }
82     }
83 }