mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chore(har): brush up post data serialization (#14994)
This commit is contained in:
parent
e3da3ebfa4
commit
b5524aa756
@ -21,7 +21,7 @@ import { helper } from '../helper';
|
|||||||
import * as network from '../network';
|
import * as network from '../network';
|
||||||
import type { Page } from '../page';
|
import type { Page } from '../page';
|
||||||
import type * as har from './har';
|
import type * as har from './har';
|
||||||
import { calculateSha1, monotonicTime } from '../../utils';
|
import { assert, calculateSha1, monotonicTime } from '../../utils';
|
||||||
import type { RegisteredListener } from '../../utils/eventsHelper';
|
import type { RegisteredListener } from '../../utils/eventsHelper';
|
||||||
import { eventsHelper } from '../../utils/eventsHelper';
|
import { eventsHelper } from '../../utils/eventsHelper';
|
||||||
import { mime } from '../../utilsBundle';
|
import { mime } from '../../utilsBundle';
|
||||||
@ -164,7 +164,7 @@ export class HarTracer {
|
|||||||
const harEntry = createHarEntry(event.method, event.url, '', '');
|
const harEntry = createHarEntry(event.method, event.url, '', '');
|
||||||
harEntry.request.cookies = event.cookies;
|
harEntry.request.cookies = event.cookies;
|
||||||
harEntry.request.headers = Object.entries(event.headers).map(([name, value]) => ({ name, value }));
|
harEntry.request.headers = Object.entries(event.headers).map(([name, value]) => ({ name, value }));
|
||||||
harEntry.request.postData = postDataForBuffer(event.postData || null, event.headers['content-type'], this._options.content);
|
harEntry.request.postData = this._postDataForBuffer(event.postData || null, event.headers['content-type'], this._options.content);
|
||||||
harEntry.request.bodySize = event.postData?.length || 0;
|
harEntry.request.bodySize = event.postData?.length || 0;
|
||||||
(event as any)[this._entrySymbol] = harEntry;
|
(event as any)[this._entrySymbol] = harEntry;
|
||||||
if (this._started)
|
if (this._started)
|
||||||
@ -214,7 +214,7 @@ export class HarTracer {
|
|||||||
const pageEntry = this._ensurePageEntry(page);
|
const pageEntry = this._ensurePageEntry(page);
|
||||||
const harEntry = createHarEntry(request.method(), url, request.guid, request.frame().guid);
|
const harEntry = createHarEntry(request.method(), url, request.guid, request.frame().guid);
|
||||||
harEntry.pageref = pageEntry.id;
|
harEntry.pageref = pageEntry.id;
|
||||||
harEntry.request.postData = postDataForRequest(request, this._options.content);
|
harEntry.request.postData = this._postDataForRequest(request, this._options.content);
|
||||||
harEntry.request.bodySize = request.bodySize();
|
harEntry.request.bodySize = request.bodySize();
|
||||||
if (request.redirectedFrom()) {
|
if (request.redirectedFrom()) {
|
||||||
const fromEntry = this._entryForRequest(request.redirectedFrom()!);
|
const fromEntry = this._entryForRequest(request.redirectedFrom()!);
|
||||||
@ -222,7 +222,7 @@ export class HarTracer {
|
|||||||
fromEntry.response.redirectURL = request.url();
|
fromEntry.response.redirectURL = request.url();
|
||||||
}
|
}
|
||||||
(request as any)[this._entrySymbol] = harEntry;
|
(request as any)[this._entrySymbol] = harEntry;
|
||||||
if (this._started)
|
assert(this._started);
|
||||||
this._delegate.onEntryStarted(harEntry);
|
this._delegate.onEntryStarted(harEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,12 +271,6 @@ export class HarTracer {
|
|||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
compressionCalculationBarrier.setDecodedBodySize(0);
|
compressionCalculationBarrier.setDecodedBodySize(0);
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
const postData = response.request().postDataBuffer();
|
|
||||||
if (postData && harEntry.request.postData && this._options.content === 'attach') {
|
|
||||||
harEntry.request.postData._sha1 = calculateSha1(postData) + '.' + (mime.getExtension(harEntry.request.postData.mimeType) || 'dat');
|
|
||||||
if (this._started)
|
|
||||||
this._delegate.onContentBlob(harEntry.request.postData._sha1, postData);
|
|
||||||
}
|
|
||||||
if (this._started)
|
if (this._started)
|
||||||
this._delegate.onEntryFinished(harEntry);
|
this._delegate.onEntryFinished(harEntry);
|
||||||
});
|
});
|
||||||
@ -332,8 +326,6 @@ export class HarTracer {
|
|||||||
return;
|
return;
|
||||||
const request = response.request();
|
const request = response.request();
|
||||||
|
|
||||||
harEntry.request.postData = postDataForRequest(request, this._options.content);
|
|
||||||
|
|
||||||
harEntry.response = {
|
harEntry.response = {
|
||||||
status: response.status(),
|
status: response.status(),
|
||||||
statusText: response.statusText(),
|
statusText: response.statusText(),
|
||||||
@ -428,6 +420,45 @@ export class HarTracer {
|
|||||||
this._pageEntries.clear();
|
this._pageEntries.clear();
|
||||||
return log;
|
return log;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _postDataForRequest(request: network.Request, content: 'omit' | 'attach' | 'embed'): har.PostData | undefined {
|
||||||
|
const postData = request.postDataBuffer();
|
||||||
|
if (!postData)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const contentType = request.headerValue('content-type');
|
||||||
|
return this._postDataForBuffer(postData, contentType, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _postDataForBuffer(postData: Buffer | null, contentType: string | undefined, content: 'omit' | 'attach' | 'embed'): har.PostData | undefined {
|
||||||
|
if (!postData)
|
||||||
|
return;
|
||||||
|
|
||||||
|
contentType ??= 'application/octet-stream';
|
||||||
|
|
||||||
|
const result: har.PostData = {
|
||||||
|
mimeType: contentType,
|
||||||
|
text: '',
|
||||||
|
params: []
|
||||||
|
};
|
||||||
|
|
||||||
|
if (content === 'embed' && contentType !== 'application/octet-stream')
|
||||||
|
result.text = postData.toString();
|
||||||
|
|
||||||
|
if (content === 'attach') {
|
||||||
|
result._sha1 = calculateSha1(postData) + '.' + (mime.getExtension(contentType) || 'dat');
|
||||||
|
this._delegate.onContentBlob(result._sha1, postData);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentType === 'application/x-www-form-urlencoded') {
|
||||||
|
const parsed = new URLSearchParams(postData.toString());
|
||||||
|
for (const [name, value] of parsed.entries())
|
||||||
|
result.params.push({ name, value });
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createHarEntry(method: string, url: URL, requestref: string, frameref: string): har.Entry {
|
function createHarEntry(method: string, url: URL, requestref: string, frameref: string): har.Entry {
|
||||||
@ -475,38 +506,6 @@ function createHarEntry(method: string, url: URL, requestref: string, frameref:
|
|||||||
return harEntry;
|
return harEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
function postDataForRequest(request: network.Request, content: 'omit' | 'attach' | 'embed'): har.PostData | undefined {
|
|
||||||
const postData = request.postDataBuffer();
|
|
||||||
if (!postData)
|
|
||||||
return;
|
|
||||||
|
|
||||||
const contentType = request.headerValue('content-type');
|
|
||||||
return postDataForBuffer(postData, contentType, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
function postDataForBuffer(postData: Buffer | null, contentType: string | undefined, content: 'omit' | 'attach' | 'embed'): har.PostData | undefined {
|
|
||||||
if (!postData)
|
|
||||||
return;
|
|
||||||
|
|
||||||
contentType ??= 'application/octet-stream';
|
|
||||||
|
|
||||||
const result: har.PostData = {
|
|
||||||
mimeType: contentType,
|
|
||||||
text: '',
|
|
||||||
params: []
|
|
||||||
};
|
|
||||||
|
|
||||||
if (content === 'embed' && contentType !== 'application/octet-stream')
|
|
||||||
result.text = postData.toString();
|
|
||||||
|
|
||||||
if (contentType === 'application/x-www-form-urlencoded') {
|
|
||||||
const parsed = new URLSearchParams(postData.toString());
|
|
||||||
for (const [name, value] of parsed.entries())
|
|
||||||
result.params.push({ name, value });
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseCookie(c: string): har.Cookie {
|
function parseCookie(c: string): har.Cookie {
|
||||||
const cookie: har.Cookie = {
|
const cookie: har.Cookie = {
|
||||||
name: '',
|
name: '',
|
||||||
@ -543,5 +542,5 @@ function parseCookie(c: string): har.Cookie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isTextualMimeType(mimeType: string) {
|
function isTextualMimeType(mimeType: string) {
|
||||||
return !!mimeType.match(/^(text\/.*?|application\/(json|(x-)?javascript|xml.*?|ecmascript)|image\/svg(\+xml)?|application\/.*?(\+json|\+xml))(;\s*charset=.*)?$/);
|
return !!mimeType.match(/^(text\/.*?|application\/(json|(x-)?javascript|xml.*?|ecmascript|graphql|x-www-form-urlencoded)|image\/svg(\+xml)?|application\/.*?(\+json|\+xml))(;\s*charset=.*)?$/);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user