use std::{collections::BTreeMap, sync::Arc};
use bytes::Bytes;
use futures::stream;
use gql::Upload;
use multer::Multipart;
use serde_json::Value as JsonValue;
use crate::request::{GraphQLRequest, RequestError, RequestPayload, UploadBinding};
pub const DEFAULT_MAX_MULTIPART_PARTS: usize = 32;
pub async fn parse(
content_type: &str,
body: &[u8],
max_parts: usize,
max_batch_size: usize,
) -> Result<RequestPayload, RequestError> {
parse_bytes(
content_type,
Bytes::copy_from_slice(body),
max_parts,
max_batch_size,
)
.await
}
pub(crate) async fn parse_bytes(
content_type: &str,
body: Bytes,
max_parts: usize,
max_batch_size: usize,
) -> Result<RequestPayload, RequestError> {
let boundary = multer::parse_boundary(content_type)
.map_err(|error| invalid(format!("invalid multipart content type: {error}")))?;
let stream = stream::once(async move { Ok::<Bytes, std::io::Error>(body) });
let mut multipart = Multipart::new(stream, boundary);
let mut operations: Option<JsonValue> = None;
let mut map: Option<BTreeMap<String, Vec<MappedPath>>> = None;
let mut files: BTreeMap<String, Upload> = BTreeMap::new();
let mut parts = 0usize;
while let Some(field) = multipart
.next_field()
.await
.map_err(|error| invalid(format!("invalid multipart body: {error}")))?
{
parts += 1;
if max_parts != 0 && parts > max_parts {
return Err(RequestError::new(format!(
"A multipart request may contain at most {max_parts} parts."
)));
}
match field.name() {
Some("operations") if operations.is_none() && map.is_none() && files.is_empty() => {
let text = field
.text()
.await
.map_err(|error| invalid(format!("could not read `operations`: {error}")))?;
let value: JsonValue = serde_json::from_str(&text)
.map_err(|error| invalid(format!("invalid `operations` JSON: {error}")))?;
match &value {
JsonValue::Object(_) => {}
JsonValue::Array(requests) => {
if requests.is_empty() {
return Err(invalid(
"a batch `operations` array must contain at least one request",
));
}
if max_batch_size != 0 && requests.len() > max_batch_size {
return Err(RequestError::new(format!(
"A batch request may contain at most {max_batch_size} operations."
)));
}
}
_ => {
return Err(invalid(
"`operations` must be an object or an array of requests",
));
}
}
operations = Some(value);
}
Some("map") if map.is_none() && operations.is_some() && files.is_empty() => {
let Some(operations) = operations.as_ref() else {
unreachable!("the match guard requires operations");
};
let text = field
.text()
.await
.map_err(|error| invalid(format!("could not read `map`: {error}")))?;
map = Some(parse_map(&text, operations)?);
}
Some(name) => {
if name == "operations" && operations.is_some() && map.is_none() {
return Err(invalid("the `operations` field appears more than once"));
}
if name == "map" && operations.is_none() {
return Err(invalid("`operations` must appear before `map`"));
}
if name == "map"
&& map.as_ref().is_some_and(|map| !map.contains_key("map"))
&& files.is_empty()
{
return Err(invalid("the `map` field appears more than once"));
}
let name = name.to_owned();
let Some(map) = map.as_ref() else {
return Err(invalid(
"file fields must follow the `operations` and `map` fields",
));
};
if !map.contains_key(&name) {
return Err(invalid(format!(
"file field \"{name}\" is not listed in the multipart `map`"
)));
}
if files.contains_key(&name) {
return Err(invalid(format!(
"file field \"{name}\" appears more than once"
)));
}
let filename = field
.file_name()
.map(Arc::<str>::from)
.ok_or_else(|| invalid(format!("field \"{name}\" must be a file")))?;
let content_type = field
.content_type()
.map(|mime| Arc::<str>::from(mime.as_ref()));
let bytes = field
.bytes()
.await
.map_err(|error| invalid(format!("could not read file \"{name}\": {error}")))?;
files.insert(
name,
Upload::from_parts(Some(filename), content_type, Arc::from(bytes.as_ref())),
);
}
None => {
return Err(invalid("every multipart part must have a field name"));
}
}
}
let operations = operations.ok_or_else(|| invalid("missing the `operations` field"))?;
let map = map.ok_or_else(|| invalid("missing the `map` field"))?;
for (name, paths) in &map {
if !files.contains_key(name) {
return Err(invalid(format!(
"no file was provided for map entry \"{name}\""
)));
}
if paths.is_empty() {
return Err(invalid(format!(
"map entry \"{name}\" must list at least one operations path"
)));
}
}
let (mut requests, batch) = into_requests(operations)?;
for (name, mut paths) in map {
let upload = files
.remove(&name)
.ok_or_else(|| invalid(format!("no file was provided for map entry \"{name}\"")))?;
let Some(last) = paths.pop() else {
continue;
};
for path in paths {
bind(&mut requests, path, upload.clone(), &name)?;
}
bind(&mut requests, last, upload, &name)?;
}
Ok(if batch {
RequestPayload::Batch(requests)
} else {
RequestPayload::Single(
requests
.pop()
.ok_or_else(|| invalid("missing the `operations` field"))?,
)
})
}
fn bind(
requests: &mut [GraphQLRequest],
path: MappedPath,
upload: Upload,
name: &str,
) -> Result<(), RequestError> {
let request_count = requests.len();
let request = requests.get_mut(path.request_index).ok_or_else(|| {
invalid(format!(
"map entry \"{name}\" refers to batch index {}, but the batch has {request_count} requests",
path.request_index
))
})?;
request
.uploads
.push(UploadBinding::new(path.relative, upload));
Ok(())
}
struct MappedPath {
request_index: usize,
relative: String,
}
fn parse_map(
text: &str,
operations: &JsonValue,
) -> Result<BTreeMap<String, Vec<MappedPath>>, RequestError> {
let raw: JsonValue = serde_json::from_str(text)
.map_err(|error| invalid(format!("invalid `map` JSON: {error}")))?;
let raw = raw
.as_object()
.ok_or_else(|| invalid("`map` must be a JSON object"))?;
let batch_len = operations.as_array().map(Vec::len);
let mut seen: Vec<(usize, String)> = Vec::new();
let mut map = BTreeMap::new();
for (name, paths) in raw {
let paths = paths
.as_array()
.ok_or_else(|| invalid(format!("map entry \"{name}\" must be an array of paths")))?;
let mut mapped = Vec::with_capacity(paths.len());
for path in paths {
let path = path.as_str().ok_or_else(|| {
invalid(format!(
"map entry \"{name}\" contains a path that is not a string"
))
})?;
let mapped_path = parse_path(path, operations, batch_len)?;
if seen.iter().any(|(index, relative)| {
*index == mapped_path.request_index && relative == &mapped_path.relative
}) {
return Err(invalid(format!(
"map paths contain \"{path}\" more than once"
)));
}
seen.push((mapped_path.request_index, mapped_path.relative.clone()));
mapped.push(mapped_path);
}
map.insert(name.clone(), mapped);
}
Ok(map)
}
fn parse_path(
path: &str,
operations: &JsonValue,
batch_len: Option<usize>,
) -> Result<MappedPath, RequestError> {
if path.is_empty() || path.starts_with('.') || path.ends_with('.') || path.contains("..") {
return Err(invalid(format!(
"map path \"{path}\" contains an empty segment"
)));
}
let (request_index, relative) = match batch_len {
Some(len) => {
let (index, relative) = path.split_once('.').unwrap_or((path, ""));
let index = index.parse::<usize>().map_err(|_| {
invalid(format!(
"map path \"{path}\" must start with a batch index because `operations` is an array"
))
})?;
if index >= len {
return Err(invalid(format!(
"map path \"{path}\" refers to batch index {index}, but the batch has {len} requests"
)));
}
(index, relative)
}
None => (0, path),
};
let mut segments = relative.split('.');
if segments.next() != Some("variables") {
return Err(invalid(format!(
"map path \"{path}\" must address `variables`; files may only be placed in request variables"
)));
}
if segments.next().is_none() {
return Err(invalid(format!(
"map path \"{path}\" does not name a variable"
)));
}
let mut value = match batch_len {
Some(_) => &operations[request_index],
None => operations,
};
for segment in relative.split('.') {
value = match value {
JsonValue::Object(object) => object.get(segment),
JsonValue::Array(list) => segment
.parse::<usize>()
.ok()
.and_then(|index| list.get(index)),
_ => None,
}
.ok_or_else(|| {
invalid(format!(
"map path \"{path}\" does not exist in `operations`"
))
})?;
}
if !value.is_null() {
return Err(invalid(format!(
"map path \"{path}\" points at a value that is not `null`; the multipart specification requires files to be replaced with `null`"
)));
}
Ok(MappedPath {
request_index,
relative: relative.to_owned(),
})
}
fn into_requests(operations: JsonValue) -> Result<(Vec<GraphQLRequest>, bool), RequestError> {
match operations {
JsonValue::Array(requests) => {
let requests =
serde_json::from_value::<Vec<GraphQLRequest>>(JsonValue::Array(requests))
.map_err(|error| invalid(format!("invalid `operations` JSON: {error}")))?;
Ok((requests, true))
}
operation => {
let request = serde_json::from_value::<GraphQLRequest>(operation)
.map_err(|error| invalid(format!("invalid `operations` JSON: {error}")))?;
Ok((vec![request], false))
}
}
}
fn invalid(message: impl Into<String>) -> RequestError {
RequestError::new(format!("Invalid multipart request: {}", message.into()))
}