10
10
using System . Web ;
11
11
using System . Net ;
12
12
using System . Threading ;
13
+ using Newtonsoft . Json ;
14
+ using System . Data ;
13
15
14
16
namespace BrowserSelect
15
17
{
@@ -31,8 +33,6 @@ static class Program
31
33
[ STAThread ]
32
34
static void Main ( string [ ] args )
33
35
{
34
- // fix #28
35
- LeaveDotsAndSlashesEscaped ( ) ;
36
36
// to prevent loss of settings when on update
37
37
if ( Settings . Default . UpdateSettings )
38
38
{
@@ -94,32 +94,9 @@ static void Main(string[] args)
94
94
uri = UriFollowRedirects ( uri ) ;
95
95
url = uri . AbsoluteUri ;
96
96
97
- foreach ( var sr in Settings . Default . AutoBrowser . Cast < string > ( )
98
- // maybe i should use a better way to split the pattern and browser name ?
99
- . Select ( x => x . Split ( new [ ] { "[#!][$~][?_]" } , StringSplitOptions . None ) )
100
- // to make sure * doesn't match when non-* rules exist.
101
- . OrderBy ( x => ( ( x [ 0 ] . Contains ( "*" ) ) ? 1 : 0 ) + ( x [ 0 ] == "*" ? 1 : 0 ) ) )
102
- {
103
- var pattern = sr [ 0 ] ;
104
- var browser = sr [ 1 ] ;
105
-
106
- // matching the domain to pattern
107
- if ( DoesDomainMatchPattern ( uri . Host , pattern ) )
108
- {
109
- // ignore the display browser select entry to prevent app running itself
110
- if ( browser != "display BrowserSelect" )
111
- {
112
- //todo: handle the case if browser is not found (e.g. imported settings or uninstalled browser)
113
- Form1 . open_url ( ( Browser ) browser ) ;
114
- return ;
115
- }
116
- else
117
- {
118
- // simply break the loop to let the app display selection dialogue
119
- break ;
120
- }
121
- }
122
- }
97
+ //if we loaded the browser finish execution here...
98
+ if ( load_browser ( uri ) )
99
+ return ;
123
100
}
124
101
125
102
// display main form
@@ -131,6 +108,71 @@ static void Main(string[] args)
131
108
Application . Run ( new frm_settings ( ) ) ;
132
109
}
133
110
111
+ private static Boolean load_browser ( Uri uri )
112
+ {
113
+ if ( Settings . Default . Rules != null && Settings . Default . Rules != "" )
114
+ {
115
+ DataTable rules = ( DataTable ) JsonConvert . DeserializeObject ( Settings . Default . Rules , ( typeof ( DataTable ) ) ) ;
116
+ foreach ( DataRow rule in rules . Rows )
117
+ {
118
+ Boolean rule_match = false ;
119
+ string match_type = ( string ) rule [ "Type" ] ;
120
+ string match = ( string ) rule [ "Match" ] ;
121
+ string pattern = ( string ) rule [ "Pattern" ] ;
122
+
123
+ string test_uri = "" ;
124
+ if ( match == "Domain" )
125
+ test_uri = uri . Host ;
126
+ else if ( match == "URL Path" )
127
+ test_uri = uri . PathAndQuery ;
128
+ else if ( match == "Full URL" )
129
+ test_uri = uri . AbsoluteUri ;
130
+
131
+ switch ( match_type )
132
+ {
133
+ case "Ends With" :
134
+ if ( test_uri . EndsWith ( pattern , StringComparison . OrdinalIgnoreCase ) )
135
+ rule_match = true ;
136
+ break ;
137
+ case "Starts With" :
138
+ if ( test_uri . StartsWith ( pattern , StringComparison . OrdinalIgnoreCase ) )
139
+ rule_match = true ;
140
+ break ;
141
+ case "Contains" :
142
+ if ( test_uri . IndexOf ( pattern , StringComparison . OrdinalIgnoreCase ) >= 0 )
143
+ rule_match = true ;
144
+ break ;
145
+ case "Matches" :
146
+ if ( test_uri . Equals ( pattern , StringComparison . OrdinalIgnoreCase ) )
147
+ rule_match = true ;
148
+ break ;
149
+ case "RegEx" :
150
+ Regex regex = new Regex ( pattern , RegexOptions . IgnoreCase ) ;
151
+ if ( regex . IsMatch ( test_uri ) )
152
+ rule_match = true ;
153
+ break ;
154
+ }
155
+
156
+ if ( rule_match )
157
+ {
158
+ System . Diagnostics . Debug . WriteLine ( test_uri + " " + match_type + " " + pattern ) ;
159
+ string browser = ( string ) rule [ "Browser" ] ;
160
+ if ( browser != "display BrowserSelect" )
161
+ Form1 . open_url ( ( Browser ) browser ) ;
162
+ return true ;
163
+ }
164
+ }
165
+ }
166
+ if ( Settings . Default . DefaultBrowser != null &&
167
+ Settings . Default . DefaultBrowser != "" &&
168
+ Settings . Default . DefaultBrowser != "display BrowserSelect" )
169
+ {
170
+ Form1 . open_url ( ( Browser ) Settings . Default . DefaultBrowser ) ;
171
+ return true ;
172
+ }
173
+ return false ;
174
+ }
175
+
134
176
// from : http://stackoverflow.com/a/250400/1461004
135
177
public static double time ( )
136
178
{
@@ -191,68 +233,6 @@ public static string ProgramFilesx86()
191
233
return Environment . GetEnvironmentVariable ( "ProgramFiles" ) ;
192
234
}
193
235
194
-
195
- /// <summary>
196
- /// Checks if a wildcard string matches a domain
197
- /// taken from http://madskristensen.net/post/wildcard-search-for-domains-in-c
198
- /// </summary>
199
- public static bool DoesDomainMatchPattern ( string domain , string domainToCheck )
200
- {
201
- if ( domainToCheck . Contains ( "*" ) )
202
- {
203
- string checkDomain = domainToCheck ;
204
- if ( checkDomain . StartsWith ( "*." ) )
205
- checkDomain = "*" + checkDomain . Substring ( 2 , checkDomain . Length - 2 ) ;
206
- return DoesWildcardMatch ( domain , checkDomain ) ;
207
- }
208
- else
209
- {
210
- return domainToCheck . Equals ( domain , StringComparison . OrdinalIgnoreCase ) ;
211
- }
212
- }
213
- /// <summary>
214
- /// Performs a wildcard (*) search on any string.
215
- /// </summary>
216
- public static bool DoesWildcardMatch ( string originalString , string searchString )
217
- {
218
- if ( ! searchString . StartsWith ( "*" ) )
219
- {
220
- int stop = searchString . IndexOf ( '*' ) ;
221
- if ( ! originalString . StartsWith ( searchString . Substring ( 0 , stop ) ) )
222
- return false ;
223
- }
224
- if ( ! searchString . EndsWith ( "*" ) )
225
- {
226
- int start = searchString . LastIndexOf ( '*' ) + 1 ;
227
- if ( ! originalString . EndsWith ( searchString . Substring ( start , searchString . Length - start ) ) )
228
- return false ;
229
- }
230
- Regex regex = new Regex ( searchString . Replace ( @"." , @"\." ) . Replace ( @"*" , @".*" ) ) ;
231
- return regex . IsMatch ( originalString ) ;
232
- }
233
-
234
- // https://stackoverflow.com/a/7202560/1461004
235
- private static void LeaveDotsAndSlashesEscaped ( )
236
- {
237
- var getSyntaxMethod =
238
- typeof ( UriParser ) . GetMethod ( "GetSyntax" , BindingFlags . Static | BindingFlags . NonPublic ) ;
239
- if ( getSyntaxMethod == null )
240
- {
241
- throw new MissingMethodException ( "UriParser" , "GetSyntax" ) ;
242
- }
243
-
244
- var uriParser = getSyntaxMethod . Invoke ( null , new object [ ] { "http" } ) ;
245
-
246
- var setUpdatableFlagsMethod =
247
- uriParser . GetType ( ) . GetMethod ( "SetUpdatableFlags" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
248
- if ( setUpdatableFlagsMethod == null )
249
- {
250
- throw new MissingMethodException ( "UriParser" , "SetUpdatableFlags" ) ;
251
- }
252
-
253
- setUpdatableFlagsMethod . Invoke ( uriParser , new object [ ] { 0 } ) ;
254
- }
255
-
256
236
private static Uri UriExpander ( Uri uri )
257
237
{
258
238
List < string > enabled_url_expanders = new List < string > ( ) ;
@@ -280,6 +260,7 @@ private static Uri UriExpander(Uri uri)
280
260
281
261
return uri ;
282
262
}
263
+
283
264
private static Uri UriFollowRedirects ( Uri uri , int num_redirects = 0 )
284
265
{
285
266
int max_redirects = 20 ;
0 commit comments