Skip to content

Commit

Permalink
Move dns_instrument from onCompleted to onHeadersReceived
Browse files Browse the repository at this point in the history
  • Loading branch information
englehardt committed Jan 12, 2023
1 parent ab5fac2 commit 159c5fa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
16 changes: 8 additions & 8 deletions Extension/src/background/dns-instrument.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PendingResponse } from "../lib/pending-response";
import { DnsResolved } from "../schema";
import { WebRequestOnCompletedEventDetails } from "../types/browser-web-request-event-details";
import { WebRequestOnHeadersReceivedDetails } from "../types/browser-web-request-event-details";
import { allTypes } from "./http-instrument";
import RequestFilter = browser.webRequest.RequestFilter;

export class DnsInstrument {
private readonly dataReceiver;
private onCompleteListener;
private onHeadersReceivedListener;
private pendingResponses: {
[requestId: number]: PendingResponse;
} = {};
Expand All @@ -29,23 +29,23 @@ export class DnsInstrument {
/*
* Attach handlers to event listeners
*/
this.onCompleteListener = (details: WebRequestOnCompletedEventDetails) => {
this.onHeadersReceivedListener = (details: WebRequestOnHeadersReceivedDetails) => {
// Ignore requests made by extensions
if (requestStemsFromExtension(details)) {
return;
}
const pendingResponse = this.getPendingResponse(details.requestId);
pendingResponse.resolveOnCompletedEventDetails(details);
pendingResponse.resolveOnHeadersReceivedDetails(details);

this.onCompleteDnsHandler(details, crawlID);
};

browser.webRequest.onCompleted.addListener(this.onCompleteListener, filter);
browser.webRequest.onHeadersReceived.addListener(this.onHeadersReceivedListener, filter);
}

public cleanup() {
if (this.onCompleteListener) {
browser.webRequest.onCompleted.removeListener(this.onCompleteListener);
if (this.onHeadersReceivedListener) {
browser.webRequest.onHeadersReceived.removeListener(this.onHeadersReceivedListener);
}
}

Expand All @@ -70,7 +70,7 @@ export class DnsInstrument {
}

private async onCompleteDnsHandler(
details: WebRequestOnCompletedEventDetails,
details: WebRequestOnHeadersReceivedDetails,
crawlID,
) {
// Create and populate DnsResolve object
Expand Down
13 changes: 12 additions & 1 deletion Extension/src/lib/pending-response.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import {
WebRequestOnBeforeRequestEventDetails,
WebRequestOnCompletedEventDetails,
WebRequestOnHeadersReceivedDetails
} from "../types/browser-web-request-event-details";
import { ResponseBodyListener } from "./response-body-listener";

/**
* Ties together the two separate events that together holds information about both response headers and body
* Ties together three separate events that hold information about both response headers and body
*/
export class PendingResponse {
public readonly onBeforeRequestEventDetails: Promise<
WebRequestOnBeforeRequestEventDetails
>;
public readonly onHeadersReceivedEventDetails: Promise<
WebRequestOnHeadersReceivedDetails
>;
public readonly onCompletedEventDetails: Promise<
WebRequestOnCompletedEventDetails
>;
public responseBodyListener: ResponseBodyListener;
public resolveOnBeforeRequestEventDetails: (
details: WebRequestOnBeforeRequestEventDetails,
) => void;
public resolveOnHeadersReceivedDetails: (
details: WebRequestOnHeadersReceivedDetails,
) => void;
public resolveOnCompletedEventDetails: (
details: WebRequestOnCompletedEventDetails,
) => void;
constructor() {
this.onBeforeRequestEventDetails = new Promise(resolve => {
this.resolveOnBeforeRequestEventDetails = resolve;
});
this.onHeadersReceivedEventDetails = new Promise(resolve => {
this.resolveOnHeadersReceivedDetails = resolve;
});
this.onCompletedEventDetails = new Promise(resolve => {
this.resolveOnCompletedEventDetails = resolve;
});
Expand All @@ -37,6 +47,7 @@ export class PendingResponse {
public resolved() {
return Promise.all([
this.onBeforeRequestEventDetails,
this.onHeadersReceivedEventDetails,
this.onCompletedEventDetails,
]);
}
Expand Down
47 changes: 47 additions & 0 deletions Extension/src/types/browser-web-request-event-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ResourceType = browser.webRequest.ResourceType;
import UploadData = browser.webRequest.UploadData;
import HttpHeaders = browser.webRequest.HttpHeaders;
import UrlClassification = browser.webRequest.UrlClassification;

export interface FrameAncestor {
/** The URL that the document was loaded from. */
Expand Down Expand Up @@ -189,3 +190,49 @@ export interface WebRequestOnCompletedEventDetails {
*/
statusLine: string;
}

export interface WebRequestOnHeadersReceivedDetails {
/**
* 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.
*/
requestId: string;
url: string;
/** Standard HTTP method. */
method: string;
/**
* 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.
*/
frameId: number;
/** ID of frame that wraps the frame which sent the request. Set to -1 if no parent frame exists. */
parentFrameId: number;
/** True for private browsing requests. */
incognito?: boolean | undefined;
/** The cookie store ID of the contextual identity. */
cookieStoreId?: string | undefined;
/** URL of the resource that triggered this request. */
originUrl?: string | undefined;
/** URL of the page into which the requested resource will be loaded. */
documentUrl?: string | undefined;
/** The ID of the tab in which the request takes place. Set to -1 if the request isn't related to a tab. */
tabId: number;
/** How the requested resource will be used. */
type: ResourceType;
/** The time when this signal is triggered, in milliseconds since the epoch. */
timeStamp: number;
/**
* 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).
*/
statusLine: string;
/**
* The server IP address that the request was actually sent to. Note that it may be a literal IPv6 address.
*/
ip?: string;
/** The HTTP response headers that have been received with this response. */
responseHeaders?: HttpHeaders | undefined;
/** Standard HTTP status code returned by the server. */
statusCode: number;
/** Tracking classification if the request has been classified. */
urlClassification?: UrlClassification | undefined;
/** Indicates if this request and its content window hierarchy is third party. */
thirdParty: boolean;
}

0 comments on commit 159c5fa

Please sign in to comment.