Merge feat/multipart into master
This commit is contained in:
commit
b95852cf28
|
@ -63,12 +63,33 @@ pub async fn receive_batch_body(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// application/json or cbor (currently)
|
||||||
// default to json and try that
|
// cbor is in application/octet-stream.
|
||||||
_ => receive_batch_json(body).await,
|
// TODO: wait for mime to add application/cbor and match against that too
|
||||||
|
(mime::APPLICATION, mime::JSON)
|
||||||
|
| (mime::OCTET_STREAM, _)
|
||||||
|
| (mime::APPLICATION, mime::OCTET_STREAM)
|
||||||
|
| _ => receive_batch_body_no_multipart(&content_type, body).await,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Recieves a GraphQL query which is either cbor or json but NOT multipart
|
||||||
|
/// This method is only to avoid recursive calls with [``receive_batch_body``] and [``multipart::receive_batch_multipart``]
|
||||||
|
pub(super) async fn receive_batch_body_no_multipart(
|
||||||
|
content_type: &mime::Mime,
|
||||||
|
body: impl AsyncRead + Send,
|
||||||
|
) -> Result<BatchRequest, ParseRequestError> {
|
||||||
|
assert_ne!(content_type.type_(), mime::MULTIPART, "received multipart");
|
||||||
|
match (content_type.type_(), content_type.subtype()) {
|
||||||
|
// cbor is in application/octet-stream.
|
||||||
|
// TODO: wait for mime to add application/cbor and match against that too
|
||||||
|
(mime::OCTET_STREAM, _) | (mime::APPLICATION, mime::OCTET_STREAM) => {
|
||||||
|
receive_batch_cbor(body).await
|
||||||
|
}
|
||||||
|
// application/json -> try json
|
||||||
|
(mime::APPLICATION, mime::JSON) | _ => receive_batch_json(body).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
/// Receive a GraphQL request from a body as JSON.
|
/// Receive a GraphQL request from a body as JSON.
|
||||||
pub async fn receive_json(body: impl AsyncRead) -> Result<Request, ParseRequestError> {
|
pub async fn receive_json(body: impl AsyncRead) -> Result<Request, ParseRequestError> {
|
||||||
receive_batch_json(body).await?.into_single()
|
receive_batch_json(body).await?.into_single()
|
||||||
|
|
|
@ -65,21 +65,46 @@ pub(super) async fn receive_batch_multipart(
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
|
|
||||||
while let Some(mut field) = multipart.next_field().await? {
|
while let Some(mut field) = multipart.next_field().await? {
|
||||||
|
// in multipart, each field / file can actually have a own Content-Type.
|
||||||
|
// We use this to determine the encoding of the graphql query
|
||||||
|
let content_type = field
|
||||||
|
.content_type()
|
||||||
|
// default to json
|
||||||
|
.unwrap_or(&mime::APPLICATION_JSON)
|
||||||
|
.clone();
|
||||||
match field.name() {
|
match field.name() {
|
||||||
Some("operations") => {
|
Some("operations") => {
|
||||||
let request_str = field.text().await?;
|
let body = field.bytes().await?;
|
||||||
request = Some(
|
request = Some(
|
||||||
serde_json::from_str::<BatchRequest>(&request_str)
|
super::receive_batch_body_no_multipart(&content_type, body.as_ref()).await?,
|
||||||
.map_err(|e| ParseRequestError::InvalidRequest(Box::new(e)))?,
|
)
|
||||||
);
|
|
||||||
}
|
}
|
||||||
Some("map") => {
|
Some("map") => {
|
||||||
let map_str = field.text().await?;
|
let map_bytes = field.bytes().await?;
|
||||||
|
// Note: we actually differ here from the inoffical spec for this:
|
||||||
|
// (https://github.com/jaydenseric/graphql-multipart-request-spec#multipart-form-field-structure)
|
||||||
|
// It says: "map: A JSON encoded map of where files occurred in the operations.
|
||||||
|
// For each file, the key is the file multipart form field name and the value is an array of operations paths."
|
||||||
|
// However, I think, that since we accepct CBOR as operation, which is valid, we should also accepct it
|
||||||
|
// as the mapping for the files.
|
||||||
|
match (content_type.type_(), content_type.subtype()) {
|
||||||
|
// cbor is in application/octet-stream.
|
||||||
|
// TODO: wait for mime to add application/cbor and match against that too
|
||||||
|
(mime::OCTET_STREAM, _) | (mime::APPLICATION, mime::OCTET_STREAM) => {
|
||||||
map = Some(
|
map = Some(
|
||||||
serde_json::from_str::<HashMap<String, Vec<String>>>(&map_str)
|
serde_cbor::from_slice::<HashMap<String, Vec<String>>>(&map_bytes)
|
||||||
.map_err(|e| ParseRequestError::InvalidFilesMap(Box::new(e)))?,
|
.map_err(|e| ParseRequestError::InvalidFilesMap(Box::new(e)))?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
// application/json -> try json
|
||||||
|
(mime::APPLICATION, mime::JSON) | _ => {
|
||||||
|
map = Some(
|
||||||
|
serde_json::from_slice::<HashMap<String, Vec<String>>>(&map_bytes)
|
||||||
|
.map_err(|e| ParseRequestError::InvalidFilesMap(Box::new(e)))?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if let Some(name) = field.name().map(ToString::to_string) {
|
if let Some(name) = field.name().map(ToString::to_string) {
|
||||||
if let Some(filename) = field.file_name().map(ToString::to_string) {
|
if let Some(filename) = field.file_name().map(ToString::to_string) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user