想要 瀏覽更多Fiddler內容:請點擊進入Fiddler官方文檔web
3、建立Fiddler擴展項目cookie
4、在擴展程序選項卡中添加一個圖標session
6、構建自定義檢查器less
7、導入器和導出器接口編輯器
9、將參數傳遞給Importer或Exporter Extension
使用Fiddler的可擴展性機制添加到Fiddler的UI,自動修改請求或響應,並建立自定義檢查器,以啓用特定於方案的顯示和手動修改請求和響應。
要使擴展可供計算機上的全部用戶使用,請將擴展程序集DLL安裝到:
%Program Files%\Fiddler2\Scripts
要使擴展僅對當前用戶可用,請將擴展程序集DLL安裝到:
%USERPROFILE%\My Documents\Fiddler2\Scripts
在AssemblyInfo.cs文件(或代碼中的其餘位置)中設置Fiddler.RequiredVersion屬性,以下所示:
using Fiddler; // Extension requires Fiddler 2.2.8.6+ because it uses types introduced in v2.2.8... [assembly: Fiddler.RequiredVersion("2.2.8.6")]
啓動Visual Studio 2005或更高版本。
建立一個Visual C#類庫類型的新項目。
在解決方案資源管理器中右鍵單擊項目的References文件夾。
單擊「 瀏覽」選項卡,而後在C:\ Program Files \ Fiddler2文件夾中選擇Fiddler.exe。
單擊「 肯定」以添加引用。
若是您的擴展程序修改了Fiddler的UI:
在「構建事件」選項卡上,將如下內容添加到「構建後事件」命令行:
複製「$(TargetPath)」「%userprofile%\ My Documents \ Fiddler2 \ Scripts \ $(TargetFilename)」
修改項目中的默認class1.cs(或建立一個新類),以下所示:
using System; using System.Windows.Forms; using Fiddler; [assembly: Fiddler.RequiredVersion("2.3.5.0")] public class Violin : IAutoTamper // Ensure class is public, or Fiddler won't see it! { string sUserAgent = ""; public Violin(){ /* NOTE: It's possible that Fiddler UI isn't fully loaded yet, so don't add any UI in the constructor. But it's also possible that AutoTamper* methods are called before OnLoad (below), so be sure any needed data structures are initialized to safe values here in this constructor */ sUserAgent = "Violin"; } public void OnLoad(){ /* Load your UI here */ } public void OnBeforeUnload() { } public void AutoTamperRequestBefore(Session oSession){ oSession.oRequest["User-Agent"] = sUserAgent; } public void AutoTamperRequestAfter(Session oSession){} public void AutoTamperResponseBefore(Session oSession){} public void AutoTamperResponseAfter(Session oSession){} public void OnBeforeReturningError(Session oSession){} }
實現Fiddler接口
2、
在Fiddler執行期間實現Fiddler接口以加載程序集。
實現IFiddlerExtension接口的程序集中的公共類將在啓動期間由Fiddler加載。
public interface IFiddlerExtension { // Called when Fiddler User Interface is fully available void OnLoad(); // Called when Fiddler is shutting down void OnBeforeUnload(); }
該的OnLoad當小提琴手加載完成其UI是徹底可用的功能將被調用。此時,您能夠安全地將菜單項,選項卡式頁面或其餘元素添加到Fiddler UI。
該OnBeforeUnload函數將被調用時,提琴手被關閉和卸載全部擴展。
爲每一個HTTP / HTTPS請求和響應調用實現IAutoTamper接口(擴展IFiddlerExtension)的擴展,從而啓用修改,日誌記錄或其餘操做。
警告:此接口中的函數在後臺非UI線程上調用。要更新UI,請使用 Invoke或 BeginInvoke更新UI。另請注意,能夠在調用 OnLoad事件以前調用IAutoTamper :: *函數 -Fiddler容許流量在UI徹底可用以前流動。
public interface IAutoTamper : IFiddlerExtension { // Called before the user can edit a request using the Fiddler Inspectors void AutoTamperRequestBefore(Session oSession); // Called after the user has had the chance to edit the request using the Fiddler Inspectors, but before the request is sent void AutoTamperRequestAfter(Session oSession); // Called before the user can edit a response using the Fiddler Inspectors, unless streaming. void AutoTamperResponseBefore(Session oSession); // Called after the user edited a response using the Fiddler Inspectors. Not called when streaming. void AutoTamperResponseAfter(Session oSession); // Called Fiddler returns a self-generated HTTP error (for instance DNS lookup failed, etc) void OnBeforeReturningError(Session oSession); }
當響應頭可用時,將調用實現IAutoTamper2接口(擴展IAutoTamper)的擴展。
/// <summary> /// Interface for AutoTamper extensions that want to "peek" at response headers /// </summary> public interface IAutoTamper2 : IAutoTamper { /// <summary> /// Called when the response headers become available /// </summary> /// <param name="oSession">The Session object for which the response headers are available</param> void OnPeekAtResponseHeaders(Session oSession); }
當請求標頭可用時,將調用實現IAutoTamper3接口(擴展IAutoTamper2)的擴展。
/// <summary> /// Interface for AutoTamper extensions that want to "peek" at request headers /// </summary> public interface IAutoTamper3 : IAutoTamper2 { /// <summary> /// Called when the request headers become available /// </summary> /// <param name="oSession">The Session object for which the request headers are available</param> void OnPeekAtRequestHeaders(Session oSession); }
當用戶在QuickExec框中輸入命令時,將調用實現IHandleExecAction接口的擴展。要對命令做出反應(並防止其餘擴展和Fiddler自己進一步處理),請今後方法返回true。
public interface IHandleExecAction { // return TRUE if handled. bool OnExecAction(string sCommand); }
Fiddler.Utilities類包含一個輔助函數Parameterize(),它有助於解釋sCommand參數。
[CodeDescription("Tokenize a string into tokens. Delimits on whitespace; Quotation marks are dropped unless preceded by a \ character.")] public static string[] Parameterize(string sCommand)
按照如下步驟建立示例Fiddler擴展,修改全部出站請求的User-Agent字符串:
啓動Visual Studio 2005或更高版本。
建立一個Visual C#類庫類型的新項目。
在解決方案資源管理器中右鍵單擊項目的References文件夾。
單擊「 瀏覽」選項卡,而後在C:\ Program Files \ Fiddler2文件夾中選擇Fiddler.exe。
單擊「 肯定」以添加引用。
若是您的擴展程序修改了Fiddler的UI:
再次右鍵單擊解決方案資源管理器中項目的References文件夾。
在.NET選項卡上,選擇System.Windows.Forms。
單擊「 肯定」以添加引用。
在解決方案資源管理器中,右鍵單擊該項目。
單擊屬性。
單擊「 構建事件」選項卡。
將如下內容添加到Post-build事件命令行:
copy "$(TargetPath)" "%userprofile%\My Documents\Fiddler2\Scripts\$(TargetFilename)"
修改項目中的默認class1.cs(或建立一個新類),以下所示:
using System; using System.Windows.Forms; using Fiddler; [assembly: Fiddler.RequiredVersion("2.3.5.0")] public class Violin : IAutoTamper // Ensure class is public, or Fiddler won't see it! { string sUserAgent = ""; public Violin(){ /* NOTE: It's possible that Fiddler UI isn't fully loaded yet, so don't add any UI in the constructor. But it's also possible that AutoTamper* methods are called before OnLoad (below), so be sure any needed data structures are initialized to safe values here in this constructor */ sUserAgent = "Violin"; } public void OnLoad(){ /* Load your UI here */ } public void OnBeforeUnload() { } public void AutoTamperRequestBefore(Session oSession){ oSession.oRequest["User-Agent"] = sUserAgent; } public void AutoTamperRequestAfter(Session oSession){} public void AutoTamperResponseBefore(Session oSession){} public void AutoTamperResponseAfter(Session oSession){} public void OnBeforeReturningError(Session oSession){} }
請參閱Fiddler接口。
設置.ImageIndex屬性,以下所示:
public void OnLoad() { oPage = new TabPage("Timeline"); oPage.ImageIndex = (int)Fiddler.SessionIcons.Timeline; oView = new TimelineView(); oPage.Controls.Add(oView); oView.Dock = DockStyle.Fill; FiddlerApplication.UI.tabsViews.TabPages.Add(oPage); }
將圖像添加到imglSessionIcons。
設置.ImageIndex屬性,以下所示:
public void OnLoad() { oPage = new TabPage("Timeline"); oPage.ImageIndex = (int)Fiddler.SessionIcons.Timeline; oView = new TimelineView(); oPage.Controls.Add(oView); oView.Dock = DockStyle.Fill; FiddlerApplication.UI.tabsViews.TabPages.Add(oPage); }
編譯您的項目。
將程序集.DLL複製到正確的Scripts文件夾:
使用\ My Documents \ Fiddler2 \ Scripts使擴展可供當前用戶使用。
使用\ Program Files \ Fiddler2 \ Scripts使擴展可供計算機上的全部用戶使用。
重啓Fiddler。
建立一個Fiddler擴展項目。
更改代碼以從Inspector2類派生並實現IResponseInspector2或IRequestInspector2。
using Fiddler; [assembly: Fiddler.RequiredVersion("2.3.0.0")] public class WebViewer: Inspector2, IResponseInspector2 { public Viewers() { // // TODO: Add constructor logic here // } }
在課堂內,建立一個新方法。經過鍵入公共覆蓋,您將得到須要編寫的方法的自動完成列表。
在解決方案資源管理器中,右鍵單擊項目,而後單擊添加>用戶控件。
使用工具箱將控件添加到用戶控件。這些將顯示有關正在檢查的HTTP消息的數據。
在body {set}和headers {set}屬性中,您應該更新控件的請求或響應的可視化表示。
目前,在MAIN UI線程上調用ISessionImporter和ISessionExporter接口。這幾乎確定會在未來發生變化,所以您應該確保您的類是線程安全的,而且他們不會嘗試直接操做Fiddler UI。
對Fiddler UI的操縱仍是不明智的,由於Fiddler自己可能沒法加載; FiddlerCore可能直接託管您的進口商/出口商。爲了支持FiddlerCore,建議您在dictOptions參數中支持Filename鍵(具備徹底限定路徑的字符串值),並考慮支持Silent鍵(值爲boolean)。
當用戶使用「 文件」>「導入」菜單選項時,將調用實現ISessionImporter接口(實現IDisposable接口)的擴展。
public interface ISessionImporter : IDisposable { Session[] ImportSessions(string sImportFormat, Dictionary<string, object> dictOptions, EventHandler<ProgressCallbackEventArgs> evtProgressNotifications); }
該方法返回從導入數據建立的Session對象數組。
所述dictOptions字典能夠爲空,也能夠含有一組字符串鍵控對象。大多數進口商都支持文件名的規範。例如:
dictOptions["Filename"] = "C:\\test.file"
此類由Fiddler定義,容許您報告導入或導出操做的進度。
若是沒法肯定完成率,只需傳遞0或0到1.0之間的「猜想」。
若是在傳遞給evtProgressNotifications回調後在ProgressCallbackEventArgs對象上設置了Cancel標誌,則導入或導出應該儘快正常終止。
public class ProgressCallbackEventArgs: EventArgs { public ProgressCallbackEventArgs(float flCompletionRatio, string sProgressText) public string ProgressText { get; } public string PercentComplete { get; } public bool Cancel { get; set; } }
構建自定義導入程序或導出程序
8、
建立一個Fiddler擴展項目。
修改項目中的默認class1.cs(或建立一個新類),以下所示:
using System; using System.IO; using System.Text; using System.Windows.Forms; using Fiddler; using System.Diagnostics; using System.Reflection; [assembly: AssemblyVersion("1.0.0.0")] [assembly: Fiddler.RequiredVersion("2.4.0.0")] [ProfferFormat("TAB-Separated Values", "Session List in Tab-Delimited Format")] [ProfferFormat("Comma-Separated Values", "Session List in Comma-Delimited Format; import into Excel or other tools")] public class CSVTranscoder: ISessionExporter // Ensure class is public, or Fiddler won't see it! { public bool ExportSessions(string sFormat, Session[] oSessions, Dictionary<string, object> dictOptions, EventHandler<ProgressCallbackEventArgs> evtProgressNotifications) { bool bResult = false; string chSplit; // Determine if we already have a filename from the dictOptions collection string sFilename = null; if (null != dictOptions && dictOptions.ContainsKey("Filename")) { sFilename = dictOptions["Filename"] as string; } if (sFormat == "Comma-Separated Values") { chSplit = ","; if (string.IsNullOrEmpty(sFilename)) sFilename = Fiddler.Utilities.ObtainSaveFilename("Export As " + sFormat, "CSV Files (*.csv)|*.csv"); } else { chSplit = "\t"; if (string.IsNullOrEmpty(sFilename)) sFilename = Fiddler.Utilities.ObtainSaveFilename("Export As " + sFormat, "TSV Files (*.tsv)|*.tsv"); } if (String.IsNullOrEmpty(sFilename)) return false; try { StreamWriter swOutput = new StreamWriter(sFilename, false, Encoding.UTF8); int iCount = 0; int iMax = oSessions.Length; #region WriteColHeaders bool bFirstCol = true; foreach (ColumnHeader oLVCol in FiddlerApplication.UI.lvSessions.Columns) { if (!bFirstCol) { swOutput.Write(chSplit); } else { bFirstCol = false; } swOutput.Write(oLVCol.Text.Replace(chSplit, "")); } swOutput.WriteLine(); #endregion WriteColHeaders #region WriteEachSession foreach (Session oS in oSessions) { iCount++; if (null != oS.ViewItem) { bFirstCol = true; ListViewItem oLVI = (oS.ViewItem as ListViewItem); if (null == oLVI) continue; foreach (ListViewItem.ListViewSubItem oLVC in oLVI.SubItems) { if (!bFirstCol) { swOutput.Write(chSplit); } else { bFirstCol = false; } swOutput.Write(oLVC.Text.Replace(chSplit,""));} swOutput.WriteLine();}if(null!= evtProgressNotifications){ evtProgressNotifications(null,newProgressCallbackEventArgs(,));ProgressCallbackEventArgs PCEA =newProgressCallbackEventArgs((iCount/(float)iMax),"wrote "+ iCount.ToString()+" records."); evtProgressNotifications(null, PCEA);if(PCEA.Cancel){ swOutput.Close();returnfalse;}}}#endregion WriteEachSession swOutput.Close(); bResult =true;}catch(Exception eX){MessageBox.Show(eX.Message,"Failed to export"); bResult =false;}}return bResult;}publicvoidDispose(){}}
9、將參數傳遞給Importer或Exporter Extension
轉碼器(實現導入器或導出器接口的對象)能夠在字典對象中傳遞參數。例如,FiddlerScript能夠調用HTTPArchive轉碼器,傳遞文件名字符串和最大響應大小整數,以下所示:
var oSessions = FiddlerApplication.UI.GetAllSessions(); var oExportOptions = FiddlerObject.createDictionary(); oExportOptions.Add("Filename", "C:\\users\\ericlaw\\desktop\\out1.har"); oExportOptions.Add("MaxTextBodyLength", 1024); oExportOptions.Add("MaxBinaryBodyLength", 16384); FiddlerApplication.DoExport("HTTPArchive v1.2", oSessions, oExportOptions, null);
代碼轉換器擴展能夠按以下方式收集這些選項:
public bool ExportSessions(string sFormat, Session[] oSessions, Dictionary<string, object> dictOptions, EventHandler<ProgressCallbackEventArgs> evtProgressNotifications) { //... if (null != dictOptions) { if (dictOptions.ContainsKey("Filename")) { sFilename = dictOptions["Filename"] as string; } if (dictOptions.ContainsKey("MaxTextBodyLength")) { iMaxTextBodyLength = (int)dictOptions["MaxTextBodyLength"]; } if (dictOptions.ContainsKey("MaxBinaryBodyLength")) { iMaxBinaryBodyLength = (int)dictOptions["MaxBinaryBodyLength"]; } }
若是您但願擴展程序集在Fiddler2和Fiddler4中運行,請爲.NET Framework v2構建它,並避免對在更高版本的Framework中刪除或移動的任何類具備任何依賴性。(我所知道的惟一一個實例是Microsoft JScript.NET代碼編譯器,其類移動了一下)。
您還須要確保若是使用任何不推薦使用的方法(例如,使用帶有Evidence參數的重載調用Assembly.LoadFrom),則只能有條件地執行此操做。例如:
if (CONFIG.bRunningOnCLRv4) { a = Assembly.LoadFrom(oFile.FullName); } else { a = Assembly.LoadFrom(oFile.FullName, evidenceFiddler); }
來自Fiddler網站的全部擴展都是針對Fiddler v2編譯的。
或者,您能夠簡單地構建兩個版本的DLL,一個版本針對.NET Framework v4,另外一個針對.NET Framework v2。
這就是Fiddler自己的構建方式。基本上,只需將v2目標項目的「克隆」版本添加到同一解決方案中。使用「 添加」>「現有項」上下文菜單將.CS文件從以v2爲目標的項目添加到以v4爲目標的項目,但在選擇文件時,請務必使用文件選取器對話框中的拆分按鈕,而後選擇「 添加爲」。連接。在v4項目的「 屬性」>「構建」選項卡上,添加像DOTNET4這樣的條件編譯符號。而後,您能夠將任何特定於.NETv4的代碼置於條件編譯以後:
#if DOTNET4 // ... code targeting .NETv4 #else // ... code targeting .NETv2 #endif
您的擴展程序可能會根據在其中找到的InstalledVersion註冊表項的內容安裝適當目標的版本:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fiddler2
.NET2版的Fiddler目前比.NETv4版本更受歡迎。當.NET Framework v4.5發佈時,我可能會將v4項目移到v4.5。除此以外,這將容許我在後面的框架中利用新的內置.ZIP類。
RequiredVersion屬性怎麼樣?
Fiddler v4是「聰明的」 - 若是您的擴展指定
[assembly: Fiddler.RequiredVersion("2.1.0.1")]
當Fiddler v4加載它時,它將須要4.3.9.9或更高版本。
示例擴展11、
要查看一些示例擴展,請查看Fiddler Add-Ons頁面或Privacy Scanner Add-On代碼。
要將自定義列添加到Fiddler UI,修改請求或響應,測試應用程序性能以及各類其餘自定義任務,請在FiddlerScript中向Fiddler的JScript.NET CustomRules.js文件添加規則。
按此規則>自定義規則...。
在相應的函數內輸入FiddlerScript代碼。
保存文件。
Fiddler會自動從新加載規則。
要在腳本中使用其餘.NET程序集:
單擊工具>提琴選項。
單擊「 擴展」選項卡。
編輯參考列表。
或者:
在GAC中註冊程序集; 要麼
將程序集複製到包含Fiddler.exe的文件夾。
要在不徹底限定它們的狀況下使用新程序集的函數,請更新腳本頂部的#import子句。
單擊工具>提琴選項。
編輯編輯器字符串。
刪除〜/ Documents / Fiddler2 / Scripts中的CustomRules.js文件。
重啓Fiddler。
注意:Fiddler的默認規則存儲在〜/ Program Files / Fiddler2 / Scripts / SampleRules.js中。
要將菜單操做添加到「 工具」菜單或上下文菜單,或者將選項添加到「 規則」菜單:
建立並執行.REG文件,以下所示:
[HKEY_CURRENT_USER\Software\Microsoft\Fiddler2\MenuExt\&YourMenuItemName] "Command"="YourExeName.exe" "Parameters"="Your Parameters To Pass To The EXE"
重啓Fiddler。
要使用.NET插件(對於此示例,修改用戶代理字符串的C#插件):
關閉提琴手。
保存.NET文件(例如,此文件名爲UASimulator.cs):
using System; using System.Windows.Forms; using Fiddler; namespace FiddlerUtility{ public class UASimulator { string m_sUAString; public UASimulator(string s_UAString){ m_sUAString = s_UAString; } public bool OverwriteUA(Session oSession){ oSession.oRequest["User-Agent"] = m_sUAString; return true; } } }
在VS命令提示符下,轉到找到.CS文件的文件夾。
輸入命令以在VS命令提示符中建立DLL。例如:
csc /target:library /out:c:\UASim.dll UASimulator.cs /reference:"C:\program files\fiddler2\fiddler.exe"
在Fiddler中,單擊工具> Fiddler選項。
單擊「 擴展」選項卡。
在「 引用」字段中,輸入DLL的位置。例如:
C:\UASim.dll
向Fiddler添加規則以更新腳本。例如:
import System; import System.Windows.Forms; import Fiddler; import FiddlerUtility; class Handlers{ static var UASim = new UASimulator("Mozilla/12.0"); static function OnBeforeRequest(oSession:Fiddler.Session){ UASim.OverwriteUA(oSession); } static function Main(){ var today: Date = new Date(); FiddlerObject.StatusText = " CustomRules.js was loaded at: " + today; } }
構建Cookie掃描擴展
十5、
如下是Fiddler Privacy Scanner插件的代碼。
using System; using System.Collections; using System.Globalization; using System.Collections.Generic; using System.Windows.Forms; using System.Text; using Fiddler; using System.IO; using System.Diagnostics; using Microsoft.Win32; using System.Reflection; using System.Text.RegularExpressions; [assembly: Fiddler.RequiredVersion("2.3.9.0")] [assembly: AssemblyVersion("1.0.1.0")] [assembly: AssemblyTitle("PrivacyScanner")] [assembly: AssemblyDescription("Scans for Cookies and P3P")] [assembly: AssemblyCompany("Eric Lawrence")] [assembly: AssemblyProduct("PrivacyScanner")] public class TagCookies : IAutoTamper2 { private bool bEnabled = false; private bool bEnforceP3PValidity = false; private bool bCreatedColumn = false; private System.Windows.Forms.MenuItem miEnabled; private System.Windows.Forms.MenuItem miEnforceP3PValidity; private System.Windows.Forms.MenuItem mnuCookieTag; public void OnLoad() { /* * NB: You might not get called here until ~after~ one of the AutoTamper methods was called. * This is okay for us, because we created our mnuContentBlock in the constructor and its simply not * visible anywhere until this method is called and we merge it onto the Fiddler Main menu. */ FiddlerApplication.UI.mnuMain.MenuItems.Add(mnuCookieTag); } public void OnBeforeUnload() { /*noop*/ } private void InitializeMenu() { this.miEnabled = new System.Windows.Forms.MenuItem("&Enabled"); this.miEnforceP3PValidity = new System.Windows.Forms.MenuItem("&Rename P3P header if invalid"); this.miEnabled.Index = 0; this.miEnforceP3PValidity.Index = 1; this.mnuCookieTag = new System.Windows.Forms.MenuItem(); this.mnuCookieTag.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.miEnabled, this.miEnforceP3PValidity }); this.mnuCookieTag.Text = "Privacy"; this.miEnabled.Click += new System.EventHandler(this.miEnabled_Click); this.miEnabled.Checked = bEnabled; this.miEnforceP3PValidity.Click += new System.EventHandler(this.miEnforceP3PValidity_Click); this.miEnforceP3PValidity.Checked = bEnforceP3PValidity; } public void miEnabled_Click(object sender, EventArgs e) { miEnabled.Checked = !miEnabled.Checked; bEnabled = miEnabled.Checked; this.miEnforceP3PValidity.Enabled = bEnabled; if (bEnabled) { EnsureColumn(); } FiddlerApplication.Prefs.SetBoolPref("extensions.tagcookies.enabled", bEnabled); }publicvoid miEnforceP3PValidity_Click(object sender,EventArgs e){ miEnforceP3PValidity.Checked=!miEnforceP3PValidity.Checked; bEnforceP3PValidity = miEnforceP3PValidity.Checked;FiddlerApplication.Prefs.SetBoolPref("extensions.tagcookies.EnforceP3PValidity", bEnforceP3PValidity);}privatevoidEnsureColumn(){if(bCreatedColumn)return;FiddlerApplication.UI.lvSessions.AddBoundColumn("Privacy Info",1,120,"X-Privacy"); bCreatedColumn =true;}publicTagCookies(){this.bEnabled =FiddlerApplication.Prefs.GetBoolPref("extensions.tagcookies.enabled",false);this.bEnforceP3PValidity =FiddlerApplication.Prefs.GetBoolPref("extensions.tagcookies.EnforceP3PValidity",true);InitializeMenu();if(bEnabled){EnsureColumn();}else{this.miEnforceP3PValidity.Enabled=false;}}privatevoidSetP3PStateFromHeader(string sValue,ref P3PState oP3PState){if(string.IsNullOrEmpty(sValue)){return;}string sUnsatCat =String.Empty;string sUnsatPurpose =String.Empty; sValue = sValue.Replace('\'','"');string sCP =null;Regex r =newRegex("CP\\s?=\\s?[\"]?(?<TokenValue>[^\";]*)");Match m = r.Match(sValue);if(m.Success&&(null!= m.Groups["TokenValue"])){ sCP = m.Groups["TokenValue"].Value;}if(String.IsNullOrEmpty(sCP)){return;}// Okay, we've got a compact policy token. oP3PState = P3PState.P3POk;string[] sTokens = sCP.Split(newchar[]{' '},StringSplitOptions.RemoveEmptyEntries);foreach(string sToken in sTokens){// Reject clearly invalid tokens...if((sToken.Length<3)||(sToken.Length>4)){ oP3PState = P3PState.P3PMalformed;return;}if(",PHY,ONL,GOV,FIN,".IndexOf(","+ sToken +",",StringComparison.OrdinalIgnoreCase)>-1){ sUnsatCat +=(sToken +" ");continue;}if(",SAM,OTR,UNR,PUB,IVA,IVD,CON,TEL,OTP,".IndexOf(","+ sToken +",",StringComparison.OrdinalIgnoreCase)>-1){ sUnsatPurpose +=(sToken +" ");continue;}// TODO: Look up the token in the complete collection and check validity}// If a cookie contains an unsatisfactory purpose and an unsatisfactory category, mark it// https://msdn.microsoft.com/en-us/library/ie/ms537343(v=vs.85).aspx#unsatisfactory_cookiesif((sUnsatCat.Length>0)&&(sUnsatPurpose.Length>0)){if(oP3PState == P3PState.P3POk){ oP3PState = P3PState.P3PUnsatisfactory;}}}privateenum P3PState {NoCookies,NoP3PAndSetsCookies, P3POk, P3PUnsatisfactory, P3PMalformed }publicvoidOnPeekAtResponseHeaders(Session oSession){if(!bEnabled)return; P3PState oP3PState = P3PState.NoCookies;if(!oSession.oResponse.headers.Exists("Set-Cookie")){return;} oP3PState = P3PState.NoP3PAndSetsCookies;if(oSession.oResponse.headers.Exists("P3P")){SetP3PStateFromHeader(oSession.oResponse.headers["P3P"],ref oP3PState);}switch(oP3PState){case P3PState.P3POk: oSession["ui-backcolor"]="#ACDC85"; oSession["X-Privacy"]="Sets cookies & P3P";break;case P3PState.NoP3PAndSetsCookies: oSession["ui-backcolor"]="#FAFDA4"; oSession["X-Privacy"]="Sets cookies without P3P";break;case P3PState.P3PUnsatisfactory: oSession["ui-backcolor"]="#EC921A"; oSession["X-Privacy"]="Sets cookies; P3P unsatisfactory for 3rd-party use";break;case P3PState.P3PMalformed: oSession["ui-backcolor"]="#E90A05";if(bEnforceP3PValidity){ oSession.oResponse.headers["MALFORMED-P3P"]= oSession.oResponse.headers["P3P"]; oSession["X-Privacy"]="MALFORMED P3P: "+ oSession.oResponse.headers["P3P"]; oSession.oResponse.headers.Remove("P3P");}break;}}publicvoidAutoTamperRequestBefore(Session oSession){}publicvoidAutoTamperRequestAfter(Session oSession){/*noop*/}publicvoidAutoTamperResponseAfter(Session oSession){/*noop*/}publicvoidAutoTamperResponseBefore(Session oSession){/*noop*/}publicvoidOnBeforeReturningError(Session oSession){/*noop*/}}
十3、添加菜單項: