diff --git a/src/http/mod.rs b/src/http/mod.rs index 86bb5bbf..d1d5f956 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -63,12 +63,33 @@ pub async fn receive_batch_body( )) } } - - // default to json and try that - _ => receive_batch_json(body).await, + // application/json or cbor (currently) + // cbor is in application/octet-stream. + // 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 { + 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. pub async fn receive_json(body: impl AsyncRead) -> Result { receive_batch_json(body).await?.into_single() diff --git a/src/http/multipart.rs b/src/http/multipart.rs index c26927ea..967f9359 100644 --- a/src/http/multipart.rs +++ b/src/http/multipart.rs @@ -65,20 +65,45 @@ pub(super) async fn receive_batch_multipart( let mut files = Vec::new(); 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() { Some("operations") => { - let request_str = field.text().await?; + let body = field.bytes().await?; request = Some( - serde_json::from_str::(&request_str) - .map_err(|e| ParseRequestError::InvalidRequest(Box::new(e)))?, - ); + super::receive_batch_body_no_multipart(&content_type, body.as_ref()).await?, + ) } Some("map") => { - let map_str = field.text().await?; - map = Some( - serde_json::from_str::>>(&map_str) - .map_err(|e| ParseRequestError::InvalidFilesMap(Box::new(e)))?, - ); + 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( + serde_cbor::from_slice::>>(&map_bytes) + .map_err(|e| ParseRequestError::InvalidFilesMap(Box::new(e)))?, + ); + } + // application/json -> try json + (mime::APPLICATION, mime::JSON) | _ => { + map = Some( + serde_json::from_slice::>>(&map_bytes) + .map_err(|e| ParseRequestError::InvalidFilesMap(Box::new(e)))?, + ); + } + } } _ => { if let Some(name) = field.name().map(ToString::to_string) {