topcoat-router 0.8.0

A modular, batteries-included Rust web framework for server-rendered apps.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#![doc = include_str!("../../docs/content/sitemap.md")]

use std::{borrow::Cow, fmt, time::SystemTime};

use http::header::{CONTENT_TYPE, HeaderValue};
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use topcoat_core::{base_url::base_url, context::Cx, error::Result};
use topcoat_view::{Formatter, HtmlContext};

use crate::{
    Body,
    response::{IntoResponse, Response},
};

/// An XML sitemap response, assembled URL by URL.
///
/// A sitemap lists the pages of a site for crawlers. Build one by adding
/// entries with [`url`](Sitemap::url) and [`urls`](Sitemap::urls), then
/// return it from a route serving `/sitemap.xml`. The response is sent with
/// `Content-Type: application/xml`.
///
/// An entry holding a root-relative path is resolved against the base URL
/// registered on the router with `.base_url(...)`, because the sitemap
/// format requires absolute URLs. An entry that is already an absolute
/// `http` or `https` URL is used as is.
///
/// # Panics
///
/// Converting the sitemap into a response panics if an entry holds a
/// root-relative path and no base URL is registered.
///
/// # Examples
///
/// ```rust
/// use topcoat::{
///     Result,
///     router::{
///         content::sitemap::{ChangeFrequency, Sitemap, SitemapUrl},
///         route,
///     },
/// };
///
/// #[route(GET "/sitemap.xml")]
/// async fn sitemap() -> Result<Sitemap> {
///     Ok(Sitemap::new()
///         .url("/")
///         .url(SitemapUrl::new("/about").change_frequency(ChangeFrequency::Monthly)))
/// }
/// ```
#[derive(Clone, Debug, Default)]
#[must_use]
pub struct Sitemap {
    urls: Vec<SitemapUrl>,
}

impl Sitemap {
    /// Creates a sitemap without any entries.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds an entry.
    ///
    /// Accepts a location string or a [`SitemapUrl`] carrying the optional
    /// fields.
    pub fn url(mut self, url: impl Into<SitemapUrl>) -> Self {
        self.urls.push(url.into());
        self
    }

    /// Adds every entry of an iterator, such as one built from the rows of
    /// a database query.
    pub fn urls<I>(mut self, urls: I) -> Self
    where
        I: IntoIterator,
        I::Item: Into<SitemapUrl>,
    {
        self.urls.extend(urls.into_iter().map(Into::into));
        self
    }

    /// Serializes the sitemap into the XML document, resolving relative
    /// locations against the registered base URL.
    fn serialize(&self, cx: &Cx) -> Result<String> {
        let mut xml = String::from(
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
             <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n",
        );
        let mut f = Formatter::new(&mut xml);
        for url in &self.urls {
            let location = if is_absolute(&url.location) {
                Cow::Borrowed(url.location.as_str())
            } else {
                Cow::Owned(base_url(cx).join(&url.location))
            };
            f.write_str("<url><loc>");
            // Text-node escaping applies to XML content just as to HTML.
            HtmlContext::Text.writer(&mut f).write_str(&location);
            f.write_str("</loc>");
            if let Some(last_modified) = url.last_modified {
                let last_modified = OffsetDateTime::from(last_modified)
                    .format(&Rfc3339)
                    .map_err(|_| {
                        InvalidSitemapError::new(
                            "a last modified time is outside the representable range",
                        )
                    })?;
                f.write_str("<lastmod>");
                f.write_str(&last_modified);
                f.write_str("</lastmod>");
            }
            if let Some(change_frequency) = url.change_frequency {
                f.write_str("<changefreq>");
                f.write_str(change_frequency.as_str());
                f.write_str("</changefreq>");
            }
            if let Some(priority) = url.priority {
                if !(0.0..=1.0).contains(&priority) {
                    return Err(
                        InvalidSitemapError::new("a priority must be between 0.0 and 1.0").into(),
                    );
                }
                f.write_str("<priority>");
                f.write_str(&priority.to_string());
                f.write_str("</priority>");
            }
            f.write_str("</url>\n");
        }
        f.write_str("</urlset>\n");
        Ok(xml)
    }
}

