Skip to content

Commit 159c5fa

Browse files
committed
Move dns_instrument from onCompleted to onHeadersReceived
1 parent ab5fac2 commit 159c5fa

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

Extension/src/background/dns-instrument.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { PendingResponse } from "../lib/pending-response";
22
import { DnsResolved } from "../schema";
3-
import { WebRequestOnCompletedEventDetails } from "../types/browser-web-request-event-details";
3+
import { WebRequestOnHeadersReceivedDetails } from "../types/browser-web-request-event-details";
44
import { allTypes } from "./http-instrument";
55
import RequestFilter = browser.webRequest.RequestFilter;
66

77
export class DnsInstrument {
88
private readonly dataReceiver;
9-
private onCompleteListener;
9+
private onHeadersReceivedListener;
1010
private pendingResponses: {
1111
[requestId: number]: PendingResponse;
1212
} = {};
@@ -29,23 +29,23 @@ export class DnsInstrument {
2929
/*
3030
* Attach handlers to event listeners
3131
*/
32-
this.onCompleteListener = (details: WebRequestOnCompletedEventDetails) => {
32+
this.onHeadersReceivedListener = (details: WebRequestOnHeadersReceivedDetails) => {
3333
// Ignore requests made by extensions
3434
if (requestStemsFromExtension(details)) {
3535
return;
3636
}
3737
const pendingResponse = this.getPendingResponse(details.requestId);
38-
pendingResponse.resolveOnCompletedEventDetails(details);
38+
pendingResponse.resolveOnHeadersReceivedDetails(details);
3939

4040
this.onCompleteDnsHandler(details, crawlID);
4141
};
4242

43-
browser.webRequest.onCompleted.addListener(this.onCompleteListener, filter);
43+
browser.webRequest.onHeadersReceived.addListener(this.onHeadersReceivedListener, filter);
4444
}
4545

4646
public cleanup() {
47-
if (this.onCompleteListener) {
48-
browser.webRequest.onCompleted.removeListener(this.onCompleteListener);
47+
if (this.onHeadersReceivedListener) {
48+
browser.webRequest.onHeadersReceived.removeListener(this.onHeadersReceivedListener);
4949
}
5050
}
5151

@@ -70,7 +70,7 @@ export class DnsInstrument {
7070
}
7171

7272
private async onCompleteDnsHandler(
73-
details: WebRequestOnCompletedEventDetails,
73+
details: WebRequestOnHeadersReceivedDetails,
7474
crawlID,
7575
) {
7676
// Create and populate DnsResolve object

Extension/src/lib/pending-response.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
11
import {
22
WebRequestOnBeforeRequestEventDetails,
33
WebRequestOnCompletedEventDetails,
4+
WebRequestOnHeadersReceivedDetails
45
} from "../types/browser-web-request-event-details";
56
import { ResponseBodyListener } from "./response-body-listener";
67

78
/**
8-
* Ties together the two separate events that together holds information about both response headers and body
9+
* Ties together three separate events that hold information about both response headers and body
910
*/
1011
export class PendingResponse {
1112
public readonly onBeforeRequestEventDetails: Promise<
1213
WebRequestOnBeforeRequestEventDetails
1314
>;
15+
public readonly onHeadersReceivedEventDetails: Promise<
16+
WebRequestOnHeadersReceivedDetails
17+
>;
1418
public readonly onCompletedEventDetails: Promise<
1519
WebRequestOnCompletedEventDetails
1620
>;
1721
public responseBodyListener: ResponseBodyListener;
1822
public resolveOnBeforeRequestEventDetails: (
1923
details: WebRequestOnBeforeRequestEventDetails,
2024
) => void;
25+
public resolveOnHeadersReceivedDetails: (
26+
details: WebRequestOnHeadersReceivedDetails,
27+
) => void;
2128
public resolveOnCompletedEventDetails: (
2229
details: WebRequestOnCompletedEventDetails,
2330
) => void;
2431
constructor() {
2532
this.onBeforeRequestEventDetails = new Promise(resolve => {
2633
this.resolveOnBeforeRequestEventDetails = resolve;
2734
});
35+
this.onHeadersReceivedEventDetails = new Promise(resolve => {
36+
this.resolveOnHeadersReceivedDetails = resolve;
37+
});
2838
this.onCompletedEventDetails = new Promise(resolve => {
2939
this.resolveOnCompletedEventDetails = resolve;
3040
});
@@ -37,6 +47,7 @@ export class PendingResponse {
3747
public resolved() {
3848
return Promise.all([
3949
this.onBeforeRequestEventDetails,
50+
this.onHeadersReceivedEventDetails,
4051
this.onCompletedEventDetails,
4152
]);
4253
}

Extension/src/types/browser-web-request-event-details.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import ResourceType = browser.webRequest.ResourceType;
88
import UploadData = browser.webRequest.UploadData;
99
import HttpHeaders = browser.webRequest.HttpHeaders;
10+
import UrlClassification = browser.webRequest.UrlClassification;
1011

1112
export interface FrameAncestor {
1213
/** The URL that the document was loaded from. */
@@ -189,3 +190,49 @@ export interface WebRequestOnCompletedEventDetails {
189190
*/
190191
statusLine: string;
191192
}
193+
194+
export interface WebRequestOnHeadersReceivedDetails {
195+
/**
196+
* The ID of the request. Request IDs are unique within a browser session. As a result, they could be used to relate different events of the same request.
197+
*/
198+
requestId: string;
199+
url: string;
200+
/** Standard HTTP method. */
201+
method: string;
202+
/**
203+
* The value 0 indicates that the request happens in the main frame; a positive value indicates the ID of a subframe in which the request happens. If the document of a (sub-)frame is loaded (`type` is `main_frame` or `sub_frame`), `frameId` indicates the ID of this frame, not the ID of the outer frame. Frame IDs are unique within a tab.
204+
*/
205+
frameId: number;
206+
/** ID of frame that wraps the frame which sent the request. Set to -1 if no parent frame exists. */
207+
parentFrameId: number;
208+
/** True for private browsing requests. */
209+
incognito?: boolean | undefined;
210+
/** The cookie store ID of the contextual identity. */
211+
cookieStoreId?: string | undefined;
212+
/** URL of the resource that triggered this request. */
213+
originUrl?: string | undefined;
214+
/** URL of the page into which the requested resource will be loaded. */
215+
documentUrl?: string | undefined;
216+
/** The ID of the tab in which the request takes place. Set to -1 if the request isn't related to a tab. */
217+
tabId: number;
218+
/** How the requested resource will be used. */
219+
type: ResourceType;
220+
/** The time when this signal is triggered, in milliseconds since the epoch. */
221+
timeStamp: number;
222+
/**
223+
* HTTP status line of the response or the 'HTTP/0.9 200 OK' string for HTTP/0.9 responses (i.e., responses that lack a status line).
224+
*/
225+
statusLine: string;
226+
/**
227+
* The server IP address that the request was actually sent to. Note that it may be a literal IPv6 address.
228+
*/
229+
ip?: string;
230+
/** The HTTP response headers that have been received with this response. */
231+
responseHeaders?: HttpHeaders | undefined;
232+
/** Standard HTTP status code returned by the server. */
233+
statusCode: number;
234+
/** Tracking classification if the request has been classified. */
235+
urlClassification?: UrlClassification | undefined;
236+
/** Indicates if this request and its content window hierarchy is third party. */
237+
thirdParty: boolean;
238+
}

0 commit comments

Comments
 (0)