Repository navigation
feat(STAC): Details reads the item's own document: Formatted and JSON tabs, with asset links #3081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
4ff1db3
feat(STAC): Details reads the item's own document: Formatted and JSON…
ianhorn 267bc42
Merge branch 'main' into stac-item-json
ianhorn 6dbc413
Merge branch 'main' into stac-item-json
ianhorn 8c612b9
fix(STAC): the inspector's asset links come from the fetched document…
ianhorn a1a72fb
fix(STAC): the inspector's Download and Add to map use the address it…
ianhorn bc42c36
test(STAC): an inspector row acts on the document's own address, rela…
ianhorn 0ffd550
Apply suggestion from @github-actions[bot]
ianhorn c3f7398
Apply suggestion from @github-actions[bot]
ianhorn 50548b6
Merge branch 'main' into stac-item-json
ianhorn 060daac
fix(STAC): the inspector reads an item's document within 16 MB, off t…
ianhorn b290c60
fix(STAC): the item's own document is read under the item's reach, an…
ianhorn de0db17
Apply suggestion from @github-actions[bot]
ianhorn 8db17fe
Apply suggestion from @github-actions[bot]
ianhorn b378cd2
Merge branch 'main' into stac-item-json
ianhorn 1f59ae7
Merge branch 'main' into stac-item-json
ianhorn 18a9820
Merge branch 'main' into stac-item-json
ianhorn 67122cb
Apply suggestion from @github-actions[bot]
ianhorn 29de48d
Merge branch 'main' into stac-item-json
ianhorn b7a33fd
Apply suggestion from @github-actions[bot]
ianhorn 40a9c30
Merge branch 'main' into stac-item-json
ianhorn 0dab906
fix(STAC): an item's document is read within 4 MB, one helper cuts th…
ianhorn 97cf3ac
Apply suggestion from @github-actions[bot]
ianhorn a6e0049
Apply suggestion from @github-actions[bot]
ianhorn 9849363
Merge branch 'main' into stac-item-json
ianhorn bab9cad
fix(STAC): the laid-out document is bounded, a scheme-bearing asset a…
ianhorn edbb76a
Merge branch 'main' into stac-item-json
ianhorn 3e28637
fix(STAC): long asset addresses resolve, asset keys are cut, and roun…
ianhorn e568c28
Merge branch 'main' into stac-item-json
ianhorn 6da3c1c
Merge branch 'main' into stac-item-json
ianhorn 4bb963c
Merge branch 'main' into stac-item-json
ianhorn ec2258e
Apply suggestion from @github-actions[bot]
ianhorn 02b13ec
fix(STAC): the number scan reads whole tokens, a scheme is kept only …
ianhorn 7643654
fix(STAC): a number with more than 15 digits is named as rounded, the…
ianhorn 2765f19
Merge branch 'main' into stac-item-json
ianhorn 9704b2e
fix(STAC): a document's address is absolute only by its leading schem…
ianhorn 5475351
fix(STAC): a document's item must agree on its collection, an address…
ianhorn 1a1098f
Merge branch 'main' into stac-item-json
ianhorn 5f2a8a9
fix(STAC): an asset's layer name cuts its key, and a document address…
ianhorn 2e84489
Merge branch 'stac-item-json' of https://github.com/ianhorn/RustGIS i…
ianhorn 3bf90cf
fix(STAC): the merged address resolver takes its limit and a document…
ianhorn c4c7905
fix(STAC): signing an asset keeps a search under way, the fallback JS…
ianhorn 3b3c79d
Apply batched suggestions from code review
ianhorn 2603294
Merge branch 'main' into stac-item-json
ianhorn ccae927
Merge branch 'main' into stac-item-json
ianhorn 67c3f2e
fix(STAC): a document nested too deeply says so, an asset row with no…
ianhorn 225366b
fix(STAC): signing an asset also waits while a search is being prepared
ianhorn f9e5e4c
Merge branch 'main' into stac-item-json
ianhorn 714397a
Apply suggestion from @github-actions[bot]
ianhorn 8354fb4
Merge branch 'main' into stac-item-json
ianhorn 466fc2d
fix(STAC): an item's own address is kept up to 64 KB so a long one is…
ianhorn c81270f
Merge branch 'main' into stac-item-json
ianhorn 2c0a1da
Apply suggestion from @github-actions[bot]
ianhorn b659f4a
fix(STAC): an item's long own address is not kept per item and the pa…
ianhorn 78de3f5
Merge branch 'main' into stac-item-json
ianhorn d03365e
Merge branch 'main' into stac-item-json
ianhorn 6d1a7b1
fix(STAC): an own address over any limit is said to be too long, and …
ianhorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,7 @@ const MAX_COLLECTIONS: usize = 2000; | |
| /// The most assets read from an item. | ||
| const MAX_ASSETS: usize = 100; | ||
| /// The longest text kept (titles, ids, addresses). | ||
| const MAX_TEXT: usize = 2000; | ||
| pub const MAX_TEXT: usize = 2000; | ||
| /// The most coordinates a footprint keeps. | ||
| const MAX_FOOTPRINT: usize = 20_000; | ||
| /// The most properties kept with an item. | ||
|
|
@@ -273,6 +273,8 @@ pub struct Item { | |
| pub assets: Vec<Asset>, | ||
| /// Its own address (its `self` link), which relative asset addresses are resolved against. | ||
| pub url: Option<String>, | ||
| /// It has a `self` link, but one longer than [`MAX_TEXT`], which is not kept (so `url` is `None`). | ||
| pub url_too_long: bool, | ||
| /// The address it was fetched from, which its asset addresses are judged by (never its own | ||
| /// `self` link, which it could point anywhere); `None` for an item given whole, whose assets | ||
| /// may then only be public web addresses. | ||
|
|
@@ -695,14 +697,24 @@ pub fn item_from_json(v: &Value, from: Option<&str>) -> Option<Item> { | |
| item(v, from) | ||
| } | ||
|
|
||
| /// A link's address by its relation, resolved against `from`. | ||
| fn link(v: &Value, rel: &str, from: &str) -> Option<String> { | ||
| /// A link by its relation: its plain GET entry, if it has one. | ||
| fn link_entry<'a>(v: &'a Value, rel: &str) -> Option<&'a Value> { | ||
| let l = v.get("links")?.as_array()?.iter().take(100).find(|l| l.get("rel").and_then(Value::as_str) == Some(rel))?; | ||
| // A POST link needs its body: only a plain GET is followed. | ||
| if l.get("method").and_then(Value::as_str).is_some_and(|m| !m.eq_ignore_ascii_case("GET")) { | ||
| return None; | ||
| } | ||
| resolve(from, l.get("href")?.as_str()?) | ||
| Some(l) | ||
| } | ||
|
|
||
| /// A link's address by its relation, resolved against `from`. | ||
| fn link(v: &Value, rel: &str, from: &str) -> Option<String> { | ||
| resolve(from, link_entry(v, rel)?.get("href")?.as_str()?) | ||
| } | ||
|
|
||
| /// Whether a link has an address longer than the text limit, which [`link`] refuses. | ||
| fn link_too_long(v: &Value, rel: &str) -> bool { | ||
| link_entry(v, rel).and_then(|l| l.get("href")?.as_str()).is_some_and(|h| h.trim().len() > MAX_TEXT) | ||
| } | ||
|
|
||
| fn item(f: &Value, from: Option<&str>) -> Option<Item> { | ||
|
|
@@ -722,6 +734,8 @@ pub(crate) fn item_with_reach(f: &Value, from: Option<&str>, reach: Option<crate | |
| Some(from) => link(f, "self", from), | ||
| None => link(f, "self", "").filter(|u| is_web(u)), | ||
| }; | ||
| // Kept on every item of a search only up to the text limit; a longer one is said to be so. | ||
| let url_too_long = url.is_none() && link_too_long(f, "self"); | ||
| let base = url.clone().or_else(|| from.map(str::to_string)); | ||
| let props = f.get("properties"); | ||
| let bbox = f.get("bbox").and_then(Value::as_array).and_then(|b| { | ||
|
|
@@ -826,6 +840,7 @@ pub(crate) fn item_with_reach(f: &Value, from: Option<&str>, reach: Option<crate | |
| cloud, | ||
| assets, | ||
| url, | ||
| url_too_long, | ||
| origin: from.map(str::to_string), | ||
| origin_reach, | ||
| properties, | ||
|
|
@@ -898,10 +913,23 @@ fn footprint_size(g: &Value) -> usize { | |
| /// An address relative to another, as a browser would resolve it (`s3://` and others are kept | ||
| /// as they are, so they can be refused by name). | ||
| pub fn resolve(base: &str, href: &str) -> Option<String> { | ||
| resolve_up_to(base, href, MAX_TEXT, false) | ||
| } | ||
|
|
||
| /// [`resolve`] for an address from an item's own document, which is not cut to the parsed item's | ||
| /// text limit: up to [`MAX_DOCUMENT_HREF`] bytes (64 KB), so a long relative address still resolves. | ||
| pub fn resolve_document_href(base: &str, href: &str) -> Option<String> { | ||
| resolve_up_to(base, href, MAX_DOCUMENT_HREF, true) | ||
| } | ||
|
|
||
| /// The most bytes of an address taken from an item's own document. | ||
| pub const MAX_DOCUMENT_HREF: usize = 64 << 10; | ||
|
|
||
| fn resolve_up_to(base: &str, href: &str, limit: usize, schemes: bool) -> Option<String> { | ||
| // A login binding rides along only to an address on the base's own origin (`inherit`). | ||
| let plain_base = rustgis_data::login_address::parse(base).ok()?.map_or(base, |b| b.url); | ||
| rustgis_data::login_address::parse(href).ok()?; | ||
| let resolved = resolve_plain(plain_base, crate::logins::inner(href))?; | ||
| let resolved = resolve_plain(plain_base, crate::logins::inner(href), limit, schemes)?; | ||
|
ianhorn marked this conversation as resolved.
Resolved
|
||
| Some(crate::logins::inherit(base, &resolved)) | ||
| } | ||
|
|
||
|
|
@@ -912,12 +940,24 @@ fn has_scheme(s: &str) -> bool { | |
| bytes.next().is_some_and(|b| b.is_ascii_alphabetic()) && bytes.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.')) | ||
| } | ||
|
|
||
| fn resolve_plain(base: &str, href: &str) -> Option<String> { | ||
| /// Whether `href` begins with a URI scheme even without `//` (`data:`, `mailto:`): a letter, then | ||
| /// letters, digits, `+`, `-` or `.`, then a colon. A single letter (`c:/a.tif`) is a drive, not a | ||
| /// scheme, which matches what `check_asset_scheme` names. | ||
| fn has_uri_scheme(href: &str) -> bool { | ||
| href.split_once(':').is_some_and(|(s, rest)| { | ||
| s.starts_with(|c: char| c.is_ascii_alphabetic()) | ||
| && s.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')) | ||
| && (s.len() > 1 || !rest.starts_with(['/', '\\'])) | ||
| }) | ||
| } | ||
|
ianhorn marked this conversation as resolved.
Resolved
|
||
|
|
||
| fn resolve_plain(base: &str, href: &str, limit: usize, schemes: bool) -> Option<String> { | ||
| let href = href.trim(); | ||
| if href.is_empty() || href.len() > MAX_TEXT || href.chars().any(char::is_control) { | ||
| if href.is_empty() || href.len() > limit || href.chars().any(char::is_control) { | ||
| return None; | ||
| } | ||
| if has_scheme(href) { | ||
| // A document's address with a scheme of its own (`data:`, `mailto:`) is kept as written too. | ||
| if has_scheme(href) || (schemes && has_uri_scheme(href)) { | ||
| return Some(href.to_string()); | ||
| } | ||
| let (scheme, rest) = base.split_once("://")?; | ||
|
|
@@ -1038,19 +1078,47 @@ impl Item { | |
| /// may point to, not one that needs signing or a cloud store's own protocol. | ||
| pub fn asset_href(&self, key: Option<&str>) -> Result<(String, String), String> { | ||
| let a = self.pick(key)?; | ||
| let lower = crate::logins::inner(&a.href).to_ascii_lowercase(); | ||
| if !(lower.starts_with("https://") || lower.starts_with("http://") || crate::s3::is_s3(&lower)) { | ||
| let scheme = lower.split("://").next().unwrap_or(""); | ||
| return Err(format!( | ||
| "{}: asset '{}' is at {} (a {scheme}:// address); RustGIS adds assets by their web or s3:// address", | ||
| self.id, a.key, a.href | ||
| )); | ||
| } | ||
| self.check_asset_href(&a.key, &a.href)?; | ||
| Ok((a.key.clone(), a.href.clone())) | ||
| } | ||
|
|
||
| /// Checks an address given for asset `key` as [`Item::asset_href`] checks the item's own: a | ||
| /// web or s3:// address that the item's origin and reach allow. | ||
| pub fn check_asset_href(&self, key: &str, href: &str) -> Result<(), String> { | ||
| self.check_asset_scheme(key, href)?; | ||
| // Judged by where the item came from; an item given whole may only point at public | ||
| // addresses (as a document on the web may). A Planetary Computer asset is signed by the | ||
| // caller ([`needs_signing`]). | ||
| self.check_href(&a.href)?; | ||
| Ok((a.key.clone(), a.href.clone())) | ||
| self.check_href(href) | ||
| } | ||
|
|
||
| /// The scheme half of [`Item::check_asset_href`], for an address judged against another | ||
| /// document's origin: a web or s3:// address, else an error naming what it is. | ||
| pub fn check_asset_scheme(&self, key: &str, href: &str) -> Result<(), String> { | ||
| let lower = crate::logins::inner(href).to_ascii_lowercase(); | ||
| if lower.starts_with("https://") || lower.starts_with("http://") || crate::s3::is_s3(&lower) { | ||
| return Ok(()); | ||
| } | ||
| // The scheme is the letters before the first colon (`data:` and `mailto:` have no `//`). | ||
| let scheme = lower | ||
|
ianhorn marked this conversation as resolved.
Resolved
|
||
| .split_once(':') | ||
| .filter(|(s, rest)| s.len() > 1 || !rest.starts_with(['/', '\\'])) | ||
| .map(|(s, _)| s) | ||
| .filter(|s| s.starts_with(|c: char| c.is_ascii_alphabetic())) | ||
| .filter(|s| s.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))); | ||
| let slashes = lower.split_once(':').is_some_and(|(_, rest)| rest.starts_with("//")); | ||
| let what = match (scheme, slashes) { | ||
| (Some(scheme), true) => format!("a {scheme}:// address"), | ||
| (Some(scheme), false) => format!("a {scheme}: address"), | ||
| (None, _) => "a relative address that couldn't be made absolute".to_string(), | ||
| }; | ||
| let cut = href.chars().count() > 200; | ||
| let shown: String = href.chars().take(200).chain(cut.then_some('…')).collect(); | ||
| Err(format!( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two small points on this message, grouped:
Both are wording and consistency points, not wrong results. Category: quality |
||
| "{}: asset '{}' is at {shown} ({what}); RustGIS adds assets by their web or s3:// address", | ||
| self.id, | ||
| key.chars().take(200).collect::<String>() | ||
| )) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1658,6 +1726,13 @@ mod tests { | |
| let i = item_from_json(&f("item.json", "https://data.example.org/x.tif"), None).unwrap(); | ||
| assert!(i.asset_href(None).is_ok()); | ||
| assert!(item_from_json(&f("item.json", "x.tif"), None).unwrap().assets.is_empty()); | ||
| // A self address past the 2,000-byte text limit is not kept, and the item says so. | ||
| for n in [3000, 100_000] { | ||
| let long = format!("https://stac.example.org/items/{}", "a".repeat(n)); | ||
| let i = item_from_json(&f(&long, "https://data.example.org/x.tif"), None).unwrap(); | ||
| assert!(i.url.is_none() && i.url_too_long, "{n}"); | ||
| } | ||
| assert!(!item_from_json(&f("item.json", "https://data.example.org/x.tif"), None).unwrap().url_too_long); | ||
| // Dates: each side a date, a date and time, or open. | ||
| for good in ["2024-06-01", "2024-06-01/2024-06-30", "2024-06-01/..", "../2024-06-30", "2024-06-01T10:00:00Z/.."] { | ||
| assert!(datetime(good).is_ok(), "{good}"); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.