-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntry.cs
202 lines (168 loc) · 5.68 KB
/
Entry.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
using System;
using System.Collections.Generic;
using System.Text;
using Fiddler;
using System.IO;
using System.Windows.Forms;
using WebApiDocumentCreator;
[assembly:Fiddler.RequiredVersion("4.6.2.0")]
public class Entry : Fiddler.IFiddlerExtension
{
//private static Dictionary<int, Session> _sessions = new Dictionary<int, Session>();
protected Page page { get; set; }
public void OnBeforeUnload()
{
}
public void OnLoad()
{
page = new Page();
var b = new Button();
b.Text = "Export";
b.Click += ExportButtonClicked;
page.browse.Click += Browse_Click;
page.useTemplate.CheckedChanged += UseTemplate_CheckedChanged;
page.Controls.Add(b);
page.Dock = DockStyle.Fill;
var tab = new TabPage();
tab.Text ="WebApi";
tab.Controls.Add(page);
FiddlerApplication.UI.tabsViews.TabPages.Add(tab);
}
protected void UseTemplate_CheckedChanged(object sender, EventArgs e)
{
page.outputTemplate.Enabled = (sender as CheckBox).Checked;
}
protected virtual void Browse_Click(object sender, EventArgs e)
{
FolderBrowserDialog d = new FolderBrowserDialog();
DialogResult dr = d.ShowDialog();
switch (dr)
{
case DialogResult.OK:
case DialogResult.Yes:
this.page.exportTo.Text = d.SelectedPath;
break;
default:
break;
}
d.Dispose();
}
protected virtual void ExportButtonClicked(object sender, EventArgs e)
{
Session[] sessions = FiddlerApplication.UI.GetSelectedSessions();
PopToFile(sessions);
}
private void PopToFile(Session[] sessions)
{
lock (FiddlerApplication.UI)
{
try
{
using (StreamWriter sw = new StreamWriter(page.exportTo.Text, true))
{
foreach (Session s in sessions)
{
sw.WriteLine(GetLine(s));
}
}
MessageBox.Show("Completed.");
}
catch (Exception e)
{
MessageBox.Show(string.Format("Failed: {0}", e.ToString()));
}
}
}
/// <summary>
/// Url Method ContentType RequestData RequestHeader ResponseData ResponseHeader StatusCode
/// RequestHeader:<headerName> ResponseHeaders:<headerName>
///
/// Example:
/// {url:(Url), status:(StatusCode), refer:(ResponseHeaders:Refer)}
/// </summary>
/// <param name="session"></param>
/// <returns></returns>
protected virtual string GetLine(Session session)
{
Dictionary<string, object> item = new Dictionary<string, object>();
item.Add("Url", session.fullUrl );
if (page.hasMethod.Checked)
{
item.Add("Method", session.RequestMethod );
}
if (page.hasContentType.Checked)
{
item.Add("ContentType", session.RequestHeaders["Content-Type"] );
}
if (page.hasRequestData.Checked)
{
item.Add("RequestData", Encoding.UTF8.GetString(session.RequestBody) );
}
if (page.hasRequestHeader.Checked)
{
item.Add("RequestHeader", session.RequestHeaders.ToString() );
}
if (page.hasResponseBody.Checked)
{
item.Add("ResponseData", Encoding.UTF8.GetString(session.ResponseBody) );
}
if (page.hasResponseHeader.Checked)
{
item.Add("ResponseHeader", session.ResponseHeaders.ToString() );
}
item.Add("StatusCode", session.responseCode.ToString() );
foreach (var key in session.RequestHeaders)
{
string _key = string.Format("RequestHeaders:{0}", key.Name);
if (item.ContainsKey(_key))
{
if (item[_key] is string)
{
string tmp = item[_key].ToString();
item[_key] = new List<string> { tmp, key.Value };
}
else if (item[_key] is List<string>)
{
(item[_key] as List<string>).Add(key.Value);
}
}
else
{
item.Add(_key, key.Value);
}
}
foreach (var key in session.ResponseHeaders)
{
string _key = string.Format("ResponseHeaders:{0}", key.Name);
if (item.ContainsKey(_key))
{
if (item[_key] is string)
{
string tmp = item[_key].ToString();
item[_key] = new List<string> { tmp, key.Value };
}
else if (item[_key] is List<string>)
{
(item[_key] as List<string>).Add(key.Value);
}
}
else
{
item.Add(_key, key.Value);
}
}
return CreateLine(item);
}
protected virtual string CreateLine(Dictionary<string, object> items)
{
if (page.useTemplate.Checked)
{
TemplateCompiler c = new TemplateCompiler();
return c.CompileWithTemplate(page.outputTemplate.Text, items);
}
else
{
return string.Format("{0}{1}", Newtonsoft.Json.JsonConvert.SerializeObject(items), Environment.NewLine);
}
}
}