public static void ClearCacheItems(bool bClearFiles, bool bClearCookies) { if (!bClearCookies && !bClearFiles) { throw new ArgumentException("You must call ClearCacheItems with at least one target"); } if (!FiddlerApplication.DoClearCache(bClearFiles, bClearCookies)) { FiddlerApplication.Log.LogString("Cache clearing was handled by an extension. Default clearing was skipped."); return; } if (Environment.OSVersion.Version.Major > 5) { FiddlerApplication.Log.LogString(string.Format("Windows Vista+ detected. Calling INETCPL to clear [{0}{1}].", bClearFiles ? "CacheFiles" : string.Empty, bClearCookies ? "Cookies" : string.Empty)); WinINETCache.VistaClearTracks(bClearFiles, bClearCookies); return; } if (bClearCookies) { WinINETCache.ClearCookiesForHost("*"); } if (!bClearFiles) { return; } FiddlerApplication.Log.LogString("Beginning WinINET Cache clearing..."); long groupId = 0L; int num = 0; IntPtr intPtr = IntPtr.Zero; IntPtr intPtr2 = IntPtr.Zero; intPtr2 = WinINETCache.FindFirstUrlCacheGroup(0, 0, IntPtr.Zero, 0, ref groupId, IntPtr.Zero); int lastWin32Error = Marshal.GetLastWin32Error(); bool flag; if (intPtr2 != IntPtr.Zero && 259 != lastWin32Error && 2 != lastWin32Error) { while (true) { flag = WinINETCache.DeleteUrlCacheGroup(groupId, 2, IntPtr.Zero); lastWin32Error = Marshal.GetLastWin32Error(); if (!flag && 2 == lastWin32Error) { flag = WinINETCache.FindNextUrlCacheGroup(intPtr2, ref groupId, IntPtr.Zero); lastWin32Error = Marshal.GetLastWin32Error(); } if (!flag) { if (259 == lastWin32Error) { break; } if (2 == lastWin32Error) { break; } } } } intPtr2 = WinINETCache.FindFirstUrlCacheEntryExA(null, 0, WinINETCache.WININETCACHEENTRYTYPE.ALL, 0L, IntPtr.Zero, ref num, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); lastWin32Error = Marshal.GetLastWin32Error(); if (IntPtr.Zero == intPtr2 && 259 == lastWin32Error) { return; } int num2 = num; intPtr = Marshal.AllocHGlobal(num2); intPtr2 = WinINETCache.FindFirstUrlCacheEntryExA(null, 0, WinINETCache.WININETCACHEENTRYTYPE.ALL, 0L, intPtr, ref num, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); lastWin32Error = Marshal.GetLastWin32Error(); do { WinINETCache.INTERNET_CACHE_ENTRY_INFOA iNTERNET_CACHE_ENTRY_INFOA = (WinINETCache.INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(intPtr, typeof(WinINETCache.INTERNET_CACHE_ENTRY_INFOA)); num = num2; if (WinINETCache.WININETCACHEENTRYTYPE.COOKIE_CACHE_ENTRY != (iNTERNET_CACHE_ENTRY_INFOA.CacheEntryType & WinINETCache.WININETCACHEENTRYTYPE.COOKIE_CACHE_ENTRY)) { flag = WinINETCache.DeleteUrlCacheEntryA(iNTERNET_CACHE_ENTRY_INFOA.lpszSourceUrlName); } flag = WinINETCache.FindNextUrlCacheEntryExA(intPtr2, intPtr, ref num, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); lastWin32Error = Marshal.GetLastWin32Error(); if (!flag && 259 == lastWin32Error) { break; } if (!flag && num > num2) { num2 = num; intPtr = Marshal.ReAllocHGlobal(intPtr, (IntPtr)num2); flag = WinINETCache.FindNextUrlCacheEntryExA(intPtr2, intPtr, ref num, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } }while (flag); Marshal.FreeHGlobal(intPtr); FiddlerApplication.Log.LogString("Completed WinINET Cache clearing."); }
public static void ClearCookiesForHost(string sHost) { sHost = sHost.Trim(); if (sHost.Length < 1) { return; } string text; if (sHost == "*") { text = string.Empty; if (Environment.OSVersion.Version.Major > 5) { WinINETCache.VistaClearTracks(false, true); return; } } else { text = (sHost.StartsWith("*") ? sHost.Substring(1).ToLower() : ("@" + sHost.ToLower())); } int num = 0; IntPtr intPtr = IntPtr.Zero; IntPtr intPtr2 = IntPtr.Zero; intPtr2 = WinINETCache.FindFirstUrlCacheEntryA("cookie:", IntPtr.Zero, ref num); if (intPtr2 == IntPtr.Zero && 259 == Marshal.GetLastWin32Error()) { return; } int num2 = num; intPtr = Marshal.AllocHGlobal(num2); intPtr2 = WinINETCache.FindFirstUrlCacheEntryA("cookie:", intPtr, ref num); while (true) { WinINETCache.INTERNET_CACHE_ENTRY_INFOA iNTERNET_CACHE_ENTRY_INFOA = (WinINETCache.INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(intPtr, typeof(WinINETCache.INTERNET_CACHE_ENTRY_INFOA)); num = num2; if (WinINETCache.WININETCACHEENTRYTYPE.COOKIE_CACHE_ENTRY == (iNTERNET_CACHE_ENTRY_INFOA.CacheEntryType & WinINETCache.WININETCACHEENTRYTYPE.COOKIE_CACHE_ENTRY)) { bool flag; if (text.Length == 0) { flag = true; } else { string text2 = Marshal.PtrToStringAnsi(iNTERNET_CACHE_ENTRY_INFOA.lpszSourceUrlName); int num3 = text2.IndexOf('/'); if (num3 > 0) { text2 = text2.Remove(num3); } text2 = text2.ToLower(); flag = text2.EndsWith(text); } if (flag) { bool flag2 = WinINETCache.DeleteUrlCacheEntryA(iNTERNET_CACHE_ENTRY_INFOA.lpszSourceUrlName); } } while (true) { bool flag2; if (!(flag2 = WinINETCache.FindNextUrlCacheEntryA(intPtr2, intPtr, ref num))) { if (259 == Marshal.GetLastWin32Error()) { goto IL_19E; } } if (flag2 || num <= num2) { break; } num2 = num; intPtr = Marshal.ReAllocHGlobal(intPtr, (IntPtr)num2); } } IL_19E: Marshal.FreeHGlobal(intPtr); }