Skip to main content

PageOutput

Struct PageOutput 

Source
#[non_exhaustive]
pub struct PageOutput { pub html: String, pub csp: String, pub nonce: CspNonce, /* private fields */ }
Expand description

Output from render_page: HTML body + CSP header value + the nonce binding them together.

The HTML and the CSP header are a single unit — the HTML embeds a per-request nonce that only this policy blesses. Serve them together with Cache-Control: no-store and never cache the HTML across requests: a cached body with a fresh policy kills every script, and caching both ships one static, publicly-known nonce to every user, which defeats nonce-based CSP entirely.

Superseded by PageResponse, which writes those headers itself and cannot be sent without them. Turn a PageOutput into one with PageOutput::into_page_response; the public fields and PageOutput::headers are deprecated and go private in 3.0.0.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§html: String
👎Deprecated since 0.2.2:

send the page as a PageResponse (into_page_response), which carries no-store and the policy with it. Private in 3.0.0

The complete HTML document.

§csp: String
👎Deprecated since 0.2.2:

the policy is typed now: extend it with PolicyExtensions and send a PageResponse rather than editing this text. Private in 3.0.0

The Content-Security-Policy header value that blesses it.

§nonce: CspNonce
👎Deprecated since 0.2.2:

render with try_render (0.2.2), which draws the nonce and writes it into the page and its policy; a handler that must read it takes PageResponse::into_parts_unchecked().nonce. Private in 3.0.0, which gives no caller a nonce

The nonce embedded in html and blessed by csp.

Exposed so a caller can add its own nonced <script>/<style>, or re-stamp a cached shell, without string-parsing the CSP.

Implementations§

Source§

impl PageOutput

Source

pub fn headers(&self) -> Vec<(HeaderName, HeaderValue)>

👎Deprecated since 0.2.2:

use into_page_response: a PageResponse writes these headers itself and fails rather than dropping an invalid policy. Removed in 3.0.0

Response headers for this page: content type, no-store, the CSP, and (from 0.2.2, in development builds) Trusted Types in Content-Security-Policy-Report-Only.

Reads the (public, mutable) csp field, so a caller that must rewrite a directive can do so in place first and still use this helper. An invalid csp is left out rather than reported; that silent drop is one of the reasons this is deprecated.

Source

pub fn into_page_response( self, extensions: &PolicyExtensions, ) -> Result<PageResponse, PolicyError>

This page as a PageResponse: no-store, the strict policy for the page’s nonce with extensions added, and the document headers, written whenever it is sent.

The policy is rebuilt from the nonce the HTML embeds, so a csp edited as text would not carry over: it is refused instead, wherever the edit went (a widening one would fail in the browser, a tightening one would be lost). Express the edit as an extension:

// Was: page.csp.replace("img-src 'self' data:", "img-src 'self' data: https:")
let page = render_page(&config())
    .into_page_response(&PolicyExtensions::new().img_src("https:"))?;

The bridge trusts the deprecated public nonce and html as they are: a nonce the caller chose (through render_page_with_nonce, or by assigning the field) carries into the PageResponse. A fresh nonce per response is guaranteed only when the render draws it, as render_page and try_render do.

§Errors

A PolicyError naming the first extension that is not a source a page may add, or, for directive content-security-policy, a csp that is not the one forma-server wrote for this page’s nonce. There is then no response to send: answer with an error page rather than this one without its policy.

Verified by: crates/forma-server/tests/page_response.rs > “into_page_response_refuses_a_policy_edited_as_text”

Trait Implementations§

Source§

impl Debug for PageOutput

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more