Skip to content

Commit

Permalink
FetchRequestDelegate#{additional,prepare}HeadersForRequest()
Browse files Browse the repository at this point in the history
  • Loading branch information
sstephenson committed Jan 29, 2021
1 parent ca350d9 commit 7d98260
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/core/drive/form_submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 11 additions & 12 deletions src/http/fetch_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7d98260

Please sign in to comment.