-
Notifications
You must be signed in to change notification settings - Fork 1
/
BootStrap.cs
177 lines (136 loc) · 5.44 KB
/
BootStrap.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
// originale code: https://github.com/sunilpottumuttu/FiddlerGenerateHttpClientCode
namespace HttpClientCode
{
using Fiddler;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Web;
using System.Windows.Forms;
public class BootStrap: IHandleExecAction, IFiddlerExtension
{
private MenuItem __menuItem;
private MenuItem __menuItemGenerate;
private MenuItem __menuItemNoHeader;
private Session __selectedSession;
private FrmGenerateHttpClientCode __frm;
private string MESSAGEBOXTEXT = "HttpClientCode";
public BootStrap()
{
}
public void OnLoad()
{
this.__menuItem = new MenuItem("Http Client Code");
this.__menuItemGenerate = new MenuItem("Generate code");
this.__menuItemNoHeader = new MenuItem("Add headers");
this.__menuItemNoHeader.Checked = false;
this.__menuItem.MenuItems.Add(this.__menuItemGenerate);
this.__menuItem.MenuItems.Add(this.__menuItemNoHeader);
FiddlerApplication.UI.lvSessions.ContextMenu.MenuItems.Add(this.__menuItem);
this.__menuItemGenerate.Click += new EventHandler(__menuItemGenerate_Click);
this.__menuItemNoHeader.Click += new EventHandler(__menuItemNoHeader_Click);
}
string CanHandle()
{
if (this.__selectedSession.isTunnel)
{
return "This Not a HTTP/HTTPS Request..Please Choose HTTP/HTTPS Request";
}
if (this.__selectedSession.isFTP)
{
return "This is a FTP Request...Please Choose HTTP/HTTPS Request";
}
return string.Empty;
}
void __menuItemNoHeader_Click(object sender, EventArgs e)
{
((MenuItem)sender).Checked = !((MenuItem)sender).Checked;
}
void __menuItemGenerate_Click(object sender, EventArgs e)
{
if (FiddlerApplication.UI.lvSessions.SelectedItems.Count == 1)
{
this.__selectedSession = FiddlerApplication.UI.GetFirstSelectedSession();
var result = this.CanHandle();
if (result == string.Empty)//Check whether this can handle or not
{
string text = this.GenerateHttpClientCode();
this.__frm = new FrmGenerateHttpClientCode();
this.__frm.SetText(text);
this.__frm.ShowDialog();
}
else
{
MessageBox.Show(result, this.MESSAGEBOXTEXT, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Select Only One Session", this.MESSAGEBOXTEXT, MessageBoxButtons.OK, MessageBoxIcon.Information);
return ;
}
}
public string GenerateHttpClientCode()
{
var template = new GenerateCode();
template.Session = new Dictionary<string, object>();
template.Session.Add("uri", (this.__selectedSession.isHTTPS ? "https" : "http") + "://" + this.__selectedSession.host + this.__selectedSession.PathAndQuery);
//template.Session.Add("host", this.__selectedSession.host);
template.Session.Add("httpmethod", this.__selectedSession.RequestMethod);
#region Add HttpHeaders
var headers = new Dictionary<string, string>();
if (this.__menuItemNoHeader.Checked)
{
foreach (var item in this.__selectedSession.oRequest.headers)
{
headers.Add(item.Name, item.Value);
}
}
#endregion
template.Session.Add("headers", headers);
string queryString = null;
//string path = this.__selectedSession.PathAndQuery;
if (this.__selectedSession.RequestMethod.ToUpperInvariant() == WebRequestMethods.Http.Post)
{
queryString = this.__selectedSession.GetRequestBodyAsString();
}
//template.Session.Add("uri", path);
var bodies = new Dictionary<string, string>();
#region Add bodies
if (!string.IsNullOrWhiteSpace(queryString))
{
NameValueCollection x = HttpUtility.ParseQueryString(queryString);
try
{
foreach (var item in x.AllKeys)
{
if (string.IsNullOrWhiteSpace(x[item]))
{
continue;
}
bodies.Add(item, x[item]);
}
}
catch
{
bodies.Clear();
}
}
#endregion
template.Session.Add("bodies", bodies);
template.Initialize();
var generatedCode = template.TransformText();
return generatedCode;
}
public bool OnExecAction(string sCommand)
{
return true;
}
public void OnBeforeUnload()
{
if (this.__frm != null)
{ this.__frm.Dispose(); }
}
}
}