Skip to content

Commit 532a561

Browse files
Bug 1988955 - [devtools] Update DevTools readPostDataFromRequest helper to expose text decoding errors r=devtools-reviewers,bomsy
Differential Revision: https://phabricator.services.mozilla.com/D270272
1 parent d0dcd93 commit 532a561

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

devtools/shared/network-observer/NetworkHelper.sys.mjs

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ export var NetworkHelper = {
123123
* Text to convert.
124124
* @param string charset
125125
* Charset to convert the text to.
126+
* @param boolean throwOnFailure
127+
* Whether exceptions should be bubbled up or swallowed. Defaults to
128+
* false.
126129
* @returns string
127130
* Converted text.
128131
*/
129-
convertToUnicode(text, charset) {
132+
convertToUnicode(text, charset, throwOnFailure) {
130133
// FIXME: We need to throw when text can't be converted e.g. the contents of
131134
// an image. Until we have a way to do so with TextEncoder and TextDecoder
132135
// we need to use nsIScriptableUnicodeConverter instead.
@@ -137,6 +140,9 @@ export var NetworkHelper = {
137140
conv.charset = charset || "UTF-8";
138141
return conv.ConvertToUnicode(text);
139142
} catch (ex) {
143+
if (throwOnFailure) {
144+
throw ex;
145+
}
140146
return text;
141147
}
142148
},
@@ -160,42 +166,65 @@ export var NetworkHelper = {
160166
},
161167

162168
/**
163-
* Reads the posted text from request.
169+
* Reads the post data from request.
164170
*
165171
* @param nsIHttpChannel request
166172
* @param string charset
167173
* The content document charset, used when reading the POSTed data.
168-
* @returns string or null
169-
* Returns the posted string if it was possible to read from request
170-
* otherwise null.
174+
*
175+
* @returns object or null
176+
* Returns an object with the following properties:
177+
* - {string|null} data: post data as string if it was possible to
178+
* read from request, otherwise null.
179+
* - {boolean} isDecodedAsText: if the data could be successfully read with
180+
* the specified charset.
181+
* Returns null if the channel does not implement nsIUploadChannel,
182+
* and therefore cannot upload a data stream.
171183
*/
172-
readPostTextFromRequest(request, charset) {
173-
if (request instanceof Ci.nsIUploadChannel) {
174-
const iStream = request.uploadStream;
184+
readPostDataFromRequest(request, charset) {
185+
if (!(request instanceof Ci.nsIUploadChannel)) {
186+
return null;
187+
}
188+
const iStream = request.uploadStream;
175189

176-
let isSeekableStream = false;
177-
if (iStream instanceof Ci.nsISeekableStream) {
178-
isSeekableStream = true;
179-
}
190+
let isSeekableStream = false;
191+
if (iStream instanceof Ci.nsISeekableStream) {
192+
isSeekableStream = true;
193+
}
180194

181-
let prevOffset;
182-
if (isSeekableStream) {
183-
prevOffset = iStream.tell();
184-
iStream.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
185-
}
195+
let prevOffset;
196+
if (isSeekableStream) {
197+
prevOffset = iStream.tell();
198+
iStream.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
199+
}
186200

187-
// Read data from the stream.
188-
const text = this.readAndConvertFromStream(iStream, charset);
201+
let data = null;
202+
let isDecodedAsText = true;
189203

190-
// Seek locks the file, so seek to the beginning only if necko hasn't
191-
// read it yet, since necko doesn't seek to 0 before reading (at lest
192-
// not till 459384 is fixed).
193-
if (isSeekableStream && prevOffset == 0) {
194-
iStream.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
195-
}
196-
return text;
204+
// Read data from the stream.
205+
try {
206+
data = lazy.NetUtil.readInputStreamToString(iStream, iStream.available());
207+
} catch {
208+
// If we failed to read the stream, assume there is no valid request post
209+
// data to display.
210+
return null;
197211
}
198-
return null;
212+
213+
// Decode the data as text with the provided charset.
214+
try {
215+
data = this.convertToUnicode(data, charset, true);
216+
} catch (err) {
217+
isDecodedAsText = false;
218+
}
219+
220+
// Seek locks the file, so seek to the beginning only if necko hasn't
221+
// read it yet, since necko doesn't seek to 0 before reading (at lest
222+
// not till 459384 is fixed).
223+
if (isSeekableStream && prevOffset == 0) {
224+
iStream.seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
225+
}
226+
227+
return { data, isDecodedAsText };
199228
},
200229

201230
/**

devtools/shared/network-observer/NetworkObserver.sys.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,13 +1225,13 @@ export class NetworkObserver {
12251225
return;
12261226
}
12271227

1228-
const sentBody = lazy.NetworkHelper.readPostTextFromRequest(
1228+
const sentBody = lazy.NetworkHelper.readPostDataFromRequest(
12291229
httpActivity.channel,
12301230
httpActivity.charset
12311231
);
12321232

12331233
if (sentBody !== null) {
1234-
httpActivity.sentBody = sentBody;
1234+
httpActivity.sentBody = sentBody.data;
12351235
}
12361236
}
12371237

0 commit comments

Comments
 (0)