2020-03-26 03:34:28 +00:00
|
|
|
//! A helper module that supports HTTP
|
|
|
|
|
2020-03-05 00:39:56 +00:00
|
|
|
mod graphiql_source;
|
2020-09-10 08:43:24 +00:00
|
|
|
mod multipart;
|
2020-03-05 00:39:56 +00:00
|
|
|
mod playground_source;
|
2020-09-15 12:07:59 +00:00
|
|
|
mod websocket;
|
2021-07-31 12:52:56 +00:00
|
|
|
use mime::{self};
|
2020-10-16 06:49:22 +00:00
|
|
|
use futures_util::io::{AsyncRead, AsyncReadExt};
|
2020-10-15 06:38:10 +00:00
|
|
|
|
|
|
|
use crate::{BatchRequest, ParseRequestError, Request};
|
|
|
|
|
2020-03-05 00:39:56 +00:00
|
|
|
pub use graphiql_source::graphiql_source;
|
2020-09-17 08:39:55 +00:00
|
|
|
pub use multipart::MultipartOptions;
|
2020-06-06 01:55:53 +00:00
|
|
|
pub use playground_source::{playground_source, GraphQLPlaygroundConfig};
|
2021-05-08 06:06:46 +00:00
|
|
|
pub use websocket::{ClientMessage, Protocols as WebSocketProtocols, WebSocket, WsMessage};
|
2020-03-05 00:39:56 +00:00
|
|
|
|
2020-09-17 08:39:55 +00:00
|
|
|
/// Receive a GraphQL request from a content type and body.
|
2020-09-10 07:04:24 +00:00
|
|
|
pub async fn receive_body(
|
|
|
|
content_type: Option<impl AsRef<str>>,
|
2021-06-16 09:20:41 +00:00
|
|
|
body: impl AsyncRead + Send,
|
2020-09-10 07:04:24 +00:00
|
|
|
opts: MultipartOptions,
|
2020-09-10 08:54:38 +00:00
|
|
|
) -> Result<Request, ParseRequestError> {
|
2020-09-17 18:43:03 +00:00
|
|
|
receive_batch_body(content_type, body, opts)
|
|
|
|
.await?
|
|
|
|
.into_single()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a GraphQL request from a content type and body.
|
|
|
|
pub async fn receive_batch_body(
|
|
|
|
content_type: Option<impl AsRef<str>>,
|
2021-06-16 09:20:41 +00:00
|
|
|
body: impl AsyncRead + Send,
|
2020-09-17 18:43:03 +00:00
|
|
|
opts: MultipartOptions,
|
|
|
|
) -> Result<BatchRequest, ParseRequestError> {
|
2021-07-31 12:52:56 +00:00
|
|
|
// if no content-type header is set, we default to json
|
|
|
|
let content_type = content_type.as_ref().map(AsRef::as_ref).unwrap_or("application/json");
|
|
|
|
|
|
|
|
let content_type: mime::Mime = content_type.parse()?;
|
|
|
|
match (content_type.type_(), content_type.subtype()) {
|
|
|
|
// application/json -> try json
|
|
|
|
(mime::APPLICATION, mime::JSON) => receive_batch_json(body).await,
|
|
|
|
// 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,
|
|
|
|
// try to use multipart
|
|
|
|
(mime::MULTIPART, _) => {
|
|
|
|
if let Some(boundary) = content_type.get_param("boundary") {
|
|
|
|
multipart::receive_batch_multipart(body, boundary.to_string(), opts).await
|
|
|
|
} else {
|
|
|
|
Err(ParseRequestError::InvalidMultipart(multer::Error::NoBoundary))
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 17:30:16 +00:00
|
|
|
|
2021-07-31 12:52:56 +00:00
|
|
|
// default to json and try that
|
|
|
|
_ => receive_batch_json(body).await
|
2020-03-05 00:39:56 +00:00
|
|
|
}
|
2020-09-14 19:16:41 +00:00
|
|
|
}
|
2020-09-14 18:38:41 +00:00
|
|
|
|
2020-09-14 19:16:41 +00:00
|
|
|
/// Receive a GraphQL request from a body as JSON.
|
2020-10-15 09:33:38 +00:00
|
|
|
pub async fn receive_json(body: impl AsyncRead) -> Result<Request, ParseRequestError> {
|
2020-09-17 18:43:03 +00:00
|
|
|
receive_batch_json(body).await?.into_single()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive a GraphQL batch request from a body as JSON.
|
2020-10-15 09:33:38 +00:00
|
|
|
pub async fn receive_batch_json(body: impl AsyncRead) -> Result<BatchRequest, ParseRequestError> {
|
2020-09-14 18:38:41 +00:00
|
|
|
let mut data = Vec::new();
|
2020-10-16 06:49:22 +00:00
|
|
|
futures_util::pin_mut!(body);
|
2020-09-14 18:38:41 +00:00
|
|
|
body.read_to_end(&mut data)
|
|
|
|
.await
|
|
|
|
.map_err(ParseRequestError::Io)?;
|
2021-07-26 17:32:49 +00:00
|
|
|
Ok(serde_json::from_slice::<BatchRequest>(&data)
|
|
|
|
.map_err(|e| ParseRequestError::InvalidRequest(Box::new(e)))?)
|
2020-03-05 00:39:56 +00:00
|
|
|
}
|
2021-07-26 15:29:52 +00:00
|
|
|
|
2021-07-26 17:32:49 +00:00
|
|
|
/// Receive a GraphQL request from a body as CBOR
|
2021-07-26 15:29:52 +00:00
|
|
|
pub async fn receive_batch_cbor(body: impl AsyncRead) -> Result<BatchRequest, ParseRequestError> {
|
|
|
|
let mut data = Vec::new();
|
|
|
|
futures_util::pin_mut!(body);
|
|
|
|
body.read_to_end(&mut data)
|
|
|
|
.await
|
|
|
|
.map_err(ParseRequestError::Io)?;
|
2021-07-26 17:32:49 +00:00
|
|
|
Ok(serde_cbor::from_slice::<BatchRequest>(&data)
|
|
|
|
.map_err(|e| ParseRequestError::InvalidRequest(Box::new(e)))?)
|
2021-07-26 15:29:52 +00:00
|
|
|
}
|