Skip to main content

max / makenotwork

1.6 KB · 56 lines History Blame Raw
1 //! CORS preflight check storage.
2
3 use super::{CorsCheckResult, Result, SqlitePool};
4 use tracing::instrument;
5
6 #[instrument(skip_all)]
7 pub async fn insert_cors_check(pool: &SqlitePool, result: &CorsCheckResult) -> Result<i64> {
8 let row = sqlx::query(
9 "INSERT INTO cors_checks (target, url, origin, method, passes, checked_at, error)
10 VALUES (?, ?, ?, ?, ?, ?, ?)",
11 )
12 .bind(&result.target)
13 .bind(&result.url)
14 .bind(&result.origin)
15 .bind(&result.method)
16 .bind(result.passes)
17 .bind(&result.checked_at)
18 .bind(&result.error)
19 .execute(pool)
20 .await?;
21 Ok(row.last_insert_rowid())
22 }
23
24 /// Get the latest CORS check per URL for a target.
25 #[instrument(skip_all)]
26 pub async fn get_latest_cors_checks(
27 pool: &SqlitePool,
28 target: &str,
29 ) -> Result<Vec<CorsCheckResult>> {
30 let rows = sqlx::query_as::<_, (String, String, String, String, bool, String, Option<String>)>(
31 "SELECT c.target, c.url, c.origin, c.method, c.passes, c.checked_at, c.error
32 FROM cors_checks c
33 INNER JOIN (SELECT url, MAX(id) as max_id FROM cors_checks WHERE target = ? GROUP BY url) latest
34 ON c.id = latest.max_id
35 ORDER BY c.url",
36 )
37 .bind(target)
38 .fetch_all(pool)
39 .await?;
40
41 Ok(rows
42 .into_iter()
43 .map(
44 |(target, url, origin, method, passes, checked_at, error)| CorsCheckResult {
45 target,
46 url,
47 origin,
48 method,
49 passes,
50 checked_at,
51 error,
52 },
53 )
54 .collect())
55 }
56