-
Notifications
You must be signed in to change notification settings - Fork 5
/
BestIconFinder.cs
772 lines (636 loc) · 24.4 KB
/
BestIconFinder.cs
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
CustomIconDashboarder - KeePass Plugin to get some information and
manage custom icons
Copyright (C) 2023 Jareth Lomson <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Drawing;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
using LomsonLib.Utility;
namespace CustomIconDashboarderPlugin
{
/// <summary>
/// Find the best icon of a web site from its url.
/// </summary>
public class BestIconFinder
{
#region Static elements
/// <summary>
/// STATIC Section
/// </summary>
public static void InitClass() {
// Enable TLS 1.1 (768), TLS 1.2 (3072) and TLS 1.3 (12288)
ServicePointManager.SecurityProtocol =
(SecurityProtocolType)768 |
(SecurityProtocolType)3072 |
(SecurityProtocolType)12288;
}
~BestIconFinder() {
client.Dispose();
}
private const string REQUEST_USER_AGENT = "Mozilla/5.0 (Windows 6.1; rv:27.0) Gecko/20100101 Firefox/27.0";
private const string REQUEST_HEADER_ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
private const int REQUEST_TIMEOUT = 10000;
private const string alphabetUC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string alphabetLC = "abcdefghijklmnopqrstuvwxyz";
private static string[] xPathToTest = {
"//link[translate(@rel,'" + alphabetUC + "','" + alphabetLC +"')='shortcut icon']",
"//link[translate(@rel,'" + alphabetUC + "','" + alphabetLC +"')='icon']",
"//link[translate(@rel,'" + alphabetUC + "','" + alphabetLC +"')='apple-touch-icon']",
"//link[translate(@rel,'" + alphabetUC + "','" + alphabetLC +"')='apple-touch-icon image_src']",
"//link[translate(@rel,'" + alphabetUC + "','" + alphabetLC +"')='apple-touch-icon-precomposed']"
};
private static Dictionary<Uri,Image> imageCache = new Dictionary<Uri, Image>{};
private static object lckImageCache = new object();
private static Dictionary<Uri, CachedURIRequestResult> uriRequestCache = new Dictionary<Uri, CachedURIRequestResult>{};
private static object lckUriRequestCache = new object();
#endregion
public Image BestImage {get; private set; }
public Uri SiteUri {get; private set;} // Uri without path - eg http://www.sample.com
public Uri OriginalSiteUri {get; private set;} // Original URI with all path - eg http://www.sample.com/folder/subfolder
public Uri RootUriForIco {get; private set;}
public FinderResult Result {get; private set; }
public string Details {get;set; }
public int NbRequestCacheInvoke {get; private set; }
private MyWebClient client;
public List<ImageInfo> ListImageInfo {get; private set;} // All image and URI
public int IndexOfBestImage {get; private set; }
private MyLogger myLogger;
private List<Uri> ListUriIcons {get; set;}
public BestIconFinder(Uri siteUri)
{
this.OriginalSiteUri = siteUri;
if ( (siteUri.Scheme.ToLower() == "http") ||
(siteUri.Scheme.ToLower() == "https" ) ){
this.SiteUri = new Uri(siteUri.Scheme + "://" +siteUri.DnsSafeHost);
this.Result = new FinderResult(FinderResult.RESULT_NO_URL);
} else {
this.SiteUri = null;
this.Result = new FinderResult(FinderResult.RESULT_NO_URL);
}
this.myLogger = new MyLogger(this);
this.BestImage = null;
this.IndexOfBestImage = -1;
this.ListImageInfo = new List<ImageInfo>();
this.NbRequestCacheInvoke = 0;
client = new MyWebClient();
}
public BestIconFinder()
{
this.SiteUri = null;
this.Result = null;
this.myLogger = new MyLogger(this);
this.BestImage = null;
this.IndexOfBestImage = -1;
this.ListImageInfo = new List<ImageInfo>();
this.NbRequestCacheInvoke = 0;
client = new MyWebClient();
}
#region Embeded Factory
public static BestIconFinder BuildInstanceWithNoURLResult()
{
BestIconFinder result = new BestIconFinder
{
Result = new FinderResult(FinderResult.RESULT_NO_URL)
};
return result;
}
public static BestIconFinder BuildInstanceWithMalformedURLResult()
{
BestIconFinder result = new BestIconFinder
{
Result = new FinderResult(FinderResult.RESULT_MALFORMED_URL)
};
result.myLogger.LogDebug("Non managed URL format");
return result;
}
#endregion
public void FindBestIcon()
{
if (this.SiteUri == null) {
this.myLogger.LogWarn("No URL. Stop Find Best Icon");
return;
}
FindBestIconForURI( this.SiteUri);
if (this.BestImage == null) { // try with www
Uri testURI = this.SiteUri;
if ( ! testURI.Host.StartsWith("www.") ) {
FindBestIconForURI( new Uri(
this.SiteUri.Scheme + "://www." + this.SiteUri.DnsSafeHost ));
}
}
string rootDomain = URLUtility.RootDomain(this.SiteUri.AbsoluteUri);
if (! string.IsNullOrEmpty(rootDomain)) {
if (this.BestImage == null) { // try with root domain prefix
FindBestIconForURI( new Uri( this.SiteUri.Scheme + "://" + rootDomain ));
}
if (this.BestImage == null) { // try with host prefix and www
FindBestIconForURI( new Uri (this.SiteUri.Scheme + "://www." + rootDomain));
}
}
if (this.RootUriForIco != null) {
if (this.BestImage == null) { // try with host prefix
FindBestIconForURI( new Uri(
this.RootUriForIco.Scheme + "://" + this.RootUriForIco.Host) );
}
if (this.BestImage == null) { // try with host prefix and www
FindBestIconForURI( new Uri (
this.RootUriForIco.Scheme + "://www." + this.RootUriForIco.Host));
}
}
if (this.BestImage == null) {
FindBestIconForURI( this.OriginalSiteUri );
}
}
public void FindBestIconForURI(Uri param_uri)
{
// Prepare
if (param_uri == null) {
this.myLogger.LogWarn("No url. Stop Find Best Icon");
return;
}
Uri testedUri = param_uri;
// Get Links
GetIconLinksForURI(testedUri);
if (this.Result.ResultCode != FinderResult.RESULT_OK) {
this.myLogger.LogError("Error while retrieving Icon link for " +
testedUri.AbsoluteUri);
return;
}
// Test Icon URL
this.myLogger.LogInfo("Test Icon URL");
this.myLogger.Indent();
this.BestImage = null;
foreach ( Uri uriTested in this.ListUriIcons) {
TestImageUri( uriTested);
}
// Test default url
if ( ((this.BestImage != null)
&& (this.BestImage.Width + this.BestImage.Height < 64))
|| (this.BestImage == null) ) {
TestImageUri( new Uri(this.RootUriForIco, "favicon.ico"));
}
// Try better resolution (usefull for site like www dot francetvsport dot com
if ( ((this.BestImage != null)
&& (this.BestImage.Width + this.BestImage.Height < 64))
|| (this.BestImage == null) ) {
TestImageUri( new Uri(this.RootUriForIco, "apple-touch-icon-precomposed.png"));
TestImageUri( new Uri(this.RootUriForIco, "apple-touch-icon.png"));
}
this.myLogger.Unindent();
this.myLogger.LogInfo("End test Icon URI");
}
/// <summary>
/// Test Image URI and store in Best Image if quality is better
/// </summary>
public void TestImageUri (Uri uriToBeTested)
{
this.myLogger.LogDebug("Test " + uriToBeTested.AbsoluteUri);
byte[] pictureData = null;
Image downloadedImage = null;
bool imageWasInCache = false;
lock (lckImageCache) {
if (imageCache.ContainsKey( uriToBeTested ) ) {
downloadedImage = imageCache[uriToBeTested];
if (downloadedImage != null)
downloadedImage = (Image)downloadedImage.Clone();
imageWasInCache = true;
}
}
if (!imageWasInCache) {
try {
pictureData = client.DownloadData( uriToBeTested);
pictureData = PreparedIconBytesArrayToBeParsed( pictureData );
try {
downloadedImage = KeePassLib.Utility.GfxUtil.LoadImage( pictureData );
}
catch (Exception e) {
this.myLogger.LogError("Error while parsing image " + uriToBeTested.AbsoluteUri + System.Environment.NewLine
+ e.Message);
}
}
catch (WebException) {
this.myLogger.LogError( "Error while getting icon "
+ uriToBeTested.AbsoluteUri);
}
catch (NotSupportedException) {
this.myLogger.LogError( "Not Supported Exception while getting icon "
+ uriToBeTested.AbsoluteUri);
}
lock (lckImageCache) {
if (!imageCache.ContainsKey(uriToBeTested)) {
imageCache.Add(uriToBeTested, downloadedImage);
}
if (downloadedImage != null)
downloadedImage = (Image)downloadedImage.Clone();
}
}
if (downloadedImage != null) {
// It is necessary to clone image to avoid concurrent access during post operation.
// For example, error should occured if image is resized by several thread in the same time.
this.myLogger.LogDebug( "Size " + downloadedImage.Width + " x " + downloadedImage.Height );
this.ListImageInfo.Add( new ImageInfo( downloadedImage,uriToBeTested.AbsolutePath) );
if ( (this.BestImage == null ) ||
(CompareImageQuality( downloadedImage, this.BestImage) == 1) ) {
this.BestImage = downloadedImage;
this.IndexOfBestImage = this.ListImageInfo.Count - 1;
this.myLogger.LogInfo("-->stored as the best image");
}
}
}
public void GetIconLinks() {
GetIconLinksForURI(this.SiteUri);
}
public void GetIconLinksForURI(Uri param_Uri)
{
this.myLogger.LogDebug("Start findIconLinks");
this.myLogger.LogDebug("URL = " + param_Uri.AbsoluteUri);
this.myLogger.Indent();
Uri testedUri = param_Uri;
// Get HTML
HtmlAgilityPack.HtmlDocument htmlPage = null;
Uri fullUri = null;
bool uriInCache = false;
lock(lckUriRequestCache) {
if (uriRequestCache.ContainsKey( param_Uri )) {
CachedURIRequestResult curr = uriRequestCache[param_Uri];
fullUri = curr.FullUri;
htmlPage = curr.Html;
this.NbRequestCacheInvoke++;
uriInCache = true;
}
}
if (!uriInCache) {
try {
fullUri = GetURIOfSite(testedUri, out htmlPage);
}
catch ( WebException ) {
this.Result = new FinderResult(FinderResult.RESULT_HTML_NOT_FOUND);
this.myLogger.Unindent();
this.myLogger.LogDebug("End findIconLinks");
return;
}
catch ( Exception ) {
this.Result = new FinderResult(FinderResult.RESULT_HTML_PARSING);
this.myLogger.Unindent();
this.myLogger.LogDebug("End findIconLinks");
return;
}
lock (lckUriRequestCache) {
if (!uriRequestCache.ContainsKey( param_Uri )) {
var curr = new CachedURIRequestResult(fullUri, htmlPage);
uriRequestCache.Add( param_Uri, curr);
}
}
}
if ( htmlPage == null) {
this.Result = new FinderResult(FinderResult.RESULT_HTML_NOT_FOUND);
this.myLogger.Unindent();
this.myLogger.LogDebug("End findIconLinks");
return;
}
this.RootUriForIco = new Uri ( fullUri.Scheme + "://" + fullUri.DnsSafeHost );
this.myLogger.LogDebug("Root Url = " + this.RootUriForIco.AbsoluteUri);
// Get XPath
HtmlNode hnn = htmlPage.DocumentNode;
//this.myLogger.LogDebugIndented(hnn.OuterHtml);
this.ListUriIcons = new List<Uri>();
foreach (String currentXpath in xPathToTest)
{
this.myLogger.LogInfo("Testing " + currentXpath);
this.myLogger.Indent();
HtmlNodeCollection hnc = hnn.SelectNodes(currentXpath);
if ( (hnc == null) || (hnc.Count == 0) ) {
this.myLogger.LogInfo("Node not found for xpath " + currentXpath);
}
else {
foreach (HtmlNode hn in hnc) {
string relativeUrlPicture = hn.GetAttributeValue("href", "");
if ( relativeUrlPicture.Length == 0 ) {
this.myLogger.LogWarn("Zero Length URL found for xpath ");
}
else {
var uriPicture = new Uri(this.RootUriForIco, relativeUrlPicture);
if (!this.ListUriIcons.Exists((Uri myUri) => myUri == uriPicture)) {
this.ListUriIcons.Add( uriPicture);
}
this.myLogger.LogDebug("URL = '" + uriPicture.AbsoluteUri + "'");
}
}
}
this.myLogger.Unindent();
}
this.Result = new FinderResult( FinderResult.RESULT_OK );
this.myLogger.Unindent();
this.myLogger.LogDebug("End findIconLinks");
}
/// <summary>
/// Compare Quality of two images.
/// </summary>
/// <param name="img1"></param>
/// <param name="img2"></param>
/// <returns>1 = if img1 better than img2, 0 = equivalent, -1 = worse</returns>
private static int CompareImageQuality(Image img1, Image img2) {
if ( (img1.Width + img1.Height) > (img2.Width + img2.Height) ) {
return 1;
}
else if ( (img1.Width + img1.Height) == (img2.Width + img2.Height) ) {
return 0;
}
return -1;
}
/// <summary>
/// Gets a memory stream representing an image from an explicit favicon location.
/// </summary>
/// <param name="fullURI">The URI.</param>
/// <returns></returns>
private Uri GetURIOfSite(Uri initialURI, out HtmlDocument hdoc)
{
HtmlWeb hw = new HtmlWeb();
hw.UserAgent = REQUEST_USER_AGENT;
hdoc = null;
Uri responseURI = null;
Uri nextUri = initialURI;
try
{
int counter = 0; // Protection from cyclic redirect
do
{
// A cookie container is needed for some sites to work
hw.PreRequest += PreRequest_EventHandler;
// HtmlWeb.Load will follow 301 and 302 redirects to alternate URIs
hdoc = hw.Load(nextUri.AbsoluteUri);
responseURI = hw.ResponseUri;
// Old school meta refreshes need to parsed
nextUri = GetMetaRefreshLink(responseURI, hdoc);
counter++;
} while (nextUri != null && counter < 16); // Sixteen redirects would be more than enough.
}
catch (Exception e) {
this.myLogger.LogError("error while getting url " + nextUri.AbsoluteUri
+ " " + e.Message);
}
return responseURI;
}
private Uri GetMetaRefreshLink(Uri uri, HtmlAgilityPack.HtmlDocument hdoc)
{
HtmlNodeCollection metas = hdoc.DocumentNode.SelectNodes("/html/head/meta");
string redirect = null;
if (metas == null)
{
return null;
}
for (int i = 0; i < metas.Count; i++)
{
HtmlNode node = metas[i];
try
{
HtmlAttribute httpeq = node.Attributes["http-equiv"];
HtmlAttribute content = node.Attributes["content"];
if (httpeq.Value.ToLower().Equals("location") || httpeq.Value.ToLower().Equals("refresh"))
{
if (content.Value.ToLower().Contains("url"))
{
Match match = Regex.Match(content.Value.ToLower(), @".*?url[\s=]*(\S+)");
if (match.Success)
{
redirect = match.Captures[0].ToString();
redirect = match.Groups[1].ToString();
}
}
}
}
catch (Exception) { }
}
if (String.IsNullOrEmpty(redirect))
{
return null;
}
return new Uri(uri, redirect);
}
/// <summary>
/// Prepare bytes array representing an icon to be parsed by libraries
/// (provided by KP and MS)
/// </summary>
/// <description>
/// Pad with zero if length of array is not sufficient according to header.
/// </description>
/// <param name="batbp">Byte array to be parsed</param>
/// <returns></returns>
/// Inspired by the method KeePassLib.Utility.GsfxUtil.UnpackIco of KeePass v2.30
private byte[] PreparedIconBytesArrayToBeParsed(byte[] batbp) {
if (batbp == null) {
myLogger.LogWarn("PreparedIconBytesArrayToBeParsed: Byte array to be prepared is Null");
return batbp;
}
const int SizeICONDIR = 6;
const int SizeICONDIRENTRY = 16;
if (BitConverter.ToInt32(new byte[] { 1, 2, 3, 4 },
0) != 0x04030201) {
myLogger.LogWarn("PreparedIconBytesArrayToBeParsed: Not Little Endian detected");
}
if (batbp.Length < SizeICONDIR) return null;
if (BitConverter.ToUInt16(batbp, 0) != 0) return batbp; // Reserved, 0 if icon
if (BitConverter.ToUInt16(batbp, 2) != 1) return batbp; // ICO type, 1 if icon
int nbIcon = BitConverter.ToUInt16(batbp, 4);
if (nbIcon < 0) {
myLogger.LogWarn("PreparedIconBytesArrayToBeParsed: nb of Icons is not positive");;
}
int cbDir = SizeICONDIR + (nbIcon * SizeICONDIRENTRY);
if (batbp.Length < cbDir)
return null;
int iOffset = SizeICONDIR;
int padLength = 0;
for (int i = 0; i < nbIcon; ++i) {
int dataSize = BitConverter.ToInt32(batbp, iOffset + 8);
int iStartImgData = BitConverter.ToInt32(batbp, iOffset + 12);
if ((iStartImgData + dataSize) > batbp.Length) {
padLength = iStartImgData + dataSize - batbp.Length;
}
iOffset += SizeICONDIRENTRY;
}
if (padLength == 0 ) {
return batbp;
}
else {
myLogger.LogWarn("PreparedIconBytesArrayToBeParsed: Padding file with "
+ padLength.ToString() + " leading zero(s)");;
var padArray = new byte[padLength];
for (int ii = 0; ii < padLength; ii++) {
padArray[ii] = 0;
}
var result = new byte[batbp.Length + padLength];
Array.Copy(batbp, 0, result, 0, batbp.Length);
Array.Copy(padArray, 0, result, batbp.Length, padLength);
return result;
}
}
private bool PreRequest_EventHandler(HttpWebRequest request)
{
request.CookieContainer = new System.Net.CookieContainer();
request.Accept = REQUEST_HEADER_ACCEPT;
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "*");
request.Timeout = REQUEST_TIMEOUT;
return true;
}
private sealed class MyLogger {
private int nbIndent;
private String indent;
private BestIconFinder myBestIf;
private int logLevel; // ERROR = 1, WARN = 2, INFO = 3, DEBUG = 4
public MyLogger(BestIconFinder paramMyBestIf) {
this.nbIndent = 0;
indent = "";
this.myBestIf = paramMyBestIf;
this.logLevel = 4;
}
public MyLogger(BestIconFinder paramMyBestIf, int paramLogLevel):this(paramMyBestIf) {
logLevel = paramLogLevel;
}
public void Reset() {
this.nbIndent = 0;
indent = "";
}
public void Indent() {
nbIndent++;
updateIndent();
}
public void Unindent() {
nbIndent--;
updateIndent();
}
private void updateIndent() {
indent = "";
indent = indent.PadRight(nbIndent * 2, ' ');
}
public void LogInfo(String what) {
if ( this.logLevel >= 3)
myBestIf.Details += indent + what + System.Environment.NewLine;
}
public void LogDebug(String what) {
if ( this.logLevel >= 4)
myBestIf.Details += indent + what + System.Environment.NewLine;
}
public void LogDebugIndented(String what) {
if ( this.logLevel >= 4) {
what = what.Replace("\n", System.Environment.NewLine + indent);
myBestIf.Details += indent + what + System.Environment.NewLine;
}
}
public void LogWarn(String what) {
if ( this.logLevel >= 2)
myBestIf.Details += indent + what + System.Environment.NewLine;
}
public void LogError(String what) {
if ( this.logLevel >= 1)
myBestIf.Details += indent + "ERROR - " + what + System.Environment.NewLine;
}
}
public void Dispose() {
if (this.BestImage != null) {
this.BestImage.Dispose();
}
}
// // Set "UnsafeHeaderParsing" flags. Usefull to retrieve information from several sites
// // try www dot geni dot com
// // to be used with "using System.Reflection;" in header
// public static bool SetAllowUnsafeHeaderParsing(bool value)
// {
// //Get the assembly that contains the internal class
// Assembly aNetAssembly = Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
// if (aNetAssembly != null)
// {
// //Use the assembly in order to get the internal type for the internal class
// Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal");
// if (aSettingsType != null)
// {
// //Use the internal static property to get an instance of the internal settings class.
// //If the static instance isn't created allready the property will create it for us.
// object anInstance = aSettingsType.InvokeMember("Section",
// BindingFlags.Static | BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, new object[] { });
//
// if (anInstance != null)
// {
// //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not
// FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", BindingFlags.NonPublic | BindingFlags.Instance);
// if (aUseUnsafeHeaderParsing != null)
// {
// aUseUnsafeHeaderParsing.SetValue(anInstance, value);
// return true;
// }
// }
// }
// }
// return false;
// }
//
public class MyWebClient : WebClient
{
private readonly CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
// enhance chance to get favicon by setting some specifics parameters in request
// Sample of web site and parameters
// Header Accept - usefull for www dot akamai dot com
WebRequest request = null;
try { request = base.GetWebRequest(address); }
catch (Exception) { } // address is not well formed
var webRequest = request as HttpWebRequest;
if (webRequest != null) {
webRequest.Accept = REQUEST_HEADER_ACCEPT;
webRequest.UserAgent = REQUEST_USER_AGENT;
webRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "*");
webRequest.Timeout = REQUEST_TIMEOUT;
webRequest.CookieContainer = m_container;
}
return request;
}
}
private sealed class CachedURIRequestResult {
public Uri FullUri {get; private set; }
public HtmlDocument Html {get; private set; }
public CachedURIRequestResult (Uri fullUri, HtmlDocument html) {
this.FullUri = fullUri;
this.Html = html;
}
}
}
public sealed class FinderResult {
public const int RESULT_NOT_YET_SEARCH = 0;
public const int RESULT_OK = 1;
public const int RESULT_HTML_NOT_FOUND = 2;
public const int RESULT_HTML_PARSING = 3;
public const int RESULT_NO_URL = 4;
public const int RESULT_MALFORMED_URL = 5;
public int ResultCode {get; private set; }
public FinderResult (int code)
{
ResultCode = code;
}
public override string ToString() {
switch (this.ResultCode) {
case FinderResult.RESULT_NOT_YET_SEARCH : return "Not Yet Search";
case RESULT_OK : return "OK";
case RESULT_HTML_NOT_FOUND : return "Html not found";
case RESULT_HTML_PARSING : return "Error Html parsing";
case RESULT_NO_URL : return "No URL";
case RESULT_MALFORMED_URL: return "Malformed URL";
default : return "";
}
}
}
}