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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {
Component,
} = require("resource://devtools/client/shared/vendor/react.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const {
connect,
} = require("resource://devtools/client/shared/redux/visibility-handler-connect.js");
const {
L10N,
} = require("resource://devtools/client/netmonitor/src/utils/l10n.js");
const {
fetchNetworkUpdatePacket,
} = require("resource://devtools/client/netmonitor/src/utils/request-utils.js");
const Actions = require("resource://devtools/client/netmonitor/src/actions/index.js");
const {
getSelectedRequest,
} = require("resource://devtools/client/netmonitor/src/selectors/index.js");
const {
getUrlQuery,
parseQueryString,
writeHeaderText,
} = require("resource://devtools/client/netmonitor/src/utils/request-utils.js");
const { button, div, input, label, textarea } = dom;
const CUSTOM_CANCEL = L10N.getStr("netmonitor.custom.cancel");
const CUSTOM_HEADERS = L10N.getStr("netmonitor.custom.headers");
const CUSTOM_NEW_REQUEST = L10N.getStr("netmonitor.custom.newRequest");
const CUSTOM_NEW_REQUEST_METHOD_LABEL = L10N.getStr(
"netmonitor.custom.newRequestMethodLabel"
);
const CUSTOM_NEW_REQUEST_URL_LABEL = L10N.getStr(
"netmonitor.custom.newRequestUrlLabel"
);
const CUSTOM_POSTDATA = L10N.getStr("netmonitor.custom.postData");
const CUSTOM_QUERY = L10N.getStr("netmonitor.custom.query");
const CUSTOM_SEND = L10N.getStr("netmonitor.custom.send");
/*
* Custom request panel component
* A network request editor which simply provide edit and resend interface
* for network development.
*/
class CustomRequestPanel extends Component {
static get propTypes() {
return {
connector: PropTypes.object.isRequired,
removeSelectedCustomRequest: PropTypes.func.isRequired,
request: PropTypes.object.isRequired,
sendCustomRequest: PropTypes.func.isRequired,
updateRequest: PropTypes.func.isRequired,
};
}
componentDidMount() {
const { request, connector } = this.props;
this.initialRequestMethod = request.method;
fetchNetworkUpdatePacket(connector.requestData, request, [
"requestHeaders",
"responseHeaders",
"requestPostData",
]);
}
// FIXME: https://bugzilla.mozilla.org/show_bug.cgi?id=1774507
UNSAFE_componentWillReceiveProps(nextProps) {
const { request, connector } = nextProps;
fetchNetworkUpdatePacket(connector.requestData, request, [
"requestHeaders",
"responseHeaders",
"requestPostData",
]);
}
/**
* Parse a text representation of a name[divider]value list with
* the given name regex and divider character.
*
* @param {string} text - Text of list
* @return {array} array of headers info {name, value}
*/
parseRequestText(text, namereg, divider) {
const regex = new RegExp(`(${namereg})\\${divider}\\s*(\\S.*)`);
const pairs = [];
for (const line of text.split("\n")) {
const matches = regex.exec(line);
if (matches) {
const [, name, value] = matches;
pairs.push({ name, value });
}
}
return pairs;
}
/**
* Update Custom Request Fields
*
* @param {Object} evt click event
* @param {Object} request current request
* @param {updateRequest} updateRequest action
*/
updateCustomRequestFields(evt, request, updateRequest) {
const val = evt.target.value;
let data;
switch (evt.target.id) {
case "custom-headers-value":
data = {
requestHeaders: {
customHeadersValue: val || "",
// Parse text representation of multiple HTTP headers
headers: this.parseRequestText(val, "\\S+?", ":"),
},
};
break;
case "custom-method-value":
// If val is empty when leaving the "method" field, set the method to
// its original value
data =
evt.type === "blur" && val === ""
? { method: this.initialRequestMethod }
: { method: val.trim() };
break;
case "custom-postdata-value":
// Update "content-length" header value to reflect change
// in post data field.
const { requestHeaders } = request;
const newHeaders = requestHeaders.headers.map(header => {
if (header.name.toLowerCase() == "content-length") {
return {
name: header.name,
value: val.length,
};
}
return header;
});
data = {
requestPostData: {
postData: { text: val },
},
requestHeaders: {
headers: newHeaders,
},
};
break;
case "custom-query-value":
let customQueryValue = val || "";
// Parse readable text list of a query string
const queryArray = customQueryValue
? this.parseRequestText(customQueryValue, ".+?", "=")
: [];
// Write out a list of query params into a query string
const queryString = queryArray
.map(({ name, value }) => name + "=" + value)
.join("&");
const url = queryString
? [request.url.split("?")[0], queryString].join("?")
: request.url.split("?")[0];
// Remove temp customQueryValue while query string is parsable
if (
customQueryValue === "" ||
queryArray.length === customQueryValue.split("\n").length
) {
customQueryValue = null;
}
data = {
customQueryValue,
url,
};
break;
case "custom-url-value":
data = {
customQueryValue: null,
url: val,
};
break;
default:
break;
}
if (data) {
// All updateRequest batch mode should be disabled to make UI editing in sync
updateRequest(request.id, data, false);
}
}
render() {
const {
removeSelectedCustomRequest,
request = {},
sendCustomRequest,
updateRequest,
} = this.props;
const { method, customQueryValue, requestHeaders, requestPostData, url } =
request;
let headers = "";
if (requestHeaders) {
headers = requestHeaders.customHeadersValue
? requestHeaders.customHeadersValue
: writeHeaderText(requestHeaders.headers).trim();
}
const queryArray = url ? parseQueryString(getUrlQuery(url)) : [];
let params = customQueryValue;
if (!params) {
params = queryArray
? queryArray.map(({ name, value }) => name + "=" + value).join("\n")
: "";
}
const postData = requestPostData?.postData.text
? requestPostData.postData.text
: "";
return div(
{ className: "custom-request-panel" },
div(
{ className: "custom-request-label custom-header" },
CUSTOM_NEW_REQUEST
),
div(
{ className: "custom-request-panel-content" },
div(
{ className: "tabpanel-summary-container custom-request" },
div(
{ className: "custom-request-button-container" },
button(
{
className: "devtools-button",
id: "custom-request-close-button",
onClick: removeSelectedCustomRequest,
},
CUSTOM_CANCEL
),
button(
{
className: "devtools-button",
id: "custom-request-send-button",
onClick: sendCustomRequest,
},
CUSTOM_SEND
)
)
),
div(
{
className: "tabpanel-summary-container custom-method-and-url",
id: "custom-method-and-url",
},
label(
{
className: "custom-method-value-label custom-request-label",
htmlFor: "custom-method-value",
},
CUSTOM_NEW_REQUEST_METHOD_LABEL
),
input({
className: "custom-method-value",
id: "custom-method-value",
onChange: evt =>
this.updateCustomRequestFields(evt, request, updateRequest),
onBlur: evt =>
this.updateCustomRequestFields(evt, request, updateRequest),
value: method,
}),
label(
{
className: "custom-url-value-label custom-request-label",
htmlFor: "custom-url-value",
},
CUSTOM_NEW_REQUEST_URL_LABEL
),
input({
className: "custom-url-value",
id: "custom-url-value",
onChange: evt =>
this.updateCustomRequestFields(evt, request, updateRequest),
value: url || "http://",
})
),
// Hide query field when there is no params
params
? div(
{
className: "tabpanel-summary-container custom-section",
id: "custom-query",
},
label(
{
className: "custom-request-label",
htmlFor: "custom-query-value",
},
CUSTOM_QUERY
),
textarea({
className: "tabpanel-summary-input",
id: "custom-query-value",
onChange: evt =>
this.updateCustomRequestFields(evt, request, updateRequest),
rows: 4,
value: params,
wrap: "off",
})
)
: null,
div(
{
id: "custom-headers",
className: "tabpanel-summary-container custom-section",
},
label(
{
className: "custom-request-label",
htmlFor: "custom-headers-value",
},
CUSTOM_HEADERS
),
textarea({
className: "tabpanel-summary-input",
id: "custom-headers-value",
onChange: evt =>
this.updateCustomRequestFields(evt, request, updateRequest),
rows: 8,
value: headers,
wrap: "off",
})
),
div(
{
id: "custom-postdata",
className: "tabpanel-summary-container custom-section",
},
label(
{
className: "custom-request-label",
htmlFor: "custom-postdata-value",
},
CUSTOM_POSTDATA
),
textarea({
className: "tabpanel-summary-input",
id: "custom-postdata-value",
onChange: evt =>
this.updateCustomRequestFields(evt, request, updateRequest),
rows: 6,
value: postData,
wrap: "off",
})
)
)
);
}
}
module.exports = connect(
state => ({ request: getSelectedRequest(state) }),
dispatch => ({
removeSelectedCustomRequest: () =>
dispatch(Actions.removeSelectedCustomRequest()),
sendCustomRequest: () => dispatch(Actions.sendCustomRequest()),
updateRequest: (id, data, batch) =>
dispatch(Actions.updateRequest(id, data, batch)),
})
)(CustomRequestPanel);
|