impl IntoResponse for Sitemap {
    fn into_response(self, cx: &Cx) -> Result<Response> {
        (
            [(CONTENT_TYPE, HeaderValue::from_static("application/xml"))],
            Body::from(self.serialize(cx)?),
        )
            .into_response(cx)
    }
}

/// One [`Sitemap`] entry, assembled field by field.
///
/// The location is required and set at construction; every other field is
/// optional and replaced by the builder method that sets it.
///
/// # Examples
///
/// ```rust
/// use std::time::SystemTime;
///
/// use topcoat::router::content::sitemap::{ChangeFrequency, SitemapUrl};
///
/// let url = SitemapUrl::new("/posts/42")
///     .last_modified(SystemTime::now())
///     .change_frequency(ChangeFrequency::Weekly)
///     .priority(0.8);
/// ```
#[derive(Clone, Debug)]
#[must_use]
pub struct SitemapUrl {
    location: String,
    last_modified: Option<SystemTime>,
    change_frequency: Option<ChangeFrequency>,
    priority: Option<f32>,
}

impl SitemapUrl {
    /// Creates an entry for the page at `location`: a root-relative path
    /// resolved against the registered base URL, or an absolute `http` or
    /// `https` URL used as is.
    pub fn new(location: impl Into<String>) -> Self {
        Self {
            location: location.into(),
            last_modified: None,
            change_frequency: None,
            priority: None,
        }
    }

    /// Sets the time the page was last modified.
    ///
    /// Accepts anything convertible into a [`SystemTime`], which covers the
    /// timestamp types of the common date and time crates.
    pub fn last_modified(mut self, last_modified: impl Into<SystemTime>) -> Self {
        self.last_modified = Some(last_modified.into());
        self
    }

    /// Sets how frequently the page is likely to change.
    pub fn change_frequency(mut self, change_frequency: ChangeFrequency) -> Self {
        self.change_frequency = Some(change_frequency);
        self
    }

    /// Sets the priority of the page relative to the other pages of the
    /// site, from `0.0` to `1.0`. Crawlers treat an entry without a
    /// priority as `0.5`.
    ///
    /// A value outside the range is rejected when the sitemap is converted
    /// into a response.
    pub fn priority(mut self, priority: f32) -> Self {
        self.priority = Some(priority);
        self
    }
}

impl From<&str> for SitemapUrl {
    fn from(location: &str) -> Self {
        Self::new(location)
    }
}

impl From<String> for SitemapUrl {
    fn from(location: String) -> Self {
        Self::new(location)
    }
}

/// How frequently a page is likely to change, hinting crawlers how often to
/// revisit it.
///
/// [`Always`](ChangeFrequency::Always) describes a page that changes on
/// every access, [`Never`](ChangeFrequency::Never) an archived page.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ChangeFrequency {
    /// The page changes on every access.
    Always,
    /// The page changes about once an hour.
    Hourly,
    /// The page changes about once a day.
    Daily,
    /// The page changes about once a week.
    Weekly,
    /// The page changes about once a month.
    Monthly,
    /// The page changes about once a year.
    Yearly,
    /// The page is archived and never changes.
    Never,
}

impl ChangeFrequency {
    /// The value as it appears in a `<changefreq>` element.
    fn as_str(self) -> &'static str {
        match self {
            Self::Always => "always",
            Self::Hourly => "hourly",
            Self::Daily => "daily",
            Self::Weekly => "weekly",
            Self::Monthly => "monthly",
            Self::Yearly => "yearly",
            Self::Never => "never",
        }
    }
}

/// The error produced when a [`Sitemap`] holds a field whose value cannot
/// be represented in the sitemap format.
#[derive(Debug)]
pub struct InvalidSitemapError {
    description: &'static str,
}

impl InvalidSitemapError {
    fn new(description: &'static str) -> Self {
        Self { description }
    }

    /// Returns the description of the invalid field.
    #[must_use]
    pub fn description(&self) -> &str {
        self.description
    }
}

impl fmt::Display for InvalidSitemapError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "invalid sitemap: {}", self.description)
    }
}

impl std::error::Error for InvalidSitemapError {}

