Skip to content

Commit 93372c5

Browse files
committed
HAR: extended content is always base64-encoded + minor formatting fixes + minor improvement for base64 util
1 parent f5ec9ee commit 93372c5

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

src/components/intercept/config/android-device-config.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ const Spacer = styled.div`
6464
`;
6565

6666
function urlSafeBase64(content: string) {
67-
return stringToBuffer(content)
68-
.toString('base64')
69-
.replace(/\+/g, '-')
70-
.replace(/\//g, '_');
67+
return stringToBuffer(content).toString('base64url');
7168
}
7269

7370
function getConfigRequestIds(eventsStore: EventsStore) {

src/model/http/har.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ interface HarLog extends HarFormat.Log {
5151
export type RequestContentData = {
5252
text: string;
5353
size: number;
54-
encoding?: 'base64';
54+
encoding: 'base64';
5555
comment?: string;
5656
};
5757

5858
export interface ExtendedHarRequest extends HarFormat.Request {
5959
_requestBodyStatus?:
60-
| 'discarded:too-large'
61-
| 'discarded:not-representable'
62-
| 'discarded:not-decodable';
60+
| 'discarded:too-large'
61+
| 'discarded:not-representable' // to indicate that extended field `_content` is populated with base64 `postData`
62+
| 'discarded:not-decodable';
6363
_content?: RequestContentData;
6464
_trailers?: HarFormat.Header[];
6565
}
@@ -302,7 +302,7 @@ async function generateHarResponse(
302302

303303
const decoded = await response.body.decodedPromise;
304304

305-
let responseContent: { text: string, encoding?: string } | { comment: string};
305+
let responseContent: { text: string, encoding?: string } | { comment: string };
306306
try {
307307
if (!decoded || decoded.byteLength > options.bodySizeLimit) {
308308
// If no body or the body is too large, don't include it
@@ -435,10 +435,10 @@ function generateHarWebSocketMessage(
435435
return {
436436
// Note that msg.direction is from the perspective of Mockttp, not the client.
437437
type: message.direction === 'sent'
438-
? 'receive'
438+
? 'receive'
439439
: message.direction === 'received'
440440
? 'send'
441-
: unreachableCheck(message.direction),
441+
: unreachableCheck(message.direction),
442442

443443
opcode: message.isBinary ? 2 : 1,
444444
data: message.isBinary
@@ -751,7 +751,7 @@ function parseHttpVersion(
751751
}
752752

753753
function parseHarRequestContents(data: RequestContentData): Buffer {
754-
if (data.encoding && Buffer.isEncoding(data.encoding)) {
754+
if (Buffer.isEncoding(data.encoding)) {
755755
return Buffer.from(data.text, data.encoding);
756756
}
757757

src/services/ui-worker-formatters.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ const WorkerFormatters = {
3939
}
4040
},
4141
base64: (content: Buffer) => {
42-
return Buffer.from(content.toString('utf8'), 'base64').toString('utf8');
42+
const b64 = content.toString('ascii');
43+
const encoding = b64.match(/[-_]/) ? 'base64url' : 'base64';
44+
return Buffer.from(b64, encoding).toString('utf8');
4345
},
4446
markdown: (content: Buffer) => {
4547
return content.toString('utf8');

0 commit comments

Comments
 (0)