2021-02-19 15:44:06 +00:00
|
|
|
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
2020-04-24 18:47:11 +00:00
|
|
|
use std::fmt::Write;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-04-24 18:56:00 +00:00
|
|
|
use chrono::{DateTime, NaiveDateTime, SecondsFormat, Utc};
|
2020-07-07 15:15:53 +00:00
|
|
|
use hyper::{Body, Response};
|
2020-04-24 18:47:11 +00:00
|
|
|
|
2021-02-19 15:44:06 +00:00
|
|
|
use garage_util::error::Error as GarageError;
|
2020-04-24 18:47:11 +00:00
|
|
|
|
2020-07-07 11:59:22 +00:00
|
|
|
use garage_model::garage::Garage;
|
2020-07-08 15:33:24 +00:00
|
|
|
use garage_model::object_table::*;
|
2020-04-24 18:47:11 +00:00
|
|
|
|
2020-11-20 19:11:04 +00:00
|
|
|
use garage_table::DeletedFilter;
|
|
|
|
|
2020-04-28 10:18:14 +00:00
|
|
|
use crate::encoding::*;
|
2021-02-19 15:44:06 +00:00
|
|
|
use crate::error::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ListObjectsQuery {
|
|
|
|
pub is_v2: bool,
|
|
|
|
pub bucket: String,
|
|
|
|
pub delimiter: Option<String>,
|
|
|
|
pub max_keys: usize,
|
|
|
|
pub prefix: String,
|
|
|
|
pub marker: Option<String>,
|
|
|
|
pub continuation_token: Option<String>,
|
|
|
|
pub start_after: Option<String>,
|
|
|
|
pub urlencode_resp: bool,
|
|
|
|
}
|
2020-04-24 18:47:11 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct ListResultInfo {
|
|
|
|
last_modified: u64,
|
|
|
|
size: u64,
|
2020-12-06 14:39:03 +00:00
|
|
|
etag: String,
|
2020-04-24 18:47:11 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:44:06 +00:00
|
|
|
pub fn parse_list_objects_query(
|
|
|
|
bucket: &str,
|
|
|
|
params: &HashMap<String, String>,
|
|
|
|
) -> Result<ListObjectsQuery, Error> {
|
|
|
|
Ok(ListObjectsQuery {
|
|
|
|
is_v2: params.get("list-type").map(|x| x == "2").unwrap_or(false),
|
|
|
|
bucket: bucket.to_string(),
|
|
|
|
delimiter: params.get("delimiter").cloned(),
|
|
|
|
max_keys: params
|
|
|
|
.get("max-keys")
|
|
|
|
.map(|x| {
|
|
|
|
x.parse::<usize>()
|
|
|
|
.ok_or_bad_request("Invalid value for max-keys")
|
|
|
|
})
|
|
|
|
.unwrap_or(Ok(1000))?,
|
|
|
|
prefix: params.get("prefix").cloned().unwrap_or(String::new()),
|
|
|
|
marker: params.get("marker").cloned(),
|
|
|
|
continuation_token: params.get("continuation-token").cloned(),
|
|
|
|
start_after: params.get("start-after").cloned(),
|
|
|
|
urlencode_resp: params
|
|
|
|
.get("encoding-type")
|
|
|
|
.map(|x| x == "url")
|
|
|
|
.unwrap_or(false),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:47:11 +00:00
|
|
|
pub async fn handle_list(
|
|
|
|
garage: Arc<Garage>,
|
2021-02-19 15:44:06 +00:00
|
|
|
query: &ListObjectsQuery,
|
2020-07-07 15:15:53 +00:00
|
|
|
) -> Result<Response<Body>, Error> {
|
2020-04-26 16:22:33 +00:00
|
|
|
let mut result_keys = BTreeMap::<String, ListResultInfo>::new();
|
|
|
|
let mut result_common_prefixes = BTreeSet::<String>::new();
|
2020-05-01 14:30:50 +00:00
|
|
|
|
2021-02-19 15:44:06 +00:00
|
|
|
let mut next_chunk_start = if query.is_v2 {
|
|
|
|
if let Some(ct) = &query.continuation_token {
|
|
|
|
String::from_utf8(base64::decode(ct.as_bytes())?)?
|
|
|
|
} else {
|
|
|
|
query.start_after.clone().unwrap_or(query.prefix.clone())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query.marker.clone().unwrap_or(query.prefix.clone())
|
|
|
|
};
|
2020-04-24 18:47:11 +00:00
|
|
|
|
2021-02-19 15:44:06 +00:00
|
|
|
debug!(
|
|
|
|
"List request: `{:?}` {} `{}`",
|
|
|
|
query.delimiter, query.max_keys, query.prefix
|
|
|
|
);
|
2020-04-24 18:47:11 +00:00
|
|
|
|
2020-05-01 15:52:35 +00:00
|
|
|
let truncated;
|
|
|
|
'query_loop: loop {
|
2020-04-24 18:47:11 +00:00
|
|
|
let objects = garage
|
|
|
|
.object_table
|
|
|
|
.get_range(
|
2021-02-19 15:44:06 +00:00
|
|
|
&query.bucket,
|
2020-04-24 18:47:11 +00:00
|
|
|
Some(next_chunk_start.clone()),
|
2020-11-20 19:11:04 +00:00
|
|
|
Some(DeletedFilter::NotDeleted),
|
2021-02-19 15:44:06 +00:00
|
|
|
query.max_keys + 1,
|
2020-04-24 18:47:11 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2020-05-01 14:30:50 +00:00
|
|
|
debug!(
|
|
|
|
"List: get range {} (max {}), results: {}",
|
|
|
|
next_chunk_start,
|
2021-02-19 15:44:06 +00:00
|
|
|
query.max_keys + 1,
|
2020-05-01 14:30:50 +00:00
|
|
|
objects.len()
|
|
|
|
);
|
|
|
|
|
2020-04-24 18:47:11 +00:00
|
|
|
for object in objects.iter() {
|
2021-02-19 15:44:06 +00:00
|
|
|
if !object.key.starts_with(&query.prefix) {
|
2020-12-06 14:39:03 +00:00
|
|
|
truncated = None;
|
2020-05-01 15:52:35 +00:00
|
|
|
break 'query_loop;
|
2020-05-01 14:30:50 +00:00
|
|
|
}
|
2021-02-19 15:44:06 +00:00
|
|
|
|
|
|
|
if query.is_v2 && query.start_after.as_ref() == Some(&object.key) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:55:13 +00:00
|
|
|
if let Some(version) = object.versions().iter().find(|x| x.is_data()) {
|
2021-02-19 15:44:06 +00:00
|
|
|
if result_keys.len() + result_common_prefixes.len() >= query.max_keys {
|
2020-12-06 14:39:03 +00:00
|
|
|
truncated = Some(object.key.to_string());
|
2020-05-04 13:09:23 +00:00
|
|
|
break 'query_loop;
|
|
|
|
}
|
2021-02-19 15:44:06 +00:00
|
|
|
let common_prefix = if let Some(delimiter) = &query.delimiter {
|
|
|
|
let relative_key = &object.key[query.prefix.len()..];
|
2020-11-11 15:12:42 +00:00
|
|
|
relative_key
|
|
|
|
.find(delimiter)
|
2021-02-19 15:44:06 +00:00
|
|
|
.map(|i| &object.key[..query.prefix.len() + i + delimiter.len()])
|
2020-04-24 20:28:15 +00:00
|
|
|
} else {
|
2020-04-26 16:22:33 +00:00
|
|
|
None
|
2020-04-24 18:47:11 +00:00
|
|
|
};
|
2020-04-26 16:22:33 +00:00
|
|
|
if let Some(pfx) = common_prefix {
|
|
|
|
result_common_prefixes.insert(pfx.to_string());
|
|
|
|
} else {
|
2020-12-06 14:39:03 +00:00
|
|
|
let meta = match &version.state {
|
|
|
|
ObjectVersionState::Complete(ObjectVersionData::Inline(meta, _)) => meta,
|
2020-07-08 15:34:37 +00:00
|
|
|
ObjectVersionState::Complete(ObjectVersionData::FirstBlock(meta, _)) => {
|
2020-12-06 14:39:03 +00:00
|
|
|
meta
|
2020-07-08 15:34:37 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-04-26 16:22:33 +00:00
|
|
|
let info = match result_keys.get(&object.key) {
|
|
|
|
None => ListResultInfo {
|
|
|
|
last_modified: version.timestamp,
|
2020-12-06 14:39:03 +00:00
|
|
|
size: meta.size,
|
|
|
|
etag: meta.etag.to_string(),
|
2020-04-26 16:22:33 +00:00
|
|
|
},
|
2020-04-26 18:55:13 +00:00
|
|
|
Some(_lri) => {
|
2021-02-19 15:44:06 +00:00
|
|
|
return Err(Error::InternalError(GarageError::Message(format!(
|
|
|
|
"Duplicate key?? {}",
|
|
|
|
object.key
|
|
|
|
))))
|
2020-04-26 18:55:13 +00:00
|
|
|
}
|
2020-04-26 16:22:33 +00:00
|
|
|
};
|
|
|
|
result_keys.insert(object.key.clone(), info);
|
2020-04-24 18:47:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 15:44:06 +00:00
|
|
|
if objects.len() < query.max_keys + 1 {
|
2020-12-06 14:39:03 +00:00
|
|
|
truncated = None;
|
2020-05-04 13:09:23 +00:00
|
|
|
break 'query_loop;
|
2020-04-24 18:47:11 +00:00
|
|
|
}
|
|
|
|
if objects.len() > 0 {
|
|
|
|
next_chunk_start = objects[objects.len() - 1].key.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut xml = String::new();
|
|
|
|
writeln!(&mut xml, r#"<?xml version="1.0" encoding="UTF-8"?>"#).unwrap();
|
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
r#"<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">"#
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-19 15:44:06 +00:00
|
|
|
|
|
|
|
writeln!(&mut xml, "\t<Name>{}</Name>", query.bucket).unwrap();
|
|
|
|
|
|
|
|
// TODO: in V1, is this supposed to be urlencoded when encoding-type is URL??
|
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
"\t<Prefix>{}</Prefix>",
|
|
|
|
xml_encode_key(&query.prefix, query.urlencode_resp),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if let Some(delim) = &query.delimiter {
|
|
|
|
// TODO: in V1, is this supposed to be urlencoded when encoding-type is URL??
|
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
"\t<Delimiter>{}</Delimiter>",
|
|
|
|
xml_encode_key(delim, query.urlencode_resp),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
writeln!(&mut xml, "\t<MaxKeys>{}</MaxKeys>", query.max_keys).unwrap();
|
|
|
|
if query.urlencode_resp {
|
|
|
|
writeln!(&mut xml, "\t<EncodingType>url</EncodingType>").unwrap();
|
2020-12-06 14:39:03 +00:00
|
|
|
}
|
2021-02-19 15:44:06 +00:00
|
|
|
|
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
"\t<KeyCount>{}</KeyCount>",
|
|
|
|
result_keys.len() + result_common_prefixes.len()
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-12-06 14:39:03 +00:00
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
"\t<IsTruncated>{}</IsTruncated>",
|
|
|
|
truncated.is_some()
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-02-19 15:44:06 +00:00
|
|
|
|
|
|
|
if query.is_v2 {
|
|
|
|
if let Some(ct) = &query.continuation_token {
|
|
|
|
writeln!(&mut xml, "\t<ContinuationToken>{}</ContinuationToken>", ct).unwrap();
|
|
|
|
}
|
|
|
|
if let Some(sa) = &query.start_after {
|
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
"\t<StartAfter>{}</StartAfter>",
|
|
|
|
xml_encode_key(sa, query.urlencode_resp)
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
if let Some(nct) = truncated {
|
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
"\t<NextContinuationToken>{}</NextContinuationToken>",
|
|
|
|
base64::encode(nct.as_bytes())
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: are these supposed to be urlencoded when encoding-type is URL??
|
|
|
|
if let Some(mkr) = &query.marker {
|
|
|
|
writeln!(&mut xml, "\t<Marker>{}</Marker>", xml_encode_key(mkr, query.urlencode_resp)).unwrap();
|
|
|
|
}
|
|
|
|
if let Some(next_marker) = truncated {
|
|
|
|
writeln!(&mut xml, "\t<NextMarker>{}</NextMarker>", xml_encode_key(&next_marker, query.urlencode_resp)).unwrap();
|
|
|
|
}
|
2020-12-06 14:39:03 +00:00
|
|
|
}
|
2021-02-19 15:44:06 +00:00
|
|
|
|
2020-04-26 16:22:33 +00:00
|
|
|
for (key, info) in result_keys.iter() {
|
2020-04-24 18:47:11 +00:00
|
|
|
let last_modif = NaiveDateTime::from_timestamp(info.last_modified as i64 / 1000, 0);
|
|
|
|
let last_modif = DateTime::<Utc>::from_utc(last_modif, Utc);
|
|
|
|
let last_modif = last_modif.to_rfc3339_opts(SecondsFormat::Millis, true);
|
|
|
|
writeln!(&mut xml, "\t<Contents>").unwrap();
|
2020-04-28 10:18:14 +00:00
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
"\t\t<Key>{}</Key>",
|
2021-02-19 15:44:06 +00:00
|
|
|
xml_encode_key(key, query.urlencode_resp),
|
2020-04-28 10:18:14 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2020-04-24 18:47:11 +00:00
|
|
|
writeln!(&mut xml, "\t\t<LastModified>{}</LastModified>", last_modif).unwrap();
|
|
|
|
writeln!(&mut xml, "\t\t<Size>{}</Size>", info.size).unwrap();
|
2020-12-06 14:39:03 +00:00
|
|
|
if !info.etag.is_empty() {
|
|
|
|
writeln!(&mut xml, "\t\t<ETag>\"{}\"</ETag>", info.etag).unwrap();
|
|
|
|
}
|
2020-04-24 18:47:11 +00:00
|
|
|
writeln!(&mut xml, "\t\t<StorageClass>STANDARD</StorageClass>").unwrap();
|
|
|
|
writeln!(&mut xml, "\t</Contents>").unwrap();
|
|
|
|
}
|
2021-02-19 15:44:06 +00:00
|
|
|
|
|
|
|
for pfx in result_common_prefixes.iter() {
|
|
|
|
writeln!(&mut xml, "\t<CommonPrefixes>").unwrap();
|
|
|
|
//TODO: in V1, are these urlencoded when urlencode_resp is true ?? (proably)
|
|
|
|
writeln!(
|
|
|
|
&mut xml,
|
|
|
|
"\t\t<Prefix>{}</Prefix>",
|
|
|
|
xml_encode_key(pfx, query.urlencode_resp),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
writeln!(&mut xml, "\t</CommonPrefixes>").unwrap();
|
2020-04-26 16:22:33 +00:00
|
|
|
}
|
2021-02-19 15:44:06 +00:00
|
|
|
|
2020-04-24 18:47:11 +00:00
|
|
|
writeln!(&mut xml, "</ListBucketResult>").unwrap();
|
2021-01-16 15:05:54 +00:00
|
|
|
debug!("{}", xml);
|
2020-04-24 18:47:11 +00:00
|
|
|
|
2021-02-19 22:40:18 +00:00
|
|
|
Ok(Response::builder()
|
|
|
|
.header("Content-Type", "application/xml")
|
|
|
|
.body(Body::from(xml.into_bytes()))?)
|
2020-04-26 18:55:13 +00:00
|
|
|
}
|