/// Returns `true` if the location is an absolute `http` or `https` URL.
fn is_absolute(location: &str) -> bool {
    ["http://", "https://"].iter().any(|scheme| {
        location
            .get(..scheme.len())
            .is_some_and(|prefix| prefix.eq_ignore_ascii_case(scheme))
    })
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use topcoat_core::{base_url::BaseUrl, context::CxTestBuilder};

    use super::*;
    use crate::to_bytes;

    /// Builds a `Cx` with `https://example.com` registered as the base URL.
    fn cx() -> Cx {
        CxTestBuilder::new()
            .app_context(BaseUrl::new("https://example.com").expect("a valid base URL"))
            .build()
    }

    #[test]
    fn an_empty_sitemap_is_an_empty_urlset() {
        assert_eq!(
            Sitemap::new().serialize(&cx()).unwrap(),
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
             <urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n\
             </urlset>\n"
        );
    }

    #[test]
    fn every_field_is_serialized() {
        let url = SitemapUrl::new("/posts/1")
            .last_modified(SystemTime::UNIX_EPOCH + Duration::from_hours(24))
            .change_frequency(ChangeFrequency::Weekly)
            .priority(0.8);
        let xml = Sitemap::new().url(url).serialize(&cx()).unwrap();
        assert!(xml.contains(
            "<url>\
             <loc>https://example.com/posts/1</loc>\
             <lastmod>1970-01-02T00:00:00Z</lastmod>\
             <changefreq>weekly</changefreq>\
             <priority>0.8</priority>\
             </url>"
        ));
    }

    #[test]
    fn relative_locations_resolve_against_the_base_url() {
        let xml = Sitemap::new()
            .url("/")
            .url("about")
            .serialize(&cx())
            .unwrap();
        assert!(xml.contains("<loc>https://example.com/</loc>"));
        assert!(xml.contains("<loc>https://example.com/about</loc>"));
    }

    #[test]
    fn absolute_locations_are_used_as_is() {
        let xml = Sitemap::new()
            .url("https://cdn.example.com/page")
            .url("HTTP://example.com/UPPER")
            .serialize(&cx())
            .unwrap();
        assert!(xml.contains("<loc>https://cdn.example.com/page</loc>"));
        assert!(xml.contains("<loc>HTTP://example.com/UPPER</loc>"));
    }

    #[test]
    fn urls_adds_every_entry_of_an_iterator() {
        let xml = Sitemap::new().urls(["/a", "/b"]).serialize(&cx()).unwrap();
        assert!(xml.contains("<loc>https://example.com/a</loc>"));
        assert!(xml.contains("<loc>https://example.com/b</loc>"));
    }

    #[test]
    fn reserved_characters_in_locations_are_escaped() {
        // Quotes are legal in XML text content and pass through unescaped.
        let xml = Sitemap::new()
            .url("/search?q=<a>&sort=\"new\"")
            .serialize(&cx())
            .unwrap();
        assert!(xml.contains("<loc>https://example.com/search?q=&lt;a&gt;&amp;sort=\"new\"</loc>"));
    }

    #[test]
    fn a_priority_outside_the_range_is_an_error() {
        for priority in [-0.1, 1.1] {
            let error = Sitemap::new()
                .url(SitemapUrl::new("/").priority(priority))
                .serialize(&cx())
                .unwrap_err();
            assert!(error.downcast_ref::<InvalidSitemapError>().is_some());
        }
    }

    #[test]
    #[should_panic(expected = "attempted to access the base URL")]
    fn a_relative_location_without_a_base_url_panics() {
        let _ = Sitemap::new().url("/").serialize(&Cx::default());
    }

    #[tokio::test]
    async fn into_response_sets_the_xml_content_type() {
        let response = Sitemap::new()
            .url("/")
            .into_response(&cx())
            .expect("response builds");

        assert_eq!(
            response
                .headers()
                .get(CONTENT_TYPE)
                .map(HeaderValue::as_bytes),
            Some(b"application/xml".as_slice())
        );

        let body = to_bytes(response.into_body(), usize::MAX)
            .await
            .expect("reading the response body");
        assert!(body.starts_with(b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    }
}