/** * Fiddler Gzip stream inspector. * * Copyright (c) 2004, Rasmus Sten * You may redistribute and modify this program freely. * No warranties. * * http://dll.nu/fiddler-gzip/ * */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using ICSharpCode.SharpZipLib.GZip; namespace GzipInspector { public class ResponseDecoder: Inspector, IResponseInspector { TextBox myControl; private byte[] m_entityBody; private bool m_bDirty; private bool m_bReadOnly; HTTPResponseHeaders m_headers; public bool bReadOnly { get { return true; } set { m_bReadOnly = value; } } public void Clear() { m_entityBody = null; myControl.Text = ""; } public HTTPResponseHeaders headers { get { return null; } set { m_headers = value; } } public override void ShowAboutBox() { MessageBox.Show("Copyright (c) ", "about"); } public byte[] body { get { return m_entityBody; } set { try { m_entityBody = value; String encoding = m_headers != null ? m_headers["Content-Encoding"] : "gzip"; if (m_entityBody == null) { myControl.Text = ""; return; } if (encoding.Equals("gzip")) { byte[] unzippedData = unzip(m_entityBody); String s = System.Text.Encoding.UTF8.GetString(unzippedData); myControl.Text = s; } else { MessageBox.Show("Unknown encoding: " + encoding); } } catch (IOException ex1) { myControl.Text = ""; } catch (Exception ex1) { MessageBox.Show(ex1.ToString() + "\n" + ex1.StackTrace); } } } public bool bDirty { get { return m_bDirty; } } private byte[] unzip(byte[] data) { Stream rawstream = new MemoryStream(data, false); GZipInputStream stream = new GZipInputStream(rawstream); MemoryStream outputstream = new MemoryStream(); int tehbyte; while ((tehbyte = stream.ReadByte()) != -1) outputstream.WriteByte((byte) tehbyte); return outputstream.GetBuffer(); } public override void AddTab(System.Windows.Forms.TabPage o) { myControl = new TextBox(); o.Text = "Gzip"; myControl.Text = ""; myControl.Multiline = true; myControl.ReadOnly = true; myControl.ScrollBars = ScrollBars.Vertical; o.Controls.Add(myControl); o.Controls[0].Dock= DockStyle.Fill; } public override void Announce() { } } }