From 7d9826037b925c874638c4659565f57318af146b Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Fri, 29 Jan 2021 10:18:12 -0600 Subject: [PATCH] FetchRequestDelegate#{additional,prepare}HeadersForRequest() --- src/core/drive/form_submission.ts | 8 +++----- src/core/frames/frame_controller.ts | 6 +++--- src/http/fetch_request.ts | 23 +++++++++++------------ 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/core/drive/form_submission.ts b/src/core/drive/form_submission.ts index 91e0df725..01e6bfe06 100644 --- a/src/core/drive/form_submission.ts +++ b/src/core/drive/form_submission.ts @@ -104,16 +104,14 @@ export class FormSubmission { // Fetch request delegate - additionalHeadersForRequest(request: FetchRequest, headers: { [header: string]: string }) { - const additionalHeaders: FetchRequestHeaders = {} + prepareHeadersForRequest(headers: FetchRequestHeaders, request: FetchRequest) { if (!request.isIdempotent) { const token = getCookieValue(getMetaContent("csrf-param")) || getMetaContent("csrf-token") if (token) { - additionalHeaders["X-CSRF-Token"] = token + headers["X-CSRF-Token"] = token } - additionalHeaders["Accept"] = [ StreamMessage.contentType, headers.Accept ].join(", ") + headers["Accept"] = [ StreamMessage.contentType, headers["Accept"] ].join(", ") } - return additionalHeaders } requestStarted(request: FetchRequest) { diff --git a/src/core/frames/frame_controller.ts b/src/core/frames/frame_controller.ts index ab6b7ed92..d0d507518 100644 --- a/src/core/frames/frame_controller.ts +++ b/src/core/frames/frame_controller.ts @@ -1,5 +1,5 @@ import { FrameElement, FrameElementDelegate, FrameLoadingStyle } from "../../elements/frame_element" -import { FetchMethod, FetchRequest, FetchRequestDelegate } from "../../http/fetch_request" +import { FetchMethod, FetchRequest, FetchRequestDelegate, FetchRequestHeaders } from "../../http/fetch_request" import { FetchResponse } from "../../http/fetch_response" import { AppearanceObserver, AppearanceObserverDelegate } from "../../observers/appearance_observer" import { parseHTMLDocument } from "../../util" @@ -124,8 +124,8 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest // Fetch request delegate - additionalHeadersForRequest(request: FetchRequest) { - return { "Turbo-Frame": this.id } + prepareHeadersForRequest(headers: FetchRequestHeaders, request: FetchRequest) { + headers["Turbo-Frame"] = this.id } requestStarted(request: FetchRequest) { diff --git a/src/http/fetch_request.ts b/src/http/fetch_request.ts index 7e80f46f2..4c6c90462 100644 --- a/src/http/fetch_request.ts +++ b/src/http/fetch_request.ts @@ -2,7 +2,7 @@ import { FetchResponse } from "./fetch_response" import { dispatch } from "../util" export interface FetchRequestDelegate { - additionalHeadersForRequest?(request: FetchRequest, headers: { [header: string]: string }): { [header: string]: string } + prepareHeadersForRequest?(headers: FetchRequestHeaders, request: FetchRequest): void requestStarted(request: FetchRequest): void requestPreventedHandlingResponse(request: FetchRequest, response: FetchResponse): void requestSucceededWithResponse(request: FetchRequest, response: FetchResponse): void @@ -117,23 +117,22 @@ export class FetchRequest { } get headers() { - const defaultHeaders = { "Accept": "text/html, application/xhtml+xml" } - const additionalHeaders = this.additionalHeadersWithDefaults(defaultHeaders) - - return { ...defaultHeaders, ...additionalHeaders } - } - - additionalHeadersWithDefaults(defaults: { [header: string]: string }) { - if (typeof this.delegate.additionalHeadersForRequest == "function") { - return this.delegate.additionalHeadersForRequest(this, { ...defaults }) - } else { - return {} + const headers = { ...this.defaultHeaders } + if (typeof this.delegate.prepareHeadersForRequest == "function") { + this.delegate.prepareHeadersForRequest(headers, this) } + return headers } get abortSignal() { return this.abortController.signal } + + get defaultHeaders() { + return { + "Accept": "text/html, application/xhtml+xml" + } + } } function mergeFormDataEntries(url: URL, entries: [string, FormDataEntryValue][]): URL {